Diffusers documentation

T-GATE

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

T-GATE

T-GATE 通过跳过交叉注意力计算一旦收敛,加速了 Stable DiffusionPixArtLatency Consistency Model 管道的推理。此方法不需要任何额外训练,可以将推理速度提高 10-50%。T-GATE 还与 DeepCache 等其他优化方法兼容。

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

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

要使用 T-GATE 与管道,您需要使用其对应的加载器。

管道T-GATE 加载器
PixArtTgatePixArtLoader
Stable Diffusion XLTgateSDXLLoader
Stable Diffusion XL + DeepCacheTgateSDXLDeepCacheLoader
Stable DiffusionTgateSDLoader
Stable Diffusion + DeepCacheTgateSDDeepCacheLoader

接下来,创建一个 TgateLoader,包含管道、门限步骤(停止计算交叉注意力的时间步)和推理步骤数。然后在管道上调用 tgate 方法,提供提示、门限步骤和推理步骤数。

让我们看看如何为几个不同的管道启用此功能。

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", 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参数延迟零样本 10K-FID on MS-COCO
SD-1.516.938T859.520M7.032s23.927
SD-1.5 w/ T-GATE9.875T815.557M4.313s20.789
SD-2.138.041T865.785M16.121s22.609
SD-2.1 w/ T-GATE22.208T815.433 M9.878s19.940
SD-XL149.438T2.570B53.187s24.628
SD-XL w/ T-GATE84.438T2.024B27.932s22.738
Pixart-Alpha107.031T611.350M61.502s38.669
Pixart-Alpha w/ T-GATE65.318T462.585M37.867s35.825
DeepCache (SD-XL)57.888T-19.931s23.755
DeepCache 配合 T-GATE43.868T-14.666秒23.999
LCM (SD-XL)11.955T2.570B3.805秒25.044
LCM 配合 T-GATE11.171T2.024B3.533秒25.028
LCM (Pixart-Alpha)8.563T611.350M4.733秒36.086
LCM 配合 T-GATE7.623T462.585M4.543秒37.048

延迟测试基于 NVIDIA 1080TI,MACs 和 Params 使用 calflops 计算,FID 使用 PytorchFID 计算。

Update on GitHub