原生稀疏注意力:硬件对齐且可原生训练的稀疏注意力机制

Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention

深度求索 DeepSeek-AI · DeepSeek · 2025-02-16 · arXiv:2502.11089 ↗ · 被引 402

打开互动全文版(逐段中英对照 + 图/公式 + 论文问答)→

摘要 · Abstract

长上下文建模对下一代语言模型至关重要,但标准注意力机制的高计算成本带来了显著的计算挑战。稀疏注意力为在保持模型能力的同时提高效率提供了一个有前景的方向。我们提出了 NSA,一种可原生训练的稀疏注意力机制,它将算法创新与硬件对齐优化相结合,以实现高效的长上下文建模。NSA 采用动态分层稀疏策略,结合粗粒度令牌压缩和细粒度令牌选择,以保持全局上下文感知和局部精度。我们的方法通过两个关键创新推进了稀疏注意力设计:(1)通过算术强度平衡的算法设计实现显著加速,并针对现代硬件进行实现优化。(2)实现端到端训练,在不牺牲模型性能的情况下减少预训练计算。如图 1 所示,实验表明,使用 NSA 预训练的模型在通用基准、长上下文任务和基于指令的推理上保持或超越全注意力模型。同时,NSA 在 64k 长度序列的解码、前向传播和反向传播中实现了比全注意力显著的加速,验证了其在模型整个生命周期中的效率。

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.

核心贡献 · Key contributions

局限 · Limitations

论文章节 · Sections(共 26)

阅读逐段中英对照全文 →