Circuit Tracing: Attribution Graphs from Scratch in PyTorch

How it was built

Part 1: The Cross-Layer Transcoder

Record everything a forward pass of the small Transformer computes, then build the paper's replacement for its MLPs: a cross-layer transcoder. Its features read the residual stream in one layer through a JumpReLU and write to the MLP outputs of that layer and every later layer. You write the JumpReLU with its straight-through gradient, the transcoder, the paper's two losses and the training loop.

Part 2: Replacement Models and the Attribution Graph

Put the transcoder inside the model. First the replacement model, where features stand in for the MLPs. Then the local replacement model for one prompt, with frozen attention patterns and normalisation denominators plus error nodes, which reproduces the real model exactly. Then the backward pass through the frozen model, and the attribution graph built from it: embeddings, active features, error nodes and output tokens, joined by edges whose weight is the source activation times a virtual weight.

Part 3: Pruning, Scores and Interventions

Make the graph usable and test it. Compute each node's influence on the output through all paths, prune nodes and edges with the paper's two thresholds, measure how much of the computation the features explain with the replacement and completeness scores, and check what the graph claims by constrained patching: change a feature's contribution inside the real model and watch what happens downstream.