Chapter 16: Training and Frameworks
A framework is a contract between the math and the hardware. You write a model as composable tensor operations; the framework records them (as a graph or a trace), computes gradients by reverse-mode autodiff, and hands the graph to a compiler (XLA, Inductor, TensorRT) that fuses kernels and targets the accelerator. Everything in this chapter, from eager versus compiled execution to the parallelism zoo, LoRA, and quantization, is about getting that contract to fit on the hardware you have and the budget you have.
Concepts from other sections (7)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- LoRA / QLoRA: training small low-rank adapters on a frozen base model; QLoRA does so on a 4-bit-quantized base. Read more: 16.3 Embeddings, small models, and platforms
- Transformation (JAX): a function such as
jit,grad,vmap, orpmapthat takes a pure function and returns a compiled, differentiated, batched, or parallel version of it. Read more: 16.3 Embeddings, small models, and platforms - ZeRO / FSDP: sharding parameters, gradients, and optimizer state across data-parallel ranks, gathering each layer only when it is needed. Read more: 16.3 Embeddings, small models, and platforms
- DPO: minimizing over preference pairs, equivalent to fitting the KL-regularized reward objective. Read more: 18.2 Direct preference optimization and IPO
- Reinforcement fine-tuning (RFT): tuning a model against an evaluator that returns a reward from 0 to 1 for each output, with gold completions optional; it fits tasks that are verifiable but lack reference answers. Read more: 24.5 Routing implementations and selection
- Backpropagation: reverse-mode automatic differentiation that computes all parameter gradients in one backward pass. Read more: Chapter 2: Deep Learning
- KV cache: per-layer storage of past keys and values, so each decode step processes only the new token. Read more: 21.2 The KV cache, incremental decoding, and generate
Training and serving pull in different directions. Training is throughput over a fixed dataset; serving is latency over an open stream. The platform engineer’s job is to make both economical on the same fleet, and to own the loop that connects them: data, then training with checkpoints, then evaluation, then a registry, then serving, then monitoring, and back to data. This chapter opens the book because every later chapter, from the gateway to retrieval to evaluation, sits on top of models that someone trained, fine-tuned, and deployed with these tools.
The chapter moves from the bottom of the stack upward. It starts with the two frameworks that define models, PyTorch and JAX, then the serving engines that turn a trained model into a product. It then takes on distributed training, the problem of making a model that does not fit on one GPU fit anyway. The last three parts cover the platform jobs that sit around a model: creating and hosting embedding models, getting a small language model good enough to replace a frontier API, and running the whole lifecycle as a loop.
Learning objectives
- Explain the difference between PyTorch’s eager-by-default execution and JAX’s functional, compiler-first design, and choose between them for a given workload.
- Name the four axes of parallelism (data, tensor, pipeline, expert), what each splits, and what each costs in communication.
- Explain why ZeRO and FSDP shard optimizer state, and estimate why training memory runs several times the size of the weights.
- Describe how an embedding model is created (contrastive fine-tuning), hosted (batched encoder inference), and indexed (a vector store).
- Make the build-versus-buy case for a fine-tuned small language model, and pick SFT, DPO, or RFT from the training signal you have.
Before you begin
Section titled “Before you begin”This chapter assumes you know how neural networks are trained: backpropagation, optimizers such as Adam, and the transformer architecture, all covered in Deep Learning in AI Engineering. It also leans on LLM Systems & Inference from the same book for serving, the KV cache, and the basics of parallelism. You do not need to have trained a large model yourself; the point here is to understand the choices well enough to make them on a platform.
In this chapter
Section titled “In this chapter”- PyTorch and JAX execution: Relate eager, compiled, and transformed execution to the training loop.
- Distributed and serving boundaries: Explain which responsibilities belong to the learner and the serving runtime.
- Embeddings, small models, and platforms: Connect specialized training to its evaluation and deployment loop.