22. Decoder only: how GPT-style models use the design
GPT-style models keep one half of the paper's design: the decoder, without the encoder and without the attention that looked at it. Four changes since 2017, shown on our two real models side by side.
Step 1: the paper's design. The encoder reads the English; the decoder writes the German and reads the encoder's output through encoder–decoder attention.
- Decoder only no encoder, no encoder–decoder attention
- Pre-normalisation LayerNorm before each sub-layer, and once more at the end
- Learned positions one learned row per position instead of sine waves
- GELU instead of the ReLU
| The paper's model | The GPT-style model | |
|---|---|---|
| Halves | encoder and decoder | decoder only |
| Layers, heads, d_model, d_ff | 2, 4, 64, 256 | 2, 4, 64, 256 |
| Normalisation | after each sub-layer: LayerNorm(x + Sublayer(x)) | before each sub-layer: x + Sublayer(LayerNorm(x)) |
| Positions | fixed sine waves | learned, one row per position |
| Activation | ReLU | GELU |
| Output scores from | the shared embedding matrix | the shared embedding matrix |
| Context, in tokens | 32 | 32 |
| Weights | 244,800 | 114,496 |
The paper's Figure 1, redrawn. Step 2 fades the encoder away, step 3 the encoder–decoder attention that read it, and step 4 lists the four changes GPT-style models made to the block that is left. Under it, the lab's two models side by side, every size read from their weights and every weight counted.
The idea
The paper's model has two halves because it translates: the encoder reads the English, the decoder writes the German and looks back at the English through encoder–decoder attention (chapter 18). A model that only continues text has nothing to translate from. Drop the encoder, drop the attention that read it, and what is left is the decoder's masked self-attention and feed-forward network, stacked: a decoder-only model.
The prompt and the answer are then one sequence. The model reads the prompt, predicts the next token, appends it and reads again, exactly as the paper's decoder wrote German one word at a time, and the mask (chapter 10) keeps every position from seeing the ones after it.
The GPT model (Radford et al., 2018) built a decoder-only transformer this way and, in its own words, “used learned position embeddings instead of the sinusoidal version proposed in the original work” and the GELU as its activation. GPT-2 (Radford et al., 2019) kept that design with “a few modifications”: “Layer normalization … was moved to the input of each sub-block” and another was added after the last block. Those are the four changes:
- Decoder only. No encoder, no encoder–decoder attention.
- Pre-normalisation. LayerNorm before each sub-layer, on its input, instead of after the residual sum; one more LayerNorm after the last block.
- Learned positions. A row of numbers per position, learned like any weight, instead of the sine waves of chapter 12.
- GELU. A smooth activation in place of the ReLU of chapter 13.
Our GPT-style model is built exactly so, and the shared embedding matrix still scores the output, as in the paper's section 3.4. Try it: the playground below runs it.
The next-word playground, as on its own page: our GPT-style model reads your English after <s> and ranks the next word; temperature and top-k change the ranking, and one more word appends the choice.
A worked example: our two models side by side
Both models have 2 layers, 4 heads, d_model = 64 and d_ff = 256. The paper's model has 244,800 weights; the GPT-style model has 114,496, less than half, because it has no encoder and no encoder–decoder attention. Its 2,048 position weights are counted, because it learns them; the paper model's sine waves are computed, not learned, so they are not weights.
Pre-normalisation on “Anna has a small”: before the first block, the vector of “small” has variance 0.040. Each sub-layer reads a normalised copy, with variance 1, but the vector itself is never normalised on its way up: after the last block its variance is 336.5. That is why GPT-2 added one more LayerNorm at the end, and so does our model.
The GELU against the ReLU: at x = −0.5 the ReLU gives 0 and the GELU −0.154; at x = 1 the ReLU gives 1 and the GELU 0.841. Small negative values are let through a little, and the curve has no corner at 0.
The formula
- \(x_l\)
- the vectors going into layer l, one per token
- \(\mathrm{Sublayer}\)
- masked self-attention or the feed-forward network
- \(\mathrm{LayerNorm}\)
- re-centres a vector to mean 0 and variance 1, then applies a learned gain and bias (chapter 14)
- \(\mathrm{GELU}(x)\)
- the activation: close to 0 for large negative x, close to x for large positive x, smooth in between
- \(\tanh\)
- a squashing function from −1 to 1; the formula is the usual fast approximation of the GELU
Why it matters
When you call an LLM API, your prompt and the model's answer are one sequence read by one masked stack: the answer is written token by token, each one read back before the next. That is why output tokens are slower and usually priced higher than input tokens, why a long conversation is re-read as a whole, and why the context window, the number of positions the model can read, is a hard limit.
The four changes are small next to the design they leave alone. GPT-2's report does not say why it moved the normalisation; it calls the change similar to a pre-activation residual network (He et al., 2016). Learned positions tie the model to the lengths it was trained on. The design you meet at work is the paper's decoder with these adjustments, scaled up.
Where it fails
- Learned positions stop at the last one learned. Our model has 32 position rows and cannot read a 33rd token. The paper chose sine waves because they “may allow the model to extrapolate to sequence lengths longer than the ones encountered during training” (section 3.5).
- Our miniature shows the design, not its benefits. Pre-normalisation matters for training deep stacks; with two layers trained for minutes we cannot show the difference, only the order of the operations.
- Published facts only. The lab describes GPT-2 from its report. What today's commercial models change inside is mostly unpublished, and the lab does not guess.
Exercises
Try each one before you open a hint. The lab's engine checks your answer against the same models the chapter uses.
Counting the GPT-style model
How many weights does our GPT-style model have, its learned positions included?
The GELU at 1
What does the GELU give for x = 1? Three decimals.
Where the normalisation goes
In our GPT-style model, where is LayerNorm applied inside a block?
What a decoder-only model drops
Which sub-layer of the paper's decoder does a decoder-only model not have?
In the paper
Section 3.1 describes the decoder a GPT-style model keeps, its masking included: “We also modify the self-attention sub-layer in the decoder stack to prevent positions from attending to subsequent positions.” Section 3.5 already tried learned positions: “the two versions produced nearly identical results”.
In the paper: section 3.1