跳到主内容
@wquguru
精选86PyTorch 博客(RSS)产品发布/更新多源精选 ×6

Google与Meta联合发布TorchTPU原生PyTorch后端

Introducing TorchTPU

原文
发到 X
推荐理由

彻底打通PyTorch与TPU的原生链路,对依赖Google算力或寻求GPU替代方案的团队极具价值,建议关注其性能基准与迁移细节。

TL;DR

TorchTPU is an open source collaboration between Google and Meta that introduces a native and principled PyTorch backend for Google TPUs. It enables seamless out-of-the-box model execution and TPU migration while delivering industry-competitive inference and training performance through eager execution optimizations, torch.compile integration, and native hardware acceleration.

TorchTPU 是 Google 与 Meta 之间的开源合作项目,为 Google TPU 引入了原生且规范化的 PyTorch 后端。它通过急切执行优化、torch.compile 集成以及原生硬件加速,实现了无缝的即插即用模型执行和 TPU 迁移,同时提供具有行业竞争力的推理和训练性能。

Introducing TorchTPU

介绍 TorchTPU

Google TPUs have been used very successfully, both within and outside of Google, to train and deploy large ML models at massive scale. They are specifically designed for ML and, as such, they provide significant benefits in terms of performance, energy, and cost. While TPUs have been historically associated with the TensorFlow and the JAX ecosystems, we’re excited to introduce TorchTPU, a new effort started at Google in mid 2025, in collaboration with the PyTorch Team at Meta, to run PyTorch natively on TPUs.

Google TPU 在 Google 内部及外部都已被非常成功地用于大规模训练和部署大型机器学习模型。它们专为机器学习而设计,因此在性能、能耗和成本方面提供了显著优势。尽管 TPU 历史上主要与 TensorFlow 和 JAX 生态系统相关联,但我们很高兴地推出 TorchTPU——这是 Google 于 2025 年中启动的一项新举措,并与 Meta 的 PyTorch 团队合作,旨在让 PyTorch 在 TPU 上原生运行。

To program Google’s TPU at a level below the Python framework, developers utilize the XLA compiler stack. In 2020, Google introduced the first compiled version of PyTorch for TPU, PyTorch/XLA, based on Lazy Tensors. While foundational, this initial integration struggled to keep pace with the rapid, community-driven evolution of PyTorch. Crucially, PyTorch/XLA required intrusive source-code modifications to explicitly define compilable regions, suffered from severe compile-time latency, lacked support for PyTorch’s native imperative (“eager”) execution mode, and lagged in supporting emerging distributed computing primitives within the PyTorch ecosystem. In contrast with previous efforts such as PyTorch/XLA and TorchAX, which shoehorn PyTorch into JAX’s modus operandi, TorchTPU was built as a principled TPU backend for PyTorch: eager execution is a top priority, and existing PyTorch workloads run out of the box with trivial code changes (e.g., .to("cuda") → .to("tpu")).

要在低于 Python 框架的层级上编程 Google TPU,开发者需使用 XLA 编译器栈。2020 年,Google 基于惰性张量(Lazy Tensors)推出了首个针对 TPU 的编译版 PyTorch——PyTorch/XLA。虽然这一初始集成奠定了基础,但它难以跟上 PyTorch 快速且由社区驱动的演进步伐。关键在于,PyTorch/XLA 需要侵入式的源代码修改来显式定义可编译区域,存在严重的编译时延迟,缺乏对 PyTorch 原生命令式(“eager”)执行模式的支持,并且在支持 PyTorch 生态系统中新兴分布式计算原语方面落后。与之前将 PyTorch 强行适配到 JAX 运作模式的努力(如 PyTorch/XLA 和 TorchAX)不同,TorchTPU 是作为 PyTorch 的规范化 TPU 后端构建的:急切执行是首要优先级,现有的 PyTorch 工作负载只需进行微小的代码更改(例如 .to("cuda") → .to("tpu"))即可开箱即用。

