Phase C · mechanistic interpretability

Attribution / edge patching

Nanda 2023; Syed et al. 2023

← back to the method catalogue

What it does?

Attribution patching is a fast, gradient-based approximation to activation patching. Exact patching requires a separate re-run for every internal value tested, which is expensive on a large network. Attribution patching instead uses a first-order, linear estimate: it multiplies the gradient at each site by the change that patching would apply, to predict the patch's effect in a single backward pass. This lets researchers screen thousands of sites at once, at industrial scale, and only re-run the exact patch on the promising ones. We apply it to the VCS by forming the gradient-linear estimate of each site's patch effect and comparing it to the exact effect the oracle measures. The comparison is unusually clean here because we have the exact answer for every site. The linear surrogate keeps the edges precise: it is usually right about which dependencies exist, because a real edge tends to have a non-zero gradient. What it gets wrong is the size of an edge, since the true relationship is not linear, so its numerical agreement with the exact patch is only partial. Attribution patching is therefore a good cheap screen with a measurable error, and the VCS lets us quantify exactly how much accuracy the approximation trades away for its speed.

Attribution / edge patching result

Each point is one intervention site: its exact causal effect from the oracle (x) against the method's recovered/approximate effect (y). Points on the dashed diagonal mean the method recovered the true effect. corr_approx_vs_exact = 0.876 — this example only (pong, state gameplay(seed=0,prefix=90)+15); the audit aggregate is below.

Mechanistic family · patching at gradient cost

Learn the method — a beginner's guide

New to explainable AI? This block explains the method as it is used on neural networks, then how we run the very same method on an Atari 2600, where — unlike for a network — we know the right answer. Every implementation step links to the code. See also the XAI primer for the shared vocabulary.

The idea in plain words

Activation patching is the gold standard of mechanistic interpretability, but it is slow. You need one extra forward pass for every component you test. A large language model has millions of neurons and thousands of attention heads at every token position, so exact patching of all of them is out of reach. Attribution patching is the shortcut. It predicts the effect of every patch at once from a single forward and backward pass.

The trick is the oldest one in calculus: near a point, a smooth function looks like a straight line. If you know the slope of the output with respect to an activation (the gradient), and you know how far the patch would move that activation, then slope × distance is a guess for how far the output moves. That guess is a first-order Taylor approximation. It is exact when the output is linear in the activation, and wrong when it is not. On the VCS we can measure the exact patch effect for every site, so we can see precisely how much accuracy the shortcut trades for its speed.

How it works on a neural network

Use the notation of activation patching: clean input \(x\), corrupted input \(\tilde x\), component \(\ell\) with clean activation \(a = a_\ell(x)\) and corrupted activation \(a' = a_\ell(\tilde x)\). The exact patch effect is \(\Delta_\ell = y(x \mid a_\ell := a') - y(x)\). Attribution patching replaces it with

