Model and Inference EngineeringTraining execution and recoveryPrincipal
Why might Megatron-style sequence parallelism fail to solve a long-context attention OOM?
By Amit Kumar2 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
A Megatron-style training run enables sequence parallelism and saves activation memory, but a 64K step still runs out of memory inside attention. Explain which state sequence parallelism actually shards, design a peak-memory diagnosis, and compare kernel repair, context parallelism, recomputation and microbatch changes with their communication and quality costs.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
Different parallelism modes divide different tensors. In Megatron-style sequence parallelism, certain activations such as LayerNorm and dropout are split along the sequence dimension across tensor-parallel ranks. It does not mean each rank owns only its fraction of the entire attention computation and all its KV activations. Megatron Core's context-parallel documentation makes this distinction and introduces context parallelism for partitioning the network inputs and activations across the sequence dimension.
At 64K, I would look at the actual peak, not infer it from total allocated memory after the OOM. Is the peak attention workspace, QKV activations retained for backward, an unoptimized attention matrix, communication buffers, or a large microbatch? FlashAttention-style kernels avoid materializing a full sequence-by-sequence attention matrix, but they do not remove the need to compute and backpropagate over long contexts. Check whether the chosen attention kernel is actually used for this shape and mask. A silent fallback can make memory jump even though the flags say sequence parallelism is on.
For scale, a hypothetical fully materialized 65,536 by 65,536 attention-score matrix has 4,294,967,296 elements. At two bytes per element that is 8 GiB for just one head, before gradients or other state. This is a diagnostic illustration, not the memory layout of FlashAttention or a promise that the configured kernel allocates that matrix. A fallback that materializes scores can dominate a run even when sequence parallelism saved memory elsewhere.
Trace the selected kernel, tensor shapes and allocation lifetime at the failing operator. Then compare a reference step under the intended supported mask/dtype. If the kernel is correct and the remaining Q/K/V or backward state exceeds the per-rank budget, distribute that owner or trade storage for recomputation. Adding workers that only shard a different activation class cannot remove an unrelated replicated allocation.
If attention states are the issue, context parallelism can distribute sequence work more broadly, but it has to exchange remote KV information or otherwise compute attention across sequence shards. That trades memory for communication and implementation complexity. Activation recomputation trades memory for repeated compute. Smaller microbatches, a shorter sequence, or a different attention pattern are other choices, each with a different effect on the training objective and throughput. I would test one representative 64K step with peak memory broken down by rank and timed collectives before choosing.
Can we just add more sequence-parallel ranks? That can shrink the subset of activations SP actually shards, but it cannot solve a separate attention peak by arithmetic magic. More tensor-parallel ranks may also raise collective cost. Name the specific memory owner before selecting the parallelism axis.
Related questions
Keep practising model and inference engineering
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux