Skip to content

CUDA to TorchTPU

The translation table from chapter 3, for looking things up while you port. "Stable" and "Experimental" are TorchTPU's API stages (API matrix); "PyTorch" means a standard PyTorch call that takes a TPU device type or backend name. The rule behind the table: use torch.accelerator for anything it covers, and torch.tpu only for what is TPU-specific.

The translation table

These 25 rows are chapter 3's table.

Area On CUDA On TorchTPU Stage What to know
Device torch.device("cuda") torch.device("tpu") or torch.accelerator.current_accelerator() PyTorch Available after import torch through autoload
Device torch.cuda.is_available() torch.accelerator.is_available() Stable
Device torch.cuda.device_count() torch.accelerator.device_count() Stable Counts devices visible to the current process
Device torch.cuda.set_device(i) torch.accelerator.set_device_index(i) Stable
Device torch.cuda.current_device() torch.accelerator.current_device_index() Stable
Device torch.cuda.get_device_properties() torch.tpu.get_device_properties() Experimental name, devices_per_chip, hbm_bytes_per_device, bf16_flops_per_device and more
Sync torch.cuda.synchronize() torch.accelerator.synchronize() Stable Waits for all pending TPU work, including deferred ops
Streams torch.cuda.Stream, torch.cuda.Event torch.tpu.Stream, torch.tpu.Event Experimental See what does not translate: one computation at a time per TensorCore
Visibility CUDA_VISIBLE_DEVICES TPU_VISIBLE_DEVICES Stable Comma-separated TPU chip indices
Memory torch.cuda.memory_allocated(), max_memory_allocated() torch.accelerator.memory.memory_allocated(), max_memory_allocated() Stable
Memory torch.cuda.empty_cache() torch.accelerator.memory.empty_cache() Stable
RNG torch.cuda.manual_seed_all(s) torch.tpu.manual_seed_all(s) Experimental
Mixed precision torch.amp.autocast("cuda", dtype=torch.bfloat16) torch.amp.autocast("tpu", dtype=torch.bfloat16) PyTorch bf16 is the default autocast dtype on TPU
fp32 matmuls torch.set_float32_matmul_precision(...) The same call, plus with torch.tpu.precision(torch.tpu.Precision.HIGH): for a scope PyTorch, Experimental TorchTPU defaults to "medium": one bf16 MXU pass, fp32 accumulation. "high" is about 3 times the MXU cost, "highest" about 6 times
Compile torch.compile(model) (Inductor) torch.compile(model, backend="tpu") PyTorch XLA, not TorchInductor
Debug CUDA_LAUNCH_BLOCKING=1 TORCH_TPU_LAUNCH_BLOCKING=1 Experimental Synchronous Debug mode, adds bounds checks
Fusion in eager None TORCH_TPU_DEFER_AND_FUSE=1 Experimental No CUDA counterpart
Distributed dist.init_process_group(backend="nccl") backend="tpu_dist" (alias "tpu"), or no argument PyTorch Collectives are StableHLO ops, not a separate library
Mesh DeviceMesh("cuda", ...) DeviceMesh("tpu", mesh=torch.tpu.distributed.topology_aware_mesh(shape)) Experimental Orders ranks to follow the physical ICI torus
Launch torchrun alone torch.tpu.distributed.set_tpu_launch_env(), or the shell recipe under Porting, before torchrun Experimental Sets TORCH_TPU_TOPOLOGY and TORCH_TPU_SLICEBUILDER_ADDRESSES
IR PTX StableHLO, Mosaic The documentation's own analogy
Kernels Triton or CUDA C++ kernels Pallas kernels wrapped with torch.tpu.pallas.jax_op; Helion (experimental) Experimental A rewrite, not a port
Attention kernel A FlashAttention-style kernel torch.tpu.ops.splash_sdpa(q, k, v, ...) Experimental Backed by the Pallas Splash Attention kernel
Profiling torch.profiler with CUDA activity torch.profiler with ProfilerActivity.TPU and experimental_config=TpuProfilerConfig(...) Experimental Traces open in XProf (chapter 10)
Monitoring nvidia-smi tpu-info Installed with vllm-torchtpu; shows chip utilization and HBM use

More rows from the API reference

