Skip to content
InteractiveLive

A transformer block, one matrix at a time

Follow four tokens through a single transformer block from the residual stream's point of view. Every matrix is shown with real numbers, computed live, so you can trace one token's row from embedding to next-token probabilities.

Published
Topics
  • Transformers
  • Residual stream
  • Attention
  • Interactive

The ten steps

The residual stream (d_model numbers per token) is the one thing every part of the block reads from and writes to. The walkthrough moves through the block in the order the computation happens:

  • Input: the residual stream begins
  • LayerNorm: normalize before attention
  • Project into queries, keys and values
  • Split d_model into heads
  • Attention: who looks at whom
  • Mix values, merge heads, project back
  • Add back into the stream
  • Normalize, then widen and squeeze (the MLP)
  • Add again: the output leaves the block
  • Unembed: from hidden state to next-token probabilities

How to read it

Every row is one token. Follow a row across every matrix to see what happens to that token, and step back and forth to watch each operation change the stream. The model is kept tiny (d_model = 8) so that every number fits on screen.

It pairs with attnlab's attention lab: once you know what a head computes here, the heatmaps there show what real heads in GPT-2 do with it. For the same block drawn as circles and lines, see the neuron-by-neuron explainer.

Questions

What is the residual stream in a transformer?
It is each token's running vector, d_model numbers wide, that passes through every block. Attention and the MLP read a normalized copy of it and add their output back, so information written by an early layer can be read by any later one.
What are queries, keys and values?
Three linear projections of each token's vector. A query says what the token is looking for, a key says what it offers, and the value is what it hands over. Attention weights come from query-key dot products, and each token's output is the weighted average of the values.
Why does attention divide by the square root of the head size?
Dot products of longer vectors have larger variance. Dividing by √d_head keeps the scores at a stable scale so the softmax does not saturate onto a single token as heads get wider.
Do I need to install anything?
No. The explainer runs entirely in your browser, and every matrix is computed live from a tiny model with d_model = 8.

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