Jintao Zhang, Haofeng Huang, Pengle Zhang, Jia Wei, Jun Zhu, Jianfei Chen | 2024-11 | https://arxiv.org/abs/2411.10958 Category: kernel | Tags: attention, quantization, INT4, FP8, tensor-core Read: 2026-04-16
SageAttention2 proposes a quantized attention mechanism that uses INT4 for $QK^\top$ and FP8 for $PV$ with three key techniques (Q+K smoothing, per-thread quantization aligned to MMA instruction layout, two-level FP32 accumulation for FP22 accumulators), achieving 3x speedup over FlashAttention2 with negligible end-to-end accuracy loss.
Q1: 这篇论文试图解决什么核心痛点/问题?
Attention计算是Transformer的主要性能瓶颈,尤其在长序列场景下。虽然SageAttention已将QK用INT8加速,但INT8仅有INT4一半的速度;同时其PV的FP16加速方案仅在少数GPU上有效。如何安全地将Attention量化到INT4/FP8以获得极致速度,同时保持精度,是核心问题。
Q2: 作者提出了什么新的"杀手锏"方法/架构?
SageAttention2提出三大技术:(1) 双重Smoothing——不仅smooth K(如SageAttention),还smooth Q(减去block均值),将量化前outlier大幅削减;(2) Per-thread量化——根据PTX mma指令的线程-数据映射关系设计量化分组,使每个GPU线程只对应一个量化scale,精度接近per-token而无额外开销;(3) 两级累加策略——发现FP8 mma指令的累加器实际是FP22(非FP32),通过外层FP32 buffer累加来消除精度损失。
Q3: 最终效果/结论如何?
在RTX4090上达到481 TOPS峰值,比FlashAttention2快约3倍、比xformers快约4.5倍。在Hopper GPU上,SageAttn2-8b匹配FlashAttention3(fp8)的速度但精度显著更高。在10+个语言/图像/视频生成模型上,端到端指标损失可忽略不计。
FlashAttention系列通过tiling优化了Attention的内存访问,但计算本身仍使用FP16/BF16。SageAttention首次将 $QK^\top$ 量化为INT8实现了2x加速,但存在两个弱点:INT8 Matmul只有INT4一半的速度;PV使用FP16累加器加速仅在RTX4090等极少数GPU上生效。
👉 打个比方:想象你在给一幅画拍照(量化),如果画上有一个极亮的光点(outlier),相机为了不让光点过曝,会把其他所有细节都拍得很暗(量化为零)。解决方法是先把那个光点擦掉(smoothing),单独记录它的位置,拍完照再贴回去。SageAttention2对Q和K都做了这样的"擦光点"操作,而且巧妙地利用了GPU线程与数据的对应关系,让"贴回去"的过程零开销。
核心洞察有三:(1) Q和K的token之间高度相似,减去均值后outlier大幅缩小,INT4量化精度从80%→99.5% CosSim;(2) MMA指令的线程布局天然将特定token分配给同一线程,可以把这些token作为一个量化组,实现比per-block更细但比per-token零开销的量化;(3) NVIDIA Ada/Hopper的FP8 MMA累加器实际只有22位有效精度(非标称的FP32),需要外层FP32 buffer累加来补偿。

What it shows: 左上为RTX4090上的kernel速度对比(SageAttn2 vs FlashAttention2/xformers),右上为Llama3.1在100K序列长度下的首token生成加速和Needle-in-a-Haystack任务表现,下方为CogvideoX生成的视频对比。
Why it matters: 一图展示了SageAttention2的核心价值——3x kernel加速、端到端推理加速、以及视觉质量无损。
Detailed description: 左上bar chart显示随序列长度增加,SageAttn2-4b达到~480 TOPS,FlashAttention2约160 TOPS,xformers更低。右上展示Llama3.1在L20上100K序列的首token延迟从39.9s降至23.2s(1.72x加速),同时NIAH热力图显示精度完全保持。下方两组视频帧对比表明CogvideoX 1.5-5B使用SageAttention2加速1.8x且视频质量无差异。

