← all writing

Recurrent Memory: Write, Erase, Decay, Read

Part 4 of 4 on how a transformer remembers. KV cache · Linear attention · Kernelization · → Recurrent memory

Linear attention compresses the KV cache into one fixed-size board, and the price is that everything written lands on top of everything else. From 2020 to 2025, the entire architecture line has been doing one thing: teaching that board how to forget. Four operations, four knobs — drag them yourself and DeltaNet, Mamba-2 and KDA turn out to be the same equation on different settings.

Everything fits on one board

Attention keeps every token. That’s the whole trick and the whole bill: the KV cache grows with sequence length, and so does the cost of reading it back.

Linear attention makes a different trade. Instead of a growing list, keep one fixed $d\times d$ matrix $S$. Write into it, read out of it. Cost per token is constant, memory is constant, and the sequence can be as long as you like.

The bill just arrives somewhere else. A fixed-size board means every write lands on top of every other write. Nothing gets its own slot; everything is superposed. So the interesting question stops being how do I write and becomes how do I forget.

That’s the whole post. Four things you can do to one board — write, erase, decay, read — and by the end, one equation that covers five years of architectures.

《transformer 怎么记东西》四篇之四。 KV 缓存 · 线性注意力 · 核化 · → 递归记忆

线性注意力把 KV cache 压成了一块固定大小的板子,代价是写进去的东西会互相叠加。从 2020 到 2025,整条架构演进线其实只在做一件事:给这块板子加上「怎么忘」的能力。四个操作、四个旋钮——自己拧一遍,你会发现 DeltaNet、Mamba-2、KDA 只是同一个公式的不同档位。

所有东西都挤在一块板子上

注意力把每个 token 都留着。这是它全部的本事,也是它全部的账单:KV cache 随序列长度增长,读它的代价也跟着长。

线性注意力做了另一种取舍。不留一张越来越长的清单,只留一个固定的 $d\times d$ 矩阵 $S$。往里写,从里读。每个 token 的代价恒定,显存恒定,序列想多长就多长。

账单只是换了个地方出现。板子大小固定,意味着每一次写入都压在别人身上。没有哪条信息能独占一个格子,所有东西都是叠加态。于是有意思的问题不再是怎么写,而是怎么忘

这篇就讲这个。一块板子上能做的四件事——写入、擦除、衰减、读取——最后收成一个覆盖五年架构史的公式。

01 One full round trip

The minimal unit of recurrent memory has two moves. Write: slam the outer product of a $(k,v)$ pair onto the board. Read: multiply the board by a query.

\[\underbrace{S \leftarrow S + k^{\top}v}_{\text{write}}\qquad\qquad \underbrace{o = qS}_{\text{read}}\]

$k, v, q$ are row vectors of length $d$, so $k^{\top}v$ is $d\times d$ and $qS$ is $1\times d$. Does it actually work? Query with the exact key you wrote with:

\[kS = k\,(k^{\top}v) = (k k^{\top})\,v = \|k\|^2\,v\]

The direction comes back perfect. The magnitude is off by a factor of $|k|^2$. Drag the slider and watch that factor:

01 一次完整的往返

递归记忆的最小单元只有两个动作。写:把一对 $(k,v)$ 的外积拍进板子。读:用一个 query 去乘这块板子。

\[\underbrace{S \leftarrow S + k^{\top}v}_{\text{写}}\qquad\qquad \underbrace{o = qS}_{\text{读}}\]

$k, v, q$ 都是长度 $d$ 的行向量,所以 $k^{\top}v$ 是 $d\times d$,$qS$ 是 $1\times d$。这套真的能用吗?把 query 换成写进去时用的那个 key:

\[kS = k\,(k^{\top}v) = (k k^{\top})\,v = \|k\|^2\,v\]

方向取回来完全正确,但幅度被 $|k|^2$ 缩放了。拖动滑块看这个缩放:

02 If all you can do is add

The board has a plus. It has no minus. That alone causes two problems, and both of them are fatal.

Problem one: you can’t fix a mistake. Write twice with the same key — a variable that got reassigned, say — and a pure-accumulation board ends up holding both values stacked on top of each other. The read gives you their sum, not the latest one. Flip the switch to see the difference:

02 如果只会「加」

板子只有加法,没有减法。光是这一点就会导致两个后果,而且两个都是绝症。

