Home

Awesome

<div align="center"> <h1>T-GATE: Temporally Gating Attention to Accelerate Diffusion Model for Free! 🥳</h1> <a href="https://github.com/HaozheLiu-ST/T-GATE/blob/main/LICENSE"> <img alt="GitHub" src="https://img.shields.io/github/license/HaozheLiu-ST/T-GATE.svg?color=blue"> </a> <a href="https://arxiv.org/abs/2404.02747"> <img alt="arxiv" src="https://img.shields.io/badge/arXiv-<2404.02747>-<COLOR>.svg"> </a> <a href="https://github.com/HaozheLiu-ST/T-GATE/releases"> <img alt="GitHub release" src="https://img.shields.io/github/release/HaozheLiu-ST/T-GATE.svg"> </a> </div>

TGATE-V1: Cross-Attention Makes Inference Cumbersome in Text-to-Image Diffusion Models
Wentian Zhang<sup>*</sup>  Haozhe Liu<sup>1*</sup>  Jinheng Xie<sup>2*</sup>  Francesco Faccio<sup>1,3</sup>  Mike Zheng Shou<sup>2</sup>  Jürgen Schmidhuber<sup>1,3</sup> 

<sup>1</sup> AI Initiative, King Abdullah University of Science And Technology  

<sup>2</sup> Show Lab, National University of Singapore   <sup>3</sup> The Swiss AI Lab, IDSIA

TGATE-V2: Faster Diffusion Through Temporal Attention Decomposition
Haozhe Liu<sup>1,4*</sup>  Wentian Zhang<sup>*</sup>  Jinheng Xie<sup>2*</sup>  Francesco Faccio<sup>1,3</sup>  Mengmeng Xu<sup>4</sup>  Tao Xiang<sup>4</sup>  Mike Zheng Shou<sup>2</sup>  Juan-Manuel Pérez-Rúa<sup>4</sup>  Jürgen Schmidhuber<sup>1,3</sup> 

<sup>1</sup> AI Initiative, King Abdullah University of Science And Technology  

<sup>2</sup> Show Lab, National University of Singapore   <sup>3</sup> The Swiss AI Lab, IDSIA   <sup>4</sup> Meta

Quick Introduction

We explore the role of attention mechanism during inference in text-conditional diffusion models. Empirical observations suggest that cross-attention outputs converge to a fixed point after several inference steps. The convergence time naturally divides the entire inference process into two phases: an initial phase for planning text-oriented visual semantics, which are then translated into images in a subsequent fidelity-improving phase. Cross-attention is essential in the initial phase but almost irrelevant thereafter. However, self-attention initially plays a minor role but becomes crucial in the second phase. These findings yield a simple and training-free method known as temporally gating the attention (TGATE), which efficiently generates images by caching and reusing attention outputs at scheduled time steps. Experimental results show when widely applied to various existing text-conditional diffusion models, TGATE accelerates these models by 10%–50%.

The images generated by the diffusion model with or without TGATE. Our method can accelerate the diffusion model without generation performance drops. It is training-free and can be widely complementary to the existing studies.

🚀 Major Features

📄 Updates

📖 Key Observation

Impact of cross-attention on the inference steps in a pre-trained diffusion model (SD-2.1). The images generated by the diffusion model at different denoising steps. The first row feeds the text embedding to the cross-attention modules for all steps. The second row only uses the text embedding from the first step to the 10th step, and the third row inputs the text embedding from the 11th to the 25th step.

We summarize our observations as follows:

🖊️ Method

if gate_step == cur_step:
    hidden_uncond, hidden_pred_text = hidden_states.chunk(2)
    cache = (hidden_uncond + hidden_pred_text ) / 2
if self_attn and (gate_step>cur_step):
    hidden_states = cache
if cross_attn and (gate_step<cur_step):
    hidden_states = cache

📄 Results

ModelMACsLatencyZero-shot 10K-FID on MS-COCO
SD-XL149.438T53.187s24.164
SD-XL w/ TGATE95.988T31.643s22.917
Pixart-Alpha107.031T61.502s37.983
Pixart-Alpha w/ TGATE73.971T36.650s36.390
Pixart-Sigma107.766T60.467s34.278
Pixart-Sigma w/ TGATE74.420T36.449s32.927
DeepCache (SD-XL)57.888T19.931s25.678
DeepCache w/ TGATE43.868T14.666s24.511
LCM (SD-XL)11.955T3.805s26.357
LCM w/ TGATE11.171T3.533s26.902
LCM (Pixart-Alpha)8.563T4.733s35.989
LCM w/ TGATE7.623T4.543s35.843

The FID is computed on captions by PytorchFID.

The latency is tested on a 1080ti commercial card and diffusers v0.28.2.

The MACs are calculated by calflops.

🛠️ Requirements

🌟 Usage

Examples

To use TGATE for accelerating the denoising process, you can simply use main.py. For example,

python main.py \
--prompt 'Astronaut in a jungle, cold color palette, muted colors, detailed, 8k' \
--model 'sdxl' \
--gate_step 10 \
--sp_interval 5 \
--fi_interval 1 \
--warm_up 2 \
--saved_path './generated_tmp/sd_xl/' \
--inference_step 25 \
python main.py \
--prompt 'An alpaca made of colorful building blocks, cyberpunk.' \
--model 'pixart_alpha' \
--gate_step 15 \
--sp_interval 3 \
--fi_interval 1 \
--warm_up 2 \
--saved_path './generated_tmp/pixart_alpha/' \
--inference_step 25 \
python main.py \
--prompt 'an astronaut sitting in a diner, eating fries, cinematic, analog film.' \
--model 'pixart_sigma' \
--gate_step 15 \
--sp_interval 3 \
--fi_interval 1 \
--warm_up 2 \
--saved_path './generated_tmp/pixart_sigma/' \
--inference_step 25 \
python main.py \
--prompt 'Self-portrait oil painting, a beautiful cyborg with golden hair, 8k' \
--model 'lcm_sdxl' \
--gate_step 1 \
--sp_interval 1 \
--fi_interval 1 \
--warm_up 0 \
--saved_path './generated_tmp/lcm_sdxl/' \
--inference_step 4 \
python main.py \
--prompt 'A haunted Victorian mansion under a full moon.' \
--model 'sdxl' \
--gate_step 10 \
--sp_interval 1 \
--fi_interval 1 \
--warm_up 0 \
--saved_path './generated_tmp/sd_xl_deepcache/' \
--inference_step 25 \
--deepcache \
  1. For LCMs, gate_step is set as 1 or 2, and inference step is set as 4.

  2. To use DeepCache, deepcache is set as True.

Third-party Usage

Acknowledgment

Citation

If you find our work inspiring or use our codebase in your research, please consider giving a star ⭐ and a citation.

@article{tgate,
  title={Cross-Attention Makes Inference Cumbersome in Text-to-Image Diffusion Models},
  author={Zhang, Wentian and Liu, Haozhe and Xie, Jinheng and Faccio, Francesco and Shou, Mike Zheng and Schmidhuber, J{\"u}rgen},
  journal={arXiv preprint arXiv:2404.02747v1},
  year={2024}
}

@article{liu2024faster,
  title={Faster Diffusion via Temporal Attention Decomposition},
  author={Liu, Haozhe and Zhang, Wentian and Xie, Jinheng and Faccio, Francesco and Xu, Mengmeng and Xiang, Tao and Shou, Mike Zheng and Perez-Rua, Juan-Manuel and Schmidhuber, J{\"u}rgen},
  journal={arXiv preprint arXiv:2404.02747v2},
  year={2024}
}