From CUDA and Jax to TorchTPU: Lessons from a Cayley Graph Solver
An engineer ports a combinatorial puzzle solver to Google's TorchTPU, overcoming low matrix unit occupancy and attention-routing bugs to match A100 training speed and beat JAX beam search performance.
read it Essential reading for engineers optimizing PyTorch models on TPUs, offering concrete lessons on static shapes, attention overhead, and eager fusion.
summary
The author ports an AI-assisted Cayley graph solver from A100/CUDA and JAX to Google's native PyTorch TPU backend, TorchTPU. Initial performance lagged due to poor TPU matrix unit utilization on small attention shapes and host synchronization overhead from data-dependent operations. By implementing fused eager execution, rewriting attention with einsum to avoid layout conversions, and bypassing a Flash attention routing regression, the author achieved parity with A100 training speeds and surpassed JAX beam search performance.
key points
- Small attention shapes like sequence length 51 and head dimension 32 yield only ~2.5% TPU matrix unit occupancy, making attention the dominant bottleneck.
- Enabling fused eager execution via TORCH_TPU_DEFER_AND_FUSE=1 accelerates workflows with many small operations without needing full graph compilation.
- Data-dependent operations such as torch.unique and boolean masking force expensive host round-trips and recompilations, requiring static-shape alternatives.
- Flash attention defaults can severely degrade performance on short sequences, which can be mitigated by falling back to standard XLA paths.
who should read it
Machine learning engineers and systems developers optimizing deep learning models for TPU hardware.
discussion positive
With T=51 and head_dim=32 giving only ~2.5% MXU tile occupancy, did you consider packing multiple puzzle states into a single tile while keeping their attention independent? Or does XLA already exploit the batch dimension for this?
It questions whether packing multiple puzzle states into a single tile could mitigate the low 2.5% matrix unit occupancy caused by small attention shapes.
Generated by an LLM from the article and thread. Check the sources before relying on it.