Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention
Yuan et al., DeepSeek-AI · ACL 2025 · arXiv:2502.11089
Full attention makes every query read every earlier key, so the cost grows with the square of the context. NSA lets each query read three small views of the past: a summary vector per block of keys, the raw tokens of the few blocks those summaries rate highest, and the last \(w\) tokens. A learned per-head gate mixes the three. As the context grows, the share of keys each query reads gets smaller.
| branch | what it reads | why |
|---|---|---|
| compression | one summary vector per block of \(l\) keys, stride \(d\) | global context, cheap |
| selection | the \(n\) highest-scoring blocks of \(l'\) raw tokens | fine-grained precision |
| sliding window | the last \(w\) tokens | local detail, so the other two branches don't have to learn it |
Choosing blocks costs almost nothing extra. Block importance comes straight from the compression branch's own attention scores (Eq. 8–9). Summing those scores across a GQA group (Eq. 10) means every head in the group reads the same blocks, which is what the paper's kernel exploits.
Which keys does each query read?
Each row is a query \(t\) and each column is a key. Colored cells are what that query reads. Hover a row to inspect it, click to pin it. Turn on the needle: one key is planted far outside the window, and the selection branch finds its block from the compressed scores alone.
The page keeps the paper's constraints: \(d < l\), \(l\) and \(l'\) are multiples of \(d\), and \(l \le l'\) as in all its configs. An invalid choice gets corrected for you.
Cost vs context length
The share of the causal (query, key) pairs that NSA reads, counted the way test_nsa.py counts it: every branch's mask entries are summed, and compressed keys count once each. Dense attention is 1.0. The dashed curve uses the test's settings (\(l=32,\ d=16,\ l'=32,\ n=4,\ w=32\)) and reproduces its numbers: 0.97 at \(T=256\), 0.57 at 512, 0.33 at 1024 and 0.20 at 2048.
What this demo simplifies
This is the reference form of NSA, the same as the repo. It builds full \((T, S)\) masks per branch, so it is \(O(T^2)\) and shows which keys are read, not the wall-clock speedup. The paper's 9–11× gains come from a blockwise Triton kernel that loads each KV block once per GQA group. Neither the repo nor this page has that kernel.
Keys are compressed with a plain block mean, standing in for the learned MLP \(\varphi\) (the repo's BlockCompress). The queries and keys are random and untrained: one 16-dimensional head in one GQA group, so Eq. 10's sum over heads has a single term. The gate in Eq. 5 only weights the branch outputs and never changes which keys are read, so it is left out here. The needle is a planted key plus a shared direction added to every query. It stands in for what training would teach the model to look for.
A detail of Eq. 9 as the repo ports it: selection block \(j\) sums compressed blocks \(\tfrac{l'}{d}j + m + n\), and those windows reach past the end of block \(j\). So a key in the first \(l\) positions of a block also adds score to the block before it. The needle sits on the last key of its block, where only its own block gets the credit.