torch_tpu¶
What it is. The out-of-tree PyTorch backend for Cloud TPU. It registers "tpu" as a PyTorch device (the PrivateUse1 slot), runs eager ops as deferred ops that it compiles through XLA, provides the "tpu" backend for torch.compile, the "tpu_dist" collective backend, Pallas custom ops through jax_op, and XProf profiling through torch.profiler. Every other repository on these pages runs on it. Upstream: https://github.com/google-pytorch/torch_tpu. The book teaches it in Part I; the full public API is in the API matrix.
Layout¶
| Path | What lives there |
|---|---|
src/torch_tpu/ |
The Python package. _loader.py registers the backend on import torch and sets the torch.compile defaults |
src/torch_tpu/_internal/public_api.py |
The registry of public names (torch.tpu, torch.tpu.ops, .distributed, .pallas, .profiler, .optimizers, .compiler) and their stages |
src/torch_tpu/_internal/ |
One directory per area: compile/, device/, distributed/, execution_mode/, ops/, pallas/, precision/, profiler/, optimizers/, stream/ |
csrc/ |
C++ core: eager/ (deferred ops, the PrivateUse1 registrations in tpu_aten_kernels.cc), ops/ (one directory per ATen lowering), common/ (compilation, cache, env_vars.h), pjrt/, mlir/, distributed/ (the tpu_dist process group) |
docs/ |
User documentation; public_apis.md is the generated list of public APIs by stage |
examples/ |
simple_pytorch/, fsdp/, tensor_parallel/, muse_glimmer_30b/, FLUX_optimization/ |
perflab/ |
Benchmark harness used for model performance runs |
tests/ |
Bazel test targets (op, eager, compile, distributed, Pallas, profiler suites) |
bazel/, ci/, shims/ |
Build rules, wheel and CI scripts, per-PyTorch-version glue |
Entry points¶
Stages are from public_apis.md: Experimental names can change in any release.
| API or command | What it does | Source | Taught in |
|---|---|---|---|
pip install torch_tpu==0.2.0 --extra-index-url https://download.pytorch.org/whl/cpu |
Installs the 0.2.0 release with the CPU build of torch |
README.md |
Ch 8, VM guide |
torch.device("tpu"), torch.accelerator.* |
The device, and the Stable device, memory and stream calls. The older torch.tpu.is_available, device_count, synchronize and their peers are Internal on PyTorch 2.11 and later and raise from user code |
_loader.py |
Ch 3 |
torch.tpu.get_device_properties() |
Chip name, HBM and bf16 FLOP/s per device, topology (Experimental) | _device_module.py |
Ch 1, Ch 3 |
torch.tpu.EagerMode, eager_mode, set_eager_mode(mode) |
Eager mode: DEFER_NEVER (Strict, the default), DEFER_AND_FUSE, DEFER_NEVER_AND_LAUNCH_BLOCKING |
execution_mode/__init__.py |
Ch 4 |
torch.compile(model, backend="tpu") |
XLA compile of the FX graph; dynamic=False unless you pass dynamic |
_loader.py, compile/_backend.py |
Ch 4, Ch 7 |
torch.tpu.Precision, torch.tpu.precision(level) |
float32 matmul precision: DEFAULT 1 pass, HIGH 3, HIGHEST 6 |
precision/precision.py |
Ch 6 |
torch.tpu.allow_excess_precision |
Keep float32 intermediates in fused code; default False, locked at the first compile |
_device_module.py |
Ch 6 |
torch.tpu.get_device_layout_if_materialized(t), LayoutContext |
Read or hint the on-device tiled layout | device_utils/annotations.py |
Ch 5 |
dist.init_process_group(backend="tpu_dist") |
Collectives compiled into the TPU program; no NCCL | csrc/distributed/process_group_tpu.h |
Ch 13 |
python3 -m torch.tpu.distributed, torch.tpu.distributed.set_tpu_launch_env() |
Prints or sets TORCH_TPU_TOPOLOGY, TORCH_TPU_SLICEBUILDER_ADDRESSES and the multi-host variables before workers start |
distributed/distributed.py |
Ch 8, Ch 13 |
torch.tpu.distributed.topology_aware_mesh(mesh_shape, ...) |
Rank layout that follows the ICI torus, for DeviceMesh |
distributed/_distributed_module.py |
Ch 13, Ch 14 |
@torch.tpu.distributed.spmd_safe |
Declares a function SPMD so its collectives fuse with compute | distributed/spmd_util.py |
Ch 13 |
torch.tpu.ops.splash_sdpa, ragged_dot, ragged_all_to_all, optimization_barrier |
TPU operators: Splash attention, MoE grouped matmul, ragged exchange, XLA barrier | ops/ops.py, ops/splash_attention.py |
Ch 9, Ch 12 |
torch.tpu.pallas.jax_op(name, fn) |
Registers a JAX or Pallas function as a traceable PyTorch custom op | pallas/pallas.py |
Ch 12, kernel guide |
torch.tpu.pallas.get_pallas_mesh(...), verifier.verify(...) |
JAX mesh aligned with torch ranks; static checks of a Pallas kernel | pallas/mesh.py, pallas/verify/verifier.py |
Ch 12 |
torch.tpu.profiler.TpuProfilerConfig(...) |
Passed as experimental_config with ProfilerActivity.TPU; writes .xplane.pb |
profiler/profiler_config.py |
Ch 10, trace guide |
torch.tpu.compiler.custom_compiler_options(options) |
XLA flags for eager ops materialized inside the block | compiler_options/__init__.py |
API matrix |
torch.tpu.optimizers.SingleTraceTrainer |
Forward, backward and optimizer step in one compiled program | optimizers/single_trace_trainer.py |
API matrix |
TORCH_TPU_DEFER_AND_FUSE=1, TORCH_TPU_LAUNCH_BLOCKING=1 |
Default eager mode for the process; read at import torch |
csrc/common/env_vars.h |
Ch 4 |
TORCH_TPU_TIER3_COMPILATION_CACHE_ROOT |
Persistent compilation cache (path or gs://) across runs |
csrc/common/env_vars.h |
Ch 3 |
TORCH_TPU_TIER2_COMPILATION_CACHE |
Host-local cache under /dev/shm/torch_tpu_cache/: off at world size 1, otherwise on (named default); "disabled" turns it off (Experimental) |
csrc/common/env_vars.h |
Ch 3, Env vars |
TORCH_TPU_SPMD_HANDSHAKE_STAGE |
OFF, COMPILE_STAGE or DISPATCH_STAGE: guarded collective fusion under torch.compile |
csrc/common/env_vars.h |
Ch 13 |
LIBTPU_INIT_ARGS, XLA_FLAGS |
Runtime and compiler flags; XLA_FLAGS="--xla_dump_to=DIR" dumps HLO |
csrc/common/env_vars.h |
Env vars |
Extension points¶
| You want to add | Where | Taught in |
|---|---|---|
An ATen op lowering (an op raises NotImplementedError) |
csrc/ops/<op>/, named in csrc/ops/op_names.h, registered with ImplStable in csrc/eager/tpu_aten_kernels.cc, removed from UNREGISTERED_OPS in tests/native_functions_data.py |
Ch 25 |
| A TPU custom op | C++ torch.ops.tpu._foo, re-exported as torch.tpu.ops.foo; new public APIs start Internal or Experimental |
Ch 25 |
| A Pallas kernel | torch.tpu.pallas.jax_op in the repository that uses it |
Ch 12 |
Tests¶
Tests are Bazel targets declared in tests/BUILD. Op tests compare TPU with CPU (tests/ops_unit_test.py, assert_close_tpu_vs_cpu) and with recorded GPU goldens (tests/ops_test.py).
bazel build --config=wheel_common //ci/wheel:torch_tpu_wheel --config=no_rbe # wheel, no remote build
bazel test //tests:ops_unit_test --test_filter=<test name> # one target, on a TPU VM
python ci/tools/list_ci_tests.py --test=//tests:ops_unit_test # which CI jobs run it
CI jobs are Bazel configs in .bazelrc (ci_cpu, ci_tpu_v5, ci_tpu_v6, ci_tpu_v7 and their presubmit variants), selected by test tags. Chapter 25 walks through a full lowering with its tests.