Chapter 17: Training at Scale
A model and a correct training loop still need a memory, compute, and recovery plan. The foundations loop uses float32 and retains intermediate activations; a larger batch, longer context, or larger model can make that state dominate memory. This chapter develops the precision, accumulation, recomputation, communication, and checkpoint mechanisms needed to move the capstone from a local experiment to a larger training system.
Concepts from other sections (15)
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.
- data parallelism (DDP): each rank holds a full model replica, computes the gradient of its batch slice, and all ranks average gradients before stepping. Read more: 17.3 Data parallelism and ZeRO stages 1 to 3
- ZeRO: sharding training state across ranks so that each piece is stored once instead of times. Read more: 17.3 Data parallelism and ZeRO stages 1 to 3
- Master copy: an fp32 copy of 16-bit weights kept so small updates are not rounded away. Read more: 11.1 Counting: parameters, FLOPs, KV bytes, and memory plans in Mathematics
- bf16: a 16-bit format with float32’s 8 exponent bits and 7 mantissa bits; float32’s range at unit roundoff . Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
- fp16: a 16-bit format with 5 exponent bits and 10 mantissa bits; finer precision but a maximum of 65504 and a smallest subnormal of . Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
- loss scale: a power of two that multiplies the loss so that small fp16 gradients stay representable; divided out before the update. Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
- master weights: the float32 copy of the parameters and optimizer state that receives every update. Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
- dynamic loss scaling: halving and skipping the step on overflow, and doubling after a run of good steps. Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
- gradient accumulation: summing the gradients of micro-batches weighted by to reproduce one large batch’s gradient. Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
- activation checkpointing: discarding a segment’s activations in the forward pass and recomputing them during backward. Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
- reduce-scatter: every rank ends with one chunk of the reduction. Read more: 17.2 Collectives over processes: ring all-reduce
- all-gather: every rank ends with the concatenation of all ranks’ chunks, in rank order. Read more: 17.2 Collectives over processes: ring all-reduce
- all-reduce: every rank ends with the elementwise reduction (sum or mean) of all ranks’ arrays. Read more: 17.2 Collectives over processes: ring all-reduce
- Gradient clipping: rescaling all gradients by one factor when their global norm exceeds a limit. Read more: 16.4 Learning-rate schedules (cosine, WSD, Noam) and gradient clipping in Mathematics
- deadlock: a cycle of blocked operations, each waiting on the next, so none can proceed. Read more: 17.2 Collectives over processes: ring all-reduce
The first section supplies the capstone’s precision, accumulation, and recomputation techniques. The next two build process collectives, data parallelism, and ZeRO from small implementations. The final parts connect those mechanisms to tensor, pipeline, context, and expert layouts, then to profiling, numerical failures, checkpointing, and recovery. Implementation checks and production measurements have different scopes: use the small checks to understand the algorithm, then validate memory, quality, and throughput on the actual training stack.
Three ideas run through the whole chapter. Mixed precision keeps a float32 master copy of the weights and computes in bf16; fp16 needs a loss scale so that small gradients do not underflow. Accumulation sums gradients over micro-batches so that a large effective batch fits in memory, with each micro-batch’s loss weighted by its share of the objective’s units, such as valid target tokens; data parallelism is the same idea spread over processes. Checkpointing stores activations at segment boundaries and recomputes the rest during backward, trading compute for memory.
Learning objectives
- Train with emulated bf16 or fp16 matmuls while keeping float32 master weights, and explain why that converges.
- Derive and implement dynamic loss scaling, exact gradient accumulation, and bit-exact activation checkpointing.
- Implement ring reduce-scatter, all-gather, and all-reduce over processes, and count their bytes.
- Build data parallelism and ZeRO stages 1 to 3, and compute per-rank memory for each.
Before you begin
Section titled “Before you begin”You should be comfortable with the Tensor and training loop of the Foundations chapter. From Mathematics, the chapter leans on the bf16 and fp16 emulation in Numerical Methods and Floating Point, the optimizers and gradient clipping in Optimization, and the checkpoint schedule in Hessian-vector products and the recompute-versus-memory schedule.
In this chapter
Section titled “In this chapter”- Mixed precision, loss scaling, gradient accumulation, and activation checkpointing: the four tools every large training run uses, built from first principles and checked against float32, as used by the capstone’s
--bf16 --accum --checkpoint-activationsflags. - Collectives over processes: ring all-reduce (optional): point-to-point messages between processes, the ring reduce-scatter and all-gather, why the ring’s traffic per link does not grow with the number of ranks, and how to avoid deadlock.
- Data parallelism and ZeRO stages 1 to 3 (optional): data parallelism as accumulation across processes, and sharding the optimizer state, gradients, and parameters so that per-rank memory falls toward .
- Parallelism, topology, and the global batch: choose the dimension to split, count independent replicas, and validate distributed equivalence.
- Training failures, profiling, and recovery: diagnose data/numerical/optimization failures, save complete state, and test interrupted-run continuation.
Further reading
Section titled “Further reading”- Micikevicius et al., Mixed Precision Training; Chen et al., Training Deep Nets with Sublinear Memory Cost.
- Rajbhandari et al., ZeRO; the Training and Post-Training chapter of AI Engineering for production practice.