Simple Algorithm
Deep Learning · Emergence · 28 min

Fast Weights & Test-Time Memory

From fast-weight programmers and the delta rule to layers whose memory learns while reading.

A recurrent network's state need not be a vector. Make it a matrix, write to it with outer products and read from it with matrix–vector products, and the network is programming its own fast-changing weights. That idea, from the fast-weight programmers of the early 1990s, returns in linear attention, in the delta rule of DeltaNet, and in layers whose state is a small model trained by gradient descent on every token they read.

What you’ll learn

  • Describe memory-augmented networks and fast weights, and show that causal linear attention is a fast-weight programmer whose state update St=St−1+vtϕ(kt)⊤S_t=S_{t-1}+\mathbf v_t\phi(\mathbf k_t)^\top is a Hebbian write.
  • Derive the delta rule St=St−1+βt(vt−St−1kt)kt⊤S_t=S_{t-1}+\beta_t(\mathbf v_t-S_{t-1}\mathbf k_t)\mathbf k_t^\top as one gradient step on 12∥Skt−vt∥2\tfrac12\lVert S\mathbf k_t-\mathbf v_t\rVert^2, and analyze its transition matrix I−βtktkt⊤I-\beta_t\mathbf k_t\mathbf k_t^\top.
  • Explain gated delta rules, and how DeltaNet is trained in parallel over the sequence despite its matrix-valued transitions.
  • Describe test-time training layers and Titans as memories updated by online optimization, and the view of sequence layers as test-time regression.

Memory beyond a vector

In the mid-2010s several architectures separated computation from storage. The Neural Turing Machine (Graves, Wayne & Danihelka, 2014) couples a recurrent controller to a memory matrix that it reads and writes through differentiable heads, addressed by content and by location. Memory networks (Weston, Chopra & Bordes, 2015) store facts in memory slots and retrieve the relevant ones, at first with hard selection and supervision of which slots matter, then with soft attention trained end to end (Sukhbaatar et al., 2015), and the differentiable neural computer (Graves et al., 2016) adds learned allocation and links that record the order of writes. These models learned small algorithms, such as copying and sorting, and question answering over stories, but they were often delicate to train and never became the standard building block for large models.

A simpler idea is older. In a fast-weight programmer (Schmidhuber, 1992), a slow network, trained by gradient descent, emits at every step a change to the weights of a second, fast network; the fast weights hold short-term memory while the slow weights hold skills. Ba, Hinton, Mnih, Leibo & Ionescu (2016) revived it as a decaying Hebbian matrix, A(t)=λA(t−1)+η h(t)h(t)⊤A(t)=\lambda A(t-1)+\eta\,\mathbf h(t)\mathbf h(t)^\top, used to "attend to the recent past". In its plainest form a fast-weight memory is a matrix written with outer products of keys and values:

St=St−1+vtkt⊤,yt=Stqt=∑j≤tvj (kj⊤qt)S_t=S_{t-1}+\mathbf v_t\mathbf k_t^\top,\qquad\mathbf y_t=S_t\mathbf q_t=\sum_{j\le t}\mathbf v_j\,(\mathbf k_j^\top\mathbf q_t)
Writing adds the outer product of a value and its key, a Hebbian update ("units that fire together wire together"). Reading with a query returns every stored value weighted by how well its key matches the query: unnormalized attention with a linear similarity. With orthonormal keys retrieval is exact; with more pairs than key dimensions the keys cannot all be orthogonal and retrievals interfere. The Transformers lesson Attention as Associative Memory derives that crosstalk and the resulting capacity.
Think first A recurrent network with a dd-dimensional state vector stores dd numbers. How many does a d×dd\times d fast-weight matrix store, and what does that buy?

d2d^2 numbers, so up to about dd key–value pairs of dimension dd can be stored and retrieved with little interference, against about one dd-dimensional item for a vector state. Reading costs a matrix–vector product, O(d2)\order{d^2} per step, the same order as a vanilla RNN's step. That ratio of memory to compute is why matrix-valued states reappear in modern recurrent models, from xLSTM's mLSTM to DeltaNet.

