Skip to content

Profiling steps

Capture

from torch.tpu.profiler import TpuProfilerConfig
config = TpuProfilerConfig(host_tracer_level=2, device_tracer_level=1,
                           python_tracer_level=0, run_dir=log_dir)
for _ in range(warmup_steps):               # compiles happen here, outside the trace
    train_step(batch)
torch.accelerator.synchronize()
with torch.profiler.profile(
        activities=[torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.TPU],
        experimental_config=config,
        on_trace_ready=torch.profiler.tensorboard_trace_handler(str(log_dir))) as prof:
    for i in range(5):
        with torch.profiler.record_function(f"train_step#{i}"):
            train_step(batch)
            torch.accelerator.synchronize()  # host range covers the device work
        prof.step()
TpuProfilerConfig argument Default Meaning
host_tracer_level 2 1 user TraceMe events; 2 adds XLA; 3 adds low-level XLA
device_tracer_level 1 0 off; 1 TPU activity
python_tracer_level 0 1 records Python calls (overhead)
run_dir None where <hostname>.xplane.pb is written
worker_rank None string suffix so ranks on one host do not overwrite each other
experimental_options None v7x hardware counters (tpu_enable_periodic_counter_sampling)

Needs PyTorch 2.12 or newer. View: pip install "setuptools<81", pip install xprof tensorboard, then xprof --logdir=/tmp/profiler_output --port=6006 (point at the directory, not a file).

Check the capture before reading it

  1. The directory holds an .xplane.pb file.
  2. The trace holds the number of steps you asked for, and no step is far longer than the others (a long first step means warm-up got in).
  3. The median step is within about 5 to 10% of the same loop timed without the profiler.

Read in this order

Stage Look at If you see Go to
1. Step time step ranges outliers, or far from wall clock fix the capture
2. Device idle XLA Ops row, then host rows at each gap large gaps host syncs, input, dispatch (Ch 4)
every step slow, short bursts recompiles or graph breaks (Ch 7)
3. Top ops Op Profile, or key_averages() sorted by self_device_time_total a few ops dominate click one: HLO name, shape, layout, source line
4. Bound type each top op against its roofline estimate (Ch 2) near the estimate fewer FLOPs or fewer bytes (Ch 11)
far above it a copy, padding, a poor layout or a collective wait

Idle comes before top ops: the op ranking cannot see idle time.

Common signatures

In the trace Likely cause
gaps lined up with host work; aten::item, aten::_local_scalar_dense host sync or synchronous data loading
many short bursts in eager mode microbubbles: ops shorter than the roughly 50 µs enqueue cost
copy ops you did not write a view forced a layout change, often in backward
matmul far below its roofline, low MXU use dimensions not multiples of 128
collective with idle compute around it not overlapped, or slow link for its size (Ch 13)

Healthy end state: long continuous TPU blocks, small host gaps, MXU utilization above 60%.

Serving and multi-host

  • vLLM: configured is not armed. --profiler-config.profiler torch plus a directory records nothing until /start_profile; stop with /stop_profile. Phased capture: USE_PHASED_PROFILER=true. Profile with the attention backend you serve.
  • Never report profiled numbers: profiling cost about 11% TTFT and 4% throughput in vLLM benchmarks.
  • Keep python -m vllm_torchtpu.tools.merge_xplane <dir> output out of plugins/profile/<timestamp>/, or XProf double-counts cores.
  • Each process captures its own chips: distinct worker_rank or run_dir per rank; TORCH_TPU_XPROF_SESSION_ID shares a session ID across hosts.
  • After a fix, capture again with the same settings: the finding must be gone and the unprofiled step time must drop by about what it predicted.