Diffusers 文档

T-GATE

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

T-GATE

T-GATE 通过在交叉注意力计算收敛后跳过计算,加速 Stable DiffusionPixArtLatency Consistency Model pipelines 的推理。此方法不需要任何额外的训练,可以将推理速度提高 10-50%。T-GATE 也与其他优化方法(如 DeepCache)兼容。

在开始之前,请确保您已安装 T-GATE。

pip install tgate
pip install -U torch diffusers transformers accelerate DeepCache

要将 T-GATE 与 pipeline 一起使用,您需要使用其对应的加载器。

Pipeline T-GATE 加载器
PixArt TgatePixArtLoader
Stable Diffusion XL TgateSDXLLoader
Stable Diffusion XL + DeepCache TgateSDXLDeepCacheLoader
Stable Diffusion TgateSDLoader
Stable Diffusion + DeepCache TgateSDDeepCacheLoader

接下来,使用 pipeline、门控步长(停止计算交叉注意力的时间步长)和推理步数创建一个 TgateLoader。然后使用 prompt、门控步长和推理步数在 pipeline 上调用 tgate 方法。

让我们看看如何为几种不同的 pipelines 启用此功能。

PixArt
Stable Diffusion XL
StableDiffusionXL with DeepCache
Latent Consistency Model

使用 T-GATE 加速 PixArtAlphaPipeline

import torch
from diffusers import PixArtAlphaPipeline
from tgate import TgatePixArtLoader

pipe = PixArtAlphaPipeline.from_pretrained("PixArt-alpha/PixArt-XL-2-1024-MS", torch_dtype=torch.float16)

gate_step = 8
inference_step = 25
pipe = TgatePixArtLoader(
       pipe,
       gate_step=gate_step,
       num_inference_steps=inference_step,
).to("cuda")

image = pipe.tgate(
       "An alpaca made of colorful building blocks, cyberpunk.",
       gate_step=gate_step,
       num_inference_steps=inference_step,
).images[0]

T-GATE 也支持 StableDiffusionPipelinePixArt-alpha/PixArt-LCM-XL-2-1024-MS

基准测试

模型 MACs 参数 延迟 MS-COCO 上的 Zero-shot 10K-FID
SD-1.5 16.938T 859.520M 7.032s 23.927
SD-1.5 w/ T-GATE 9.875T 815.557M 4.313s 20.789
SD-2.1 38.041T 865.785M 16.121s 22.609
SD-2.1 w/ T-GATE 22.208T 815.433 M 9.878s 19.940
SD-XL 149.438T 2.570B 53.187s 24.628
SD-XL w/ T-GATE 84.438T 2.024B 27.932s 22.738
Pixart-Alpha 107.031T 611.350M 61.502s 38.669
Pixart-Alpha w/ T-GATE 65.318T 462.585M 37.867s 35.825
DeepCache (SD-XL) 57.888T - 19.931s 23.755
DeepCache w/ T-GATE 43.868T - 14.666s 23.999
LCM (SD-XL) 11.955T 2.570B 3.805s 25.044
LCM w/ T-GATE 11.171T 2.024B 3.533s 25.028
LCM (Pixart-Alpha) 8.563T 611.350M 4.733s 36.086
LCM w/ T-GATE 7.623T 462.585M 4.543s 37.048

延迟在 NVIDIA 1080TI 上测试,MACs 和参数使用 calflops 计算,FID 使用 PytorchFID 计算。

< > 更新 在 GitHub 上