Skip to content

Instantly share code, notes, and snippets.

View takuma104's full-sized avatar

Takuma Mori takuma104

  • Tokyo
View GitHub Profile
@takuma104
takuma104 / scratchpad.ipynb
Last active May 18, 2023 16:02
scratchpad
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import torch
import torch.nn
def test_monkey_patch():
x = torch.randn((2, 2)).cuda()
target = torch.nn.Linear(2, 2).cuda()
with torch.no_grad():
y = target(x)
assert y.shape == (2, 2)
@takuma104
takuma104 / scratchpad.ipynb
Last active May 16, 2023 16:47
scratchpad-lora
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@takuma104
takuma104 / scratchpad.ipynb
Last active May 16, 2023 15:46
scratchpad_mod
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import torch
import safetensors.torch
import sys
def dump_keys(parent, suffix=''):
for k in sorted(parent.keys()):
if isinstance(parent[k], torch.Tensor):
print(f'{suffix}{k} {list(parent[k].shape)}')
else:
dump_keys(parent[k], f'{suffix}{k}.')
from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
from torch
from PIL import Image
def image_grid(imgs, rows, cols):
assert len(imgs) == rows*cols
w, h = imgs[0].size
grid = Image.new('RGB', size=(cols*w, rows*h))
grid_w, grid_h = grid.size
import torch
import sys
from safetensors.torch import load_file
from diffusers import StableDiffusionPipeline
state_dict = load_file('some_lora.safetensors')
new_state_dict = {}
for key, value in state_dict.items():
if "lora_down" in key:
lora_name = key.split(".")[0]
import math
import safetensors
import torch
from diffusers import DiffusionPipeline
"""
Kohya's LoRA format Loader for Diffusers
Usage:
```py
@takuma104
takuma104 / README.md
Last active August 28, 2024 05:03
clip_text_custom_embedder

Usage

from clip_text_custom_embedder import text_embeddings
from diffusers import StableDiffusionPipeline
import torch
# Script for converting a HF Diffusers saved pipeline to a ControlNet checkpoint.
# *Only* converts the ControlNet.
# Does not convert optimizer state or any other thing.
import argparse
import os.path as osp
import re
import torch
from safetensors.torch import load_file, save_file