All courses

Logit lens: reading intermediate predictions

Apply the final norm and W_U to the residual stream r^{(ℓ)} at intermediate layers to see how the predicted next token develops through depth. Explain the method's limits and the tuned-lens fix: a small learned linear map per layer applied before unembedding.

Builds on: The residual stream as a shared channel, Unembedding: from hidden state to logits

▶ Watch this lesson Start the whole course

What the animation says

Where does Paris appear?

Give a model 'The capital of France is', and it answers Paris. But we only see the answer after the last block. Did Paris show up in block three, or block thirty? Inside, the residual stream is a running sum: each block adds its output to it. At the end, a final norm and the unembedding matrix read the next token off that sum. So here's the question: could that same reader be used on the stream right after block two?

The logit lens

Yes: every layer's state has the same d numbers, so the same reader fits. Take the stream at any layer, apply the model's own final norm, then multiply by the unembedding matrix. Softmax turns those logits into a guess: what the model would say if it stopped here. That's the logit lens. It adds no new parameters, and at the last layer it is exactly the model's output. Let's run it on a tiny model: two dimensions, three tokens.

Watching the guess change

The unembedding has one column per token: the, France, and Paris. At layer two the stream is four, minus four. The norm divides by its size, giving one, minus one. Logits: two, minus one, minus two. The lens reads 'the'. At layer six, five, five becomes one, one. Logits zero, one, zero. Now it reads 'France'. At layer twelve the stream is minus six, six. Which token wins?

Direction, not size, and the catch

Paris. Minus six, six normalizes to minus one, one, and the logits are minus two, one, two. Softmax gives Paris about seventy-two percent. Notice the sizes grew: four, five, six. The norm erased that, so only direction decides the reading. But this reader was trained only on the last layer. An early layer might hold Paris in a direction that later blocks still rotate. The lens would read it wrong. So when an early reading looks wrong, it may be the lens, not the model.

Tuned lens: one small map per layer

The tuned lens fixes this: each layer gets its own learned matrix A and bias b, applied before the norm. They start as identity and zero, so it begins as the plain logit lens. Then they're trained, model frozen, to match the final output. Suppose layer six stored Paris turned by a right angle. Its map learns to turn it back: five, five becomes minus five, five, reading Paris. In code, every layer is read at once. Tap the two lines that make this a plain logit lens.

Where in the depth does the answer appear?

Given *'The capital of France is'*, a model outputs ' Paris', but you only see what comes out after all $L$ blocks. Did Paris appear in block 3 or block 30? Was there a wrong guess first? Can we pause the computation at any layer and read off what the model would predict *right now*, without training anything new? That would show how a prediction is built, layer by layer.

Reuse the model's own reader, early

The residual stream is a running sum. Each block adds its output, and the final norm plus $W_U$ read the prediction off $r^{(L)}$. Every $r^{(\ell)}$ lives in the same $d$-dimensional space, so you can apply that same reader early. It asks: *what would the model say if it stopped here?* The catch: that reader was trained only on the last layer. Earlier layers may store information in directions or scales that later blocks still transform. Early readings can then be garbage.

Logit lens and tuned lens

Logit lens at layer $\ell$, position $t$: $$z^{(\ell)}_t=\mathrm{Norm}_f(r^{(\ell)}_t)\,W_U,\qquad p^{(\ell)}_t=\mathrm{softmax}(z^{(\ell)}_t)$$ $\mathrm{Norm}_f$ is the model's final LayerNorm/RMSNorm, including its learned gain. The logit lens adds no parameters. At $\ell=L$ it is exactly the model's output. Tuned lens: insert a learned map first: $$z^{(\ell)}_t=\mathrm{Norm}_f(r^{(\ell)}_tA_\ell+b_\ell)\,W_U$$ Here $A_\ell\in\mathbb R^{d\times d}$ and $b_\ell\in\mathbb R^d$. They start at identity and zero, so the tuned lens begins as the plain logit lens. They are then trained with the model frozen, so that $p^{(\ell)}_t$ matches the model's actual output distribution $p_t=p^{(L)}_t$.

Watching ' Paris' form

Let $d=2$, with tokens (' the', ' France', ' Paris') and $W_U=\begin{bmatrix}1&0&-1\\-1&1&1\end{bmatrix}$. $\mathrm{Norm}_f$ is RMSNorm with gain 1. - $r^{(2)}=[4,-4]$ → $[1,-1]$ → logits $[2,-1,-2]$: ' the' - $r^{(6)}=[5,5]$ → $[1,1]$ → $[0,1,0]$: ' France' - $r^{(12)}=[-6,6]$ → $[-1,1]$ → $[-2,1,2]$: ' Paris', with $p=\frac{e^2}{e^{-2}+e^{1}+e^{2}}\approx0.72$ The norm strips out the growing scale (4, 5, 6), so only direction decides the reading.

Lens over every layer at once

Stack the recorded $r^{(0)}\dots r^{(L)}$ as `R` with shape [L+1, T, d]. Batched matmuls then read all layers at once: first by each layer's `A`, then the norm, then by `W_U`. With `A` = identity and `b` = 0 this is the plain logit lens. A trained tuned lens just swaps in the learned `A` and `b`. In real use, `R` comes from a model's forward pass; here random running sums stand in.
import numpy as np
rng = np.random.default_rng(0)
L, T, d, V = 4, 3, 8, 10
R = rng.normal(size=(L+1, T, d)).cumsum(0)  # stand-in for r^(0..L)
W_U = rng.normal(size=(d, V)); g = np.ones(d)  # unembedding, final-norm gain
A = np.stack([np.eye(d)] * (L+1)); b = np.zeros((L+1, 1, d))  # identity = logit lens
def norm_f(x): return g * x / np.sqrt((x**2).mean(-1, keepdims=True) + 1e-6)
Z = norm_f(R @ A + b) @ W_U  # [L+1, T, V] logits at every depth
P = np.exp(Z - Z.max(-1, keepdims=True)); P /= P.sum(-1, keepdims=True)
print(P.argmax(-1))  # top token id: rows = layer, cols = position
print(P[:, -1].max(-1).round(2))  # last position's confidence by depth

Remember

The logit lens decodes any intermediate $r^{(\ell)}$ with the model's own final norm and $W_U$, showing how the next-token guess develops through depth. It assumes early layers already use the final layer's coordinates. When they don't, early readings mislead. A tuned lens fixes this by learning one small linear map per layer that translates the state before unembedding.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.