Tools › Delta rule

Linear attention and the delta rule

Standard attention keeps every past token in the KV cache. Linear-attention models squeeze the past into one fixed-size matrix instead. How you write into that matrix decides whether it works as a memory.

What to look for

  • Orthogonal keys: both rules recall perfectly up to 8 pairs. Plain linear attention is a fine memory when keys do not overlap.
  • Random or similar keys: linear attention adds every write on top of the others, so old memories pick up noise (interference). The delta rule first reads what the memory currently answers for the key and corrects only the difference.
  • Re-assign key #1: linear attention now returns a blend of the old and the new value; the delta rule returns the new one.
  • Passes: repeat the sequence and linear attention adds every pair again, so its outputs just grow and the error climbs. The delta rule keeps correcting and slowly converges; the more the keys overlap, the slower it goes.
  • Lower β: the delta rule corrects only part of the error per write, so it forgets slower but also learns slower. Lower α makes both rules forget old content on purpose.
  • More than 8 pairs: the state has only 8 × 8 = 64 numbers. No rule can store arbitrarily many independent pairs in it; the error rises for everyone.

The update rules

Softmax attention reads with o = Σᵢ softmax(q·kᵢ) vᵢ over all earlier pairs, so it needs every kᵢ, vᵢ: the cache grows with each token.

Linear attention keeps a matrix S instead. Writing is S ← S + v kᵀ, reading is o = S q. The size is constant, but a write never removes anything.

Delta rule: S ← S + β (v − S k) kᵀ. The term S k is what the memory currently returns for the key; only the error v − S k is written. With a unit-length key and β = 1 the memory answers exactly v for this key afterwards. It is one step of online gradient descent on ½‖S k − v‖² (the classic delta, or Widrow–Hoff, rule).

Gated delta rule adds forgetting: S ← α (S − β (S k) kᵀ) + β v kᵀ, with α between 0 and 1. In the demo, α also applies to plain linear attention (S ← α S + v kᵀ) so the comparison stays fair.

Why it matters for real models

The state per head is a fixed d×d matrix, so memory per sequence does not grow with context length. That is why several recent models mix many linear-attention layers with a few full-attention layers. In the KV-cache calculator pick Qwen3-Next-80B-A3B: its 36 linear layers add a constant state, and only 12 layers hold a growing cache.

This demo is a toy: tiny dimension, one head, no learned projections, no normalisation, and random vectors instead of text. It shows the write rules, not model quality. Real models also run the recurrence in parallel chunks on the GPU.

References

  1. Katharopoulos et al., Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention (2020)
  2. Schlag, Irie, Schmidhuber, Linear Transformers Are Secretly Fast Weight Programmers (2021)
  3. Yang et al., Parallelizing Linear Transformers with the Delta Rule over Sequence Length (2024)
  4. Yang, Kautz, Hatamizadeh, Gated Delta Networks: Improving Mamba2 with Delta Rule (2024)