All courses

MLPs as key-value memories for facts

Interpret the input-weight rows as keys that detect patterns in the residual stream and the output-weight rows as values written back. Relate this to evidence that factual associations are localized in mid-layer MLPs.

Builds on: The residual stream as a shared channel

▶ Watch this lesson Start the whole course

What the animation says

Where is "Paris"?

The prompt reads: The Eiffel Tower is in the city of. The model answers: Paris. But look. Paris appears nowhere in the prompt. Attention can only move information between tokens that are already there. So where can the word Paris come from?

An MLP is a soft dictionary

It comes from the weights, and about two thirds of them sit in the MLP sublayers. Treat each hidden unit as one dictionary slot with two vectors: a key and a value. The key is compared with the residual stream by a dot product. Does the stream contain this pattern? The activation turns that score into a strength. The value is added back into the stream, scaled by that strength. Unlike a Python dict, matching is graded, many slots fire at once, and their values add up.

Keys and values in the matrices

For one position, the MLP multiplies the stream by W in, adds a bias, applies the activation, then multiplies by W out. Key i is column i of W in. Its strength is the activation of stream dot key, plus bias i. Value i is row i of W out. So the whole MLP is a sum over slots: each strength times its value. In PyTorch, the weights are stored transposed: keys are rows, values are columns. With SwiGLU, a second vector gates the strength, and the picture stays the same.

Two slots, real numbers

Let's run it. Width three, two slots, ReLU, and the stream is one, zero, one. We skip the norm. Slot one: key one, zero, one, bias minus one. The dot product is two. Minus one gives one, and ReLU keeps it: strength one. Slot two: key zero, one, minus one, bias zero. The dot product is minus one, and ReLU clips it to zero. Values: slot one writes zero, two, zero. Slot two would write five, five, five. What is the stream after the MLP?

Causal tracing

The stream is one, two, one. The MLP added its output, and slot two's value was never written. Now, where do real models store facts? Researchers use causal tracing. First, add noise to the embeddings of Eiffel Tower. The probability of Paris collapses. Then restore one site's activations from the clean run, and see whether Paris comes back. Tap the MLP site you think brings Paris back most strongly.

Write, then carry

Restoring mid-layer MLPs at Tower, the subject's last token, brings Paris back strongly. Late-layer MLPs there do not. And a small targeted edit to one such MLP's W out makes the model say Rome, even for reworded prompts. Caveat: facts are spread over several layers, and edits are imperfect. So: keys detect patterns in the stream, and values are added back, weighted by how strongly each key fired. Mid-layer MLPs write the subject's attributes into the stream. Later attention carries them to where the answer is produced.

Where does the model keep "Paris"?

Given *"The Eiffel Tower is in the city of"*, a model writes **Paris**. That word is not in the prompt. Attention can only move information between tokens that are already there, so the fact must come from the weights. With $d_{ff}=4d$, about two-thirds of a transformer's non-embedding parameters sit in the MLP sublayers. **How can a position-wise MLP store a fact, and where in the network does this happen?**

An MLP is a soft dictionary

Treat each of the $d_{ff}$ hidden units as one dictionary slot with two vectors. The **key** is compared with the residual stream by a dot product, which asks "does the stream contain this pattern?" The activation turns that score into a strength $a_i$. The **value** is added back into the stream, scaled by $a_i$. Unlike a Python dict, matching is graded, many slots fire at once, and their values add up.

The MLP as a sum of key-value pairs

For one position's normalized row $x\in\mathbb{R}^{1\times d}$, with bias $b\in\mathbb{R}^{1\times d_{ff}}$: $$\text{MLP}(x)=\phi(xW_{in}+b)W_{out}=\sum_{i=1}^{d_{ff}} a_i\,v_i$$ - Key $k_i$ = column $i$ of $W_{in}\in\mathbb{R}^{d\times d_{ff}}$. PyTorch stores this as row $i$ of its weight. - Score: $a_i=\phi(xk_i+b_i)$, where $\phi$ is the activation. - Value $v_i$ = row $i$ of $W_{out}\in\mathbb{R}^{d_{ff}\times d}$. PyTorch stores this as column $i$ of its weight. With SwiGLU, $a_i=\text{SiLU}(xk_i)\,(xg_i)$. This is a gated score, and the key-value picture is the same.

Two slots, actual numbers

Take $d=3$, $d_{ff}=2$, ReLU, and stream $x=[1,0,1]$. We skip the norm here, so the MLP reads the stream directly. - Slot 1: $k_1=[1,0,1]$, $b_1=-1$, so $xk_1+b_1=2-1=1$ and $a_1=1$. - Slot 2: $k_2=[0,1,-1]$, $b_2=0$, so $xk_2=-1$ and $a_2=0$. With $v_1=[0,2,0]$ and $v_2=[5,5,5]$: $\text{MLP}(x)=1\cdot v_1+0\cdot v_2=[0,2,0]$. The new stream is $x+[0,2,0]=[1,2,1]$. Slot 2's value was never written.

Evidence: facts live in mid-layer MLPs

**Causal tracing** on GPT-style models works in two steps. First, add noise to the embeddings of the subject tokens ("Eiffel Tower"), and P(Paris) collapses. Then restore activations at one site from the clean run. Restoring **mid-layer MLP outputs at the subject's last token** brings Paris back strongly. Late-layer MLPs at that token do not. A small targeted edit to one such MLP's $W_{out}$ makes the model say "Rome", even for reworded prompts. Caveat: facts are spread over several layers, and edits are imperfect.

Remember

An MLP is $d_{ff}$ key-value pairs. Keys (columns of $W_{in}$) detect patterns in the residual stream. Values (rows of $W_{out}$) are added back, weighted by how strongly each key fired. For factual recall, mid-layer MLPs at the subject's last token write its attributes into the stream. Later attention carries them to where the answer is produced.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.