Porting NanoChat to JAX and Flax NNX: Accelerating LLMs on Google Cloud TPUs

As part of the AI Google Developer Expert (GDE) TPU Sprint 2026, I ported Andrej Karpathy’s NanoChat architecture from PyTorch to JAX and Flax NNX. The port trains an 885K-parameter nano model on TinyStories in under 10 minutes on a single GPU and runs unchanged on Google Cloud TPUs.
Introduction to the TPU Sprint
The objective of the TPU Sprint is to run clean LLM training loops on Google Cloud TPU v4 and v5e hardware accelerators using pure JAX transformations. PyTorch has long been the default framework for LLM experimentation, while JAX provides whole-graph optimization through the XLA compiler after a one-time upfront compilation cost.
Key Architectural Takeaways
- Functional Purity and JIT: Using JAX functional transformations (
jax.jit,jax.grad,jax.vmap) to eliminate Python dispatch overhead once compilation finishes. - Flax NNX Paradigm: Moving to Flax’s NNX module API for explicit, inspectable state handling without heavy functional plumbing.
- Distributed TPU Training: Sharding tensor parameters across TPU v4 and v5e pods with
jax.experimental.shard_mapandNamedSharding.
Benchmark Results
Measured on the TinyShakespeare character-level task, the port reaches 26,028 tokens/second for the nano configuration and 17,118 tokens/second for the small one, with an empirical loss curve of L = 3.29 x N^-0.027 at 600 steps. The numbers, training scripts, streaming chat UI, and checkpoint converter are all in the repository so they can be reproduced directly.
Conclusion and Open-Source Repository
The complete implementation, checkpoint converter, and training scripts are available at github.com/ainaomotayo/nanochat-jax under the MIT license, with the companion essay on DEV Community (GDE).
