Google 开源 TorchTPU,TPU 的原生 PyTorch 后端
Google open-sources TorchTPU, a native PyTorch back end for TPUs

原始链接: https://github.com/google-pytorch/torch_tpu

TorchTPU 是 Google TPU 的 PyTorch 后端,使 PyTorch 模型和操作能够在 TPU 设备上运行。它提供自定义 ATen 内核、用于优化 XLA 执行的编译缓存、分布式训练支持(数据并行与张量并行)、torch.compile 集成,以及调试和基准测试工具。示例涵盖 Llama、Qwen、ResNet、DLRM、minGPT 和 UNet 等模型。文档包含用户指南和 Marimo 笔记本。支持 Python 3.11 及以上版本。可从 PyPI 安装 torch_tpu==0.2.0(使用 CPU 版 torch wheel 索引),或使用 Bazel 从源码构建。开发使用 Bazel/Bazelisk,可通过 compile_commands.json 进行可选的 clangd 配置。该项目在 Apache 2.0 许可下开源,欢迎遵循其 CLA 和审核流程的贡献。

Google 在 GitHub 上开源了 TorchTPU,一个面向 TPU 的原生 PyTorch 后端。Hacker News 讨论中包括与亚马逊用于 AWS Trainium 的 torch-neuronx 的比较。用户询问 TPU 是否存在于 Google Cloud 之外;回复提到了 Google Coral USB 和 M.2 TPU 设备以及更广泛的 TPU 类/神经加速器。另一条评论链接到先前讨论,比较了 Cerebras、Groq、Tenstorrent、AMD 等专用 AI 芯片。该帖子在捕获时有 20 点和 4 条评论。
相关文章

原文

TorchTPU Logo

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.

  • 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.

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+ is supported. 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 the latest public release (0.2.0 on PyPI) (for uv prepend uv to all pip commands):

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

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.

Some pointers to get you started:

The project uses Bazel for dependency management. Key Python dependencies are:

TBD

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:

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:

It should print information about the repo without errors.

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. Install VSCode extensions via UI or the following command:

    code --install-extension llvm-vs-code-extensions.vscode-clangd
    # Uninstall Microsoft C++ extensions to avoid interference.
    code --uninstall-extension ms-vscode.cpptools
  2. Generate the initial compile_commands.json:

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 script 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/...

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 Chart

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

联系我们 contact @ memedata.com