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. The first half of the chapter develops that structural abstraction and shows how framework operations traverse it. We begin with module hierarchies, container registration, residual skip connections, lazy shape inference, and config-driven builders (Section 5.1), then open the state tree to separate trainable parameters from non-trainable buffers, derive the 16-byte-per-parameter memory floor of fp32 Adam training, count parameters without allocating storage, and share or freeze weights (Section 5.2). Next, we move from per-layer variance rules to whole-tree and residual initialization, showing why unscaled residual branches amplify activation variance as \((1 + g^2)^N\) and how \(1/\sqrt{N}\) or zero output-projection scaling stabilizes deep stacks (Section 5.3), before extending the framework with stateless layers, parameterized layers (RMSNorm), fixed-buffer layers (RandomFeatures), and custom backward rules via the straight-through estimator (Section 5.4).
The second half turns from model structure to numerical representation, persistence, hardware execution, and diagnostics. We compare fp32, fp16, and bf16, explaining why 16-bit weights round away small updates and how mixed-precision training pairs fp32 master weights with 16-bit compute and fp16 loss scaling (Section 5.5). We then contrast unsafe pickle archives with code-free safetensors files, construct atomic five-part training checkpoints that resume bit for bit, and audit missing or unexpected keys when loading pretrained backbones (Section 5.6). Finally, we place tensors and models on GPUs, time asynchronous command queues accurately, dissect GPU memory alongside \(O(\sqrt{K})\) activation checkpointing (Section 5.7), and combine pseudorandom seed control and GPU determinism with non-invasive forward and backward hooks (Section 5.8).
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); 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, covering 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 in which students build “needle”, a miniature framework with autograd, modules, initialization, and GPU support (Section 5.1, Section 5.3, Section 5.7).
- Neural Networks: Zero to Hero (Andrej Karpathy): free video series building autograd, modules, and training loops from scratch in plain Python (Section 5.1, Section 5.4).
- fast.ai Part 2: Deep Learning Foundations (Howard et al.): free course rebuilding 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): a guided tour of tensors, strides, dispatch, and autograd inside PyTorch, one level below Section 5.1.
- JAX: The Sharp Bits: pure functions, explicit PRNG keys, and JIT constraints, distilling the functional design behind JAX as a list of practical gotchas (Section 5.2, Section 5.8).
- Train With Mixed Precision (NVIDIA): 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): the standard reference beneath every rounding and overflow issue in Section 5.5.
- Making Deep Learning Go Brrrr From First Principles (Horace He): a first-principles account of compute, memory bandwidth, and framework overhead (Section 5.7).
- safetensors (Hugging Face): the file format for sharing weights without executing code on load, and why it replaced pickle-based checkpoints (Section 5.6).
- Reproducibility (PyTorch notes): determinism flags, their performance costs, and their limits, complementing Section 5.8.
Foundational papers
- Automatic Differentiation in Machine Learning: a Survey (Baydin, Pearlmutter, Radul & Siskind, 2018, JMLR): the definitive survey of forward- and reverse-mode autodiff, the algorithm behind every custom gradient in Section 5.4.
- Mixed Precision Training (Micikevicius et al., 2018): 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): the design rationale (eager execution, autograd, caching allocator) for the imperative style of Section 5.1 and Section 5.7.