chengzeyi/ParaAttention
https://wavespeed.ai/ Context parallel attention that accelerates DiT model inference with dynamic caching
What it is
ParaAttention is a library for speeding up inference of DiT models (the transformer-based diffusion models used for image and video generation). It does this with two main techniques:
- Context Parallelism – splitting the work across multiple GPUs.
- First Block Cache – skipping repeated computation during generation.
It also works well with PyTorch’s torch.compile optimizer.
Main techniques
Context Parallelism
Context Parallelism (CP) runs a model on multiple GPUs by splitting the input tensors along the sequence dimension (the dimension that represents positions in the input sequence). Unlike some other parallelization methods that only split certain layers, CP splits activations across all layers.
ParaAttention supports two parallelization styles:
- Ulysses Style
- Ring Style
It can also mix them together in something called Unified Attention. This lets you choose the best parallelization pattern for your model and hardware. You can adjust the parallelism style and the “mesh shape” with a few lines of code.
First Block Cache (dynamic caching)
This is ParaAttention’s caching method. It uses the residual output of the first transformer block as a “cache indicator.”
- If the current residual output is very similar to the previous one, the model can reuse the previous final residual output.
- This lets it skip all the remaining transformer blocks for that step.
The README says this can speed up inference by up to 2x while maintaining high accuracy. You can adjust a threshold (residual_diff_threshold) to balance speed and quality:
- Higher threshold → more caching, faster speed, but possibly more accuracy loss.
- Lower threshold → less caching, closer to original quality.
torch.compile friendliness
ParaAttention is designed to avoid “graph breaks” during torch.compile. That means the compiler can see the heavy computations as one graph and optimize them more effectively, for example by overlapping computation and communication.
Easy to use
For a diffusers pipeline, you can enable context parallelism with one function:
from para_attn.context_parallel.diffusers_adapters import parallelize_pipe
parallelize_pipe(pipe)
You can enable First Block Cache with one function too:
from para_attn.first_block_cache.diffusers_adapters import apply_cache_on_pipe
apply_cache_on_pipe(pipe, residual_diff_threshold=0.0)
The README says you don’t need to change the model code. For custom models, you can wrap the call with ParaAttention’s TorchFunctionMode context manager.
There is also a way to parallelize the VAE part of a model:
from para_attn.parallel_vae.diffusers_adapters import parallelize_vae
parallelize_vae(vae)
Currently only AutoencoderKL and AutoencoderKLHunyuanVideo are supported for VAE parallelization.
Supported models
The README lists these models:
- FLUX (image generation)
- HunyuanVideo (video generation)
- Mochi (video generation)
- CogVideoX (video generation)
There are example scripts for:
- Multi-GPU context parallelism:
parallel_examples/ - Single-GPU First Block Cache:
first_block_cache_examples/
Example command for FLUX on 2 GPUs:
torchrun --nproc_per_node=2 parallel_examples/run_flux.py
Example command for single-GPU HunyuanVideo with First Block Cache:
python3 first_block_cache_examples/run_hunyuan_video.py
Performance highlights
Context Parallelism without First Block Cache
From the README’s table on A100-SXM4-80GB GPUs:
| Model | Method | Wall time | Speedup |
|---|---|---|---|
| FLUX.1-dev | Baseline | 13.843s | 1.00x |
| FLUX.1-dev | para-attn (ring) + torch.compile, 2 GPUs |
5.775s | 2.39x |
| FLUX.1-dev | para-attn (ulysses + ring) + torch.compile, 4 GPUs |
3.557s | 3.89x |
| mochi-1-preview | Baseline | 196.534s | 1.00x |
| mochi-1-preview | para-attn (cfg + ring) + torch.compile, 4 GPUs |
47.100s | 4.17x |
The README notes that wall time includes overhead from the text encoder and VAE decoder, so the speedup in iterations per second is generally higher than the wall-time speedup.
First Block Cache on a single L20 GPU
For FLUX on one NVIDIA L20 GPU:
| Setting | Wall time |
|---|---|
| Original | 26.36s |
| FBCache with threshold 0.06 | 21.83s |
| FBCache with threshold 0.08 | 17.01s |
| FBCache with threshold 0.10 | 16.00s |
| FBCache with threshold 0.12 | 13.78s |
Installation
From PyPI:
pip3 install 'torch==2.6.0'
pip3 install para-attn
For local development:
git clone https://github.com/chengzeyi/ParaAttention.git
cd ParaAttention
git submodule update --init --recursive
pip3 install 'torch==2.6.0'
pip3 install 'setuptools>=64' 'setuptools_scm>=8'
pip3 install -e '.[dev]' --no-build-isolation
Notes and caveats
- To measure
torch.compileperformance correctly, the README says you should warm up the model with a few runs first. - For HunyuanVideo, the README recommends installing
diffusersfrom its latest master branch and using GPUs with at least 48GB memory. - The README has an unchecked roadmap item: “the fastest accurate attention implemented in Triton, running 50% faster than the original FA2 implementation on RTX 4090.” So that is not yet listed as a completed feature.
Hosted alternative
The README also promotes WaveSpeedAI, a hosted platform for AI image and video generation that requires no local GPU. It offers browser-based generators, an API, Python/JavaScript SDKs, a CLI, and ComfyUI nodes. But ParaAttention itself is a library for speeding up inference on your own GPUs.
Related
- Project
- Project
- Project
- Project