TorchTPU Book¶
Move a real PyTorch model to Cloud TPU with TorchTPU: run it, profile it, optimize it, serve it and train it at scale, and know when the numbers you get are correct and real.
Who this is for¶
You write PyTorch for a living, probably on CUDA, and you now have to make a model work on Cloud TPU. You already know:
- PyTorch modules, autograd,
torch.compileat the user level, andtorch.distributedbasics (DDP or FSDP). - How a Transformer is put together: attention, MLP blocks, the KV cache at a sketch level.
- Your way around a Linux shell and a cloud console.
You are not expected to know TPU hardware, XLA, StableHLO, or the TorchTPU projects (torch_tpu, vLLM-TorchTPU, SGLang-TorchTPU, TorchTitan, llm-d, the TPU Agent Suite). The book builds that up from the chip outward.
Map of the book¶
The seven Parts in reading order, each with the topics it covers. Trust your results comes after training and serving: you can judge a result only once you can produce it.
Parts of the book¶
-
Part 0: How to read this book
What TorchTPU is, reading orders by goal, notation and units, and how the companion code runs.
-
Part I: Foundations
TPU hardware, rooflines, the TorchTPU stack, execution and memory models, precision, dynamic shapes.
-
Part II: Run
Get TPU capacity on a VM or GKE, install TorchTPU, and port a model, including Hugging Face models.
-
Part III: Profile and optimize
Read XProf traces, work through the optimization playbook, and write Pallas kernels when XLA is not enough.
-
Part IV: Train at scale
Meshes and collectives, FSDP and TorchTitan, reinforcement learning with Ray, and a 30B training case study.
-
Part V: Serve at scale
Prefill, decode and KV cache math, vLLM-TorchTPU, SGLang-TorchTPU, and llm-d on GKE.
-
Part VI: Trust your results
Numerical parity gates, and benchmarks, MFU and SLO measurements you can defend.
-
Part VII: Extending the stack
Extend the stack with ATen lowerings and plugins, and build agent workflows with the TPU Agent Suite.
-
Guides
Task pages for vLLM-TorchTPU, llm-d operations and profiling recipes, each linked to the chapter that explains why.
-
Reference
API matrix, environment variables, llm-d resources, hardware generations, glossary, CUDA translation table, FAQ.
-
Cheatsheets
The Golden cheatsheet (a dense quick reference to the whole stack, by topic), one sheet per repository, and printable one-page references.
-
Code
Companion examples with a
--cpu-smokepath for your laptop and a TPU path, plus exercise solutions.
Where to start¶
Every reading order begins with the same core: chapter 0, Part I (chapters 1 to 7) and chapter 8 to get a TPU and install TorchTPU. After the core, follow the order that matches the job in front of you.
You have a PyTorch model that runs on GPU and you need it to run, correctly and at a reasonable speed, on TPU.
- Ch 9: Porting a model
- Ch 10: Profiling with XProf
- Ch 11: The optimization playbook
- Ch 23: Correctness: parity gates and numerical checks
Done when the port passes a parity check against your GPU or CPU baseline and you can name the top item in its profile.
You need an LLM behind an endpoint on TPU, with latency and throughput you can state.
- Ch 10: Profiling with XProf and Ch 13: Sharding, meshes and collectives, whose collective costs chapter 17 uses for tensor parallelism
- Ch 17: Inference math
- Ch 18: vLLM-TorchTPU, then Ch 19: SGLang-TorchTPU, whose radix cache llm-d's routing builds on
- Ch 20: llm-d architecture and Ch 21: Operating llm-d
- Ch 23: Correctness and Ch 24: Performance you can defend
- Ch 22: Muse Glimmer 30B inference case study
Done when you can report latency and throughput from a benchmark method you can defend, against a stated SLO.
You need to train or fine-tune on one host or many slices and get utilization worth paying for.
- Ch 9: Porting a model and Ch 10: Profiling with XProf
- Ch 11: The optimization playbook
- Ch 13: Sharding, meshes and collectives
- Ch 14: FSDP and TorchTitan, then Ch 15: RL with Ray if you post-train with RL
- Ch 16: Muse Glimmer 30B training case study
- Ch 23: Correctness and Ch 24: Performance you can defend
Done when you can compute the MFU of your run yourself and show that the loss matches a reference.
Chapter 0 explains these reading orders in more detail, including what to skim when time is short.
How this site is organized¶
| Tab | Use it when | What is in it |
|---|---|---|
| Book | You want the full explanation, in order | 26 chapters in seven Parts, plus chapter 0 and an appendix; every chapter shows how to check its results and cites its sources |
| Guides | You know what you want to do and need the steps | Task pages that link back to the chapter explaining the reason |
| Reference | You need one fact: an API, a flag, a hardware number | Tables and a glossary, each row tied to a source |
| Cheatsheets | You want a dense summary to review or print | The Golden cheatsheet (a dense quick reference by topic), per-repository sheets and printable one-page references |
| Code | You want to run something | Companion scripts with CPU and TPU paths, tests and solutions |
Conventions and companion code¶
The book uses decimal GB and TB, counts work in FLOPs and rates in FLOP/s, writes TPU generations as v5e, v5p, v6e and v7x, and keeps "chip" (the physical package) apart from "device" (what PyTorch sees as one "tpu" device); a v7x chip exposes two devices. Chapter 0 has the full notation table.
Chapters that include code have a folder under code/ in the book's repository. Every script has a --cpu-smoke path that runs on a laptop and checks logic and shapes, and a TPU path for a Cloud TPU VM; tests run the CPU path with pytest. The Code tab lists them all.
Every technical claim on this site cites the public document it rests on, in the References section of the page that makes it. The concepts in Parts I, IV and V follow Google DeepMind's How to Scale Your Model, which is written for JAX; the chapters translate them to PyTorch and TorchTPU and link the original sections.
