Skip to main content

DeepCache: Accelerating Diffusion Models for Free


(Results on Stable Diffusion v1.5. Left: 50 PLMS steps. Right: 2.3x acceleration upon 50 PLMS steps)

DeepCache: Accelerating Diffusion Models for Free
Xinyin Ma, Gongfan Fang, Xinchao Wang
Learning and Vision Lab, National University of Singapore
🥯[Arxiv]🎄[Project Page]

Why DeepCache

  • 🚀 Training-free and almost lossless
  • 🚀 Support Stable Diffusion, Stable Diffusion XL, Stable Video Diffusion, DDPM
  • 🚀 Compatible with sampling algorithms like DDIM and PLMS

Updates

  • December 26, 2023: 🔥Update a plug-and-play implementation of DeepCache, no longer requiring any modifications of the diffuser's code! Check here for the detailed usage!
  • December 25, 2023: A demo is available via Colab Open in Colab
  • December 21, 2023: Release the code for Stable Video Diffusion and Text2Video-Zero. In the figure below, the upper row shows the original videos generated by SVD-XT, and the lower row is accelerated by DeepCache. For Text2Video-Zero, the results can be found here

(1.7x acceleration of SVD-XT)
  • December 20, 2023: Release the code for DDPM. See here for the experimental code and instructions.

  • December 6, 2023: Release the code for Stable Diffusion XL. The results of the stabilityai/stable-diffusion-xl-base-1.0 are shown in the below figure, with the same prompts from the first figure.


(2.6x acceleration of Stable Diffusion XL)

Introduction

We introduce DeepCache, a novel training-free and almost lossless paradigm that accelerates diffusion models from the perspective of model architecture. Utilizing the property of the U-Net, we reuse the high-level features while updating the low-level features in a very cheap way. DeepCache accelerates Stable Diffusion v1.5 by 2.3x with only a 0.05 decline in CLIP Score, and LDM-4-G(ImageNet) by 4.1x with a 0.22 decrease in FID.

image

Quick Start

Requirements

pip install transformers diffusers

Usage

import torch

# Loading the original pipeline
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5', torch_dtype=torch.float16).to("cuda:0")

# Import the DeepCacheSDHelper
from DeepCache import DeepCacheSDHelper
helper = DeepCacheSDHelper(pipe=pipe)
helper.set_params(
    cache_interval=3,
    cache_branch_id=0,
)
helper.enable()

# Generate Image
deepcache_image = pipe(
        prompt,
        output_type='pt'
).images[0]
helper.disable()

We here take the Stable Diffusion pipeline as an example. You can replace pipe with any variants of the Stable Diffusion pipeline, including choices like SDXL, SVD, and more. The argument cache_branch_id specifies the selected skip branch. For the skip branches that are deeper, the model will engage them only during the caching steps, and exclude them during the retrieval steps.

You can explore DeepCache by using the command provided below:

python main.py --model_type sdxl #Support [sdxl, sd1.5, svd]

Scripts

Stable Diffusion XL

python stable_diffusion_xl.py --model stabilityai/stable-diffusion-xl-base-1.0
Output:
Loading pipeline components...: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 7/7 [00:01<00:00,  6.62it/s]
2023-12-06 01:44:28,578 - INFO - Running baseline...
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [00:17<00:00,  2.93it/s]
2023-12-06 01:44:46,095 - INFO - Baseline: 17.52 seconds
Loading pipeline components...: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 7/7 [00:00<00:00,  8.06it/s]
2023-12-06 01:45:02,865 - INFO - Running DeepCache...
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [00:06<00:00,  8.01it/s]
2023-12-06 01:45:09,573 - INFO - DeepCache: 6.71 seconds
2023-12-06 01:45:10,678 - INFO - Saved to output.png. Done!

You can add --refine at the end of the command to activate the refiner model for SDXL.

Stable Diffusion v1.5

python stable_diffusion.py --model runwayml/stable-diffusion-v1-5
Output:
2023-12-03 16:18:13,636 - INFO - Loaded safety_checker as StableDiffusionSafetyChecker from `safety_checker` subfolder of runwayml/stable-diffusion-v1-5.
2023-12-03 16:18:13,699 - INFO - Loaded vae as AutoencoderKL from `vae` subfolder of runwayml/stable-diffusion-v1-5.
Loading pipeline components...: 100%|██████████████████████████████████████████████████████████████████| 7/7 [00:01<00:00,  5.88it/s]
2023-12-03 16:18:22,837 - INFO - Running baseline...
100%|████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [00:03<00:00, 15.33it/s]
2023-12-03 16:18:26,174 - INFO - Baseline: 3.34 seconds
2023-12-03 16:18:26,174 - INFO - Running DeepCache...
100%|████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [00:01<00:00, 34.06it/s]
2023-12-03 16:18:27,718 - INFO - DeepCache: 1.54 seconds
2023-12-03 16:18:27,935 - INFO - Saved to output.png. Done!

