13.9 Further Reading
The sections of this chapter teach one representative of each idea: one roofline with one memory bandwidth, one GPU and its links, one compiler per framework, three ways to save memory on one card, one collective algorithm, one way to divide a model’s state among GPUs, and one transformer made fast on one machine. Two open-access books make natural companions to the whole chapter: How to Scale Your Model (Austin et al., 2025) works out rooflines, sharding notation, and collective costs for TPUs and GPUs, and The Ultra-Scale Playbook (Hugging Face, 2025) surveys memory budgets, data parallelism, ZeRO, and multi-node parallelism with empirical measurements. For a quick introduction to the three regimes of Section 13.1, see Horace He’s essay (He 2022).
13.9.1 Measuring and Modeling
13.9.1.1 Finer Performance Models
The roofline of Section 13.1 errs in both directions. Some kernels run above its bandwidth roof because tensors that fit in the 72 MB L2 cache or shared memory arrive faster than \(\beta\). The cache-aware roofline (Ilic et al. 2014) draws one bandwidth roof per level of the memory hierarchy, and Yang et al. (2020) read these per-level bytes from hardware counters for deep learning kernels. Other kernels stay below the roof because they keep too few bytes in flight (Little’s law, Section 13.2.3). Volkov (2016) shows that independent instructions within one thread can stand in for extra warps, and that earlier GPU performance models mispredict throughput by up to 1.7 times by misapplying Little’s law.
13.9.1.2 Device Timers and System Profilers
d2l.Benchmark (Section 13.1) drains the queue before reading the clock, which would slow a live training loop. CUDA events (torch.cuda.Event) take timestamps on the GPU’s stream without draining it, though their interval includes any gaps in which the GPU waited for the host. The profiler of Section 13.1.4 separates busy from idle time for one process and exports traces for Perfetto. NVIDIA’s Nsight Systems places OS threads, memory copies, and NCCL calls across all processes on one timeline, while Nsight Compute measures a single kernel against both roofs.
13.9.1.3 The Input Pipeline
Readers who train on images or audio often meet a starved data loader before they meet Python overhead: the GPU waits while the CPU decodes and augments the next minibatch. When the host has fewer free cores than the GPU needs (Section 13.2.4.1), decoding can move to the GPU (e.g., via NVIDIA DALI). Mohan et al. (2021) analyze data stalls across training jobs and remove much of the stall time with a training-aware cache and shared preprocessing across hyperparameter sweeps.
13.9.1.4 Utilization and Time to a Target Loss
The kernel breakdown that ends the case study (Section 13.8.4.3) was carried out for BERT by Ivanov et al. (2021), who fused the bandwidth-bound kernels between matrix products to speed up an encoder layer by \(1.3\times\). The case study’s MFU column is the standard metric reported for large training runs (Chowdhery et al. 2022). Hardware FLOPs utilization (HFU) also counts recomputed FLOPs, so under activation checkpointing it exceeds MFU (Korthikanti et al. 2022); only MFU compares systems fairly because it credits only the work the model requires.
Throughput in tokens per second rewards any change that shortens a step, even one that requires more steps to converge. The modded-nanoGPT speedrun (Jordan and contributors 2024) therefore ranks changes by wall-clock time to train a GPT to a fixed validation loss on eight GPUs, pairing compiled kernels and low-precision formats with the Muon optimizer (Section 9.6).
13.9.2 Hardware and Number Formats
13.9.2.1 Other Accelerators
The roofline analysis applies unchanged to accelerators beyond Table 13.2.1. AMD’s Instinct MI355X, Google’s TPU7x, Intel’s Gaudi 3, and AWS’s Trainium2 all pair stacked HBM with matrix units. Their bf16 ridge points range from about 230 FLOP/byte (Trainium2) to over 450 (Gaudi 3), \(1.4\times\) to \(3\times\) the 164 FLOP/byte of our RTX 4090, making fusion and narrow formats even more important.
13.9.2.2 Unified Memory
Single-stream decoding runs no faster than memory bandwidth divided by the weight bytes (Section 13.2.5), provided the weights fit. Apple’s M3 Ultra shares up to 512 GB of memory between CPU and GPU at 819 GB/s. A 70B model in 8 bits (~70 GB) does not fit on a 24 GB card, whereas on the M3 Ultra the bandwidth bound allows roughly a dozen tokens per second. Such unified-memory machines trade a lower compute roof for high capacity, suiting single-stream inference rather than training.
13.9.2.3 Block-Scaled Formats in Detail
The block scales of Section 13.2.3 add a small bit overhead. In the OCP microscaling formats (Rouhani et al. 2023), an 8-bit power-of-two scale shared by 32 values adds 0.25 bits per value. NVIDIA’s NVFP4 shares an fp8 scale among 16 values (0.5 bits per value) plus one fp32 scale per tensor, allowing each block’s largest value to map near 6 (the top of the fp4 range) rather than only within a factor of two. Either way, a 4-bit weight or activation costs at most 4.5 bits.
13.9.3 Compilers and Kernels
13.9.3.1 Writing Fused Kernels
The compilers of Section 13.3 fuse elementwise chains and reductions ((Ansel et al. 2024) for PyTorch 2), but they do not discover tiled algorithmic rewrites such as FlashAttention. Triton (Tillet et al. 2019) (and JAX’s Pallas) makes a tensor tile the unit of programming: the author specifies the tiling and on-chip buffering while the compiler manages threads, vector instructions, and shared memory. FlashAttention-2 (Dao 2024) parallelizes across query tiles, and FlashAttention-3 (Shah et al. 2024) overlaps data movement with tensor-core products and exponentials on Hopper GPUs while adding fp8 support.
13.9.3.2 Compile Time at Scale
Compiling a small step (Section 13.3.3) takes seconds, whereas compiling a large model can take minutes per process and shape. Regional compilation compiles a repeated block (such as a Transformer layer) once for all its copies (torch.compiler.nested_compile_region), and persistent caches share compiled artifacts across processes (torch.compiler.save_cache_artifacts, jax_compilation_cache_dir). Autotuning (mode='max-autotune' in PyTorch; built into XLA) benchmarks multiple matrix-product tilings at compile time, trading a longer initial compilation for faster steps (Section 13.3.3).
13.9.3.3 Kernel Replay in Training
A recorded CUDA graph fixes shapes and memory addresses (Section 13.3.4). The CUDA graph trees behind mode='reduce-overhead' record forward and backward passes once per shape in a shared memory pool. Tensors retained across steps (such as a logged loss) are overwritten on the next replay unless cloned, and when the runtime cannot detect a step boundary automatically, torch.compiler.cudagraph_mark_step_begin marks it explicitly.
13.9.3.4 Exporting and Earlier Graph Designs
A captured graph can also run outside Python: torch.export captures a full graph strictly without graph breaks, and a jitted JAX function lowers to StableHLO for deployment on mobile, web, and serving runtimes. Earlier frameworks such as Theano (Bergstra et al. 2010) and TensorFlow 1 (Abadi et al. 2016) required users to construct static symbolic graphs explicitly; tracing compilers recover the same whole-program graph from ordinary eager code (Frostig et al. 2018).
13.9.4 Memory
13.9.4.1 Smaller Optimizer State
Adam’s two moment estimates occupy 8 of the 16 bytes per parameter in Section 13.4.1. Rather than sharding them across GPUs (Section 13.7), 8-bit Adam (Dettmers et al. 2022) quantizes both moments block-by-block to 2 bytes per parameter without losing accuracy, while Adafactor (Shazeer and Stern 2018) omits the first moment and factors the second moment into row and column sums.
13.9.4.2 Offloading to Host Memory and Disk
Host DRAM is an order of magnitude larger and cheaper than GPU memory. ZeRO-Offload (Ren et al. 2021) keeps fp32 master weights and Adam moments in host memory and runs the optimizer update on the CPU, leaving only 16-bit weights and gradients on the GPU; ZeRO-Infinity (Rajbhandari et al. 2021) extends offloading to NVMe drives.
13.9.4.3 Choosing What to Recompute
Storing only block inputs (Section 13.4.3, (Chen et al. 2016)) is one point on a trade-off curve. Binomial checkpointing (Griewank and Walther 2008) minimizes recomputation for a fixed number of stored states in a chain, and Checkmate (Jain et al. 2020) solves general computation graphs via integer linear programming. Modern compilers also apply selective recomputation automatically (AOTAutograd’s min-cut partitioner in PyTorch) or via explicit policies (jax.checkpoint_policies in JAX).
13.9.4.4 Fragmentation and Preallocation
When tensor shapes vary across steps, PyTorch’s caching allocator can split free memory into non-contiguous fragments (“reserved but unallocated”); setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True allows segments to grow in place. In JAX, the default allocator preallocates 75% of GPU memory on startup (XLA_PYTHON_CLIENT_PREALLOCATE=false disables preallocation, and XLA_PYTHON_CLIENT_ALLOCATOR=vmm uses CUDA virtual memory management).
13.9.5 Communication and Scale
13.9.5.1 Collective Algorithms beyond the Ring
The ring allreduce (Section 13.5.3) minimizes total bytes moved but takes \(2(k-1)\) sequential hops. Tree algorithms scale their hop count as \(\log_2 k\), and NCCL pairs two complementary binary trees so that every device is a leaf in one and an internal node in the other. When \(k\) is a power of two, recursive halving and doubling achieves the ring’s byte volume in \(\log_2 k\) steps (Thakur et al. 2005).
13.9.5.2 Parameter Servers and Asynchronous Updates
Before collective libraries became standard, distributed training aggregated gradients on parameter servers (Dean et al. 2012; Li et al. 2014), supporting asynchronous updates and local SGD (Stich 2019). Because stale gradients alter optimization dynamics, synchronous allreduce/ZeRO ((Li et al. 2020; Rajbhandari et al. 2020)) is now standard for dense models, while parameter servers remain common for massive sparse recommender tables (Chapter 42).
13.9.5.3 Parallelism Interfaces and Beyond One Machine
PyTorch exposes each parallelism strategy through a composable API (DistributedDataParallel, fully_shard, torch.distributed.tensor.parallel, torch.distributed.pipelining) on a device mesh, whereas JAX expresses all of them declaratively via array placements on a named mesh compiled with GSPMD / Shardy (Xu et al. 2021). Multi-node tensor, pipeline, and expert parallelism (Shoeybi et al. 2019; Narayanan et al. 2021; Lepikhin et al. 2021) (Chapter 23), fp8 training (Micikevicius et al. 2022; DeepSeek-AI et al. 2024) (Section 21.3), continuous batching and PagedAttention (Yu et al. 2022; Kwon et al. 2023) (Chapter 30), and weight quantization (Chapter 28) are developed in the Language Models part.