Search

Search papers and lessons

Rank-One Model Editing

Level 5 · 13 min read

Rank-one model editing changes one stored fact in a transformer by adding a rank-one matrix to one feedforward weight matrix, using a closed-form formula instead of retraining.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Discrete math
  • Probability
  • Neural network basics
  • Architectures
  • ReadReady to readNot yet

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 W=IW = I, the 2 by 2 identity, so the layer returns its input unchanged. The new fact must map the key k∗=(1,0)k_* = (1, 0) to the value v∗=(3,1)v_* = (3, 1). Assume the old keys have covariance C=IC = I, so every input direction is equally busy.

  1. Start with the old matrix. Feed the key through the layer: Wk∗=(1,0)W k_* = (1, 0). The layer currently answers with (1,0)(1, 0). Check: this is not the value we want, (3,1)(3, 1).
  2. Measure the gap. Subtract: r=v∗−Wk∗=(3,1)−(1,0)=(2,1)r = v_* - W k_* = (3, 1) - (1, 0) = (2, 1). This is the amount the output must still move. Check: Wk∗+r=(3,1)W k_* + r = (3, 1).
  3. Pick the direction to write in. Compute u=C−1k∗=(1,0)u = C^{-1} k_* = (1, 0). The matrix CC records how busy each input direction is for the old keys. Multiplying by C−1C^{-1} makes busy directions expensive to write in. Here C=IC = I, so u=k∗u = k_*. Check: nothing is scaled, so u=(1,0)u = (1, 0).
  4. Scale so the key hits the value. Compute the number s=u⊤k∗=1s = u^\top k_* = 1, then Λ=r/s=(2,1)\Lambda = r / s = (2, 1). Check: Λ s=(2,1)\Lambda \, s = (2, 1), which is exactly rr.
  5. Write the rank-one update. Form Δ=Λu⊤=(2010)\Delta = \Lambda u^\top = \begin{pmatrix} 2 & 0 \\ 1 & 0 \end{pmatrix}. Every row is a multiple of u⊤=(1,0)u^\top = (1, 0), so the update changes the matrix along one output direction and one input direction.
  6. Check the edit. The new matrix is W^=W+Δ=(3011)\hat W = W + \Delta = \begin{pmatrix} 3 & 0 \\ 1 & 1 \end{pmatrix}. Then W^k∗=(3,1)=v∗\hat W k_* = (3, 1) = v_*. The fact is inserted.

Only step 3 looks at the old memories, through CC. 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

Definition: Rank-one model edit

Let W∈Rm×nW \in \mathbb{R}^{m \times n} be a weight matrix, k∗∈Rnk_* \in \mathbb{R}^{n} a key, and v∗∈Rmv_* \in \mathbb{R}^{m} a value. Let K∈Rn×NK \in \mathbb{R}^{n \times N} hold NN old keys as columns and C=KK⊤C = KK^\top be their (uncentered) covariance. The rank-one edit is

W^=W+Λu⊤,u=C−1k∗,Λ=v∗−Wk∗u⊤k∗,\hat W = W + \Lambda u^\top, \qquad u = C^{-1} k_*, \qquad \Lambda = \frac{v_* - W k_*}{u^\top k_*},

and it satisfies W^k∗=v∗\hat W k_* = v_*.

In words: WW is the layer's matrix, k∗k_* is the key for the new fact, v∗v_* is the value it should produce, CC is the matrix of the old keys, uu is the key after the C−1C^{-1} reweighting, and Λ\Lambda is a column vector that sets the size of the change. The product Λu⊤\Lambda u^\top is an outer product, so it has rank one (the rank is the number of independent columns, and every column is a multiple of Λ\Lambda).

Step 1 in symbols: the old matrix

The layer maps any key kk to the output WkWk. Old facts satisfy WK≈VWK \approx V, where VV stacks their values. In the walkthrough W=IW = I and k∗=(1,0)k_* = (1, 0), so Wk∗=(1,0)W k_* = (1, 0).

Step 2 in symbols: the residual

r=v∗−Wk∗r = v_* - W k_*. In the walkthrough, r=(3,1)−(1,0)=(2,1)r = (3, 1) - (1, 0) = (2, 1).

Step 3 in symbols: the direction

u=C−1k∗u = C^{-1} k_*. The inverse exists when the old keys span every input direction, which is why the paper estimates CC from many text samples rather than a few.

In the walkthrough C=IC = I, so u=k∗=(1,0)u = k_* = (1, 0).