More specifically, as we started TorchTPU we deliberately formulated a set of principles to follow:

更具体地说,当我们启动 TorchTPU 时,我们刻意制定了一套遵循的原则:

  • PyTorch Citizenship:
  • No JAX dependencies in the runtime;
  • Solid eager mode;
  • Torch.compile() based on Torch Dynamo (not Lazy Tensor) and with similar performance to JAX compile;
  • Wide support for PyTorch distributed idioms (DDP, FSDP, DTensor, …) by providing TPU collectives at the lowest level in the PyTorch stack (e.g. Process Group API);
  • Seamless integration into the PyTorch ecosystem;
  • Community engagement.
  • Focus on the User:
  • Porting a workload to TPU should be as simple as changing model.to("cuda") to model.to("tpu"). (One cannot achieve peak TPU performance with that simple change, since a model’s shapes, sharding and ops may be skewed toward a GPU architecture; however, the model should run out of the box.)
  • Provide user-friendly and actionable messages for runtime/compiler errors;
  • Integrate with the tools and frameworks that the PyTorch community already uses (e.g., torch.profiler, DTensor, Ray).
  • Improve the State of the Art:
  • Build a solution that can compete with PyTorch on GPUs for large scale workloads;
  • Introduce a JIT compilation approach for eager execution, with runtime optimizations that can bridge the performance gap between op-by-op execution and compiled execution;
  • Improve the TPU programming stack by introducing bounded dynamism, by avoiding XLA recompilation due to shape changes, and by exploring dynamic TPU kernels that can be precompiled ahead of time.
  • PyTorch 公民身份:
  • 运行时不依赖 JAX;
  • 稳固的急切模式;
  • 基于 Torch Dynamo(而非惰性张量)的 torch.compile(),并具有与 JAX 编译相似的性能;
  • 通过在 PyTorch 栈的最底层提供 TPU 集合操作(例如 Process Group API),广泛支持 PyTorch 分布式惯用法(DDP、FSDP、DTensor 等);
  • 无缝集成到 PyTorch 生态系统;
  • 社区参与。
  • 以用户为中心:
  • 将工作负载移植到 TPU 应该像把 model.to("cuda") 改为 model.to("tpu") 一样简单。(仅做这一简单更改无法实现 TPU 的性能峰值,因为模型的形状、分片和算子可能偏向 GPU 架构;但模型应能开箱即用。)
  • 为运行时/编译器错误提供用户友好且可操作的提示信息;
  • 与 PyTorch 社区已使用的工具和框架集成(例如 torch.profiler、DTensor、Ray)。
  • 提升技术水平:
  • 构建能够在大尺度工作负载中与 GPU 上的 PyTorch 相媲美的解决方案;
  • 为即时执行引入 JIT 编译方法,并通过运行时优化缩小逐算子执行与编译执行之间的性能差距;
  • 通过引入有界动态性、避免因形状变化而重新编译 XLA,以及探索可预先编译的动态 TPU 内核,来改进 TPU 编程栈。

TorchTPU is in active development, it is used in production for both inference and training workloads for some of the largest Google Cloud customers, and can be found as an Open Source project in this GitHub repo. At the time of this writing, we have:

