Companion GitHub Repository: agemagician/torchtpu-zero-to-hero
12,115.8 MiB -> 610.5 MiB at 4K Context, and 1,221.0 MiB at 8K Where Standard Attention OOMs
At long context lengths ( S >= 4,096 ), materializing the quadratic [B, H, S, S] attention score matrix in off-chip High Bandwidth Memory (HBM) hits a memory-bandwidth wall and crashes single-chip 16 GiB TPUs with RESOURCE_EXHAUSTED .
On NVIDIA GPUs, you reach for FlashAttention and Triton . On Google Cloud TPUs with TorchTPU , you reach for:
torch.tpu.ops.splash_sdpa (Splash Attention): Google's hardware-tuned Pallas Sparse Flash Attention kernel exposed as a native differentiable PyTorch operator ( 6.39x faster and 19.8x less HBM at S = 4,096 on our live TPU v5e-1 benchmark!).
torch.tpu.pallas.jax_op + pallas.verifier.verify : Write custom VMEM-tiled Pallas kernels that trace Mosaic MLIR once on CPU ( JAX_PLATFORMS="cpu" ) and execute at zero runtime JAX cost directly on torch.Tensor buffers — backed by static contract, lowering, and autograd…