Skip to content

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.compile at the user level, and torch.distributed basics (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

Seven cards in two rows, read left to right and top to bottom and joined by arrows, each with a Part name in its header and its topics below. First row: Foundations (TPU hardware, rooflines, execution, memory, precision, shapes), Run (capacity, install, porting models), Profile and optimize (XProf, tuning, Pallas kernels), Train at scale (sharding, FSDP, RL with Ray, 30B training). Second row: Serve at scale (inference math, vLLM, SGLang, llm-d), Trust your results (parity, benchmarks, MFU, SLOs), Extending the stack (lowerings, patches, plugins, agents).

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.

    Start with chapter 0

  • Part I: Foundations


    TPU hardware, rooflines, the TorchTPU stack, execution and memory models, precision, dynamic shapes.

    Open Part I

  • Part II: Run


    Get TPU capacity on a VM or GKE, install TorchTPU, and port a model, including Hugging Face models.

    Open Part II

  • Part III: Profile and optimize


    Read XProf traces, work through the optimization playbook, and write Pallas kernels when XLA is not enough.

    Open Part III

  • Part IV: Train at scale


    Meshes and collectives, FSDP and TorchTitan, reinforcement learning with Ray, and a 30B training case study.

    Open Part IV

  • Part V: Serve at scale


    Prefill, decode and KV cache math, vLLM-TorchTPU, SGLang-TorchTPU, and llm-d on GKE.

    Open Part V

  • Part VI: Trust your results


    Numerical parity gates, and benchmarks, MFU and SLO measurements you can defend.

    Open Part VI

  • Part VII: Extending the stack


    Extend the stack with ATen lowerings and plugins, and build agent workflows with the TPU Agent Suite.

    Open Part VII

  • Guides


    Task pages for vLLM-TorchTPU, llm-d operations and profiling recipes, each linked to the chapter that explains why.

    Open Guides

  • Reference


    API matrix, environment variables, llm-d resources, hardware generations, glossary, CUDA translation table, FAQ.

    Open Reference

  • Cheatsheets


    The Golden cheatsheet (a dense quick reference to the whole stack, by topic), one sheet per repository, and printable one-page references.

    Open Cheatsheets

  • Code


    Companion examples with a --cpu-smoke path for your laptop and a TPU path, plus exercise solutions.

    Open Code

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.

  1. Ch 9: Porting a model
  2. Ch 10: Profiling with XProf
  3. Ch 11: The optimization playbook
  4. 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.

  1. Ch 10: Profiling with XProf and Ch 13: Sharding, meshes and collectives, whose collective costs chapter 17 uses for tensor parallelism
  2. Ch 17: Inference math
  3. Ch 18: vLLM-TorchTPU, then Ch 19: SGLang-TorchTPU, whose radix cache llm-d's routing builds on
  4. Ch 20: llm-d architecture and Ch 21: Operating llm-d
  5. Ch 23: Correctness and Ch 24: Performance you can defend
  6. 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.

  1. Ch 9: Porting a model and Ch 10: Profiling with XProf
  2. Ch 11: The optimization playbook
  3. Ch 13: Sharding, meshes and collectives
  4. Ch 14: FSDP and TorchTitan, then Ch 15: RL with Ray if you post-train with RL
  5. Ch 16: Muse Glimmer 30B training case study
  6. 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.