Skip to main content
QUICK REVIEW

[论文解读] Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention

Jingyang Yuan, Huazuo Gao|ArXiv.org|Feb 16, 2025
Advanced Neural Network ApplicationsComputer Science被引用 3
一句话总结

NSA 引入包含压缩、选择和滑动窗口策略的三分支原生稀疏注意力,并具备硬件对齐的内核,支持端到端训练与快速长上下文建模,在长上下文任务中优于全注意力基线。

ABSTRACT

Long-context modeling is crucial for next-generation language models, yet the high computational cost of standard attention mechanisms poses significant computational challenges. Sparse attention offers a promising direction for improving efficiency while maintaining model capabilities. We present NSA, a Natively trainable Sparse Attention mechanism that integrates algorithmic innovations with hardware-aligned optimizations to achieve efficient long-context modeling. NSA employs a dynamic hierarchical sparse strategy, combining coarse-grained token compression with fine-grained token selection to preserve both global context awareness and local precision. Our approach advances sparse attention design with two key innovations: (1) We achieve substantial speedups through arithmetic intensity-balanced algorithm design, with implementation optimizations for modern hardware. (2) We enable end-to-end training, reducing pretraining computation without sacrificing model performance. As shown in Figure 1, experiments show the model pretrained with NSA maintains or exceeds Full Attention models across general benchmarks, long-context tasks, and instruction-based reasoning. Meanwhile, NSA achieves substantial speedups over Full Attention on 64k-length sequences across decoding, forward propagation, and backward propagation, validating its efficiency throughout the model lifecycle.

研究动机与目标

  • 推动长上下文建模并缓解 Transformer 的注意力瓶颈。
  • 开发可端到端训练的原生稀疏注意力机制。
  • 通过分层令牌重映射(压缩、选择、滑动窗口)在全局上下文与局部精度之间取得平衡。
  • 实现硬件高效的实现,以在现代加速器上最大化算术强度。
  • 在通用与长上下文基准上实现与全注意力相当或更优的性能,同时带来显著的加速。

提出的方法

  • 引入三条并行注意力分支:压缩、选择和滑动窗口,并通过门控聚合进行组合。
  • 使用学习的多层感知机对块级进行令牌压缩,计算压缩的键和值。
  • 基于压缩注意力得到的块级重要性分数选择顶部块,且为共享的 KV 缓存聚合分数。
  • 保留一个滑动窗口分支以维护本地上下文信息。
  • 提供内核设计,用于加载分组查询和共享 KV 块,以最大化张量核心的利用率和内存效率。
  • 在长序列上使用 27B 参数骨干网络,结合 GQA 与 MoE,进行端到端的 NSA 训练,以实现硬件对齐的稀疏计算。

实验结果

研究问题

  • RQ1本地原生稀疏注意力结合分层重映射是否能够在长上下文任务中维持或提升相对于全注意力的性能?
  • RQ2硬件对齐的实现与可训练稀疏性是否能够在解码、前向和反向传播中实现端到端训练并带来显著加速?
  • RQ3压缩、选择和滑动窗口分支如何在全球上下文意识与局部精度之间做出贡献?
  • RQ4在不影响梯度流的前提下,可否进行端到端训练且没有非可训练组件阻碍?
  • RQ5NSA 是否能够扩展到非常长的上下文(64k+)并在通用与推理基准上保持精度?

主要发现

  • NSA 在通用基准和长上下文任务上达到与全注意力相当或更优的性能。
  • 在 64k 长序列上,NSA 在解码、前向与反向传播方面相较于全注意力获得显著加速,并且对更长序列的加速提升更大。
  • 使用 NSA 的预训练在推理推理基准(如 DROP 与 GSM8K)上取得提升,并在 MMLU、CMMLU 及编码任务上保持强劲表现。
  • 在长上下文评估中,NSA 在 64k 上的 needle-in-a-haystack 测试实现了完美检索准确率。
  • 在 LongBench 中,NSA 达到了评估方法中的最高平均分,超越 Full Attention 与 Exact-Top。
  • 在链式思维风格的评估中,NSA-R 在 8k 和 16k 上超过 Full Attention-R 的 AIME 风格数学推理。

阅读本论文

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

在 Nubint 中阅读本论文

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