Porting from JAX to TorchTPU: after fixing dedup bugs, beam search beats JAX by 25%

AndLukyane · x · 2026-11-25

A hands-on port of a puzzle-solving transformer and its beam search from A100/JAX to Google's new TorchTPU (native PyTorch TPU backend), with code written by coding agents (Claude Code, Codex). Initial results were bad: training 1.65x slower than A100, beam search 5.7x slower than JAX.

Key fixes: fused eager via one env var (234.8s → 176.5s per epoch), einsum attention (→132.0s vs A100's 142.7s), eliminating dynamic shapes, a subtle deduplication bug where unstable sorting let two score copies of the same state compete and drop shorter paths (fixed with keep min(score, index)), and Flash attention tuning (3.74s → 0.93s per beam level on a v6e chip).

Result: optimized TorchTPU beam search runs 25% faster per level than JAX on the same 8-chip v6e machine with identical solution lengths. The investigation led to upstream TorchTPU attention-routing fixes. Code available on free Kaggle TPU v5e-8 notebooks.

Related event: Developer Migrates Solver to TorchTPU, Beam Search Beats JAX by 25%(2 posts)→

Original post →

More from Infra

Infra channel →