What a dense transformer does
A text-generating transformer does one thing over and over: it reads the text so far and outputs a probability for every token in its vocabulary to come next. One token is drawn from those probabilities and appended, and the model runs again. Dense means that every token goes through every parameter in every block, unlike the mixture-of-experts models on some other tabs, which send each token through only some of their weights.
This page runs GPT-2 small: a vocabulary of 50,257 tokens, 12 transformer blocks of width 768, each with 12 attention heads. It opens on recorded examples such as Data visualization empowers users to; a prompt of your own, of up to 32 tokens, runs the model in your browser after a one-time 626 MiB download.
Embedding
The Embedding step turns text into vectors. A byte-level BPE tokenizer splits the prompt into tokens from the 50,257-entry vocabulary: empowers becomes em and powers. Each token id selects a 768-dimensional row of the token embedding matrix . Attention by itself ignores order, so GPT-2 adds a learned positional encoding, one 768-dimensional row for each of its 1,024 positions:
Masked multi-head self-attention
In each block, attention lets every token gather information from the tokens before it. After a LayerNorm, one matrix of shape 768 × 2304 makes a query, a key and a value for every token, each 768 wide and split into 12 heads of 64. In each head, a token's query is compared with every key, the scores are scaled, future positions are masked, and a softmax turns each row into weights for averaging the values:
The mask is above the diagonal, so no token can look ahead at the token it is predicting. The attention matrix in the Multi-head Self Attention panel unfolds these stages: dot product, scaling and mask, softmax. The 12 heads work in parallel and can attend to different things; their outputs are joined back to 768 dimensions, projected, and added to the block's input.
MLP, residuals and layer normalisation
After attention, each token passes on its own through the MLP: a projection from 768 to 3072 dimensions, a GELU, and a projection back to 768. GELU weights its input by the probability that a standard normal falls below it, ; GPT-2 uses a tanh approximation of it.
Two residual connections add the attention output and the MLP output back to the block's input, so each block makes a correction to a running stream rather than replacing it. Layer normalisation before attention, before the MLP and after the last block keeps the scale of the stream steady:
The page draws block 1 in full; the other 11 more identical Transformer Blocks have the same shape with their own weights. Dropout appears in the diagram but is switched off when the model generates.
From logits to the next token
After the last block, the hidden state of the final token is multiplied by the transposed token embedding matrix, giving one logit per vocabulary entry. The Probabilities panel turns logits into a choice in three steps. The Temperature divides every logit before the softmax: below 1 it sharpens the distribution towards the most likely tokens, above 1 it flattens it. Then Sampling keeps a shortlist, either the most likely tokens (top-k) or the smallest set whose probabilities add up to at least (top-p), renormalises it and draws one token:
The defaults are and top-k with . With them, after Data visualization empowers users to the recorded distribution gives visualize 54.7 %, create 20.9 % and see 12.1 %, and the token is drawn at random from the five.
Where GPT-2 came from
The transformer (Vaswani et al., 2017) was an encoder–decoder for translation. GPT (Radford et al., 2018) kept only a decoder stack, pre-trained it to predict the next token on books and fine-tuned it for each task. GPT-2 (2019) scaled the same design up to 1.5B parameters and 40 GB of web text and showed that it could attempt many tasks without fine-tuning. The small model on this page is the smallest of its four sizes. Holtzman et al. (2019) showed that taking the most likely tokens produces dull, repetitive text and introduced top-p sampling.
This tab is the Transformer Explainer of Cho et al., on which the other tabs build. The Training tab runs one training step of the same model, and the MoE tab replaces the MLP with a mixture of experts.
Papers
- Attention Is All You Need The transformer: multi-head self-attention, MLPs, residual connections and layer normalisation, here in its decoder form.
- Language Models are Unsupervised Multitask Learners GPT-2, the model this page runs: a decoder-only transformer trained to predict the next token of web text.
- Improving Language Understanding by Generative Pre-Training GPT: a decoder-only transformer pre-trained on next-token prediction and then fine-tuned.
- Transformer Explainer: Learning LLM Transformers with Interactive Visual Explanation and Experimentation The interactive GPT-2 explainer this tab is, and the visual language the other tabs reuse.
- Neural Machine Translation of Rare Words with Subword Units Byte-pair encoding for subword tokens, the basis of GPT-2's byte-level tokenizer.
- The Curious Case of Neural Text Degeneration Why the most likely text is dull and repetitive, and nucleus (top-p) sampling as a remedy.
- Gaussian Error Linear Units (GELUs) The GELU activation in GPT-2's MLP, with the tanh approximation it uses.
See also
- AttentionMHA Multi-head attention · GPT-2
- TokenizationGPT-2 GPT-2 · byte-level BPE
- InterpretabilityLogit lens Logit lens · reading every layer with the unembedding
- InterpretabilityLenses Lenses compared · the logit lens and the tuned lens on the same stream
- CompilersGraph capture Graph capture · TorchDynamo tracing a model into an FX graph, and where it breaks
- PretrainingArch ladder GPT-2 to gpt-oss · one architecture change at a time on the same run