Step 4 in symbols: the scale

s=u⊤k∗s = u^\top k_* is a single number. Then Λ=r/s\Lambda = r / s. In the walkthrough s=1s = 1 and Λ=(2,1)\Lambda = (2, 1). Check: Λu⊤k∗=Λs=r\Lambda u^\top k_* = \Lambda s = r.

Step 5 and 6 in symbols: the update and the check

Δ=Λu⊤\Delta = \Lambda u^\top and W^=W+Δ\hat W = W + \Delta. Applying the new matrix to the key:

W^k∗=Wk∗+Λ(u⊤k∗)=Wk∗+Λs=Wk∗+r=v∗.\hat W k_* = W k_* + \Lambda (u^\top k_*) = W k_* + \Lambda s = W k_* + r = v_*.

Why this update and not another

Other choices of Δ\Delta also satisfy Δk∗=r\Delta k_* = r, so the constraint alone does not decide. The old memories decide. Their damage is measured by ∥ΔK∥F2\lVert \Delta K \rVert_F^2, the squared size of the change in the old outputs, which equals tr⁡(ΔCΔ⊤)\operatorname{tr}(\Delta C \Delta^\top). The edit therefore solves: minimise tr⁡(ΔCΔ⊤)\operatorname{tr}(\Delta C \Delta^\top) subject to Δk∗=r\Delta k_* = r. Setting the gradient of the Lagrangian to zero gives Δ=Λ u⊤\Delta = \Lambda\, u^\top for some vector Λ\Lambda, and the constraint Δk∗=r\Delta k_* = r fixes Λ=r/(u⊤k∗)\Lambda = r / (u^\top k_*). That is the formula above. The paper derives it in its appendix.

One property follows at once: if CC is multiplied by any positive number aa, then uu becomes u/au / a, ss becomes s/as / a, and Λ\Lambda becomes aΛa \Lambda, so Δ\Delta does not change. Only the shape of CC matters.

Worked examples

Example
1. Tiny: a non-identity matrix with C = I

Set-up: W=(2101)W = \begin{pmatrix} 2 & 1 \\ 0 & 1 \end{pmatrix}, k∗=(1,1)k_* = (1, 1), v∗=(5,0)v_* = (5, 0), and C=IC = I.

  1. Old output: Wk∗=(2⋅1+1⋅1, 0⋅1+1⋅1)=(3,1)W k_* = (2 \cdot 1 + 1 \cdot 1,\ 0 \cdot 1 + 1 \cdot 1) = (3, 1).
  2. Gap: r=v∗−Wk∗=(5−3, 0−1)=(2,−1)r = v_* - W k_* = (5 - 3,\ 0 - 1) = (2, -1).
  3. Direction: u=C−1k∗=k∗=(1,1)u = C^{-1} k_* = k_* = (1, 1).
  4. Scale: s=u⊤k∗=1+1=2s = u^\top k_* = 1 + 1 = 2.
  5. Column: Λ=r/s=(1,−0.5)\Lambda = r / s = (1, -0.5).
  6. Update: Δ=Λu⊤=(11−0.5−0.5)\Delta = \Lambda u^\top = \begin{pmatrix} 1 & 1 \\ -0.5 & -0.5 \end{pmatrix}. Both rows are multiples of (1,1)(1, 1), so the rank is one.
  7. New matrix: W^=W+Δ=(32−0.50.5)\hat W = W + \Delta = \begin{pmatrix} 3 & 2 \\ -0.5 & 0.5 \end{pmatrix}.
  8. Check: W^k∗=(3+2, −0.5+0.5)=(5,0)=v∗\hat W k_* = (3 + 2,\ -0.5 + 0.5) = (5, 0) = v_*.

Interpretation: a 2 by 2 matrix was changed by a single outer product, and the key now produces the requested value.

Example
2. Standard: correlated old keys, C = diag(1, 4)

