Diffusers BOINC AI docs
  • 🌍GET STARTED
    • Diffusers
    • Quicktour
    • Effective and efficient diffusion
    • Installation
  • 🌍TUTORIALS
    • Overview
    • Understanding models and schedulers
    • AutoPipeline
    • Train a diffusion model
  • 🌍USING DIFFUSERS
    • 🌍LOADING & HUB
      • Overview
      • Load pipelines, models, and schedulers
      • Load and compare different schedulers
      • Load community pipelines
      • Load safetensors
      • Load different Stable Diffusion formats
      • Push files to the Hub
    • 🌍TASKS
      • Unconditional image generation
      • Text-to-image
      • Image-to-image
      • Inpainting
      • Depth-to-image
    • 🌍TECHNIQUES
      • Textual inversion
      • Distributed inference with multiple GPUs
      • Improve image quality with deterministic generation
      • Control image brightness
      • Prompt weighting
    • 🌍PIPELINES FOR INFERENCE
      • Overview
      • Stable Diffusion XL
      • ControlNet
      • Shap-E
      • DiffEdit
      • Distilled Stable Diffusion inference
      • Create reproducible pipelines
      • Community pipelines
      • How to contribute a community pipeline
    • 🌍TRAINING
      • Overview
      • Create a dataset for training
      • Adapt a model to a new task
      • Unconditional image generation
      • Textual Inversion
      • DreamBooth
      • Text-to-image
      • Low-Rank Adaptation of Large Language Models (LoRA)
      • ControlNet
      • InstructPix2Pix Training
      • Custom Diffusion
      • T2I-Adapters
    • 🌍TAKING DIFFUSERS BEYOND IMAGES
      • Other Modalities
  • 🌍OPTIMIZATION/SPECIAL HARDWARE
    • Overview
    • Memory and Speed
    • Torch2.0 support
    • Stable Diffusion in JAX/Flax
    • xFormers
    • ONNX
    • OpenVINO
    • Core ML
    • MPS
    • Habana Gaudi
    • Token Merging
  • 🌍CONCEPTUAL GUIDES
    • Philosophy
    • Controlled generation
    • How to contribute?
    • Diffusers' Ethical Guidelines
    • Evaluating Diffusion Models
  • 🌍API
    • 🌍MAIN CLASSES
      • Attention Processor
      • Diffusion Pipeline
      • Logging
      • Configuration
      • Outputs
      • Loaders
      • Utilities
      • VAE Image Processor
    • 🌍MODELS
      • Overview
      • UNet1DModel
      • UNet2DModel
      • UNet2DConditionModel
      • UNet3DConditionModel
      • VQModel
      • AutoencoderKL
      • AsymmetricAutoencoderKL
      • Tiny AutoEncoder
      • Transformer2D
      • Transformer Temporal
      • Prior Transformer
      • ControlNet
    • 🌍PIPELINES
      • Overview
      • AltDiffusion
      • Attend-and-Excite
      • Audio Diffusion
      • AudioLDM
      • AudioLDM 2
      • AutoPipeline
      • Consistency Models
      • ControlNet
      • ControlNet with Stable Diffusion XL
      • Cycle Diffusion
      • Dance Diffusion
      • DDIM
      • DDPM
      • DeepFloyd IF
      • DiffEdit
      • DiT
      • IF
      • PaInstructPix2Pix
      • Kandinsky
      • Kandinsky 2.2
      • Latent Diffusionge
      • MultiDiffusion
      • MusicLDM
      • PaintByExample
      • Parallel Sampling of Diffusion Models
      • Pix2Pix Zero
      • PNDM
      • RePaint
      • Score SDE VE
      • Self-Attention Guidance
      • Semantic Guidance
      • Shap-E
      • Spectrogram Diffusion
      • 🌍STABLE DIFFUSION
        • Overview
        • Text-to-image
        • Image-to-image
        • Inpainting
        • Depth-to-image
        • Image variation
        • Safe Stable Diffusion
        • Stable Diffusion 2
        • Stable Diffusion XL
        • Latent upscaler
        • Super-resolution
        • LDM3D Text-to-(RGB, Depth)
        • Stable Diffusion T2I-adapter
        • GLIGEN (Grounded Language-to-Image Generation)
      • Stable unCLIP
      • Stochastic Karras VE
      • Text-to-image model editing
      • Text-to-video
      • Text2Video-Zero
      • UnCLIP
      • Unconditional Latent Diffusion
      • UniDiffuser
      • Value-guided sampling
      • Versatile Diffusion
      • VQ Diffusion
      • Wuerstchen
    • 🌍SCHEDULERS
      • Overview
      • CMStochasticIterativeScheduler
      • DDIMInverseScheduler
      • DDIMScheduler
      • DDPMScheduler
      • DEISMultistepScheduler
      • DPMSolverMultistepInverse
      • DPMSolverMultistepScheduler
      • DPMSolverSDEScheduler
      • DPMSolverSinglestepScheduler
      • EulerAncestralDiscreteScheduler
      • EulerDiscreteScheduler
      • HeunDiscreteScheduler
      • IPNDMScheduler
      • KarrasVeScheduler
      • KDPM2AncestralDiscreteScheduler
      • KDPM2DiscreteScheduler
      • LMSDiscreteScheduler
      • PNDMScheduler
      • RePaintScheduler
      • ScoreSdeVeScheduler
      • ScoreSdeVpScheduler
      • UniPCMultistepScheduler
      • VQDiffusionScheduler
