SageAttention2++: A More Efficient Implementation of SageAttention2

Jintao Zhang, Xiaoming Xu, Jia wei, Haofeng Huang, Pengle Zhang, Chendong Xiang, Jun Zhu, Jianfei Chen

TL;DR

SageAttention2++ dramatically speeds up inference attention by swapping the accumulator in the FP8 attention-value ($PV$) matrix multiplication from FP32 to FP16, unlocking hardware instructions that run twice as fast. By mathematically constraining the quantization scales and buffering intermediate results, it avoids FP16 overflow issues and achieves up to a $3.9\times$ speedup over FlashAttention2 on RTX 4090/5090 GPUs with virtually no end-to-end accuracy loss across language, image, and video models.

What Problem Does This Paper Address?

Quantized attention mechanisms like SageAttention2 significantly cut inference latency by compressing the query-key ($QK^\top$) and attention-value ($PV$) matrix multiplications into low-bit operations (INT4/INT8 and FP8). However, for the $PV$ multiplication, SageAttention2 relies on Tensor Core instructions configured with an FP32 accumulator (mma.f32.f8.f8.f32). On modern architectures like NVIDIA Ada Lovelace (RTX 4090) and Blackwell (RTX 5090), this instruction only delivers a $2\times$ theoretical speedup over standard FP16.

In contrast, Tensor Core instructions utilizing an FP16 accumulator (mma.f16.f8.f8.f16) deliver a $4\times$ speedup. The roadblock to using FP16 accumulation has been numerical overflow: summing 32 low-precision products inside a matrix multiply easily breaches the narrow dynamic range of 16-bit floats ($[-65504, 65504]$).

Methods

SageAttention2++ retains the core pipeline of SageAttention2—smoothing $Q$ and $K$, quantizing $QK^\top$ to INT4/INT8, and quantizing $P$ and $V$ to FP8 (E4M3)—while introducing two critical optimizations to safely deploy FP16 accumulation:

  1. Restricted FP8 Quantization Range:
    The underlying MMA instruction (mma.m16n8k32) accumulates 32 product values ($pv$) in FP16. To guarantee $|32 \times pv| \le 65504$, the authors adjust the quantization scale factors: $$\delta_P = \frac{|\max(\tilde{P})|}{P_r}, \quad \delta_V = \frac{|\max(V)|}{V_r}$$ They constrain the maximum quantized representation limits such that $P_r \times V_r \le 2047$ (since $65504 / 32 = 2047$). Empirically, selecting $P_r = 224$ and $V_r = 4.5$ prevents overflow while maintaining an attention cosine similarity of 99.97%.

  2. Delayed FP32 Buffering:
    Converting accumulated values from FP16 back to FP32 introduces instruction overhead via PTX type conversions. To amortize this cost, the kernel accumulates two consecutive mma.m16n8k32 operations in FP16 before executing a single FP32 conversion. This tightens the scaling constraint to $P_r \times V_r \le 2047 / 2 \approx 1023$, but halves data-type conversion overhead.

The authors evaluate two configurations: SageAttn2++ (8+8) (INT8 for $Q, K$) and SageAttn2++ (4+8) (INT4 for $Q, K$), both with FP8 for $P$ and $V$.

Datasets

The authors evaluate generation fidelity across diverse domains using standard benchmarks (no new benchmarks are introduced):

  • Text Generation (Llama 3.1 8B): Perplexity on WikiText, zero-shot accuracy on LAMBADA, and long-context retrieval via Needle-in-A-Haystack (NIAH).
  • Text-to-Video (CogVideoX-2B, HunyuanVideo, Wan): Evaluated on prompts from the Open-Sora benchmark using CLIPSIM, CLIP-Temp, aesthetic quality (VQA-a), technical quality (VQA-t), and temporal consistency (Flow-score).
  • Text-to-Image (Flux-schnell, Stable Diffusion 3.5 turbo): Evaluated on COCO annotations via FID, sFID, CLIP score, and ImageReward (IR).

Key Insights and Ideas

  • Instruction-level awareness drives real-world gains: Simply selecting FP8 inputs does not automatically maximize throughput. Matching the accumulator type to hardware-native fast paths (mma.f16.f8.f8.f16) doubles execution efficiency.
  • Quantization range narrowing is surprisingly lossless: Clamping the effective dynamic range ($P_r = 224, V_r = 4.5$) instead of standard E4M3 bounds (up to $\pm 448$) does not degrade attention accuracy, solving numerical overflow at zero cost to model fidelity.
  • Amortizing PTX conversion overhead matters: High-speed Tensor Core kernels are frequently bottlenecked by instruction conversion overhead; batching accumulation steps before casting to FP32 saves critical clock cycles.

Key Results

  • Kernel Throughput: On RTX 4090 and RTX 5090 (with sequence lengths from 1K to 32K and head dimensions 64/128):
  • SageAttn2++ (4+8) achieves up to $3.9\times$ speedup over FlashAttention2.
  • SageAttn2++ (8+8) achieves roughly $3.0\times$ speedup over FlashAttention2, consistently outperforming both SageAttention and SageAttention2.
  • End-to-End Accuracy:
  • Llama 3.1 (8B): SageAttn2++ (8+8) achieves 6.020 WikiText perplexity (vs. 6.013 full-precision) and 0.901 NIAH accuracy (vs. 0.906 full-precision).
  • Image/Video: FID/sFID and video aesthetic metrics match full-precision and SageAttention2 baselines with negligible deviation across CogVideoX, Wan, Flux, and SD 3.5.

Limitations and Future Work

  • INT4 Precision Drop on Video: While the INT8 variant matches full precision closely, the more aggressive INT4 variant (4+8) leads to non-trivial metric degradation on complex video models (e.g., Wan's VQA-a score drops from 53.25 to 29.72).
  • Target Hardware Scope: The optimizations target Ada and Blackwell consumer GPUs (RTX 4090/5090) where FlashAttention3 is unsupported.
  • Inference Only: The paper targets forward-pass inference acceleration; extending these low-precision accumulator optimizations to backward-pass training remains an open challenge.

Summary updated

BibTeX
@inproceedings{zhang2025sageattention,
  author = {Jintao Zhang and Xiaoming Xu and Jia wei and Haofeng Huang and Pengle Zhang and Chendong Xiang and Jun Zhu and Jianfei Chen},
  title = {{SageAttention2++: A More Efficient Implementation of SageAttention2}},
  booktitle = {ES-FoMo-III @ ICML},
  year = {2025},
  url = {https://openreview.net/forum?id=Eev5rXcgng},
}
Figure