Core

Linear attention and fast weights

Rewriting attention as an RNN with a matrix-valued state makes per-token cost constant, and the delta rule turns that state into an error-correcting memory.

Updated

01 · Concept

Concept

Attention pays for its memory in the most literal way possible: it keeps everything. To produce token tt, a causal attention layer compares the new query against the key of every earlier position and mixes their values:

ot=i=1texp(qtki)j=1texp(qtkj)vi.o_t=\sum_{i=1}^{t}\frac{\exp(q_t^{\top}k_i)}{\sum_{j=1}^{t}\exp(q_t^{\top}k_j)}\,v_i.

Count what this step actually costs. The naive plan — the one every attention decoder follows — stores all tt past key and value vectors and performs tt similarity computations for the new token. Token one costs one comparison, token one thousand costs a thousand, token nn costs nn: per-token work is O(n)O(n) in context length, and the stored keys and values grow without bound. That store is the KV cache, and lesson 7.2 derives its price for our course model: in Qwen3.8-27B at its native 262,144-token context, the cached keys and values of the attention layers reach 16 GiB for a single sequence. The layer retains every past key-value record and gives the new query a direct weighted path to each one. That avoids fixed-state compression, but softmax still forms a weighted mixture and does not guarantee exact retrieval of an arbitrary stored token.

Lesson 3.2 showed the opposite design: an RNN carries one fixed-size hidden state, pays the same cost at every step, and stores nothing per token. The question this lesson answers is whether attention’s addressing behavior can live inside a fixed-size state.

Katharopoulos et al. (2020) noticed that the obstacle is the softmax. Replace the exponential similarity with a kernel feature map ϕ\phi, so the weight on position ii is proportional to ϕ(qt)ϕ(ki)\phi(q_t)^{\top}\phi(k_i), and the sum becomes associative:

ot=ϕ(qt)itϕ(ki)viϕ(qt)itϕ(ki).o_t=\frac{\phi(q_t)^{\top}\sum_{i\le t}\phi(k_i)v_i^{\top}}{\phi(q_t)^{\top}\sum_{i\le t}\phi(k_i)}.

Both sums no longer depend on the query, so they can be maintained incrementally. Define a matrix-valued state and a normalizer:

St=St1+ϕ(kt)vt,zt=zt1+ϕ(kt),S_t=S_{t-1}+\phi(k_t)v_t^{\top},\qquad z_t=z_{t-1}+\phi(k_t),

and the output is one read, ot=Stϕ(qt)ztϕ(qt)o_t=\dfrac{S_t^{\top}\phi(q_t)}{z_t^{\top}\phi(q_t)}. Look at what happened: this is a recurrence. Same update rule at every position, fixed-size state, constant work per token — the RNN blueprint from lesson 3.2, except the hidden state is now a d×dd\times d matrix of key-to-value associations instead of a vector. Per-token cost drops from O(n)O(n) to O(1)O(1), and memory from unbounded to constant.

Here is the classic wrong turn. The additive update only ever adds. Write value vav_a under some key, later write a different vbv_b under a nearly identical key, and retrieval returns a blend of both; the stale association is never removed, and as thousands of writes superpose, interference grows. Early linear attention traded away softmax attention’s precision and underperformed it on tasks needing exact recall — which is why it spent years as the “losing” idea. The tempting conclusion is that this is fundamental: a fixed-size state cannot forget selectively, so lossy blur is the unavoidable price of O(1)O(1). That conclusion is wrong, and the fix was sitting in decades-old associative-memory theory: change the write, not the size.

The delta rule makes the write error-correcting. Before writing, retrieve what the state currently predicts for the incoming key — read it the same way any query is read, St1ϕ(kt)S_{t-1}^{\top}\phi(k_t) — and write only the difference:

St=St1+βtϕ(kt)(vtSt1ϕ(kt)),S_t=S_{t-1}+\beta_t\,\phi(k_t)\bigl(v_t-S_{t-1}^{\top}\phi(k_t)\bigr)^{\top},

