Before you start: words you need#
| Word | Plain meaning |
|---|---|
| Sparse attention | Each query attends to a chosen subset of past tokens instead of all of them. |
| Natively trainable | The sparsity is used during pre-training (or long continued training), so the model learns to work with it, rather than being added only at inference. |
| Block selection | Choosing whole contiguous blocks of keys (for example 64 tokens) rather than scattered single tokens, so memory reads stay coalesced (6.b). |
| Compression branch | Summarising each block of keys into one “compressed” key, giving a coarse view of the whole context cheaply. |
| Indexer | A small, cheap attention-like scorer that rates every past token for the current query, used only to decide which tokens the real attention reads. |
| Top-k | Keep the k highest-scoring items (DSA uses k = 2,048 tokens). |
| Gate | A learned weight that mixes the outputs of several branches. |
| KL divergence | A measure of how different two probability distributions are; used here to train the indexer to imitate full attention. |
1. The paper#
- NSA — Jingyang Yuan, Huazuo Gao, Damai Dai and others, Wenfeng Liang, Wangding Zeng (DeepSeek-AI, Peking University, University of Washington), “Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention”, ACL 2025, Best Paper Award (arXiv February 2025). arXiv:2502.11089
- DSA — DeepSeek-AI, “DeepSeek-V3.2: Pushing the Frontier of Open Large Language Models”, technical report, December 2025, which describes DeepSeek Sparse Attention, first shipped in DeepSeek-V3.2-Exp (September 2025). arXiv:2512.02556 · code: deepseek-ai/DeepSeek-V3.2-Exp
The last paper in this module closes the loop opened by the first Transformer paper (30.b): the O(n²) cost of attention.
2. The problem#
Reasoning models and agents produce and consume very long contexts — tens to hundreds of thousands of tokens (27.a). Even with MLA’s small cache (30.v), each decode step must read the whole KV cache, and each prefill does O(n²) work. At 128K tokens, attention dominates the cost of a step.
Sparse attention had been tried for years, with three recurring failures the NSA paper lists:
- Inference-only sparsity (prune at inference time) hurts quality because the model was trained dense, and many methods speed up only prefill or only decode.
- Hardware-unfriendly patterns: picking scattered single tokens gives poor memory access, and per-head choices break GQA/MQA sharing, so the theoretical savings never show up in wall-clock time.
- Non-trainable selection (top-k with no gradient) prevents the model from learning what to keep.
3. The idea#
NSA: three branches, all trained end to end.
- Compressed attention: summarise each block of 32 keys (stride 16) into one compressed key and value with a small learned network; attend over all compressed tokens for a coarse global view.
- Selected attention: reuse the compressed-attention scores to rate blocks of 64 tokens; pick the top 16 blocks (always including the first block and two local ones) and attend to them at full resolution. All heads in a GQA group pick the same blocks, so one KV load serves the group.
- Sliding window: attend to the last 512 tokens, so local patterns do not crowd out the other branches.
A learned gate mixes the three outputs. Triton kernels are designed around the selection pattern (load a GQA group’s queries together, stream the selected blocks contiguously).
DSA: a learned indexer in front of ordinary attention.
- A lightning indexer — a few small heads, ReLU instead of softmax, runnable in FP8 — scores every past token for each query.
- Main attention reads only the top-2,048 tokens by index score. Core attention cost drops from O(L²) to O(L·k); the indexer is still O(L²) but far cheaper per element.
- Built on MLA’s MQA mode (30.v): every query head shares the latent KV entries, so one selection serves all heads — the same kernel-efficiency point NSA made.
- Added by continued training of an existing model (DeepSeek-V3.1-Terminus, 128K context): a short dense warm-up trains only the indexer to match the dense attention distribution (KL loss), then sparse training adapts the whole model.
flowchart LR Q["Query token"] --> IX["Lightning indexer (FP8, few heads): score all past tokens"] IX --> TK["Top-2,048 tokens"] TK --> AT["MLA attention (MQA mode) over selected tokens only"] KV["Full latent KV cache (still stored)"] --> AT AT --> O["Output"] class Q,O io class IX,TK neutral class KV memory class AT compute
4. How it works#
What sparsity does and does not save. Both methods still store the whole KV cache — any token might be selected later. What shrinks is the KV read per step and the attention FLOPs. So sizing GPU memory for a DSA model is the same as for its dense parent, while decode at long context gets much cheaper per token (13.i).
Why selection must be shared across heads. A GPU kernel is fast when it loads a KV block once and uses it for many queries. If each head picked different tokens, every head would need its own loads, and the memory traffic saved by sparsity would come back. NSA shares selection across a GQA group; DSA across all heads via MLA’s MQA mode.
Training the selector.
- NSA learns block importance from the compressed branch’s attention scores, which are differentiable, and trains everything from scratch (a 27 B-parameter MoE with 3 B active, on 8k-token texts, then continued training at 32k; the paper says 260 B tokens in one place and 270 B in another).
- DSA trains the indexer separately: warm-up of 1,000 steps (16 sequences × 128K tokens, 2.1 B tokens) with the main model frozen, then about 944 B tokens of sparse training. The indexer’s input is detached, so the indexer learns only from its KL loss and the main model only from the language-model loss.
Short contexts. For short prefills DeepSeek runs a masked dense (MHA) mode that simulates DSA, because sparse selection only pays when the context is long.
5. Results#
NSA (27 B MoE, 3 B active; Triton kernels on A100):
| Claim | Result |
|---|---|
| Quality vs full attention | matched or exceeded on general benchmarks, LongBench and reasoning (math) after fine-tuning |
| Speed at 64K context vs FlashAttention-2-based full attention | 11.6× decode, 9.0× forward, 6.0× backward |
| Needle-in-a-haystack at 64K | perfect retrieval in the paper’s test |
DSA (DeepSeek-V3.2 vs DeepSeek-V3.1-Terminus):
| Claim | Result |
|---|---|
| Quality | no substantial degradation on short- or long-context benchmarks; similar or better human-preference ratings |
| Cost | per-token cost grows much more slowly with position in the sequence (Figure 3), measured on DeepSeek’s H800 service; DeepSeek cut its API prices sharply with the V3.2-Exp release |
| Training to convert | about 946 B tokens of continued pre-training, versus 14.8 T to pre-train V3 |
The DSA report gives its cost results as curves, not single numbers; this material’s 13.i quotes about 60× less KV read per step at 128K context as an order of magnitude (2,048 of 128K tokens), not a measured speed-up.
6. What held up and what did not#
Held up
- Trainable, hardware-aligned sparsity works at the frontier. DSA is in production at DeepSeek; GLM-5.x, Mistral Large 4 and the DeepSeek-V4 line use DeepSeek-style indexers that keep about 2,048 keys per query (24.b, 24.j).
- Share selections across heads. Both papers show that sparsity only saves time if the kernel can share loads; this is now a design rule for sparse-attention kernels (25.f).
- Convert by continued training. DSA showed an existing dense-attention model can be converted with a few percent of its pre-training budget, much like GQA uptraining (30.d).
Did not, or changed
- NSA’s three-branch design was not adopted as-is by DeepSeek’s own production model: DSA’s single indexer plus top-k is simpler and fits MLA. NSA’s ideas (block-aligned, group-shared selection, trained end to end) carried over.
- Memory capacity is unchanged. Long contexts still need room for the whole cache; KV offloading and compression (14.k, 24.c) are still needed. DeepSeek-V4 compresses the stored cache itself (24.b).
- Serving complexity grew. Engines needed new kernels (sparse MLA decode, FP8 indexer kernels), indexer caches, and changes to prefix caching and disaggregation for the indexer’s own keys (17.i).
- The indexer is still O(L²). At millions of tokens it becomes the next bottleneck.
7. Where it lives in 2026#
- Taught in depth: attention research in 24.b, long-context research in 24.g, MLA and DSA in 12.g, KV-cache optimisation in 13.i, long-context inference in 14.i, sparse and MLA kernels in 25.f.
- vLLM v0.32.0 and SGLang serve DeepSeek-V3.2 and its successors with DSA; v0.32.0 defaults the KV
cache for GLM-5.x on SM100 to FP8 with
--kv-cache-dtype auto.
Checked on 13 November 2026.
8. Questions#
- Why did many earlier sparse-attention methods fail to give real wall-clock speed-ups?
- Name NSA’s three branches and what each contributes.
- Why must all heads in a group select the same blocks?
- What does DSA’s lightning indexer compute, and why can it run in FP8 with ReLU?
- Does DSA reduce the memory needed to hold a 128K-token KV cache? What does it reduce?
- How was DSA added to an existing model, and why train the indexer with a KL loss first?
- The indexer is still O(L²). At what point would that matter, and what might come next?
9. Further reading#
- NSA, arXiv:2502.11089 — sections 3 and 4.
- DeepSeek-V3.2, arXiv:2512.02556 — section 2 (DSA).
- deepseek-ai/DeepSeek-V3.2-Exp on GitHub.
- 24.b and 12.g in this material.
- Back to the start: Roofline (30.a) — every paper in this module is an argument about where work sits on that graph.