TL;DR
- MaxText v0.2.4 adds four context parallelism strategies (All-gather, All-to-All, P2P, and USP hybrid) to train LLMs efficiently at multi-million-token sequence lengths
- Llama 3.1 8B achieves 70% Model FLOPs Utilization on TPU 5p up to 4M tokens and maintains 50% MFU on TPU 7 up to 6M tokens
- Context parallelism splits long sequences across chips to avoid memory exhaustion while keeping attention compute efficient using the Splash Attention kernel
- Choice of strategy depends on whether gathered KV fits in HBM and the model's number of KV heads
- MaxText Version
- v0.2.4
- Max Sequence Length
- 14 million tokens
- Peak MFU (TPU 5p)
- 70% on Llama 3.1 8B
What changed
Google Cloud announced context parallelism support in MaxText, an open-source JAX training framework for long-context LLM training. The update introduces four context-parallel strategies: All-gather, All-to-All (DeepSpeed Ulysses), P2P (Ring Attention), and A2A + P2P hybrid (Unified Sequence Parallelism or USP). All strategies leverage the Splash Attention kernel from Tokamax, Google's open-source library of TPU kernels.
Context parallelism addresses two fundamental bottlenecks in long-context training. First, attention compute grows quadratically with sequence length, making the attention kernel critical for throughput. Second, activations including keys and values grow with sequence length; naive replication across chips exhausts high-bandwidth memory and triggers out-of-memory failures. Context parallelism streams these values instead of materializing them fully on every chip.
The feature works on Cloud TPU infrastructure including TPU 5p and TPU 7 (Ironwood), which have the 3D inter-chip interconnect needed for communication overlap. In benchmarks, Llama 3.1 8B demonstrates 70% Model FLOPs Utilization on TPU 5p for sequences up to 4 million tokens. Across TPU 5p and TPU 7, context can extend to 14 million tokens, with TPU 7 maintaining 50% MFU on sequences below 6 million tokens.
How the strategies differ
Each context parallelism strategy makes different tradeoffs between memory, communication overhead, and model shape. All-gather is the simplest: each chip keeps its sequence chunk and gathers keys and values from other chips at every layer. It works well when gathered KV fits in memory, though it requires load balancing because causal masking causes later tokens to attend to larger prefixes.
All-to-All (Ulysses) rotates sharding from the token axis to the head axis. Each chip gives up heads it does not own and receives missing tokens for heads it keeps, ending with the full sequence for a subset of heads instead of a subset of tokens for all heads. This eliminates the need for token load balancing, but restricts context parallelism to be no larger than the model's head count. Tensor parallelism shares the same head dimension, so the product of tensor parallelism and context parallelism is capped by head count.
P2P (Ring Attention) removes the head-count ceiling by keeping keys and values on the token axis. KV chunks travel around a ring of chips, with each chip attending while the next chunk arrives in flight. A chip holds only its own chunk and one visiting chunk, with communication overlapping compute. However, smaller attention chunks can reduce local kernel efficiency, and explicit load balancing is required.
Unified Sequence Parallelism combines All-to-All and P2P on two axes of the chip mesh. All-to-All operates along one axis up to the KV-head count; a ring extends along the other axis beyond it. Larger chunks per ring step improve compute-per-hop ratios and reduce latency accumulation compared to P2P alone.
Choosing the right strategy
Selection of the context parallelism strategy depends primarily on whether gathered KV fits in high-bandwidth memory and the model's KV-head count. When gathered KV fits, All-gather with load balancing is typically fastest and simplest. For grouped-query models, KV size is a small fraction of activations, making all-gather cheaper. When gathered KV no longer fits, the ring family—P2P or hybrid USP—keeps memory constant per chip instead of growing with the whole sequence.
All-to-All alone suits scenarios where a sequence spans only a few chips up to the KV-head count, with no ring component. For multi-query attention models that do not support All-to-All, or when the head count does not divide evenly for the all-to-all factor, P2P remains the choice. The hybrid approach (USP) is recommended when KV heads allow an All-to-All factor of 2 or more, as it maintains performance competitive with all-gather while supporting longer sequences across more chips.
When memory becomes the bottleneck even with context parallelism, MaxText supports offloading saved activations, optimizer states, and parameters to host memory tensor by tensor. At long context lengths, these transfers are hidden behind compute.
Who it affects
This feature is relevant to teams training large language models at production scale on Google Cloud TPUs. Researchers and engineers working with models that benefit from million-token native sequence lengths—such as those reading entire code repositories or processing hours of video—can now train more efficiently. The announcement is also relevant to users currently limited by memory constraints when training at moderate context lengths, as context parallelism can enable longer sequences on the same hardware.
What to do
The functionality is available today in MaxText v0.2.4. Users should consult the MaxText documentation to configure context parallelism for their workloads. The choice of strategy depends on model architecture, sequence length requirements, and available hardware. Start with all-gather if gathered KV fits in memory; otherwise, use the ring family (P2P or USP hybrid) based on model head count and desired sequence length.
Source
Google Developers Bloghttps://developers.googleblog.com/announcing-long-context-training-on-tpus-with-maxtext-context-parallelism/AI-assisted, verified against the source. This post was drafted with AI from the primary source linked above and checked automatically against it. For anything you depend on, read the original.
- #tpu
- #llms
- #maxtext
- #long-contexttraining
- #distributedtraining
