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

Prompting and evaluation

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.intervene of 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