Local, sparse and linear attention; state-space models
Windows, sparsity, recurrence and hybrids
Chapter 13 of 21 · III. Fast attention and inference · about 45 minutes. Builds on: 6. Attention, 10. Generating text: the KV cache and inference cost.
Beyond full attention
Section titled “Beyond full attention”Full attention costs in time and its cache grows with . Several families of ideas relax that, covered in the six sections below.
1. Local (sliding-window) attention
Section titled “1. Local (sliding-window) attention”Let each token attend only to the previous tokens. Cost becomes , and the cache for such a layer never needs to exceed entries. Information still travels far because stacking layers extends the reach to about tokens. Longformer combined local windows with a few global tokens; Mistral 7B used a 4,096-token window with a rolling buffer cache. gpt-oss alternates banded layers (128-token window) with fully dense layers : the local layers are cheap, and every second layer can still move information across the whole context. Try a window in this toy:
2. Attention sinks
Section titled “2. Attention sinks”Softmax forces each row of weights to sum to 1, even when a token has nothing relevant to attend to. Models learn to dump that surplus on a few tokens, typically the first ones. showed that evicting these initial tokens from a sliding cache wrecks generation, while keeping just a handful of them plus the recent window lets a model stream indefinitely without retraining. The gpt-oss model card describes a related design in which each head has a learned bias added to the softmax denominator, which gives the head a place to put unused weight .
3. Sparse attention: attend to the right few tokens
Section titled “3. Sparse attention: attend to the right few tokens”Instead of a fixed window, choose which tokens each query attends to.
- Native Sparse Attention (NSA) organizes keys and values into blocks and combines three branches per query, mixed by learned gates: attention over compressed coarse-grained block summaries, attention over selected fine-grained blocks (chosen using the compressed scores), and a local sliding window. It is designed around contiguous block reads for GPU efficiency and is trained with sparsity from the start so the selection is learned, rather than being added after the fact.
- DeepSeek Sparse Attention (DSA), introduced in DeepSeek-V3.2 : a lightweight “lightning indexer” scores every earlier token for each query using a small number of ReLU-activated dot-product heads, then full attention is computed over only the top-k selected tokens. Core attention drops from to . The report states that, relative to the prior V3.1-Terminus model, DSA is the only architectural change and was added through continued training.
4. Linear attention and recurrent state
Section titled “4. Linear attention and recurrent state”Standard attention computes which cannot be factored. Replace it with a product of feature maps, score(, ) = . Now regroup the sum for the output at position :
The matrix (value-dim × key-dim) can be updated incrementally: Sₜ = Sₜ₋₁ + vₜ kₜᵀ (writing for ). So the layer is a recurrent network with a fixed-size matrix state: constant memory, time . The same computation can be written in the parallel form (good for training) or the recurrent form (good for generation).
The price is capacity: everything the layer remembers must fit in . Adding new associations on top of old ones causes interference, so plain linear attention cannot recall exactly. Two repairs are used:
- Forgetting gate: with (the form used in Mamba-2) lets the state decay old information.
- Delta rule: treat as a linear regressor from keys to values and do one gradient step on the error . The gradient is , giving
Sₜ = Sₜ₋₁ (I − βₜ kₜkₜᵀ) + βₜ vₜ kₜᵀ. It first erases what the state currently returns for key , then writes the new value, so an updated association replaces the old one instead of adding to it. - Gated delta rule combines both:
Sₜ = Sₜ₋₁ · αₜ(I − βₜ kₜkₜᵀ) + βₜ vₜ kₜᵀ, with the gate for fast bulk forgetting and the delta term for targeted overwriting. Kimi Linear’s “Kimi Delta Attention” extends this with finer-grained gating .
5. State-space models
Section titled “5. State-space models”A different lineage arrives at a similar place. A state-space model evolves a hidden state with a linear recurrence and reads it out: = Ā·̄·, (the barred matrices come from discretizing a continuous system). Early versions had fixed A, , , so they treated all inputs the same. Mamba makes the step size and the and matrices functions of the current input (“selective”), which lets the model choose what to remember or ignore, and it computes the recurrence with a hardware-aware parallel scan. Mamba-2 shows that these SSMs and linear attention are two views of one family of structured computations.
6. Hybrids
Section titled “6. Hybrids”Pure recurrent layers are cheap but weak at exact lookup far back; full attention is exact but expensive. Recent designs mix them. Kimi Linear interleaves its delta-attention layers with latent-attention (MLA) layers and reports up to a 75% smaller KV cache and up to 6× faster decoding at a 1M-token context compared with full attention . These are the authors’ reported figures; check the paper for the exact setup.
Summary of trade-offs
Section titled “Summary of trade-offs”| Approach | Time | Memory during generation | Weakness |
|---|---|---|---|
| Full attention | grows with | cost | |
| Sliding window | bounded by | no direct long-range access in that layer | |
| Learned sparse (NSA, DSA) | full cache kept, reads limited | selector must be good | |
| Linear / SSM | constant | lossy memory, weaker exact recall |
Check yourself
Section titled “Check yourself”Why can a sliding-window model still use information from beyond one window?
Each layer lets a token see tokens, but those tokens’ vectors already contain information from their own windows in earlier layers, so after layers the effective reach is about .
In the delta rule with β = 1 and ‖k‖ = 1, what does S·k return after the update?
. The state returns exactly the newly written value.
Why is training a model with sparse attention from the start different from sparsifying a finished model?
When sparsity is present during training, the model (and the selector) learn to put important information where it will be found; imposing it afterwards removes information the model was relying on.
Papers
Section titled “Papers”- and Windowed attention.
- Attention sinks.
- Native Sparse Attention.
- DeepSeek Sparse Attention (architecture section).
- Linear attention as an RNN.
- Mamba.
- The duality between SSMs and attention.
- Gated Delta Networks; section 3 has the update rule.
- A recent hybrid at scale.