Linear attention is a fast-weight programmer

Causal softmax attention computes yt=∑j≤tsim(qt,kj) vj/∑j≤tsim(qt,kj)\mathbf y_t=\sum_{j\le t}\mathrm{sim}(\mathbf q_t,\mathbf k_j)\,\mathbf v_j\big/\sum_{j\le t}\mathrm{sim}(\mathbf q_t,\mathbf k_j) with sim=exp⁡(q⊤k/dk)\mathrm{sim}=\exp(\mathbf q^\top\mathbf k/\sqrt{d_k}), and must keep every key and value. Linear attention (Katharopoulos, Vyas, Pappas & Fleuret, 2020) replaces the similarity by an inner product of feature maps, sim(q,k)=ϕ(q)⊤ϕ(k)\mathrm{sim}(\mathbf q,\mathbf k)=\phi(\mathbf q)^\top\phi(\mathbf k), and the sums collapse into a recurrence.

DerivationFrom attention to a recurrent state
  1. yt=∑j≤tvj ϕ(kj)⊤ϕ(qt)∑j≤tϕ(kj)⊤ϕ(qt)=St ϕ(qt)zt⊤ϕ(qt)\mathbf y_t=\frac{\sum_{j\le t}\mathbf v_j\,\phi(\mathbf k_j)^\top\phi(\mathbf q_t)}{\sum_{j\le t}\phi(\mathbf k_j)^\top\phi(\mathbf q_t)}=\frac{S_t\,\phi(\mathbf q_t)}{\mathbf z_t^\top\phi(\mathbf q_t)}

    Pull the query's features out of the sums; what remains depends only on past keys and values:

  2. St=St−1+vt ϕ(kt)⊤,zt=zt−1+ϕ(kt)S_t=S_{t-1}+\mathbf v_t\,\phi(\mathbf k_t)^\top,\qquad\mathbf z_t=\mathbf z_{t-1}+\phi(\mathbf k_t)

    The matrix StS_t and the normalizer zt\mathbf z_t are running sums, so they obey a recurrence with a fixed-size state:

  3. Generation now costs O(dvdϕ)\order{d_vd_\phi} per token and memory that does not grow with the context, while training can still run in parallel. Schlag, Irie & Schmidhuber (2021) pointed out that this is exactly a fast-weight programmer: the slow projections WQW_Q, WKW_K, WVW_V program the fast weight matrix StS_t with outer products, and reading it with ϕ(qt)\phi(\mathbf q_t) is a forward pass of the fast network.

The purely additive write has two weaknesses. It never forgets: every pair ever written stays in StS_t. And it cannot revise: writing a new value for a key already in memory adds to the old value instead of replacing it. Multiplying St−1S_{t-1} by a data-dependent decay αt∈(0,1)\alpha_t\in(0,1) at every step, as Mamba-2 does in this form, fixes the first weakness by forgetting everything a little, but not the second. Schlag and colleagues traced the limited capacity of linearized attention to this and proposed a different write.

Check your understanding

A linear-attention memory (no decay) first writes the pair (k,v1)(\mathbf k,\mathbf v_1) and later (k,v2)(\mathbf k,\mathbf v_2) with the same unit-norm key k\mathbf k. What does reading with q=k\mathbf q=\mathbf k return (ignoring the normalizer)?

  1. v2\mathbf v_2, the most recent value
  2. v1\mathbf v_1, the first value
  3. v1+v2\mathbf v_1+\mathbf v_2: additive writes accumulate instead of replacing
  4. The zero vector, since the two writes cancel
Show the answer

C. v1+v2\mathbf v_1+\mathbf v_2: additive writes accumulate instead of replacing

S=v1k⊤+v2k⊤S=\mathbf v_1\mathbf k^\top+\mathbf v_2\mathbf k^\top, so Sk=(v1+v2)∥k∥2=v1+v2S\mathbf k=(\mathbf v_1+\mathbf v_2)\lVert\mathbf k\rVert^2=\mathbf v_1+\mathbf v_2. A memory that should hold the current value of a variable needs a write that replaces what is stored at a key: the delta rule.

