Skip to main content
QUICK REVIEW

[论文解读] HyperAttention: Long-context Attention in Near-Linear Time

In‐Su Han, Rajesh Jayaram|arXiv (Cornell University)|Oct 9, 2023
Advanced Neural Network ApplicationsComputer Science被引用 9
一句话总结

HyperAttention 引入了一种近线性时间的近似注意力方法,使用对扰动容忍的对角 D 近似和基于行范数采样的 AMM,使因果屏蔽成为可能,并在长上下文上实现实际的加速。

ABSTRACT

We present an approximate attention mechanism named HyperAttention to address the computational challenges posed by the growing complexity of long contexts used in Large Language Models (LLMs). Recent work suggests that in the worst-case scenario, quadratic time is necessary unless the entries of the attention matrix are bounded or the matrix has low stable rank. We introduce two parameters which measure: (1) the max column norm in the normalized attention matrix, and (2) the ratio of row norms in the unnormalized attention matrix after detecting and removing large entries. We use these fine-grained parameters to capture the hardness of the problem. Despite previous lower bounds, we are able to achieve a linear time sampling algorithm even when the matrix has unbounded entries or a large stable rank, provided the above parameters are small. HyperAttention features a modular design that easily accommodates integration of other fast low-level implementations, particularly FlashAttention. Empirically, employing Locality Sensitive Hashing (LSH) to identify large entries, HyperAttention outperforms existing methods, giving significant speed improvements compared to state-of-the-art solutions like FlashAttention. We validate the empirical performance of HyperAttention on a variety of different long-context length datasets. For example, HyperAttention makes the inference time of ChatGLM2 50\% faster on 32k context length while perplexity increases from 5.6 to 6.3. On larger context length, e.g., 131k, with causal masking, HyperAttention offers 5-fold speedup on a single attention layer.

研究动机与目标

  • 动机:克服 Transformer 在长序列中注意力的二次时间和内存复杂度。
  • 目标:设计在长上下文和因果屏蔽下可实现光谱保证的近似注意力机制。
  • 目的:在接近线性时间内实现同时在真实大语言模型和长上下文基准上保持性能。

提出的方法

  • 为注意力输出定义一个基于采样的近似,以保持光谱特性。
  • 利用 Hamming 排序的 LSH 来定位注意力矩阵中的大条目,并形成一个稀疏的主导条目掩码。
  • 用近线性时间的估计器近似对角缩放 D,该估计器依赖于键/行的均匀采样。
  • 通过按平方行范数对 V 的行进行采样来近似矩阵乘法 D^{-1}AV,以近似。
  • 通过递归分块方案将该方法扩展到因果屏蔽。
  • 给出一个主要理论保证(HyperAttention),使输出在谱范数误差 ε 内近似 Att,且运行时间近似线性。
Figure 2: Causal attention matrix can be divided into three equal-sized non-zero sections: ${\bm{M}}^{\mathcal{C}}_{1}\odot{\bm{A}}_{11}$ and ${\bm{M}}^{\mathcal{C}}_{2}\odot{\bm{A}}_{22}$ are both causal attention matrices, and ${\bm{A}}_{21}$ is an unmasked attention matrix.
Figure 2: Causal attention matrix can be divided into three equal-sized non-zero sections: ${\bm{M}}^{\mathcal{C}}_{1}\odot{\bm{A}}_{11}$ and ${\bm{M}}^{\mathcal{C}}_{2}\odot{\bm{A}}_{22}$ are both causal attention matrices, and ${\bm{A}}_{21}$ is an unmasked attention matrix.

实验结果

研究问题

  • RQ1在长上下文长度下,是否能实现点积注意力的近线性时间近似,同时保持谱属性?
  • RQ2是否通过重条目检测加上基于行范数的采样,能够在不要求条目有界或低稳定秩的条件下提供可行且可证明的保证?
  • RQ3在因果屏蔽下,HyperAttention 在预训练的大模型上对长上下文基准的表现如何?

主要发现

  • HyperAttention 在温和假设下实现了接近线性时间复杂度的注意力近似。
  • 经验上,在不进行屏蔽的情况下前向/反向速度提升显著(例如在131k上下文下>50x 加速;在带因果屏蔽时约5x),同时在长上下文任务上维持有竞争力的困惑度。
  • 将其通过猴子补丁整合到预训练模型中(如 ChatGLM2-6B-32k、Phi-1.5)时,能够显著提升速度,且在某些任务上仅有适度性能下降,尤其在摘要和代码任务上保持质量。
  • 该方法通过递归分区方案(算法4)实现因果屏蔽,在LongBench等长上下文数据集上仍然有效。
  • 关于 softmax 注意力的行范数的经验性假设(α 上界)在实践中成立,支持该方法的可行性。
(a) $\mathtt{chatglm2}$ - $\mathtt{6b}$ - $\mathtt{32k}$
(a) $\mathtt{chatglm2}$ - $\mathtt{6b}$ - $\mathtt{32k}$

阅读本论文

在 Nubint 阅读器中打开原文,边读边就任意段落提问。

在 Nubint 中阅读本论文

本解读由 AI 根据论文原文生成,引用前请核对原文。