Welcome back. The previous lesson showed that an ablation effect depends on what replaces the activation. This time, the replacement comes from a matched input: you will cache an activation on a reference run, insert it at one site during a counterfactual run, and measure the change in output. The goal is to make the intervention precise enough that its result can be interpreted, rather than merely producing a striking change in behavior.
What a matched patch compares
Suppose two inputs share the same context and differ in one deliberately chosen detail. Call one the reference input and the other the counterfactual input. Run the model on each without intervention, then rerun the counterfactual while replacing one of its activations with the value recorded at the corresponding site on the reference.
If is a score whose meaning remains fixed across runs, the restoration effect is
Unlike the generic resampling in the previous lesson, the donor is paired with the recipient by design. Matching does not make their internal states identical everywhere. It makes the input difference you intended to study clearer, while the patched run still combines a reference activation with the counterfactual run’s other activations.
The Transformer Lens Main Demo gives a transformer example. It uses its own activation hook points rather than ordinary PyTorch module hooks, but the intervention is the same: cache a value, replace a selected activation during another forward pass, and score the resulting output.
Transformer Lens Main Demo Notebook - GitHub Pages
Read the Transformer Lens demo for its clean/corrupted setup, choice of score, and single-position patch. The code later in this lesson implements the same core operation with PyTorch module hooks.
In “Hooks: Intervening on Activations,” read the hook mechanism, beginning at the explanation of hook points and ending where it specifies the replacement tensor. Then, in “Activation Patching on the Indirect Object Identification Task,” read the task setup, including the clean and corrupted prompts and their fixed logit-difference metric. Continue from “We now setup the hook function to do activation patching” through the patching loop. Focus on what is cached, which position is replaced, and which run supplies the rest of the computation.
In that demo, changing the prompt also changes which person is the correct continuation. Its fixed logit difference measures preference for the original reference answer across both runs. That is useful for measuring restoration, but it is distinct from a shortcut experiment in which a nuisance cue changes while the task-relevant answer stays fixed.
Implement one-position patching with PyTorch hooks
The following small network makes every value inspectable. Its three input positions are context, cue, and query. The matched inputs differ only at the cue. The network is hand-set rather than trained: it is a check that the patching procedure does what it claims, not evidence that a learned model has a particular mechanism.
The site module produces a tensor of shape [batch, position, hidden]. We will cache its entire output on each input but replace only position 1 during a patched run. The output score is always the logit difference for answer A versus answer B; we do not switch its meaning when the cue changes.
import torch
from torch import nn
class ThreeTokenModel(nn.Module):
def __init__(self):
super().__init__()
self.embedding = nn.Embedding(5, 2)
self.site = nn.Linear(2, 2, bias=False)
with torch.no_grad():
# Token 3: shared context; 1: cue A; 2: cue B;
# token 4: shared query.
self.embedding.weight.copy_(torch.tensor([
[ 0.0, 0.0], # unused
[ 1.0, 0.0], # cue A
[-1.0, 0.0], # cue B
[ 0.0, 1.0], # context
[ 0.0, 0.0], # query
]))
self.site.weight.copy_(torch.eye(2))
def forward(self, tokens):
h = self.site(self.embedding(tokens)) # [batch, 3, 2]
preference = h[:, 1, 0] + 0.25 * h[:, 0, 1]
return torch.stack((preference, -preference), dim=-1)
model = ThreeTokenModel().eval()
reference = torch.tensor([[3, 1, 4]])
counterfactual = torch.tensor([[3, 2, 4]])
assert reference.shape == counterfactual.shape
assert torch.equal(reference[:, [0, 2]], counterfactual[:, [0, 2]])
def run(tokens):
with torch.no_grad():
return model(tokens)
def logit_difference(logits):
# Fixed score: preference for answer A over answer B.
return (logits[:, 0] - logits[:, 1]).item()
def cache_site(tokens):
cached = []
def capture(_module, _inputs, output):
cached.append(output.detach().clone())
handle = model.site.register_forward_hook(capture)
try:
run(tokens)
finally:
handle.remove()
assert len(cached) == 1
return cached[0]
def run_patched(recipient_tokens, donor_activations, position):
def replace(_module, _inputs, output):
assert output.shape == donor_activations.shape
patched = output.clone()
patched[:, position, :] = donor_activations[:, position, :]
return patched
handle = model.site.register_forward_hook(replace)
try:
return run(recipient_tokens)
finally:
handle.remove()
reference_score = logit_difference(run(reference))
counterfactual_score = logit_difference(run(counterfactual))
reference_cache = cache_site(reference)
counterfactual_cache = cache_site(counterfactual)
restored_score = logit_difference(
run_patched(counterfactual, reference_cache, position=1)
)
unchanged_position_score = logit_difference(
run_patched(counterfactual, reference_cache, position=2)
)
reverse_score = logit_difference(
run_patched(reference, counterfactual_cache, position=1)
)
recovery = (
(restored_score - counterfactual_score)
/ (reference_score - counterfactual_score)
)
print("reference:", reference_score)
print("counterfactual:", counterfactual_score)
print("restored cue position:", restored_score)
print("patched unchanged position:", unchanged_position_score)
print("reverse patch:", reverse_score)
print("fraction recovered:", recovery)
The expected scores are 2.5, -1.5, 2.5, -1.5, and -1.5, followed by a recovered fraction of 1.0. Patching position 2 changes nothing in this toy model because the matched query activations are the same. Reversing the direction inserts the counterfactual cue activation into the reference run and moves its score to the counterfactual value.
Three implementation details matter beyond this example. First, the hook is removed in a finally block so it cannot silently alter later baseline runs. Second, the hook clones the output and modifies one position, rather than replacing every token’s activation. Third, the patched forward pass recomputes downstream operations from the substituted value; it does not splice together two finished output scores. On a real transformer, check tokenization and positional alignment before treating two positions as matched. If a module returns a tuple rather than a tensor, the hook must preserve that output structure.
Read the result as a conditional intervention
In this toy network, complete restoration is unsurprising: the differing cue activation supplies the changing term in the score. In a transformer, a score that recovers substantially says something narrower: at the specified site and position, inserting this reference activation into these counterfactual runs changes the measured behavior. A residual-stream patch replaces an entire hidden vector, not a named concept within it. It may restore several kinds of information at once.
You can report the raw effect and, when the reference and counterfactual scores differ, a normalized recovery:
Here means that the patched score equals the reference score, while means that the patch leaves the counterfactual score unchanged. This is a comparison of scores, not a guaranteed percentage of a mechanism recovered. A patch can overshoot the reference score or move in the opposite direction, so values outside the range from zero to one are possible. If the two unpatched scores are nearly equal, the denominator makes normalized recovery unstable; inspect the raw effects instead.
The direction also matters. The image below contrasts putting a corrupted activation into a clean computation with restoring a clean activation in a corrupted computation. Its different downstream gates illustrate why those two experiments need not have symmetric effects.

