Skip to content

7.3 Introduction to interpretability

ready≈ 12 hoursNeeds: 4.6, 1.2
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Logit lens, Attention head viewer)
  4. 4 Do
  5. 5 Check

The residual stream is a sum. In a pre-norm transformer (4.5) every attention and MLP sublayer reads the stream and adds its output back. So the stream after the last block is the token embedding plus one write per sublayer, and since each head’s write is its attention pattern times its values times its slice of the output matrix, it is a sum over heads too (Elhage et al. 2021, “the residual stream” and “attention heads are independent and additive”). That additivity is what makes the tools below work.

The logit lens. Apply the final norm and the output layer to the stream after an intermediate block, as if it were the last, and see what the model would predict there (nostalgebraist 2020). In GPT-2 the prediction sharpens layer by layer. In other model families (GPT-Neo, BLOOM, OPT, even small ones) early streams are not in the output layer’s basis yet, and the tuned lens fits a small map per layer to correct for that (Belrose et al. 2023).

Heads you can recognise. A head’s attention pattern says where it reads from. A previous-token head attends from each position to the one before. An induction head finds the previous occurrence of the current token and attends to the token after it, so a repeated sequence can be copied; it needs a previous-token head in an earlier layer to tell it what came before each token. Olsson et al. 2022 argue that this circuit drives much of in-context learning. To test whether a head matters, ablate it: zero its output and measure the change in loss.

Probes and their controls. A linear probe is a linear model fitted to predict a feature from the stream at one layer. High accuracy shows the feature is linearly readable there, not that the model uses it. Compare against controls: the same probe on the embedding and on an untrained model, and on a control task with random labels per word type (Hewitt and Liang 2019, section 2). To show use, intervene, as with ablation.

The stream is a sum, so the logits split into partsstep 1 of 5
  1. xℓ+1=xℓ+Attnℓ(N(xℓ))+MLPℓ(⋅)x_{\ell+1} = x_\ell + \mathrm{Attn}_\ell(\mathrm{N}(x_\ell)) + \mathrm{MLP}_\ell(\cdot)

Logit lens

When a prediction forms

Static view on small screens. Open this page on a laptop to interact.

input
embeddingROMEO:↵But␣soft,␣what␣light␣through␣yonder␣window␣breaks?
after block 1DIEEO↵Aut␣mtnfs↵mhittsinhtsmhiyunhtmyneenethttsnatuoatet↵
after block 2 (output)YSEO:↵Iet␣to␣t␣␣that␣tokht␣theough␣touder␣titd␣nnteeak␣␣↵
actual nextOMEO:↵But␣soft,␣what␣light␣through␣yonder␣window␣breaks?
After “t” (position 10) the next character is “␣”. Top 5 at each layer: embedding: t 100% c 0% s 0% n 0% d 0% (the actual next: 0.0%)after block 1: ␣ 32% t 23% y 7% ↵ 7% s 7% (the actual next: 31.9%)after block 2 (output): ␣ 80% , 17% ! 0% c 0% f 0% (the actual next: 80.5%) Mean loss per character: embedding 13.86, after block 1 2.40, after block 2 (output) 1.41 nats.

The stream after the embedding alone, read through the tied output layer, mostly predicts the current character again, because each character's embedding is most similar to itself. Each block adds context. Click a character to see how its prediction sharpens; cells whose top prediction is right are outlined.

A table for a line of text run through the bundled two-layer character GPT. Columns are positions; rows show the top predicted next character read from the stream after the embedding, after block 1 and after block 2, shaded by the probability given to the actual next character, with a last row for the actual next character. Selecting a position lists the top five predictions at each layer and the actual next character’s probability; the readout also gives the mean loss per layer. Keyboard: Tab to the text box and the characters; press Enter to select a position.

Attention head viewer

What heads attend to

Static view on small screens. Open this page on a laptop to interact.

keys →queries →
query 41: “o” strongest key 41: “o” at 45% (itself) this head's top key is the previous character for 7% of queries, itself for 17%

The same tiny GPT as the decoding playground: 2 layers of 4 heads. Click a character to make it the query; the strip and the outlined matrix row show where it looks. Nothing after the query is visible, because of the causal mask. On Shakespeare most of these heads look one or two characters back: with 64 dimensions and a character vocabulary, the first job is to read the current word. Layer 1 head 4 is close to a pure previous-character head. Layer 2 head 4 is the most spread out and often lands on the last space or newline, the start of the current word or line. Heads in large models do more varied things; reading them is the subject of 7.3.

The attention head viewer from 4.6, on the same model: choose a layer and head and a query character to see which earlier characters it attends to, with how often the head’s strongest key is the previous character. Look for a previous-token head in layer 1. Keyboard: Tab to the layer and head buttons and the characters; press Enter to select.

Logit lens, attention patterns, ablation and probes: edit exercises/stubs/m7_3_interp.py on your own machine. This task needs PyTorch, which the in-page runner does not have.

pip install torch   # CPU build is enough for the tests
mlc check 7.3
Hints · attention_pattern 0/3 revealed
Hints · run_with_ablation 0/3 revealed
Hints · ridge_probe 0/3 revealed

Then run the lab. Open notebooks/7.3-interpretability.ipynb (about 5 minutes on a CPU) and paste in your 7.3 file. It trains a 4-layer Shakespeare GPT and reads it with the logit lens; probes every layer for the characters one, two and three positions back, against an untrained control; then trains a 2-layer GPT to copy repeated segments, finds its previous-token and induction heads, and ablates them.

1. What does the logit lens compute at layer ℓ?
2. How does an induction head predict the next token in "… A B … A"?
3. A probe reads a feature from layer 3 with 95% accuracy. What has that shown?
4. Why can a transformer's logits be split into one term per head and MLP?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.