长上下文 LLM 的推理成本一直是痛点,DashAttention 用可微分稀疏注意力在保持精度的同时大幅提速,做长文本推理和模型优化的研究者值得关注。
DashAttention 提出了一种新的分层注意力机制,通过可微分的 α-entmax 变换替代传统 top-k 操作,自适应地为每个查询选择可变数量的关键值块,从而解决了现有方法(如 NSA 和 InfLLMv2)中固定块数和梯度阻断的问题。该方法保持整个层次结构完全可微分,且具有非分散性,提升了长上下文建模能力。实验表明,在 75% 稀疏度下,DashAttention 的准确率与全注意力相当,在高稀疏场景下优于 NSA 和 InfLLMv2。其基于 Triton 的 GPU 实现推理速度甚至超过 FlashAttention-3。DashAttention 为长上下文模型提供了一种高效且经济的方案。
DashAttention: Differentiable and Adaptive Sparse Hierarchical Attention
Current hierarchical attention methods, such as NSA and InfLLMv2, select the top-k relevant key-value (KV) blocks based on coarse attention scores and subsequently apply fine-grained softmax attention on the selected tokens. However, the top-k operation assumes the number of relevant tokens for any query is fixed and it precludes the gradient flow between the sparse and dense stages. In this work, we propose DashAttention (Differentiable and Adaptive Sparse Hierarchical Attention), which leverages the adaptively sparse $α$-entmax transformation to select a variable number of blocks according to the current query in the first stage. This in turn provides a prior for the second-stage softmax attention, keeping the entire hierarchy fully differentiable. Contrary to other hierarchical attention methods, we show that DashAttention is non-dispersive, translating to better long-context modeling ability. Experiments with large language models (LLMs) show that DashAttention achieves comparable accuracy as full attention with 75% sparsity and a better Pareto frontier than NSA and InfLLMv2, especially in high-sparsity regimes. We also provide an efficient, GPU-aware implementation of DashAttention in Triton, which achieves a speedup of up to over FlashAttention-3 at inference time. Overall, DashAttention offers a cost-effective strategy to model long contexts.