Overview
PyTorch/XLA is the package that lets PyTorch code run on Google Cloud TPUs. It uses the XLA deep-learning compiler as the bridge: tensors are moved to an `xla` device, the training step is traced into an XLA graph, and the compiler turns that graph into TPU execution. In day-to-day use the change to an existing script is small — move the model and inputs to `'xla'`, and wrap the step in `torch_xla.step()`.
The distributed story is what most users come for. Single-process and multi-process training are both supported, along with FSDP for sharded parameters, `DistributedDataParallel`, and SPMD, where you describe how tensors are sharded and the compiler works out the collectives. Dynamic shapes and eager mode are available for the cases where full tracing is awkward, and the project ships profiling and performance-analysis tooling because compiled execution makes "why is this slow?" a different question than it is on GPU.
Google has since announced TorchTPU, a more native PyTorch-on-TPU path built on the PrivateUse1 device extension mechanism rather than on a compiler bridge, and has said it will replace PyTorch/XLA once its public repository ships. Until that transition happens, PyTorch/XLA remains the supported way to run PyTorch on TPU hardware, and it is the codebase the TPU ecosystem is currently built on.
What it does
- Runs PyTorch models on Cloud TPUs by moving tensors to an `xla` device
- Single-process and multi-process training modes
- SPMD sharding, FSDP and DistributedDataParallel for large-model training
- Dynamic shape and eager mode support alongside traced execution
- Profiling and performance-analysis tooling for compiled graphs
- Versioned in step with PyTorch releases (`torch` and `torch_xla` share a version)
Getting started
Install `torch_xla` matched to your `torch` version, then adapt the training loop. The TPU extra pulls in the runtime.
Install
The torch and torch_xla versions must match; the [tpu] extra installs the TPU runtime. This is the stable Python 3.11 line from the README.
pip install torch==2.8.0 'torch_xla[tpu]==2.8.0'Move the model to the XLA device
Same as `.to('cuda')` on GPU — the device string is `xla`.
import torch_xla
model.to('xla')Wrap the training step
torch_xla.step() marks the graph boundary so the compiler knows what to compile and execute.
with torch_xla.step():
inputs, labels = inputs.to('xla'), labels.to('xla')
# forward, loss, backward, optimizer stepScale out
From there, the docs at https://pytorch.org/xla cover multi-process launch, SPMD sharding annotations, FSDP and the profiler.
Commands and code are distilled from the project's own documentation — always check the official repo for the latest.
When to use it
- Train an existing PyTorch model on Cloud TPU pods without rewriting it for JAX
- Shard a large model across TPU cores with SPMD or FSDP
- Fine-tune on TPU capacity when GPUs are unavailable or more expensive
- Profile and debug TPU execution of a PyTorch training loop
How PyTorch/XLA compares
PyTorch/XLA alongside other open-source efficient training tools AI/TLDR tracks, ranked by GitHub stars.
| Tool | Stars | What it does |
|---|---|---|
| DeepSpeed | ★ 43.2k | A deep learning optimization library whose ZeRO memory partitioning and offloading let you train very large models across many GPUs. |
| Megatron-LM | ★ 18k | NVIDIA's library for training large transformer models at scale using tensor, pipeline, and sequence parallelism. |
| Accelerate | ★ 9.9k | A library that runs the same PyTorch training code across CPUs, multiple GPUs, and TPUs while handling mixed precision, FSDP, and DeepSpeed. |
| DeepSpec | ★ 7.1k | DeepSeek's codebase for training and evaluating draft models for speculative decoding, bundling data preparation, the DSpark, DFlash and Eagle3 drafters, training code and benchmarks. |
| TorchTitan | ★ 5.8k | A PyTorch-native platform for pre-training large models that combines FSDP, tensor, pipeline, and context parallelism in one codebase. |
| LightlySSL | ★ 3.8k | A modular PyTorch framework for self-supervised computer vision: losses, model heads and multi-view transforms for 20+ methods from MoCo and SimCLR to DINOv2, CAPI and LeJEPA. |
| Nanotron | ★ 2.8k | Hugging Face's minimal library for pre-training LLMs with 3D parallelism, designed to be readable and easy to modify. |
| PyTorch/XLA | ★ 2.8k | Run PyTorch models on Cloud TPUs through the XLA compiler |