Skip to content

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

Part Chapters
0 How to read this book 0. How to read this book
I Foundations 1. TPU hardware for PyTorch engineers
2. Rooflines and arithmetic intensity
3. The TorchTPU stack and the CUDA translation table
4. Execution model: eager modes, deferred ops, torch.compile
5. Memory model, storage and views
6. Numerical precision and the MXU
7. Dynamic shapes and recompilation
II Run 8. Getting capacity and installing: VM and GKE
9. Porting a model, including Hugging Face Transformers and Diffusers
III Profile and optimize 10. Profiling with XProf
11. The optimization playbook
12. Custom kernels with Pallas
IV Train at scale 13. Sharding, meshes and collectives
14. FSDP and TorchTitan on TPU
15. Reinforcement learning with Ray
16. Case study: Muse Glimmer 30B training
V Serve at scale 17. Inference math: prefill, decode, KV cache
18. vLLM-TorchTPU
19. SGLang-TorchTPU
20. llm-d 1.0 on GKE: routing, disaggregation, KV management
21. Operating llm-d: autoscaling, rollouts, observability
22. Case study: Muse Glimmer 30B inference
VI Trust your results 23. Correctness: parity gates and numerical checks
24. Performance you can defend: benchmarks, MFU, SLOs
VII Extending the stack 25. Extending the stack: ATen lowerings, patch registry, plugins
26. Agent-driven workflows with the TPU Agent Suite
Appendix Conclusions and further reading
Production readiness checklist

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.

Eight dashed boxes stacked from top to bottom, one per Part in reading order, each with its name on the left and its chapter numbers as dark discs on the right. How to read this book: 0. I Foundations: 1, 2, 3, 4, 5, 6, 7. II Run: 8, 9, with the line Needs chapters 3, 4, 7. III Profile and optimize: 10, 11, 12, Needs chapters 2, 5, 7. IV Train at scale: 13, 14, 15, 16, Needs chapters 1, 2, 10. V Serve at scale: 17, 18, 19, 20, 21, 22, Needs chapters 2, 7, 13. VI Trust your results: 23, 24, Needs chapter 6. VII Extending the stack: 25, 26, Needs chapters 12, 23, 24. Short arrows join I to II, III to IV, IV to V and VI to VII. No arrows join How to read this book to I, II to III, or V to VI. From the left side of I, one trunk line runs down the left margin with a dot and a short arrow into III, IV and V, and ends in an arrow into VI. From the right side of III, a line runs down the right margin into VII. A legend reads: Part B has a prerequisite in Part A, with two small dashed boxes A and B joined by an arrow.

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.md that says what the script shows, which path needs a TPU, and what output to expect;
  • tests under tests/chNN/, which run the CPU path with pytest.

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.