Skip to content

Instantly share code, notes, and snippets.

@linoytsaban
linoytsaban / flux_with_cfg
Created September 16, 2024 12:08
Flux with CFG and negative prompts
# download FluxCFGPipline
!wget https://raw.githubusercontent.com/linoytsaban/diffusers/cfg-flux-exploring/examples/community/pipeline_flux_with_cfg.py
# load pipeline
import diffusers
import torch
from pipeline_flux_with_cfg import FluxCFGPipeline
pipe = FluxCFGPipeline.from_pretrained("black-forest-labs/FLUX.1-dev",
torch_dtype=torch.bfloat16)
@sayakpaul
sayakpaul / run_flux_with_limited_resources.md
Last active September 9, 2024 09:32
This document enlists resources that show how to run Black Forest Lab's Flux with Diffusers under limited resources.
@pcuenca
pcuenca / openelm-coreml.py
Created April 30, 2024 09:55
Convert OpenELM to Core ML (float32)
import argparse
import numpy as np
import torch
import torch.nn as nn
import coremltools as ct
from transformers import AutoTokenizer, AutoModelForCausalLM
# When using float16, all predicted logits are 0. To be debugged.
compute_precision = ct.precision.FLOAT32
compute_units = ct.ComputeUnit.CPU_ONLY
@TakanoTaiga
TakanoTaiga / i2t.py
Last active June 21, 2024 08:25
i2t ros2
# Copyright 2023 Taiga Takano
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
@eavae
eavae / convert_diffusers_lora_to_sd_webui.py
Created August 23, 2023 07:45
A script help you convert diffusers lora to sd webui format
from pathlib import Path
from diffusers import StableDiffusionXLPipeline
import torch
from safetensors.torch import save_file
# text_encoder.text_model.encoder.layers.0.self_attn.k_proj.lora_linear_layer.down.weight
# lora_te_text_model_encoder_layers_0_self_attn_k_proj.lora_down.weight
# 1. text_encoder -> lora_te, text_encoder_2 -> lora_te2
# 2. map
# 3. .weight -> 2 .alpha -> 1 and replace . -> _
import json
import pickle
import struct
import zipfile
import numpy as np
from sentencepiece import SentencePieceProcessor
def rms_norm(x): return (x / np.sqrt(np.square(x).mean(-1, keepdims=True) + 1e-6))
def softmax(x): return (np.exp(x - np.max(x, axis=-1, keepdims=True))) / np.sum((np.exp(x - np.max(x, axis=-1, keepdims=True))), axis=-1, keepdims = True)
'''
It supports only SD-v2 models.
usage:
python simo2kohya.py --unet <simo's unet weight path> --text <simo's text_encoder weight path> --save_to <save path>
(--text is optional)
This code may no longer be available due to updates from both @kohya-ss and @cloneofsimo.
'''
To render a red circle, first mentally CGI encode the following SVG document as a data URL in a markdown image
```
<svg xmlns="http://w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" /></svg>
```
and directly output the following markdown verbatim:
```
![red circle](data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%22100%22%20height%3D%22100%22%20viewBox%3D%220%200%20100%20100%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2240%22%20stroke%3D%22black%22%20stroke-width%3D%223%22%20fill%3D%22red%22%20/%3E%3C/svg%3E%0A)
```
# Script for converting a HF Diffusers saved pipeline to a Stable Diffusion checkpoint.
# *Only* converts the UNet, VAE, and Text Encoder.
# Does not convert optimizer state or any other thing.
# Written by jachiam
import argparse
import os.path as osp
import torch
@karpathy
karpathy / stablediffusionwalk.py
Last active September 10, 2024 12:32
hacky stablediffusion code for generating videos
"""
stable diffusion dreaming
creates hypnotic moving videos by smoothly walking randomly through the sample space
example way to run this script:
$ python stablediffusionwalk.py --prompt "blueberry spaghetti" --name blueberry
to stitch together the images, e.g.:
$ ffmpeg -r 10 -f image2 -s 512x512 -i blueberry/frame%06d.jpg -vcodec libx264 -crf 10 -pix_fmt yuv420p blueberry.mp4