An illustrated workbench labeled X_eTaL-circuits: monitors of circuit diagrams and analysis panels, bundles of glowing wires routed between instruments, and a drawing tablet with a circuit sketch

I recently started attending a weekly Deep Learning meetup on Zoom, where each week one person goes through a paper and the others discuss it. The group votes on a paper to study the following week. This series covers some of the papers discussed, for which I went on to create X_eTaL-based repos to explore and demonstrate concepts from each paper.

This first post is about circuits: the groups of parts inside a language model that work together to produce one behavior. X_eTaL-circuits is a workbench for finding, testing and drawing them, with X_eTaL as the analysis and drawing language. Its first study finds the circuit GPT-2 Small uses to continue a repeated sequence, and tests every part of it by experiment.

What a circuit is

A language model reads some words and guesses the next one. Inside, it is a stack of layers. On every layer, attention heads look back at earlier words and fetch something useful, and MLPs work on what was fetched. GPT-2 Small has 12 layers of 12 heads, 144 heads in all. Each position carries a running record, the residual stream, that every part reads and adds to.

Nobody wrote down which part does what; the model learned it. A circuit is a set of those parts, and the paths between them, that cooperate on one behavior. Circuits are not separate programs the model switches between. They describe mechanisms inside one fixed, learned computation, and they can only be found by experiment.

The method: change something, measure the margin

The workbench follows the causal method of the paper A Circuit for In-Context Learning, which looks for the parts that let a model learn made-up labels from a few examples in its prompt:

  1. Twin prompts that differ in exactly one controlled way, with their tokens lined up.
  2. A continuous measure: the margin, log P(correct) - log P(incorrect), not just right or wrong.
  3. Interventions: switch a head off (ablation), or copy its output from one twin into the other (patching), and compare each result with matched random controls.
  4. A circuit graph whose edges are tested by path patching, then checked on held-out prompts.

An attention map shows where a model looks. A circuit shows what the model needs.

Before trying this on the paper’s models, the workbench checks itself on a circuit that is already well known: GPT-2 Small’s induction heads. Give it a random list of tokens, then the same list again, and the second time through it predicts each next token easily. It looks back at what came after this token last time, and copies it.

Looking: where a head attends

Heatmap of GPT-2 Small head 5.1 on a beginning token, eight random tokens, then the same eight again: on the second half each row's strongest cell sits on the token after the earlier copy, a stripe below the diagonal
Head 5.1's attention, drawn by X_eTaL: a row per query token, a column per key. Click to enlarge.

A small Python adapter runs the model through TransformerLens and saves a capture: every head’s attention (layer by head by query by key), the last position’s logits, and everything needed to repeat the run (the model and its revision, the library versions, the seed, the prompt and its tokens). X_eTaL reads the capture, checks it (no head looks at a future token; each row of attention sums to 1), and draws it.

The prompt here is a beginning token, eight random tokens, then the same eight again. On the first half, head 5.1 rests on the first token. On the second half, each token looks at the token that followed its earlier copy: the stripe below the diagonal. The values X_eTaL reads agree with TransformerLens’s own cache within 1e-5 relative.

That is an observation of where the head looks. It is not evidence that the output needs it.

Scoring every head

A 12 by 12 grid, layer by head, each cell a GPT-2 Small head's induction score; the strongest cells are in layers 5 to 7
The induction score of every head. Click to enlarge.

Each head gets two scores from a repeated prompt. The previous-token score is its mean weight from each position to the one before it. The induction score is its mean weight from each token of the second copy to the token that followed it in the first. Both are layer by head matrices, which is the kind of thing an array language is for:

ˡp̲revious ← { d →
  A ← ᶜᵖa̲ttention d
  n ← f̲irst r̲ev s̲hape A
  A ʰm̲asked f̲loat (r̲ange n) '{ a b → a = b + 1 } t̲able r̲ange n
}

ʰm̲asked ← { A M → 2 '{ S → ('+ r̲/ r̲avel S × M) ÷ '+ r̲/ r̲avel M } r̲ank A }

A table of query against key positions makes a mask of the cells one step back; 2 … r̲ank A takes each head’s query by key matrix and averages it under the mask. The induction score is the same with a different mask.

The five leading induction heads, 5.5, 5.1, 6.9, 7.10 and 7.2, are the GPT-2 Small induction heads known from the literature, and the top previous-token head, 4.11, is its known previous-token head. Still, these are candidates.

Testing: which heads the output needs

A layer by head grid of the share of the gap each confirmed head's output restores on held-out seeds, largest for 6.9, 5.1, 7.2 and 5.5
Patching: the share of the gap each confirmed head restores, on held-out seeds. Click to enlarge.

Each repeated prompt gets a twin whose second copy is fresh tokens, and the measure is the logit difference between the right next token and the twin’s. Then one head at a time is switched off (ablation), or its output is copied between the twins (patching), over ten seeds.

An effect counts only if chance cannot explain it. Each head also gets 20 changes to its output of the same size as the real intervention, in random directions. A head passes when its mean effect is at least 5% and larger than every one of its controls. Then the same test runs on ten held-out seeds that played no part in choosing heads or the threshold; a head that passes again is confirmed.

What survives: switching off 5.1, 4.11, 7.6, 8.5 or 5.5 costs 5 to 22% of the logit difference, far more than a random change of the same size. Restoring any of the five induction heads, or 9.9, 9.6, 10.1 or 0.1, brings back 5 to 15% of what the twin lost. The heads back each other up: removing one costs little, except 5.1.

Which head feeds which

A head is one thing; an edge is one head’s output reaching a later head’s query, key or value, or the final output. GPT-2 Small has 29,089 of them, too many to path-patch one by one. So a gradient approximation estimates them all at once, the 400 with the largest estimated changes are path-patched (each with 20 random controls), and X_eTaL judges which pass:

The edges into attention keys that beat their random controls, drawn between GPT-2 Small's layers; most start at head 4.11 and end at the induction heads
Edges into keys that beat their controls: 4.11 feeds the key of every induction head. Click to enlarge.

186 of the 400 edges pass. The previous-token head 4.11 feeds the key of every induction head (5.1, 5.5, 6.9, 7.2, 7.10, and 5.0, 5.8, 7.11, 8.1), restoring 17 to 45% of each one’s attention score at the induction target, against controls of 2% at most. Heads 3.7, 5.6 and 2.2 add to it, and 4.7 pushes against it. The induction heads in turn feed the queries of later heads, and ten heads write straight into the logits, two of them against the answer.

That is the textbook induction circuit, found again by experiment: a previous-token head tells each position what came before it, and the induction heads use that to find and copy the next token. The same 400 edges, path-patched again on ten held-out seeds that played no part in choosing them, confirm 181 of the 186: 174 of the 176 into queries and keys, among them every edge from 4.11 into an induction head’s key, at the same strength (36% on the finding seeds, 37% held out, for 4.11 into 5.1).

The model map

GPT-2 Small's model map: a row per layer, a cell per head and one per MLP, heads colored blue for induction and orange for previous token, confirmed heads outlined, the induction circuit's 22 confirmed edges drawn thick between them
The model map, drawn by X_eTaL: the circuit's twelve confirmed heads and its 22 edges, all confirmed. Click to enlarge.

The map draws the whole model in one picture: a row per layer, a cell per head and per MLP, the residual stream beside them. Heads are colored by their scores, blue for induction and orange for previous token. A circuit file draws its heads and edges over the map, and the line says how far the evidence has come: dashed for observed (an attention pattern or score only), solid for tested against random controls, thick for confirmed on held-out prompts. The circuit drawn has twelve confirmed heads: the previous-token head 4.11, the induction heads 5.1, 5.5, 6.9, 7.2 and 7.10, and six more the interventions found (7.6, 8.5, 9.9, 9.6, 10.1 and 0.1). Its 22 edges are every edge between those heads that path patching confirms, so all of them are drawn thick.

The live demo has the map (hover a head for its numbers), every head’s attention with a layer and head picker, the score grids, an introduction for a first-time reader and a glossary. Every picture there is drawn by X_eTaL, exactly as at the command line.

Why an array language

The live demo page: GPT-2 Small's model map with heads colored by their scores and the induction circuit drawn over it
The live demo. Click to enlarge.

These are array computations. An attention capture is a layers by heads by queries by keys tensor; a patching sweep is a layer by head effect matrix; a score is a mask and a mean. X_eTaL does the slicing, the statistics, the verdicts and the drawing. The model itself runs in Python through TransformerLens, on purpose: the analysis is in X_eTaL, and the model hooks stay in Python until the results match the paper’s own.

The rule throughout: an attention pattern is a suspect, not a culprit. Everything the workbench reports says which kind of evidence it has.

What comes next in the workbench

GPT-2 Small’s induction circuit is the check that the tools work. The studies planned next:

  • local models through Ollama: do they learn arbitrary labels from examples, and reverse them?
  • Qwen3-4B-Base, the paper’s reference model: are its writers, readers, gate and brakes found here too?
  • X_eTaL code: does the same mechanism carry over to programming-language examples?
  • a tiny trained transformer: in what order do circuit parts appear during training?
  • Baby Dragon Hatchling (BDH), a model designed to be inspected: does it make circuits easier to find?

Two of those connect to earlier posts here. The paper’s question, how a model learns from examples in its prompt, is the one In-Context Learning Revisited asked from the outside; circuits are the inside view. And BDH, the Baby Dragon Hatchling, is a model I have already coded and written about in Small Models (4/6): This AI Has a Visible Brain (my version): its sparse activations, with most neurons silent for each token, are meant to make what it does visible. GPT-2 Small’s circuit had to be found by experiment; the open question is how much of that work a model built to be inspected saves.

Closing

This is a first look at work still in progress. The circuit is found and its edges hold up on held-out seeds, but two checks remain before I’d call it done. The first is faithfulness: keep only the circuit’s heads and edges, blank out the rest of the model, and see how much of the behavior survives, compared against random subgraphs of the same size. With that come completeness and minimality: does removing the circuit remove the behavior, and does every head in it earn its place? The second is a graph view that draws the circuit directly, senders to receivers, with each edge’s evidence and whether it feeds a head’s query, key or value. After that come the studies above, starting with local models, and ending with BDH, a model built to be inspected. I’ll write about them as results come in.