SpargeAttention: Accurate and Training-free Sparse Attention Accelerating Any Model Inference
About
An efficient attention implementation is essential for large models due to its quadratic time complexity. Fortunately, attention commonly exhibits sparsity, i.e., many values in the attention map are near zero, allowing for the omission of corresponding computations. Many studies have utilized the sparse pattern to accelerate attention. However, most existing works focus on optimizing attention within specific models by exploiting certain sparse patterns of the attention map. A universal sparse attention that guarantees both the speedup and end-to-end performance of diverse models remains elusive. In this paper, we propose SpargeAttn, a universal sparse and quantized attention for any model. Our method uses a two-stage online filter: in the first stage, we rapidly and accurately predict the attention map, enabling the skip of some matrix multiplications in attention. In the second stage, we design an online softmax-aware filter that incurs no extra overhead and further skips some matrix multiplications. Experiments show that our method significantly accelerates diverse models, including language, image, and video generation, without sacrificing end-to-end metrics. The code is available at https://github.com/thu-ml/SpargeAttn.
Related benchmarks
| Task | Dataset | Result | Rank | |
|---|---|---|---|---|
| Text-to-Video Generation | VBench | -- | 155 | |
| Long-context Language Understanding | LongBench (test) | Average Score35.87 | 147 | |
| Video Understanding | VideoMME (test) | Overall Score65 | 34 | |
| Long-context Understanding | RULER 64k | Accuracy83.03 | 25 | |
| Text-to-Video | Text-to-Video evaluation suite | Image Quality Score70.77 | 24 | |
| Image-to-Video Generation | VBench (test) | Image Quality Score72.61 | 18 | |
| Image-to-Video | VBench++ | Temporal Flickering97.49 | 18 | |
| Text-to-Video Generation | VBench official evaluation prompts | Semantic Score72.87 | 15 | |
| Long-context Understanding | RULER 128k | Accuracy75.42 | 15 | |
| Video Generation | VBench CogVideoX v1.5 | Speedup1.49 | 12 |