TorchTPU 正在积极开发中,它已被用于一些最大的 Google Cloud 客户的推理和训练工作负载的生产环境,并且可以作为开源项目在以下 GitHub 仓库中找到。在撰写本文时,我们已经:

  • Added support for both eager and compiled modes, including a new DeferAndFuse eager mode;
  • Added support for ~450 Aten ops (skipping ops with structural or composite delegates), including forward and backward pass for all Core ATen ops required for the major deep learning architectures;
  • Added ability to author custom kernels via JAX/Pallas and via Helion DSL;
  • Validated out-of-box execution on the 5,000 most downloaded Hugging Face models, including:
  • LLMs: Qwen, DeepSeek, Gemma, LLama, Mistral, GPT, OPT, BLOOM, Falcon, StableML
  • Audio/Speech: Whisper, Wav2Vec2, HuBERT, MMS
  • Vision: DinoV2, SigLIP, CLIP, Swin Transformer, ConvNeXt, ViT, ResNet, DETR
  • Text Encoders & Search: BERT, ALBERT & Electra, RoBERTa, DeBERTa, T5, MT5, Flan-T5
  • Validated DTensor-based sharding;
  • Validated multi-host inference (vLLM and SGLang) and fine-tuning / training workloads (TorchTitan), with Ray-based orchestration;
  • Validated integration with vLLM, SGLang, TorchTitan, Ray, and TorchRec.
  • 添加了对即时模式和编译模式的支持,包括一种新的 DeferAndFuse 即时模式;
  • 添加了对约 450 个 Aten 算子的支持(跳过具有结构性或组合委托的算子),包括所有主要深度学习架构所需的核心 ATen 算子的前向和后向传播;
  • 添加了通过 JAX/Pallas 和 Helion DSL 编写自定义内核的能力;
  • 验证了 Hugging Face 下载量最高的 5,000 个模型的可开箱即用执行,包括:
  • 大语言模型:Qwen、DeepSeek、Gemma、LLama、Mistral、GPT、OPT、BLOOM、Falcon、StableML
  • 音频/语音:Whisper、Wav2Vec2、HuBERT、MMS
  • 视觉:DinoV2、SigLIP、CLIP、Swin Transformer、ConvNeXt、ViT、ResNet、DETR
  • 文本编码器 & 搜索:BERT、ALBERT & Electra、RoBERTa、DeBERTa、T5、MT5、Flan-T5
  • 验证了基于 DTensor 的分片;
  • 验证了多主机推理(vLLM 和 SGLang)以及微调/训练工作负载(TorchTitan),采用基于 Ray 的编排;
  • 验证了与 vLLM、SGLang、TorchTitan、Ray 和 TorchRec 的集成。

In the remainder of this document we discuss key aspects of the TorchTPU architecture and some initial results.

在本文档的其余部分,我们将讨论 TorchTPU 架构的关键方面以及一些初步结果。

Architecture Deep Dive

架构深入解析

A TPU Primer

TPU 入门

Google TPUs are specialized ASICs designed specifically to perform the massive tensor operations that are the foundation of deep learning. From a HW architecture perspective, a TPU chip contains (depending on the generation):

Google TPUs 是专为执行深度学习基础的庞大张量运算而设计的专用 ASIC。从硬件架构角度来看,TPU 芯片包含(因代际而异):

  • One or more TensorCores, each comprising one or more matrix-multiply units (MXUs), a vector unit (VPU), and a scalar unit (see this link). Based on the TPU generation, an MXU can include 256×256 multiply-accumulators, which are arranged as a systolic array.
  • One or more SparseCores, which are specialized units for sparse operations (e.g., gather, scatter, embeddings) and for collectives. They can run in parallel with a TensorCore, enabling compute-compute and compute-communication overlaps.
  • A large High Bandwidth Memory (HBM) – 288 GB at 8.6 TB/s in TPU 8i, which can be used to store ML data and share that with the CPU.
  • Bidirectional Inter-Chip Interconnect (ICI) – 19.2 TBps in TPU 8t and 8i, allowing fast communication between TPU chips within user-configured TPU Slices, which are logical subgroups of a TPU Pod, comprising of a preconfigured number of TPU chips connected in a 3D torus (for TPU 7, TPUv 8t) or Boardfly (TPU 8i) configuration.
  • Bidirectional Data-Center-Network (DCN) – 400 GBps in TPU 8t, allowing communication between TPU chips across Slices.
  • 一个或多个 TensorCore,每个由一个或多个矩阵乘法单元(MXU)、向量单元(VPU)和标量单元组成(参见此链接)。根据 TPU 的代际,MXU 可包含 256×256 个乘加器,这些乘加器排列成脉动阵列。
  • 一个或多个 SparseCores,这是用于稀疏操作(例如 gather、scatter、embeddings)和集合操作的专用单元。它们可以与 TensorCore 并行运行,从而实现计算与计算、计算与通信的重叠。
  • 大容量高带宽内存(HBM)——在 TPU 8i 中为 288 GB,带宽为 8.6 TB/s,可用于存储 ML 数据并与 CPU 共享这些数据。
  • 双向片间互连(ICI)——在 TPU 8t 和 8i 中为 19.2 TBps,允许用户配置的 TPU Slice 内的 TPU 芯片之间进行快速通信。TPU Slice 是 TPU Pod 的逻辑子组,由预配置数量的 TPU 芯片以 3D 环形拓扑(适用于 TPU 7、TPUv 8t)或 Boardfly(TPU 8i)配置连接而成。
  • 双向数据中心网络(DCN)——在 TPU 8t 中为 400 GBps,允许跨 Slice 的 TPU 芯片之间进行通信。

