pytorch/xla
Enabling PyTorch on XLA Devices (e.g. Google TPU)
PyTorch/XLA — What It Is
PyTorch/XLA is a Python package that connects two major pieces of software:
- PyTorch – a popular framework for building and training machine learning models.
- XLA – a compiler that optimizes and runs machine learning models, especially on specialized hardware.
- Cloud TPUs – Google's custom AI accelerator chips (Tensor Processing Units).
The package lets you run PyTorch models on Cloud TPUs, using XLA to translate PyTorch operations into code that TPUs can execute efficiently.
Important: The README states that this project is deprecated and will be replaced by a new project called TorchTPU. The PyTorch/XLA repository will be archived on November 15, 2026. New workloads should use TorchTPU, and existing PyTorch/XLA workloads should migrate to it.
How It Works (in Simple Terms)
Normally, PyTorch runs on CPUs or GPUs. To use TPUs, you need a bridge. PyTorch/XLA acts as that bridge:
- It provides a special device type called
'xla'that you can move your model and data to (e.g.,model.to('xla')). - It uses XLA to compile and run the model on TPUs.
- It offers tools to manage training steps, synchronize data, and handle multi‑TPU setups.
Key Features (from the README)
- Single‑process mode – one Python script controls one TPU.
- Multi‑process mode – multiple Python processes each control a separate TPU, useful for larger models.
- SPMD mode – one Python process controls all TPUs at once (mentioned but not detailed in the README).
- Integration with PyTorch’s
DistributedDataParallel– the README shows how to adapt existing code to use TPUs. - Performance optimizations – the README mentions that using C++11 ABI builds can improve performance when the host CPU is busy tracing the model.
Installation
The README provides installation instructions for TPU VMs. Key points:
- Stable releases are available via
pip(e.g.,pip install torch==2.8.0 'torch_xla[tpu]==2.8.0'). - Nightly builds are also available from Google Cloud Storage.
- Python versions 3.8–3.11 are supported for stable builds; nightly builds support 3.11–3.13.
- Docker images are provided for various versions.
- The README notes that starting with version 2.7, C++11 ABI builds are the default.
Getting Started (Minimal Example)
The README shows how to modify a typical PyTorch training loop to run on TPU. The essential changes are:
- Import
torch_xla. - Move the model to the
'xla'device. - Wrap the training step in
with torch_xla.step():. - Move input data to
'xla'as well. - Call
torch_xla.sync()at the end to ensure all operations finish.
For multi‑process training, you also use torch_xla.launch() instead of torch.multiprocessing.spawn(), and use xm.optimizer_step() to combine gradients across replicas.
Documentation and Resources
The README links to:
- Detailed documentation on pytorch.org/xla
- Tutorials for Cloud TPU VMs and Pod slices
- A troubleshooting guide
- Example notebooks (e.g., Stable Diffusion, distributed training)
- A repository with reference implementations for LLMs and diffusion models
Feedback and Contribution
The project welcomes issues and contributions via GitHub. It is jointly maintained by Google, Meta, and individual contributors.
Summary
PyTorch/XLA is a bridge that lets PyTorch models run on Google Cloud TPUs. It is now deprecated in favor of TorchTPU, but the README still provides installation and usage instructions for those who need to use it before migration.
Related
- Dispatch
- Project
- Project
- Project
- Project