TL;DR
Attribution patching estimates how much each hidden state contributes to an answer without running a separate forward pass for every state. It takes the difference between the clean and corrupted values of a state, multiplies it by the gradient of the answer's probability at that state, and treats the product as the effect of patching. With one forward pass, one backward pass and a dot product per site, it gives a rough map of the whole network. Its numbers are approximations, so the best candidates still need a real patch to confirm them.
Builds on: Causal Tracing, Taylor Series, Residual Stream · Leads to: Mechanistic Interpretability experiments, where it serves as a screening step.
Where it's used
Screening a large model for circuits. A circuit is a small set of components that together perform one behaviour. Finding one by causal tracing means one forward pass per site, so a model with thousands of heads, layers and token positions needs thousands of runs for one prompt. Attribution patching scores all sites in about two passes, and researchers then confirm the top scores with real patches.
Ranking attention heads for a behaviour. In a task such as indirect object identification, a team wants to know which attention heads move a name to the final position. A quick attribution score for every head shows which dozen deserve a careful test. The confirmed heads are then the ones reported.
Checking a fact-editing location. Before editing a stored fact, a team can screen every layer at the last subject token with attribution patching, then run exact causal tracing only on the few layers that score high. The estimate saves time, and the exact check protects the edit from a bad choice.
How it works
The numbers below are invented for illustration. They are not measurements from any paper. We reuse the toy prompt from Causal Tracing: "The Space Needle is in", where the correct answer is "Seattle". The metric is the probability of "Seattle". In the clean run . In the corrupted run, where the subject embeddings are noised, .
- Clean run. Run the good prompt once and save the hidden state of every token at every layer. Check: this is one forward pass, and it gives .
- Corrupted run. Run the corrupted prompt once and save its hidden states. Check: , so the total effect to explain is .
- Backward pass. From the corrupted run, compute the gradient of with respect to every saved hidden state at once. One backward sweep, the same reverse pass used to train networks, gives all of them. Check: one backward pass covers every site, so the cost does not grow with the number of sites.
- Difference per site. For each site, subtract the corrupted vector from the clean one. For the token "Needle" after layer 2 this gives a vector . Check: is read from the two saved runs, so no new run is needed.
- Dot product. Multiply the gradient by the difference, which is a dot product of two vectors. For the site "Needle" after layer 2 this gives an estimate of 0.30. Check: the estimate is a single number per site.
- Compare with the exact patch. Causal tracing gave 0.35 for this site, which costs one forward pass. The estimate is 0.30, an error of 0.05. Check: the error is small enough to rank sites, which is what screening needs.
- Verify the top sites. Sort all sites by estimate, then run real patches on the top few. Check: only the verified sites go into the final claim.
flowchart LR A["Clean run: save every hidden state"] --> D["Difference d per site"] B["Corrupted run: save states and metric"] --> C["One backward pass: gradient per site"] C --> E["Dot product: gradient times d"] D --> E E --> F["Ranked estimates"] F --> G["Real patches on the top sites"]
- exact patch (causal tracing)
- gradient estimate
The math
Let be the metric, here , the probability of the correct answer. For a site with hidden state , let be its value in the clean run and its value in the corrupted run. Define the difference and the gradient evaluated on the corrupted run. The estimate of the indirect effect of that site is
Read the definition as a recipe. The Gradient says how fast the metric moves when the state moves. The difference says how far the state would move if we patched it. Their dot product is the first-order prediction of how much the metric moves.
Step 2 in symbols: the metric
The corrupted run gives . The clean run gives , and the total effect is .
Step 3 in symbols: one backward pass for all sites
Write for the hidden state at token after layer . The backward pass applies the chain rule from back through every layer, which gives for every site in one sweep. This is the reverse-mode computation described in Backpropagation. The gradient at each site includes every downstream path, because the chain rule adds up the effect along all of them.
Step 4 and 5 in symbols: the dot product
For the site "Needle" after layer 2, write for the difference between clean and corrupted vectors. The gradient dotted with gives the estimate:
The exact patch gave , so the error is .
Why this is a Taylor approximation
Let , the metric as a function of the patched state, with the other states held at their corrupted values. The first-order Taylor expansion around is
Subtracting from both sides gives the estimate. The exact indirect effect is the left side minus . The error is the remainder of the series, which is roughly proportional to the square of the size of times the curvature of . Curvature comes from nonlinear parts such as the Softmax inside attention, the normalisation layers and the activation functions. The estimate is good when is small or is nearly flat in that direction.
Worked examples
Take one scalar state and a metric that is linear near the corrupted point, . The corrupted value is and the clean value is .
- The gradient is everywhere, so at the corrupted point it is 0.20.
- The difference is .
- The estimate is .
- The exact patch sets and gives . The indirect effect is .
- Check: the estimate and the exact value both equal 0.40.
Interpretation: when the metric is a straight line between the two points, the first-order estimate is perfect. Real networks are curved, so the next example is more typical.
Use the numbers from the walkthrough. The corrupted metric is 0.10, the clean metric is 0.80, and the total effect is 0.70.
- The estimate for the site "Needle" after layer 2 is 0.30.
- The exact patch from causal tracing gives 0.35.
- The error is , which is about 14 percent of the exact value.
- The estimated share of the total effect is . The exact share was .
- Compare the two shares: both say the site carries a large part of the effect, and both rank it near the top of this prompt.
Interpretation: a small error in the value does not change the conclusion that this site matters. For screening, the ranking is what counts, and the exact patch can settle any close call.
These numbers are hypothetical, shaped like the factual-recall setting in Causal Tracing. The prompt is "The Eiffel Tower is in", and the correct answer is "Paris". The clean probability is 0.90 and the corrupted probability, after noising the subject, is 0.05.
- Exact causal tracing for the last subject token gave an indirect effect of 0.65 at layer 6 and 0.03 at layer 1 (hypothetical values for this prompt).
- The gradient estimate for the same token at layer 6 is 0.60, and at layer 1 it is 0.02.
- The ratio of estimate to exact is at layer 6, and at layer 1.
- Rank the sites: the estimate puts layer 6 far above layer 1, as the exact patch does.
- Cost: exact tracing for this token and all 13 hidden-state layers, including embeddings, needs 13 patched forward passes. The estimate needs one clean pass, one corrupted pass and one backward pass for every site at once.
Interpretation: the estimate is off by 10 to 30 percent at these sites, but it gets the ranking right. The exact patch at layer 6 is still needed before any editing claim.
Try it: at one site the gradient is 0.4 for a single scalar state, the clean value is 1.5 and the corrupted value is 0.5. What is the estimate? Answer: the difference is , so the estimate is .
Common mistakes and tradeoffs
It is tempting to think that attribution patching gives the same numbers as causal tracing. In fact it gives a first-order approximation. The two agree when the metric is nearly linear over the size of the patch, and they can differ a lot when an attention pattern or a normalisation changes sharply.
It is tempting to think that a gradient of zero means a site does nothing. In fact a zero or tiny gradient at the corrupted point can hide an effect. The metric may be flat there but move a lot after a large patch, for instance when a softmax is saturated. The estimate then misses a real effect, which is a false negative. Real patches catch these cases, so screening should never be the final word on a site that looks unimportant.
It is tempting to think that the estimates add up to the total effect. In fact the effects overlap, as in causal tracing, so the sum of estimates is not the total effect. Read each estimate on its own, and use them to rank sites.
It is tempting to think that the estimate is reliable whenever it is cheap. In fact the cheapness is the reason to use it for screening. Its value is in choosing which few sites to test exactly.
When to use it. Use attribution patching when there are many sites, the model is large, and you need a ranked list quickly. It is a good first pass before a study of a circuit or an editing location.
When not to use it. Do not use it alone when the claim must be exact, when only a few sites exist (just patch them directly), or when the behaviour depends on a very large change, since the first-order error grows with the size of the change.
Alternatives. Causal tracing gives exact indirect effects at one forward pass per site. Ablation sets a state to zero or to its mean instead of a clean value. Path patching restricts the intervention to one route between components. Later variants of attribution patching add corrections for the cases where the plain gradient misses an effect.
History
Causal mediation analysis came from statistics and the social sciences, where people split a total effect into direct and indirect parts (see Mediation Analysis). Causal tracing was used for factual recall in the ROME work (Meng et al., 2022). Its exact method, copying one state at a time, costs one forward pass per site, which becomes expensive for large models.
Attribution patching came later as a way around that cost. It reuses a tool that already exists for training networks, the backward pass, to estimate all the patches at once. It is used because it turns a sweep of thousands of runs into a small number of passes, which made large screening studies practical. Its known weaknesses, the first-order error and the false negatives, are why it is paired with exact patches in practice.
Recap
- Causal tracing measures the effect of each hidden state by patching it, one forward pass per site.
- A first-order Taylor expansion turns each patch into a dot product of the gradient and the difference between the clean and corrupted states.
- One backward pass gives the gradient for every site, so the whole map costs about two passes.
- The estimate ranks sites well but can be off by a fair margin, and it can miss effects where the metric is flat or saturated.
- The final claim about a site should come from an exact patch, which is why screening and verification are used together.
Read next: Mechanistic Interpretability for the wider project this tool serves, and Gradient and Backpropagation for the two ideas it is built from.