The delta rule

Treat the memory as a linear model that should map each key to its value, and let every token take one step of online least squares. This is the delta rule of Widrow & Hoff (1960), the classic least-mean-squares update, applied to a fast weight matrix.

DerivationThe delta rule as online regression
  1. Lt(S)=12∥Skt−vt∥2,∇SLt=(Skt−vt) kt⊤\loss_t(S)=\tfrac12\lVert S\mathbf k_t-\mathbf v_t\rVert^2,\qquad\nabla_S\loss_t=(S\mathbf k_t-\mathbf v_t)\,\mathbf k_t^\top

    The memory's error on the current pair is a squared loss in SS, with gradient an outer product:

  2. St=St−1−βt(St−1kt−vt)kt⊤=St−1(I−βtktkt⊤)+βt vtkt⊤S_t=S_{t-1}-\beta_t(S_{t-1}\mathbf k_t-\mathbf v_t)\mathbf k_t^\top=S_{t-1}\big(I-\beta_t\mathbf k_t\mathbf k_t^\top\big)+\beta_t\,\mathbf v_t\mathbf k_t^\top

    One gradient step of size βt\beta_t from St−1S_{t-1} gives the delta rule: write the error vt−St−1kt\mathbf v_t-S_{t-1}\mathbf k_t, not the value itself:

  3. ∥kt∥=1, βt=1⟹Stkt=vt,Stu=St−1u  for u⊥kt\lVert\mathbf k_t\rVert=1,\ \beta_t=1\quad\Longrightarrow\quad S_t\mathbf k_t=\mathbf v_t,\qquad S_t\mathbf u=S_{t-1}\mathbf u\ \text{ for }\mathbf u\perp\mathbf k_t

    With a unit-norm key and βt=1\beta_t=1 the write is exact: Stkt=St−1kt−(St−1kt−vt)=vtS_t\mathbf k_t=S_{t-1}\mathbf k_t-(S_{t-1}\mathbf k_t-\mathbf v_t)=\mathbf v_t. Whatever was stored at kt\mathbf k_t is replaced, and directions orthogonal to kt\mathbf k_t are untouched.

  4. eig⁡(I−βtktkt⊤)={1−βt∥kt∥2, 1, …, 1}\operatorname{eig}\big(I-\beta_t\mathbf k_t\mathbf k_t^\top\big)=\big\{1-\beta_t\lVert\mathbf k_t\rVert^2,\ 1,\ \dots,\ 1\big\}

    The state transition I−βtktkt⊤I-\beta_t\mathbf k_t\mathbf k_t^\top is a generalized Householder matrix: eigenvalue 1−βt∥kt∥21-\beta_t\lVert\mathbf k_t\rVert^2 along the key and 1 on its orthogonal complement. The memory forgets only in the direction it is about to write, and for 0<βt∥kt∥2<20<\beta_t\lVert\mathbf k_t\rVert^2<2 no direction grows.

Predict before you look

Starting from S=0S=0, the same pair (k,v)(\mathbf k,\mathbf v) with ∥k∥=1\lVert\mathbf k\rVert=1 is written three times. Linear attention then returns Sk=3vS\mathbf k=3\mathbf v. What multiple of v\mathbf v does the delta rule with β=0.5\beta=0.5 return?

Show the answer

0.875

Each write closes half of the remaining gap: SkS\mathbf k goes 0→0.5v→0.75v→0.875v0\to0.5\mathbf v\to0.75\mathbf v\to0.875\mathbf v. In general tt writes give (1−(1−β)t)v(1-(1-\beta)^t)\mathbf v, converging to v\mathbf v rather than growing like tvt\mathbf v. The delta rule is a regression that converges; the Hebbian write is a sum that grows.

