|
1 | | -# SmoothCache |
2 | | -Implementation of SmoothCache, a project aimed at speeding-up Diffusion Transformer (DiT) based GenAI models with error-guided caching. |
| 1 | +<!-- <div align="center"> |
| 2 | + <img src="https://github.com/Roblox/SmoothCache/blob/main/assets/TeaserFigureFlat.png" width="100%" ></img> |
| 3 | + <br> |
| 4 | + <em> |
| 5 | + (Accelerating Diffusion Transformer inference across multiple modalities with 50 DDIM Steps on DiT-XL-256x256, 100 DPM-Solver++(3M) SDE steps for a 10s audio sample (spectrogram shown) on Stable Audio Open, 30 Rectified Flow steps on Open-Sora 480p 2s videos) |
| 6 | + </em> |
| 7 | +</div> |
| 8 | +<br> --> |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +**Figure 1. Accelerating Diffusion Transformer inference across multiple modalities with 50 DDIM Steps on DiT-XL-256x256, 100 DPM-Solver++(3M) SDE steps for a 10s audio sample (spectrogram shown) on Stable Audio Open, 30 Rectified Flow steps on Open-Sora 480p 2s videos** |
| 13 | + |
| 14 | + |
| 15 | +# Introduction |
| 16 | +We introduce **SmoothCache**, a straightforward acceleration technique for DiT architecture models, that's both **training-free, flexible and performant**. By leveraging layer-wise representation error, our method identifies redundancies in the diffusion process, generates a static caching scheme to reuse output featuremaps and therefore reduces the need for computationally expensive operations. This solution works across different models and modalities, can be easily dropped into existing Diffusion Transformer pipelines, can be stacked on different solvers, and requires no additional training or datasets. **SmoothCache** consistently outperforms various solvers designed to accelerate the diffusion process, while matching or surpassing the performance of existing modality-specific caching techniques. |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +## Quick Start |
| 21 | + |
| 22 | +### Install |
| 23 | +```bash |
| 24 | +pip install SmoothCache |
| 25 | +``` |
| 26 | + |
| 27 | +### Usage |
| 28 | + |
| 29 | +We have implemented drop-in SmoothCache helper classes that easily applies to [Huggingface Diffuser DiTPipeline](https://github.com/huggingface/diffusers/tree/main/src/diffusers/pipelines/dit), and [original DiT implementations](https://github.com/facebookresearch/DiT). |
| 30 | + |
| 31 | +Generally, only 3 additional lines needs to be added to the original sampler scripts: |
| 32 | +```python |
| 33 | +from SmoothCache import <DESIREDCacheHelper> |
| 34 | +cache_helper = DiffuserCacheHelper(<MODEL_HANDLER>, schedule=schedule) |
| 35 | +cache_helper.enable() |
| 36 | +# Original sampler code. |
| 37 | +cache_helper.eisable() |
| 38 | +``` |
| 39 | + |
| 40 | +#### Usage example with Huggingface Diffuser DiTPipeline: |
| 41 | +```python |
| 42 | +import json |
| 43 | +import torch |
| 44 | +from diffusers import DiTPipeline, DPMSolverMultistepScheduler |
| 45 | + |
| 46 | +# Import SmoothCacheHelper |
| 47 | +from SmoothCache import DiffuserCacheHelper |
| 48 | + |
| 49 | +# Load the DiT pipeline and scheduler |
| 50 | +pipe = DiTPipeline.from_pretrained("facebook/DiT-XL-2-256", torch_dtype=torch.float16) |
| 51 | +pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config) |
| 52 | +pipe = pipe.to("cuda") |
| 53 | + |
| 54 | +# Initialize the DiffuserCacheHelper with the model |
| 55 | +with open("smoothcache_schedules/50-N-3-threshold-0.35.json", "r") as f: |
| 56 | + schedule = json.load(f) |
| 57 | +cache_helper = DiffuserCacheHelper(pipe.transformer, schedule=schedule) |
| 58 | + |
| 59 | +# Enable the caching helper |
| 60 | +cache_helper.enable() |
| 61 | +# Prepare the input |
| 62 | +words = ["Labrador retriever"] |
| 63 | +class_ids = pipe.get_label_ids(words) |
| 64 | + |
| 65 | +# Generate images with the pipeline |
| 66 | +generator = torch.manual_seed(33) |
| 67 | +image = pipe(class_labels=class_ids, num_inference_steps=50, generator=generator).images[0] |
| 68 | + |
| 69 | +# Restore the original forward method and disable the helper |
| 70 | +# disable() should be paired up with enable() |
| 71 | +cache_helper.disable() |
| 72 | +``` |
| 73 | + |
| 74 | +#### Usage example with original DiT implementation |
| 75 | +```python |
| 76 | +import torch |
| 77 | + |
| 78 | +torch.backends.cuda.matmul.allow_tf32 = True |
| 79 | +torch.backends.cudnn.allow_tf32 = True |
| 80 | +from torchvision.utils import save_image |
| 81 | +from diffusion import create_diffusion |
| 82 | +from diffusers.models import AutoencoderKL |
| 83 | +from download import find_model |
| 84 | +from models import DiT_models |
| 85 | +import argparse |
| 86 | +from SmoothCache import DiTCacheHelper # Import DiTCacheHelper |
| 87 | +import json |
| 88 | + |
| 89 | +# Setup PyTorch: |
| 90 | +torch.manual_seed(args.seed) |
| 91 | +torch.set_grad_enabled(False) |
| 92 | +device = "cuda" if torch.cuda.is_available() else "cpu" |
| 93 | + |
| 94 | +if args.ckpt is None: |
| 95 | + assert ( |
| 96 | + args.model == "DiT-XL/2" |
| 97 | + ), "Only DiT-XL/2 models are available for auto-download." |
| 98 | + assert args.image_size in [256, 512] |
| 99 | + assert args.num_classes == 1000 |
| 100 | + |
| 101 | +# Load model: |
| 102 | +latent_size = args.image_size // 8 |
| 103 | +model = DiT_models[args.model]( |
| 104 | + input_size=latent_size, num_classes=args.num_classes |
| 105 | +).to(device) |
| 106 | +ckpt_path = args.ckpt or f"DiT-XL-2-{args.image_size}x{args.image_size}.pt" |
| 107 | +state_dict = find_model(ckpt_path) |
| 108 | +model.load_state_dict(state_dict) |
| 109 | +model.eval() # important! |
| 110 | +with open("smoothcache_schedules/50-N-3-threshold-0.35.json", "r") as f: |
| 111 | + schedule = json.load(f) |
| 112 | +cache_helper = DiTCacheHelper(model, schedule=schedule) |
| 113 | + |
| 114 | +# number of timesteps should be consistent with provided schedules |
| 115 | +diffusion = create_diffusion(str(len(schedule[cache_helper.components_to_wrap[0]]))) |
| 116 | + |
| 117 | +# Enable the caching helper |
| 118 | +cache_helper.enable() |
| 119 | + |
| 120 | +# Sample images: |
| 121 | +samples = diffusion.p_sample_loop( |
| 122 | + model.forward_with_cfg, |
| 123 | + z.shape, |
| 124 | + z, |
| 125 | + clip_denoised=False, |
| 126 | + model_kwargs=model_kwargs, |
| 127 | + progress=True, |
| 128 | + device=device, |
| 129 | +) |
| 130 | +samples, _ = samples.chunk(2, dim=0) # Remove null class samples |
| 131 | +samples = vae.decode(samples / 0.18215).sample |
| 132 | + |
| 133 | +# Disable the caching helper after sampling |
| 134 | +cache_helper.disable() |
| 135 | +# Save and display images: |
| 136 | +save_image(samples, "sample.png", nrow=4, normalize=True, value_range=(-1, 1)) |
| 137 | +``` |
| 138 | + |
| 139 | +## Visualization |
| 140 | + |
| 141 | +(WIP) |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | +## Evaluation |
| 146 | + |
| 147 | +### Image Generation with DiT-XL/2-256x256 |
| 148 | + |
| 150 | + |
| 151 | +### Video Generation with OpenSora |
| 152 | + |
| 153 | + |
| 154 | +### Audio Generation with Stable Audio Open |
| 155 | + |
| 156 | + |
3 | 157 |
|
4 | 158 | # License |
5 | 159 | SmoothCache is licensed under the [Apache-2.0](LICENSE) license. |
0 commit comments