Skip to content

Execution and recompiles

The four modes

Mode Select it with One executable holds XLA Use it for
Strict (default) nothing; EagerMode.DEFER_NEVER one op -O1 fastest first result, notebooks, tests
Debug TORCH_TPU_LAUNCH_BLOCKING=1 one op, host waits -O1 errors at the line that caused them; never production
DeferAndFuse TORCH_TPU_DEFER_AND_FUSE=1 ops up to a materialization point or detected loop -O2 faster eager when torch.compile is not an option
Compiled torch.compile(fn, backend="tpu") a whole FX graph -O2 best warmed-up throughput and latency
  • Variables are read once at import torch; set them before Python starts. Launch blocking wins if both are set.
  • Inside a process: with torch.tpu.set_eager_mode(torch.tpu.EagerMode.DEFER_AND_FUSE):. Not allowed inside a compiled region.
  • Know what ran: print(torch.tpu.eager_mode).

Materialization points (host syncs)

t.item(), t.cpu(), print(t), torch.accelerator.synchronize(), if t.any():, while loss > tol:, torch.nonzero, torch.masked_select. Each makes the host wait; under DeferAndFuse or torch.compile it also cuts the graph. Keep decisions on the device: torch.where(cond, a, b), torch.clamp, masks. Log every N steps, not every step.

torch.compile and shapes

Setting On TorchTPU
dynamic not passed dynamic=False: one executable per exact shape; a new shape recompiles
dynamic=None bounded dynamism: buckets on a 128 grid (bucket_size 128, num_dynamic_buckets 1 to 8); one range guard
torch._dynamo.mark_dynamic(t, dim, min=, max=) bounded dynamism from the first call
dynamic=True NotImplementedError

Bounded dynamism pays for the bucket bound \(U\), not the real size; its executables are not persisted; Pallas kernels and data-dependent shapes are not supported.

Dimension changes how Do this
never dynamic=False, done
grows each step (KV cache) static cache: preallocated buffer, 0-D length, index_copy_, mask; 2 compiles
per request or batch pad to buckets on the host, dynamic=False, warm every bucket before timing or serving
depends on values fixed shape plus a mask

Detect and prevent recompiles

TORCH_LOGS="graph_breaks,recompiles" python train.py          # reason for each break or recompile
XLA_FLAGS="--xla_dump_hlo_as_text --xla_dump_to=DIR" python train.py   # count *after_optimizations.txt
  • torch.compile(..., fullgraph=True) fails on any graph break: a cheap proof.
  • After warm-up: torch.compiler.set_stance("fail_on_recompile") in a test.
  • Time per step with a synchronize: after warm-up the line must be flat. Dynamo counts miss compiles inside the backend (new bucket, new on-device layout).
Symptom Fix
idle gaps between tiny ops (microbubbles, ops under about 50 µs) DeferAndFuse, torch.compile, or larger ops
every step slow by the same amount a shape changes each step: static cache or buckets
fast, then slow for good after recompile_limit fix the shape; do not raise the limit
DeferAndFuse uses more HBM than expected drop intermediates (del t) before the materialization point
TORCH_TPU_DEFER_AND_FUSE=1 has no effect set before Python starts; check TORCH_TPU_LAUNCH_BLOCKING