TL;DR
Rank-one model editing changes one stored fact inside a trained Transformer without retraining it. It adds a rank-one matrix (a single column times a single row) to one weight matrix in one feedforward layer, so that a chosen input key now produces a chosen output value. A closed-form formula gives the update, and it is chosen to disturb other stored memories as little as possible.
Builds on: Transformer, Causal Tracing, Matrix Inverse, Linear Regression, Low-Rank Matrix, Covariance Matrix · Leads to: ROME
Where it's used
Correcting one fact in a deployed model. Suppose a chat assistant built on a Language Model says the Space Needle is in Seattle, and the product team must make it say Paris. Retraining the whole model costs far more than a targeted change. Fine-tuning on the single fact is cheaper but moves the whole network, which can break answers that were correct before. A rank-one edit touches one matrix in one layer and needs one small calculation.
Benchmarking model editors. Research groups compare editing methods on two test sets. The zero-shot relation extraction set (zsRE) has 10,000 records, each with a fact, a paraphrase, and an unrelated fact. The CounterFact set, introduced with the method that this lesson describes, has 21,919 counterfactual records. A good editor must make the new answer appear, keep it under paraphrase, and leave unrelated answers alone.
Testing an interpretability claim. Causal Tracing says that a fact is carried by the feedforward (MLP) module at the last token of the subject, in middle layers. If that is true, editing that matrix should move the fact and leave the rest of the model alone. The edit is therefore a test of the claim, not only a tool: when the edit works and the neighbours hold, the localisation claim gains support.
How it works
Set-up: a tiny memory. The matrix is , the 2 by 2 identity, so the layer returns its input unchanged. The new fact must map the key to the value . Assume the old keys have covariance , so every input direction is equally busy.
- Start with the old matrix. Feed the key through the layer: . The layer currently answers with . Check: this is not the value we want, .
- Measure the gap. Subtract: . This is the amount the output must still move. Check: .
- Pick the direction to write in. Compute . The matrix records how busy each input direction is for the old keys. Multiplying by makes busy directions expensive to write in. Here , so . Check: nothing is scaled, so .
- Scale so the key hits the value. Compute the number , then . Check: , which is exactly .
- Write the rank-one update. Form . Every row is a multiple of , so the update changes the matrix along one output direction and one input direction.
- Check the edit. The new matrix is . Then . The fact is inserted.
Only step 3 looks at the old memories, through . Everything else uses only the new key and value.
flowchart LR A["Old matrix W, new key k and value v"] --> B["Gap r = v minus W k"] B --> C["Direction u = C inverse times k"] C --> D["Scale: Lambda = r divided by u dot k"] D --> E["New matrix = W plus Lambda times u transpose"] E --> F["Check: new matrix times k equals v"]
The math
Let be a weight matrix, a key, and a value. Let hold old keys as columns and be their (uncentered) covariance. The rank-one edit is
and it satisfies .
In words: is the layer's matrix, is the key for the new fact, is the value it should produce, is the matrix of the old keys, is the key after the reweighting, and is a column vector that sets the size of the change. The product is an outer product, so it has rank one (the rank is the number of independent columns, and every column is a multiple of ).
Step 1 in symbols: the old matrix
The layer maps any key to the output . Old facts satisfy , where stacks their values. In the walkthrough and , so .
Step 2 in symbols: the residual
. In the walkthrough, .
Step 3 in symbols: the direction
. The inverse exists when the old keys span every input direction, which is why the paper estimates from many text samples rather than a few. In the walkthrough , so .
Step 4 in symbols: the scale
is a single number. Then . In the walkthrough and . Check: .
Step 5 and 6 in symbols: the update and the check
and . Applying the new matrix to the key:
Why this update and not another
Other choices of also satisfy , so the constraint alone does not decide. The old memories decide. Their damage is measured by , the squared size of the change in the old outputs, which equals . The edit therefore solves: minimise subject to . Setting the gradient of the Lagrangian to zero gives for some vector , and the constraint fixes . That is the formula above. The paper derives it in its appendix.
One property follows at once: if is multiplied by any positive number , then becomes , becomes , and becomes , so does not change. Only the shape of matters.
Worked examples
Set-up: , , , and .
- Old output: .
- Gap: .
- Direction: .
- Scale: .
- Column: .
- Update: . Both rows are multiples of , so the rank is one.
- New matrix: .
- Check: .
Interpretation: a 2 by 2 matrix was changed by a single outer product, and the key now produces the requested value.
Set-up: (2 by 2), , . The old keys have covariance , so the second input direction is busy (variance 4) and the first is quiet (variance 1).
- Old output: .
- Gap: .
- Direction: , so .
- Scale: .
- Column: .
- Update: . Check: and .
- New matrix: .
- Check: .
Interpretation: the first column of (on the quiet input) is four times the second column (on the busy input). The edit writes most of its change where old keys rarely look.
Set-up: the same , , and as Example 2. In a real model keys have thousands of coordinates, but the arithmetic is the same. Take a neighbouring subject, an old memory with key whose output before the edit is .
- Before the edit, the neighbour outputs .
- After the edit, the neighbour outputs , the second column of . The change is , of length .
- A naive edit adds the gap along the key without using : . Check: .
- Try it: under the naive edit, how large is the neighbour's output change, and how does it compare with the rank-one edit's change of length ? Work it out before reading on.
Answer: , of length . That is 2.5 times the rank-one change. The neighbour's key lies in the busy direction (variance 4), and the weighting makes writes into that direction small relative to the quiet one, so the neighbour moves less.
Common mistakes and tradeoffs
It is tempting to think a rank-one edit changes only the one fact. In fact it changes a whole matrix, so every input that has a component along moves a little, including paraphrases of the prompt and neighbouring subjects. The weighting reduces that damage but cannot remove it, as Example 3 shows. This is why the paper reports specificity scores next to success scores.
It is tempting to think the key is the whole prompt. In fact the key is one stored activation: the feedforward input at the last token of the subject, in one layer, averaged over short random prefixes so that it does not depend on the words before the subject.
It is tempting to think the edit is a retraining run. In fact no weights are trained for the fact. The value is found by a small optimisation over a vector, which asks the network to output the new object while keeping a Kullback–Leibler Divergence penalty small on a prompt about the subject alone. The weights then change by one closed-form formula.
It is tempting to think edits stack for free. In fact each edit adds its own error to the same matrix, so many edits interfere with each other. Later methods edit many facts at once to deal with this.
When to use it: one fact must change in a model you cannot retrain, and causal tracing has located the fact in a specific layer. When not to use it: many facts must change together, the fact is spread over many layers, or the change must hold under every paraphrase. Alternatives include Fine-Tuning on the new fact (broad, and it can damage other answers), hypernetwork editors such as KE and MEND (which learn to predict edits and need training), and putting the fact in the prompt or in retrieved context (no weight change at all).
History
The idea of a weight matrix as a memory is old. Kohonen (1972) and Anderson (1972) described linear associative memories, in which a matrix stores key-value pairs and recalls a value when given its key. Bau and colleagues (2020) edited the weights of a generative image model to change what it draws, which showed that single layers could be changed directly. Their method needed an optimiser rather than a closed-form update.
Earlier model editors used fine-tuning with constraints (Zhu et al., 2020), interpretability-based changes (Dai et al., 2022), and hypernetworks, KE by De Cao et al. (2021) and MEND by Mitchell et al. (2021). Meng, Bau, Andonian and Belinkov published the closed form for fully connected layers, paired it with causal tracing, and reported it in 2022. It caught on because it needs no training per edit and because the CounterFact benchmark showed differences that the older zsRE test did not reveal.
Recap
- A feedforward layer acts as a linear associative memory: its matrix maps keys to values.
- To insert a fact with key and value , compute the gap , the reweighted direction , and the scale .
- The update has rank one and is the least-damaging choice under the cost .
- The new key gives the new value exactly, but neighbouring inputs move a little; specificity is the test of that.
- Read next: ROME, which applies this to GPT-2 XL, uses causal tracing to choose the layer, and reports the benchmark results.
Check yourself
QuestionRecall: what does the matrix Delta = Lambda times u transpose look like, and why is it called rank one?Show answer
It is a column vector times a row vector, so every row is a multiple of the same row and every column is a multiple of . A matrix built from one column and one row has rank one, which means it changes the layer along a single output direction and a single input direction.
QuestionCompute: W = I (2 by 2), k_* = (2, 0), v_* = (2, 4), C = I. Find Lambda and the new matrix.Show answer
Old output: . Gap: . Direction: . Scale: . So . Update: . New matrix: . Check: .
QuestionWhy does the formula contain C inverse rather than just the key k_*?Show answer
Because the old memories care about some input directions much more than others. records how busy each direction is across the old keys. Multiplying by makes writing in a busy direction expensive, so the edit spends its change where the old memories are quiet. Without the formula ignores the old memories and can damage them more, as Example 3 shows.
QuestionApplied: after an edit, a paraphrase of the prompt gives the new answer, but an unrelated fact about a neighbouring subject has changed. Is the method broken?Show answer
Not necessarily. The edit changed one shared matrix, so inputs that share a direction with the new key can move. The weighting reduces this spillover but cannot remove it. The right response is to measure specificity on neighbouring subjects, check how large the spillover is, and consider fewer or more carefully chosen edits.
Sources
- Meng, Bau, Andonian, Belinkov (2022), Locating and Editing Factual Associations in GPT: https://arxiv.org/abs/2202.05262
- Published version of the same paper: https://doi.org/10.52202/068431-1262