with βt(0,1)\beta_t\in(0,1) a learned write strength. Walk through the two cases. If the state already stores vtv_t for this key, the error is near zero and the state is left alone. If it stores something stale, the update uses that error to change the state along the incoming key direction — an error-correcting write, not an exact overwrite, whose effect depends on βt\beta_t, the key features, and interference from other associations. Each step is one step of online gradient descent on the memory’s own retrieval error: the state learns, while the model runs. DeltaNet is linear attention with this write; adding the learned forgetting gates of lesson 3.3 — a decay on St1S_{t-1} so the state can also fade what no longer matters — yields Gated DeltaNet (Yang et al., 2024), which lesson 4.15 dissects in full.

And that difference is precisely how the losing idea of 2020 became production architecture in 2026. Qwen3.8-27B, this course’s specimen, builds 48 of its 64 layers as Gated DeltaNet and keeps full softmax attention in only 16, one in every four (Qwen3.8-27B Model Card, 2026). On the current Transformers reference float32 path, each DeltaNet layer.s recurrent state is about 3 MiB; across all 48 layers that is roughly 144 MiB — constant no matter whether the sequence holds one hundred tokens or 262,144 — against the 16 GiB the attention layers’ KV cache reaches at full context. The hybrid is an engineering confession that both function classes earn their place: fixed-size error-correcting memory for cheap length, a few per-position key-value access layers for lookups that fixed-state compression cannot represent reliably. The RNN did not lose to attention after all; it went away, learned to write like a memory, and came back as most of the model.

02 · Analogy

Analogy

One office answers correspondence two ways. The archivist keeps every letter ever received and rereads the whole pile before drafting each reply: perfectly faithful, increasingly slow, and the shelves fill without bound. The clerk keeps a single fixed-size ledger of running totals: each letter updates the totals and is discarded, so every reply costs the same. The naive clerk only ever adds, so contradictory entries pile up under the same name. The careful clerk first checks what the ledger already says for that name and writes only a correction aimed at the stale entry. Linear attention is the naive clerk; the delta rule is the careful one.

03 · Teach it back

Teach it back

Explain how linear attention turns attention into an RNN with a matrix-valued state, why the purely additive update causes interference, and how the delta rule fixes it.

Minimum: 80 characters and 15 words. Your writing stays only in this browser.

Waiting for your explanation.

Compare with a model answer

Replacing the softmax similarity with a feature map makes the decode sum associative, so all past key-value contributions collapse into one running matrix S_t=S_{t−1}+φ(k_t)v_tᵀ that the query reads with one multiplication. That is an RNN whose hidden state is a fixed-size matrix, so per-token cost and memory are constant in sequence length. But the additive write superposes every association: writing two different values under similar keys blends them at retrieval, and nothing is ever removed. The delta rule first retrieves what the state currently predicts for the incoming key, S_{t−1}ᵀφ(k_t), and writes only the scaled error β_t φ(k_t)(v_t−S_{t−1}ᵀφ(k_t))ᵀ, so the write is error-correcting rather than blindly accumulative — a fixed-size state that forgets selectively.

04 · Check your understanding

Check your understanding

01How does the linear-attention state relate to the RNN hidden state from lesson 3.2?
Answer and explanation

Both are fixed-size states updated by one shared rule at every step, but here the state is a matrix of associations rather than a vector — Linear attention is literally a recurrence: same update applied at every position, constant-size state — the RNN design with a matrix-valued hidden state.

02What does the delta rule change about the memory write?
Answer and explanation

It writes the error between the incoming value and what the state already retrieves for that key, rather than another raw value — Retrieve first, then write only the correction: if the state already stores the right value, the update is near zero; otherwise, the correction is proportional to the retrieval error.

03Why does per-token decode cost stay constant in linear attention but grow in softmax attention?
Answer and explanation

The query reads one fixed-size state matrix instead of comparing against every stored past key — Associativity folds the whole past into S_t once; softmax attention must touch all t cached keys and values at every step.

Complete the teach-back and answer the quiz correctly to finish this lesson.

◎ · Evidence marker

Sources

  1. Angelos Katharopoulos et al. (2020). Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention.
  2. Songlin Yang et al. (2024). Gated Delta Networks: Improving Mamba2 with Delta Rule.
  3. Qwen Team (2026). Qwen3.8-27B Model Card.