Skip to content

Instantly share code, notes, and snippets.

View Birch-san's full-sized avatar

Birch-san

View GitHub Profile
@Birch-san
Birch-san / slerp.py
Last active February 15, 2025 15:12
PyTorch implementation of spherical linear interpolation
from torch import FloatTensor, LongTensor, Tensor, Size, lerp, zeros_like
from torch.linalg import norm
# adapted to PyTorch from:
# https://gist.github.com/dvschultz/3af50c40df002da3b751efab1daddf2c
# most of the extra complexity is to support:
# - many-dimensional vectors
# - v0 or v1 with last dim all zeroes, or v0 ~colinear with v1
# - falls back to lerp()
# - conditional logic implemented with parallelism rather than Python loops
@Birch-san
Birch-san / README.md
Last active April 26, 2023 20:05
Using diffusers + pytorch + torchvision with Python 3.11 on Linux with Conda and CUDA 11.8 on Ubuntu 22.10 with Nvidia driver 525
@Birch-san
Birch-san / zfs-home-encryption-ubuntu.md
Last active February 20, 2025 00:12
ZFS home encryption Ubuntu 22.10

I started with a basic Ubuntu 22.10 installation, where I chose in the installer to use ZFS as my volume manager.
I wanted to encrypt my home folder.

I followed the article (and comments, including Christoph Hagemann's) from:
https://talldanestale.dk/2020/04/06/zfs-and-homedir-encryption/

To achieve:

  • Home directory (a ZFS rpool mount) is encrypted
  • You are only prompted for password if you are trying to login to that user
@Birch-san
Birch-san / separated_matmul.py
Created January 19, 2023 23:07
Matrix multiplication, by computing mantissae and exponents separately
# suppose we want to matmul some matrix against some column vector..
# the usual way is this:
mat = np.array([[1.3e2, 8.e2], [1.6e1, 5.e-1]])
vec = np.array([2.1e2, 3.6e-4])
mat @ vec
array([27300.288 , 3360.00018])
# but what if we wanted to exploit some efficiencies..
# - addition can use less cycles / energy / silicon / time than multiplication
# - for machine learning training: we want to represent a wide range of exponents, but don't need such range on the mantissa
@Birch-san
Birch-san / gist:4a85dcfba2923547cd52527b89fe1203
Created November 19, 2022 19:10
Asking justinpinkney how stable-diffusion image variations works (i.e. how to finetune SD to condition on CLIP image embeddings)
Screenshots in comments below
https://canary.discord.com/channels/930499730843250783/950914873800396841/1026450454068084798
@Birch-san
Birch-san / dynthresh.py
Last active November 6, 2022 18:39
Dynamic thresholding of stable-diffusion latents, by referring to known-good CFG7.5's dynamic range
from torch import Tensor, FloatTensor
from typing import Protocol, Optional
from k_diffusion.external import CompVisDenoiser
from k_diffusion.sampling import sample_heun
class DiffusionModel(Protocol):
def __call__(self, x: Tensor, sigma: Tensor, **kwargs) -> Tensor: ...
class DiffusionModelMixin(DiffusionModel):
inner_model: DiffusionModel
@Birch-san
Birch-san / benchmark2.py
Created November 5, 2022 01:02
benchmark: batched matmul
import torch
from torch import einsum, matmul, bmm
import time
repeats = 10
batch_duration = 0
for ix in range(repeats):
attn = torch.rand(16, 4096, 4096, dtype=torch.float, device="mps")
v = torch.rand(16, 4096, 40, dtype=torch.float, device="mps")
@Birch-san
Birch-san / benchmark.py
Last active November 22, 2022 21:10
benchmark: batched matmul with scale factor
import torch
from torch import einsum, tensor, matmul, bmm, baddbmm, empty
import time
scale=2
repeats = 10
# both einsum 0s use the same plan, so whichever batch runs first has to pay the price of warmup
# uncomment this to run a warmup before either batch runs, for fairer comparison of batch avg time
# q = torch.rand(16, 4096, 40, dtype=torch.float, device="mps")
@Birch-san
Birch-san / histogram.py
Created October 17, 2022 00:12
plotting da histograms (partial snippet from Jupyter notebook)
import matplotlib.pyplot as plt
# …
latents: FloatTensor = self.inner_model(x, sigma, cond=cond, **kwargs)
unscaled: Tensor = latents / self.scale_factor
chs = [torch.histogram(c) for c in unscaled[0].flatten(1)]
h = torch.histogram(unscaled[0].ravel())
plt.figure(figsize=(10,2))
plt.title('Per-channel latent values after denoising sigma %.3f at CFG scale %d' % (sigma.item(), cfg_scale))
@Birch-san
Birch-san / gist:f4ae5c843f2f31319cabd9b40c94e4be
Created October 10, 2022 18:20
denoised latents returned by k-diffusion callback at each sampler step (8 steps, Heun, sigma_min=0.092)
CFG20
sigma: 14.615 absmax: 6.942 std: 2.517 min: -6.840 max: 6.942 shape: [1, 4, 64, 64]
sigma: 8.560 absmax: 16.164 std: 3.112 min: -16.164 max: 12.644 shape: [1, 4, 64, 64]
sigma: 8.560 absmax: 28.488 std: 4.065 min: -19.071 max: 28.488 shape: [1, 4, 64, 64]
sigma: 4.797 absmax: 12.463 std: 1.584 min: -10.655 max: 12.463 shape: [1, 4, 64, 64]
sigma: 4.797 absmax: 24.024 std: 1.559 min: -24.024 max: 11.797 shape: [1, 4, 64, 64]
sigma: 2.551 absmax: 5.675 std: 1.151 min: -5.675 max: 4.937 shape: [1, 4, 64, 64]
sigma: 2.551 absmax: 10.035 std: 1.201 min: -10.035 max: 5.773 shape: [1, 4, 64, 64]
sigma: 1.274 absmax: 4.725 std: 1.095 min: -3.790 max: 4.725 shape: [1, 4, 64, 64]
sigma: 1.274 absmax: 4.997 std: 1.104 min: -4.997 max: 4.111 shape: [1, 4, 64, 64]