Powered by GitBook
On this page
  • Token Merging
  • Using ToMe
  • Benchmarking tomesd with StableDiffusionPipeline
  • Quality
  1. OPTIMIZATION/SPECIAL HARDWARE

Token Merging

PreviousHabana GaudiNextCONCEPTUAL GUIDES

Last updated 1 year ago

Token Merging

Token Merging (introduced in ) works by merging the redundant tokens / patches progressively in the forward pass of a Transformer-based network. It can speed up the inference latency of the underlying network.

After Token Merging (ToMe) was released, the authors released , which introduced a version of ToMe which is more compatible with Stable Diffusion. We can use ToMe to gracefully speed up the inference latency of a . This doc discusses how to apply ToMe to the , the expected speedups, and the qualitative aspects of using ToMe on the .

Using ToMe

The authors of ToMe released a convenient Python library called that lets us apply ToMe to a like so:

Copied

from diffusers import StableDiffusionPipeline
import tomesd

pipeline = StableDiffusionPipeline.from_pretrained(
      "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16
).to("cuda")
+ tomesd.apply_patch(pipeline, ratio=0.5)

image = pipeline("a photo of an astronaut riding a horse on mars").images[0]

And that’s it!

tomesd.apply_patch() exposes to let us strike a balance between the pipeline inference speed and the quality of the generated tokens. Amongst those arguments, the most important one is ratio. ratio controls the number of tokens that will be merged during the forward pass. For more details on tomesd, please refer to the original repository and .

Benchmarking tomesd with StableDiffusionPipeline

Copied

- `diffusers` version: 0.15.1
- Python version: 3.8.16
- PyTorch version (GPU?): 1.13.1+cu116 (True)
- Huggingface_hub version: 0.13.2
- Transformers version: 4.27.2
- Accelerate version: 0.18.0
- xFormers version: 0.0.16
- tomesd version: 0.1.2

A100

Resolution
Batch size
Vanilla
ToMe
ToMe + xFormers
ToMe speedup (%)
ToMe + xFormers speedup (%)

512

10

6.88

5.26

4.69

23.54651163

31.83139535

768

10

OOM

14.71

11

8

OOM

11.56

8.84

4

OOM

5.98

4.66

2

4.99

3.24

3.1

35.07014028

37.8757515

1

3.29

2.24

2.03

31.91489362

38.29787234

1024

10

OOM

OOM

OOM

8

OOM

OOM

OOM

4

OOM

12.51

9.09

2

OOM

6.52

4.96

1

6.4

3.61

2.81

43.59375

56.09375

The timings reported here are in seconds. Speedups are calculated over the Vanilla timings.

V100

Resolution
Batch size
Vanilla
ToMe
ToMe + xFormers
ToMe speedup (%)
ToMe + xFormers speedup (%)

512

10

OOM

10.03

9.29

8

OOM

8.05

7.47

4

5.7

4.3

3.98

24.56140351

30.1754386

2

3.14

2.43

2.27

22.61146497

27.70700637

1

1.88

1.57

1.57

16.4893617

16.4893617

768

10

OOM

OOM

23.67

8

OOM

OOM

18.81

4

OOM

11.81

9.7

2

OOM

6.27

5.2

1

5.43

3.38

2.82

37.75322284

48.06629834

1024

10

OOM

OOM

OOM

8

OOM

OOM

OOM

4

OOM

OOM

19.35

2

OOM

13

10.78

1

OOM

6.66

5.54

As seen in the tables above, the speedup with tomesd becomes more pronounced for larger image resolutions. It is also interesting to note that with tomesd, it becomes possible to run the pipeline on a higher resolution, like 1024x1024.

Quality

We didn’t notice any significant decrease in the quality of the generated samples. Here are samples:

We benchmarked the impact of using tomesd on along with across different image resolutions. We used A100 and V100 as our test GPU devices with the following development environment (with Python 3.8.5):

We used this script for benchmarking: . Following are our findings:

It might be possible to speed up inference even further with .

As reported in , ToMe can preserve the quality of the generated images to a great extent while speeding up inference. By increasing the ratio, it is possible to further speed up inference, but that might come at the cost of a deterioration in the image quality.

To test the quality of the generated samples using our setup, we sampled a few prompts from the β€œParti Prompts” (introduced in ) and performed inference with the in the following settings:

Vanilla

+ ToMe

+ ToMe + xformers

tome-samples

You can check out the generated samples . We used for conducting this experiment.

🌍
Token Merging: Your ViT But Faster
Token Merging for Fast Stable Diffusion
DiffusionPipeline
StableDiffusionPipeline
StableDiffusionPipeline
tomesd
DiffusionPipeline
a number of arguments
https://github.com/dbolya/tomesd
the paper
StableDiffusionPipeline
xformers
https://gist.github.com/sayakpaul/27aec6bca7eb7b0e0aa4112205850335
torch.compile()
the paper
Parti
StableDiffusionPipeline
StableDiffusionPipeline
StableDiffusionPipeline
StableDiffusionPipeline
here
this script