[ICLR2025, ICML2025, NeurIPS2025 Spotlight] Quantized Attention achieves speedup of 2-5x compared to FlashAttention, without losing end-to-end metrics across language, image, and video models.
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
SageAttention
This repository provides the official implementation of SageAttention, SageAttention2, and SageAttention2++, which achieve surprising speedup on most GPUs without lossing accuracy across all models in a plug-and-play way.
SageAttention: Accurate 8-Bit Attention for Plug-and-play Inference Acceleration
Jintao Zhang, Jia Wei, Haofeng Huang, Pengle Zhang, Jun Zhu, Jianfei Chen
Paper: https://arxiv.org/abs/2410.02367
SageAttention2: Efficient Attention with Thorough Outlier Smoothing and Per-thread INT4 Quantization
Jintao Zhang, Haofeng Huang, Pengle Zhang, Jia Wei, Jun Zhu, Jianfei Chen
Paper: https://arxiv.org/abs/2411.10958
SageAttention3: Microscaling FP4 Attention for Inference and An Exploration of 8-Bit Training
Jintao Zhang, Jia Wei, Haoxu Wang, Pengle Zhang, Xiaoming Xu, Haofeng Huang, Kai Jiang, Jianfei Chen, Jun Zhu
Paper: https://arxiv.org/abs/2505.11594
Current Features
torch.compilewith non-cudagraphs mode and distributed inference.Project Updates
Results for CogVideoX1.5-5B on NVIDIA H20 GPU
[2025-01-24]: 🎉SageAttention is accepted by ICLR 2025!
[2024-12-20]: 🔥Update the SageAttention2 Paper.
[2024-12-20]: 🔥Release SageAttention 2.0.1 Beta! In this version, we introduce a new feature: per-thread quantization, which offers finer granularity while maintaining hardware efficiency.
[2024-11-21]: 🔥SageAttention 2.0.0 beta is released! Now SageAttention has measured speedup on L20, L40, A100, A800, and A6000, RTX3090 and RTX4090.
[2024-11-12]: Support for
sageattn_varlenis available now.[2024-11-11]: Support for different sequence lengths between
qandk,v,(batch_size, head_num, seq_len, head_dim)or(batch_size, seq_len, head_num, head_dim)input shapes, andgroup-query attentionis available now.Installation
Base environment
python>=3.9,torch>=2.3.0,triton>=3.0.0CUDA:>=12.8for Blackwell or SageAttention2++>=12.4for fp8 support on Ada>=12.3for fp8 support on Hopper>=12.0for Ampereflash-attnfor benchmarkingInstall Package
For SageAttention V1 in Triton (slower than SageAttention V2/V2++/V3), refer to SageAttention-1 branch and install using pip:
pip install sageattention==1.0.6To use SageAttention 2.2.0 (containing SageAttention2++), you can install using pip:
Or you can compile from source:
To benchmark the speed against FlashAttention3, please compile FlashAttention3 from source:
How to Use
q, k, vare FP16/BF16 dtype with the shape(batch_size, head_num, seq_len, head_dim)using defaulttensor_layout="HND". For shape(batch_size, seq_len, head_num, head_dim), settensor_layout="NHD".is_causaldetermines the use of a causal mask.Available APIs:
sageattn: Automatically selects the optimal kernel based on the GPU to achieve a good performance-accuracy trade-off.sageattn_qk_int8_pv_fp16_triton: INT8 quantization for QK⊤ and FP16 for PV using Triton backend.sageattn_qk_int8_pv_fp16_cuda: INT8 quantization for QK⊤ and FP16 for PV using CUDA backend.sageattn_qk_int8_pv_fp8_cuda: INT8 quantization for QK⊤ and FP8 for PV using CUDA backend. (Note that settingpv_accum_dtype=fp32+fp16corresponds to SageAttention2++.)sageattn_qk_int8_pv_fp8_cuda_sm90: INT8 quantization for QK⊤ and FP8 for PV using CUDA backend, specifically optimized for Hopper GPUs.sageattn_varlen: INT8 quantization for QK⊤ and FP16 for PV using Triton backend. Support for varying sequence lengths within the same batch.For optimal speed and accuracy performance on custom devices and models, we strongly recommend referring to the this file for detailed guidance.
Plug-and-play Example
We can replace
scaled_dot_product_attentioneasily. We will take CogvideoX as an example:Add the following codes and run
Specifically,
You can get a lossless video in
./example/videos/<model>/<attention_type>/faster than by using--attention_type sdpa. More examples and guidance can be found under theexample/directory.Kernel Benchmarking
We provide a benchmarking script to compare the speed of different kernels including SageAttention, FlashAttention2 and FlashAttention3. Please refer to the
benchmark/directory for more details.Performance
Speed of Kernels
8+8means the kernel with INT8 quantization for QK⊤ and FP8 quantization for PV.8+16uses FP16 with FP16 accumulator for PV.End-to-end Performance
End-to-End Accuracy:
End-to-End Speedup:
Citation
If you use this code or find our work valuable, please cite: