Paper recreation · interactive demo

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.

branchwhat it readswhy
compressionone summary vector per block of \(l\) keys, stride \(d\)global context, cheap
selectionthe \(n\) highest-scoring blocks of \(l'\) raw tokensfine-grained precision
sliding windowthe last \(w\) tokenslocal detail, so the other two branches don't have to learn it
\[ \begin{aligned} o_t &= \sum_{c \in \{\mathrm{cmp},\,\mathrm{slc},\,\mathrm{win}\}} g_t^{c}\,\mathrm{Attn}\big(q_t,\ \tilde K_t^{c},\ \tilde V_t^{c}\big) && \text{(Eq. 5)}\\ \tilde K_t^{\mathrm{cmp}} &= \Big\{\, \varphi\big(k_{id+1\,:\,id+l}\big) \;\Big|\; 0 \le i \le \big\lfloor \tfrac{t-l}{d} \big\rfloor \Big\} && \text{(Eq. 7)}\\ p_t^{\mathrm{cmp}} &= \mathrm{softmax}\big(q_t^{\top} \tilde K_t^{\mathrm{cmp}} / \sqrt{d_k}\big) && \text{(Eq. 8)}\\ p_t^{\mathrm{slc}}[j] &= \sum_{m=0}^{l'/d-1} \sum_{n=0}^{l/d-1} p_t^{\mathrm{cmp}}\Big[\tfrac{l'}{d}\,j + m + n\Big] && \text{(Eq. 9)}\\ p_t^{\mathrm{slc}'} &= \sum_{h=1}^{H} p_t^{\mathrm{slc},(h)} \quad \text{(heads of one GQA group)} && \text{(Eq. 10)}\\ \mathcal I_t &= \operatorname{top-}n\big(p_t^{\mathrm{slc}'}\big) \;\cup\; \{\text{initial block},\ 2\ \text{local blocks}\} && \text{(Eq. 11)} \end{aligned} \]

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.

window selected block window + selected seen only as a compressed summary not read ▼ needle key
Inspected query: bars are the per-block selection scores \(p_t^{\mathrm{slc}}\) from Eq. 9. Orange bars were chosen, outlined bars were forced (initial and 2 local blocks). The strip below the bars is the same row of the grid.

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.

your settings (dot = current T) test_nsa.py settings dense attention

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.