arXiv:2410.05462cs.LGcs.DS2024-10ICLR被引 3

提出高效注意力算法,让长序列模型更快更省内存。

LevAttention: Time, Space, and Streaming Efficient Algorithm for Heavy Attentions

  • 用杠杆得分找关键键值,大幅降低计算量。
  • 线性时间处理长序列,无需额外存储空间。
  • 适合大规模视觉模型训练,支持流式并行计算。

Transformer的核心问题在于:给定两个 $n \times d$ 矩阵 $Q$ 和 $K$,以及一个非负函数 $f$,定义矩阵 $A$ 如下:(1) 对 $Q K^T$ 的每个元素应用函数 $f$,(2) 归一化 $A$ 每行的和为 1。标准方法耗时 $O(n^2 d)$,但 $n$ 代表上下文长度时,二次复杂度难以承受。对于一类广义函数 $f(x) = |x|^p$,我们证明可在 $n \cdot \textrm{poly}(d/\varepsilon)$ 时间内找到所有大于阈值 $\varepsilon$ 的注意力分数。关键发现是:对任意 $K$,存在一个与 $n$ 无关的“通用集合” $U \subset [n]$,使得每行中所有大注意力分数对应的键索引均属于 $U$,且该集合可在相同时间内构造。算法不依赖数据分布,内存恒定,支持流式与并行计算。我们将此机制命名为 LevAttention,基于杠杆得分识别 $U$。实验验证其在视觉变换器中的有效性,表明模型可自适应选择重要键值,实现高效训练。

原文摘要 · Abstract (English)

A central problem related to transformers can be stated as follows: given two $n \times d$ matrices $Q$ and $K$, and a non-negative function $f$, define the matrix $A$ as follows: (1) apply the function $f$ to each entry of the $n \times n$ matrix $Q K^T$, and then (2) normalize each of the row sums of $A$ to be equal to $1$. The matrix $A$ can be computed in $O(n^2 d)$ time assuming $f$ can be applied to a number in constant time, but the quadratic dependence on $n$ is prohibitive in applications where it corresponds to long context lengths. For a large class of functions $f$, we show how to find all the ``large attention scores", i.e., entries of $A$ which are at least a positive value $\varepsilon$, in time with linear dependence on $n$ (i.e., $n \cdot \textrm{poly}(d/\varepsilon)$) for a positive parameter $\varepsilon > 0$. Our class of functions include all functions $f$ of the form $f(x) = |x|^p$, as explored recently in transformer models. Using recently developed tools from randomized numerical linear algebra, we prove that for any $K$, there is a ``universal set" $U \subset [n]$ of size independent of $n$, such that for any $Q$ and any row $i$, the large attention scores $A_{i,j}$ in row $i$ of $A$ all have $j \in U$. We also find $U$ in $n \cdot \textrm{poly}(d/\varepsilon)$ time. Notably, we (1) make no assumptions on the data, (2) our workspace does not grow with $n$, and (3) our algorithms can be computed in streaming and parallel settings. We call the attention mechanism that uses only the subset of keys in the universal set as LevAttention since our algorithm to identify the universal set $U$ is based on leverage scores. We empirically show the benefits of our scheme for vision transformers, showing how to train new models that use our universal set while training as well, showing that our model is able to consistently select ``important keys'' during training.

注意力机制长序列高效计算视觉模型

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