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 / softmax.py
Created April 3, 2023 00:12
Typical softmax
from torch import FloatTensor
def softmax(x: FloatTensor, dim=-1) -> FloatTensor:
maxes = x.max(dim, keepdim=True).values
diffs = x-maxes
x_exp = diffs.exp()
x_exp_sum = x_exp.sum(dim, keepdim=True)
quotient = x_exp/x_exp_sum
return quotient
@Birch-san
Birch-san / gist:0c36d228e1d4b881a06d1c6e5289d569
Created March 9, 2023 22:49
xformers attention bias limitations
No operator found for `memory_efficient_attention_forward` with inputs:
query : shape=(32, 4096, 1, 40) (torch.float16)
key : shape=(32, 77, 1, 40) (torch.float16)
value : shape=(32, 77, 1, 40) (torch.float16)
attn_bias : <class 'torch.Tensor'>
p : 0.0
`cutlassF` is not supported because:
attn_bias.shape[-1] % 8 != 0
`flshattF` is not supported because:
attn_bias type is <class 'torch.Tensor'>
@Birch-san
Birch-san / cfg_video.sh
Created March 5, 2023 01:08
ffmpeg: side-by-side comparison video
#!/usr/bin/env bash
# makes a video like:
# https://twitter.com/Birchlabs/status/1632185133435437057
# expects folders with filenames such as:
# /home/birch/git/diffusers-play/out_dynthreshoff/00000.679566949.cfg07.50.png
i=0
CFG_COUNTER=''
FONT='fontfile=/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'

Mahouko prompts used for latent walk (WD 1.5 beta 1)

kimono + bridge + river

anime

positive prompt:

beautiful, 1girl, a smiling and winking girl, wearing a dark kimono, taking a selfie on a bridge over a river. detailed hair, portrait, floating hair, anime, carnelian, best aesthetic, best quality, ribbon, outdoors, good posture, watercolor (medium), traditional media, ponytail'
@Birch-san
Birch-san / slerp.py
Last active June 11, 2025 03:15
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