The TorchTPU stack at a glance¶
PyTorch on Cloud TPU spans six layers, from the compiler and silicon up to agent tooling. This page summarises each layer, its canonical entry point in the torch_tpu, torchtitan, vllm-torchtpu, sglang-torchtpu and tpu-agent-suite repositories or the llm-d documentation, and the recommended production default. The other pages of the quick reference go deeper by topic: Fundamentals, How it works, Recipes, Diagnosis and evaluation, Extending the stack and Reference tables.
Layers and subsystems¶
The six layers are numbered from the silicon up. The engines in layers 3 and 4 are ordinary PyTorch code that reaches the TPU only through torch_tpu, which hands its programs to the compilers in libtpu; layers 5 and 6 deploy, route and tune that code.
| Core Subsystem | Canonical Entrypoint / Symbol | Primary Responsibility | Recommended default |
|---|---|---|---|
| Device Backend | torch.device("tpu") / torch.accelerator.* (_device_module.py) |
Autoloads PrivateUse1='tpu' via entry-point (TORCH_DEVICE_BACKEND_AUTOLOAD=1) + torch.accelerator API |
torch.accelerator.current_accelerator() + LD_PRELOAD=libtcmalloc |
| Graph Compiler | torch.compile(model, backend="tpu") (compiler.py) |
Lowers FX graphs (EagerLikeFxInterpreter) \(\to\) StableHLO MLIR \(\to\) XLA HLO (-O2) \(\to\) PjRtLoadedExecutable |
backend="tpu", 3-tier cache (TORCH_TPU_TIER2_COMPILATION_CACHE, TORCH_TPU_TIER3_COMPILATION_CACHE_ROOT) |
| Memory & Storage | TpuAllocator & DeviceBufferRef (tensor_to_buffer.cc, device_buffer.h) |
Maps UntypedStorage.data_ptr() to CPU metadata (DeviceBufferList / DeferredOp) + HBM PjRtBuffer |
Contiguous (8, 128)-aligned tensors + buffer donation |
| Distributed Collectives | tpu_dist / ProcessGroupTpu (process_group_tpu.h, window_tpu.h) |
Lowers collectives to StableHLO over 2D/3D optical ICI torus & DCN; WindowTPU window API (peer ICI transfers currently emulated with two-sided send and receive) |
torch.tpu.distributed.set_tpu_launch_env() + topology_aware_mesh() + @spmd_safe / TORCH_TPU_SPMD_HANDSHAKE_STAGE=COMPILE_STAGE |
| Pre-Training Engine | TorchTitan (torchtitan/) & TorchRec |
6D Parallelism (DDP, SimpleFSDP/FSDP2, TP/AsyncTP, PP, CP, EP), scan loop & SparseCore DLRM |
SimpleFSDP + scan + SingleTraceTrainer + Activation Offloading |
| Serving Engines | vLLM-TorchTPU (vllm-torchtpu/) & SGLang-TorchTPU (sglang-torchtpu/) |
Paged RPA v3 attention, sequence-on-lane FP8 KV layout, token/batch bucketing, tpu-sync (Raiden) P/D transfer |
Pre-warmed token/batch buckets + CPU page table + KVFreeFence |
| K8s Disaggregation | llm-d 1.0 (Announcing llm-d 1.0) |
Envoy ext_proc Endpoint Picker (EPP), ZMQ KV Indexer, LeaderWorkerSet (LWS), DisaggregatedSet, KEDA + EPP autoscaling |
ZMQ KV events (:5556), precise-prefix-cache-scorer, tpu-sync / NIXL P/D |
| Custom Kernel Stack | Pallas (@jax_op) / Tokamax / Kiln / Helion (pallas.py, kiln.h) |
Explicit VMEM double-buffered DMA pipelines, MXU/VPU/SparseCore scheduling & static kernel verification | (8, 128) tile alignment, out_dtype=jnp.float32, get_pallas_mesh, verifier.verify |
Notes on the table:
- Memory and storage. The allocator TorchTPU registers,
TpuAllocatorintensor_to_buffer.cc, leaves memory management to PjRt, and itsemptyCachedoes nothing. There is no separate caching allocator. Input and output aliasing is called buffer donation in TorchTPU and the TPU memory model (chapter 5). - Device backend. The device is
torch.device("tpu"), and each TorchTPU process sees one device (chapter 3). - Distributed collectives. The launcher is
torch.tpu.distributed.set_tpu_launch_env(), listed in TorchTPU Public APIs (chapter 13).
