Foundations to Frontier
Tests
E. Deep Learning › E12 Transformer Architectures

GPT-1, GPT-2, GPT-3

The GPT models (Radford and colleagues 2018 and 2019; Brown and colleagues 2020) are Transformer decoders trained only to predict the next token. GPT-1 (117 M parameters) showed that generative pre-training followed by fine-tuning works across tasks. GPT-2 (1.5 B) showed zero-shot task performance emerging from a larger model and web text. GPT-3 (175 B) showed few-shot in-context learning from examples in the prompt, without gradient updates. Along the way, test loss was found to fall as a power law in model size, data and compute. This page covers the causal language model, the three models, scaling laws and in-context learning. In the exercises you count GPT-2's 124,439,808 parameters and apply the 6ND compute rule. You then train causal models of five widths on a source with a known entropy floor: loss falls steadily with size, and the excess over the floor shrinks roughly as N^−0.38.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Just predict the next word

GPT models learn one skill: given the text so far, guess the next word. Trained on enormous amounts of text, that single skill turns into many abilities: completing code, answering questions, translating, following examples in the prompt. Each new GPT was mostly bigger and trained on more data, and the gains were predictable: the error falls smoothly as models grow. The demo shows the causal attention pattern: each word looks only at earlier words.

Transformer lab
Computed pairs: 136 of 256 for 16 tokens. Sparse patterns grow linearly with length instead of quadratically.
Why engineers care

Large language models built this way now assist with code, documentation, procedures and data analysis. Scaling laws let teams estimate what a bigger model or more data will buy before paying for it.

📝 Quick check (Level 0)
0/3 answered
  1. 1. GPT models are trained to…
  2. 2. GPT's attention lets each token see…
  3. 3. GPT-3 could learn tasks from…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Causal language model:
    • p(x) = Πₜ p(xₜ | x₁ … xₜ₋₁);
    • inputs are tokens[:−1] and targets tokens[1:];
    • one forward pass gives a prediction at every position, thanks to the causal mask.
  • Parameters:
    • GPT-2 small (d = 768, 12 layers, 1024 positions, 50,257 tokens, tied output) has 124,439,808 parameters;
    • GPT-2 XL (d = 1600, 48 layers) has 1.56 B;
    • the rule of thumb 12·L·d² gives 173.9 B for GPT-3’s 96 layers of width 12,288 (reported 175 B).
  • The three models:
    • GPT-1 had 12 layers and 117 M parameters, trained on BooksCorpus and then fine-tuned;
    • GPT-2 had 1.5 B parameters, trained on 40 GB of WebText, and evaluated zero-shot;
    • GPT-3 had 175 B parameters, a 2048-token context and 300 B training tokens, and was evaluated zero-, one- and few-shot.
  • Compute:
    • training costs about 6·N·D FLOPs;
    • for GPT-3, 6 × 175 × 10⁹ × 300 × 10⁹ = 3.15 × 10²³.
  • Scaling laws: Kaplan et al. (2020) found loss falling as a power law in N, D and compute. Chinchilla (Hoffmann et al., 2022) found compute-optimal training uses about 20 tokens per parameter. By that rule, GPT-3 would have wanted 3.5 T tokens, 12 times more.
📝 Quick check (Level 1)
0/3 answered
  1. 1. GPT-2 small parameters:
  2. 2. Training compute rule of thumb:
  3. 3. Chinchilla's tokens per parameter:

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

GPT in code

Exercise 1.

  • The parameter formula gave 124,439,808 for GPT-2 small.
  • 12·L·d² gave 173.9 B for GPT-3’s shape.
  • The 6ND rule gave 3.15 × 10²³ FLOPs for GPT-3’s training.

Practice.

  • Hugging Face GPT2LMHeadModel with generate() handles sampling. Pass labels=input_ids and the library shifts targets internally.
  • Few-shot prompting means writing k input–output examples followed by the new input. GPT-3 fits up to about 32 shots of 40 tokens in 2048 tokens.
  • GPT-2 used pre-LN and a final LayerNorm, scaled the initialisation of residual projections by 1/√(number of layers), and used byte-level BPE.
