SageAttention2 Technical Report: Accurate 4 Bit Attention for Plug-and-play Inference Acceleration
SageAttention2通过4位量化技术和精度增强方法,实现了准确且高效的注意力计算,比FlashAttention2和xformers在RTX4090上的运算速度分别快3.1倍和5.4倍。
更新于 2026-01-05。作者:Jintao Zhang、Haofeng Huang、Pengle Zhang、Jia Wei、Jun Zhu、Jianfei Chen。
中文摘要
尽管线性层的量化已经被广泛应用,但其应用于加速注意力过程的应用仍然有限。SageAttention采用了8位矩阵乘法、16位矩阵乘法(带有16位累加器)和精度增强方法,实现了相较于FlashAttention2的准确性和2倍速度的内核。为了进一步提高注意力计算的效率同时保持精度,我们提出了SageAttention2,它利用了显著更快的4位矩阵乘法(Matmul)以及额外的精度增强技术。首先,我们建议以warp级别的粒度将矩阵(Q, K)量化为INT4,并将矩阵(widetilde P, V)量化为FP8。其次,我们提出了一种平滑Q和V的方法,通过INT4 QK和FP8 PV增强了注意力的准确性。第三,我们分析了跨时间步长和层的量化精度,然后提出了一种自适应量化方法,以确保跨各种模型的端到端指标。SageAttention2在RTX4090上的每秒操作数(OPS)比FlashAttention2和xformers分别高出约3倍和5倍。全面的实验证实,我们的方法在各种模型上,包括大型语言处理、图像生成和视频生成模型,都不会造成可忽略的端到端指标损失。代码可在https://github.com/thu-ml/SageAttention找到。
英文摘要
Although quantization for linear layers has been widely used, its application to accelerate the attention process remains limited. SageAttention utilizes 8-bit matrix multiplication, 16-bit matrix multiplication with 16-bit accumulator, and precision-enhancing methods, implementing an accurate and 2x speedup kernel compared to FlashAttention2. To further enhance the efficiency of attention computation while maintaining precision, we propose SageAttention2, which utilizes significantly faster 4-bit matrix multiplication (Matmul) alongside additional precision-enhancing techniques. First, we propose to quantize matrixes (Q, K) to INT4 in a warp-level granularity and quantize matrixes (widetilde P, V) to FP8. Second, we propose a method to smooth Q and V, enhancing the accuracy of attention with INT4 QK and FP8 PV. Third, we analyze the quantization accuracy across timesteps and layers, then propose an adaptive quantization method to ensure the end-to-end metrics over various models. The operations per second (OPS) of SageAttention2 surpass FlashAttention2 and xformers by about 3x and 5x on RTX4090, respectively. Comprehensive experiments confirm that our approach incurs negligible end-to-end metrics loss across diverse models, including those for large language processing, image generation, and video generation. The codes are available at https://github.com/thu-ml/SageAttention.
解读
一句话总结
SageAttention2通过4位量化技术和精度增强方法,实现了准确且高效的注意力计算,比FlashAttention2和xformers在RTX4090上的运算速度分别快3.1倍和5.4倍。
问题1:这篇论文想要解决什么具体问题?
• 问题背景:虽然量化技术在线性层中已被广泛应用,但其应用于注意力过程的加速仍有限。 • 现有方案不足:SageAttention虽然提高了精度,但速度提升有限,且仅适用于特定的GPU。 • 研究目标:提出SageAttention2,通过使用更快的4位矩阵乘法以及额外的精度增强技术,实现高效的注意力计算。
问题2:论文的核心创新点是什么?
• 技术创新:提出了一种新的量化策略,将Q和K量化为INT4,将P和V量化为FP8。 • 方法改进:提出了平滑Q和V的方法,以增强使用INT4 QK和FP8 PV时的注意力精度。 • 优势:在保持精度的情况下,SageAttention2在RTX4090和L20 GPU上的运算速度比FlashAttention2和xformers快3.1倍和5.4倍。
问题3:实验结果如何验证了方法的有效性?
• 关键实验:在多种模型(包括文本、图像和视频生成模型)上进行了广泛的实验。 • 性能提升:SageAttention2在RTX4090上的运算速度比FlashAttention2和xformers分别快3.1倍和5.4倍。 • 对比结果:SageAttention2在各种模型和任务上实现了可忽略的端到端性能损失。
问题4:这个研究的实际应用价值是什么?
• 应用场景:适用于需要高效注意力计算的领域,如语言、图像和视频生成。 • 实施建议:可直接应用于现有模型,无需修改模型结构。 • 局限与展望:未来工作将包括在Hopper架构上实现FP8 MatMul和SageAttention2。