Module 10

Interpretability foundations

The transformer from module 9 sorts lists almost perfectly, and nobody told it how. This module introduces four standard tools for finding out what a trained network is doing, and uses each of them on that model.

Why it matters

Training gives you a model that works and no account of why. For a list sorter that is a curiosity. For a large language model it is a practical problem: you would like to know what the model has worked out before it answers, which parts of it are responsible for an answer, and whether what it says matches what it computed. Interpretability is the study of those questions, and most of its methods fit in a few lines of code. The four in this module are the ones the rest of the field builds on, and the last module of the course, on the Jacobian lens, starts from two of them.

The idea

Where to look

Recall the residual stream from module 9: one vector of $d = 32$ numbers per position, which every layer reads and adds to. Write $h_{\ell,t}$ for that vector at position $t$ after layer $\ell$, with $h_{0,t}$ the embeddings and $h_{2,t}$ the vector the model's output is computed from. Everything a later layer knows about earlier tokens has to pass through these vectors, so they are the natural place to look. Each tool below asks a different question of them.

Linear probes: is a piece of information there?

Pick a property of the input, for example the largest digit in the list. Collect $h_{\ell,t}$ for many inputs, and train a classifier to predict the property from the vector alone, keeping the transformer frozen. If the classifier is linear, it is called a linear probe, and it is exactly the multinomial logistic regression of module 1:

$$\hat{p} = \operatorname{softmax}\big(h_{\ell,t}\, W + b\big), \qquad W \in \mathbb{R}^{d \times C},$$

trained with cross-entropy on some lists and scored on others. High accuracy means the property can be read from that layer by a linear map. Two cautions come with every probe. First, compare against a baseline: a probe that always guesses the commonest value can look respectable. Second, a probe shows that information is present, not that the model uses it. That needs an intervention, which is what patching is for.

The logit lens: what would the model say now?

The model turns its final vector into next-token scores with one normalisation and the unembedding matrix. Because layers only add to the stream, nothing stops you from applying that same readout early, to the vector after any layer:

$$\operatorname{LogitLens}(h_{\ell,t}) = \operatorname{softmax}\big(\operatorname{LN}_{\text{final}}(h_{\ell,t})\; W_U\big).$$

This is the logit lens. It needs no training, and it gives a running commentary: at each layer, the token the model would output if it stopped there. Its weakness is the assumption built into it, that intermediate layers already write in the directions the unembedding reads. Where a later layer still has to move or rotate the information, the logit lens misses it. Module 11 is about repairing exactly that.

Activation patching: which part is responsible?

To test whether a component matters, change what it outputs and watch the answer. Run the model on a clean input and on a second, different input. Then run the clean input again, but at one component (here, one attention head) substitute the output it produced on the second input, and let everything downstream proceed:

$$\text{effect of head } (\ell, k) \;=\; \text{output}\big(\text{clean}\big) \;-\; \text{output}\big(\text{clean} \mid \text{head}_{\ell,k} \leftarrow \text{head}_{\ell,k}(\text{other})\big).$$

If the answer does not change, the head was not carrying anything about this input that the answer depends on. If the answer moves towards what would be right for the other input, the head is on the causal path. This is activation patching. Substituting a real activation from another input, and not zero, is deliberate: the rest of the network has never seen a head output exactly zero, so zeroing tests the network in a situation it was not trained for and usually understates or misstates the head's role.

Sparse autoencoders: what are the directions?

Probes need you to guess the property in advance. A sparse autoencoder tries to let the activations speak for themselves. It is the autoencoder of module 8 with two changes: the hidden layer is wider than the input (here 64 units for a 32-dimensional stream), and a penalty pushes most hidden units to zero on any given input:

$$f = \operatorname{ReLU}\big((h - b_d)\, W_e + b_e\big), \qquad \hat{h} = f\, W_d + b_d, \qquad \text{loss} = \lVert h - \hat{h} \rVert^2 + \lambda \sum_i f_i ,$$

with each row of $W_d$ held at length 1. Every hidden unit, or feature, then stands for one direction in the residual stream (its row of $W_d$), and each activation vector is rebuilt as a positive combination of a few of them. The hope is that such features line up with things the model represents, more cleanly than individual coordinates do. Whether they do has to be checked feature by feature, by looking at the inputs that make each one fire. $\lambda$ sets the trade: larger means fewer active features and a worse reconstruction.

Live demos: four tools, one model

All four demos use the pretrained weights from module 9 (two layers, two heads per layer, 17,920 parameters), loaded when this page opens. Row labels emb, L1 and L2 mean the residual stream after the embeddings, after layer 1 and after layer 2.

1 · The logit lens

What each layer would output, position by position

Over 500 held-out lists

Columns are the model's input: your list, the separator, then the first four digits of the sorted answer. Each cell shows the token the logit lens reads there and its probability in percent; stronger colour means more confident. A solid underline marks a correct next token and a dotted one a wrong one. Under the five digits of the list itself the model was never trained to predict anything, so those cells are not marked.

2 · Linear probes

What the probe tries to read

One probe per layer, each a 32-to-10 logistic regression trained on residual-stream vectors from training lists and scored on 500 held-out lists. Next digit is read at the five answer positions; the other two are read at the separator, before any of the answer has been written.

3 · Activation patching, one head at a time

