Search

Search papers and lessons

Causal Tracing

Level 4 · 13 min read

Causal tracing finds which hidden states inside a network carry an answer by copying clean activations into a corrupted run, one site at a time, and measuring how much the answer comes back.

On this page

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

TL;DR

Causal tracing finds the parts of a neural network that carry a specific answer. You run the model on a prompt it gets right, and on a corrupted version it gets wrong. Then you copy one internal state at a time from the good run into the bad run and measure how much of the right answer comes back. The states whose copy restores the answer are the ones carrying the information.

Builds on: Transformer, Mediation Analysis, Counterfactual Reasoning, Residual Connection · Leads to: ROME, Rank-One Model Editing

Where it's used

Locating a fact inside a language model. Suppose a GPT-style model answers "Paris" to "The Eiffel Tower is in". Where is that fact stored? Meng and colleagues (2022) applied causal tracing to models of this kind and found that the effect of a corrupted subject name was concentrated at the last token of the subject, in a band of middle layers. That location is what made it possible to edit the fact directly, which is the subject of ROME.

Finding the circuit behind a behaviour. In indirect object identification (Wang and colleagues, 2022), a model reads a sentence such as "When Mary and John went to the store, John gave a drink to" and should continue with "Mary". Researchers used the same copy-and-measure idea to find which attention heads move the name "Mary" to the final position. Each head they named was confirmed by patching it and watching the answer change.

Checking whether a model relies on a cue. A team worried that a model's answer depends on a biased cue, such as a name that signals gender. They can corrupt only that cue and measure how far the answer moves. Causal tracing then names the layers and token positions that carry the effect, so the team can inspect those places instead of guessing from the output.

How it works

The numbers below are invented for illustration. They are not measurements from any paper.

Set-up: the prompt is "The Space Needle is in", tokens "The", "Space", "Needle", "is", "in". The correct answer is "Seattle". The hidden state of a token after layer l is the vector the model holds at that position between layers. Layer 0 means the embeddings. The toy model has four layers after that, so layer 4 is the last one.

  1. Clean run. Feed the good prompt to the model. It gives probability 0.80 to "Seattle", read from the final Softmax over the vocabulary. Save every hidden state for every token and every layer. Check: the clean answer probability is 0.80.
  2. Corrupted run. Feed a version where the subject tokens "Space" and "Needle" get noisy embeddings. The noise is random numbers drawn from a Gaussian, large enough to confuse the model. The probability of "Seattle" falls to 0.10. Check: the total effect to explain is 0.80 minus 0.10, which is 0.70.
  3. Patch one site. Choose the hidden state of the token "Needle" after layer 2. During the corrupted run, overwrite that one vector with its saved clean value and let the rest of the run continue unchanged. The probability of "Seattle" rises to 0.45. Check: the patch recovers 0.45 minus 0.10, which is 0.35.
  4. Read the effect. The 0.35 is the indirect effect of that site: how much of the total effect runs through that one hidden state. Since 0.35 is half of 0.70, this site carries half of the answer's dependence on the subject, on this toy model.
  5. Sweep. Repeat step 3 for every token and every layer. Each patch is one extra forward pass. For the token "Needle" the probability at each layer came out as in the plot below. The peak is at layer 2. After the last layer the patch does nothing for a token that is not the last one, because the next word is read only from the state of the last token.
  6. Map. Put the effects into a grid of token by layer. Bright cells mark where the answer's information is carried.
flowchart LR
  A["Clean run: save every hidden state"] --> C["Patch one (token, layer) site into the corrupted run"]
  B["Corrupted run: add noise to subject embeddings"] --> C
  C --> D["Measure P(Seattle) and subtract the corrupted value"]
  D --> E{"More sites?"}
  E -- yes --> C
  E -- no --> F["Grid of indirect effects over tokens and layers"]
00.511.522.533.5400.20.40.60.81layerP(Seattle) after patch
  • patch the token Needle
Toy numbers: restoring the clean state of the token Needle helps most at layer 2. Restoring it after the last layer changes nothing.

The math

Definition: Causal tracing effects

Let p(y∣x)p(y \mid x) be the probability the model gives to the correct answer yy for input xx. Let xcleanx_{\text{clean}} be the good prompt and xcorrx_{\text{corr}} the corrupted one. Write ht(l)(x)h^{(l)}_t(x) for the hidden state at token position tt after layer ll, computed on input xx. Let do(ht(l):=v)\text{do}\big(h^{(l)}_t := v\big) mean that we run the corrupted input but force that one hidden state to the value vv, leaving every other state to be computed normally. The total effect and the indirect effect of the site (t,l)(t, l) are

