Skip to main content

pydiffuse

CI Python versions License: MIT uv Ruff Checked with pyright

A python library for generating media with diffusion.

Development

The project uses uv. With it installed:

git clone git@github.com:samirelanduk/pydiffuse.git
cd pydiffuse
uv sync
pre-commit install

uv sync creates the virtualenv in .venv and installs the project with its dev dependencies from the lockfile. pre-commit install sets up the ruff check and format hooks, which CI also enforces.

Then, to run the checks:

uv run python -m unittest discover
uv run pyright
uv run pre-commit run --all-files

CLI

The library exposes most of its functionality as a Command-Line Interface.

pydiffuse <command>

CLIP

CLIP turns a text prompt into a set of vectors that represent its semantic meaning, which diffusion models use to condition image generation. The process is split into three commands - tokenising, embedding, and encoding - each of which writes its output to a file that the next one reads. See docs/clip.md for an explanation of what each stage does.

The embedding and encoding steps need model weights in safetensors format - typically a Stable Diffusion checkpoint, whose CLIP text encoder tensors are stored under cond_stage_model.

Tokenising

tokenize converts a prompt into the integer token IDs that CLIP uses.

pydiffuse tokenize "a photo of a lighthouse"

This writes two files. tokens.json is a list of lists of token IDs, where each inner list is a chunk of exactly 77 tokens - prompts longer than that are split over as many chunks as they need, and the last chunk is padded:

[[49406, 320, 1125, 539, 320, 13717, 49407, 49407, ...]]

mappings.json has the same shape, but pairs each token ID with the substring it came from, so you can see how the prompt was split up:

[[["<|startoftext|>", 49406], ["a</w>", 320], ["photo</w>", 1125], ...]]
Option Default Description
--tokens tokens.json Path to save the tokens JSON to.
--mappings mappings.json Path to save the mappings JSON to.
--tokenizer bundled tokenizer Directory containing a custom CLIP tokenizer.

The library bundles the standard CLIP tokenizer, so --tokenizer is only needed if your model was trained with a different vocabulary.

Embedding

embed looks up the vector for each token, and adds the vector for its position in the chunk. The result represents each token in isolation, with no context from the rest of the prompt.

pydiffuse embed tokens.json model.safetensors

This writes embedding.pt, a torch.save-d tensor of shape (chunks, 77, width), where width is the embedding width of the model (768 for Stable Diffusion 1.x). The model must contain tensors whose keys end in token_embedding.weight and position_embedding.weight, and it must use the same vocabulary as the tokenizer that produced the tokens.

Option Default Description
--embedding embedding.pt Path to save the embedding to.

Encoding

encode runs the embeddings through the CLIP transformer layers, so that each vector is adjusted by the tokens before it. This is the conditioning that gets fed to a diffusion model.

pydiffuse encode embedding.pt model.safetensors

This writes conditioning.pt, a tensor of the same shape as the embedding. The model must contain the attention, MLP and layer norm tensors for every encoder layer under cond_stage_model, plus the final layer norm - any number of layers is supported.

Option Default Description
--conditioning conditioning.pt Path to save the conditioning to.

Metadata

Release files for pydiffuse 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydiffuse 0.1.0
File Size Uploaded
pydiffuse-0.1.0.tar.gz 611.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydiffuse 0.1.0
File Interpreter ABI Platform
pydiffuse-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 1.2 MB

Release files / pydiffuse-0.1.0.tar.gz

Download URL pydiffuse-0.1.0.tar.gz
Size 611.9 kB
Tags Source
SHA-256 checksum
How to use checksums
902d36eae22ac31adf4a0d07bed863efdf8390687fd545389664db4a471ae5e5
BLAKE2b-256 checksum
How to use checksums
649638be5498b3481355bab1ca31596b2ac5ffb38d125e528836aeca79f51c61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.19

Release files / pydiffuse-0.1.0-py3-none-any.whl

Download URL pydiffuse-0.1.0-py3-none-any.whl
Size 634.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
27711d47bce6148e424e1005396a0f3a38607e8aa6db2bdd7e20b8be478fe377
BLAKE2b-256 checksum
How to use checksums
51bdc864331fadbe4ffb1e472dc0bd2ddee644fa9529aa73b14790ef788c50b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.19

Release history Release notifications | RSS feed

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page