Beyond attention

Linear attention, state space models such as Mamba, and delta-rule layers replace the growing KV cache with a fixed-size state that is updated token by token. They are fast and cheap on long texts but can’t recall everything exactly. The 2025–26 answer is the hybrid: mostly linear layers, with a full attention layer every few.

Key idea
Drop the softmax and attention becomes a running sum of key × value outer products: a fixed-size state. Add gates (Mamba, Mamba-2) to forget, a delta rule (DeltaNet) to overwrite, and keep a few full attention layers for exact recall
Real world
Taking notes in a notebook with a fixed number of pages instead of keeping a recording of the whole lecture. Cheap to carry and quick to consult, but if the lecture is long you have to summarise, and some details are lost.
In an LLM
Qwen3.5-397B-A17B (2026) has 60 layers: 45 are Gated DeltaNet layers that each keep a fixed state per head, and only 15 are full attention layers with a KV cache. It reads 262,144 tokens natively.

The cost that won’t go away

Attention compares each new token with every earlier one, and keeps every earlier key and value in the KV cache. Token number 100,000 costs a hundred thousand comparisons, and the cache keeps growing. The tricks on the previous page make this cheaper but do not change its shape.

Recurrent networks (RNNs), which came before transformers, had the opposite profile: they read text one token at a time into a fixed-size memory, so every token costs the same. Their problem was training: they could not be parallelised across a sequence, and they forgot. The ideas on this page aim for both: train in parallel like a transformer, generate with a fixed memory like an RNN.

Linear attention: a running sum

Write one attention output without the softmax, just a weighted sum of values with weights q⋅kjq \cdot k_j:

ot=∑j≤t(qt⋅kj) vj=qt⊤(∑j≤tkjvj⊤)=qt⊤Sto_t = \sum_{j \le t} (q_t \cdot k_j)\, v_j = q_t^\top \Big( \sum_{j \le t} k_j v_j^\top \Big) = q_t^\top S_t

The bracket does not depend on the query. Call it the state StS_t: a d×dd \times d matrix, the sum of the outer products kjvj⊤k_j v_j^\top of every key with its value. Each new token just adds its own outer product, St=St−1+ktvt⊤S_t = S_{t-1} + k_t v_t^\top, and reads with qt⊤Stq_t^\top S_t. That is (Katharopoulos et al., 2020). Cost per token: constant. Memory: one d×dd \times d matrix per head, however long the text.

The softmax is what made attention sharp: it can put 99% of its weight on one key. Without it, every stored pair leaks a little into every read. The state is like a single sheet on which every fact is written on top of the others.

empty
Ann→RomeBob→OsloCat→LimaDan→PuneEve→LimaAnn→Pune (moved)
old state4 × 4
RomeOsloLimaPunek10.000.000.000.00k20.000.000.000.00k30.000.000.000.00k40.000.000.000.00
+
key4 × 1
k10.0k20.0k30.0k40.0
×
value1 × 4
RomeOsloLimaPune0.000.000.000.00
=
new state S4 × 4
RomeOsloLimaPunek10.000.000.000.00k20.000.000.000.00k30.000.000.000.00k40.000.000.000.00
ask the state: multiply a name’s key into S
Rome0.00
Oslo0.00
Lima0.00
Pune0.00
green = the right answer · this state answers: never written
state size16 numbers
a KV cache would hold0 numbers
after 100,000 tokens16 vs 800,000
The state starts empty. Each fact will add a small matrix to it. Press play.
Facts arrive one token at a time: a name (the key) and a city (the value). Each is written into a 4 × 4 state by adding the key times the value, an outer product. Play it, then ask the state about any name.

Three ways to update the state, all in use:

  • Add (linear attention): St=St−1+ktvt⊤S_t = S_{t-1} + k_t v_t^\top. Never forgets, never corrects.
  • Decay (gated linear attention, Mamba-2): St=αtSt−1+ktvt⊤S_t = \alpha_t S_{t-1} + k_t v_t^\top, with a forget factor αt\alpha_t between 0 and 1 computed from the current token. Old facts fade so new ones fit.
  • Delta rule (DeltaNet, Schlag et al., 2021; Yang et al., 2024): first read what the state already says for this key, St−1⊤ktS_{t-1}^\top k_t, and write only the difference. If a fact changes, the old value is replaced instead of piled on.

Gated DeltaNet (Yang, Kautz and Hatamizadeh, 2024) combines the last two: decay to forget, delta rule to overwrite. Kimi Delta Attention (KDA, in Kimi Linear and Kimi K3) refines the gate so each channel of the state can forget at its own speed.

example · a notebook with fixed pages

