Train a GPT2 model with JAX on TPU for free- Google Developers Blog
Aug 12, 2025Wei Wei

Summary
This post provides a hands-on tutorial for building and pretraining a GPT-2 model using JAX on Google TPUs. It details the use of the JAX ecosystem (Flax, Orbax, Optax) for model definition, including the implementation of a Transformer block with NNX. It explains how to leverage SPMD for parallel computation across TPU cores and implement data parallelism. The post also covers setting up a hardware mesh, partitioning model parameters with `nnx.with_partitioning_`, defining JIT-compiled loss and training step functions, and using an AdamW optimizer with a cosine decay schedule. It demonstrates data partitioning using `jax.device_put` and mentions the use of Weights and Biases for tracking training progress.
More from the JAX Model Evaluation Metrics trail
- Accelerating Spatio-Temporal Attention for Video Diffusion on TPUs- Google Developers Blog
- Reproducing OLMo 3 7B Pre-training in MaxText: case study of large scale training on TPUs- Google Developers Blog
- Decoding cosmic signals with deep learning and Keras- Google Developers Blog
- Enterprise-Grade Precision for Long-Context Multimodal Embedding Inference on Cloud TPU- Google Developers Blog
- HeyGen x Google Cloud: Bringing Avatar IV to TPUs- Google Developers Blog