Skip to main content
TorchTPU: PyTorch backend for Google Cloud TPUs

Description

This project provides a PyTorch backend for Google's Tensor Processing Units (TPUs), enabling users to run PyTorch models and operations on TPU devices. It includes custom ATen kernels for various PyTorch operations, a compilation cache for optimizing execution, and examples for distributed training and model inference.

Features

  • PyTorch ATen kernel implementation for TPU.
  • Compilation cache for optimized execution of XLA computations.
  • Support for distributed training (Data Parallelism, Tensor Parallelism).
  • Examples for various models (Llama, Qwen, ResNet, DLRM, minGPT, UNet).
  • Integration with torch.compile() for graph mode execution.
  • Utilities for debugging and benchmarking.

User Guide and Interactive Notebooks

A user guide is available at https://torchtpu.readthedocs.io/. This is a static version of the interactive notebooks. They are available in the docs folder.

The notebooks are Marimo (similar to Jupyter). They can be run with marimo edit file.py and then you can access them in your browser. You will need to be running this from a TPU VM, a v6e GCE instance is easiest.

Installation

The same instructions are available in the user guide linked above.

TorchTPU's Python version support policy matches PyTorch's Release Compatibility Matrix. Currently, Python 3.11–3.15 is supported (including free-threaded 3.14t and 3.15t builds, with Python 3.15 requiring PyTorch >= 2.15). See the Python Version Support Policy for more details.

First create a Python virtual environment:

python -m venv .venv
source .venv/bin/activate

or, with uv

uv venv .venv
source .venv/bin/activate

Install via pip

Install the latest public release from PyPI (for uv, prepend uv to all pip commands):

pip install torch_tpu --extra-index-url https://download.pytorch.org/whl/cpu

Build from source

Wheels can be built via:

bazel build --config=wheel_common //ci/wheel:torch_tpu_wheel

All OSS Bazel builds default to optimized mode (-c opt) automatically via .bazelrc. Unoptimized builds (-c fastbuild, -c dbg) can degrade performance and are not used for wheel builds. If you are developing or debugging C++ code and explicitly want an unoptimized wheel, pass --//:allow_unoptimized_wheel=True:

bazel build -c dbg --config=wheel_common //ci/wheel:torch_tpu_wheel \
    --//:allow_unoptimized_wheel=True

--config=wheel_common is required, and the build fails at build time without it. The config sets --//:wheel_build=True, which routes the shared XLA/MLIR backend through the torch_version-reset transition so that pywrap factors it into a single libxla_base.so; built without the flag, every per-version common ships its own copy of the backend and the resulting wheel aborts on import torch with duplicate static registrations.

wheel_common also turns on the Remote Build Execution (RBE) remote cache and remote executor. If you do not have RBE credentials, append --config=no_rbe to strip those flags back out:

bazel build --config=wheel_common //ci/wheel:torch_tpu_wheel --config=no_rbe

The order matters here: --config=no_rbe has to come after --config=wheel_common, because the later config is the one that wins for the remote-cache and remote-executor flags they both set.

Install wheel via:

