Search

Search papers and lessons

Attribution Patching

Level 3 · 13 min read

Attribution patching estimates the effect of each patch in causal tracing with one gradient, a dot product, instead of one forward pass per site, trading exactness for a map of the whole network in two passes.

On this page

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

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 mm of "Seattle". In the clean run m=0.80m = 0.80. In the corrupted run, where the subject embeddings are noised, m=0.10m = 0.10.

  1. 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 m=0.80m = 0.80.
  2. Corrupted run. Run the corrupted prompt once and save its hidden states. Check: m=0.10m = 0.10, so the total effect to explain is 0.80−0.10=0.700.80 - 0.10 = 0.70.
  3. Backward pass. From the corrupted run, compute the gradient of mm 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.
  4. 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 dd. Check: dd is read from the two saved runs, so no new run is needed.
  5. 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.
  6. 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.
  7. 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"]
00.511.522.533.5400.10.20.30.4layerindirect effect on P(Seattle)
  • exact patch (causal tracing)
  • gradient estimate
Toy numbers: the estimate follows the exact patch closely and ranks the layers the same way. Both are near zero at the last layer.

The math

Definition: Attribution patching estimate

Let mm be the metric, here m=p(y∣x)m = p(y \mid x), the probability of the correct answer. For a site with hidden state hh, let hcleanh_{\text{clean}} be its value in the clean run and hcorrh_{\text{corr}} its value in the corrupted run. Define the difference d=hclean−hcorrd = h_{\text{clean}} - h_{\text{corr}} and the gradient ∇hm\nabla_h m evaluated on the corrupted run. The estimate of the indirect effect of that site is

IE^=∇hm∣xcorr⋅d.\widehat{\text{IE}} = \nabla_h m \big|_{x_{\text{corr}}} \cdot d.

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 m(xcorr)=0.10m(x_{\text{corr}}) = 0.10. The clean run gives m(xclean)=0.80m(x_{\text{clean}}) = 0.80, and the total effect is 0.80−0.10=0.700.80 - 0.10 = 0.70.

Step 3 in symbols: one backward pass for all sites

Write ht(l)h^{(l)}_t for the hidden state at token tt after layer ll. The backward pass applies the chain rule from mm back through every layer, which gives ∇ht(l)m\nabla_{h^{(l)}_t} m 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 dd for the difference between clean and corrupted vectors. The gradient dotted with dd gives the estimate:

IE^(Needle,2)=∇m⋅d=0.30.\widehat{\text{IE}}(\text{Needle}, 2) = \nabla m \cdot d = 0.30.

The exact patch gave IE(Needle,2)=0.35\text{IE}(\text{Needle}, 2) = 0.35, so the error is 0.35−0.30=0.050.35 - 0.30 = 0.05.

Why this is a Taylor approximation

Let f(h)=mf(h) = m, the metric as a function of the patched state, with the other states held at their corrupted values. The first-order Taylor expansion around hcorrh_{\text{corr}} is

m(hcorr+d)≈m(hcorr)+∇hm⋅d.m(h_{\text{corr}} + d) \approx m(h_{\text{corr}}) + \nabla_h m \cdot d.

Subtracting m(hcorr)m(h_{\text{corr}}) from both sides gives the estimate. The exact indirect effect is the left side minus m(hcorr)m(h_{\text{corr}}). The error is the remainder of the series, which is roughly proportional to the square of the size of dd times the curvature of mm. Curvature comes from nonlinear parts such as the Softmax inside attention, the normalisation layers and the activation functions. The estimate is good when dd is small or mm is nearly flat in that direction.

Worked examples

Example
1. Tiny: a linear metric, where the estimate is exact

Take one scalar state and a metric that is linear near the corrupted point, m(h)=0.10+0.20 hm(h) = 0.10 + 0.20\,h. The corrupted value is hcorr=0h_{\text{corr}} = 0 and the clean value is hclean=2h_{\text{clean}} = 2.

  1. The gradient is ∂m/∂h=0.20\partial m/\partial h = 0.20 everywhere, so at the corrupted point it is 0.20.
  2. The difference is d=2−0=2d = 2 - 0 = 2.
  3. The estimate is 0.20⋅2=0.400.20 \cdot 2 = 0.40.
  4. The exact patch sets h=2h = 2 and gives m=0.10+0.20⋅2=0.50m = 0.10 + 0.20 \cdot 2 = 0.50. The indirect effect is 0.50−0.10=0.400.50 - 0.10 = 0.40.
  5. 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.

Example
2. Standard: the Needle site from the walkthrough

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.

  1. The estimate for the site "Needle" after layer 2 is 0.30.
  2. The exact patch from causal tracing gives 0.35.
  3. The error is 0.35−0.30=0.050.35 - 0.30 = 0.05, which is about 14 percent of the exact value.
  4. The estimated share of the total effect is 0.30/0.70≈0.430.30 / 0.70 \approx 0.43. The exact share was 0.35/0.70=0.500.35 / 0.70 = 0.50.
  5. 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.

Example
3. Realistic: screening a 12-layer model for a fact

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.

  1. 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).
  2. The gradient estimate for the same token at layer 6 is 0.60, and at layer 1 it is 0.02.
  3. The ratio of estimate to exact is 0.60/0.65≈0.920.60 / 0.65 \approx 0.92 at layer 6, and 0.02/0.03≈0.670.02 / 0.03 \approx 0.67 at layer 1.
  4. Rank the sites: the estimate puts layer 6 far above layer 1, as the exact patch does.
  5. 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 1.5−0.5=1.01.5 - 0.5 = 1.0, so the estimate is 0.4⋅1.0=0.40.4 \cdot 1.0 = 0.4.

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.

Check yourself

QuestionRecall: what are the two quantities multiplied together in the attribution patching estimate?Show answer
The gradient of the metric with respect to the hidden state, taken on the corrupted run, and the difference between the clean and corrupted values of that state. Their dot product estimates the indirect effect of patching that state.
QuestionCompute: the gradient of the metric at a site is 0.25 for one scalar state. The clean value is 2.0 and the corrupted value is 0.4. What is the estimated indirect effect?Show answer
The difference is 2.0−0.4=1.62.0 - 0.4 = 1.6. The estimate is 0.25⋅1.6=0.400.25 \cdot 1.6 = 0.40.
QuestionWhy: why does one backward pass cover every site, when causal tracing needs one forward pass per site?Show answer
The backward pass applies the chain rule from the metric back through the network, and it produces the gradient at every hidden state along the way. Each site's estimate is then a dot product with a difference that is already saved from the two runs. Causal tracing has to rerun the network with each site patched, so its cost grows with the number of sites.
QuestionApplied: a screening run gives a site an estimate near zero, and a colleague says the site is unimportant. What would you check before agreeing?Show answer
Check whether the metric is saturated or flat at the corrupted point, since a zero gradient can hide an effect that appears after a large patch. Then run an exact patch on that site. Only if the exact patch is also near zero is the site unimportant for this prompt.

Sources