NanoChat JAX & Flax NNX Port (TPU Sprint)
Port of Andrej Karpathy’s NanoChat LLM architecture to JAX and Flax NNX for Google Cloud TPUs.

Context and problem
Porting PyTorch-centric LLM training loops to Google Cloud TPUs often hits XLA recompilation traps, dynamic-shape bottlenecks, and stateful module friction.
Created as part of the AI Google Developer Expert TPU Sprint 2026. Successfully ported the NanoChat architecture from PyTorch to pure JAX and Flax NNX, enabling accelerated distributed training and low-latency inference on Google Cloud TPU v4/v5e pods.
My contribution
Rebuilt Andrej Karpathy’s NanoChat in pure JAX and Flax NNX for the 2026 AI Google Developer Expert TPU Sprint, authoring the training pipeline, checkpoint converter, and empirical XLA benchmark write-up.
Approach and key decisions
- Migrated model definitions to Flax NNX for explicit, inspectable state management compatible with `jax.jit` and `jax.value_and_grad` compilation.
- Implemented static sequence packing and `NamedSharding` tensor parallelism across Google Cloud TPU v4/v5e slices to eliminate XLA graph retracing.
- Published reproducible TinyShakespeare training benchmarks and PyTorch-to-JAX weight conversion utilities.
Outcome
Achieved 26,028 tokens/second (nano) and 17,118 tokens/second (small) on TinyShakespeare with open MIT-licensed training code and a technical post in the Google Developer Experts collective on DEV.
The source code is public, so the implementation speaks for itself. Questions about design decisions or adapting it to your environment are welcome: get in touch
Architecture Dossier
- Role
- AI GDE Contributor
- Stage
- GDE TPU Sprint 2026
- Category
- Applied research
- Source code
- Public repository
Technology
- JAX
- Flax NNX
- Google TPU
- Python
- LLM Pretraining
Topics
- Model portability
- Accelerator performance
- Open source