arXiv:2502.01659cs.LGcs.AI2025-02被引 3

用图计算优化注意力机制,让Transformer处理超长序列

Longer Attention Span: Increasing Transformer Context Length with Sparse Graph Processing Techniques

  • 将注意力视为图计算,只计算必要连接,实现真正稀疏
  • 在A100上实现1.6亿长度序列,速度远超FlashAttention
  • 适合需要长序列建模的NLP、基因组分析等场景

Transformer在自然语言处理和生物信息学等领域取得成功,主要得益于其注意力机制对序列中词元间成对交互的建模。然而,该机制随上下文长度呈二次方增长的内存与时间复杂度,严重限制了可处理序列长度。尽管已有研究通过引入稀疏注意力掩码降低复杂度,但高效实现真正的稀疏性仍不足。本文提出将注意力视为图计算:词元为节点,注意力掩码决定边。基于此,开发图处理算法实现注意力机制。理论上和实验上均证明,算法仅执行必要计算,达到工作最优。通过大量实验验证多种常见注意力掩码的效果,结果表明在长序列下相比FlashAttention等先进实现有显著加速。在单张NVIDIA A100 GPU(SXM4 80GB)上,可支持最高达1.6亿长度的序列输入。

原文摘要 · Abstract (English)

Transformers have demonstrated great success in numerous domains including natural language processing and bioinformatics. This success stems from the use of the attention mechanism by these models in order to represent and propagate pairwise interactions between individual tokens of sequential data. However, the primary limitation of this operation is its quadratic memory and time complexity in relation to the input's context length - the length of a sequence over which the interactions need to be captured. This significantly limits the length of sequences that can be inferred upon by these models. Extensive research has been conducted to reduce the number of pairwise interactions to sub-quadratic in relation to the context length by introducing sparsity into the attention mechanism through the development of sparse attention masks. However, efficient implementations that achieve "true sparsity" are lacking. In this work, we address this issue by proposing a graph computing view of attention where tokens are perceived as nodes of the graph and the attention mask determines the edges of the graph. Using this view, we develop graph processing algorithms to implement the attention mechanism. Both theoretically and empirically, we demonstrate that our algorithms only perform the needed computations, i.e., they are work optimal. We also perform extensive experimentation using popular attention masks to explore the impact of sparsity on execution time and achievable context length. Our experiments demonstrate significant speedups in execution times compared to state-of-the-art attention implementations such as FlashAttention for large sequence lengths. We also demonstrate that our algorithms are able to achieve extremely long sequence lengths of as high as 160 million on a single NVIDIA A100 GPU (SXM4 80GB).

注意力机制长序列图计算稀疏性

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