↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Layer 1 of 12 Formula demo on GPT-2's recorded Q, K, V · blocks of 4 · top 3 blocks · window 8 · gates ⅓
Loading the NSA demo…

What native sparse attention is

Dense attention lets every query read every earlier key, so a sequence of tokens costs scores, and at decoding time each new token reads the whole key-value cache. Native Sparse Attention (NSA), from DeepSeek (Yuan et al., 2025), replaces that single softmax with three cheaper branches that each read a small, structured set of keys: compressed blocks, a few selected blocks, and a sliding window. Native means the model is trained with the sparsity from the start, with kernels laid out for GPU memory, rather than pruning the attention of a model trained dense.

This tab is a formula demo. GPT-2 small was trained with dense attention and has no NSA weights, so the page takes its recorded queries, keys and values at layers 1, 3, 6, 9 and 12 and runs them through NSA's formulas, with two named stand-ins and sizes scaled down to a 32-token prompt. It shows how the three branches divide one query's attention, not what a trained NSA model would attend to. The GPT-2 dense view keeps the model's own attention beside it for comparison.

Three branches and a gate

For query , each branch builds its own keys and values from the past, attends over them with an ordinary softmax, and a gate weights the three results:

NSA learns the gates per token, with a small MLP and a sigmoid on the token's input. The demo fixes each at ⅓. Because every branch is a weighted sum of GPT-2's values, the gated sum is itself one row of weights over the tokens, and the All three view draws that row for every query: the NSA mixing matrix. The Branches for panel lays the selected query's three rows side by side over the same keys, then their gated sum, then GPT-2's dense row.

Compressed blocks

The compressed branch gives each query a coarse view of the whole past. The keys are cut into blocks of length , moved along by a stride , and a learned function turns each block into one key (and likewise one value):

In the paper is an MLP with a position encoding inside the block, and . The demo uses non-overlapping blocks of 4 and takes the mean of each block's keys and values, which needs no training. A block becomes visible only once its last token has arrived, so the Compressed view has one column per block, masked as “block not complete yet” until then. A query attends to about compressed keys instead of tokens.

Selecting blocks

Block means are coarse, so NSA also reads a few blocks at full resolution, and it picks them with scores it already has: the compressed attention ranks the blocks and the query keeps the top . The selected branch then attends over every token of those blocks:

The paper selects 16 blocks of 64 tokens, summing the compression scores that overlap each larger selection block. Here selection blocks equal compression blocks and a query keeps 3: its own block, always, plus the two earlier blocks with the highest . The Selected view masks the rest as “block not selected”, and the caption under the branch panel says how much of GPT-2's dense attention, for this query and head, falls inside the kept blocks: a direct measure of how well a choice made from block means finds the tokens the model actually reads.

Selection is also where NSA is aligned with the hardware. Contiguous blocks load efficiently, and in grouped-query attention all heads of a group share one key-value head, so NSA sums the block scores over the group's heads and has them all read the same blocks (the GQA tab shows the grouping). GPT-2 gives every head its own keys, so in this demo each head selects for itself.

Sliding window and cost

The third branch reads only the last tokens, the query included: . Nearby tokens usually take much of the attention, and giving them a branch of their own leaves the compressed and selected branches free for longer-range patterns. The paper's window is 512 tokens; the Window view uses 8. A window on its own is the subject of the Window tab.

Added up, a query reads about compressed keys, selected blocks and window tokens. With the paper's sizes that is one compressed key per 16 tokens of context, 16 × 64 = 1,024 selected tokens and 512 window tokens, nearly flat in apart from the compressed part. The paper reports that a model pretrained with NSA matches or exceeds full attention on general, long-context and reasoning benchmarks while running substantially faster on 64k-token sequences, in decoding and in the forward and backward passes.

Where it came from

Fixed sparse patterns came first: Sparse Transformers (Child et al., 2019) attended along strided and local patterns, and Longformer (2020) combined a sliding window with a few global tokens (see the Sparse tab). Quest (2024) chose key-value pages per query at inference time, from per-page bounds on the keys, without training. NSA, posted in February 2025 within days of Moonshot's similar Mixture of Block Attention (MoBA), trains block selection end to end; it received a best paper award at ACL 2025. DeepSeek's next step, DeepSeek Sparse Attention, selects single tokens with a small learned indexer instead of blocks: see the DSA tab.

Papers

  1. Native Sparse Attention: Hardware-Aligned and Natively Trainable Sparse Attention Jingyang Yuan et al. · ACL 2025 NSA: compressed, selected and sliding-window branches with learned gates, trained end to end with hardware-aligned kernels.
  2. Language Models are Unsupervised Multitask Learners Alec Radford et al. · 2019 GPT-2, whose recorded queries, keys and values this demo runs through NSA's formulas.
  3. Generating Long Sequences with Sparse Transformers Rewon Child, Scott Gray, Alec Radford, Ilya Sutskever · arXiv 2019 Fixed strided and local attention patterns, the first widely used sparse attention.
  4. Longformer: The Long-Document Transformer Iz Beltagy, Matthew E. Peters, Arman Cohan · arXiv 2020 Sliding-window attention plus a few global tokens, the fixed-pattern design NSA's window branch resembles.
  5. Quest: Query-Aware Sparsity for Efficient Long-Context LLM Inference Jiaming Tang et al. · ICML 2024 Query-aware selection of key-value pages at inference time, without training: block selection before NSA.
  6. MoBA: Mixture of Block Attention for Long-Context LLMs Enzhe Lu et al. · arXiv 2025 Concurrent trainable block attention that routes each query to its top blocks by their mean keys.
  7. GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints Joshua Ainslie et al. · EMNLP 2023 Grouped-query attention, whose shared key-value heads let NSA select one set of blocks per group.

See also