Computation

Dive into Deep Learning · §5

Deep Learning Computation: From Prototype Code to Production Systems
module trees · parameter state · initialization · custom layers · numerics · checkpoints · GPUs · reproducibility

Scaling From a 4-Array MLP to an 8B Foundation Model

The Systems Challenge

  • Memory capacity (128 GB floor): Storing 8B weights takes 16 GB in bfloat16, yet training with Adam in float32 requires 128 GB of parameters, gradients, and optimizer moments before computing a single activation.
  • Numerical stability & precision: Unscaled residual branches blow up activation variance by 2^{32} across 32 blocks, while 16-bit weights silently round away small updates.
  • Fault tolerance, security & async GPUs: Atomic checkpoints (96 GB), safetensors vs. pickle RCE, synced GPU timers, and non-invasive layer hooks.

The Unifying Abstraction: A Model Is a Tree of Modules

Core Mental Model

  • Structured path addressing: Every parameter and non-trainable buffer is uniquely addressed by its path from the root module (e.g., block 1, layer 0, weight).
  • Unified tree traversals: Optimizer setup, initialization, safetensors serialization, dtype casting, GPU placement, and hooks all walk registered children recursively.
  • The silent registration invariant: Any layer or tensor stored in a plain Python attribute or list outside the module registry is invisible to optimizers, checkpoints, and GPUs.

Chapter 5 Roadmap: Architecture, State, and Execution

Architectural & Systems Roadmap

  • Model structure & state (§§5.1–5.4): Module trees and __setattr__ registration, the 16\text{ B/param} fp32 Adam floor, weight tying, (1+g^2)^N residual variance scaling (g = 1/\sqrt{N}), RMSNorm, fixed buffers, and the Straight-Through Estimator.
  • Precision, I/O & hardware (§§5.5–5.8): fp32 master weights with bf16/fp16 compute, zero-copy safetensors and 5-part atomic checkpoints, asynchronous GPU queues, O(\sqrt{K}) activation checkpointing, PRNG seeding, and forward/backward hooks.

Concrete Running Benchmarks Across the Chapter

Quantitative Anchors

  • Parameter & memory scaling (§§5.1–5.2, §5.5): Toy 4-block Residual MLP (729{,}866 params) vs. Llama 3 8B (291 tensors, 16 GB bf16 inference, 128 GB fp32 Adam training floor).
  • Signal stability & I/O (§5.3, §5.6): g = 1/\sqrt{N} caps 32-block std growth at \sqrt{e} \approx 1.65\times (vs. 65{,}536\times unscaled); 5-part 96 GB checkpoints resume bit for bit.
  • Activation memory (§5.7): Segmenting K = 16 blocks by s = \sqrt{K} = 4 cuts peak step RAM from 1.1\text{ GiB} \to 0.45\text{ GiB}.

How to Navigate This Chapter

Study & Engineering Guide

Each section combines runnable multi-framework code with systems diagnostics that prevent silent training failures:

  • Core path for all readers: Read §5.1–§5.3 to master module registration, parameter/buffer accounting, and residual variance scaling, then §5.5–§5.7 for mixed precision, safe checkpointing, and GPU memory management.
  • Framework-specific idioms: Pay close attention to how your framework handles list containers, lazy shapes, PRNG state, and graph compilation.
  • Diagnostic habits to adopt immediately: Count parameter and activation bytes before launching a job, verify initial activation standard deviations with a single forward pass, and audit missing/unexpected keys whenever loading external weights.