arXiv:2505.15080cs.LGcs.AI2025-05

通过随机剪枝注意力梯度流,实现长序列Transformer的线性反向传播。

SUS backprop: linear backpropagation algorithm for long inputs in transformers

  • 用单参数概率规则剪裁大部分注意力梯度路径,仅保留每头每令牌最多c条连接。
  • 当c=25-30时,梯度方差仅增1%,反向传播计算量从O(n²)降至O(nc)。
  • 适合训练超长序列(如2000词)的Transformer模型,尤其对算力敏感场景有优势。

设计一种无偏梯度估计器,可随机截断计算图中任意部分的反向传播流。通过剪除对计算影响小的部分,可在某些情况下显著减少反向传播计算量,同时仅小幅增加梯度方差。这一情况出现在Transformer架构的注意力机制中:对于长序列,注意力计算随序列长度n呈二次增长,而多数注意力权重极小,即大多数注意力头仅连接当前词与序列中少数词。这些小权重成为剪裁的理想目标。我们提出一个由单参数c控制的简单概率规则,剪裁大部分注意力权重,每头每令牌最多保留c个交互。这使注意力反向传播计算量降低为c/n倍,复杂度从O(n²)变为O(nc)。实验证明,对典型Transformer模型,剪裁约99%注意力梯度流(即c≈25-30),在n≈2000时相对梯度方差仅增加约1%,且随n增大而下降。该方法可高效实现稀疏矩阵运算,有望使长序列Transformer训练中反向传播开销远低于前向传播。

原文摘要 · Abstract (English)

It is straightforward to design an unbiased gradient estimator that stochastically cuts the backpropagation flow through any part of a computational graph. By cutting the parts that have little effect on the computation, one can potentially save a significant amount of backpropagation computation in exchange for a minimal increase in the stochastic gradient variance, in some situations. Such a situation occurs in the attention mechanism of the transformer architecture. For long sequences, attention becomes the limiting factor, as its compute requirements increase quadratically with sequence length $n$. At the same time, most attention weights become very small, as most attention heads tend to connect a given token with only a small fraction of other tokens in the sequence. These weights become promising targets for cutting backpropagation. We propose a simple probabilistic rule controlled by a single parameter $c$ that cuts back-propagation through most attention weights, leaving at most $c$ interactions per token per attention head. This brings a factor of $c/n$ reduction in the compute required for the attention backpropagation, turning it from quadratic $O(n^2)$ to linear complexity $O(nc)$. We have empirically verified that, for a typical transformer model, cutting about $99\%$ of the attention gradient flow (i.e. choosing $c \sim 25-30$) results in relative gradient variance increase of only about $1\%$ for $n \sim 2000$, and it decreases with $n$. This approach is amenable to efficient sparse matrix implementation, thus being promising for making the cost of a backward pass negligible relative to the cost of a forward pass when training a transformer model on long sequences.

Transformer反向传播注意力机制长序列

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