Hugging Face ❤️ TorchTPU: TPUs, Natively
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Back to Articles Hugging Face ❤️ TorchTPU: TPUs, Natively Published October 20, 2026
回到文章 Hugging Face ❤️ TorchTPU:原生支持 TPU,发布于 2026 年 10 月 20 日。
If you ever tried to run PyTorch on a TPU, you probably ported your code to torch_xla, or you ended up having to learn JAX. With TorchTPU, a native PyTorch backend, TPU is now just another device. It opens up native support of TPU in the Hugging Face ecosystem. Transformers, Diffusers and Kernels run natively on TPU. Pick a model from the Hub, send it to “tpu”, and you’re done.
如果你曾经尝试在 TPU 上运行 PyTorch,你可能需要将代码移植到 torch_xla,或者最终不得不学习 JAX。有了作为原生 PyTorch 后端的 TorchTPU,TPU 现在只是另一种设备。它在 Hugging Face 生态系统中开启了对 TPU 的原生支持。Transformers、Diffusers 和 Kernels 均可在 TPU 上原生运行。只需从 Hub 中选择一个模型,将其发送到 “tpu”,就大功告成了。
Wait, what’s a TPU? 🤔 TPU stands for Tensor Processing Unit. TPUs are Google’s own AI accelerators. They are chips built for one job: the matrix math that neural networks spend most of their time on. Google has been designing them for about ten years, and uses them to train and serve its own models, including Gemini.
等等,什么是 TPU?🤔 TPU 代表张量处理单元(Tensor Processing Unit)。TPU 是谷歌自有的 AI 加速器。它们是为了一项工作而制造的芯片:神经网络花费大部分时间进行的矩阵数学运算。谷歌设计这些芯片已有大约十年,并使用它们来训练和部署自己的模型,包括 Gemini。
TPUs are available on Google Cloud, from a single chip up to pods of thousands of chips. They shine when you need a lot of compute, for training or for serving at scale. The catch has always been software. The TPU stack grew up around TensorFlow and JAX, and PyTorch users had to work harder to get in.
TPU 可在 Google Cloud 上使用,从单芯片到数千个芯片的集群(pods)均可提供。当你需要大量计算资源进行训练或大规模部署时,它们表现尤为出色。问题一直在于软件。TPU 技术栈是围绕 TensorFlow 和 JAX 发展起来的,PyTorch 用户必须付出更多努力才能接入。
The old way: Until now, running PyTorch on TPUs meant going through PyTorch/XLA (torch_xla). It worked, but it had its quirks. Tensors were lazy (the tensors, not you), so your code didn’t always run when you thought it did. Small changes could trigger graph recompilations, and debugging was hard to reason about. To make life easier, we built Optimum TPU. It gave Hugging Face models inference and training support on TPUs. Now that TorchTPU brings native support, Optimum TPU is not required anymore to run Hugging Face models with Transformers and Diffusers.
旧方式:到目前为止,在 TPU 上运行 PyTorch 意味着必须通过 PyTorch/XLA (torch_xla)。它虽然有效,但有一些怪癖。张量是惰性的(是张量,不是你),所以你的代码并不总是在你认为的时候运行。微小的改动可能会触发图的重新编译,而且调试起来很难推断。为了让生活更轻松,我们构建了 Optimum TPU。它为 Hugging Face 模型提供了在 TPU 上的推理和训练支持。现在,随着 TorchTPU 带来原生支持,运行带有 Transformers 和 Diffusers 的 Hugging Face 模型已不再需要 Optimum TPU。
Enter TorchTPU 🚀 TorchTPU adds TPU as a new backend to PyTorch. You write regular PyTorch code. It comes with Pallas kernels tuned for TPUs, support for distributed training, and a torch.compile integration for when you want more speed. Installing it on a TPU VM takes one command: pip install torch-tpu —extra-index-url https://download.pytorch.org/whl/cpu. The extra index gives you the CPU build of torch. Without it, pip pulls the much bigger CUDA build, which you don’t need on a TPU.
引入 TorchTPU 🚀 TorchTPU 将 TPU 作为新的后端添加到 PyTorch 中。你只需编写常规的 PyTorch 代码。它附带了针对 TPU 优化的 Pallas 内核、对分布式训练的支持,以及当你需要更高速度时的 torch.compile 集成。在 TPU 虚拟机上安装它只需一条命令:pip install torch-tpu —extra-index-url https://download.pytorch.org/whl/cpu。额外的索引为你提供了 torch 的 CPU 构建版本。如果没有它,pip 会拉取体积大得多的 CUDA 构建版本,而你在 TPU 上并不需要它。