TE=p(y∣xclean)−p(y∣xcorr),IE(t,l)=p(y∣xcorr, do(ht(l):=ht(l)(xclean)))−p(y∣xcorr).\text{TE} = p(y \mid x_{\text{clean}}) - p(y \mid x_{\text{corr}}), \qquad \text{IE}(t, l) = p\big(y \mid x_{\text{corr}},\ \text{do}(h^{(l)}_t := h^{(l)}_t(x_{\text{clean}}))\big) - p(y \mid x_{\text{corr}}).

The share of the effect carried by the site is IE(t,l)/TE\text{IE}(t, l) / \text{TE}.

Two lines of the definition are worth reading slowly. The first is the total effect: it is how much the corruption moved the answer. The second is the indirect effect: it is how much the answer moves back when one state is restored and everything else stays corrupted. This is the same idea as the indirect effect, with the hidden state playing the role of the mediator.

Step 1 in symbols: the clean run

Store ht(l)(xclean)h^{(l)}_t(x_{\text{clean}}) for every token tt and layer ll. In the toy model, p(Seattle∣xclean)=0.80p(\text{Seattle} \mid x_{\text{clean}}) = 0.80.

Step 2 in symbols: the corruption

The corrupted input is xcorrx_{\text{corr}}, made by adding noise ε∼N(0,σ2I)\varepsilon \sim \mathcal{N}(0, \sigma^2 I) to the embeddings of the subject tokens. Here σ\sigma is the noise scale, N\mathcal{N} is the Gaussian distribution and II is the identity matrix, so each embedding number gets independent noise. In the toy model p(Seattle∣xcorr)=0.10p(\text{Seattle} \mid x_{\text{corr}}) = 0.10, so

TE=0.80−0.10=0.70.\text{TE} = 0.80 - 0.10 = 0.70.

Step 3 and 4 in symbols: patch and read the indirect effect

Patching the token "Needle" at layer 2 gives p=0.45p = 0.45. So

IE(Needle,2)=0.45−0.10=0.35,IE(Needle,2)TE=0.350.70=0.5.\text{IE}(\text{Needle}, 2) = 0.45 - 0.10 = 0.35, \qquad \frac{\text{IE}(\text{Needle}, 2)}{\text{TE}} = \frac{0.35}{0.70} = 0.5.

How the residual stream explains the patch

In a standard transformer block the hidden state is a running sum over residual connections:

ht(l)=ht(l−1)+at(l)+mt(l),h^{(l)}_t = h^{(l-1)}_t + a^{(l)}_t + m^{(l)}_t,

where at(l)a^{(l)}_t is the output of attention at layer ll and mt(l)m^{(l)}_t is the output of the feedforward (MLP) sublayer. So patching ht(l)h^{(l)}_t means handing the later layers everything the token has accumulated up to that layer. It does not patch the attention or MLP outputs separately; that finer cut is a different experiment.

Worked examples

Example
1. Tiny: the share carried by one site

Use the toy numbers from the walkthrough.

  1. The clean answer probability is 0.80. The corrupted answer probability is 0.10.
  2. Total effect: 0.80 minus 0.10 equals 0.70.
  3. Patch the token "Needle" at layer 2. The answer probability rises to 0.45.
  4. Indirect effect of that site: 0.45 minus 0.10 equals 0.35.
  5. Share: 0.35 divided by 0.70 equals 0.5.

Interpretation: half of the drop in the answer's probability can be undone by restoring one hidden state at one token and one layer.

Example
2. Standard: the last token at a late layer

Now patch the final token "in" after layer 4 instead. Suppose the answer probability comes back to 0.75.

  1. Total effect is still 0.70, because the corruption and the clean run are unchanged.
  2. Indirect effect of this site: 0.75 minus 0.10 equals 0.65.
  3. Share: 0.65 divided by 0.70 is about 0.93.
  4. Compare with the subject site from Example 1, which had share 0.5.

Interpretation: at the last token, late in the network, almost the whole effect is present. The shares are not additive across sites. The subject site and the last-token site both carry overlapping information, so 0.5 plus 0.93 does not mean the effects sum to 1.43.

Example
3. Realistic: a fact in a larger model

These numbers are hypothetical, shaped like the factual-recall setting in ROME. A 12-layer model reads "The Eiffel Tower is in" and should answer "Paris".

  1. Clean probability of "Paris" is 0.90. Corrupting the subject "Eiffel Tower" with noise drops it to 0.05.
  2. Total effect: 0.90 minus 0.05 equals 0.85.
  3. Patch the last subject token "Tower" at layer 6. The probability rises to 0.70.
  4. Indirect effect: 0.70 minus 0.05 equals 0.65. Share: 0.65 divided by 0.85 is about 0.76.
  5. Patch the same token at layer 1. The probability rises only to 0.08.
  6. Indirect effect: 0.08 minus 0.05 equals 0.03. Share: 0.03 divided by 0.85 is about 0.04.

