SAS:通过上下文排序的端到端优化实现简单注意力稀疏化

·

研究论文 · 2026/09/14

SAS 是一种通过端到端优化上下文排序的简单注意力稀疏化方法。现有可训练稀疏注意力方法通常使用轻量选择器对上下文单元评分,然后进行硬 Top-K 选择,这阻断了语言建模损失的梯度,导致选择器仅蒸馏逐层密集注意力分布,其排序与固定注意力预算下对预测的影响不直接对齐,可能浪费预算。SAS 提出门控稀疏注意力机制,在训练时将选择器的连续分数注入注意力 logits,使损失能通过标准反向传播更新选择器。关键设计包括:将门放在注意力 softmax 内以对数形式、使用归一化 softmax 门来校准历史上下文与始终保留的当前块、保留连续选择器分数以学习相对优先级而非仅硬选择。为支持长序列训练,实现了内存高效的 Triton 内核,将 SAS 集成到 FlashAttention 风格计算中。在推理、长上下文理解和智能体任务上,SAS 在不同注意力预算下均优于可训练稀疏注意力基线,尤其在紧张预算下增益显著,表明其能更有效地为下游任务进行上下文排序。

SAS 通过端到端优化上下文排序,解决了稀疏注意力中排序与预测影响不对齐的关键问题,并在多种任务和预算下显著优于基线,尤其适合资源受限的长序列场景。

Post-training attention sparsification reduces the quadratic cumulative attention cost of pretrained Transformers by selecting a small set of context units (tokens or blocks) for each query. Existing trainable methods usually use a lightweight selector to score context units, followed by hard Top-K selection that blocks gradients from the language modeling loss. Consequently, these methods commonly distill layer-wise dense attention distributions. Although this encourages the selector to rank context units by dense attention weights in the original model, the ranking is not directly aligned with their impact on predictions under a fixed attention budget (i.e., the number of attended context units per query), potentially wasting the limited budget on less useful units. To address this misalignment, we propose Simple Attention Sparsification (SAS), a gated sparse attention mechanism that optimizes context ranking end-to-end with the language modeling loss. The key idea is to inject the selector's continuous scores into attention logits during training, allowing the loss to update the selector through standard backpropagation. We identify several choices crucial for this simple design to work well in practice: placing the gate inside the attention softmax in log form, using normalized softmax gates to calibrate historical context against the always-retained current block, and preserving continuous selector scores so the model learns relative priorities rather than only hard selections. To support long-sequence training, we implement a memory-efficient Triton kernel that integrates SAS into FlashAttention-style computation. Across reasoning, long-context understanding, and agentic tasks, SAS consistently outperforms trainable sparse attention baselines across attention budgets, with especially large gains under tight budgets, demonstrating more effective context ranking for downstream tasks.

阅读原文

参与讨论