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
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.
An MLP is a soft dictionary
The MLP as a sum of key-value pairs
Two slots, actual numbers
Evidence: facts live in mid-layer MLPs
Remember
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.
Bytewise
All courses