arXiv:2505.18698cs.LG2025-05NeurIPS被引 5

用结构化矩阵实现零样本快速注意力,显著提速且无需重训练。

MonarchAttention: Zero-Shot Conversion to Fast, Hardware-Aware Structured Attention

  • 通过蒙德矩阵近似注意力,将复杂度降至亚二次方。
  • 在不同长度序列上比FlashAttention-2快1.4到8.2倍。
  • 无需训练即可替换所有注意力层,适配现代GPU硬件。

Transformer 在各类任务中表现优异,但其注意力机制导致序列长度呈二次复杂度增长。本文提出 MonarchAttention,一种基于蒙德矩阵的亚二次注意力近似方法。基于软最大函数的变分形式,设计了一种高效优化算法,实现对软最大注意力的近似投影,计算复杂度为 $Θ(N\sqrt{N} d)$,内存与输入输出复杂度为 $Θ(Nd)$。与以往方法不同,MonarchAttention 具备(1)可迁移性:替换任意注意力层后性能损失极小,无需额外训练;(2)硬件友好性:充分利用现代GPU的高吞吐张量核心。通过优化内核,MonarchAttention 在实际运行时间上显著超越 FlashAttention-2:短序列($N=256$)提升1.4倍,中长序列($N=4K$)达4.5倍,长序列($N=16K$)达8.2倍。我们在视觉与语言多种任务和架构上验证了其效果,证明其在多种场景下能灵活、准确地近似软最大注意力。代码已开源:https://github.com/cjyaras/monarch-attention。

原文摘要 · Abstract (English)

Transformers have achieved state-of-the-art performance across various tasks, but suffer from a notable quadratic complexity in sequence length due to the attention mechanism. In this work, we propose MonarchAttention -- a novel approach to sub-quadratic attention approximation via Monarch matrices, an expressive class of structured matrices. Based on the variational form of softmax, we describe an efficient optimization-based algorithm to compute an approximate projection of softmax attention onto the class of Monarch matrices with $Θ(N\sqrt{N} d)$ computational complexity and $Θ(Nd)$ memory/IO complexity. Unlike previous approaches, MonarchAttention is both (1) transferable, yielding minimal performance loss with no additional training, even when replacing every attention layer of the Transformer, and (2) hardware-efficient, utilizing the highest-throughput tensor core units on modern GPUs. With optimized kernels, MonarchAttention achieves substantial speed-ups in wall-time over FlashAttention-2: $1.4\times$ for shorter sequences $(N=256)$, $4.5\times$ for medium-length sequences $(N=4K)$, and $8.2\times$ for longer sequences $(N=16K)$. We demonstrate the quality of MonarchAttention on diverse tasks and architectures in vision and language problems, showing that it flexibly and accurately approximates softmax attention in a variety of contexts. Our code is available at https://github.com/cjyaras/monarch-attention.

注意力机制模型加速硬件优化

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。