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

Courses and video lectures

Tutorials, notes, and interactive

Foundational papers