TPUs are programmed with the XLA compiler stack through two different entry points: StableHLO (higher-level IR for ML ops) and Mosaic (lower-level IR for custom kernels). The actual TPU ISA is proprietary, subject to change, and not publicly available. Effectively, one can think of StableHLO and (even more so) of Mosaic as equivalent to PTX. See picture below for a high-level sketch.

TPU 通过 XLA 编译器栈使用两个不同的入口点进行编程:StableHLO(用于 ML 操作的高级中间表示 IR)和 Mosaic(用于自定义内核的低级中间表示 IR)。实际的 TPU ISA 是专有的,可能会发生变化,且不对外公开。实际上,可以将 StableHLO 和(尤其是)Mosaic 视为等同于 PTX。请参阅下图的高层示意图。

A C++ program can access a local TPU by instantiating a PjRT Client, whose API provides methods to compile MLIR (StableHLO or Mosaic) into a TPU executable, to launch that executable, and to copy data to-from the TPU. Note that TPU execution is asynchronous and the CPU can wait for a TPU result to be ready by waiting on the associated PjRTBuffer.

C++ 程序可以通过实例化 PjRT Client 来访问本地 TPU,其 API 提供了将 MLIR(StableHLO 或 Mosaic)编译为 TPU 可执行文件、启动该可执行文件以及将数据复制到/从 TPU 的方法。请注意,TPU 执行是异步的,CPU 可以通过等待关联的 PjRTBuffer 来等待 TPU 结果就绪。

Notable features of the TPU programming model:

TPU 编程模型的显著特点:

  • A TensorCore device can be thought of as a single-threaded processor with one or more massive matrix-multiply units (MXU). Consequently, to reach peak performance it is key that a model’s shapes be sufficiently wide to exploit the full potential of the MXU. This differs from GPUs where launching multiple smaller kernels in parallel can help maximize HW utilization.
  • The XLA compiler can instruct a TensorCore to offload sparse and collective operations to SparseCore, for more efficient and/or parallel execution. This happens either automatically or through user-supplied MLIR annotation in a compiled StableHLO module.
  • TensorCore 设备可被视为具有一个或多个大规模矩阵乘法单元(MXU)的单线程处理器。因此,要达到峰值性能,关键在于模型的形状必须足够宽,以充分利用 MXU 的全部潜力。这与 GPU 不同,在 GPU 上,并行启动多个较小的内核有助于最大化硬件利用率。
  • XLA 编译器可以指示 TensorCore 将稀疏和集体操作卸载到 SparseCore,以实现更高效和/或并行的执行。这可以通过自动方式或在编译后的 StableHLO 模块中通过用户提供的 MLIR 注解来实现。

更进一步:量化金融体系

看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力

进入量化体系 →