Overview

10 Exploring the transformer black box

In this chapter, the model is treated less like a black box and more like a system whose internal signals can be observed and compared. The core idea is to use hooks to capture activations from selected transformer submodules, especially the MLP path, and then turn those tensors into readable views of how information moves through the network. Because transformer blocks are hierarchical and model families name their parts differently, the chapter also emphasizes how to find the right hook points in architectures such as Llama, Gemma, and Qwen, while accounting for details like forward hooks versus forward pre-hooks when the goal is to inspect an expanded representation before it is compressed again.

Once the activations are available, the chapter builds a practical visualization toolkit to make them interpretable. Heatmaps show neuron activity either for one layer across tokens or across all layers after averaging tokens, revealing how different words trigger different response patterns and how those patterns evolve with depth. The GLU mechanism is then inspected in action by comparing the activation function output, the up projection, and the input to the down projection side by side, making clear that the elementwise product acts as a filter rather than a simple amplifier. These visualizations are designed not just to describe one prompt, but to provide a reusable method for later investigations of behavior and bias.

The final perspective comes from the logit lens, which projects hidden states from each layer through the model’s final normalization and output head to show how a prediction gradually forms. Early layers produce noisy or incoherent guesses, intermediate layers begin to settle on semantically related candidates, and the last layers crystallize a coherent answer. The chapter closes by connecting these lightweight interpretability tools to broader mechanistic interpretability work, contrasting the accessible logit lens with more resource-intensive approaches like sparse autoencoders, and positioning the whole toolkit as a practical foundation for future model analysis and intervention.

Token × neuron activation heatmap for act_fn_layer_12 on the prompt "The cat sleeps". Each row is a token, each column is a bin of 256 neurons averaged together. Activation intensity varies visibly across tokens: "cat" shows consistently stronger responses across most neuron bins, while "The" and "sleeps" remain more uniform. The BOS token has been excluded from the visualization.
Layer × neuron heatmap for act_fn on the prompt "The cat sleeps", averaged over tokens. Each row is a transformer layer, each column a bin of 256 neurons. The BOS token has been excluded before averaging.
GLU three-panel visualization for layer 14 on the prompt "The cat sleeps". From left to right: act_fn (SiLU(gate)), up_proj, and down_proj_input (SiLU(gate) × up). Each panel shares the same token axis but has its own independent color scale. The BOS token has been excluded.
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.

Summary

  • PyTorch hooks let you capture a model's internal activations by attaching to the input or output of any submodule via forward hooks and forward pre-hooks.
  • The internal structure of a Transformer block is hierarchical and can be explored separately. Although submodule names vary between model families (self_attn versus linear_attn, for example), the MLP module keeps the same structure in Llama, Gemma, and Qwen, which lets you apply the same capture techniques consistently.
  • Activation heatmaps and grouping neurons into bins turn tensors of thousands of values into a manageable visual representation.
  • The GLU mechanism can be observed in action on real activations. Visualizing its three components side by side shows that the multiplication between act_fn and up_proj does not amplify the signal, it filters and selects it.
  • The logit lens technique extracts hidden states from intermediate layers and projects them through the final normalization and the prediction head. It lets you visualize how the model builds its output layer by layer.
  • Direct activation interpretability offers a viable alternative for resource-constrained environments compared to computationally expensive methods like sparse autoencoders.

FAQ

What is the purpose of using hooks in this chapter?Hooks let us capture internal activations from specific Transformer submodules during a forward pass, so we can inspect how information flows through the model without retraining or modifying its weights.
Which parts of a Transformer block can be hooked for inspection?The chapter shows that a block exposes multiple hook points, including the attention module, the MLP module, layer norms, and internal submodules such as gate_proj, up_proj, down_proj, and attention projections like q_proj, k_proj, and v_proj.
Why does the chapter focus on the MLP module rather than attention for most examples?The MLP structure is consistent across the model families discussed, while attention module names and internals vary more. That makes the MLP a more reliable target for cross-model activation analysis.
What is the difference between a forward hook and a forward pre-hook?A forward hook captures the output of a layer after it has processed its input, while a forward pre-hook captures the input before the layer transforms it. The chapter uses a pre-hook for down_proj_input so it can observe the expanded GLU representation before compression.
Why is down_proj_input captured with a pre-hook instead of a normal hook?Because the goal is to inspect the expanded MLP signal right before down_proj compresses it back to the model dimension. Capturing the input preserves the full GLU intermediate state.
Why are activation heatmaps useful in this chapter?Heatmaps turn large activation tensors into readable visual summaries, showing which tokens activate which neuron bins and how activation patterns change across tokens or across layers.
Why does the chapter discard the BOS token before visualizing activations?The BOS token often behaves like an attention sink and can have unusually large activations that distort the color scale, so it is removed to make the heatmap reflect the semantic content of the prompt more clearly.
What does the layer-by-neuron heatmap reveal that the token-by-neuron heatmap does not?The layer-by-neuron heatmap averages over tokens and shows how activation intensity evolves through depth, making it easier to see which layers become more active or more output-oriented as the model processes information.
What does the GLU three-panel visualization show?It compares act_fn, up_proj, and down_proj_input side by side for the same prompt and layer, so you can see how the gate signal and the up projection combine before reaching down_proj.
What is the logit lens and what does it help us understand?The logit lens projects each layer’s hidden state through the final normalization and output head to estimate the model’s likely next-token predictions at each depth. It helps show how the answer gradually forms from noisy early-layer guesses into a coherent final prediction.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rearchitecting LLMs ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rearchitecting LLMs ebook for free