\[ \widehat{\Delta}_\ell \;=\; \left.\frac{\partial y}{\partial a_\ell}\right|_{x} \cdot \,(a' - a). \]

The gradient for all components comes from one backward pass on the clean run; the \(a' - a\) terms come from one cached corrupted run. That is three passes in total, instead of one per component. Edge attribution patching applies the same estimate to a connection between two components (the sender’s change times the receiver’s gradient), so it can score every edge of the computational graph cheaply.

Toy example. Let \(y = a^2\) with clean \(a = 1\). The slope is \(2a = 2\).

The first two cases are why attribution patching is good at which components matter but poor at how much. The third is the dangerous case: a real effect hidden behind a zero gradient, just like saturated attention or a dead ReLU.

Recipe

  1. Run the corrupted input once and cache every activation \(a'_\ell\).
  2. Run the clean input forward, caching \(a_\ell\), and backward from the metric \(y\) to get every \(\partial y/\partial a_\ell\).
  3. For each component, compute \(\widehat\Delta_\ell = \partial y/\partial a_\ell \cdot (a'_\ell - a_\ell)\) (a dot product for vector activations).
  4. Rank components by \(|\widehat\Delta_\ell|\) and keep the top ones.
  5. Verify the shortlist with exact activation patching; the estimate is a screen, not a verdict.

From a neural network to the Atari

Activation \(a_\ell\)One state site at frame \(f^*\): a candidate RAM byte or the COLUBK TIA register.
Corrupted activation \(a'\)The value from a donor run (LEFT or RIGHT pressed before \(f^*\)), or the directed value base+17.
Gradient \(\partial y / \partial a\)The local slope of the true 15-frame output, measured by poking the byte to \(a \pm 1\) and re-running the real ROM (a central finite difference).
Metric \(y\)The two most causally active candidate bytes and the count of changed screen pixels, read 15 frames later.
Exact patching (the reference)The full re-run with the patched value, which on the VCS equals the oracle.
An edgeA (patched site, output) pair: one cell of the effect matrix.

How we apply it to the VCS

The runner is built on top of the activation-patching harness. It uses the same shared gameplay checkpoint, the same outputs and the same patch list: donor patches from LEFT/RIGHT runs, directed base+17 patches on every candidate byte, and one background-colour patch.

  1. The slope. For each site the runner writes \(a+1\) and \(a-1\), re-runs 15 frames each, and divides the difference by 2. At 0 or 255 it uses a one-sided difference. This is the program’s own local derivative, measured on the real machine. It is not a Zygote backward pass. For a single-frame content read the two coincide, but the finite difference also works across 15 frames of discrete code, where autodiff gives no useful answer.
  2. The estimate. \(\widehat\Delta = \text{slope} \times (a' - a)\) for every (site, output) pair.
  3. The truth. The exact patch: write \(a'\), re-run, read the outputs.
  4. Scores. Pearson correlation of estimate vs truth over all cells, mean and relative error, edge precision/recall (does the estimate fire where the truth fires?), and the fraction of active edges where estimate and truth agree exactly (the “linear regime”).
  5. Positive control. A byte read back at the patch frame itself is perfectly linear, \(y(a) = a\). For it the estimate must equal the truth for steps of 1 to 32. This proves the wiring is right, so any error elsewhere is the program’s nonlinearity.
  6. Position gradient. Because this is a gradient method, the runner also reports the naive Zygote gradient of the screen-position output (identically zero) next to the bilinear-sampler gradient, which restores a non-zero value.

Note one honest consequence. On a network the gradient is nearly free. Here each slope costs two re-runs, so the VCS version is not cheaper than exact patching. It is run to measure the approximation error, not to save compute.

Read the code

XAI the interpretability method   substrate the bit-exact emulator it runs on (jutari, Julia).

Reading the result

Expect the estimate to agree with the truth on which edges exist far better than on their size. Small donor patches and single-frame reads sit in the linear regime and match closely. Large directed patches, bytes that wrap at 255, and bytes the game recomputes each frame break linearity, and the scatter of estimate vs truth spreads out. The correlation is therefore good but clearly below the exact method, and the positive control shows the gap is the program’s fault, not the harness’s.

Pitfalls

Try it yourself

julia --project=jutari tools/xai_study/phaseC_mechanistic/attribution_patching.jl --games core

Writes attribution_patching_<game>.json/.npz and a core summary into tools/xai_study/phaseC_mechanistic/out/. Three re-runs per patch (two slope probes plus the exact patch); minutes per game on one CPU core.

Original papers & further reading

Read next: Activation patching / causal mediation · Vanilla gradient (saliency) · ACDC — automatic circuit discovery · Path patching / IOI circuit

How it's scored

The score is the Pearson correlation between the gradient-approximate patch effect and the exact patch effect from the oracle. The grading is always against the intervention oracle. Attribution patching forms a first-order, linear estimate of each site's patch effect in a single backward pass, so we can compare that estimate directly to the exact effect the oracle measures at the same site. Faithfulness for this method is that correlation, and it is good but not perfect, because the true relationship is not linear. The linear surrogate keeps the edges precise: it is usually right about which dependencies exist, since a real edge tends to have a non-zero gradient, so edge precision and recall stay high. What it gets wrong is the size of an edge, which is why the numerical agreement with the exact patch is only partial. There is no sprite-position collapse framed as such here, because the method is scored on internal patch effects rather than on a rendered position; but its gradient origin is exactly why it approximates rather than matches. The grading is always against the oracle. The audit box reports the measured triad, so the correlation is an average across the scored games, quantifying how much accuracy the fast approximation trades away for its speed.

The score is measured against the §1 intervention oracle — never against another interpretability method. F (faithful) is always vs the oracle; see the execution stack. How each of F / S / M is actually computed for this method (and whether it matches the paper) is in the box just below; the numbers are in the In the audit box under it.

How F, S, M are computed here

The exact formula this method uses for each score, read from its runner, and whether it matches the paper's §3 (F ∧ S ∧ M triad) definition. From the committed audit fsm_math_phaseC.json.

AxisFormula (as computed) What it measuresMatches §3?
F
faithfulness
\(F = \rho_{\mathrm{Pearson}}\!\left(\hat{\Delta y}(u),\ \Delta y(u)\right)\)Pearson correlation between the first-order attribution-patching approximation of each patch effect and the exact re-run patch effect.✓ matches attribution_patching.jl (F = clamp(r.corr,0,1)); metric_name corr_approx_vs_exact. This is the paper's Phase-C patching F = causal-effect-agreement correlation vs Delta_y(u). Record: extra.triad.F (attribution_patching_pong.json F=0.876; battery mean F=0.456, n=42).
how it's measured — call stack:
  1. F = clamp Pearson(approx Δ, exact patch Δ), into the triad — attribution_patching.jl:580
  2. true causal effect Δy(u) by bit-exact re-run — oracle_intervene.jl:242
S
sufficiency
\(S = \dfrac{\#\{\,u_{\mathrm{held}}:\ |\hat{y}-y|\le\varepsilon\,\}}{|\mathrm{held\text{-}out}|}\in[0,1]\)Same shared held-out predictive sufficiency estimator as activation_patching.✓ matches common/triad_sm.jl sufficiency_score via triad_extra_dict (attribution_patching.jl). Paper-conform. Record: extra.triad.S (pong S=0.973; battery mean S=0.769, n=42).
how it's measured — call stack:
  1. sufficiency_score: fit Δy=a·attr+b on a calib half, predict held-out within ε — triad_sm.jl:111
  2. assemble the triad via triad_extra_dict — attribution_patching.jl:580
  3. true causal effect Δy(u) by bit-exact re-run — oracle_intervene.jl:242
M
minimality
\(M = |U^{\star}| / |\hat{U}| \in (0,1]\quad U^{\star}=\{u:\Delta y(u)>0\},\ \hat{U}=\{u:|\mathrm{attr}(u)|>\tau\}\)True minimal cause set over method-named (above-threshold) set.✓ matches common/triad_sm.jl minimality_score. Paper-conform. Record: extra.triad.M (pong |U*|=6/|U_hat|=5, M=1.0; battery mean M=0.995, n=42).
how it's measured — call stack:
  1. minimality_score: |U*| oracle movers / |U_hat| named cells — triad_sm.jl:57
  2. assemble the triad via triad_extra_dict — attribution_patching.jl:580
  3. true causal effect Δy(u) by bit-exact re-run — oracle_intervene.jl:242

✓ matches = the same quantity as §3; ◐ partial = the same kind of estimator but a differing detail; ✗ does not match = a different quantity (see the note). Definitions: F = agreement with the oracle's true causal effects Δy(u); S = held-out predictive score in [−1, 1]; M = |U*|/|Û| (true-minimal-set size / named-set size).

In the audit

This is the method's entry in the actual cross-method audit — scored on the paper's correctness triad, each axis a mean over 44 of the 44 scored games (44 committed §R records), not the single example shown above. Tradition: gradient. The example figure (Pong) is one of those records.

0.482F — faithfulness vs oracle (mean over 44 games, ±0.111 CI95)
0.798S — sufficiency: held-out predictive (n/a where the paper does not define this axis)
0.990M — minimality: true-minimal-set / named-set (n/a otherwise)
44committed records aggregated
0.00human-plausibility proxy

F faithfulness (scored vs the oracle for every method) · S sufficiency (held-out predictive score in [−1, 1]; a negative value means the explanation predicts held-out interventions worse than the unperturbed baseline; reported for the predictive methods across all three phases where the calibration/held-out split is defined) · M minimality (true-minimal-set / named-set; where the method names a cause set) — n/a otherwise, per the paper's F ∧ S ∧ M triad.

Source: leaderboard.json · the whole leaderboard is on the methods page and the Paper 2 audit.

Results per game

This method's faithfulness on each of the 44 scored games (all-regime F; this method has no content-vs-position split (see the note below)). Click a header to sort. Every number is read from site_data.json (methods.attribution_patching.per_game).

content-F and position-F are n/a for this method. The content-vs-position split applies only to pixel-attribution methods — those that produce an attribution map over the framebuffer, whose score can be read separately on the smooth content outputs and the discrete position outputs (where the naive gradient is provably zero). This method does not attribute to a pixel output; it is graded by its own fidelity metric (corr_approx_vs_exact), reported as the F column, so there is no content or position regime to separate.

GameF (all)content-Fposition-FSMNote
Kung-Fu Master1.000n/an/a0.9761.000recovers the true causes
Berzerk1.000n/an/a0.9581.000recovers the true causes
Centipede1.000n/an/a0.9761.000recovers the true causes
Yars' Revenge1.000n/an/a0.9831.000recovers the true causes
Kangaroo0.999n/an/a0.9271.000recovers the true causes
Carnival0.998n/an/a0.9511.000recovers the true causes
Tennis0.986n/an/a0.9271.000recovers the true causes
Hero0.960n/an/a0.9621.000recovers the true causes
Asterix0.946n/an/a0.9761.000recovers the true causes
Breakout0.936n/an/a0.9521.000recovers the true causes
Venture0.909n/an/a0.0001.000recovers the true causes
Pong0.876n/an/a0.9731.000recovers the true causes
Pitfall0.783n/an/a0.9181.000
Video Pinball0.769n/an/a0.9091.000
Skiing0.759n/an/a0.9521.000
Riverraid0.654n/an/a0.9601.000
Ms. Pac-Man0.645n/an/a0.9431.000
Air Raid0.634n/an/a0.9831.000
Montezuma's Revenge0.567n/an/a0.9571.000
Private Eye0.497n/an/a0.9541.000
Atlantis0.493n/an/a0.9521.000
Bank Heist0.463n/an/a0.0421.000
Road Runner0.434n/an/a0.0120.778
Assault0.408n/an/a0.9781.000
Pacman0.371n/an/a0.0541.000
Space Invaders0.360n/an/a0.9621.000
Ice Hockey0.310n/an/a0.8911.000
Chopper Command0.257n/an/a0.9781.000
Gopher0.232n/an/a0.9781.000
Freeway0.198n/an/a0.8911.000
Fishing Derby0.183n/an/a0.9521.000
Seaquest0.178n/an/a0.9521.000
Jamesbond0.174n/an/a0.9171.000
Phoenix0.131n/an/a0.9091.000
Krull0.082n/an/a0.9651.000
Frostbite0.031n/an/a0.9431.000no true-cause signal
Q*bert0.006n/an/a0.0001.000no true-cause signal
Alien0.000n/an/a0.8931.000no true-cause signal
Bowling0.000n/an/a0.9451.000no true-cause signal
Boxing0.000n/an/a0.9411.000no true-cause signal
Demon Attack0.000n/an/a0.9131.000no true-cause signal
Double Dunk0.000n/an/a0.8911.000no true-cause signal
Enduro0.000n/an/a0.0000.800no true-cause signal
Name This Game0.000n/an/a0.0001.000no true-cause signal

Call stack — how it runs on jutari / jaxtari

The path from this method's runner (run_game) into the bit-exact VCS substrate, and the computation it involves. Every step links to the exact source on main. From callstack_phaseC.json.

Applied on the substrate

  1. Boot the real ROM with the xitari-parity reset (60 NOOP + 4 RESET) — env_reset!(env; boot_noop_steps = 60, boot_reset_steps = 4) jutari
  2. Deterministically step the emulator one action per env-frame — env_step!(env::StellaEnvironment, action::Integer) jutari
  3. Assemble the shared gameplay-state testbed via build_attr_shared_state (also builds the sampler-aware position read through soft_ram_peek) — build_shared_testbed(game; ... run_intervention = run_intervention, soft_ram_peek = soft_ram_peek, ...) via build_attr_shared_state runner
  4. Freeze byte-exact RAM + screen out of the env at a frame — snapshot(env::StellaEnvironment, frame::Integer) jutari

The computation

  1. THE gradient step (linear/gradient surrogate): local slope (partial y / partial a)|_clean via a central finite difference of the true patch at a+eps and a-eps — site_gradient(...) -> snap_hi = run_patch(...hi); snap_lo = run_patch(...lo); (read(snap_hi)-read(snap_lo))/denom runner gradient surrogate over the true patch run_patch (activation_patching.jl:282)
  2. Attribution approx = grad*(a'-a) compared cell-by-cell against the exact full re-run patch (ex_snap = run_patch) in _run_game_body — ex_snap = run_patch(clean_ckpt, tail, p.kind, p.site, p.value) runner ex_snap @ :352; linear-probe positive control @ linear_probe :481
  3. Sampler-aware position gradient (redesign Problem 2): differentiable screen-position read of the RAM tape through the bilinear sampler; nonzero partial/partial ram[pidx] via the one-hot soft RAM read — _st_sampler_position_read(ram, geom, cell; soft_ram_peek) -> soft_ram_peek(ram, pidx) runner soft_ram_peek definition @ jutari/src/diff/SoftStep.jl:108

Scored against the exact intervention oracle Δy(u): run_intervention(checkpoint, actions, target_frame, horizon, cause).

On jaxtari (JAX / GPU): the same differentiable path has a bit-exact JAX sibling (Theorem 1, forward-exact) for batched GPU runs — jaxtari/jaxtari/diff/soft_step.py (soft_ram_peek @ line 102) — the JAX SOFT-STE gradient path, forward-bit-exact to the HARD jutari map. attribution_patching is the ONLY Phase-C method with a gradient step; its sampler-aware position gradient routes through soft_ram_peek (jutari/src/diff/SoftStep.jl:108), whose JAX sibling is this file.

Implementation
tools/xai_study/phaseC_mechanistic/attribution_patching.jl
Reference
Nanda 2023; Syed et al. 2023
Record
attribution_patching_pong.json
All records
phaseC_mechanistic/out

The figure is generated from the committed record by docs/gen_method_figures.py; the game frame and each RAM cell's screen footprint are produced by render_scenes.jl / cell_footprints.jl.