Hugging Face ❤️ TorchTPU: TPUs, Natively

2026年10月20日 00:00
站內 AI 整理稿

Back to Articles Hugging Face ❤️ TorchTPU: TPUs, Natively Published October 20, 2026 Update on GitHub Upvote 2 Alvaro Moran tengomucho Follow Jingya Huang Jingya Follow Simon Pagezy pagezyhf Follow Jeff Boudier jeffboudier Follow Régis Pierrard regisss Follow If you ever tried to run PyTorch on a TPU, you probably ported your code to torchxla, 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.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.

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.

The old way Until now, running PyTorch on TPUs meant going through PyTorch/XLA (torchxla).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.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.

Your favourite libraries, now on TPU Transformers Transformers v5 was built around one idea: be the source of truth for model definitions, so they can run anywhere."Anywhere" now includes TPUs.To load a model on a TPU, pass devicemap="tpu".That's it.

from transformers import AutoModelForCausalLM, AutoTokenizer modelid = "Qwen/Qwen3.5-9B" model = AutoModelForCausalLM.frompretrained( modelid, devicemap="tpu", # will load model directly on the TPU ) tokenizer = AutoTokenizer.

frompretrained(modelid) inputs = tokenizer("The future of AI is", returntensors="pt").to("tpu") out = model.generate(**inputs, maxnewtokens=64) print(tokenizer.decode(out[0], skipspecialtokens=True)) The model runs in eager mode by default.You can also use torch.

compile for better performance, or run it in a distributed setup.Images?Videos?Diffusers paints them on TPUs 🎨 TPU is now a first-class device in Diffusers.For prototyping or debugging, eager mode only needs pipe.to("tpu").For deployment, compiled mode adds one compile call.

Your pipelines and code stay the same, and there's no TPU fork to maintain.import torch from diffusers import StableDiffusion3Pipeline pipe = StableDiffusion3Pipeline.frompretrained( "stabilityai/stable-diffusion-3.5-medium", dtype=torch.bfloat16 ) pipe.

to("tpu") # eager mode: that's the whole change # optional: compiled mode pipe.transformer.compile(backend="tpu", fullgraph=True, dynamic=False) image = pipe("a corgi astronaut planting a flag on a pizza moon").

images[0] The first call compiles, and every call after that with the same image size reuses the graph.Models too big for one chip?Pass a TensorParallelConfig to frompretrained and the transformer is sharded across chips as it loads, so the full model never has to fit in one place.

Many popular pipelines support it out of the box, and the ones that don't are just a tpplan away.The Diffusers TPU guide has the full recipe.Here are some examples run on a Cloud TPU v6e-4: TP=4 (each model split across four chips) · bf16 · batch 1 MiniMax-H3 (video + audio) Qwen-Image-2.

1 "The fluffy plush Hugging Face mascot from the picture, a round yellow smiling face with rosy cheeks and two little open hands, stands at the prow of an ancient Greek trireme […] Sound of crashing waves, creaking timber, wind in the rigging, distant seagulls, and a low epic orchestral swell.

" · 1344×768, 5.2 s, 49 steps · 188 s compiled, 262 s eager "A capybara wearing a wizard hat, reading a book by candlelight, oil painting" · 1024×1024, 40 steps · 3.5 s compiled, 8.0 s eager "A neon shop sign that reads 'QWEN IMAGE 2.

1', rainy night, reflections on wet pavement" · 2048×2048, 40 steps Kernels The Kernels Hub has ready-to-use TPU kernels.Load one with the Kernels library and give your model a speed boost.TPU kernels are written in Pallas, which gives you low-level control over the hardware.

You can also bring in kernels from the Tokamax library.Here is a ragged-dot kernel from Tokamax plugged into a Mixture-of-Experts model: from kernels import getkernel from transformers import AutoModelForCausalLM from transformers.integrations.

moe import ALLEXPERTSFUNCTIONS repoid = "tengomucho/ragged-dot-tpu" kernel = getkernel(repoid, version=1, trustremotecode=[repoid]) ALLEXPERTSFUNCTIONS.register("tokamaxraggeddot", kernel.expertsforward) model = AutoModelForCausalLM.

frompretrained( "Qwen/Qwen3-30B-A3B-Instruct-2507", expertsimplementation="tokamaxragged_dot", ) The bigger the batch, the bigger the win.At batch size 64, decoding is 21.5× faster on Qwen3-30B-A3B and 12.8× faster on Gemma 4 26B-A4B.Decode throughput on a TPU v6e-8, Hub kernel vs.

the default implementation.We test it so you don't have to 🧪 We're continuously improving TPU support in transformers by running the library test suite.We are working to add a nightly CI on TPU and make the results available in the (transformers dashboard)[https://huggingface.

co/spaces/transformers-community/transformers-ci-dashboard].The Transformers CI dashboard, with TPU results next to other accelerators.Work in progress!It's not all green yet, and we're fine with that.Of the tests that run on TPU, 96.5% pass.

Every failure we find becomes a small repro script and an issue on the TorchTPU repo.So far we have reported 34 issues, and 19 are already fixed.Running on TPU also caught a few bugs in Transformers itself, like in StaticCache and the Qwen3 tensor-parallel plans.We fixed those upstream.

If something breaks on TPU, we want to find out before you do.Get started TPUs are no longer a side quest for PyTorch users.Install TorchTPU, grab a model from the Hub, and send it to "tpu".TorchTPU on GitHub TPU kernels on the Hub Cloud TPU documentation We can't wait to see what you build 🤗.

If you hit a problem, open an issue and tell us about it.

More Articles from our Blog partnershipsgcpspaces Google Cloud TPUs made available to Hugging Face users 24 July 9, 2024 partnershipsgcphardware Making thousands of open LLMs bloom in the Vertex AI Model Garden 22 April 10, 2024 Community EditPreview Upload images, audio, and videos by dragging in the text input, pasting, or clicking here.

Tap or paste here to upload images Comment · Sign up or log in to comment Upvote 2

Related

相關文章

抖音電商公佈雙11開門紅數據:消費券帶動成交額破億商家數漲2倍

抖音電商公佈2026年雙11“開門紅”階段數據報告。報告顯示,10月15日至18日大促“開門紅”階段,平臺上消費券帶動成交額破億的商家數量同比增長200%。平臺出資消費券與分層扶持政策共同發力,助力不同發展階段商家在大促開局實現生意增長;店播、特色行業商家和農產品產業帶生意集中爆發。平臺全資消費券,助力不同階段商家生意爆發報告顯示,開門紅期間平臺出資消費券持續帶動商家生意增長。

11 小時前

全年最優惠!真正的雙11今晚8點天貓開賣

全年最優惠時刻開啟!真正的雙11今晚8點天貓開賣。 到手價就是划算的,店鋪立減疊加平臺消費券,低至7.2折。如果再加上行業加補券、國家補貼、淘金幣這些權益,到手價還能更低,甚至到5折! 這樣買省更多已經加好購物車的消費者,今晚記得做兩件事。 第一,領齊券。能領的權益都去領,重點是平臺消費券、行業加補券、國家補貼; 第二,用對券。今年消費券有個新玩法叫“膨脹”——先看購物車裡的商品總價,再決定膨脹到哪個面值,這樣買更划算。 一句話總結4個動作:先領券,看總價,後膨脹,再下單。 這些爆款直接買今年參加天貓雙11打折的商品數量歷屆最多。 準備換家電、手機或平板的消費者,至高可額外享15%國補,低至6折。 舉幾個例子:美的酷省電2代大1匹掛機空調,國補到手價1699元;新品OPPO Find X10,國補到手價4999元;連Hypershell極殼外骨骼機器人也能用國補,到手價5949元。智能門鎖、數碼相機同樣在補貼範圍內。 蘋果天貓官方旗艦店的配件首次支持消費券,這是頭一回。Max 40W充電頭、iPhone Duo手機殼、1米充電線,都能用券,至少8.7折。iPhone 18 Pro系列雙11優惠800元、iPhone Duo享受以舊換新補貼至高700元,支持12期免息。 潮流、美妝和黃金同樣有新品及專屬優惠:ALO Accolade系列王一博同款作為全球新品在天貓首發;老鋪黃金參與天貓雙11優惠,首次推出“滿30000減2000”、“滿60000減4000”的店鋪優惠券;海藍之謎精萃水250ml為天貓獨家,買正送正享同款450ml。 網友熱盼雙11到來,在社交媒體評論:“雙11就像牛郎等織女,一年只見一次面”。今晚8點,大家一起打開購物車,把心心念唸的東西一次買齊吧。

14 小時前

抖音商城雙11首日數據:消費券帶動破千萬商家數同比增長74%

本文作者: Nemo 2026-10-17 12:16 導語:10月15日,2026抖音商城雙11好物節正式開啟。10月15日,2026抖音商城雙11好物節正式開啟。今年雙11覆蓋服飾、美妝個護、快消耐消等重點類目,橫跨秋冬換季、囤貨等消費節點,通過多場景、多節點助力商家把握年末增長機遇,實現銷量與生意規模的持續提升。

3 天前

抖音電商公佈雙11首日數據:店播成交額破千萬元商家數量同比增長96%

抖音電商公佈2026年雙11首日數據。報告顯示,10月15日大促首日,平臺上成交額新突破百萬元的商家數量同比增長74%,店播成交額破千萬元的商家數量同比增長96%。眾多剛在平臺起步不久的中小新商,藉助平臺分層扶持和經營激勵,在大促首日完成了成交額突破百萬的成績。同時,今年雙11開局呈現出商家與達人成長、秋冬換季消費活躍的特點,首次參加雙11且成交額突破百萬達人數量同比增長30%。商品卡、搜索等多種路徑,助力商家接住更多好生意報告顯示,抖音電商出資消費券在大促首日帶動消費增長。

4 天前

京東左琦發佈詆譭信息被起訴,再發謠言後抖音電商表示將追加起訴

記者近日從抖音電商獲悉,因在“618”大促期間通過朋友圈發佈不實信息、詆譭其他平臺,京東公司左琦被抖音電商起訴,北京市海淀區人民法院已正式立案受理。10月16日,抖音黑板報發佈聲明稱,左琦被起訴後再次在朋友圈發佈謠言,抖音電商已對其最新謠言內容完成法律存證,並將依法追加起訴。

4 天前