Set-up: W=IW = I (2 by 2), k∗=(1,1)k_* = (1, 1), v∗=(4,2)v_* = (4, 2). The old keys have covariance C=(1004)C = \begin{pmatrix} 1 & 0 \\ 0 & 4 \end{pmatrix}, so the second input direction is busy (variance 4) and the first is quiet (variance 1).

  1. Old output: Wk∗=(1,1)W k_* = (1, 1).
  2. Gap: r=(4−1, 2−1)=(3,1)r = (4 - 1,\ 2 - 1) = (3, 1).
  3. Direction: C−1=(1000.25)C^{-1} = \begin{pmatrix} 1 & 0 \\ 0 & 0.25 \end{pmatrix}, so u=C−1k∗=(1,0.25)u = C^{-1} k_* = (1, 0.25).
  4. Scale: s=u⊤k∗=1⋅1+0.25⋅1=1.25s = u^\top k_* = 1 \cdot 1 + 0.25 \cdot 1 = 1.25.
  5. Column: Λ=(3,1)/1.25=(2.4,0.8)\Lambda = (3, 1) / 1.25 = (2.4, 0.8).
  6. Update: Δ=Λu⊤=(2.40.60.80.2)\Delta = \Lambda u^\top = \begin{pmatrix} 2.4 & 0.6 \\ 0.8 & 0.2 \end{pmatrix}. Check: 2.4⋅0.25=0.62.4 \cdot 0.25 = 0.6 and 0.8⋅0.25=0.20.8 \cdot 0.25 = 0.2.
  7. New matrix: W^=(3.40.60.81.2)\hat W = \begin{pmatrix} 3.4 & 0.6 \\ 0.8 & 1.2 \end{pmatrix}.
  8. Check: W^k∗=(3.4+0.6, 0.8+1.2)=(4,2)=v∗\hat W k_* = (3.4 + 0.6,\ 0.8 + 1.2) = (4, 2) = v_*.

Interpretation: the first column of Δ\Delta (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.

Example
3. Realistic: what a neighbouring memory sees

Set-up: the same WW, CC, k∗k_* and v∗v_* 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 kn=(0,1)k_n = (0, 1) whose output before the edit is Wkn=(0,1)W k_n = (0, 1).

  1. Before the edit, the neighbour outputs (0,1)(0, 1).
  2. After the edit, the neighbour outputs W^kn=(0.6,1.2)\hat W k_n = (0.6, 1.2), the second column of W^\hat W. The change is (0.6,0.2)(0.6, 0.2), of length 0.36+0.04≈0.63\sqrt{0.36 + 0.04} \approx 0.63.
  3. A naive edit adds the gap along the key without using CC: Δ′=rk∗⊤/(k∗⊤k∗)=(1.51.50.50.5)\Delta' = r k_*^\top / (k_*^\top k_*) = \begin{pmatrix} 1.5 & 1.5 \\ 0.5 & 0.5 \end{pmatrix}. Check: Δ′k∗=(3,1)=r\Delta' k_* = (3, 1) = r.
  4. 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 0.4\sqrt{0.4}? Work it out before reading on.

Answer: Δ′kn=(1.5,0.5)\Delta' k_n = (1.5, 0.5), of length 2.5≈1.58\sqrt{2.5} \approx 1.58. That is 2.5 times the rank-one change. The neighbour's key lies in the busy direction (variance 4), and the CC 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 uu moves a little, including paraphrases of the prompt and neighbouring subjects. The CC 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 v∗v_* 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 k∗k_* and value v∗v_*, compute the gap rr, the reweighted direction u=C−1k∗u = C^{-1} k_*, and the scale Λ=r/(u⊤k∗)\Lambda = r / (u^\top k_*).
  • The update Λu⊤\Lambda u^\top has rank one and is the least-damaging choice under the cost tr⁡(ΔCΔ⊤)\operatorname{tr}(\Delta C \Delta^\top).
  • 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 u⊤u^\top and every column is a multiple of Λ\Lambda. 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: Wk∗=(2,0)W k_* = (2, 0). Gap: r=(0,4)r = (0, 4). Direction: u=k∗=(2,0)u = k_* = (2, 0). Scale: s=u⊤k∗=4s = u^\top k_* = 4. So Λ=(0,4)/4=(0,1)\Lambda = (0, 4) / 4 = (0, 1). Update: Δ=Λu⊤=(0020)\Delta = \Lambda u^\top = \begin{pmatrix} 0 & 0 \\ 2 & 0 \end{pmatrix}. New matrix: W^=(1021)\hat W = \begin{pmatrix} 1 & 0 \\ 2 & 1 \end{pmatrix}. Check: W^k∗=(2,4)=v∗\hat W k_* = (2, 4) = v_*.

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. CC records how busy each direction is across the old keys. Multiplying by C−1C^{-1} makes writing in a busy direction expensive, so the edit spends its change where the old memories are quiet. Without CC 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 CC 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