from transformers import GPT2LMHeadModel, GPT2TokenizerFast
tok = GPT2TokenizerFast.from_pretrained("gpt2"); lm = GPT2LMHeadModel.from_pretrained("gpt2")
ids = tok("Flight log: pressure nominal. Temperature", return_tensors="pt").input_ids
loss = lm(ids, labels=ids).loss                                  # next-token cross-entropy
print(tok.decode(lm.generate(ids, max_new_tokens=10, do_sample=False)[0]))
🐍 Exercise 1: sizing GPT
Count GPT-2's parameters, apply the 12·L·d² rule and the 6ND compute rule, and form causal LM targets.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Hugging Face shifts targets when you pass…
  2. 2. GPT-3's context length:
  3. 3. GPT-2's layer normalisation placement:

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why next-token prediction is so general.

  • To predict text well, a model must model whatever produced it: grammar, facts, arithmetic, the intent behind a question.
  • Every token gives a training signal, and unlabelled text is plentiful.
  • Tasks can be posed as text, as in “Translate to French: …”, so one model handles many of them.

Scaling laws.

  • Loss behaves like L(N) ≈ L∞ + (N_c/N)^α_N for model size, with similar forms for data and compute, over many orders of magnitude (Kaplan et al.).
  • The irreducible term L∞ is the entropy of the data. The reducible part shrinks as a power law, which makes big-model returns predictable.
  • Chinchilla revised how to split a compute budget: scale data and model about equally.

In-context learning.

  • GPT-3 improved on tasks given a few examples in the prompt, with no weight updates.
  • Larger models gained more from examples. Mechanistic studies trace part of this to induction heads, which copy patterns earlier in the context (Olsson et al., 2022).
📝 Quick check (Level 3)
0/3 answered
  1. 1. The irreducible loss term is…
  2. 2. Chinchilla advises scaling model and data…
  3. 3. In-context learning uses…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Does loss scale smoothly with size, even tiny?

History.

  • A. Radford, K. Narasimhan, T. Salimans and I. Sutskever introduced generative pre-training (GPT-1, 2018).
  • A. Radford et al., “Language models are unsupervised multitask learners” (GPT-2, 2019).
  • T. Brown et al., “Language models are few-shot learners” (GPT-3, NeurIPS 2020).
  • J. Kaplan et al. (2020) and J. Hoffmann et al. (2022) established scaling laws.

Exercise 2.

  • Data: sequences from a fixed random order-2 Markov source over 16 tokens, where each context allows 3 next tokens. Its entropy floor is 0.679 nats, the best any model can do.
  • Models: one causal Transformer layer of width 4, 8, 16, 32 or 64.
  • Training: 400 steps of 16 sequences.
  • Measurement: test loss and the excess over the floor.
width parameters test loss excess over floor
4 364 2.514 1.836
8 968 2.379 1.701
16 2,944 2.014 1.335
32 9,968 1.397 0.718
64 36,304 1.005 0.326

A straight-line fit of log excess against log parameters gives a slope of −0.38.

  • Loss falls steadily with size, approaching the entropy floor, even at this tiny scale.
  • The trend is only roughly a power law. The smallest models improve slowly, then the curve steepens; a single exponent summarises it loosely.
  • The exponent is not comparable to Kaplan et al.’s: we fit the excess over a known floor, at fixed training steps, on synthetic data. The qualitative lesson carries over: bigger is predictably better until data or steps run out.
🐍 Exercise 2 (research): does loss scale smoothly with size, even tiny?
Train causal language models of five widths on a source with a known entropy floor and fit the excess loss against size.

GPT in AI, robotics and aerospace

  • Engineering assistants: language models draft procedures, summarise anomaly reports and write analysis code, with human review.
  • Planning with language: robots and agents use language models to break goals into steps, grounded by perception and checks.
  • Budgeting: scaling laws help estimate the model size and data needed to reach a target error before training.

References

  1. A. Radford, K. Narasimhan, T. Salimans, I. Sutskever (2018). “Improving language understanding by generative pre-training.” OpenAI report.
  2. A. Radford et al. (2019). “Language models are unsupervised multitask learners.” OpenAI report.
  3. T. Brown et al. (2020). “Language models are few-shot learners.” Proc. NeurIPS.
  4. J. Kaplan et al. (2020). “Scaling laws for neural language models.” arXiv:2001.08361.
  5. J. Hoffmann et al. (2022). “Training compute-optimal large language models.” Proc. NeurIPS.