Skip to content

Part II Run

Cover art: a single TPU host on the left and, on the right, a cluster of hosts that receives containers.

Part II follows this picture from left to right: one TPU host that you create and log in to yourself, then a cluster of hosts that a scheduler fills with containers.

Part I gave you a model of the machine. Part II puts a real PyTorch program on it. You will request TPU capacity from Google Cloud, install TorchTPU on a TPU VM or run its container on Google Kubernetes Engine (GKE), prove that the install sees every chip it should, and then port a model, including Hugging Face Transformers and Diffusers models, until it runs on the TPU and gives the same answers as before.

Both chapters are recipes, and both end in a check you can run. A TPU VM that boots is not yet a working environment, and a model that runs without errors is not yet a correct port. The two chapters are written so that each step has an observable result: a VM state, a device count, a log line, a numerical comparison.

The chapters

Chapter Summary Reading time (min)
8. Getting capacity and installing: VM and GKE Getting TPU chips, installing TorchTPU on them, and proving that every host sees every device. 35
9. Porting a model, including Hugging Face Transformers and Diffusers What to change in an existing PyTorch model to run it on TorchTPU, and how to show the port is correct and fast. 35

Read end to end, Part II takes about seventy minutes, plus the time a TPU request waits in a queue.

Chapter 8 produces the environment that chapter 9 assumes. It covers the choices you make before any code runs: which capacity model (flex-start, spot, on-demand, queued resources, or a GKE node pool) fits the TPU generation and the length of your job; which quotas must be non-zero; whether the slice is a single host or several hosts that must all run the same commands; how to install TorchTPU so that it pulls the CPU build of PyTorch; and how to check, on one host and then on all of them, that the process sees the expected number of TPU devices. It closes with GKE, where the same program runs as a Kubernetes Job on one host or a JobSet across a multi-host slice.

Chapter 9 changes the model, not the machine. Starting from a device that answers torch.device("tpu"), it applies a six-item porting checklist (device, dtype, eager mode, host syncs, shapes, collectives), with a static scanner that finds the lines to change, to a small custom model and then to Hugging Face Transformers (device_map="tpu", static-cache generation, a Kernels Hub MoE kernel) and Diffusers (compiled on one chip, sharded across several with TensorParallelConfig). It reads the FLUX case study as a worked analysis of where a port's speedup comes from, and it ends every port with three checks: parity against a CPU reference, no recompiles after warm-up, and step time against a baseline. It uses the execution model (chapter 4), precision (chapter 6) and recompilation (chapter 7) from Part I to explain what goes wrong and why.

A map of Part II read from top to bottom. Under a note reading From earlier Parts, three dashed stub boxes in a row: 3 TorchTPU stack, 7 Dynamic shapes, 4 Execution model. Below the first stub, a column of two boxes joined by an arrow: 8 Capacity and install, then 9 Porting a model. Stub 3 has a straight arrow down into 8, and stub 7 an arrow that turns left into 8 as well. Stub 7 also has a long arrow down and left into the right side of 9, and stub 4 a longer one below it into the right side of 9. A legend along the bottom reads: Chapter B lists chapter A as a prerequisite, with an A box, an arrow and a B box; and Chapter in an earlier Part, with a dashed A box.

Chapter 8 needs chapters 3 and 7 from Part I, so you can request capacity and install TorchTPU as soon as Part I is done; chapter 9 adds the execution model of chapter 4 and a working install from chapter 8.

Chapter 8 needs the layers of the stack from chapter 3, to diagnose an install that shows no TPU device, and the warm-up compiles of chapter 7, to time a verification script fairly. Chapter 9 needs a working install from chapter 8, the host syncs of the execution model from chapter 4 and the recompilation rules from chapter 7.

What this Part covers

  • Choosing a capacity model for a TPU generation, a chip count and a job length, and the gcloud command that requests it.
  • Installing TorchTPU on a fresh TPU VM, and a script whose expected output shows that every host sees the expected number of TPU devices.
  • The TorchTPU examples on GKE as a single-host Job and as a multi-host JobSet, and what each topology setting in the manifest means.
  • Porting a PyTorch model, and the three checks that close a port: outputs that match a reference within the tolerance the precision rules of chapter 6 predict, no compiles after warm-up, and a step time that beats a recorded baseline.

Part III then measures what you have running: profiling with XProf, the optimization playbook and custom Pallas kernels.