Part III Profile and optimize¶
Read the picture from left to right: a timeline full of short ops and idle gaps becomes one with long, packed blocks. The lens in the middle is the profiler, and the tiles inside it are where the last chapter of this Part works.
Part II ended with a model that runs on the TPU, gives the right answers and stops compiling after warm-up. Part III asks where its step time goes and what to do about it. You will capture a trace with torch.profiler and read it in XProf, turn what you see into a ranked list of causes, remove the common ones (host syncs, recompiles, hidden copies, unfused ops), and, when one operation still dominates after that, replace it with a custom Pallas kernel.
The three chapters form one loop, not a sequence you finish once. A profile names the slowest thing; a change removes it; a second profile shows whether the change worked and what is now the slowest thing. Every chapter in this Part ends with evidence of that kind: a trace whose findings changed after a fix, or a step time that moved by a measured amount.
The chapters¶
| Chapter | Summary | Reading time (min) |
|---|---|---|
| 10. Profiling with XProf | Capturing a trustworthy trace of a PyTorch program on a TPU, and reading it to find what bounds the step. | 35 |
| 11. The optimization playbook | Choosing the first change from what the profile shows, and showing that it helped. | 40 |
| 12. Custom kernels with Pallas | Writing, verifying and calling your own TPU kernel from PyTorch when the compiler's version of one op is the bottleneck. | 40 |
Read end to end, Part III takes about two hours, plus the time to profile your own model.
What links them¶
Chapter 10 produces the evidence that chapters 11 and 12 act on. It shows what the TPU profiler records (a host trace and a device trace, written as an XPlane file that XProf reads), how to capture it from PyTorch with torch.profiler.profile, the TPU activity and TpuProfilerConfig, and how to read the result in a fixed order: step time, then idle time on the device, then the ops that take the most time, then whether each of those is limited by compute, memory or communication. It ties the signatures you will see most often (gaps from host syncs, repeated compiles, collectives waiting, copies you did not write, padding) back to the Part I chapters that explain them.
Chapter 11 turns a profile into a change. It takes the bound types from chapter 10 and maps each to the optimizations that address it, in the order that usually pays off: remove host syncs and recompiles, compile, eliminate layout copies, then look at the largest remaining ops. Each step is checked the same way chapter 10 checks a capture, by profiling again and comparing step times.
Chapter 12 is the last resort that chapter 11 points to. When the playbook leaves one op dominating the step and the compiler's fusion of it is the limit, a Pallas kernel lets you control how data moves between HBM and on-chip memory tile by tile. You will write one, verify it against a reference, and call it from PyTorch, and then profile it with the tools from chapter 10.
Read 10, 11 and 12 in order. Chapter 10 needs only rooflines and dynamic shapes from Part I, not a port from Part II; the memory model of chapter 5 comes back in both 11 and 12.
Chapter 10 profiles any loop you can run, so a port from chapter 9 is the usual subject but not a prerequisite. It leans on Part I: the roofline test from chapter 2 names a bound type, and the recompilation rules from chapter 7 explain uniformly slow steps. The gaps on a timeline are explained by the deferred execution model of chapter 4, which chapter 7 builds on. Chapters 11 and 12 each build on the one before and on the view and tile rules of chapter 5, which explain unexpected copies and padded shapes.
What this Part covers¶
- Capturing a trace of a few steady-state steps of a training or inference loop, without the warm-up compiles, that holds the number of steps asked for.
- Reading a trace: the fraction of a step the device was idle, the top ops by time, and whether each is compute-bound, memory-bound or waiting on communication, with a roofline estimate behind the claim.
- Applying the optimization that fits the evidence, profiling again, and reporting the step time before and after with the measurement conditions.
- Pallas kernels for an op that dominates the profile: writing one, verifying it against a reference implementation and calling it from a PyTorch model.
Part IV then scales the profiled, optimized model out: sharding across meshes, FSDP with TorchTitan, and reinforcement learning with Ray.

