Skip to content
NotebookLive

The logit lens in PyTorch: a hands-on GPT-2 tutorial

Watch GPT-2 build its prediction layer by layer. A runnable walkthrough that decodes the residual stream after every block, shows why ln_final matters, and plots top-k and rank heatmaps.

Published
Length
15 min read
Topics
  • Interpretability
  • Logit lens
  • PyTorch
  • TransformerLens
  • GPT-2

The idea

GPT-2 is a stack of blocks. Each token carries a vector, the residual stream (768 numbers in gpt2-small), and every block reads it and adds its result back. Normally only the last vector becomes a prediction. The logit lens applies the same vector-to-word-scores step to every intermediate vector, so you can see when the model figures out the next token.

What it covers

  • Setup: load GPT-2 with its original weights and poke at W_E and W_U
  • Tokens and one forward pass, with honest labels for byte fragments
  • The residual stream: stack every layer, and why raw vectors can't be compared
  • Normalization and the first lens: plain norm vs ln_final
  • Comparing lenses by agreement and rank
  • A reusable run_example(text) pipeline
  • Top-k and rank heatmaps, including Nepali text in Devanagari

Try it without the notebook

The same lens, extended with trajectories and direct logit attribution, is the logit lens lab in attnlab. It reproduces this notebook number for number.

Questions

What is the logit lens?
An interpretability technique that decodes a transformer's intermediate residual stream with the model's own final LayerNorm and unembedding, as if each layer were the last. It shows which next token the model favours at every layer.
Why apply ln_final before unembedding?
The unembedding was trained on vectors that had passed through the final LayerNorm. Intermediate residual vectors have very different scales, so decoding them raw gives misleading answers; ln_final puts them on the scale W_U expects.
What is the difference between the logit lens and the tuned lens?
The logit lens reuses the model's final layers unchanged at every depth. The tuned lens (Belrose et al., 2023) learns a small affine correction per layer first, which makes early-layer predictions more reliable.
What do I need to run the tutorial?
Python with PyTorch, TransformerLens, NumPy and matplotlib. GPT-2 small runs on a laptop CPU. To skip the setup, the logit lens lab in attnlab runs the same analysis in the browser.

Credits

The logit lens in PyTorch stands on open-source work and published research. Thank you to everyone behind it.

Built with

Background

More from Sarvabhaum

Web appLive

attnlab

A set of labs, walked in order, that run real TransformerLens models behind a web page. Type a prompt, pick a model, and see how it is tokenized, where every head attends, and when the model knows its answer.

  • Mechanistic interpretability
  • TransformerLens
  • Attention
InteractiveLive

The geometry of refusal

Somewhere in the middle of a chat model, the residual stream decides whether to help you or say "I can't." A rotatable globe and measured numbers from the papers' own code show one refusal direction, a separate harmfulness direction, and where the 3D picture stops being a good guide.

  • Interpretability
  • AI safety
  • Refusal
InteractiveLive

Do Llamas think in English?

Ask Llama-2 to translate fleur into Chinese and its middle layers say flower, in English, before the last layers settle on 花. A rotatable sphere shows why: the hidden state moves from input space through an English-leaning concept space to the output language.

  • Interpretability
  • Multilingual
  • Logit lens