What it shows: SageAttention2的完整数据流管线,包含5个阶段:①Smooth Q,K,V → ②GEMV计算 $\Delta S$ → ③Per-thread量化Q,K和per-channel量化V → ④执行SageAttention2 kernel → ⑤修正输出。
Why it matters: 这是论文的"灵魂图",清晰展示了从FP16输入到最终输出的完整处理流程,以及各量化/补偿技术如何协同工作。
Detailed description: 图左侧显示Q经过减均值(smooth Q)后进行per-thread INT4量化,K经过减全局均值(smooth K)后同样per-thread INT4量化。中间GEMV计算 $\bar{q}_i$ 与 $\gamma(K_j)$ 的向量积作为 $\Delta S$ 补偿。V做per-channel FP8量化。kernel内部执行INT4 $QK^\top$ + $\Delta S$ → softmax → FP8 $PV$(两级累加),最后乘以V的per-channel scale得到输出。

What it shows: 左侧展示量化scale与GPU warp内token的对应关系,右侧展示MMA.m16n8k64指令中GPU线程与量化token的映射。
Why it matters: 这是per-thread量化的核心机制图,说明了为何每个线程只需一个scale就能完成反量化——因为MMA指令天然将特定token映射到同一线程。
Detailed description: 左图显示bq=128个query token被4个warp分割,每个warp处理32个token,其中token $i$, $8+i$, $16+i$, $24+i$ 共享一个量化scale。右图是mma.m16n8k64指令的线程布局,展示32个线程(T0-T31)如何分别持有结果矩阵D的不同元素,每个线程仅对应 $\delta_Q$ 和 $\delta_K$ 各一个scale值。

What it shows: RTX4090上headdim=128时,SageAttention2与FlashAttention2、xformers在不同序列长度下的速度对比(有/无causal mask)。
Why it matters: 直观展示SageAttn2-4b达到~480 TOPS,约为FlashAttention2的3倍和xformers的4.5倍。
Detailed description: 四张子图分别对应headdim=128 non-causal、headdim=128 causal、headdim=64 non-causal、headdim=64 causal。在所有配置下,SageAttn2-4b(红色线)均显著领先,SageAttn2-8b(橙色线)次之,两者均大幅超越FlashAttention2(蓝色线)和xformers(灰色线)。
| Model | Attention | Key Metric | Value |
|---|---|---|---|
| Llama3.1 | Full-Precision | WikiText PPL↓ | 6.013 |
| SageAttn2-8b | WikiText PPL↓ | 6.019 | |
| SageAttn2-4b | WikiText PPL↓ | 6.256 | |
| CogvideoX 1.5-5B | Full-Precision | VQA-t↑ | 70.928 |
| SageAttn2-8b | VQA-t↑ | 74.415 | |
| FlashAttn3-fp8 | VQA-t↑ | 2.181 | |
| HunyuanVideo | Full-Precision | VQA-a↑ | 82.516 |
| SageAttn2-8b | VQA-a↑ | 81.786 | |
| FlashAttn3-fp8 | VQA-a↑ | 4.433 |
Takeaway: SageAttn2-8b在所有模型上几乎无损,SageAttn2-4b有轻微损失但远优于其他baseline。FlashAttn3-fp8在视频生成模型上严重退化。
| Method | CosSim↑ | Relative L1↓ | RMSE↓ |
|---|---|---|---|
| None | 80.04% | 0.3906 | 0.2223 |
| HadmdAttn | 79.77% | 0.3782 | 0.2180 |
| SmoothAttn | 90.21% | 0.3383 | 0.1952 |
| Smooth K | 98.07% | 0.1493 | 0.0743 |
| Smooth Q | 98.30% | 0.1250 | 0.0712 |
| Smooth Q+K | 99.46% | 0.0648 | 0.0334 |
Takeaway: Smooth Q+K将CosSim从80%提升至99.46%,是所有smoothing方法中效果最好的,证明了双重smoothing的关键价值。
| Method | Cos Sim↑ | Relative L1↓ | RMSE↓ |
|---|---|---|---|
| Per-token | 99.45% | 0.0649 | 0.0335 |
| Per-thread | 99.45% | 0.0622 | 0.0313 |
| Per-block | 98.03% | 0.1492 | 0.0744 |
| Per-tensor | 97.15% | 0.1800 | 0.0865 |
Takeaway: Per-thread量化精度与per-token几乎相同(99.45% vs 99.45%),但比per-block(98.03%)显著更好,且无额外速度开销。
| Model | GPU | Original | SageAttn2-8b | SageAttn2-4b |
|---|---|---|---|---|
| CogvideoX 1.5-5B | RTX4090 | 1040s | 577s | 555s |
| HunyuanVideo | L20 | 2221s | 1486s | 1435s |
| Mochi | L20 | 2336s | 1316s | 1190s |
| Llama3.1 100K | L20 | 39.9s | 25.4s | 23.2s |
Takeaway: CogvideoX获得1.8x端到端加速,Mochi获得近2x加速,长序列LLM首token延迟显著降低。
Input: Q(FP16), K(FP16), V(FP16) ∈ R^{N×d}, block size bq, bkv, warp count cw
Preprocessing:
K = K - mean(K) // Smooth K: 减去全局token均值
(δV, V̂) = ψV(V) // Per-channel FP8 quantize V
Divide Q into Tm = N/bq blocks {Qi}
Divide K, V into Tn = N/bkv blocks {Ki}, {Vi}
for i = 1 to Tm do // 外层循环:遍历Q blocks
q̄i = mean(Qi) // 计算Q block均值
(δQ, Q̂i) = ψQ(Qi - q̄i) // Smooth Q + per-thread INT4 quantize
for j = 1 to Tn do // 内层循环:遍历K,V blocks
(δK, K̂j) = ψK(Kj) // Per-thread INT4 quantize K block
// INT4 GEMM + dequant + GEMV compensation (per warp parallel)
Sij = ψ⁻¹(Q̂i · K̂j^T) + GEMV(q̄i, Kj^T)
// Online softmax
mij = max(mi,j-1, rowmax(Sij))
P̃ij = exp(Sij - mij)
lij = exp(mi,j-1 - mij) · li,j-1 + rowsum(P̃ij)
// FP8 PV matmul (two-level accumulation)
Oij(FP22) = Matmul((P̃ij * 448).to(FP8.E4M3), Vj) // Level 1: FP22
Oij(FP32) = diag(exp(...)) · Oi,j-1(FP32) + Oij(FP22) // Level 2: FP32
end for
Oi = diag(li,Tn)⁻¹ · Oi,Tn(FP32) / 448 * δV // Final rescale
end for
return O = {Oi}
逐行解读:
Computational primitive: Attention — 包含两个核心矩阵乘法 $QK^\top$($N \times d \cdot d \times N = N \times N$)和 $PV$($N \times N \cdot N \times d = N \times d$),以及中间的softmax操作。
Input/output shapes and data types:
Theoretical arithmetic intensity: 对于Attention整体,$2N^2d$ FLOPs(两个matmul),数据量约 $6Nd$ bytes(读Q,K,V + 写O,忽略中间S,P的HBM访问因FlashAttention tiling)。$\text{AI} \approx 2N^2d / 6Nd = N/3$。当 $N > 1000$ 时远超大多数GPU的计算/带宽比,是compute-bound。
Roofline position: 对于 $N \geq 4K$, $d = 128$ 的典型配置,Attention是计算密集型操作,使用低精度tensor core(INT4/FP8)正好能最大化计算吞吐。
Target architectures:
Hardware units:
Memory hierarchy:
Theoretical peak on RTX4090: INT4 tensor core理论峰值约660 TOPS(mma.m16n8k64),论文实测达到481 TOPS(约73%利用率)。FlashAttention2在FP16约160 TOPS。

