11. Conclusions
This last section is just thanks and a reading list, so here's a short recap instead. Basically every chapter is the same move: count the FLOPs, count the bytes, divide each by the right hardware number, and take the max.
- Rooflines: A TPU v5e does 1.97e14 FLOPs/s against 8.2e11 bytes/s of HBM, roughly 240 FLOPs/byte, and a bf16 matmul's intensity is about . So we need more than 240 tokens per batch to be compute-bound.
- The speed hierarchy: VMEM, HBM, ICI, PCIe, DCN, fastest to slowest. The traffic on each link has to scale with its speed, so DCN only gets the infrequent transfers.
- Sharding: A matmul only needs communication when a contracting dimension is sharded: an AllGather first if one input is, an AllReduce or ReduceScatter after if both are. Throughput-bound, an AllGather takes whatever the ring size.
- Transformer math: About 2 FLOPs per parameter per token for a forward pass, 6 for training, and the MLP holds most of the parameters and FLOPs.
- Training: Data parallelism and FSDP go comms-bound below about 2,550 tokens per chip on a TPU v5p, tensor parallelism past about shards, and mixing the two gets us down to roughly 100 tokens.
- Inference: Prefill looks like training and is compute-bound. Generation needs 240 requests in flight to get there, so at small batch the step time is just bytes over bandwidth, and the KV cache decides how big a batch we can afford.
The full series is at jax-ml.github.io/scaling-book.
Further Reading
The reading list from the end of How to Scale Your Model:
- TPU Deep Dive: the TPU architecture in depth.
- Domain specific architectures for AI inference: AI inference from both the hardware and the model side.
- A Domain-Specific Supercomputer for Training Deep Neural Networks: one of the original TPU papers.
- Making Deep Learning Go Brrrr From First Principles: rooflines and performance engineering from the GPU and PyTorch side.
- Writing TPU Kernels with Pallas: custom TPU kernels in Pallas, plus lower-level TPU details.
- How to Optimize a CUDA Matmul Kernel for cuBLAS-like Performance: a Worklog: optimizing a matmul kernel in CUDA.
- Distributed arrays and automatic parallelization: JAX's parallelism APIs, for actually implementing some of these ideas.
- Rafi Witten's High Performance LLMs 2024 Class: a course on TPU performance engineering, with slides on GitHub.
- Efficiently Scaling Transformer Inference: the math of Transformer inference, credited as the inspiration for a lot of the series.
- Huggingface Ultra-Scale Playbook: roughly the GPU counterpart to the series, on PyTorch's parallelism and memory-saving tricks for training.
- Transformer Inference Arithmetic: an illustrated blog on inference arithmetic.
- Stanford CS336 Slides and Videos: a course on LLM training and serving. The authors point to Assignments 1 and 2.
- Stas Bekman's ML Engineering Handbook: practical ML infrastructure, like negotiating with cloud providers, cluster management and measured GPU throughput.
- ezyang's blog: sharding and PyTorch, from a PyTorch lead. The two posts linked are the PyTorch internals guide and the post on sharded matmuls.
- The Anatomy of Collective Communication: GPU and TPU collectives. The authors rate its N-D and GPU coverage above their own.