Training in parallel. Because the transition I−βtktkt⊤I-\beta_t\mathbf k_t\mathbf k_t^\top is a full matrix rather than a diagonal one, the delta rule is not an elementwise scan. Yang, Wang, Zhang, Shen & Kim (2024) showed that products of such Householder-like matrices have a compact WY representation, which lets the recurrence be computed chunk by chunk with dense matrix multiplications, and trained DeltaNet (with L2-normalized keys and βt=σ(⋅)∈(0,1)\beta_t=\sigma(\cdot)\in(0,1)) at 1.3 billion parameters on 100 billion tokens, where it outperformed Mamba and gated linear attention in perplexity and zero-shot evaluations. Gated DeltaNet (Yang, Kautz & Hatamizadeh, 2025) adds a data-dependent decay, which in the online-regression view is an adaptive weight decay: "gating enables rapid memory erasure while the delta rule facilitates targeted updates".

ModelState updateAs online learning
Linear attentionSt=St−1+vtkt⊤S_t=S_{t-1}+\mathbf v_t\mathbf k_t^\topstep on a linear score ⟨Skt,vt⟩\langle S\mathbf k_t,\mathbf v_t\rangle; never forgets
Mamba-2 (scalar decay)St=αtSt−1+vtkt⊤S_t=\alpha_tS_{t-1}+\mathbf v_t\mathbf k_t^\topthe same, plus uniform forgetting
DeltaNetSt=St−1(I−βtktkt⊤)+βtvtkt⊤S_t=S_{t-1}(I-\beta_t\mathbf k_t\mathbf k_t^\top)+\beta_t\mathbf v_t\mathbf k_t^\topgradient step on 12∥Skt−vt∥2\tfrac12\lVert S\mathbf k_t-\mathbf v_t\rVert^2
Gated DeltaNetSt=αtSt−1(I−βtktkt⊤)+βtvtkt⊤S_t=\alpha_tS_{t-1}(I-\beta_t\mathbf k_t\mathbf k_t^\top)+\beta_t\mathbf v_t\mathbf k_t^\topdecay, then the same gradient step
After Table 1 of Yang, Kautz & Hatamizadeh (2025), simplified. Each row is one step of an online optimizer on a memory objective; the differences are the objective and the regularization.

Memories that learn at test time

If one step of least squares on a linear memory works, why not a more expressive memory and a better optimizer? Test-time training (TTT) layers (Sun et al., 2025) make the hidden state the weights WW of a small model ff, and update them with one gradient step of a self-supervised loss per token, using learned projections θK,θV,θQ\theta_K,\theta_V,\theta_Q that are trained, like everything else, in the outer loop:

ℓ(W;xt)=∥f(θKxt;W)−θVxt∥2,Wt=Wt−1−η ∇Wℓ(Wt−1;xt),zt=f(θQxt;Wt)\ell(W;\mathbf x_t)=\big\lVert f(\theta_K\mathbf x_t;W)-\theta_V\mathbf x_t\big\rVert^2,\qquad W_t=W_{t-1}-\eta\,\nabla_W\ell(W_{t-1};\mathbf x_t),\qquad\mathbf z_t=f(\theta_Q\mathbf x_t;W_t)
TTT-Linear's inner model is linear and TTT-MLP's a two-layer MLP, each wrapped in a LayerNorm and a residual connection for stability. For a plain linear f(k;W)=Wkf(\mathbf k;W)=W\mathbf k the gradient is 2(Wkt−vt)kt⊤2(W\mathbf k_t-\mathbf v_t)\mathbf k_t^\top, so the update is exactly the delta rule with β=2η\beta=2\eta, keys θKxt\theta_K\mathbf x_t and values θVxt\theta_V\mathbf x_t. Sun et al. also prove that a linear ff trained by batch gradient descent with η=12\eta=\tfrac12 from W0=0W_0=0 reproduces linear attention.

For speed, TTT takes its gradients in mini-batches of 16 tokens, all evaluated at the weights left by the previous mini-batch, which turns most of the work into parallel matrix products. Titans (Behrouz, Zhong & Mirrokni, 2025) make the memory a deeper MLP M\mathcal M and add two optimizer features. Momentum accumulates "surprise", the gradient of the memory's loss ∥M(kt)−vt∥2\lVert\mathcal M(\mathbf k_t)-\mathbf v_t\rVert^2, and a forgetting gate decays the memory, with data-dependent rates ηt\eta_t, θt\theta_t, αt\alpha_t:

