Benchmark a training run¶
Use this guide when you need a step time, tokens per second or MFU figure that someone else can trust: to compare two configurations, to report a result, or to check a published claim against your own run. You exclude compilation by rule, time a steady window with a synchronize per step, repeat the run, compute MFU on a stated convention, put the number next to its roofline floor, and write it into a report where every field has a source. Chapter 24 explains why each element is needed and how published claims fail without it.
Prerequisites¶
- A training step that is already correct (Parity-check a port) and compiled. Correctness comes first: a fast wrong step is not a result.
- This book's
code/ch24_evidence/(source):bench.py(steady_state,time_steps,warmup_steps,slow_prefix,compare) andreport.py, which reuse earlier chapters' percentile, MFU and roofline code. - A TorchTPU wheel built with optimizations on, if you build your own (
-c optfor a Bazel build): a fastbuild wheel "will silently ruin every measurement".
Steps¶
1. Fill the report spec before you measure¶
Copy code/ch24_evidence/examples/glimmer_training.json and replace every value and source: workload (model, step scope, tokens per step, precision, compile settings), hardware (accelerator, topology, whether rates are per chip or per device), software (image and versions), method (timer, excluded steps, repetitions), efficiency convention (FLOP count, parameter count, peak), and correctness evidence. An unknown value goes in as null with a source that says why.
Check: python code/ch24_evidence/report.py my_run.json renders, or names the field that is missing or has no source. Print torch.__version__ and your TorchTPU version in the job log so the versions row has a source.
2. Time each step with a synchronize¶
import sys
import torch
sys.path.insert(0, "code/ch24_evidence")
from bench import steady_state, time_steps
times = time_steps(lambda: step(batch), steps=60, sync=torch.accelerator.synchronize)
TPU execution is asynchronous: without the synchronize the host times only the dispatch. A sync per step gives a distribution (for percentiles and for spotting a compile); for a throughput figure once the window is known to be clean, you can also dispatch steps back to back and synchronize once after the last, as the FLUX case study does. Keep the profiler off and nothing else running in the timed window. Log the compile count after each step if you can.
Check: the list has one time per step, the first few far above the rest.
3. Exclude warm-up by rule, not by eye¶
Use TorchTPU perflab's rule: warm-up ends at the first step whose cumulative compile count equals the previous step's. warmup_steps(compile_counts) applies it (perflab's own minimum is 10 warm-up steps); without compile counts, slow_prefix(times) drops leading steps slower than twice the median of the second half.
st = steady_state(times, warmup=10) # ValueError if warmup >= len(times)
st["p50"], st["p90"], st["p99"], st["mean"], st["std"], st["n"]
Report compile time and first-step time in their own fields; they are real costs, just not the steady rate. Time at least perflab's default of 10 steady steps, more for percentiles: a nearest-rank p99 of fewer than 100 values is the maximum.
Check: no step in the window is far above the median, and the median barely moves if you drop one more warm-up step.
4. Derive tokens per second and MFU¶
Tokens per second is tokens per step over the mean steady step time. MFU is model FLOPs per second over the peak, and both halves carry a convention you must state:
| Convention | Counts | Used by |
|---|---|---|
| \(6N\) | 6 FLOPs per matmul parameter per token; no attention, no recompute | The TorchTPU announcement post |
| \(6N\) plus attention | Adds \(6\, n_\text{heads} (d_{qk} + d_v)\) per attended token per layer | TorchTitan's logged MFU |
| Counted ops | FlopCounterMode on one eager step; _grouped_mm counts zero |
TorchTPU perflab |
Keep the rate and the peak in the same unit: a v7x chip's bf16 peak is 2,307 TFLOP/s and a device's 1,153.5. mfu() in code/ch16_case_study/mfu.py computes the \(6N\) convention and code/ch14_fsdp/fsdp_memory.py the TorchTitan one.
Check: MFU is below 100%. Above it, or exactly half or double a reference, almost always means a per-chip rate against a per-device peak.
5. Put the number next to its floor¶
Estimate the step's floor with chapter 2's roofline: the larger of compute time at peak FLOP/s and memory time at peak bandwidth. For the Muse Glimmer 30B training step the floor is \(6N \times 40{,}960 / 1{,}153.5\) TFLOP/s \(= 5.65\) s per device against a measured 17.10 s, 3.03x.
Check: below the floor, an input to the floor is wrong. Near it, only less work helps. Far above it, capture an XProf trace of the steady window before changing anything (Capture and read an XProf trace).
6. Repeat, and compare configurations fairly¶
Run each configuration at least three times in separate processes and keep one summary per run (its median step time). Change exactly one thing, and when parallelism changes, keep the global token budget equal.
from bench import compare
compare(baseline_runs, candidate_runs) # one summary per independent run on each side
compare calls a change better only if it beats both 5% and \(1.645\) times the combined relative standard error, the form of vLLM-TorchTPU's accuracy gate. With one run per side it falls back to the flat 5% and says the variance is unknown.
Check: the verdict is better with variance_known true. A 3% gain from one run each is not a result.
7. Render the report¶
python code/ch24_evidence/report.py my_run.json -o my_run.md
Check: every number in the Results block traces to a provenance row, and the Gaps and flags block lists what a reviewer cannot check (short windows, single runs, MFU above 1, a stopwatch throughput). A report with gaps listed is defensible; one with gaps hidden is not. python code/ch24_evidence/report.py --cpu-smoke renders both examples and shows a broken spec refused; python code/ch24_evidence/bench.py --cpu-smoke reproduces the chapter's numbers (2,395 tokens per second and 33.0% MFU from the Muse Glimmer log). Both end cpu-smoke: OK.
On a TPU VM, python code/ch24_evidence/bench.py --m 4096 --k 4096 --n 4096 --steps 50 times a compiled bf16 matmul per step, finds its warm-up and prints the ratio of its median to the roofline bound: a quick check of the method on your machine.
Troubleshooting¶
| What you see | Likely cause | Check or fix |
|---|---|---|
| Throughput far below the published figure | Compilation inside the timed window, or an eager run | Exclude steps until the compile count is flat; compile with backend="tpu", dynamic=False |
| One run much worse than the last | Cold caches: weights from storage, programs not in AOTAutogradCache, bounded-dynamic programs recompiled per process |
Say which start you measured; warm up explicitly |
| Periodic spikes in the steady window | A recompile, garbage collection, or a host sync in the loop | fail_on_recompile; disable GC around the window; read device values every N steps |
| A speedup that does not survive a rerun | Inside run-to-run noise | Repeat runs; compare() |
| A parallelism change wins by more than its mechanism predicts | Different global token budget | Match the global budget |
| MFU above 100%, or half or double a reference | Per-chip against per-device | Put both in one unit |
| MoE MFU absurdly low | A counter that gives _grouped_mm zero FLOPs |
Count expert FLOPs from the active parameters |
| A p99 that changes a lot when one step is added | Too few steps | Time more steps; print n |
| Timings differ with and without the profiler | Profiler overhead in the window | Disable it in timed steps |
| Results differ from a recipe's with "the same" flags | A fastbuild wheel, different data or lengths, shape checks left on | Copy the recipe's command and environment exactly |
References¶
- torch_tpu repository: perflab
- TorchTPU docs: FLUX optimization
- torch_tpu repository: Muse Glimmer 30B FSDP training loop
- TorchTPU docs: Muse Glimmer 30B: training
- TorchTitan TPU experiments: Guide to optimizing LLMs on TorchTPU
- vLLM-TorchTPU: Benchmarking & Evaluation Guide
- vLLM-TorchTPU: Optimization Work Best Practices
- Introducing TorchTPU
- Companion code: code/ch24_evidence/
- Chapter 24: Performance you can defend: benchmarks, MFU, SLOs
- Chapter 16: Case study: Muse Glimmer 30B training