Stable Diffusion v2.1

python stable_diffusion.py --model stabilityai/stable-diffusion-2-1
Output:
2023-12-03 16:21:17,858 - INFO - Loaded feature_extractor as CLIPImageProcessor from `feature_extractor` subfolder of stabilityai/stable-diffusion-2-1.
2023-12-03 16:21:17,864 - INFO - Loaded scheduler as DDIMScheduler from `scheduler` subfolder of stabilityai/stable-diffusion-2-1.
Loading pipeline components...: 100%|██████████████████████████████████████████████████████████████████| 6/6 [00:01<00:00,  5.35it/s]
2023-12-03 16:21:49,770 - INFO - Running baseline...
100%|████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [00:14<00:00,  3.42it/s]
2023-12-03 16:22:04,551 - INFO - Baseline: 14.78 seconds
2023-12-03 16:22:04,551 - INFO - Running DeepCache...
100%|████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [00:08<00:00,  6.10it/s]
2023-12-03 16:22:12,911 - INFO - DeepCache: 8.36 seconds
2023-12-03 16:22:13,417 - INFO - Saved to output.png. Done!

Currently, our code supports the models that can be loaded by StableDiffusionPipeline. You can specify the model name by the argument --model, which by default, is runwayml/stable-diffusion-v1-5.

Stable Video Diffusion

python stable_video_diffusion.py
Output:
Loading pipeline components...: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00,  8.36it/s]
2023-12-21 04:56:47,329 - INFO - Running baseline...
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 25/25 [01:27<00:00,  3.49s/it]
2023-12-21 04:58:26,121 - INFO - Origin: 98.66 seconds
Loading pipeline components...: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 10.59it/s]
2023-12-21 04:58:27,202 - INFO - Running DeepCache...
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 25/25 [00:49<00:00,  1.96s/it]
2023-12-21 04:59:26,607 - INFO - DeepCache: 59.31 seconds

DDPM and LDM

Please check here for the experimental code of DDPM. The code for LDM will be released soon.

Visualization

Images in the upper line are the baselines, and the images in the lower line are accelerated by DeepCache.

Stable Diffusion v1.5 (2.15x Acceleration)

LDM-4-G for ImageNet (6.96x Acceleration)

DDPM for LSUN Church & Bedroom (1.48x Acceleration)

Quantitative Results

ImageNet

image

Stable Diffusion v1.5

image

More results can be found in our paper

Other Implementations and Plugins

We sincerely thank the authors listed below who implemented DeepCache in plugins or other contexts.

We warmly welcome contributions from everyone. Please feel free to reach out to us.

Bibtex

@article{ma2023deepcache,
  title={DeepCache: Accelerating Diffusion Models for Free},
  author={Ma, Xinyin and Fang, Gongfan and Wang, Xinchao},
  journal={arXiv preprint arXiv:2312.00858},
  year={2023}
}

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

DeepCache-0.1.0.tar.gz (189.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

DeepCache-0.1.0-py3-none-any.whl (190.8 kB view details)

Uploaded Python 3

File details

Details for the file DeepCache-0.1.0.tar.gz.

File metadata

  • Download URL: DeepCache-0.1.0.tar.gz
  • Upload date:
  • Size: 189.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for DeepCache-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e4d9ef62975a0cbbd1689df5b71b74b5a63f3dd379e34239e15a8e071ad63ed2
MD5 54a84a44a0e75c00d581f3a3c1837dd6
BLAKE2b-256 0982ab6ecac399f9d757798e99048a1289cc139a52331060b479a084a2c1cbe4

See more details on using hashes here.

File details

Details for the file DeepCache-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: DeepCache-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 190.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for DeepCache-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 357e224f97ae1b5fcffa1a606253c93b5ef093a3f21da95506dc63b42fa713c4
MD5 70ca44a5574b993b53c4bd9b2e18d925
BLAKE2b-256 2f86a3f47a9e5b44071e8a3c1e634e222e9f773e34b4d4d0aecf3a77bc6931b1

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.1

2 files

This release

0.1.0 This release

2 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