Decoder-only
- Machine Learning course
- Expert
- Lesson 25/44
- 5 min
GPT is a transformer with only decoder blocks.
The decoder-only architecture
GPT's block is the original transformer decoder without cross-attention: only masked self-attention and the FFN. There is no encoder; the context is the input sequence itself:
x = gomme[ids] + konum[:n]
for blok in bloklar:
x = x + maskeli_dikkat(layer_norm(x)) # pre-LN
x = x + ffn(layer_norm(x))
logit = layer_norm(x) @ gomme.T # weight tyingFrom GPT-2 on, LayerNorm is placed before the sub-layer (pre-LN); this makes training stable in deep networks. The output layer reuses the embedding matrix (weight tying).
GPT-2 small: 124 million
GPT-2 small: vocabulary 50257, context 1024, d = 768, 12 layers. Thanks to weight tying, the output layer adds no parameters:
V, N, d, L = 50257, 1024, 768, 12
gomme = V * d + N * d
katman = 2 * (2 * d) + (d * 3 * d + 3 * d) + (d * d + d) + (d * 4 * d + 4 * d) + (4 * d * d + d)
print(gomme + L * katman + 2 * d) # including the final LayerNormWithout weight tying, a separate 38.6-million-parameter output matrix would be needed.
Practice GPT-2 parameter count
Compute the parameter counts of the embeddings and a single layer with the given settings and print them on one line. Print the total (12 layers + the final LayerNorm). Finally print the number of parameters that would be added without weight tying (V * d).
Starter code
V, N, d, L = 50257, 1024, 768, 12
# buraya yazSample solution
V, N, d, L = 50257, 1024, 768, 12
gomme = V * d + N * d
katman = 2 * (2 * d) + (d * 3 * d + 3 * d) + (d * d + d) + (d * 4 * d + 4 * d) + (4 * d * d + d)
print(gomme, katman)
print(gomme + L * katman + 2 * d)
print(V * d)Expected output
39383808 7087872 124439808 38597376
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which sub-layer is missing in a GPT block compared with the original transformer decoder?
- Cross-attention Correct answer
- Masked self-attention
- FFN
- LayerNorm
Explanation: Since there is no encoder, there is no source sequence to look at.
2 Reuse the embedding matrix at the output.
logit = layer_norm(x) @ gomme.____
logit = layer_norm(x) @ gomme.T
Explanation: The embedding is (V, d); its transpose (d, V) gives the logits.
3 Match.
| Term | Meaning |
|---|---|
pre-LN |
Norm before the sub-layer |
weight tying |
Output = embeddingᵀ |
context |
1024 tokens |
GPT-2 small |
~124M parameters |