解读: 如Figure 3所示,SageAttention2的完整pipeline包含预处理(smooth + quantize)和kernel内部(INT4 $QK^\top$ + FP8 $PV$ + two-level accumulation)两个阶段。预处理中smooth和quantize操作被fuse成单个kernel以避免额外的HBM读写。
| Technique | Applied? | Details |
|---|---|---|
| Tiling / blocking | ✅ | FlashAttention-style, $b_q = 128$, $b_{kv} = 64$, 4 warps per SM |
| Software pipelining | 部分 | K,V tiles的加载与前一个tile的计算重叠 |
| Warp specialization | ✅ | 4个warp并行处理Q block的不同段(cw=4) |
| Vectorized memory access | ✅ | INT4数据紧凑存储,128-bit向量化加载 |
| Shared memory optimization | ✅ | 标准FlashAttention共享内存布局 |
| Register pressure | 中等 | FP32累加器Oij + scale + softmax状态 |
| Kernel fusion | ✅ | smooth + quantize + GEMV fuse为单个预处理kernel |

解读: 如Figure 5所示,SageAttn2-4b在RTX4090上随序列长度增加趋近481 TOPS,而FlashAttention2稳定在约160 TOPS,加速比约3x。在短序列(<2K)时加速比略低,因为预处理kernel的固定开销占比更大。
Peak utilization: 481 TOPS / ~660 TOPS(INT4理论峰值)≈ 73%,在实际attention workload中是很好的利用率。
vs vendor library:
| Method | RTX4090 TOPS | vs FA2 |
|---|---|---|
| SageAttn2-4b | 481 | 3.0x |
| SageAttn2-8b | ~430 | 2.7x |
| FlashAttention2 | ~160 | 1.0x |
| xformers | ~107 | 0.67x |
Scaling: 速度随序列长度增加趋于稳定(compute-bound区域)。在短序列下($N < 1K$),固定开销(smooth、quantize预处理)占比增大,加速比下降。
跨GPU表现: 如Table 9所示,SageAttention2在不同GPU上的加速比从2.46x(L20)到3.12x(H20)不等,Hopper上使用8b变体仍能匹配FlashAttention3(fp8)速度。