For a research example, read the causal-tracing setup in Locating and Editing Factual Associations in GPT. Its corruption adds noise to subject-token embeddings rather than swapping between the naturally occurring cue tokens used in our code. That difference is worth retaining when interpreting what its restoration measures.
Locating and Editing Factual Associations in GPT
Read Section 2.1 for an example of caching a clean run and restoring one internal state while the rest of the run remains corrupted. Notice that the authors measure the probability of a specified factual completion, rather than the logit difference used above.
In Section 2.1, “Causal Tracing of Factual Associations” (around printed pp. 2–3), begin after the opening paragraph on the causal graph, at “We wish to understand if there are.” Read the intervention setup through the definition of the average indirect effect, stopping before Section 2.2, “Causal Tracing Results.” Track separately the clean activation being restored, the corrupted input that remains in place, and the output probability used to measure the effect.
For your own patching study, state the paired-input rule, the exact module and position, the patch direction, and the fixed score. Check both unpatched runs before interpreting recovery. The single example above verifies the mechanics; a claim about a learned feature would also need results across held-out matched pairs and attention to effects outside the target behavior.
Key takeaways
Activation patching is a controlled substitution during a forward pass. Cache an activation on a reference input, replace the corresponding site in a counterfactual run, and compare outputs using a score with the same meaning in every run. Matching inputs, checking position alignment, limiting the edit to the intended site, and cleaning up hooks are part of the experiment—not just coding details.
A successful restoration demonstrates an effect of that substitution in the tested context. The next lesson will examine the further step of deciding whether an intervention supports conditional necessity, conditional sufficiency, or neither for a specified behavior.