GPT-2 · RoBERTa Lab · Part II — The GPT family
06

GPT-2 end to end,
and what scale buys

The full assembly of the pieces — and an honest measurement of what changes from 124 million to 1.5 billion parameters.

Chapter 06 of 15 Prerequisite: chapters 01 to 05

1 The whole model, top to bottom

You already know all the pieces. Here is the assembly, with nothing omitted:

TEXT    "The capital of France is"
   |
   |  BPE tokenizer (chapter 01)              — not part of the model
   v
IDs     [464, 3139, 286, 4881, 318]                        (5,)
   |
   |  wte[IDs]  +  wpe[0..4]                 (chapter 02)
   v
h_0                                                        (5, 768)
   |
   +-- BLOCK  1 ---+                                                 .
   |   x = x + attn(ln_1(x))   communication (chapters 03, 04)       .
   |   x = x + mlp (ln_2(x))   computation   (chapter 05)            .
   +---------------+                                                 .
   |                                                                 .
   |  ... 12 identical blocks, shape always (5, 768) ...             .
   |                                                                 .
   +-- BLOCK 12 ---+
   |
   v
h_12                                                       (5, 768)
   |
   |  ln_f   — final LayerNorm (exists only because of Pre-LN)
   v
   |  @ wte.T   — weight tying (chapter 02, section 6)
   v
LOGITS                                                   (5, 50257)
   |
   |  we take ONLY the last row: the prediction of the next token
   v
logits[-1]                                                 (50257,)
   |
   |  softmax + sampling strategy             (chapter 07)
   v
NEXT TOKEN  ->  goes back to the top, now with 6 tokens
What the model returns — and what gets thrown away

For a 5-token input, GPT-2 returns 5 predictions, one per position: "what comes after The", "what comes after The capital", and so on. All 5 are computed, always.

In training, all 5 are used — that is what makes pre-training so efficient (chapter 08). In generation, 4 are thrown away and only the last one matters. That "waste" is exactly what the KV cache recovers.

2 The whole family in one table

The four GPT-2s are the same model. No new piece, no different trick. Only three numbers change.

Note that dk = dmodel / heads = 64 in all of them. That constant runs through the whole family, and it is no accident: 64 is the subspace size at which √dk keeps the softmax well behaved.
ModelLayersdmodelHeadsdkMLPParametersfp32
gpt21276812643072124 M0.5 GB
gpt2-medium24102416644096355 M1.4 GB
gpt2-large36128020645120774 M3.0 GB
gpt2-xl481600256464001558 M6.0 GB

They all share: a vocabulary of 50,257, a context of 1024, byte-level BPE, Pre-LN, GELU, 4× expansion in the MLP, weight tying. The training was the same too — the same WebText, the same 40 GB of text. The only experimental variable was size.

That was, in fact, the thesis of the GPT-2 paper ("Language Models are Unsupervised Multitask Learners", 2019): keep everything fixed and grow, to show that new capabilities emerge from scale alone.

The counting formula

N ≈ V·d + nctx·d + L · (12d² + 13d) Per block: 4d² in attention (QKV is 3d², the projection is d²) + 8d² in the MLP (two matrices of d×4d) = 12d². The 13d are biases and the LayerNorms' γ/β.

The term that matters is L · 12d². Since d is squared, doubling the width quadruples the cost of that block. That is why models grow more in depth than in width: it is cheaper.

3 What scale really buys

The intuitive answer ("bigger models write better") does not hold up in the lab.

In this course's initial test, I asked the same thing of all four models, in greedy mode (always the most likely word). The prompt was "The capital of France is":

Real result, measured on this machine. Read the right-hand column carefully.
ModelParametersGenerated continuationCorrect?
openai-gpt117 M"a great city , and the city is a great city ."no
gpt2124 M"the capital of the French Republic, and the capital of the"no
gpt2-medium355 M"Paris, and the capital of France is Paris."yes
gpt2-large774 M"the capital of France.\n\nThe capital of France is"no
gpt2-xl1558 M"the city of Paris. It is the capital of France and"yes
Scale is not monotonic

The large (774 M) got wrong a question the medium (355 M) got right. That contradicts the intuition that "more parameters = better", and it is a real result, reproducible on your machine.

Two things are going on. First, greedy is a fragile measure: it takes the most likely word and never backtracks, so a near-tie on the first token decides everything. Second — and more important — a single sample does not measure capability. To say anything serious about a model, you have to measure a distribution over many examples. That is exactly what perplexity does, and it is the subject of chapter 08.