解读: 如Figure 4所示,per-thread量化的核心是将MMA指令的线程布局映射为量化分组。左图展示了每个warp内token与量化scale的对应——token $i$, $8+i$, $16+i$, $24+i$ 共享一个scale,形成32个量化组(8×4 warps),比per-block(1个scale)细32倍。右图展示MMA指令中每个线程仅持有D矩阵128个元素中的4个,且这4个元素恰好对应同一个Q scale和同一个K scale。
Accuracy vs baseline:
Numerical stability tricks:
Edge cases: 论文未明确讨论NaN/Inf/subnormal处理。INT4范围[-7,+7]加上smoothing后,实际输入值应在合理范围内。
| Dimension | Assessment |
|---|---|
| Cross-GPU within NVIDIA | Ada全功能(INT4 TC),Hopper降级为INT8变体(无INT4 TC),Ampere未提及 |
| Cross-vendor | CUDA-only,依赖PTX mma指令,无HIP/Triton版本 |
| Integration | 独立CUDA kernel,可作为drop-in替换FlashAttention |
| Compiler dependency | 需要CUDA 12.x+(PTX mma.m16n8k64 INT4指令支持) |
// [Grid level] Each CUDA block processes one Q tile (bq tokens)
// Grid dim: (N/bq, num_heads, batch_size)
for i = 1 to Tm do // → maps to block index
// [Block level] Compute Q block mean and smooth (SRAM → registers)
// This is compute-bound: FP16 reduction over bq tokens
q̄i = mean(Qi) // Shared memory reduction
(δQ, Q̂i) = ψQ(Qi - q̄i) // Per-thread INT4 quantize in registers
for j = 1 to Tn do // → sequential K,V tile iterations
// [Block level] Load Kj from HBM → SRAM, quantize
(δK, K̂j) = ψK(Kj) // Per-thread INT4 quantize
// [Warp level] INT4 GEMM via mma.m16n8k64 (compute-bound)
// Each of 4 warps processes cw=bq/4=32 query tokens
// Dequantization: each thread multiplies by its single δQ·δK
Sij = ψ⁻¹(Q̂i · K̂j^T) // Tensor Core (INT4)
// [Warp level] GEMV compensation (memory-bound, small)
Sij += GEMV(q̄i, Kj^T) // CUDA cores, 1×N vector
// [Thread level] Online softmax (registers)
// Standard FlashAttention online softmax
mij, P̃ij, lij = online_softmax(Sij)
// [Warp level] FP8 PV matmul (compute-bound)
// Static scale: P̃ * 448 → FP8 E4M3 (no dynamic quantization)
// Two-level accumulation:
// Level 1: mma(f32f8f8f32) → FP22 result in registers
// Level 2: accumulate to FP32 buffer (also in registers)
Oij(FP22) = Matmul(P̃ij.to(FP8), Vj) // Tensor Core (FP8)
Oij(FP32) += Oij(FP22) // FP32 register accumulation
// [Sync] Barrier before loading next K,V tile
end for
// [Block level] Final rescale and writeback to HBM
Oi = normalize(Oij) * δV // Apply V per-channel scale
// Write Oi to global memory (HBM)
end for
| Layer | Impact |
|---|---|
| Algorithm | 为所有基于标准Attention的模型提供免费的推理加速,与sparse attention、linear attention正交 |
| Framework | 可作为PyTorch custom op集成,已有Python wrapper;适合集成到vLLM、SGLang等serving框架 |
| LLM | 长上下文推理(100K+ tokens)首token延迟降低40-50%,直接提升用户体验;使更长上下文窗口在现有硬件上可行 |
| Agent | Attention延迟降低使实时multi-turn agent更可行,特别是需要处理长对话历史或大量工具输出的场景 |