Learn › How Transformers Work
·★ MEMBER·PAPER·15 min read

Paper Walkthrough: SAS — Training a Sparse-Attention Block Selector End-to-End With the Language Modeling Loss

The selector that decides which context blocks a query reads has traditionally been trained by imitating the dense model's attention distribution. SAS injects the selector's scores into the softmax as log-space gates, so the gradient that Top-K used to block flows straight from next-token prediction into the selector. We follow the paper through its four-axis ablation, the gradient formulas behind each choice, and the 10.6–15.5 point GPQA-Diamond gain at a 1024-token budget.

Key points

  • SAS injects the selector's continuous scores into the attention logits during training, letting standard backpropagation update the selector.
  • Block-sparse attention uses Top-K selection, which is piecewise constant in the scores, so gradients from the loss do not reach the selector.
  • Existing methods distill the dense model's layer-wise attention distributions, which ignores complementarity across layers and the values.
  • The final form is softmax(qK^T + log g)V, and adding the log of the gate equals multiplying the post-softmax attention probability by the gate.
  • At inference, Top-K is taken and ordinary sparse attention runs without gates, computationally identical to SeerAttention-R; only the selector's training differs.
ModalitytextTaskarchitecture

SAS: Simple Attention Sparsification via End-to-End Optimization of Context Ranking

Primary source — what this article is built on

undefined2026-09-11→undefined2026-09-15same month

SAS: Simple Attention Sparsification via End-to-End Optimization of Context RankingZhiwei Li, Lei Zhu, Hao Gu et al. · 2026-09-11 · v1arXiv:2609.13141Paper page·PDF
undefined

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.


The paper's claim in one paragraph

This walkthrough covers "SAS: Simple Attention Sparsification via End-to-End Optimization of Context Ranking" (arXiv:2609.13141, published 11 September 2026) from Tencent HY LLM Frontier and the Hong Kong University of Science and Technology. The abstract argues as follows. Post-training attention sparsification cuts the quadratic attention cost of a pretrained Transformer by having a lightweight selector pick a small set of context blocks per query, but the hard Top-K step blocks gradients from the language modeling loss, so existing trainable methods fall back on distilling the dense model's layer-wise attention distributions. That ranking is not aligned with how much each block actually helps the prediction under a fixed attention budget, so the budget gets wasted. SAS injects the selector's continuous scores into the attention logits during training, letting standard backpropagation update the selector. Three choices turned out to matter: putting the gate inside the softmax in log form, normalizing the gates with a softmax so historical blocks are calibrated against the always-retained current block, and keeping the scores continuous so the model learns relative priorities. A FlashAttention-style Triton kernel makes long-sequence training feasible, and across reasoning, long-context and agentic tasks SAS consistently beats trainable sparse-attention baselines, with the largest gains at tight budgets.

Below we go through why the gradient stops, how a log-space gate lets it through, and how each of the four design decisions is backed by the experiments.

An analogy: how do you train the person who decides who gets heard?

In Attention from scratch we described self-attention as note-taking in a meeting: compare your question (Query) against everyone's name tag (Key), and listen longer to the people whose tags match, mixing their statements (Values) accordingly.

When the meeting runs long and there are tens of thousands of participants, comparing every tag every time is out of the question. So you appoint a gatekeeper who says "for this question, only listen to those few rows of seats." That is the selector in block-sparse attention: it scores groups of seats, called blocks, and only the top K blocks get attended.

The question is how to train this gatekeeper. The standard approach has been to have them imitate where the teacher (the original dense attention) was looking: record the teacher's gaze at every layer and train the gatekeeper's scores to match. That is attention distillation.

The paper's point is that this training signal is misaligned. Where the teacher looked is not the same as which blocks are actually needed to get the final answer right when you can only afford K of them. Ideally, if the gatekeeper picks badly, the answer comes out wrong and the gatekeeper is held directly responsible. That is exactly what SAS does: it folds the gatekeeper's scores into the attention computation so the final next-token loss can push gradients all the way back into the selector.

Background: block-sparse attention, and why Top-K stops the gradient

First the notation from §3. With q\mathbf{q} the query of the current token and K,V\mathbf{K},\mathbf{V} the keys and values of the nn previous tokens, plain attention is (scale factor omitted, as in the paper):

o=softmax⁡(qK⊤)V\mathbf{o}=\operatorname{softmax}(\mathbf{q}\mathbf{K}^{\top})\mathbf{V}
(1)

In words, this is the plain attention recipe: take the dot product of the question with every name tag, turn those scores into proportions, and mix everyone's statements in those proportions.

In words: take the dot product of the question with every name tag, turn the scores into proportions, and mix everyone's statements in those proportions. Since generation does this for every token, the cumulative cost over a sequence of length nn grows as n2n^2.

Block-sparse attention splits the nn context tokens into C=n/bC=n/b blocks of bb tokens each, has a selector Rθ\mathcal{R}_\theta produce a score per block s∈RC\mathbf{s}\in\mathbb{R}^C, and attends only to the union S\mathcal{S} of the top KK blocks.

I=Top-K(s,K),S=⋃m∈IBm,o=softmax⁡(qKS⊤)VS\mathcal{I}=\text{Top-}K(\mathbf{s},K),\qquad \mathcal{S}=\bigcup_{m\in\mathcal{I}}B_m,\qquad \mathbf{o}=\operatorname{softmax}\left(\mathbf{q}\mathbf{K}_{\mathcal{S}}^{\top}\right)\mathbf{V}_{\mathcal{S}}
(2)

