With the masked batches ready, two steps remain before evaluation: load a pretrained model and run the fine-tuning. (Scope note: the source notes cover these two sections in a few sentences; this page presents exactly that much, connected to the machinery built in earlier chapters.)
7.5 Loading a pretrained LLM — and why it’s a bigger one
Every previous chapter used GPT-2 small (124M). This chapter loads GPT-2 MEDIUM (355M) instead:
The capacity threshold
The 124-million-parameter model is too limited in capacity to achieve satisfactory results via instruction fine-tuning: smaller models lack the capacity to learn and retain the intricate patterns and nuanced behaviors that high-quality instruction-following requires. Classifying spam (ch6) asks the model one narrow question; generating a good free-form response to an arbitrary instruction asks for far more.The upgrade costs exactly three config numbers — the ch4 GPT-2 family table, realized:
| Config key⇅ | GPT-2 small (ch4–6)⇅ | GPT-2 medium (ch7)⇅ | |
|---|---|---|---|
| emb_dim | 768 | 1024 | + |
| n_layers | 12 | 24 | + |
| n_heads | 12 | 16 | + |
| parameters | 124 | 355 | + |
Loading works exactly as in chapter 5: instantiate
GPTModel(BASE_CONFIG) with the medium dimensions, pour in OpenAI’s
published tensors via the custom mapping, eval().
7.6 Fine-tuning on the instruction data
The training itself is deliberately anticlimactic — per the book, a training
loop similar to pretraining: the ch5 rhythm of
zero_grad → loss → backward → step with AdamW, periodic evaluation, and
sample generations, now fed by the instruction loaders. All the
task-specific intelligence lives in what was already built:
- The data carries the format: Alpaca-templated prompt+response texts (§7.2).
- The collate function carries the objective: next-token targets with padding masked to −100, one end-of-text target kept per example (§7.3).
- The model carries the knowledge: 355M pretrained parameters, all of them trainable — unlike ch6, no freezing and no head swap, because the model must keep generating over the full vocabulary.
Key idea — two fine-tuning paths, one machinery
Chapter 6 changed the model’s output space (a 2-way head, last-token read-out, frozen tower). Chapter 7 keeps the model a full text generator and instead changes what text it practices on. Same loop, same loss function — the −100 masking simply decides which positions count. What remains is to see whether it worked: extracting and judging the responses.📝 Check yourself: model choice & fine-tuning
0 / 41.Why does this chapter load GPT-2 MEDIUM (355M) instead of the 124M model used so far?
2.What changes in the GPTModel code to go from 124M to 355M?
3.How does the instruction fine-tuning training loop differ from chapter 5's pretraining loop?
4.Why does instruction fine-tuning keep the original 50,257-way output head (unlike ch6's 2-way replacement)?