Logit Lens › How the Logit Lens Works

Michael BrenndoerferFebruary 12, 20266 min read
Language AI HandbookAll chapters
Part 59 of 67Interpretability
Chapter 452Logit Lens › How the Logit Lens Works
Reading section 3 of 12 · How the Logit Lens Works · 6 minSection 3 of 12 · 6 min

Book contents

Section 3 of 12 · 6 min read

Logit Lens › How the Logit Lens WorksOne section · Sections in this chapter3 / 12 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The core idea is elegantly simple: transformers end with a specific structure that converts hidden states to vocabulary probabilities, and we can apply that same structure to any intermediate hidden state.

The Transformer Output Pipeline

Before diving into the logit lens, it helps to understand exactly how the final layer of a transformer produces a probability distribution over the vocabulary.

A standard transformer language model maintains a hidden state h∈Rd\mathbf{h} \in \mathbb{R}^d for each token position, where dd is the hidden dimension, also called the model dimension or dmodeld_{\text{model}}. After all transformer layers have processed the sequence, the final hidden state passes through two components to produce a prediction.

The first component is layer normalization. The final hidden state h(L)\mathbf{h}^{(L)} is normalized to have zero mean and unit variance across its dd dimensions, then rescaled by learned parameters:

h^=LayerNorm(h(L))=h(L)−μσ⋅γ+β\hat{\mathbf{h}} = \text{LayerNorm}(\mathbf{h}^{(L)}) = \frac{\mathbf{h}^{(L)} - \mu}{\sigma} \cdot \boldsymbol{\gamma} + \boldsymbol{\beta}

where:

  • μ=1d∑i=1dhi(L)\mu = \frac{1}{d} \sum_{i=1}^{d} h_i^{(L)} is the mean of the hidden state components
  • σ=1d∑i=1d(hi(L)−μ)2+ϵ\sigma = \sqrt{\frac{1}{d} \sum_{i=1}^{d} (h_i^{(L)} - \mu)^2 + \epsilon} is the standard deviation with a small stability constant ϵ\epsilon
  • γ∈Rd\boldsymbol{\gamma} \in \mathbb{R}^d and β∈Rd\boldsymbol{\beta} \in \mathbb{R}^d are learned scale and shift parameters

The second component is the unembedding matrix multiplication:

logits=WUh^\mathbf{logits} = W_U \hat{\mathbf{h}}

where WU∈RV×dW_U \in \mathbb{R}^{V \times d} is the unembedding matrix and VV is the vocabulary size.

The resulting logits vector logits∈RV\mathbf{logits} \in \mathbb{R}^V assigns a raw score to each vocabulary token. Applying softmax converts these to probabilities:

P(token=v∣h(L))=exp⁡(logitsv)∑v′=1Vexp⁡(logitsv′)P(\text{token} = v \mid \mathbf{h}^{(L)}) = \frac{\exp(\mathbf{logits}_v)}{\sum_{v'=1}^{V} \exp(\mathbf{logits}_{v'})}

where:

  • logitsv\mathbf{logits}_v is the score for vocabulary token vv
  • VV is the total vocabulary size
  • The denominator normalizes the scores to form a valid probability distribution over all tokens

This pipeline, LayerNorm followed by unembedding followed by softmax, is the entire machinery that converts a hidden state to a next-token prediction. The logit lens borrows this machinery and applies it to every layer, including intermediate ones.

Applying the Final-Layer Decoder to Intermediate States

The logit lens applies this same final-layer decoder, consisting of LayerNorm followed by the unembedding matrix, to the hidden state at each intermediate layer. For a model with LL layers, the forward pass produces a sequence of hidden states at each token position:

h(0),h(1),h(2),…,h(L)\mathbf{h}^{(0)}, \mathbf{h}^{(1)}, \mathbf{h}^{(2)}, \ldots, \mathbf{h}^{(L)}

where h(0)\mathbf{h}^{(0)} is the embedding layer output (token embedding plus positional encoding, before any transformer layers) and h(L)\mathbf{h}^{(L)} is the final hidden state that produces the model's output.

The logit lens at layer ℓ\ell is defined as:

LogitLens(ℓ)(h(ℓ))=WU⋅LayerNorm(h(ℓ))\text{LogitLens}^{(\ell)}(\mathbf{h}^{(\ell)}) = W_U \cdot \text{LayerNorm}(\mathbf{h}^{(\ell)})