Keep this as mental hygiene: when someone shows you a screenshot of a model getting a question right or wrong, you are looking at a sample of size 1.

So what really improves?

When you measure properly, over many examples, three things improve consistently with scale:

  1. Perplexity. The measure of "how surprised the model is by real text" drops cleanly and predictably. You will measure this in lab 08.
  2. Long-range coherence. The small loses the thread within 2–3 sentences; the XL sustains a paragraph. It is what the logit lens of chapter 05 suggested: the small's problem is not ignoring the fact, it is failing to sustain it all the way to the output.
  3. In-context learning. This is the genuinely emergent capability: giving examples in the prompt and having the model follow the pattern with no training at all. See the measurement below.

The measurement that shows scale clearly

The same prompt as in the previous table, now with three examples before it:

Portugal -> Lisbon
Japan -> Tokyo
Italy -> Rome
France ->
Probability assigned to " Paris" as the next token, measured on this machine. No weight was updated — the only difference is the prompt text.
ModelNo examplesWith 3 examplesGain2nd and 3rd place
gpt20.0320.38512×" Marse", " Nice"
gpt2-medium0.1740.8715×" Marse", " Lyon"
gpt2-xl0.0860.90410×" Lyon", " Nice"

Look at the last column. The runners-up are Marseille, Lyon, Nice — other French cities. The model did not merely get it right: it understood that the task was "country → city of that country" and started erring within the right category. That is the pattern being captured, not a memorized correspondence.

And note: no weight was updated. The only difference between 3.2% and 38.5% was the text that came before the question. Taken to GPT-3 (175 billion parameters), this capability became so strong that it made fine-tuning unnecessary for most tasks — and it is the reason "prompt engineering" became a discipline.

And what does not improve

4 The cost of running, measured

On an ordinary 8-core desktop CPU, in fp32 and without a GPU, measured in practice:

ModelRAM requiredLoad from diskGeneration
gpt2~0.6 GB~1 s~40 tok/s
gpt2-medium~1.6 GB~2 s~15 tok/s
gpt2-large~3.3 GB~4 s~7 tok/s
gpt2-xl~6.5 GB~8 s~3 tok/s

The rule of thumb: fp32 costs 4 bytes per parameter. A 1.5 B model takes up 6 GB in weights alone. That is why quantization exists — in 8 bits, the same model fits in 1.5 GB, and in 4 bits, in 800 MB. Quality drops a little and the practical reach changes completely.

5 In the lab

.venv\Scripts\python.exe lab\06_scale.py
  1. Checks the architecture of each model by reading the real config.json, and verifies the parameter-count formula against the true number.
  2. Runs the same prompt on the five models, greedy, and shows the table above live on your machine.
  3. Measures the probability each model assigns to the right answer — not just what it generated. This is far more informative than the single sample: you will see that the large "almost" got it right.
  4. Tests in-context learning with examples in the prompt, comparing small and XL. It is where scale shows up most clearly.
  5. Times everything and builds the cost table for your machine.

Exercise 6 — predict before running

  1. Use the formula from section 2 to compute the parameters of gpt2-medium (L=24, d=1024). Compare with 355 M. By how much were you off, and where does the difference come from?
  2. gpt2-xl has 12.5× the parameters of gpt2. Does it generate 12.5× slower? Measure and explain the difference.
  3. If you had to choose ONE model to use on this machine day to day, which would it be and why? Justify with numbers, not intuition.
  4. Hard: the formula has the term V·d (embedding) and the term L·12d². For which value of d are the two equal, with V=50257 and L=12? What does that say about very small models?
References

The primary sources for the concepts in this chapter. The numbers in the text were measured in the lab; the ideas come from here.

Technical terms here carry their machine-learning meaning. The course glossary shows that meaning next to the others the same words have in IT.

  1. Radford, A., Wu, J., Child, R., Luan, D., Amodei, D. and Sutskever, I. (2019). Language Models are Unsupervised Multitask Learners. Technical report, OpenAI. cdn.openai.com
  2. Press, O. and Wolf, L. (2017). Using the Output Embedding to Improve Language Models. EACL 2017. arXiv:1608.05859
  3. Xiong, R. et al. (2020). On Layer Normalization in the Transformer Architecture. ICML 2020. arXiv:2002.04745

The model produces a probability distribution over 50,257 tokens. Turning that into text is your decision, not its.
Chapter 07 shows that this decision changes the result more than switching models does.