The rows below come from TorchTPU's API reference, which gives a CUDA name for these calls too; the chapter does not use them.

Area On CUDA On TorchTPU Stage What to know
Device torch.cuda.is_initialized() torch.tpu.is_initialized() Experimental Whether TorchTPU's runtime state is initialized
Device torch.cuda.device(i) torch.accelerator.device_index(i) Stable Context manager
Device torch.cuda.get_device_name() torch.tpu.get_device_properties().name Experimental torch.tpu.get_device_name is deprecated since 0.2.1
RNG torch.cuda.manual_seed(s) torch.tpu.manual_seed(s) Experimental Active device only
RNG torch.cuda.initial_seed() torch.accelerator.random.initial_seed() Stable PyTorch 2.13 or later
RNG torch.cuda.get_rng_state() torch.accelerator.random.get_rng_state() Stable PyTorch 2.13 or later
RNG torch.cuda.set_rng_state(state, device=...) torch.tpu.set_rng_state(state, device=...) Experimental
RNG torch.cuda.default_generators torch.tpu.default_generators Experimental One generator per local device
Mixed precision torch.cuda.get_amp_supported_dtype() torch.tpu.get_amp_supported_dtype() Experimental
Streams torch.cuda.current_stream(), torch.cuda.set_stream(s) torch.accelerator.current_stream(), torch.accelerator.set_stream(s) Stable
Streams torch.cuda.default_stream(), torch.cuda.stream(s) torch.tpu.default_stream(), torch.tpu.stream(s) Experimental Manual multi-stream management is not needed on TPU

On PyTorch versions older than 2.11 (2.13 for the RNG calls) the torch.accelerator calls do not exist, and the corresponding torch.tpu call is the one to use; on newer versions those torch.tpu duplicates raise an error (API matrix).

Notes

  • Launch environment. The launch helper is torch.tpu.distributed.set_tpu_launch_env(), in the torch.tpu.distributed namespace.
  • Device visibility. The public variable is TPU_VISIBLE_DEVICES. TPU_VISIBLE_CHIPS is the variable Ray sets (chapter 15).
  • Precision cost. "high" costs about 3 times the MXU work of the default, "highest" about 6 times.
  • Timing events. Use torch.tpu.Event(enable_timing=...).
  • Device per process. Each tpu_dist process uses torch.device("tpu") rather than indexing the device by local rank as on CUDA.
  • No documented TorchTPU counterpart: pinned host memory with non_blocking=True, CUDA Graphs, one-sided RDMA in the style of NVSHMEM, a fusion-length limit for DeferAndFuse, FP8 and FP4 throughput figures, and telemetry metric names.

What does not translate one to one

The tables above map names. These habits need more than a new name.

CUDA habit What is different on TorchTPU Chapter
An eager op runs as soon as it is launched Three eager modes; deferred ops run at materialization points such as .cpu(), .item() or if tensor.all():, which also end a fused region 4
Views and in-place writes are free metadata changes StableHLO values are immutable; views are rebuilt by decomposition and inversion, and some cost a copy 5
fp32 matmuls run at fp32 or TF32 accuracy The default is one bf16 MXU pass with fp32 accumulation; "high" and "highest" cost about 3 and 6 times as much 6
Input shapes may change every step Each new shape is a new compilation; torch.compile currently sets dynamic=False on TPU, and bounded dynamism is the way out 7
A process sees every GPU on the node and picks cuda:{local_rank} A process sees one device, and must know it is distributed, the world size and the TPU type before init_process_group 8, 13
Streams overlap independent kernels A TensorCore runs one computation at a time; overlap comes from DMA engines, SparseCores and the XLA schedule. The API reference states that manual multi-stream management is not needed 4, 10
NCCL runs collectives as a separate library Collectives are StableHLO ops; by default each is compiled in its own small module so ranks cannot diverge, which costs overlap until you mark code with torch.tpu.distributed.spmd_safe 13
FSDP2 with eager hooks TorchTPU lists SimpleFSDP as validated and FSDPv2 as experimental with low performance 14
Triton or CUDA kernels Rewritten in Pallas and called through torch.tpu.pallas.jax_op, or in Helion (experimental) 12
Two processes can share a GPU Only one process can access a TPU device, so RL colocation does not work out of the box 15

See also

References