where:

  • h(ℓ)∈Rd\mathbf{h}^{(\ell)} \in \mathbb{R}^d is the hidden state after the ℓ\ell-th transformer layer
  • LayerNorm\text{LayerNorm} uses the same learned parameters (γ\boldsymbol{\gamma}, β\boldsymbol{\beta}) as the model's actual final layer normalization
  • WU∈RV×dW_U \in \mathbb{R}^{V \times d} is the fixed unembedding matrix, shared across all layers
  • The result is a vector of logits ∈RV\in \mathbb{R}^V over the vocabulary, which can be converted to probabilities via softmax

This gives us a probability distribution over the vocabulary at every single layer, which we can inspect and visualize across layers. We are not re-running the model from each intermediate state; we are simply applying the output machinery at different points in the processing pipeline.

Why Layer Normalization Matters

The logit lens applies LayerNorm before unembedding because the unembedding matrix WUW_U was trained assuming normalized inputs. Without LayerNorm, the raw hidden states (which can have varying magnitudes across layers) produce distorted logit distributions. The layer norm effectively "prepares" the hidden state for the unembedding, mimicking what the model does at its actual output.

The Residual Stream View

The logit lens connects naturally to the residual stream view of transformers. In a transformer with pre-norm architecture (like GPT-2 and most modern language models), each layer adds a delta to the residual stream:

h(ℓ)=h(ℓ−1)+Δattn(ℓ)+Δffn(ℓ)\mathbf{h}^{(\ell)} = \mathbf{h}^{(\ell-1)} + \Delta_{\text{attn}}^{(\ell)} + \Delta_{\text{ffn}}^{(\ell)}

where Δattn(ℓ)\Delta_{\text{attn}}^{(\ell)} is the contribution from the multi-head attention sublayer and Δffn(ℓ)\Delta_{\text{ffn}}^{(\ell)} is the contribution from the feed-forward network sublayer at layer ℓ\ell. Because each layer adds to the same residual stream rather than replacing it, the final hidden state h(L)\mathbf{h}^{(L)} is the sum of contributions from all layers plus the initial embedding.

The logit lens at any intermediate layer ℓ\ell shows what prediction is implied by the partial sum of contributions accumulated up to that layer. In this sense, the logit lens acts as a running tally of how the cumulative residual stream converges toward the final prediction. This makes the technique especially natural for residual-stream architectures: intermediate states live in the same geometric space as the final state, and the unembedding matrix was trained to decode from this space.

Weight Tying and the Geometric Interpretation

In many transformer language models, including GPT-2, GPT-Neo, and others, the token embedding matrix WE∈RV×dW_E \in \mathbb{R}^{V \times d} and the unembedding matrix WU∈RV×dW_U \in \mathbb{R}^{V \times d} are the same matrix. This is called weight tying, and it was originally introduced to reduce parameter count and improve sample efficiency. A convenient side effect is that it gives the logit lens a clean geometric interpretation.

When WU=WEW_U = W_E, the logit for token vv at layer ℓ\ell becomes:

logitv(ℓ)=ev⋅LayerNorm(h(ℓ))\text{logit}_{v}^{(\ell)} = \mathbf{e}_v \cdot \text{LayerNorm}(\mathbf{h}^{(\ell)})

where:

  • ev∈Rd\mathbf{e}_v \in \mathbb{R}^d is the embedding vector for vocabulary token vv
  • The dot product measures geometric alignment between the normalized hidden state and each token's embedding vector

A high logit for token vv means the hidden state at that layer is geometrically close to vv's embedding in the dd-dimensional representation space. This is an intuitive way to think about what "the model is thinking" at each layer: the model implicitly compares its current partial computation to every token it knows and reports which ones are most similar. Early layers produce hidden states that are close to many tokens' embeddings (high uncertainty). Later layers produce hidden states that concentrate geometric similarity onto the correct answer token (low uncertainty).

For models without weight tying, such as some larger-scale transformers where weight tying is removed for training stability, this geometric interpretation does not apply directly, but the operational definition of the logit lens (applying LayerNorm then WUW_U) remains valid.

What the Logit Lens Is Not

The logit lens shows you an implicit prediction at each layer, but it does not show you the hidden state itself, the attention patterns at that layer, or what computation that layer performed. It is a read-out mechanism, not a causal analysis. The token that ranks highest at layer 7 was not necessarily computed at layer 7; it may have been present in the residual stream since layer 3, with layer 7 simply leaving it undisturbed.

This distinction matters for interpretation. When you see "Paris" emerge as a top prediction in a late layer, you should read that as "by this point, the accumulated computation is pointing toward Paris." You should not read it as "this layer is the one that retrieved the Paris fact." Establishing causal responsibility requires different tools, which we cover in the Activation Patching chapter.