Skip to content

Activation patching: causal experiments inside a network

Swap one internal activation from a clean run into a corrupted run and measure how much behavior comes back. Learn the recovery score, the design choices that dominate it, and why restoring a behavior doesn't prove you found the mechanism.

Advanced ~13 min8 steps

What runs in CodeSplash

You will be able to

  • Compute a patching effect and its normalized recovery
  • Explain how the corruption, the patch location and the metric shape the result
  • Distinguish sufficiency, necessity and mediation, and why single-component rankings miss interactions

Read first

01

From correlation to intervention

Read

Attributions and attention describe a model from its gradients or weights. Mechanistic interpretability runs experiments instead: change something inside the network and watch the output. Activation patchingActivation patchingCopies an internal activation from one run into another and measures how much of the output's difference it restores. (also called causal tracing) is the basic experiment.

Run the model twice: on a clean input that produces the behavior (the right answer to a fact question) and on a corrupted one that doesn't (the subject's name scrambled). Then rerun the corrupted input, but replace one internal activation, at a chosen layer, position or head, with its value from the clean run, and let the rest of the network compute forward.

Reference · EXP014 · Explained hereActivation patching and causal tracingProbe model computations by replacing selected activations in a corrupted or contrasting run with activations from a reference run and measuring a target behavior.
02

The recovery score

Read

With a behavior score ss (say the logit of the right answer),

Δpatch=s(xcorrupt; hS←hSclean)−s(xcorrupt),r=Δpatchs(xclean)−s(xcorrupt).\Delta_{\text{patch}}=s\big(x_{\text{corrupt}};\,h_S\leftarrow h_S^{\text{clean}}\big)-s(x_{\text{corrupt}}),\qquad r=\frac{\Delta_{\text{patch}}}{s(x_{\text{clean}})-s(x_{\text{corrupt}})} .

r=1r=1 means the patched component alone restores the clean behavior; r=0r=0 means it restores nothing. Report the raw change Δ\Delta beside rr: a recovery of one half of a tiny gap means little.

03

By hand: an interaction

Read

The reference's toy has two hidden units and the score s(h)=tanh⁡(h1)+h1h2s(h)=\tanh(h_1)+h_1h_2. The clean activations are (2,1)(2,1), the corrupted ones (−0.5, 0.2)(-0.5,\,0.2). Patching only h1h_1 with its clean value raises the score by 1.93, a recovery of 0.55.

Why not all of it? The score multiplies h1h_1 by h2h_2, and h2h_2 is still corrupted. The two units interact: patching each alone and adding the recoveries wouldn't give the effect of patching both. Single-component rankings miss exactly this.

Patch each hidden unit of a hand-wired network in both directions, then add a backup unit.

DExplorable · Activation patching: which units carry the answer?

Swap one hidden unit's activation between a clean and a corrupted run. How much of the answer moves with it, and what can that miss?

Patch h1 alone, clean into corrupted. How much of the logit gap comes back?

How sure?
1.5

The clean and corrupted inputs differ only in x₁ (think: the subject of a prompt).

−0.5

The corrupted run's x₁; everything else is shared.

0.4

The same in both runs, so units that read only x₂ shouldn't matter.

Patch

Clean into corrupted asks whether a unit is enough to restore the answer; corrupted into clean asks whether it is needed.

h₃ copies h₁ and the output takes whichever is larger, so either one alone carries the signal.

x₁ (differs)x₂ (shared)100%h₁ (reads x₁)0%h₂ (reads x₂)0%h₃ (backup of h₁ when on)0%h₄ (reads x₂, inhibits)logit6.35 clean · 1.35 corrupted

h₁

restores 100%

destroys 100%

h₂

restores 0%

destroys 0%

h₃

restores 0%

destroys 0%

h₄

restores 0%

destroys 0%

Each unit is shaded by the share of the clean–corrupted logit gap its patch moves in the chosen direction. "Restores" is the clean value patched into the corrupted run; "destroys" is the corrupted value patched into the clean run.

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

04

Predict: a big recovery

Predict

Predict first

Patching one layer's activation at the subject's position restores 90% of the right answer's logit. Have you found the layer that does the task?

Yes: that layer is where the model does the taskNo: it carries enough to restore the behavior, but other paths may do it too
Reveal

No. You've shown that this activation is sufficient, under this corruption, to carry the information. Redundant paths may do the same; patching elsewhere might also restore it; and a different corruption might give another picture. Necessity needs the reverse experiment: corrupt the component in a clean run.

05

Design choices that dominate

Read
  • The corruption. Noise added to embeddings, a swapped name and a random token test different things; corruptions can create internal states the model never sees.
  • Direction. Clean-into-corrupt tests sufficiency; corrupt-into-clean tests necessity. Mediation through a path needs patching edges, not whole states.
  • The metric. A logit difference, a probability or a loss can rank components differently.
  • Controls. Check that patching also leaves unrelated outputs alone, and try combinations of components.

Patching activations isn't editing weights: a patch that restores a fact says nothing about whether a weight edit would change it reliably.

06

Where it stands in CodeSplash

Read
07

Check your understanding

Checkpoint

Check yourself

The clean score is 5 and the corrupted score is 1. Patching one component into the corrupted run gives a score of 2. What is the normalized recovery rr?

Show the answer

Answer: 0.25

r=(2−1)/(5−1)=0.25r=(2-1)/(5-1)=0.25: the component restores a quarter of the gap.

Check yourself

Patching unit A alone recovers 40% and unit B alone 30%. Will patching both recover 70%?

Show the answer

Answer: b

Nonlinear interactions make single-component effects non-additive. Test combinations before ranking components.

Check yourself

Two studies patch the same model and disagree on which layer stores a fact. One corrupted the input with Gaussian noise on embeddings, the other by swapping the subject's name. What's the likely reason?

Show the answer

Answer: b

Report the corruption, direction, location and metric; conclusions depend on all four.

08

Recap

Recap
  • Patching copies a clean activation into a corrupted run; recovery rr is the share of the gap restored.
  • Report raw changes, the corruption, the direction and the metric.
  • Interactions make single-component effects non-additive.
  • Restoring a behavior shows a sufficient pathway, not the unique mechanism; explained here.

Concepts in this lesson