cd <path_to_repo>
python -m venv .venv; source .venv/bin/activate
# The extra-index-url provides the CPU version of torch rather than the default CUDA build.
pip install ../bazel-bin/ci/wheel/*.whl --extra-index-url https://download.pytorch.org/whl/cpu

Note that installing without --extra-index-url https://download.pytorch.org/whl/cpu will install the CUDA version of torch; pass --extra-index-url as shown above or install the CPU build separately.

Getting Started

Some pointers to get you started:

Bazel setup

This project uses Bazel for building and dependency management.

To install Bazel, we actually install Bazelisk, which is a transparent wrapper that handles downloading and installing Bazel itself.

There are several ways to install Bazelisk. Here are some common ways:

  • If you have Go installed:

    go install github.com/bazelbuild/bazelisk@latest
    
  • Download the binary directly:

    wget -O ~/bin/bazel https://github.com/bazelbuild/bazelisk/releases/download/v1.27.0/bazelisk-linux-amd64
    

See the Bazelisk README for more install instructions

However you install it, add it's location to PATH.

To verify the install, run bazel info in the repo's root directory:

bazel info

It should print information about the repo without errors.

VSCode clangd Setup

For C++ code navigation, it is recommended to use VSCode with clangd extension. Relying on a compilation database generated from Bazel action graph, clangd is able to enrich the editor with various smart features, including code completions, go-to definitions, etc.

  1. Generate the initial compile_commands.json:

    tools/setup_clangd.py
    

    If the VSCode code CLI is on PATH, the script also installs the clangd extension automatically. Otherwise, install the extension via the editor UI.

  2. Uninstall the Microsoft C++ extension, which interferes with clangd:

    code --uninstall-extension ms-vscode.cpptools
    

It may take a while (> 30 minutes) for the first run to make a full build, but reruns will be incremental and cheap. If you have run bazel build recently, generated files already exist so you can pass --no-build to skip waiting for heavy builds (note that on a fresh workspace, skipping the build may prevent clangd from resolving generated files). See more options in the tools/setup_clangd.py file-level comments.

Alternatively, you can invoke the script via nox (arguments after -- are forwarded):

nox -s refresh_compile_commands -- --no-build
nox -s refresh_compile_commands -- //src/torch_tpu/ops/...

Contributing

We welcome contributions to the PyTorch on TPU project! Please see our CONTRIBUTING.md for guidelines on how to contribute, including signing our Contributor License Agreement (CLA) and code review processes.

Star History

Star History Chart

License

This project is licensed under the Apache 2.0 License. See the LICENSE file for details.

Metadata

Release files for torch-tpu 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for torch-tpu 0.3.0
File Actions
torch_tpu-0.3.0-cp315-cp315t-manylinux_2_31_x86_64.whl CPython 3.15 CPython 3.15 free-threading Linux glibc 2.31+ x86-64 Details for torch_tpu-0.3.0-cp315-cp315t-manylinux_2_31_x86_64.whl
torch_tpu-0.3.0-cp315-cp315-manylinux_2_31_x86_64.whl CPython 3.15 CPython 3.15 Linux glibc 2.31+ x86-64 Details for torch_tpu-0.3.0-cp315-cp315-manylinux_2_31_x86_64.whl
torch_tpu-0.3.0-cp314-cp314t-manylinux_2_31_x86_64.whl CPython 3.14 CPython 3.14 free-threading Linux glibc 2.31+ x86-64 Details for torch_tpu-0.3.0-cp314-cp314t-manylinux_2_31_x86_64.whl
torch_tpu-0.3.0-cp314-cp314-manylinux_2_31_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.31+ x86-64 Details for torch_tpu-0.3.0-cp314-cp314-manylinux_2_31_x86_64.whl
torch_tpu-0.3.0-cp313-cp313-manylinux_2_31_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.31+ x86-64 Details for torch_tpu-0.3.0-cp313-cp313-manylinux_2_31_x86_64.whl
torch_tpu-0.3.0-cp312-cp312-manylinux_2_31_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.31+ x86-64 Details for torch_tpu-0.3.0-cp312-cp312-manylinux_2_31_x86_64.whl
torch_tpu-0.3.0-cp311-cp311-manylinux_2_31_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.31+ x86-64 Details for torch_tpu-0.3.0-cp311-cp311-manylinux_2_31_x86_64.whl

Total release size: 623.1 MB

Release files / torch_tpu-0.3.0-cp315-cp315t-manylinux_2_31_x86_64.whl

Download URL torch_tpu-0.3.0-cp315-cp315t-manylinux_2_31_x86_64.whl
Size 64.7 MB
Tags CPython 3.15 CPython 3.15 free-threading Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
730806f500c514b6891fa2fef50ac658a7c747913f7aed3c89ab0d0e4b6cc629
BLAKE2b-256 checksum
How to use checksums
76ce50467da02e12451f19b7f4bf5cbce3638d842d3d782d0a1b1ab451039bb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.16

Release files / torch_tpu-0.3.0-cp315-cp315-manylinux_2_31_x86_64.whl

Download URL torch_tpu-0.3.0-cp315-cp315-manylinux_2_31_x86_64.whl
Size 64.6 MB
Tags CPython 3.15 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
38180578f8d46b5f3850711d12e64bf60c5fbd7eaa1fa75244f3661f27215184
BLAKE2b-256 checksum
How to use checksums
18f0976295885abb25f131a3ad2d4963232b3d8d77e4da7a2674137e49129c69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.16

Release files / torch_tpu-0.3.0-cp314-cp314t-manylinux_2_31_x86_64.whl

Download URL torch_tpu-0.3.0-cp314-cp314t-manylinux_2_31_x86_64.whl
Size 99.3 MB
Tags CPython 3.14 CPython 3.14 free-threading Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
077c787e06600552f5ca3f3b68c89823abf417e4a78f94d8f60419e5904508f8
BLAKE2b-256 checksum
How to use checksums
4bc461a3aa4b30c653c3f57d25722955d2d575ba32a8da31945aec254648466b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.16

Release files / torch_tpu-0.3.0-cp314-cp314-manylinux_2_31_x86_64.whl

Download URL torch_tpu-0.3.0-cp314-cp314-manylinux_2_31_x86_64.whl
Size 98.7 MB
Tags CPython 3.14 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
44e2d90eccd81e296c0105c817b363da8915cea655b7617027a8b60d1ca0fd24
BLAKE2b-256 checksum
How to use checksums
9d349e1f831ed08c0cdd9d614ea793a741b1a10724a60f17b1e589e7c7e3a086
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.16

Release files / torch_tpu-0.3.0-cp313-cp313-manylinux_2_31_x86_64.whl

Download URL torch_tpu-0.3.0-cp313-cp313-manylinux_2_31_x86_64.whl
Size 98.7 MB
Tags CPython 3.13 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
e1fa5f04a2e2b980d6e31c74ac325a9fbfac1dad7cef40ea54e8dea829535587
BLAKE2b-256 checksum
How to use checksums
dc687c8c62e6b252d753927ba43853b60b608b1f24da309d157adb28193458f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.16

Release files / torch_tpu-0.3.0-cp312-cp312-manylinux_2_31_x86_64.whl

Download URL torch_tpu-0.3.0-cp312-cp312-manylinux_2_31_x86_64.whl
Size 98.7 MB
Tags CPython 3.12 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
cec959a8a3cb0d47290534d9a1f763e59ae3bd92c441756480fea3f0e24ff9c0
BLAKE2b-256 checksum
How to use checksums
a07ea080fe4102b89d22dc0245337ab7fd0c97602399e4dd7e1214dc5e44c76f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.16

Release files / torch_tpu-0.3.0-cp311-cp311-manylinux_2_31_x86_64.whl

Download URL torch_tpu-0.3.0-cp311-cp311-manylinux_2_31_x86_64.whl
Size 98.5 MB
Tags CPython 3.11 Linux glibc 2.31+ x86-64
SHA-256 checksum
How to use checksums
1db73fdfc355f9aafe656631fd973d7455b745df8a9f52d440737300507104d0
BLAKE2b-256 checksum
How to use checksums
af96e54aad3fd8de339114781998bdc58c61864794a6c49b68b0893cb64d3ca8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.16

Security for torch-tpu 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.0 This release

7 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page