在 TorchTPU 上用 Rust 跑 Gemma 4:单块 TPU 芯片的实测
来源:dev.to — 2026-11-02
📋 概述
作者在 Colab 的单块 TPU v5e 上,用 Rust crate 读取 Gemma 4 E2B 检查点、解包 4 位权重,跑完 35 层和环形缓存,并自己驱动解码循环,全程由 TorchTPU 内核执行,Python 只负责后端和启动。结果是 Rust 版逐 token 输出与编译后的 Python 完全一致,流式速度约为后者一半,峰值内存却不到一半,全程不碰 JAX。
🔑 核心要点
延迟融合模式跑到每秒 14.7 至 15.7 个 token ,Python 的 torch.compile 是 33.4,但后者峰值内存 16.9 GB,Rust 只有 7.75 GB。
严格 eager 模式掉到每秒 4.8 个 token ,说明延迟融合是性能的关键。
两种 Rust 实现在全 部 64 个贪心 token 与三个长提示答案上都与 Python 一致。
整个实现不涉及 JAX ,模型运算全部落在 TorchTPU 内核上。
💡 金句
“Rust writes the same tokens as compiled Python at about half the streaming speed and less than half the peak memory.”译:Rust 写出的 token 与编译后的 Python 相同,流式速度约为一半,峰值内存不到一半。
👍 0
❤️ 点赞
👎 0
沉底
← 返回 dev.to 首页