13 Computational Performance
Until now we have trained every model by writing the network, starting the training loop, and waiting, taking step time and memory as given. That stops working when a run takes days, a model does not fit on the card, or a second GPU makes training slower. Consider the GPT of Section 11.2, made twice as wide to give the GPU more work. Dividing the arithmetic of one training step by the GPU’s peak rate gives the shortest time the step could take, yet the measured step takes several times longer. Where does the rest of the time go?
Computer architects met this question long before deep learning did. By the 1990s processors had outpaced their memory so far that many programs spent most of their time waiting for data. In 2009 Williams, Waterman, and Patterson summarized the consequence in the roofline model (Williams et al. 2009): a program runs no faster than its arithmetic units allow, and no faster than memory can deliver the bytes it needs. A GPU driven from Python adds a third limit, the host cost of issuing each operation. Which of the three holds a step back decides which change speeds it up. This view explains choices made in earlier chapters, such as large minibatches and 16-bit numbers (Section 5.5), and gives a method: count the arithmetic and the bytes of a step, predict its time, measure it, and explain the difference.
Everything rests on Section 13.1, which introduces arithmetic intensity—the FLOPs an operation performs per byte it moves—and puts the method to work. Hardware (Section 13.2) explains where peak arithmetic and bandwidth come from. Compilation (Section 13.3) and the memory techniques of Section 13.4 are the single-GPU tools most practitioners use every day. Three sections then add GPUs: data parallelism from first principles (Section 13.5) and in practice (Section 13.6), followed by sharding the weights, gradients, and optimizer state across cards (Section 13.7) so that models larger than any single GPU can train. Finally, the case study (Section 13.8) returns to the GPT above on one and four GPUs and accounts for its step in milliseconds.
We assume you can time GPU code and read its peak memory (Section 5.7), know bf16 and tf32 (Section 5.5), and have met the GPT and its key–value cache (Section 11.2, Section 11.3). Every measurement comes from one host with four RTX 4090 cards, PyTorch 2.11, and JAX 0.12. On your machine the times will differ; the counts of FLOPs and bytes will not, nor will the method that turns them into predictions.