绝症一:写错了改不掉。用同一个 key 写两次——比如同一个变量被重新赋值——纯累加的板子上,两个值会直接叠在一起,读出来是它们的和,而不是最新的那个。打开开关看差别:

Problem two: it fills up, and there’s no getting off. The board is $d\times d$, and in $d$-dimensional space there are at most $d$ mutually orthogonal directions. The $(d+1)$-th key necessarily has nonzero inner product with something already stored. Crosstalk isn’t bad luck, it’s arithmetic. Here’s a $d=6$ board with more and more facts packed into it — watch mean recall collapse. And flip the switch: it separates the two diseases, and the second one is where erasing actually earns its keep.

绝症二:装满了下不了车。板子是 $d\times d$,而在 $d$ 维空间里最多只有 $d$ 个互相正交的方向。第 $d+1$ 个 key 必然和已经存进去的东西有非零内积。串味不是运气不好,是算术。下面是一块 $d=6$ 的板子,往里塞越来越多条事实,看平均召回怎么塌下去。另外记得打开那个开关:它把两个绝症分开来看,而擦除真正值钱的地方在第二个。

03 Delta Rule: read before you write

The fix is embarrassingly plain. It’s how you’d update a hash table: look at what’s stored under this key, then write only the difference. Three lines.

\[\begin{aligned} v_{\text{old}} &= k_t S_{t-1} &&\text{// ① read}\\ u_t &= \beta_t\,(v_t - v_{\text{old}}) &&\text{// ② the delta}\\ S_t &= S_{t-1} + k_t^{\top}u_t &&\text{// ③ write back} \end{aligned}\]

Expand that and a nicer form falls out (with $|k|=1$):

\[S_t = \big(I - \beta_t\,k_t^{\top}k_t\big)\,S_{t-1} \;+\; \beta_t\,k_t^{\top}v_t\]

That $(I-\beta k^{\top}k)$ on the left is a generalized Householder matrix. At $\beta=1$ it is exactly the orthogonal projection that annihilates the $k$ direction — and leaves every memory orthogonal to $k$ completely untouched, not one hair moved. At $\beta<1$ it’s a partial version: it scales the $k$ component down instead of deleting it. (At $\beta=2$ you’d get the true Householder reflection; learned gates stay in $[0,1]$.)

The linearity is also why this idea survived. The erase step is an honest linear map, not an elementwise if-else, so it can be reassociated into chunkwise-parallel form and actually run fast on a GPU. That’s what Yang et al. did to DeltaNet in 2024 — and it’s why a 2021 idea became a 2025 architecture.

03 Delta Rule:写之前,先读一次

解法朴素得让人尴尬。就是你改哈希表的做法:先看这个 key 现在存的是什么,再只写「差值」。三行。

\[\begin{aligned} v_{\text{old}} &= k_t S_{t-1} &&\text{// ① 先读}\\ u_t &= \beta_t\,(v_t - v_{\text{old}}) &&\text{// ② 只留差值}\\ S_t &= S_{t-1} + k_t^{\top}u_t &&\text{// ③ 写回去} \end{aligned}\]

把它展开,会露出一个更漂亮的形式(取 $|k|=1$):

\[S_t = \big(I - \beta_t\,k_t^{\top}k_t\big)\,S_{t-1} \;+\; \beta_t\,k_t^{\top}v_t\]

左边那个 $(I-\beta k^{\top}k)$ 就是广义 Householder 矩阵。$\beta=1$ 时它恰好是把 $k$ 方向打掉的正交投影——而与 $k$ 正交的其它记忆一根毛都不动。$\beta<1$ 时是它的部分版本:把 $k$ 方向上的分量按比例压小,而不是直接删掉。($\beta=2$ 才是真正的 Householder 反射;学出来的门控只会待在 $[0,1]$ 里。)

线性也正是这个想法能活下来的原因。擦除这一步是个规规矩矩的线性映射,不是逐元素的 if-else,所以它可以被重新结合成分块并行的形式,在 GPU 上真的跑得快。这就是 Yang 等人 2024 年对 DeltaNet 做的事——也是一个 2021 年的想法能变成 2025 年架构的原因。

04 Decay: let old memories fade on their own

The Delta Rule has a blind spot: it can only forget things that have a replacement coming in. Topic switched, section finished, you just want the room back — it can’t help you, because there’s no new $v$ to write.

Mamba-2’s answer is blunt. Every step, multiply the whole board by something less than 1.

\[S_t = \alpha_t\,S_{t-1} + k_t^{\top}v_t\]

A token written at time $x$ — how much of it is left at $x+t$? Multiply the $\alpha$’s along the way:

\[\prod_{i=x}^{x+t}\alpha_i \;=\; \exp\!\Big(\sum_{i=x}^{x+t}\log \alpha_i\Big)\]

The right-hand form matters more than it looks. A running product becomes a prefix sum of logs, and prefix sums parallel-scan. That is the entire reason gating fits inside a chunkwise kernel at all.

04 衰减:让旧记忆自己褪色

Delta Rule 有个盲区:它只能忘掉「有新内容顶替」的东西。话题切换了、这一段读完了、你只是想把容量腾出来——它无能为力,因为没有新的 $v$ 可以写进去。

Mamba-2 给的答案简单粗暴。每一步都让整块板子先乘一个小于 1 的数。

\[S_t = \alpha_t\,S_{t-1} + k_t^{\top}v_t\]

一个在时刻 $x$ 写入的 token,到 $x+t$ 时还剩多少?把路上每一步的 $\alpha$ 连乘起来:

\[\prod_{i=x}^{x+t}\alpha_i \;=\; \exp\!\Big(\sum_{i=x}^{x+t}\log \alpha_i\Big)\]

右边这个形式比看上去重要得多。连乘变成了对数的前缀和,而前缀和可以并行扫描。门控能塞进 chunkwise 内核,全靠这一步。

05 The mixing board

One equation covers the whole line from 2020 to 2025:

\[S_t \;=\; \underbrace{\mathrm{Diag}(\boldsymbol{\alpha}_t)}_{\text{decay}}\;\underbrace{\big(I - \beta_t k_t^{\top}k_t\big)}_{\text{erase}}\;S_{t-1} \;+\; \underbrace{\beta_t\,k_t^{\top}v_t}_{\text{write}}\]

Turn erase off and pin $\alpha$ to 1 — that’s 2020 linear attention. Turn erase on — DeltaNet. Leave erase off but pull $\alpha$ down — gated linear attention, the Mamba-2 shape. Both at once — Gated DeltaNet. Swap the scalar $\alpha$ for a per-channel vector — KDA, the layer Kimi Linear is built out of.

Go dial it yourself. The panel runs a 10-step write script — including one overwrite with a repeated key, and one write past capacity — then reads every fact back and scores it.

05 调音台

下面这一个式子涵盖了从 2020 到 2025 的整条线:

\[S_t \;=\; \underbrace{\mathrm{Diag}(\boldsymbol{\alpha}_t)}_{\text{衰减}}\;\underbrace{\big(I - \beta_t k_t^{\top}k_t\big)}_{\text{擦除}}\;S_{t-1} \;+\; \underbrace{\beta_t\,k_t^{\top}v_t}_{\text{写入}}\]

关掉擦除、把 $\alpha$ 拧到 1,它就是 2020 年的线性注意力。打开擦除,它是 DeltaNet。擦除不开、只把 $\alpha$ 拧下来,它是门控线性注意力,也就是 Mamba-2 那个形状。两个一起开,是 Gated DeltaNet。把标量 $\alpha$ 换成逐通道的向量,它就是 KDA——Kimi Linear 拿来搭层的那个东西。

自己拧一遍。下面这块面板跑的是一段 10 步的写入剧本——中间有一次用重复 key 的覆盖,还有一次写到超出容量——跑完之后把每条事实读回来打分。

So what did five years actually buy

Look at the equation again and the history reads as one sentence: linear attention was born able to write, and everything since has been about learning to forget.

2020 gave you addition. 2021 added a subtraction that is surgical but reactive — it can only erase where something new is arriving. 2024 added a decay that is bulk but blind — it can clear room, but it fades the function signature you still need along with the temp variable you don’t. Multiplying them together fixed neither weakness individually; it just meant the model no longer had to pick one. And 2025’s move — per-channel $\alpha$ — is the smallest-looking change with the biggest consequence: the model stops choosing only how much to forget and starts choosing which dimensions to forget in.

But dial the demo past capacity and you’ll see the thing none of this fixes. Nine facts into a six-dimensional board, and every rule is losing information. The decaying rules even score worse on “all facts” — because they’ve deliberately written off the oldest entries to keep the newest ones at near-perfect recall. That’s not a defect. That is the correct trade, and it’s why the last knob matters at all.

Which is also why nobody ships a pure linear stack. Kimi Linear interleaves three KDA layers for every one full-attention layer; Qwen3-Next builds the same shape out of Gated DeltaNet. The recurrent layers handle the near, the frequent, the constant-cost majority; the occasional full-attention layer goes back and fishes out whatever the board decided to sacrifice. Linear attention was never a replacement for attention. It’s the cheap 75% of the stack — and getting that 75% right turned out to be a five-year argument about forgetting.


A $d\times d$ board, a plus, a minus, and a fade. Everything else is scheduling.

References

  • Linear attention — Katharopoulos et al., Transformers are RNNs: Fast Autoregressive Transformers with Linear Attention (arXiv:2006.16236)
  • Delta rule / fast weights — Schlag, Irie, Schmidhuber, Linear Transformers Are Secretly Fast Weight Programmers (arXiv:2102.11174)
  • Making DeltaNet fast — Yang et al., Parallelizing Linear Transformers with the Delta Rule over Sequence Length (arXiv:2406.06484)
  • Gating — Yang et al., Gated Linear Attention Transformers with Hardware-Efficient Training (arXiv:2312.06635) · Dao & Gu, Transformers are SSMs (Mamba-2, arXiv:2405.21060)
  • Both at once — Yang, Kautz, Hatamizadeh, Gated Delta Networks: Improving Mamba2 with Delta Rule (arXiv:2412.06464)
  • Per-channel gating — Moonshot AI, Kimi Linear: An Expressive, Efficient Attention Architecture (arXiv:2510.26692)

Every panel above is live, interactive and bilingual — drag the knobs, switch the language, and the demos follow.

这五年到底买到了什么

再看一眼那个式子,这段历史可以缩成一句话:线性注意力生下来就会写,之后所有的事都是在学怎么忘。

2020 年给了你加法。2021 年补上一个减法,精准,但被动——它只能在有新东西要写进来的地方擦。2024 年补上一个衰减,管得宽,但眼瞎——它能腾地方,但会把你还要用的函数签名,和你不要的临时变量,一起淡掉。两个乘在一起,并没有单独治好其中任何一个毛病;它只是让模型不必二选一了。而 2025 年那一步——逐通道的 $\alpha$——看着改动最小,后果最大:模型不再只能选择忘多少,而是开始选择在哪些维度上忘

但把 demo 拖过容量线,你会看到这一切都治不好的那件事。九条事实塞进六维的板子,任何规则都在丢信息。带衰减的那几档在「全部事实」上甚至更难看——因为它们主动把最老的几条注销掉,换最新几条接近满分的召回。这不是缺陷,这就是正确的取舍,也正是最后那个旋钮存在的意义。

这同样解释了为什么没人真的上纯线性的栈。Kimi Linear 每三层 KDA 插一层全注意力;Qwen3-Next 用 Gated DeltaNet 搭出同样的形状。递归层负责近期的、高频的、恒定成本的大头;偶尔一层全注意力回头去把板子决定牺牲掉的东西捞回来。线性注意力从来不是注意力的替代品。它是这个栈里便宜的那 75%——而把这 75% 做对,最后变成了一场持续五年、关于「怎么忘」的争论。


一块 $d\times d$ 的板子,一个加号,一个减号,一次褪色。剩下的都是调度。

参考

  • 线性注意力 — Katharopoulos 等, Transformers are RNNs: Fast Autoregressive Transformers with Linear AttentionarXiv:2006.16236
  • Delta rule / 快速权重 — Schlag, Irie, Schmidhuber, Linear Transformers Are Secretly Fast Weight ProgrammersarXiv:2102.11174
  • 让 DeltaNet 跑得快 — Yang 等, Parallelizing Linear Transformers with the Delta Rule over Sequence LengtharXiv:2406.06484
  • 门控 — Yang 等, Gated Linear Attention Transformers with Hardware-Efficient TrainingarXiv:2312.06635)· Dao & Gu, Transformers are SSMs(Mamba-2,arXiv:2405.21060
  • 两个一起开 — Yang, Kautz, Hatamizadeh, Gated Delta Networks: Improving Mamba2 with Delta RulearXiv:2412.06464
  • 逐通道门控 — Moonshot AI, Kimi Linear: An Expressive, Efficient Attention ArchitecturearXiv:2510.26692

上面每一块面板都是实时、可交互、双语的——拖动旋钮、切换语言,演示会跟着变。