From reading to intervening
The lenses and probes read activations; they cannot say whether the model uses what they read. Activation patching answers that by experiment. Run the model on two prompts that differ in one detail, take one activation from the first run, put it into the second, and see how much of the first run's answer comes back. If swapping in a single component's output restores the behaviour, that component carries the information that tells the prompts apart.
This page patches GPT-2 small on indirect object identification (IOI), the task Wang et al. reverse-engineered: in “When Mary and John went to the store, John gave a drink to”, the model should continue with “ Mary”, the name that is not repeated. The page uses one template, four name pairs, each in both orders (ABBA, with the indirect object named first, and BABA), eight prompts in all.
Clean, corrupted and the metric
Each clean prompt has a corrupted twin in which the subject's second mention (S2) is replaced by the indirect object's name, so the repeated name and the answer flip: “… Mary gave a drink to” should now end in “ John”. The Clean and corrupted panel shows both prompts and their top next tokens. The measure is the logit difference between the clean prompt's two names at the last position,
which averages +2.54 on the clean prompts and −3.18 on the corrupted ones. A patch runs the corrupted prompt with one clean activation in place and reports the fraction of the gap it closes:
0 means no effect and 1 means the clean answer is fully back. Patching clean into corrupted, as here, is called denoising: it finds components whose clean value is enough to restore the answer. The reverse, noising, finds components the clean run cannot do without; with redundant circuits the two can disagree, which is why Heimersheim and Nanda recommend saying which one was run.
Patching one position at a time
The Patching one position at a time grid patches the residual stream at one point and one position: row ℓ, column t is the corrupted run with the clean stream after block ℓ at token t only, the embeddings at the bottom. Averaged over the pairs of one order, the picture is two red bands. At S2, the only token that differs, the clean stream restores the whole answer from the embeddings up to block 7, part of it after block 8, and nothing from block 9 on. At the last token it does nothing up to block 7, restores part after block 8, and almost everything from block 9 on. Somewhere around blocks 8 and 9 the information that the subject was repeated is moved from S2 to the last token, where the prediction is made.
The same grid can patch each layer's attention output or MLP output instead of the whole stream. The strip's At the last token chart shows which layers matter there: the attention outputs of layers 7, 8 and 9 (counting from 0, as head names do) recover a third, a half and a quarter of the gap, those of layers 10 and 11 push it back the other way, and the MLPs hardly matter. Note the two numberings: the stream's “after block 8” is the output of layers 0 to 7.
Each head
The Each head, at every position map patches one head's output (its 64 columns of the attention output projection's input) at all positions at once, for all 144 heads, layers as rows and heads as columns; the selected head can then be split by position. Letters mark the classes of heads the IOI paper names, for comparison only: the map is the recording's own.
Averaged over all eight pairs, the eight largest effects are all heads the paper names. Heads 8.10, 8.6, 7.9 and 7.3, which the paper calls S-inhibition heads, each recover between 0.16 and 0.28 of the gap, together with the induction head 5.5 (0.28) and the name mover 9.9 (0.25). Two heads go the other way: 10.7 (−0.40) and 11.10 (−0.19), the paper's negative name movers, which push against the answer, so restoring their clean output moves the corrupted run even further from the clean answer. The paper's account fits this map: earlier heads detect that S is duplicated, S-inhibition heads in layers 7 and 8 carry that to the last token, and name movers in layers 9 and 10 copy the name that was not inhibited.
What patching measures
Patching measures a total effect: the clean activation changes everything downstream of it, including other heads that react. That is its strength, since it tests the model as it really runs, and its difficulty, since effects need not add up and a component can look unimportant because others compensate for it. The results also depend on choices Zhang and Nanda showed to matter: replacing a name with another name, as here, rather than adding noise to the input, and measuring a logit difference rather than a probability. The page's own checks confirm the plumbing: patching the corrupted run with its own values changes nothing, and patching every clean embedding, or the whole clean stream at the last token, gives the clean logit difference back exactly.
The Attribution tab measures the other half: each head's direct effect on the logits in a single clean run, without any intervention.
Where it came from
Interventions on intermediate variables come from causal mediation analysis, where Pearl (2001) defined direct and indirect effects. Vig et al. (2020) brought them to neural language models, swapping neurons and attention heads between prompts to locate gender bias. Meng et al. (2022) used causal tracing, restoring clean states into a run whose subject had been corrupted with noise, to locate factual recall in GPT's middle MLP layers; Wang et al. (2022) used patching to map a full circuit of 26 heads in 7 classes for IOI in GPT-2 small; and Geiger et al. framed such interchange interventions as tests of a causal abstraction of the network. Later guides by Zhang and Nanda and by Heimersheim and Nanda set out how to choose corruptions and metrics.
Papers
- Causal Mediation Analysis for Interpreting Neural NLP: The Case of Gender Bias Introduces interventions on a language model's neurons and attention heads, read as mediators of an effect, the method activation patching grew from.
- Interpretability in the Wild: a Circuit for Indirect Object Identification in GPT-2 small The IOI task, its circuit of 26 heads in 7 classes found largely by patching, and the head classes the map marks.
- Locating and Editing Factual Associations in GPT Causal tracing: restoring clean hidden states into a corrupted run, layer by layer and token by token, to locate where facts are recalled.
- Towards Best Practices of Activation Patching in Language Models: Metrics and Methods Shows that the corruption method and the metric change patching results, and argues for name replacement and logit differences.
- How to use and interpret activation patching Practical advice: denoising against noising, what patching results do and do not show about circuits, and metric pitfalls.
- Causal Abstractions of Neural Networks Interchange interventions as a test of whether a network implements a hypothesised high-level causal model.
- Direct and Indirect Effects Defines direct and indirect effects of one variable on another through mediators, the causal notions patching estimates.
- Language Models are Unsupervised Multitask Learners GPT-2, whose small model this page patches.