Linear attention: a memory that is learned

attention
Remove the softmax and the KV cache becomes one small matrix. Each token updates it with a gradient step, then reads it.
Published

January 10, 2026

Linear attention swaps the growing KV cache for one matrix of fixed size. Each token updates the matrix with one gradient step, then reads it. Per-token memory and compute stay fixed as the context grows, and exact recall gets worse.

Below is the derivation with small matrices and a few lines of Python. Shapes are in brackets.

What softmax attention keeps

Take one attention head. A new token comes in with a query q [1, dk]. The past is two matrices, one row per earlier token: keys K [T, dk] and values V [T, dv].

p = softmax(q @ K.T)      # [1, dk] @ [dk, T] -> [1, T]    one weight per past token
o = p @ V                 # [1, T]  @ [T, dv] -> [1, dv]

q has to touch every row of K, and the softmax needs all T scores at once to normalize them. So the KV cache holds every past key and value. It grows by one row per token, and every new token reads all of it again.

Drop the softmax

Without the softmax the output is two matrix products, and you can move the brackets:

o = (q @ K.T) @ V   =   q @ (K.T @ V)

K.T @ V is [dk, T] @ [T, dv] -> [dk, dv]. T is gone from the shape, and q is not in it. Call it S. It depends only on the past, so it is ready before the new token shows up, and the new token only computes q @ S.

The loop

K.T @ V is a sum over tokens: one outer product of key and value per token. So you can build S one token at a time:

S = zeros(dk, dv)                 # [dk, dv], never grows
for each new token:
    S += outer(k, v)              # [dk] x [dv] -> [dk, dv]    write
    o  = q @ S                    # [1, dk] @ [dk, dv] -> [1, dv]    read

Three tokens, dk = dv = 2:

K  [3, 2]         V  [3, 2]
[[1, 0],          [[10, 1],
 [0, 1],           [20, 2],
 [1, 1]]           [30, 3]]

after token 0:  S = [[10, 1], [ 0, 0]]
after token 1:  S = [[10, 1], [20, 2]]
after token 2:  S = [[40, 4], [50, 5]]

Token 1 has q = [1, 1] and reads q @ S = [30, 3]. Softmax-free attention over tokens 0 and 1 gives the same thing: (q @ K[:2].T) @ V[:2] = [30, 3].

Each token reads S right after its own write, when S holds tokens 0 to t and nothing later. That keeps the loop causal without a mask.

What it saves

There is no KV cache. The only state is S, and it is the same size at token 10 and at token 1,000,000.

One head with dk = dv = d, one new token:

softmax linear
state kept between tokens K and V, 2·T·d numbers S, d² numbers
numbers read per token 2·T·d d²
FLOPs per token 4·T·d 4·d²

At d = 128 and T = 32,000, softmax reads about 8 million numbers per token and linear attention reads 16,384, about 500 times less.

Over a full prompt of T tokens, softmax attention builds a [T, T] score matrix and costs 4·T²·d FLOPs. Linear attention costs 4·T·d², linear in T.

What it costs

Softmax makes the weights sharp: a score that is 3 higher gets about 20 times the weight (e³), not 3 times. Without the softmax, a read meant for one token out of thousands mixes in the values of the others.

S is also small. It holds about dk key-value pairs before they start to mix. Schlag, Irie and Schmidhuber (2021) measured this: with keys of 64 numbers, reads start to fail at 60 or more stored pairs.

And writing twice under the same key does not replace the old value. It adds to it:

k = [0.6, 0.8]
write (k, [10, 1]):   k @ S = [10, 1]
write (k, [30, 3]):   k @ S = [40, 4]     we wanted [30, 3]

The delta rule

S is a key-value store, so one thing should hold: after you store (k, v), reading with k gives back v.

k @ S_new = v

Every write in linear attention has the form S + outer(k, c), where c is the vector you write under k:

S_new = S + outer(k, c)

Read the new memory with k:

k @ S_new = k @ S + k @ outer(k, c)

The second term is (k·k) * c. If k is normalized, k·k = 1 and the term is just c:

k = [0.6, 0.8]    c = [10, 1]
outer(k, c)     = [[6, 0.6], [8, 0.8]]
k @ outer(k, c) = [0.6*6 + 0.8*8, 0.6*0.6 + 0.8*0.8] = [10, 1] = c

So the read is k @ S + c, and you want it to be v:

v = k @ S + c
c = v - k @ S

Put c back into the write:

S_new = S + outer(k, v - k @ S)

The fix is one changed line in the loop:

S += outer(k, v)                  # before
S += outer(k, v - k @ S)          # after

Or in two steps:

pred = k @ S                      # [dk] @ [dk, dv] -> [dv]    what S returns for this key now
err  = v - pred                   # [dv]                       the gap to the new value
S   += outer(k, err)              # write the gap, not the value

c is err. Now the second write from the example above lands where it should:

S = [[6, 0.6], [8, 0.8]]          k @ S = [10, 1]
new v = [30, 3]                   c = [20, 2]
S_new = [[18, 1.8], [24, 2.4]]    k @ S_new = [30, 3] = v

The overwrite is exact only when k·k = 1. Gated DeltaNet L2-normalizes q and k, which it says is for training stability.

It is a gradient step

The delta rule is one step of gradient descent on the loss ½ ||k @ S - v||² with respect to S (Gated DeltaNet, section 3.1), with step size 1 in the code above.

S is a weight matrix that the model trains on its own context, one gradient step per token, inside the forward pass. No backprop runs here; the update is written in closed form.

Two gates

Gated DeltaNet adds two learned numbers per token:

for t in range(T):
    S    = alpha[t] * S                   # scalar * [dk, dv]    forget: shrink the whole memory a little
    pred = k[t] @ S                       # [dv]
    err  = v[t] - pred                    # [dv]
    S   += beta[t] * outer(k[t], err)     # write the gap, with strength beta
    o[t] = q[t] @ S                       # [dv]
  • alpha is the forget gate, shape [T]. Near 0 clears the memory.
  • beta is the step size, shape [T]. Near 0 skips the token.

Both come from a linear projection of the token and lie between 0 and 1. Gated DeltaNet uses Mamba2’s parameterization for alpha; a sigmoid is the simple choice for beta.

q = x @ Wq                                  # [T, dk]
k = F.normalize(x @ Wk, dim=-1)             # [T, dk]   length 1, so k·k = 1
v = x @ Wv                                  # [T, dv]
alpha = decay(x @ Wa).squeeze(-1)           # [T]   in (0, 1), Mamba2's parameterization
beta  = torch.sigmoid(x @ Wb).squeeze(-1)   # [T]   in (0, 1)

GLA uses a different forget gate: one decay rate per row of S, so alpha has shape [T, dk].

The model learns at two speeds:

  • In training, backprop learns Wa and Wb together with Wq, Wk, Wv. This is where the model learns when to forget and how hard to write.
  • At inference those weights are frozen, and S does the learning, from the context, one token at a time, with alpha and beta setting each step.

Papers

  • Schmidhuber, “Learning to Control Fast-Weight Memories: An Alternative to Dynamic Recurrent Networks”, Neural Computation 4(1):131–139, 1992. Tech report FKI-147-91, 1991.
  • Schlag, Irie, Schmidhuber, “Linear Transformers Are Secretly Fast Weight Programmers”, arXiv 2102.11174, 2021.
  • Yang, Kautz, Hatamizadeh, “Gated Delta Networks: Improving Mamba2 with Delta Rule”, arXiv 2412.06464, 2024.
  • Irie, Gershman, “Fast weight programming and linear transformers: from machine learning to neurobiology”, arXiv 2508.08435, 2025.