Registry / ai-ml / tomesd

tomesd

JSON →
library0.1.3pypypi✓ verified 89d ago

tomesd is a Python library that implements Token Merging (ToMe) for Stable Diffusion models. It accelerates inference by merging redundant tokens, reducing the computational load on the transformer blocks without requiring model retraining. The library is pure Python and PyTorch-based, currently at version 0.1.3, with an active development and release cadence focused on performance improvements and compatibility.

pip install tomesd
INSTALL
IMPORT
SIG · TOMESD
T
tomesd
ai-mlpythonv0.1.3
Install
65.9s avg
Import
5405ms
Disk
4787MB
Pass rate
4/ 10
Env Coverage4 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.1.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
glibc
py 3.10
✕ build_error
✓ 74.2s
py 3.11
✕ build_error
✓ 67.65s
py 3.12
✕ build_error
✓ 63.83s
py 3.13
✕ build_error
✓ 57.75s
py 3.9
✕ build_error
1/4 runs
4787MB installed
● package 4787MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

tomesd
✓ import tomesd
✗ from tomesd import apply_patch
The primary interaction is through the 'tomesd' module, typically calling `tomesd.apply_patch()` on a model.
apply_patch
✓ tomesd.apply_patch(model, ratio=0.5)
✗ from tomesd import apply_patch; apply_patch(model, ratio=0.5)
While importing `apply_patch` directly might work, the standard and documented approach is to import `tomesd` and call `tomesd.apply_patch()`. This avoids potential naming conflicts and aligns with examples.

This quickstart demonstrates how to apply `tomesd` to a `diffusers` Stable Diffusion pipeline. It loads a pre-trained model, applies the `tomesd` patch with a specified merging ratio, and then generates an image. The `ratio` parameter is key to balancing speedup and image quality.

import torch, tomesd from diffusers import StableDiffusionPipeline import os # Ensure you have a Hugging Face token set up if accessing private models # For public models, this is often not strictly needed, but good practice # Replace 'YOUR_HF_TOKEN' with an actual token or use environment variable hf_token = os.environ.get('HF_TOKEN', '') # 1. Load a Stable Diffusion pipeline pipeline = StableDiffusionPipeline.from_pretrained( "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, # Pass token if required for private models # use_auth_token=hf_token if hf_token else None ).to("cuda") # 2. Apply ToMe with a 50% merging ratio # The 'ratio' parameter controls the amount of token merging. Higher ratio means more speedup, potentially lower quality. tomesd.apply_patch(pipeline, ratio=0.5) # 3. Generate an image prompt = "a photo of an astronaut riding a horse on mars" image = pipeline(prompt).images[0] # 4. Save the image image.save("astronaut.png") print("Image generated and saved as astronaut.png")
Debug
Known issues
gotchaPrior to v0.1.3, `tomesd`'s random perturbations could affect the global torch seed, potentially leading to inconsistencies in image generation if not explicitly managed. [cite: v0.1.3]
fix
Upgrade to `tomesd` v0.1.3 or later. If using older versions, ensure a separate RNG or consistent seed setting for each batch if reproducibility is critical.
affects: < 0.1.3
gotchaWhen `use_rand` is enabled, odd batch sizes (where prompted and unprompted images are not in the same batch) could lead to artifacting. [cite: v0.1.2]
fix
Upgrade to `tomesd` v0.1.2 or later, which automatically disables `use_rand` for odd batch sizes to prevent this issue. Alternatively, ensure an even batch size.
affects: < 0.1.2
gotcha`tomesd` is a lossy process, meaning applying it will subtly change the generated image. While designed to minimize quality loss, aggressive merging (higher `ratio`) can degrade image quality.
fix
Experiment with different `ratio` values (e.g., 0.3 to 0.6) to find a balance between speedup and acceptable image quality for your specific use case.
affects: All versions
gotchaExpected speedups from `tomesd` can vary significantly based on image resolution, batch size, and the underlying Stable Diffusion implementation (e.g., `diffusers` vs. original `runway-ml` repo). Smaller images or pipelines with existing optimizations (like `xformers` or `torch.compile`) might show less dramatic gains.
fix
Benchmark performance with your specific setup and image sizes. `tomesd` generally provides more substantial speedups for larger image resolutions (e.g., 1024x1024 and above) and typically works well in conjunction with other optimizations.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'tomesd'
The `tomesd` package is not installed in the Python environment currently being used by your Stable Diffusion application or script.
fix
Ensure `tomesd` is installed correctly in the active Python environment: `pip install tomesd`. If using a virtual environment (e.g., `venv`, `conda`), activate it before installing.
Failed to apply ToMe patch, continuing as normal module 'tomesd' has no attribute 'apply_patch'
This usually indicates an incomplete or corrupted installation, or an incorrect import. The `tomesd` module was found, but `apply_patch` is missing or not accessible. This could happen if a partial or incorrect `tomesd` directory exists in the Python path.
fix
Try reinstalling `tomesd`: `pip uninstall tomesd` followed by `pip install tomesd`. If installing from source, ensure `python setup.py build develop` was run successfully. Double-check the import statement is `import tomesd` before calling `tomesd.apply_patch()`.
RuntimeError: The size of tensor a (X) must match the size of tensor b (Y) at non-singleton dimension Z
This error often occurs when `tomesd` is applied to models generating at certain non-standard resolutions that cause tensor dimension mismatches during the token merging process. Issues with specific resolutions (e.g., 1920x1080) were reported early on.
fix
Upgrade `tomesd` to the latest version, as compatibility with more resolutions was added in v0.1.1. If the problem persists, try adjusting image dimensions to multiples of 16 or using common Stable Diffusion resolutions like 512x512 or 768x768 for better compatibility. [cite: v0.1.1, 10]
Lower than expected speedup or even slowdown when using tomesd.
This can occur on smaller image resolutions, with specific GPU architectures, or when other powerful optimizations (like `xformers` or `torch.compile` using SDPA) are already heavily optimizing the pipeline. The overhead of token merging might outweigh its benefits in such scenarios.
fix
Verify that `tomesd` is actually being called (e.g., add print statements). Ensure you are testing with larger image resolutions (e.g., 1024x1024) where `tomesd` typically provides more significant gains. Consider disabling other transformer optimizations temporarily to isolate the effect of `tomesd` and then re-evaluate.
Upgrade
Version history
0.1.3latest on PyPI · released May 14, 2023
Audit
Dependencies
torchrequiredRequired for underlying tensor operations and model patching. PyTorch >= 1.12.1 is needed for `scatter_reduce` functionality.
diffusersoptionalCommonly used framework for Stable Diffusion models, which tomesd is designed to optimize. The quickstart heavily features its integration.
Agent activity
40 hits · last 30 days
node
36
OpenAI (training)
1
Resources