Clean list (the one the model is asked to sort)
Other list (where the patched head's output comes from)
Head replacedAnswer for the clean listDigits rightProbability on the right digits

Averaged over many pairs

Each row runs the model on the clean list with one head's output, at every position, swapped for what that head produced on the other list. The answer is read at the five answer positions with the correct earlier digits supplied, as in training. Wrong digits are dotted-underlined.

4 · A sparse autoencoder on the middle of the model

Sparsity penalty λ

Press the button to train a 32 → 64 → 32 sparse autoencoder on the residual stream after layer 1. It takes a few seconds.

FeatureIts strongest activations shareHow many of the top 40Fires on

    The training data are 7,500 residual-stream vectors: layer 1's output at the five answer positions of 1,500 lists. Each feature is labelled automatically by checking what its 40 strongest activations have in common among three candidates: the digit about to be written, which answer position it is, and the token at that position. In the examples, the highlighted token is the position where the feature fired and the arrow shows the digit that comes next.

    Try this

    1. In the logit lens, enter 3 1 4 1 5 and read down a column in the answer half. The embeddings have no idea, layer 1 is often right but unsure, and layer 2 is the model's actual output. The bars below show the same thing in aggregate: roughly 20%, 57% and 100%.
    2. Train the Next digit probes. After layer 1 a trained probe reads the next digit almost perfectly, while the logit lens managed little more than half. The information is there; it is not yet written in the directions the unembedding reads.
    3. Switch to Largest digit and train again. At the embeddings the probe does no better than guessing the commonest value, since the separator's own embedding knows nothing about the list. After layer 1 it is around 90%, and after layer 2 it has dropped: the model gathered the information and then stopped keeping it in a linearly readable form once it was no longer needed.
    4. In the patching demo, press Random pair a few times and watch which rows change. Then press Average over 200 random pairs. One head in layer 1 hurts the answer most when replaced. Compare the two columns: setting a head to zero does much less damage than giving it another list's output, and would have made every head look less important.
    5. Train the sparse autoencoder at λ = 0.5, then at 0.2 and 1.0. Note the trade between variance explained and the number of active features. Click through the feature table: many features have a clean label, some do not, and the labels are only as good as the three candidates the page checks.

    What this small model can and cannot show. With two layers there is exactly one residual stream that is neither the input nor the output, so “across layers” here means three rows. The sparse autoencoder is far from sparse by the standards of work on language models: around a fifth of its features are active at once, because a 32-dimensional stream at five positions does not hold many independent things. The methods are the real ones. The scale is not.

    Exercise

    Train a probe; find the head that patching breaks

    The pretrained transformer is provided in NumPy as run_model (open “Provided code” to read it). Write a linear probe trained by gradient descent, then use activation patching to measure how much each attention head matters, and return the head whose replacement damages the answer most. In the code, layers and heads are counted from 0, so the demo's “layer 1, head 2” is (0, 1).

    Check yourself

    A probe reads some property from layer 1 with 99% accuracy. What does that establish, and what does it leave open?

    It establishes that the property is linearly decodable from that layer's activations, provided the probe was scored on held-out data and beats a sensible baseline. It leaves open whether the model uses that information: a property can be decodable simply because it is correlated with things the model does use. Showing use takes an intervention, such as patching or editing the activation and seeing the output change.

    Why patch a head with its output on another input, and not simply set it to zero?

    The layers after the head were trained with that head producing outputs of a typical size and shape. Zero is not one of them, so zeroing measures how the network copes with an input it has never seen, which is a different question from what the head contributes. An output taken from another input is a realistic value that removes only what was specific to the clean input. In the demo the two give clearly different pictures of the same four heads.

    In a sparse autoencoder, what happens as λ is raised, and why is the hidden layer wider than the input?

    A larger λ makes each active feature more costly, so fewer fire on each input and the reconstruction gets worse; at the extreme, features die and never fire. The hidden layer is wider because the aim is not compression. A network may represent more distinct things than it has dimensions, by using directions that are not orthogonal; a wide, sparse layer gives each of them a chance of getting a feature to itself.

    Where this leads

    The logit lens was right about the middle of this model little more than half the time, although a trained probe showed the answer was there. Module 11 builds the Jacobian lens, which keeps the logit lens's readout but first asks the model itself how each layer's vectors reach the final layer.

    Sources: G. Alain and Y. Bengio, “Understanding intermediate layers using linear classifier probes”, arXiv:1610.01644 (2016); J. Hewitt and P. Liang, “Designing and interpreting probes with control tasks”, EMNLP (2019), on baselines for probes; nostalgebraist, “Interpreting GPT: the logit lens”, LessWrong (2020); J. Vig and others, “Investigating gender bias in language models using causal mediation analysis”, NeurIPS (2020), and K. Meng, D. Bau, A. Andonian and Y. Belinkov, “Locating and editing factual associations in GPT”, NeurIPS (2022), for activation patching; S. Heimersheim and N. Nanda, “How to use and interpret activation patching”, arXiv:2404.15255 (2024), on zero against resampled replacements; T. Bricken and others, “Towards monosemanticity: decomposing language models with dictionary learning”, Transformer Circuits Thread (2023), and H. Cunningham, A. Ewart, L. Riggs, R. Huben and L. Sharkey, “Sparse autoencoders find highly interpretable features in language models”, arXiv:2309.08600 (2023), for sparse autoencoders.