How does Google train models on 14 million tokens without running out of memory?
Google has added long-context training to MaxText, its open-source JAX framework for Cloud TPUs. The update lets a single sequence stretch across an entire TPU slice while memory per chip stays flat.
Four ways to split a sequence across chips
MaxText v0.2.4 introduces four context-parallel strategies. All-gather collects every key and value on every chip at each layer. All-to-All, also called Ulysses, swaps token chunks for attention heads so each chip holds the full sequence for only a few heads. P2P, or ring attention, passes key-value chunks around a ring one hop at a time. The hybrid USP combines an All-to-All on one mesh axis with a ring on the other. Every strategy runs on the Tokamax Splash Attention kernel. The kernel skips fully masked blocks and handles packed sequences without padding.
What each strategy trades off
All-gather is the simplest option. Its memory grows with the whole sequence, so it hits the HBM limit first. When the gathered KV fits, it was the fastest or nearly the fastest in Google's runs. Ulysses shrinks KV per chip by the context-parallel factor. You cannot split across more chips than the model has KV heads — eight for Llama 3 8B. The all-to-all does not overlap with compute; every chip waits for data from every other chip before attention starts. Ring attention removes the head-count ceiling. A chip holds its own chunk and one visiting chunk, never the full sequence. Communication overlaps compute. Dividing attention into smaller chunks can reduce local kernel efficiency, and ring attention requires explicit load balancing. USP composes both axes. The All-to-All factor must be two or more. Each ring step then carries a larger chunk, the ring is shorter, and more compute per hop hides the transfer. USP keeps pace with all-gather for as long as all-gather fits. P2P alone remains the choice for MLA models such as DeepSeek, or when the head count does not divide.
Measured results on TPU 5p and TPU 7
Llama 3.1 8B ran on a fixed 64-chip TPU 5p slice with 256 head dim and 16 query heads. P2P reached 70 percent MFU at sequence lengths up to 4 million tokens. Context extended seamlessly to 14 million tokens on both TPU 5p and TPU 7. TPU 7 consistently maintained 50 percent MFU across all sequence scales below 6 million tokens. On TPU 7, P2P training of 14 million tokens on 64 chips reached 48.7 percent MFU compared to 37.1 percent on TPU 5p. Ring attention outperformed Ulysses and USP on TPU 7 chips.
Moving activations to host memory
When memory runs low, offloading keeps MFU flat through the offload steps. MaxText moves saved activations, optimizer states, and parameters to host memory tensor by tensor. The recommended order: attention outputs first, then optimizer states, layer inputs, parameters, and finally remat as a last resort.
What comes next and how to start
Broader model coverage is planned. Gemma mixes sliding-window and global layers; window layers would run with a short neighbor exchange while global layers use the ring. Linear attention mechanisms such as Gated Delta Net and Kimi Delta Attention are also on the roadmap. Performance work continues: further tuning of Tokamax kernels and the P2P backward pass to narrow the gap to all-gather at short context, plus recipes by model and context length for TPU 5p and TPU 7. The feature is live in MaxText v0.2.4 on PyPI, in Docker images, and on GitHub main. Sample recipes exist for TPU 7 and TPU 5p. Pick a strategy with context_parallel_strategy — ring, ulysses, or usp — set the context-parallel size, and keep the rest of your run the same. The sharding guide lists flags and constraints for each strategy.
The sources this article was written from
- "Google Developers Blog
https://developers.googleblog.com/announcing-long-context-training-on-tpus-with-maxtext-context-parallelism/"