St=ηt St−1−θt ∇ℓ(Mt−1;xt),Mt=(1−αt) Mt−1+StS_t=\eta_t\,S_{t-1}-\theta_t\,\nabla\ell(\mathcal M_{t-1};\mathbf x_t),\qquad\mathcal M_t=(1-\alpha_t)\,\mathcal M_{t-1}+S_t
Titans combine this long-term memory with attention over a short window, using the memory's output as extra context, as a gate, or as a layer in front of attention.

A fixed-size memory, however cleverly trained, has a fixed capacity: a d×dd\times d matrix can hold only about dd clean associations, and a small MLP only so much more. Softmax attention escapes by keeping every key and value, at a cost that grows with the context. Hybrid Models & the Limits of Fixed-Size Memory compares the two on recall and copying, and shows why many recent architectures combine them.

Key takeaways

  • Fast-weight programmers keep a matrix-valued state written with outer products and read with matrix–vector products (Schmidhuber, 1992); causal linear attention is one, with the Hebbian update St=St−1+vtϕ(kt)⊤S_t=S_{t-1}+\mathbf v_t\phi(\mathbf k_t)^\top (Schlag, Irie & Schmidhuber, 2021).
  • The additive write never forgets and cannot revise a key's value. The delta rule St=St−1(I−βtktkt⊤)+βtvtkt⊤S_t=S_{t-1}(I-\beta_t\mathbf k_t\mathbf k_t^\top)+\beta_t\mathbf v_t\mathbf k_t^\top is one gradient step on 12∥Skt−vt∥2\tfrac12\lVert S\mathbf k_t-\mathbf v_t\rVert^2: it overwrites exactly at β=1\beta=1 and forgets only along the key.
  • DeltaNet trains in parallel through the WY representation of products of generalized Householder matrices; Gated DeltaNet adds a data-dependent decay, an adaptive weight decay in the online-regression view.
  • TTT layers and Titans make the memory a small model trained by gradient steps (with mini-batches, momentum and forgetting) at test time; a linear TTT layer with online steps is the delta rule. All are instances of test-time regression, and all have a fixed capacity that attention's growing cache does not.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

In what sense is causal linear attention a fast-weight programmer?

  1. It updates its slow weights WQW_Q, WKW_K, WVW_V by gradient descent at every token
  2. Its state is a matrix StS_t that the slow projections write with outer products vtϕ(kt)⊤\mathbf v_t\phi(\mathbf k_t)^\top and that is read as a linear layer applied to ϕ(qt)\phi(\mathbf q_t)
  3. It stores every past key and value, like a KV cache
  4. It uses a softmax to choose one memory slot per step
Show the answer

B. Its state is a matrix StS_t that the slow projections write with outer products vtϕ(kt)⊤\mathbf v_t\phi(\mathbf k_t)^\top and that is read as a linear layer applied to ϕ(qt)\phi(\mathbf q_t)

The sums in linear attention collapse into St=St−1+vtϕ(kt)⊤S_t=S_{t-1}+\mathbf v_t\phi(\mathbf k_t)^\top (plus a normalizer). StS_t acts as the weight matrix of a fast network that the slow, trained projections reprogram at every step (Schlag, Irie & Schmidhuber, 2021).

Question 2

The delta rule St=St−1+βt(vt−St−1kt)kt⊤S_t=S_{t-1}+\beta_t(\mathbf v_t-S_{t-1}\mathbf k_t)\mathbf k_t^\top is one gradient step of size βt\beta_t on which objective?

  1. −⟨Skt,vt⟩-\langle S\mathbf k_t,\mathbf v_t\rangle, the linear score that linear attention follows
  2. ∥S∥F2\lVert S\rVert_F^2, a weight-decay penalty
  3. ∥kt−S⊤vt∥2\lVert\mathbf k_t-S^\top\mathbf v_t\rVert^2, reconstructing keys from values
  4. 12∥Skt−vt∥2\tfrac12\lVert S\mathbf k_t-\mathbf v_t\rVert^2, the squared error of predicting the value from the key
