The Book¶
The Book is the narrative half of this site: 26 chapters in seven Parts, plus chapter 0 and an appendix, read in order. Chapter order is a dependency order. No chapter uses a concept that a later chapter introduces, and every chapter names its prerequisites at the top. If you need one fact rather than a chapter, the Reference and Guides tabs are faster.
About this book¶
The book is written for PyTorch engineers who know CUDA and now have to make a model work on Cloud TPU. It assumes PyTorch modules, autograd, torch.compile at the user level, the basics of torch.distributed, and a working picture of a Transformer. It does not assume any TPU, XLA or TorchTPU knowledge.
It covers the whole path from a first tensor on a TPU to a model that trains or serves at scale: the hardware and the cost arithmetic that predicts performance (Part I), getting capacity and porting a model (Part II), profiling and optimization down to custom Pallas kernels (Part III), sharded training with FSDP, TorchTitan and Ray (Part IV), serving with vLLM-TorchTPU, SGLang-TorchTPU and llm-d (Part V), the correctness and benchmarking checks that make results defensible (Part VI), and extending the stack itself (Part VII).
How to use it¶
Read Part I first whatever your goal, and chapter 8 before you run anything on a TPU: every later chapter builds on Part I, and every chapter with TPU code runs on the install of chapter 8. After that, three reading orders cover most jobs:
- Porting a model: chapters 9, 10, 11 and 23.
- Serving a model: chapters 10, 13, 17, 18, 19, 20, 21, 23 and 24, then the case study in chapter 22.
- Training a model: chapters 9, 10, 11, 13, 14, 15 (if you post-train with RL), 16, 23 and 24.
Chapter 0 explains each order and what to skim if you already know JAX or PyTorch/XLA. The map of Parts below shows which Parts build on which and the chapters each needs from earlier Parts, and every Part overview maps its own chapters' prerequisites, so you can also choose your own order: a chapter is safe to read once every chapter it lists as a prerequisite is done.
Each chapter names its prerequisites at the top and shows how to check its results: a parity test against a baseline, an expected compile count, an MFU you compute by hand, an SLO you measure. Part VI gathers those checks into one method.
Table of contents¶
Read end to end, the book takes about 16 hours, not counting the worked problems or the code. Each Part overview gives the reading time of its chapters.
Chapter dependencies¶
An arrow from Part A to Part B means a chapter in Part B lists a chapter in Part A as a prerequisite. Under each Part's name are the chapters from earlier Parts that its chapters list as prerequisites; each Part overview draws its chapters' prerequisites one by one. They are the same prerequisites the chapter openers show.
The long arrows come out of Part I: rooflines feed profiling, sharding and inference math (2 to 10, 13 and 17), dynamic shapes feed profiling and serving (7 to 10, 17 and 18), and precision feeds correctness (6 to 23). Profiling feeds sharding (10 to 13), sharding feeds inference math (13 to 17), and Pallas kernels feed extending the stack (12 to 25), so the training and serving reading orders both pass through chapters 10 and 13. Part II feeds no later Part as a prerequisite, and chapter 0 has no arrows because nothing depends on it.
Part I feeds every Part from II to VI directly, and Part II feeds none of the later Parts as a prerequisite. Under each Part's name are the chapters from earlier Parts that its chapters list as prerequisites; each Part overview maps them chapter by chapter.
Conventions¶
Units are decimal (GB, TB) unless a source uses binary units, which keep their GiB spelling. FLOPs count work and FLOP/s is a rate. TPU generations are written v5e, v5p, v6e and v7x. A chip is the physical package and a device is what PyTorch sees as one "tpu" device; a v7x chip exposes two devices, and every per-chip or per-device number says which it is. Chapter 0 has the full table.
Answers to worked problems are collapsed; open them when you have an answer of your own to compare. Warning boxes mark failure modes you are likely to meet.
Using the companion code¶
Chapters that include code have a folder in the repository's code/ directory, named code/chNN_<topic>/. Each folder holds:
- one or more scripts with two paths:
--cpu-smoke, which runs on any laptop and checks the logic and tensor shapes, and a TPU path, which needs a Cloud TPU VM with TorchTPU installed (chapter 8 sets one up); - a
README.mdthat says what the script shows, which path needs a TPU, and what output to expect; - tests under
tests/chNN/, which run the CPU path withpytest.
Worked problems that ask for code have solutions in code/solutions/chNN/, tested the same way. The Code tab lists every example. When a script cannot do anything useful without a TPU, its CPU path says so and exits cleanly rather than pretending to run.