A student takes notes in a notebook with one page per subject. Each lecture adds lines to the right pages (add). Old notes in pencil fade over the term (decay). When the teacher corrects an earlier fact, the student rubs out the old line and writes the new one (delta rule). The notebook never gets thicker. But if the course has more facts than the pages can hold, notes start overlapping, and some answers come out muddled.

example · a long chat with a linear-attention model

In a 200,000-token conversation, a pure transformer keeps 200,000 keys and values per layer and checks all of them for every new token. A Gated DeltaNet layer keeps one 128 × 128 state per head and updates it once per token, so the 200,000th token costs the same as the first. The catch: if you ask for the exact phone number mentioned 150,000 tokens ago, the state may have blurred it, while an attention layer can still look it up.

State space models and Mamba

State space models (), such as S4 (Gu et al., 2021), came from signal processing. They run a linear recurrence

ht=a ht−1+b xt,yt=c⊤hth_t = a \, h_{t-1} + b \, x_t, \qquad y_t = c^\top h_t

with learned aa, bb, cc. Because aa and bb are the same for every token, the whole sequence can be computed in parallel, but the model can’t decide to remember an important token or skip a filler word.

Mamba (Gu and Dao, 2023) made the recurrence selective: ata_t, btb_t and ctc_t are computed from the current token. A full stop can wipe the state; a filler word can pass through without writing. A hardware-aware “parallel scan” keeps training fast. Mamba-2 (Dao and Gu, 2024) simplified the gate to one number per head per token and showed the result is exactly a masked attention with decay weights, so it can be computed either as a recurrence (for generation) or as matrix multiplications (for training).

gates per token: a = keep the old state, b = write this token
Thea 0.97
b 0.15
cata 0.80
b 1.00
sata 0.80
b 1.00
.a 0.10
b 0.30
Thena 0.97
b 0.15
aa 0.97
b 0.15
doga 0.80
b 1.00
barkeda 0.80
b 1.00
.a 0.10
b 0.30
Thecatsat.Thenadogbarked.The0.15cat0.121.00sat0.100.801.00.0.010.080.100.30Then0.010.080.100.290.15a0.010.080.090.280.150.15dog0.010.060.080.230.120.121.00barked0.010.050.060.180.090.100.801.00.0.000.000.010.020.010.010.080.100.30
After “dog” (row 7), the state holds most of “dog” (1.00). The full stop at position 4 had a keep-gate of 0.1, so every word before it is down to 0.075 or less: the model chose to forget the first sentence. Filler words barely write (b = 0.15) and barely erase (a = 0.97), so they pass through without disturbing the content words. Notice the matrix is lower-triangular, like a causal attention matrix: Mamba-2 is exactly this kind of masked “attention” with decay weights, computed as a recurrence.
A recurrence h_t = a_t · h_(t−1) + b_t · x_t reads one token at a time. The matrix shows how much of each earlier token (column) is still in the state after each token (row). Click a row.

What they trade away

A fixed state means fixed capacity. A d×dd \times d state can keep about dd unrelated facts cleanly; beyond that, they interfere. Softmax attention has no such limit, because it keeps everything, and that is exactly what it pays for in memory.

This shows up most in recall tasks: “what was the code mentioned earlier?”, copying a long passage, answering from a document. Studies found pure recurrent models fall behind transformers of the same size here (Zoology, Arora et al., 2023; Jelassi et al., 2024), even when they match them on ordinary next-token loss.

softmax attention (KV cache)linear attention (16 × 16 state)
attention, N = 16100%
fixed state, N = 1686%
memory: cache vs state512 vs 256
With 16 pairs, the fixed state gets 86% right: 16 random keys in 16 dimensions overlap with each other, so every read picks up echoes of the other pairs. Attention still gets 100%, because it keeps every key and value and can look one up exactly, at the cost of memory that grows with N. Doubling d helps the state, but its size grows with d².
Store N random key → value pairs, then ask for every key. Attention keeps every pair in its cache; linear attention squeezes them into one 16 × 16 state. Score: how often the right value comes back. Averaged over 6 random trials.

Hybrids: the 2025–26 answer

If most layers can be cheap and a few keep exact lookup, you get most of both. Recent models do exactly that:

  • Jamba (AI21, 2024, paper): one attention layer for every seven Mamba layers, with MoE; 52B total, 12B active, 256K context.
  • Qwen3-Next-80B-A3B (2025) and Qwen3.5-397B-A17B (2026): three Gated DeltaNet layers for every gated attention layer, 262,144-token native context.
  • Kimi K3 (2026): 69 KDA layers and 24 MLA layers (3 to 1, plus one final MLA layer), 2.8T parameters, 1M-token context. Its attention layers use no position encoding at all: the recurrent KDA layers already know the order.
  • Nemotron-H (NVIDIA, 2025, paper): mostly Mamba-2 layers with a few attention layers.