Show the answer

D. 12∥Skt−vt∥2\tfrac12\lVert S\mathbf k_t-\mathbf v_t\rVert^2, the squared error of predicting the value from the key

Its gradient is (Skt−vt)kt⊤(S\mathbf k_t-\mathbf v_t)\mathbf k_t^\top, so the step writes the prediction error rather than the value. That is why repeated writes of the same pair converge to v\mathbf v instead of growing.

Question 3

For a unit-norm key k\mathbf k and 0<β<10<\beta<1, what does the transition I−βkk⊤I-\beta\mathbf k\mathbf k^\top do to the memory SS when it multiplies it from the right?

  1. It shrinks only the component of SS that acts on k\mathbf k, by the factor 1−β1-\beta, and leaves every direction orthogonal to k\mathbf k unchanged
  2. It shrinks every direction by 1−β1-\beta
  3. It rotates the memory by an angle β\beta
  4. It doubles the component along k\mathbf k
Show the answer

A. It shrinks only the component of SS that acts on k\mathbf k, by the factor 1−β1-\beta, and leaves every direction orthogonal to k\mathbf k unchanged

I−βkk⊤I-\beta\mathbf k\mathbf k^\top has eigenvalue 1−β1-\beta along k\mathbf k and 1 elsewhere. The delta rule therefore forgets precisely what is stored under the key it is about to write, a targeted erasure; a scalar decay αt\alpha_t would erase everything uniformly.

Question 4

A TTT layer uses a linear inner model f(k;W)=Wkf(\mathbf k;W)=W\mathbf k, the loss ∥WθKxt−θVxt∥2\lVert W\theta_K\mathbf x_t-\theta_V\mathbf x_t\rVert^2 and one gradient step with rate η\eta per token. What is its update?

  1. Linear attention's Hebbian write, Wt=Wt−1+θVxt(θKxt)⊤W_t=W_{t-1}+\theta_V\mathbf x_t(\theta_K\mathbf x_t)^\top
  2. An exact least-squares fit to all past tokens at every step
  3. The delta rule with β=2η\beta=2\eta, keys θKxt\theta_K\mathbf x_t and values θVxt\theta_V\mathbf x_t
  4. A softmax attention over all past tokens
Show the answer

C. The delta rule with β=2η\beta=2\eta, keys θKxt\theta_K\mathbf x_t and values θVxt\theta_V\mathbf x_t

∇W∥Wk−v∥2=2(Wk−v)k⊤\nabla_W\lVert W\mathbf k-\mathbf v\rVert^2=2(W\mathbf k-\mathbf v)\mathbf k^\top, so Wt=Wt−1−2η(Wt−1kt−vt)kt⊤W_t=W_{t-1}-2\eta(W_{t-1}\mathbf k_t-\mathbf v_t)\mathbf k_t^\top. TTT layers generalize this with richer models and mini-batched steps; with batch gradient descent from zero, Sun et al. show the linear case reproduces linear attention instead.

Further reading

  • J. Schmidhuber, "Learning to control fast-weight memories: An alternative to dynamic recurrent networks," Neural Computation 4(1), 131–139 (1992).
  • I. Schlag, K. Irie & J. Schmidhuber, "Linear transformers are secretly fast weight programmers," ICML (2021).
  • S. Yang, B. Wang, Y. Zhang, Y. Shen & Y. Kim, "Parallelizing linear transformers with the delta rule over sequence length," NeurIPS (2024).
  • S. Yang, J. Kautz & A. Hatamizadeh, "Gated delta networks: Improving Mamba2 with delta rule," ICLR (2025).
  • Y. Sun, X. Li, K. Dalal, J. Xu, A. Vikram, G. Zhang, Y. Dubois, X. Chen, X. Wang, S. Koyejo, T. Hashimoto & C. Guestrin, "Learning to (learn at test time): RNNs with expressive hidden states," ICML (2025).
  • A. Behrouz, P. Zhong & V. Mirrokni, "Titans: Learning to memorize at test time," NeurIPS (2025).