Diffusers 文档

DeepCache

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

DeepCache

DeepCache 利用 U-Net 架构的特性,通过有策略地缓存和重用高层特征,同时高效地更新低层特征,从而加速 StableDiffusionPipelineStableDiffusionXLPipeline

首先安装 DeepCache

pip install DeepCache

然后加载并启用 DeepCacheSDHelper

  import torch
  from diffusers import StableDiffusionPipeline
  pipe = StableDiffusionPipeline.from_pretrained('stable-diffusion-v1-5/stable-diffusion-v1-5', torch_dtype=torch.float16).to("cuda")

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

  image = pipe("a photo of an astronaut on a moon").images[0]

set_params 方法接受两个参数:cache_intervalcache_branch_idcache_interval 表示特征缓存的频率,即每次缓存操作之间的步数。cache_branch_id 标识负责执行缓存过程的网络分支(按从最浅层到最深层的顺序排列)。选择较小的 cache_branch_id 或较大的 cache_interval 可以加快推理速度,但会牺牲一定的图像质量(关于这两个超参数的消融实验可以在论文中找到)。设置好这些参数后,使用 enabledisable 方法来激活或停用 DeepCacheSDHelper

你可以在 WandB 报告中找到更多生成的样本(原始 pipeline vs DeepCache)以及相应的推理延迟。提示词随机选自 MS-COCO 2017 数据集。

基准测试

我们在 NVIDIA RTX A5000 上测试了 DeepCache 在 50 个推理步骤下对 Stable Diffusion v2.1 的加速效果,测试中使用了不同的分辨率、批大小、缓存间隔 (I) 和缓存分支 (B) 配置。

分辨率 批次大小 原始 DeepCache(I=3, B=0) DeepCache(I=5, B=0) DeepCache(I=5, B=1)
512 8 15.96 6.88 (2.32x) 5.03 (3.18x) 7.27 (2.20x)
4 8.39 3.60 (2.33x) 2.62 (3.21x) 3.75 (2.24x)
1 2.61 1.12 (2.33x) 0.81 (3.24x) 1.11 (2.35x)
768 8 43.58 18.99 (2.29x) 13.96 (3.12x) 21.27 (2.05x)
4 22.24 9.67 (2.30x) 7.10 (3.13x) 10.74 (2.07x)
1 6.33 2.72 (2.33x) 1.97 (3.21x) 2.98 (2.12x)
1024 8 101.95 45.57 (2.24x) 33.72 (3.02x) 53.00 (1.92x)
4 49.25 21.86 (2.25x) 16.19 (3.04x) 25.78 (1.91x)
1 13.83 6.07 (2.28x) 4.43 (3.12x) 7.15 (1.93x)
< > 在 GitHub 上更新