4 Compute and Systems
  1. In this chapter
  2. LLM hardware basics
  3. GPUs and the Roofline Model
  4. GPU kernels
  5. Numerical Precision
  6. Collective Communication
All chapters →

Part 2 · Train LLMs Chapter 4

Compute and Systems

GPU compute and bandwidth, the roofline model, numerical precision and collective communication. The same algorithm can differ tenfold in speed with another implementation, batch size or precision, so the hardware limits have to be understood first.

5 pages about 2.5 h Chapter labs on GitHub ↗ Beginner → Advanced Updated

After this chapter you will be able to

  • Use the roofline model to decide whether an operator is limited by compute or by memory bandwidth, and explain why batched decoding is bandwidth-bound.
  • Explain the trade-offs between FP32, BF16, FP16 and FP8 in range and precision, and the role of accumulation precision and loss scaling in mixed-precision training.
  • Write down the communication volume of all-reduce, all-gather and reduce-scatter, and estimate communication time in multi-GPU training with the alpha-beta model.
  • Apply the four walls of compute, bandwidth, capacity and communication to the pretraining and inference chapters, and say which wall each optimization sidesteps.

If CPUs, GPUs, host memory, and device memory are new to you, start with LLM hardware basics, then use the roofline to compare bottlenecks.

The first three chapters were about algorithms: what a model is and how attention is computed. This chapter looks at the hardware. The same algorithm can differ tenfold in speed with a different implementation, batch size or numerical precision. Training a large model costs millions of GPU hours and inference is priced in dollars per million tokens, and both bills are set by the limits that hardware draws around the algorithm.

The chapter answers three questions: what actually limits an operator (compute or memory bandwidth); why both training and inference keep moving to lower precision, and where low precision breaks; and how the communication between the GPUs of a large training job shapes the parallelism strategy. Once you have them, many designs in the pretraining and inference chapters, such as FlashAttention, GQA, continuous batching, ZeRO and pipeline parallelism, turn into different answers to one question: how to get around one of the walls.

Every page in this chapter comes with experiments you can run. The roofline page measures the same curves on a laptop without a GPU; the experiments on numerical precision and collective communication are fully reproducible.

GPU kernels uses matrix multiplication to connect layout and logical traffic with tile sizes, fusion, and device measurements.

Chapter roadmap

  1. 4.1

    LLM hardware basics: CPUs, GPUs, memory, and moving data

    Follow a model request through disk, host and device memory, and CPU and GPU work. Estimate tensor storage and copy time to distinguish capacity, bandwidth, and compute before diagnosing inference.

    Beginner 6 min read

    Prerequisites2.1Python and tensor shapes

  2. 4.2

    GPUs and the Roofline Model: Why Big Models Are Bound by Memory Bandwidth

    A GPU is limited by two walls: how many operations it can do per second (compute) and how many bytes it can move per second (memory bandwidth). The roofline model uses arithmetic intensity to tell which wall an operator hits first. This page derives it and measures it on a CPU.

    Intermediate 41 min read

    Prerequisites4.1LLM hardware basics3.4Transformer Block Explained

  3. 4.3

    GPU kernels: from tiled matrix multiplication to fusion

    GPU kernel tuning combines thread-level memory access, on-chip reuse, and output stores. Derive the logical traffic of tiled matrix multiplication with edge masks, check it with a CPU experiment, and trace practical limits from staging buffers to register spills.

    Intermediate 9 min read

    Prerequisites4.2GPUs and the Roofline Model

  4. 4.4

    Numerical Precision: Three Ways FP16, BF16 and FP8 Training Goes Wrong

    Floating-point formats split a few bits between exponent (range) and mantissa (precision). Low-precision training fails in three ways: overflow, underflow (small gradients become 0) and swamping (small terms vanish in sums). Real Qwen2.5-0.5B data shows how large each is and what fixes it.

    Intermediate 31 min read

    Prerequisites4.2GPUs and the Roofline Model2.6Optimizers and Training Stability

  5. 4.5

    Collective Communication: Why All-Reduce Has So Many Algorithms

    Collective communication is the plumbing of multi-GPU training. This page simulates five all-reduce algorithms and checks them element by element, compares rounds and traffic with the alpha-beta model, then shows how topology, summation order and MoE all-to-all change the choice.

    Advanced 48 min read

    Prerequisites4.2GPUs and the Roofline Model4.4Numerical Precision