liveBook · Manning

Logit lens

As explained in Rearchitecting LLMs

The logit lens is an interpretability technique that projects intermediate hidden states through a model's final normalization and prediction head to inspect layer-wise predictions.

The logit lens makes a Transformer’s evolving predictions inspectable: it projects each layer’s hidden state through the model’s final normalization and prediction head, producing a vocabulary distribution for that point in the forward pass. section 10.0 section 10.3

A typical workflow is:

  • Run the model with output_hidden_states=True. In the Llama-3.2-1B example, discard the embedding output and process the 16 Transformer-block outputs. section 10.3
  • For each layer, select the final token’s hidden state, apply the model’s final normalization and lm_head, then use softmax and top-k to inspect likely tokens and probabilities. section 10.3
Listing 10.10 The logit lens function
def logit_lens(model, tokenizer, hidden_states, topk=3):
    final_norm = model.model.norm #A
    lm_head = model.get_output_embeddings() #B
    results = []
    for hs in hidden_states[1:]: #C
        last_token_hs = hs[0, -1, :] #1
        normed = final_norm(last_token_hs)
        logits = lm_head(normed)
        probs = torch.softmax(logits.float(), dim=-1)
        top = torch.topk(probs, topk)
        tokens = [tokenizer.decode([idx]) for idx in top.indices.tolist()]
        results.append(list(zip(tokens, top.values.tolist())))
    return results

The function shows the per-layer projection and top-k steps. section 10.3

Layer-wise prediction

For “The most significant building in Barcelona is the”, a top-three-per-layer table shows how candidate tokens and their probabilities change across the network. section 10.3

Figure 10.4 Logit lens top-three tokens per layer for the prompt "The most significant building in Barcelona is the". Each cell shows the top-three predicted tokens and their probabilities for one layer, projected through the model's final normalization and lm_head. Background intensity encodes the top-1 probability, with darker cells indicating higher confidence.

The figure visualizes top-three predictions per layer, with background intensity indicating top-token probability. section 10.3

The direct lens is not reliable for every architecture; the described requirement is Pre-LayerNorm. The tuned lens instead trains an affine transformation per layer, at the cost of requiring training. section 10.4

How Rearchitecting LLMs covers it

How deep it goes
Implements hidden-state projection and top-k probability extraction, and notes Pre-LayerNorm limits without deriving a broader theory.
Examples
Uses a Barcelona-building prompt and per-layer top-three token predictions with probabilities.
What it assumes
Requires access to model hidden states, final normalization, and the prediction head.
What you can do after
Understand how candidate predictions change across layers by inspecting projected vocabulary distributions.
How it connects
Connects intermediate hidden states, final normalization, prediction heads, and the tuned lens alternative.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rearchitecting LLMs. The section links point to the parts of the book it draws on.