In words, pick the K highest-scoring blocks, gather their tokens into one set, and run attention over only those tokens.

That is, pick the K highest-scoring blocks and run attention over their tokens only. Per query the cost drops from nn to ∣S∣|\mathcal{S}|, and over the whole sequence it becomes proportional to n∣S∣n|\mathcal{S}| (§3).

FIG 1Cumulative cost of dense attention grows as n² with context length n. Sparse attention with a fixed budget |S| grows as n×|S|, a straight line. Switch to the log axis to see how many orders of magnitude separate them

Here is the crux: Top-K is piecewise constant in the scores. Nudge a score slightly and, unless the membership of the top K changes, the output does not move at all. The derivative is zero, so the loss carries no information about how the selector should change its scores. The paper draws this as Figure 1(a), "Gradient Blockage in Discrete Selection". Distillation and heuristic proxy signals exist precisely to route around this wall (§1, §2).

What exactly is wrong with distillation

Section 1 names two limitations of distilling attention distributions.

First, it ignores complementarity across layers. A block missed at one layer could be picked up at another, but when each layer is trained to mimic its own teacher, every layer independently chases "the same place the teacher looked."

Second, it ignores the values. Even if the attention weights match, how the mixed V\mathbf{V} contents affect the final prediction is not part of the distillation objective. In the paper's Figure 1, the V\mathbf{V} matrix is simply absent from the training signal.

SAS resolves both with the most naive possible move: train the selector on the language modeling loss itself. Cross-layer division of labor and the contents of the values are both baked into the final loss. All that remains is to open a path for the gradient.

The mechanism: scores become log-space gates inside the softmax

Section 4.1 first reframes sparsification as context ranking rather than selection. Top-K is determined by the ordering of scores, so what the selector really needs to learn is a continuous ordering of "how important is each block." During training that ordering participates in attention as a continuous quantity; only at inference is it cut down to K.

Concretely, let B0B_0 be the always-retained current block and H={B1,…,BC}\mathcal{H}=\{B_1,\ldots,B_C\} the historical blocks the selector ranks. The historical scores s\mathbf{s} pass through a differentiable activation ϕ\phi to become positive gates.

g=ϕ(s)∈R+C,g0=1\mathbf{g}=\phi(\mathbf{s})\in\mathbb{R}_+^{C},\qquad g_0=1
(3)

In words, each historical block gets a positive weight derived from its score, while the current block's weight is pinned to 1 so it carries no bias.

Which says: historical blocks get a positive weight derived from their score, while the current block's weight is pinned to 1 so it carries no bias. Each gate gmg_m is broadcast to every token in its block.

Then the gate's logarithm is added to the attention logits. The final form in §4.3 (equation 9) is:

oSAS=softmax⁡(qKS⊤+log⁡gS)VS\mathbf{o}_{SAS}=\operatorname{softmax}\left(\mathbf{q}\mathbf{K}_{\mathcal{S}}^{\top}+\log\mathbf{g}_{\mathcal{S}}\right)\mathbf{V}_{\mathcal{S}}
(4)

In words, add the log of the gatekeeper's weight to the dot-product scores before the softmax. Because addition inside an exponential is multiplication outside it, this is the same as multiplying each post-softmax attention probability by its block's gate, so the loss gradient can reach the selector.

gS\mathbf{g}_{\mathcal{S}} is the selected blocks' gates expanded to token level, and S=B0∪⋃m∈IBm\mathcal{S}=B_0\cup\bigcup_{m\in\mathcal{I}}B_m is the current block plus the selected historical blocks. Read it as: add the log of the gatekeeper's weight to the dot-product score table, then softmax. Addition inside an exponential is multiplication outside it, exp⁡(zi+log⁡gm)=gmexp⁡(zi)\exp(z_i+\log g_m)=g_m\exp(z_i), so this is the same as multiplying the post-softmax attention probability by the gate. The scores s\mathbf{s} now enter the attention probabilities continuously, and gradients from the loss reach θ\theta through g\mathbf{g}.

At inference (Algorithm 2), Top-K is taken on s\mathbf{s} and ordinary sparse attention runs without any gates. In other words, inference is computationally identical to SeerAttention-R; only the way the selector was trained differs (§5.1).

FIG 2Adding or subtracting a constant on the logit side moves the peaks of the softmax distribution. SAS's log gate raises or lowers a block's bar on the logit side, and that directly becomes its share of attention. Only the current block gets a zero offset and serves as the reference point

"Add the scores to the logits" is something anyone could propose. The value of §4.2 is a controlled experiment along four axes showing where the naive design breaks. The setup: Qwen3-4B, the AttnGate selector borrowed from SeerAttention-R, block size 64, Top-K 32, 93.7K examples from OpenR1-Math-220k, sequence length 3…

What's behind this

§

Members-only from here

449 walkthroughs, 42 textbook chapters, 48 student units and 6 close readings — all included for $4.99/mo, with about three new explainers a day on average. Cancel any time; access runs to the end of the period.

Already a member? Sign in to keep reading

References

  1. Zhiwei Li, Lei Zhu, Hao Gu, Xiang Hu et al.. (2026-09-11) SAS: Simple Attention Sparsification via End-to-End Optimization of Context Ranking. arXiv:2609.13141Paper page·PDF

This article is written from the source paper above. Where they differ, the original is authoritative.

Published by TechAthletes · Editorial policy · How we cite · Corrections

Comments

Sign in to comment