MCPcopy Index your code
hub / github.com/Gadersd/stable-diffusion-burn

github.com/Gadersd/stable-diffusion-burn @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
282 symbols 570 edges 29 files 5 documented · 2%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Stable-Diffusion-Burn

Stable-Diffusion-Burn is a Rust-based project which ports the V1 stable diffusion model into the deep learning framework, Burn. This repository is licensed under the MIT Licence.

How To Use

Step 0: Install libtorch v2.4.1

Step 1: Download the Model and Set Environment Variables

Start by downloading the SDv1-4 model provided on HuggingFace.

wget https://huggingface.co/Gadersd/Stable-Diffusion-Burn/resolve/main/SDv1-4.mpk

Step 2: Run the Sample Binary

Invoke the sample binary provided in the rust code. By default, torch is used. The WGPU backend is unstable for SD but may work well in the future as burn-wpu is optimized.

# torch (at least 6 GB VRAM, possibly less)
# Arguments: <model_type(burn or dump)> <model_name> <unconditional_guidance_scale> <n_diffusion_steps> <prompt> <output_image_name> [cuda, mps, cpu]

# Cuda
cargo run --release --bin sample burn SDv1-4 7.5 20 "An ancient mossy stone." img cuda

# Mps(Mac)
cargo run --release --bin sample burn SDv1-4 7.5 20 "An ancient mossy stone." img mps

# wgpu (UNSTABLE)
# Arguments: <model_type(burn or dump)> <model> <unconditional_guidance_scale> <n_diffusion_steps> <prompt> <output_image>
cargo run --release --features wgpu-backend --bin sample burn SDv1-4 7.5 20 "An ancient mossy stone." img

This command will generate an image according to the provided prompt, which will be saved as 'img0.png'.

An image of an ancient mossy stone

Optional: Extract and Convert a Fine-Tuned Model

If users are interested in using a fine-tuned version of stable diffusion, the Python scripts provided in this project can be used to transform a weight dump into a Burn model file. This does not work on Windows.

# Step into the Python directory
cd python

# Download the model, this is just the base v1.4 model as an example
wget https://huggingface.co/CompVis/stable-diffusion-v-1-4-original/resolve/main/sd-v1-4.ckpt

# Install tinygrad
pip install -r requirements.txt

# Extract the weights
CPU=1 python3 dump.py sd-v1-4.ckpt

# Move the extracted weight folder out
mv params ..

# Step out of the Python directory
cd ..

# Convert the weights into a usable form
cargo run --release --bin convert params SDv1-4

The binaries 'convert' and 'sample' are contained in Rust. Convert works on CPU whereas sample needs CUDA.

Remember, convert should be used if you're planning on using the fine-tuned version of the stable diffusion.

License

This project is licensed under MIT license.

We wish you a productive time using this project. Enjoy!

Extension points exported contracts — how you extend this code

UNetBlock (Interface)
(no doc) [7 implementers]
src/model/unet/mod.rs

Core symbols most depended-on inside this repo

save_conv2d
called by 24
python/save.py
save_tensor
called by 16
python/save.py
load_conv2d
called by 16
src/model/load.rs
save_scalar
called by 15
python/save.py
save_linear
called by 15
python/save.py
save_res_transformer
called by 13
python/unet.py
save_res_block
called by 9
python/unet.py
save_group_norm
called by 9
python/save.py

Shape

Function 113
Class 87
Method 81
Interface 1

Languages

Rust55%
Python45%

Modules by API surface

python/dump.py76 symbols
src/model/unet/mod.rs32 symbols
src/model/autoencoder/mod.rs25 symbols
src/model/unet/load.rs15 symbols
python/unet.py15 symbols
src/model/stablediffusion/mod.rs12 symbols
src/model/clip/mod.rs12 symbols
src/tokenizer.rs11 symbols
src/model/load.rs10 symbols
python/tokenizer.py10 symbols
src/model/autoencoder/load.rs9 symbols
python/save.py9 symbols

For agents

$ claude mcp add stable-diffusion-burn \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact