{"slug": "deep-learning-1-finding-circuits-in-gpt-2-small-with-x-etal", "title": "Deep Learning #1: Finding Circuits in GPT-2 Small with X_eTaL", "summary": "A new open-source workbench called X_eTaL-circuits, built on the TransformerLens library, finds, tests and draws the circuits inside GPT-2 Small, starting with the circuit the model uses to continue a repeated sequence, according to the author's write-up of the project. The workbench follows the causal method of the paper \"A Circuit for In-Context Learning\" (arXiv 2611.05344): it compares twin prompts differing in one controlled way, measures the margin log P(correct) - log P(incorrect) rather than right-or-wrong, and validates each head by ablation or patching against matched random controls, with circuit edges tested by path patching and checked on held-out prompts. Before applying the method to the paper's models, the author verified the workbench on GPT-2 Small's known induction heads, which let the 12-layer, 12-head-per-layer model (144 heads total) predict each next token on a repeated token list by copying what followed that token the previous time.", "body_md": "# Deep Learning #1: Finding Circuits in GPT-2 Small with X_eTaL\n\n*2677 words • 14 min read* • Abstract\n\nI 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.\n\nThis first post is about circuits: the groups of parts inside a language model that work together to produce one behavior. [X_eTaL-circuits](https://github.com/softwarewrighter/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.\n\n| Resource | Link | \n|---|---|\n| **Paper** | [A Circuit for In-Context Learning](https://arxiv.org/abs/2611.05344) (arXiv 2611.05344) | \n| **Code** | [X_eTaL-circuits](https://github.com/softwarewrighter/X_eTaL-circuits) | \n| **Live demo** | [The model map, every head, the scores](https://softwarewrighter.github.io/X_eTaL-circuits/) | \n| **In plain words** | [eli5.md](https://github.com/softwarewrighter/X_eTaL-circuits/blob/main/docs/eli5.md) | \n| **Related posts** | [In-Context Learning Revisited](https://blog.softwarewrighter.com/2026/02/22/icl-revisited-from-mystery-to-engineering/) ,[Small Models (4/6): BDH, a visible brain](https://blog.softwarewrighter.com/2026/02/03/small-models-part4-bdh/) | \n| **Related code** | [bdh](https://github.com/softwarewrighter/bdh) (my BDH) | \n| **X_eTaL** | [The language](https://github.com/softwarewrighter/X_eTaL) ,[try it in your browser](https://softwarewrighter.github.io/X_eTaL/) | \n| **The series** | [Deep Learning: every post](https://blog.softwarewrighter.com/series/#deep-learning) | \n| **Comments** | [Discord](https://discord.com/invite/Ctzk5uHggZ) | \n\n## What a circuit is\n\nA 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.\n\nNobody 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.\n\n## The method: change something, measure the margin\n\nThe workbench follows the causal method of the paper [A Circuit for In-Context Learning](https://arxiv.org/abs/2611.05344), which looks for the parts that let a model learn made-up labels from a few examples in its prompt:\n\n1. **Twin prompts** that differ in exactly one controlled way, with their tokens lined up.\n2. **A continuous measure:** the margin,`log P(correct) - log P(incorrect)` , not just right or wrong.\n3. **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.\n4. **A circuit graph** whose edges are tested by path patching, then checked on held-out prompts.\n\nAn attention map shows where a model looks. A circuit shows what the model needs.\n\nBefore 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.\n\n## Looking: where a head attends\n\nA small Python adapter runs the model through [TransformerLens](https://github.com/TransformerLensOrg/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.\n\nThe 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.\n\nThat is an observation of where the head looks. It is not evidence that the output needs it.\n\n## Scoring every head\n\nEach 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:\n\n```\nˡp̲revious ← { d →\n  A ← ᶜᵖa̲ttention d\n  n ← f̲irst r̲ev s̲hape A\n  A ʰm̲asked f̲loat (r̲ange n) '{ a b → a = b + 1 } t̲able r̲ange n\n}\n\nʰm̲asked ← { A M → 2 '{ S → ('+ r̲/ r̲avel S × M) ÷ '+ r̲/ r̲avel M } r̲ank A }\n```\n\nA 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.\n\nThe 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.\n\n## Testing: which heads the output needs\n\nEach 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.\n\nAn 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**.\n\nWhat 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.\n\n## Which head feeds which\n\nA 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:\n\n186 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.\n\nThat 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).\n\n## The model map\n\nThe 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.\n\nThe [live demo](https://softwarewrighter.github.io/X_eTaL-circuits/) 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.\n\n## Why an array language\n\nThese 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.\n\nThe rule throughout: an attention pattern is a suspect, not a culprit. Everything the workbench reports says which kind of evidence it has.\n\n## What comes next in the workbench\n\nGPT-2 Small’s induction circuit is the check that the tools work. The studies planned next:\n\n- local models through Ollama: do they learn arbitrary labels from examples, and reverse them?\n- Qwen3-4B-Base, the paper’s reference model: are its writers, readers, gate and brakes found here too?\n- X_eTaL code: does the same mechanism carry over to programming-language examples?\n- a tiny trained transformer: in what order do circuit parts appear during training?\n- Baby Dragon Hatchling (BDH), a model designed to be inspected: does it make circuits easier to find?\n\nTwo 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](https://blog.softwarewrighter.com/2026/02/22/icl-revisited-from-mystery-to-engineering/) asked from the outside; circuits are the inside view. And BDH, the [Baby Dragon Hatchling](https://github.com/pathwaycom/bdh), is a model I have already coded and written about in [Small Models (4/6): This AI Has a Visible Brain](https://blog.softwarewrighter.com/2026/02/03/small-models-part4-bdh/) ([my version](https://github.com/softwarewrighter/bdh)): 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.\n\n## Closing\n\nThis 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.\n\n*Part 1 of the Deep Learning series. [View all parts](https://blog.softwarewrighter.com/series/#deep-learning)*\n\n*Comments or questions? [SW Lab Discord](https://discord.com/invite/Ctzk5uHggZ) or [YouTube @SoftwareWrighter](https://www.youtube.com/@SoftwareWrighter).*", "url": "https://wpnews.pro/news/deep-learning-1-finding-circuits-in-gpt-2-small-with-x-etal", "canonical_source": "https://blog.softwarewrighter.com/2026/11/27/deep-learning-xetal-circuits/", "published_at": "2026-11-27 08:15:00+00:00", "updated_at": "2026-11-27 18:15:31.432318+00:00", "lang": "en", "topics": ["ai-research", "machine-learning", "large-language-models", "ai-tools", "artificial-intelligence"], "entities": ["GPT-2 Small", "X_eTaL-circuits", "X_eTaL", "TransformerLens", "A Circuit for In-Context Learning", "arXiv 2611.05344"], "also_reported_by": [], "takeaway": "Test each attention head's contribution by ablation or patching against matched random controls and score with the margin log P(correct) - log P(incorrect) rather than accuracy, then validate circuit edges with path patching on held-out prompts.", "recommendation": {"advice": "Score circuit interventions with the margin log P(correct) - log P(incorrect) instead of right-or-wrong, and compare every ablation or patch against matched random controls before trusting a head's role.", "playbook": "evals-testing", "playbook_url": "https://wpnews.pro/playbooks/evals-testing"}, "alternates": {"html": "https://wpnews.pro/news/deep-learning-1-finding-circuits-in-gpt-2-small-with-x-etal", "markdown": "https://wpnews.pro/news/deep-learning-1-finding-circuits-in-gpt-2-small-with-x-etal.md", "text": "https://wpnews.pro/news/deep-learning-1-finding-circuits-in-gpt-2-small-with-x-etal.txt", "jsonld": "https://wpnews.pro/news/deep-learning-1-finding-circuits-in-gpt-2-small-with-x-etal.jsonld"}}