thu-ml/SLA
SLA: Beyond Sparsity in Diffusion Transformers via Fine-Tunable Sparse–Linear Attention
SLA – Sparse‑Linear Attention
What it is
- An open‑source PyTorch implementation of Sparse‑Linear Attention (SLA), a trainable attention mechanism that blends sparse and linear attention to speed up diffusion‑based transformers.
- Accompanied by a highly‑optimized variant called SageSLA, built on the authors’ SageAttention and SpargeAttn codebases.
Why it matters
- Diffusion models (e.g., image generation) often use very long token sequences, making standard quadratic‑time attention a bottleneck.
- SLA reduces the computational cost while preserving accuracy by keeping only the most important keys/values (controlled by a
topksparsity ratio) and applying a linear‑complexity kernel (elu,relu, orsoftmax).
Installation
git clone https://github.com/thu-ml/SLA.git
cd SLA
pip install -e . # installs the package in editable mode
Basic usage
import torch
from sparse_linear_attention import SparseLinearAttention
attn = SparseLinearAttention(
head_dim=128,
topk=0.2, # keep 20 % of tokens (1‑sparsity)
feature_map="softmax", # kernel for linear attention
BLKQ=64, # block size for queries
BLKK=64, # block size for keys/values
).cuda()
B, H, L, D = 2, 4, 4096, 128
q = torch.randn((B, H, L, D), dtype=torch.bfloat16, device='cuda')
k = torch.randn((B, H, L, D), dtype=torch.bfloat16, device='cuda')
v = torch.randn((B, H, L, D), dtype=torch.bfloat16, device='cuda')
output = attn(q, k, v) # shape (B, H, L, D)
The example shows a typical forward pass on a GPU using bfloat16 tensors.
SageSLA
- A drop‑in faster forward implementation located in the
SageSLA/directory. - Leverages low‑precision (8‑bit) kernels from SageAttention and code from SpargeAttn for inference‑time speedups.
Key take‑aways
- Two branches are provided: the
SLA-1branch (original version) and themainbranch (latest updates). - The repository includes visual figures (motivation, effectiveness, efficiency) that illustrate how SLA improves speed and quality for diffusion transformers.
- Authors supply full citation information for both the original SLA paper (arXiv 2509.24006) and a follow‑up SLA2 work.
Citation If you use SLA in research or products, please cite the provided BibTeX entries.
This summary is based solely on the repository’s README; no additional features are inferred.
Related
- Project
- Project
- Project
- Project