Skip to main content
QUICK REVIEW

[论文解读] Sparser is Faster and Less is More: Efficient Sparse Attention for Long-Range Transformers

Chao Lou, Zixia Jia|arXiv (Cornell University)|Jun 24, 2024
Advanced Memory and Neural ComputingEngineering被引用 3
一句话总结

本文提出 SparseK Attention,一种可微分的稀疏注意力机制,通过可学习的打分网络和可微分的 top-k 掩码操作符,为每个查询选择固定数量的键值对。该方法在推理过程中实现线性时间复杂度和常数内存消耗,相较于先前方法在速度和效率方面表现更优,同时在长上下文语言建模和下游任务中保持了强劲的性能。

ABSTRACT

Accommodating long sequences efficiently in autoregressive Transformers, especially within an extended context window, poses significant challenges due to the quadratic computational complexity and substantial KV memory requirements inherent in self-attention mechanisms. In this work, we introduce SPARSEK Attention, a novel sparse attention mechanism designed to overcome these computational and memory obstacles while maintaining performance. Our approach integrates a scoring network and a differentiable top-k mask operator, SPARSEK, to select a constant number of KV pairs for each query, thereby enabling gradient-based optimization. As a result, SPARSEK Attention offers linear time complexity and constant memory footprint during generation. Experimental results reveal that SPARSEK Attention outperforms previous sparse attention methods and provides significant speed improvements during both training and inference, particularly in language modeling and downstream tasks. Furthermore, our method can be seamlessly integrated into pre-trained Large Language Models (LLMs) with minimal fine-tuning, offering a practical solution for effectively managing long-range dependencies in diverse applications.

研究动机与目标

  • 解决长上下文 Transformer 中自注意力机制的二次方计算与内存开销问题。
  • 克服先前可学习稀疏注意力方法的局限性,包括超线性训练复杂度和高 KV 内存开销。
  • 为自回归解码器中的长序列建模实现高效、基于梯度的训练与推理。
  • 通过解耦 KV 选择与查询访问,实现计算效率与内存效率的双重提升,并支持增量式、不可逆的掩码操作。
  • 与预训练大语言模型无缝集成,仅需极少微调即可实现实际部署。

提出的方法

  • 引入一个打分网络,独立于查询评估 KV 对的重要性,实现与查询无关的选择。
  • 提出一种可微分的 top-k 掩码操作符 SparseK,通过阈值化机制在时间复杂度为线性的前提下计算精确的软门控。
  • 将 KV 选择与注意力计算融合为单一 Triton 内核,实现 I/O 友好的优化并提升运行速度。
  • 在训练与推理阶段采用相同的稀疏度水平,确保效率一致性并避免重复计算。
  • 在自回归解码过程中支持 KV 对的增量式评估,保持线性复杂度。
  • 设计一个后向传播内核,将梯度计算与 SparseK 融合,实现内存与速度效率的双重优化。
Figure 1 : Left: SparseK operation in the attention module. KV pairs are scored by $\mathbf{u}$ . SparseK computes a threshold for each query ( $\tau(\mathbf{u})$ ) such that the sum of normalized scores is $k$ , which is 3 in this example. We select top- $k$ KV pairs (orange cells) to perform atten
Figure 1 : Left: SparseK operation in the attention module. KV pairs are scored by $\mathbf{u}$ . SparseK computes a threshold for each query ( $\tau(\mathbf{u})$ ) such that the sum of normalized scores is $k$ , which is 3 in this example. We select top- $k$ KV pairs (orange cells) to perform atten

实验结果

研究问题

  • RQ1可微分的稀疏注意力机制是否能在长上下文 Transformer 的训练与推理过程中均实现线性时间复杂度?
  • RQ2将 KV 打分与查询访问分离,是否能在保持性能的同时实现推理过程中的常数内存占用?
  • RQ3与现有可学习稀疏注意力方法相比,所提出的 SparseK 操作符在训练速度与推理效率方面表现如何?
  • RQ4SparseK 在多大程度上可与预训练大语言模型集成,且仅需极少微调即可用于长上下文任务?
  • RQ5该方法在长上下文长度(例如 16K token)下是否能保持速度与准确率优于全连接注意力?

主要发现

  • SparseK Attention 将训练时间复杂度从二次方降低至线性,在 8192 上下文长度下,Pythia-160M 模型的相对训练时间仅为全连接注意力的 68.9%。
  • 通过立即移除被掩码的 KV 对,该方法在推理过程中实现了常数内存开销,消除了冗余存储。
  • 在 4096 token 序列、每查询选择 1024 个 KV 对的情况下,由于计算融合与稀疏感知访问,Triton 内核的运行速度优于 FlashAttention。
  • 在相同上下文长度下,SparseK 在语言建模与下游任务中的表现与全连接注意力或其他稀疏方法相当或更优。
  • 该方法可无缝集成至 GPT-2 和 Pythia 等预训练模型中,仅需极少微调,展现出良好的实际可部署性。
  • 可视化结果表明,软选择机制学习到了有意义的注意力模式,高分值集中于语义相关的词元。
Figure 2 : Perplexity on the held-out set of fine-tuned models. L denotes the training context length.
Figure 2 : Perplexity on the held-out set of fine-tuned models. L denotes the training context length.

阅读本论文

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

在 Nubint 中阅读本论文

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