Interpretation: the middle layer at the last subject token carries most of the effect, and the early layer carries almost none. That is the pattern that makes a middle layer a candidate place to edit the fact.

Try it: the clean probability is 0.90, the corrupted probability is 0.05, and patching a site gives 0.45. What is the share of the total effect carried by that site? Answer: total effect is 0.90 minus 0.05, which is 0.85. Indirect effect is 0.45 minus 0.05, which is 0.40. Share is 0.40 divided by 0.85, which is about 0.47.

Common mistakes and tradeoffs

It is tempting to think that a large effect at one site means that site stores the fact. In fact it means the answer's information passes through that site in this run. The same information may have been written there by earlier layers or by other tokens. Changing the corruption can move the map, because each corruption asks a slightly different question.

It is tempting to think the corruption choice does not matter. In fact the noise scale and the tokens you corrupt decide what the map means. Corrupt the subject and you learn where the subject's information flows; corrupt something else and you learn something else.

It is tempting to think the indirect effects of all sites add up to the total. In fact the paths overlap, so they do not sum. Read each site on its own, and treat the map as a set of separate measurements.

It is tempting to think the last layer is the most important. In fact the final layers matter at the last token, but a subject token's state after the last layer has no effect on the prediction, because only the last token's state is read out, as the toy numbers show.

When to use it. Use causal tracing when you have a natural pair of inputs, one that works and one that is corrupted in a controlled way, and you want to know where a specific piece of information lives. It is an intervention, so it gives a causal claim about the model rather than a correlation.

When not to use it. Do not use it when no clean counterfactual exists, or when the behaviour is spread across many components with backup paths. A redundant network can hide an effect: patching one path does nothing because another path takes over. Each site costs one forward pass, so a full sweep over a large model with long prompts is expensive.

Alternatives. Probing trains a small classifier on hidden states. It is cheap but only shows correlation, since a probe can read information the model does not use. Ablation sets a state to zero or to a mean instead of a clean value; it measures necessity rather than sufficiency. Attribution patching approximates each patch with a gradient, which is much faster and less exact. Path patching restricts the intervention to one specific route between components.

History

Causal mediation was developed in statistics and the social sciences to split a total effect into direct and indirect parts (see Mediation Analysis and Causal Inference). People working on neural networks were stuck on a different question: a network is a large tangle of numbers, and looking at activations or attention maps gave pictures but not measured claims.

In 2020 Vig and colleagues applied causal mediation analysis to neural language models, intervening on attention heads and neurons to study gender bias. That showed how to treat a model component as a mediator.

In 2022 Meng and colleagues used the same kind of tracing on a GPT-style model to locate factual associations, and then edited one of them directly in ROME. Later in 2022 Wang and colleagues traced indirect object identification to a set of attention heads. The method caught on because it returns a number for each component, which can be checked by changing the component again.

Recap

  • A clean run and a corrupted run give two answers. The gap between them is the total effect.
  • Copying one hidden state from the clean run into the corrupted run, and measuring the answer, gives the indirect effect of that site.
  • The hidden state is a running sum of attention and MLP outputs, so a patch hands later layers everything accumulated so far.
  • Sweeping over tokens and layers gives a map of where information is carried. Shares do not add across overlapping sites.
  • The map identifies candidate locations, such as a middle layer at the last subject token, which is what makes Rank-One Model Editing and ROME possible.

Read next: ROME for the first use of this map to edit a fact, and Rank-One Model Editing for the editing method that follows from it.

Check yourself

QuestionRecall: what is the difference between the clean run and the corrupted run?Show answer

The clean run uses the good prompt, which the model answers correctly. The corrupted run uses a version where part of the input, here the subject tokens, has been changed with noise, so the model answers worse. Both are needed: the gap between their answers is the total effect that causal tracing tries to explain.

QuestionCompute: the clean probability is 0.75, the corrupted probability is 0.15, and patching one site gives 0.39. What are the indirect effect and the share of the total effect?Show answer

Total effect: 0.75 minus 0.15 equals 0.60. Indirect effect: 0.39 minus 0.15 equals 0.24. Share: 0.24 divided by 0.60 equals 0.4, so 40 percent of the total effect runs through that site.

QuestionWhy: why patch a single site at a time instead of copying the entire clean run into the corrupted run?Show answer

If you copy the whole clean run, the answer comes back trivially, and you learn nothing about where the information lives. Patching one site isolates how much that one state carries. The sweep over sites then compares sites against each other.

QuestionApplied: a team patches the token Tower at layer 1 and sees almost no change. Does that mean the model does not use that token?Show answer

No. It means that restoring the clean state of that token at that layer, alone, does not bring back the answer. Later layers or other tokens may carry the information, and the model may also use the token in ways that a single patch cannot reveal. A null result for one site is a statement about that site only.

Sources