18 Working with Pretrained Language Models
By now you can build a language model from nothing. In Section 11.2 we trained a small GPT on a few hundred megabytes of Wikipedia, and we loaded the released weights of GPT-2 into the same class with a name map that we wrote by hand (Section 11.2.5). Neither is a model that anybody would consult. The models that people do consult were trained on more text and more machines than any of us commands. Few readers of this book will ever train one. All of them can download one.
That changes what the work is. Since about 2019, when a shared library and public repositories of weights made the pretrained model the usual place to start, the effort has moved from training to checking and measuring. What did we download? Does it compute on our machine what its authors computed on theirs? When one model scores two points above another, is that a difference between the models? These questions sound like housekeeping, and they are where results go wrong. A score, it turns out, belongs to a model together with a prompt, a rule for reading the answer and a sample of questions. Every later chapter of this part compares two scores, of a fine-tuned model and its base or of a compressed model and the original, and this chapter builds the instruments for that.
The chapter has three movements. The first opens a checkpoint: Section 18.1 reads its files until the configuration alone accounts for the bytes of its weights and its cache and bounds its speed, and Section 18.2 runs the same tensors in the book’s own code, which forces us to say how closely two correct programs can agree. The second takes a score apart in the order in which it is made: the prompt (Section 18.3), the rule that turns outputs into a number (Section 18.4), and the error bar that says whether a difference would survive another set of questions, with a model as the judge where no answer key exists (Section 18.5). The third looks at a model from both sides. With the weights in hand we can overwrite an activation and watch what changes, which is how a claim about the inside of a model is tested (Section 18.6). The largest models we cannot hold at all, and Section 18.7 asks what can still be measured when only text comes back.
Every model in this chapter keeps the weights it was released with. Fine-tuning has to wait for Chapter 24, architectures beyond what a configuration file shows for Chapter 31, and serving engines for Chapter 30. Models that turn text into vectors, and retrieval, follow directly in Chapter 19. For licenses, version pins and the choice of hardware we point to the Tools appendix (Section 39.5, Section 39.4).
We assume the transformer of Chapter 11 with its key-value cache (Section 11.3), and a first acquaintance with confidence intervals. We ran the notebooks on one GPU with 24 GB of memory, and none of them requires one. The long computations were done once and published with the book. A notebook downloads their results, repeats the cheap measurements, and without a GPU falls back on a smaller model or on fewer examples, and says so. One notebook is a recording on every machine: the requests of Section 18.7 cost money and cannot be repeated exactly, so its cells replay the responses of one dated session.
Resources and Further Reading
The resources below accompany the sections of this chapter, and each names the section it goes with. All are freely accessible online. If you read only three, read the note by Miller on error bars, the guide by Heimersheim and Nanda on patching, and the essay by He on nondeterminism. The chapter has no notebook of further reading: fine-tuning, architectures, serving and retrieval, the subjects it leaves out, have chapters of their own in this part.
Courses
- Hugging Face LLM Course, chapter 2 is a tour of the Transformers interface. Read it first if the loading calls and the
Autoclasses of Section 18.1 are new to you. - Stanford CS336: Language Modeling from Scratch has lectures on evaluation that make a habit of reporting what was held fixed, the habit behind the grid of conventions in Section 18.4 and the report card of Section 18.5.4.
- Stanford CS224N: Natural Language Processing with Deep Learning sets an assignment on benchmarking and evaluating language models, the course-side counterpart of Section 18.4 and Section 18.5, and its guest lecture on interpretability pairs with Section 18.6.
- CMU 11-711: Advanced Natural Language Processing and Berkeley CS288 each give a lecture to experimental design and human annotation, and both ask students to reproduce a published result, the task that Section 18.5.4 works through on one number.
- ARENA is a curriculum of programming exercises whose chapter on the interpretability of transformers treats patching, sparse autoencoders and steering at greater length. Section 18.6 follows it in spirit, on one released model and with plain hooks.
Prompting and evaluation
- Lilian Weng’s Prompt Engineering surveys few-shot prompting, the choice and order of the examples, and the label biases that the calibration of Section 18.3.3.3 is meant to remove. Section 18.3 measures these effects on two checkpoints.
- lm-evaluation-harness is the program behind many published scores of open models, and its authors’ Lessons from the Trenches on Reproducible Evaluation of Language Models explains its design. Section 18.4 reproduces its scores item by item, and Section 18.5.4.2 borrows its protocol for GSM8K.
- OLMES is a standard that fixes the prompt format, the choice of demonstrations and the normalization of a multiple-choice score, the decisions that Section 18.4 varies one at a time.
- Evan Miller’s Adding Error Bars to Evals sets out standard errors, paired differences, clustered questions and power for the evaluation of language models. It is the statistics of Section 18.5 in compact form, with formulas where the section uses the bootstrap.
- The tutorials on the evaluation of language models at NeurIPS 2024 and ICML 2024 survey the benchmarks and methods of the field, the background of Section 18.5.2.1.
- Hamel Husain’s Your AI Product Needs Evals builds a small suite of tests from the failures of a real product, the development suite of Section 18.5.2.2 as a practitioner keeps it.
Ports and interpretability
- Sebastian Raschka’s Qwen3 from scratch writes the model of Section 18.2 in plain PyTorch and loads the released weights, a second independent port to set beside ours.
- TransformerLens offers recording and overwriting of activations for many models. The
d2l.interveneof Section 18.6 writes the same operations out for one. - Stefan Heimersheim and Neel Nanda’s How to Use and Interpret Activation Patching discusses the choices that Section 18.6.2.1 has to make: the corrupted prompt, the metric, and the direction of the patch.
- Neuronpedia is a gallery of the features of published sparse autoencoders with the contexts on which they fire. Browse it after Section 18.6.3.4, and ask of every label there what that subsection asks of ours.
Hosted models and reproducibility
- Horace He’s Defeating Nondeterminism in LLM Inference traces the run-to-run drift of a served model to kernels whose arithmetic depends on the batch. It is the explanation behind Section 18.4.5.2 and behind the third experiment of Section 18.7.
- openai-python is the client package that the
d2l.ChatAPIof Section 18.7.2 wraps. - Marc Brooker’s Exponential Backoff and Jitter simulates the retry schedules of Section 18.7.2 for many competing clients and is the source of full jitter.