5 Computation
Up to now we have relied on frameworks to manage model construction, parameters, initialization, and device execution behind the scenes. Moving from textbook prototypes to production-scale systems requires opening that black box. Training an 8-billion-parameter language model in 32-bit floating point with Adam demands 128 GB of parameter, gradient, and optimizer state before storing a single activation, and runs across thousands of GPUs where hardware failures interrupt training every few hours. At that scale, understanding how frameworks represent, initialize, compute with, and persist models is essential.
Everything in this chapter builds on one central data structure: a neural network is a tree of modules whose leaves hold its state—trainable parameters and fixed buffers alike—each addressed by its path from the root. Every framework operation walks that same tree:
- Section 5.1 shows how modules register child modules into a hierarchy, why hiding layers in plain Python lists silently drops them from training, how residual blocks and compiled forward passes work, how lazy layers infer shapes on the fly, and how to separate architecture configs from builder code.
- Section 5.2 opens the state tree to distinguish trainable parameters from non-trainable buffers, derives the 16-byte-per-parameter memory floor for fp32 Adam training, counts parameters abstractly without allocating memory, and covers weight tying and parameter freezing.
- Section 5.3 moves from single-layer initialization to whole-tree and residual-network initialization, showing why unscaled residual blocks blow up activation variance by \((1 + g^2)^N\) and how scaling output projections by \(1/\sqrt{N}\) or zeroing them keeps deep stacks stable.
- Section 5.4 shows how to extend a framework with stateless layers, parameterized layers (
RMSNorm), layers holding fixed buffers (RandomFeatures), and custom backward passes (the straight-through estimator for non-differentiable rounding). - Section 5.5 compares fp32, fp16, and bf16, showing why 16-bit weights round away small updates and how mixed-precision training pairs fp32 master weights with 16-bit compute (and dynamic loss scaling for fp16).
- Section 5.6 covers safe weight serialization (
safetensorsvs. arbitrary code execution inpickle), crash-safe atomic training checkpoints that resume bit for bit, and auditing missing or unexpected keys when loading pretrained weights. - Section 5.7 places tensors and models on GPUs, explains asynchronous command queues and accurate GPU benchmarking, dissects live vs. reserved vs. driver memory, uses activation checkpointing to cut activation memory from \(O(K)\) to \(O(\sqrt{K})\), and wires GPU support into
d2l.Trainer. - Section 5.8 shows how to control pseudorandom seeds across workers and devices, why parallel GPU atomic additions are non-associative, and how to attach forward and backward hooks to inspect activation statistics, gradient norms, and the first layer that emits a NaN.
Resources and Further Reading
The resources below accompany the sections of this chapter; each names the section it goes with. All are freely accessible online except where noted.
Books
- Deep Learning with PyTorch — Stevens, Antiga & Viehmann — free PDF from the PyTorch team (archived copy; the original pytorch.org link has gone away); Part 1 covers tensors, storage, autograd, and
nn.Moduleat the same level as Section 5.1 and Section 5.2. - Machine Learning Systems — Vijay Janapa Reddi — free online; the systems view around this chapter: frameworks, data pipelines, training infrastructure, and efficient deployment (Section 5.7, Section 5.8).
Courses and video lectures
- CMU 10-414/714: Deep Learning Systems — Chen & Kolter — free lectures and assignments; students build “needle”, a miniature framework with autograd, modules, initialization, and GPU support, an implementer’s view of Section 5.1, Section 5.3, and Section 5.7.
- Neural Networks: Zero to Hero — Andrej Karpathy — free video series; builds autograd, modules, and training loops from scratch in plain Python, the abstractions behind Section 5.1 and Section 5.4.
- fast.ai Part 2: Deep Learning Foundations — Howard et al. — free; rebuilds a training framework from tensor operations upward, including modules, initialization, and mixed precision (Section 5.1, Section 5.3, Section 5.5).
Tutorials, notes, and interactive
- PyTorch internals — Edward Yang — free; a guided tour of tensors, strides, dispatch, and autograd inside PyTorch, one level below Section 5.1.
- JAX — The Sharp Bits — free; pure functions, explicit PRNG keys, and jit constraints — the functional worldview behind this book’s JAX tab, stated as a list of gotchas (Section 5.2, Section 5.8).
- Train With Mixed Precision — NVIDIA — free; the vendor guide to fp16/bf16 arithmetic and loss scaling that Section 5.5 distills.
- What Every Computer Scientist Should Know About Floating-Point Arithmetic — David Goldberg — free; the standard reference beneath every rounding and overflow issue in Section 5.5.
- Making Deep Learning Go Brrrr From First Principles — Horace He — free; a first-principles account of compute, memory, and framework overhead covered in Section 5.7.
- safetensors — Hugging Face — free; the file format for sharing weights without executing code on load, and why it replaced pickle-based checkpoints (Section 5.6).
- Reproducibility — PyTorch notes — free; the determinism flags, their costs, and their limits, complementing Section 5.8.
Foundational papers
- Automatic Differentiation in Machine Learning: a Survey — Baydin, Pearlmutter, Radul & Siskind (2018), JMLR — free; the definitive account of forward- and reverse-mode autodiff, the algorithm behind every custom gradient in Section 5.4.
- Mixed Precision Training — Micikevicius et al. (2018) — free; the origin of the fp16 + master-weights + loss-scaling recipe in Section 5.5.
- PyTorch: An Imperative Style, High-Performance Deep Learning Library — Paszke et al. (2019) — free; the design rationale (eager execution, autograd, memory allocator) for the imperative style of Section 5.1 and Section 5.7.