Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention
TL;DR. Full attention at 64K tokens is slow, and most sparse attention methods are applied only at inference and save less than they promise. NSA builds sparsity into pretraining. Each query attends to three things: compressed summaries of all blocks, the top- blocks chosen using those summaries, and a local sliding window. A learned gate mixes the three, and kernels are designed around GQA. It matches or beats full attention and is much faster at long context.
Key idea
Section titled “Key idea”For query , NSA replaces the full key-value set with three smaller ones and mixes their outputs:
- Compression: an MLP maps each block of keys (stride ) to one compressed key. This gives a coarse view of the whole context.
- Selection: block importance comes from the compression attention scores, summed over the heads in a GQA group. The top- blocks of size are attended at full resolution.
- Window: the last tokens, kept separate so that local patterns do not shortcut the other branches.
The three attention branches and their gates are trained from scratch, but top- block selection is discrete. The compression-attention branch supplies learned importance scores, and gradients flow through the attention computations for the selected blocks, not through the selected indices. The kernel loads all query heads of a GQA group together and fetches each selected KV block into SRAM once.
Results. The backbone is a 27B-parameter MoE with 3B active parameters, GQA and DeepSeekMoE. It is pretrained on 270B tokens of 8K text (the introduction says 260B) and then extended to 32K with YaRN. Settings are , , , and . NSA beats full attention on 7 of 9 general metrics. It retrieves perfectly in needle-in-a-haystack tests at 64K, and its LongBench average is 0.469, against 0.437 for full attention and 0.423 for Exact-Top. After SFT on 10B tokens of R1-distilled reasoning traces, it scores 0.121 against 0.046 on AIME at an 8K generation limit, and 0.146 against 0.092 at 16K. Kernels written in Triton on 8×A100 reach the speedups above. Decoding speedup tracks KV tokens loaded: 2,048, 2,560, 3,584 and 5,632 tokens at 8K, 16K, 32K and 64K (Table 4).
Connections
Section titled “Connections”- Same goal, different route: MLA shrinks each token’s KV entry, while NSA reads fewer tokens. Recurrent layers such as Mamba and Gated DeltaNet drop the cache entirely.
- Built on GQA and DeepSeekMoE. Kernels follow FlashAttention-2.
- Related in production: Qwen3.8-Flash-Next interleaves its own sparse-attention layers (QSA) with GDN layers.
- IR angle: compressed-block scoring followed by fine selection is coarse-to-fine retrieval, like centroid pruning then exact scoring in PLAID.
Questions to think through
These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.
- Derive / compute
During decoding, NSA loads about ⌊(s − l)/d⌋ + n·l′ + w tokens of KV per attention operation. With l = 32, d = 16, n = 16, l′ = 64 and w = 512, compute this at s = 8,192 and s = 65,536 and compare with Table 4 (2,048 and 5,632) and the expected speedups (4× and 11.6×). Why is the speedup roughly equal to the ratio of tokens loaded?
What a strong answer covers
- s = 8,192: ⌊8,160/16⌋ = 510 compressed tokens, plus 16 × 64 = 1,024 selected tokens, plus a 512 window, giving 2,046 ≈ 2,048. The ratio 8,192/2,048 = 4×.
- s = 65,536: ⌊65,504/16⌋ = 4,094, plus 1,024, plus 512, giving 5,630 ≈ 5,632. The ratio 65,536/5,632 ≈ 11.6×.
- Only the compressed branch grows with s, at rate 1/d. The selected and window parts are constant, so the advantage grows with context length.
- Decoding has low arithmetic intensity and is limited by memory bandwidth, so time scales with bytes of KV read. The speedup therefore tracks the ratio of tokens loaded.
- Mechanism
Explain how NSA picks which blocks to attend to without a separate scoring pass, and why it forces all query heads in a GQA group to share the same selected blocks. Contrast this with per-head selection methods such as Quest.
What a strong answer covers
- The compression branch already computes attention scores over compressed blocks. NSA reuses those softmax scores to derive importance for the selection blocks, which costs almost nothing extra.
- Scores are summed across heads in a GQA group, and the top-n blocks are chosen for the whole group.
- In GQA, heads share one KV cache. If each head picks different blocks, memory traffic is the union of their selections, so compute falls but memory access does not.
- Shared selection lets the kernel load all of a group's query heads together and fetch each selected KV block once into SRAM (group-centric loading).
- A sliding-window branch handles local context separately, so the other branches cannot be dominated by easy local patterns. A learned sigmoid gate mixes the three branches.
- Critique
NSA reports that it beats full attention on most general benchmarks, on LongBench and on AIME after distillation. Critique how strong this evidence is for adopting trainable sparse attention in a retrieval-augmented long-context model.
What a strong answer covers
- A single run at one scale (27B MoE, 3B active), with no seeds or error bars. Several general-benchmark gains are small, such as +0.034 on GSM8K.
- Long-context evidence is LongBench (0.469 against 0.437 for full attention) and needle tests at 64K. That is limited for multi-hop retrieval over many passages.
- The AIME comparison uses SFT on 10B tokens distilled from DeepSeek-R1, and absolute scores are low (0.146 against 0.092 at a 16K limit).
- Kernel speedups are measured in Triton on A100 against a Triton FlashAttention-2, not against the fastest production kernels.
- For RAG, a key test is whether block selection keeps rare but critical evidence tokens. This could be probed with position-controlled retrieval tasks.