Not everyone went this way. MiniMax’s M1 (2025) used a hybrid of linear “lightning” attention and softmax attention, but its M2 (late 2025) went back to full attention in every layer, and DeepSeek-V4 kept softmax attention in every layer, compressing and sparsifying it instead (see efficient attention).

Llama 3 8B100%
Jamba13%
Qwen3.5-397B-A17B25%
Kimi K326%
share of layers whose cache grows with the context (relative KV-cache size, same layer width)
At 1,048,576 tokens, only the purple layers hold a cache of 1,048,576 entries; the others hold the same small state they held at token 1. A hybrid with 1 attention layer in 4 needs roughly a quarter of the KV cache of an all-attention model of the same shape, and spends far less compute on long inputs, while those few attention layers keep exact lookup available.
Each bar is one model, each slice one layer. Purple: full attention, whose KV cache grows with the text. Green: linear attention or Mamba, with a fixed-size state.
going deeper

Linear attention with a feature map

The general form replaces exp⁡(q⋅k)\exp(q \cdot k) by ϕ(q)⊤ϕ(k)\phi(q)^\top \phi(k) for a feature map ϕ\phi (Katharopoulos et al. used elu⁡(x)+1\operatorname{elu}(x)+1), and divides by a running normaliser:

ot=ϕ(qt)⊤Stϕ(qt)⊤zt,St=St−1+ϕ(kt)vt⊤,zt=zt−1+ϕ(kt)o_t = \frac{\phi(q_t)^\top S_t}{\phi(q_t)^\top z_t}, \qquad S_t = S_{t-1} + \phi(k_t) v_t^\top, \qquad z_t = z_{t-1} + \phi(k_t)

Modern variants usually drop the normaliser and normalise qq and kk (Kimi K3’s KDA uses L2-normalised queries and keys) and the output instead.

The update rules side by side

With state S∈Rdk×dvS \in \mathbb{R}^{d_k \times d_v} and read-out ot=St⊤qto_t = S_t^\top q_t:

LayerUpdate
Linear attentionSt=St−1+ktvt⊤S_t = S_{t-1} + k_t v_t^\top
Mamba-2 / gatedSt=αtSt−1+ktvt⊤S_t = \alpha_t S_{t-1} + k_t v_t^\top, scalar αt\alpha_t per head
DeltaNetSt=(I−βtktkt⊤)St−1+βtktvt⊤S_t = (I - \beta_t k_t k_t^\top) S_{t-1} + \beta_t k_t v_t^\top
Gated DeltaNetSt=αt(I−βtktkt⊤)St−1+βtktvt⊤S_t = \alpha_t (I - \beta_t k_t k_t^\top) S_{t-1} + \beta_t k_t v_t^\top
KDA (Kimi)St=(I−βtktkt⊤)Diag⁡(αt)St−1+βtktvt⊤S_t = (I - \beta_t k_t k_t^\top) \operatorname{Diag}(\alpha_t) S_{t-1} + \beta_t k_t v_t^\top, a gate per key channel

The delta rule with βt=1\beta_t = 1 and a unit-length key is exactly “read the old value, write the difference”: St=St−1+kt(vt−St−1⊤kt)⊤S_t = S_{t-1} + k_t (v_t - S_{t-1}^\top k_t)^\top. Training uses chunked parallel forms of all of these.

The toys

The state widget uses 4-number one-hot keys for four names and an overlapping key for Eve, decay 0.7, β=1\beta = 1. The recall test uses random unit keys and values in dd dimensions, linear attention without a feature map, and softmax attention with scores scaled by 10; a read counts as correct when the output is closest to the right stored value.

In papers

Related

  • efficient-attention - Attention’s two costs are the KV cache, which grows with every token, and the score matrix, which grows with the square of the length. Modern models share keys and values between heads (GQA, MLA), let most layers look only at recent tokens, compute attention in tiles that never store the full matrix (FlashAttention), and pick a few keys to read per query (sparse attention).
  • kv-cache - When a model writes text, each new token needs the keys and values of every token before it. Those never change, so they are computed once and stored. This KV cache turns generation from work that grows with the square of the length into work that grows linearly, at the price of a lot of memory.
  • rope - Almost every modern LLM marks word order by rotating queries and keys. Each pair of numbers is turned by an angle that grows with the token’s position, so the attention score between two tokens depends only on how far apart they are. Stretching those angles is how models are extended to longer texts.
  • modern-recipe - A 2026 open model is still GPT-2’s skeleton: embed, a stack of attention and feed-forward blocks on a residual stream, then predict the next token. Almost every part inside has been swapped: RMSNorm, SwiGLU, RoPE, shared or compressed keys and values, mixtures of experts, no biases, QK-norm, bigger vocabularies and contexts a thousand times longer. Here is each change, model by model.