Scaling context windows in Large Language Models (LLMs) from 8,192 tokens to 128k, 256k, and 1M tokens introduces severe memory and compute constraints. Standard Data Parallelism (DP) duplicates entire model activations per sequence, failing when a single sequence exceeds GPU VRAM. Tensor Parallelism (TP) splits weight matrices across GPUs, but scaling TP beyond a single 8-GPU node degrades throughput due to high-frequency, low-latency cross-GPU collective communications. Pipeline Parallelism (PP) partitions layers across GPUs, but leaves per-layer activation memory unaddressed.
Context Parallelism (CP), also categorized broadly as Sequence Parallelism (SP), solves this bottleneck by distributing the sequence dimension across multiple accelerators. Several distinct architectures have emerged to address long-sequence distributed training and inference: Megatron-LM Sequence Parallelism (Megatron-SP), DeepSpeed Ulysses, Ring Attention, and Unified Sequence Parallelism (USP).
This technical analysis evaluates the mathematical formulations, communication topologies, attention head constraints, memory scaling profiles, and production serving trade-offs of each approach.

1. The Long-Context Scaling Wall
In standard Transformer architectures, the memory footprint during training and inference consists of model parameters, optimizer states, gradients, Key-Value (KV) cache, and intermediate activation tensors.
While parameter and optimizer memory scale with parameter count, attention activation memory and KV cache memory scale linearly or quadratically with sequence length N:
Even when using memory-efficient exact attention such as FlashAttention-2, which avoids materializing the full N x N attention matrix in High Bandwidth Memory (HBM) by computing softmax in SRAM tiles, the activation memory for storing query, key, value projections, LayerNorm outputs, and feed-forward intermediate states still scales linearly with sequence length:
Activation Memory per Layer is approximately B * N * d_model * c_layer, where B is batch size, N is sequence length, d_model is hidden dimension, and c_layer is a constant determined by the layer architecture (typically 16 to 34 bytes per token per layer depending on recomputation strategy).
For an 8-billion parameter model (d_model = 4096, 32 layers) with a batch size of 1 at a sequence length of 128,000 tokens in bfloat16 precision:
- KV cache requires approximately 2 * 2 * 32 * 8 * 128 * 128,000 * 2 bytes (approximately 33.5 GB for 8 KV heads with head dimension 128).
- Activation tensors without recomputation exceed 140 GB per sequence.
When sequence length scales into hundreds of thousands or millions of tokens, a single sample cannot fit into the memory of an 80 GB NVIDIA H100 GPU, regardless of batch size. Sequence partitioning across multiple GPUs becomes necessary.
2. Megatron-LM Sequence Parallelism (Megatron-SP)
Introduced by Korthikanti et al. (2022), Megatron-LM Sequence Parallelism was designed specifically to eliminate redundant activation storage across Tensor Parallelism (TP) groups.
Architecture and Mechanism
In standard Megatron-LM Tensor Parallelism:
- Self-attention linear projections (W_Q, W_K, W_V) and the first MLP projection (W_gate, W_up) are column-parallel.
- Self-attention output projection (W_O) and the second MLP projection (W_down) are row-parallel.
- In row-parallel operations, an All-Reduce operation sums the partial outputs across all TP ranks.
- In non-tensor-parallel regions (LayerNorm, Dropout, Residual Additions), every GPU within the TP group holds an identical, replicated copy of the entire activation tensor of shape [B, N, d_model].
Megatron-SP observes that these non-tensor-parallel regions do not perform inter-token interactions. Therefore, the activation tensor can be partitioned along the sequence dimension across the TP ranks, so each GPU holds only [B, N / TP, d_model].
Megatron-TP:
[LayerNorm (Full Seq)] -> [All-Gather] -> [Column-Parallel QKV] -> [Row-Parallel W_O] -> [All-Reduce] -> [Dropout (Full Seq)]
Megatron-SP:
[LayerNorm (Seq / TP)] -> [All-Gather] -> [Column-Parallel QKV] -> [Row-Parallel W_O] -> [Reduce-Scatter] -> [Dropout (Seq / TP)]The standard All-Reduce communication in TP consists of two phases: Reduce-Scatter followed by All-Gather. Megatron-SP splits these primitives across the boundaries:
- In the forward pass, before entering the column-parallel region, an All-Gather reconstructs the full sequence [B, N, d_model].
- After the row-parallel region, a Reduce-Scatter replaces the All-Reduce, outputting the partitioned sequence [B, N / TP, d_model].
Strengths and Limitations
- Zero Additional Communication: The total communication volume per layer remains 2x the activation size, exactly identical to standard TP's All-Reduce.
- Activation Reduction: Cuts non-attention activation memory by a factor of TP.
- Boundary Constraint: The degree of sequence parallelism is strictly tied to the Tensor Parallelism degree (SP = TP <= 8). It cannot scale sequence length independently of model dimension or across multi-node InfiniBand networks due to TP latency sensitivity.
3. DeepSpeed Ulysses
Published by Jacobs et al. (2023), DeepSpeed Ulysses decouples sequence parallelism from Tensor Parallelism by using global All-to-All transpositions around the core self-attention operator.
Input Tokens: [Batch, Seq / P, Hidden]
│
▼ (Q, K, V Projections)
Q, K, V: [Batch, Seq / P, Num_Heads, Head_Dim]
│
┌─────┴────────────────────────┐
│ All-to-All-v (Token -> Head) │
└─────┬────────────────────────┘
▼
Q, K, V: [Batch, Full_Seq, Num_Heads / P, Head_Dim]
│
▼ (Local FlashAttention on full sequence per head subset)
Attention: [Batch, Full_Seq, Num_Heads / P, Head_Dim]
│
┌─────┴────────────────────────┐
│ All-to-All-v (Head -> Token) │
└─────┬────────────────────────┘
▼
Output: [Batch, Seq / P, Num_Heads, Head_Dim]
│
▼ (Linear Out Projection, MLP, LayerNorm)Mechanism and Data Flow
Let P be the Ulysses sequence parallel degree, N the total sequence length, and H the number of attention heads:
- Partitioning by Sequence: Each GPU receives a contiguous sequence slice of length N/P with all hidden dimensions and attention heads: [B, N/P, H, d_head].
- QKV Projections: Projections are computed locally on the token slice.
- First All-to-All Collective: An All-to-All-v operation transposes the partitioned sequence dimension into a partitioned head dimension. After communication, each GPU holds the full sequence length N for a subset of attention heads H/P: shape [B, N, H/P, d_head].
- Local Attention Computation: Any optimized single-GPU attention kernel (such as FlashAttention-2 or FlashDecoding) executes locally on the full sequence for the local heads. No inter-GPU communication occurs during attention computation.
- Second All-to-All Collective: An All-to-All-v transposes the attention output back from partitioned heads to partitioned sequence tokens: shape [B, N/P, H, d_head].
- Feed-Forward and LayerNorm: All subsequent MLP and normalization layers operate purely on the N/P slice locally.
Communication Complexity and Constraints
- Communication Volume: In each layer, the two All-to-All calls transfer 2 * 2 * B * N * d_model * ((P-1)/P) bytes in the forward pass (and 4x in backward). Communication volume per GPU is O(N/P), remaining constant as sequence length and GPU count scale proportionally.
- Head Divisibility Constraint: The degree of parallelism P must divide the number of query attention heads (P <= H_Q). Under Grouped-Query Attention (GQA), P must divide the number of Key-Value heads (P <= H_KV). For models with 8 KV heads (such as Llama-3-70B), pure Ulysses cannot scale beyond P = 8.
4. Ring Attention and Striped/Zigzag Attention
Introduced by Liu et al. (2023), Ring Attention formulates long-context distributed attention as a blockwise circular peer-to-peer (P2P) token-passing ring.
GPU 0 (Tokens 0..N/P-1) GPU 1 (Tokens N/P..2N/P-1) GPU 2 (Tokens 2N/P..3N/P-1)
┌───────────────────────┐ ┌────────────────────────┐ ┌─────────────────────────┐
│ Q_0 (Local, Static) │ │ Q_1 (Local, Static) │ │ Q_2 (Local, Static) │
│ K_0, V_0 (Current) │ │ K_1, V_1 (Current) │ │ K_2, V_2 (Current) │
└──────────┬────────────┘ └──────────┬─────────────┘ └───────────┬─────────────┘
│ Send K, V │ Send K, V │ Send K, V
▼ ▼ ▼
[To GPU 1] [To GPU 2] [To GPU 0]Mechanism and Overlap Pipeline
In Ring Attention:
- Sequence N is divided into P blocks of size B_s = N/P. Each GPU i retains its local Query block Q_i throughout the entire layer.
- At step 0, GPU i initializes its key-value buffers with its local K_i, V_i.
- At each step k from 0 to P-1:
- Compute partial attention Softmax(Q_i * K_curr^T / sqrt(d)) * V_curr using online softmax scaling (tracking running maximum m_i and normalizer l_i).
- Asynchronously send K_curr, V_curr to rank (i+1) mod P and receive K_next, V_next from rank (i-1) mod P via non-blocking torch.distributed P2P calls.
- Wait for communication to finish, update key-value pointers, and update online softmax accumulators.
- After P steps, local queries have attended to all key-value blocks across the entire sequence.
Causal Masking Imbalance and Zigzag Mitigation
In standard causal language modeling, tokens at position i only attend to positions j <= i. Under naive sequential block assignment:
- GPU 0 (holding tokens 0 to N/P - 1) only computes attention on step 0 and remains idle for the remaining P-1 steps.
- GPU P-1 computes attention across all P steps.
- This creates a 50% theoretical computational bubble.
Zigzag Attention / Striped Attention: To balance causal workloads, tokens are assigned across GPUs in an interleaved zigzag pattern (e.g., GPU 0 gets blocks [0, 2P-1], GPU 1 gets [1, 2P-2], etc.). This ensures that every GPU processes an equal number of active causal query-key pairs across the ring steps, restoring computational balance to near 100% efficiency.
Strengths and Limitations
- Arbitrary Scaling: Ring Attention is not constrained by attention head count (H_Q or H_KV). It can scale to P = 64 or P = 512 even on models with single-head MQA.
- Computation-Communication Overlap: When local compute time T_compute exceeds P2P transfer time T_comm, communication latency is completely masked behind arithmetic execution.
- Small-Block Degradation: For shorter sequences or very high P, block size N/P becomes small (e.g. under 1024 tokens), degrading GPU Tensor Core utilization in FlashAttention and preventing complete communication overlap.
5. Unified Sequence Parallelism (USP / LoongTrain)
Presented by Fang et al. (2024) and implemented in frameworks like LoongTrain and NVIDIA Transformer Engine, Unified Sequence Parallelism (USP) combines DeepSpeed Ulysses and Ring Attention into a hierarchical 2D sequence parallel mesh.
┌──────────────────────────────────────────────┐
│ Global Sequence: N Tokens │
└──────────────────────┬───────────────────────┘
│
┌───────────────────────┴───────────────────────┐
▼ ▼
┌───────────────────────┐ ┌───────────────────────┐
│ Node 0 (Seq / P_ring) │ ◄── Ring P2P (IB) ──► │ Node 1 (Seq / P_ring) │
└───────────┬───────────┘ └───────────┬───────────┘
│ │
┌────────┴────────┐ ┌────────┴────────┐
▼ ▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐
│ GPU 0,0 │ │ GPU 0,1 │ │ GPU 1,0 │ │ GPU 1,1 │
└────┬────┘ └────┬────┘ └────┬────┘ └────┬────┘
▲ ▲ ▲ ▲
└─ All2All NVLink ┘ └─ All2All NVLink ┘
(P_ulysses = 2) (P_ulysses = 2)2D Sequence Parallel Mesh
USP decomposes the total sequence parallel group of size P = P_ulysses * P_ring:
- Intra-Node Group (P_ulysses): Placed within a single node connected via ultra-high-bandwidth NVLink (900 GB/s bidirectional per GPU on H100). Operates via DeepSpeed Ulysses All-to-All.
- Inter-Node Group (P_ring): Placed across nodes connected via InfiniBand or RoCE (400 Gbps / 50 GB/s per NIC). Operates via Ring Attention P2P communication.
Execution Workflow in USP
- Input sequence [B, N / (P_ulysses * P_ring), d_model] is projected to Q, K, V locally.
- Intra-Node All-to-All: Transforms token slices to head slices within each node. Each GPU now holds N / P_ring tokens for H / P_ulysses heads.
- Inter-Node Ring Attention: Executes ring passing of KV blocks across the P_ring dimension over InfiniBand, computing FlashAttention on H / P_ulysses heads per step while overlapping inter-node P2P transfers.
- Intra-Node All-to-All: Transposes heads back to token slices within the node.
Comparative Advantages
- Bypasses GQA Limits: If a model has 8 KV heads, P_ulysses is set to 8 (saturating intra-node NVLink), while P_ring can scale to 4, 8, 16, or 32 nodes, achieving total sequence parallel degrees of P = 32 to 256.
- Maximizes Hardware Bandwidth: Places dense collective All-to-All communication on 900 GB/s NVLink, while reserving low-contention point-to-point ring communication for the 50 GB/s inter-node network.
- Maintains Large Block Sizes: Because sequence partitioning is distributed across a 2D grid, attention compute kernels maintain large SRAM block tiles, preserving high Model FLOPs Utilization (MFU).
6. Architectural Comparison Across Key Dimensions
Megatron-SP
- Primary Communication Pattern: Reduce-Scatter and All-Gather.
- Hardware Topology: Intra-Node NVLink only.
- Head Count Constraint: Tied to Tensor Parallelism (TP <= 8).
- Causal Mask Handling: Native standard causal masking.
- Communication Overlap: Sequential with TP linear projections.
- Effective Context Limit: Approximately 32k tokens.
DeepSpeed Ulysses
- Primary Communication Pattern: All-to-All-v transposition.
- Hardware Topology: Optimized for Intra-Node NVLink (usable inter-node with high-bandwidth fabrics).
- Head Count Constraint: P must divide query heads and KV heads (P <= H_KV in GQA).
- Causal Mask Handling: Native standard causal masking on full sequence per head subset.
- Communication Overlap: Difficult to overlap All-to-All with local attention.
- Effective Context Limit: Up to 128k tokens per node.
Ring Attention (with Zigzag)
- Primary Communication Pattern: Asynchronous Point-to-Point (P2P) ring passing.
- Hardware Topology: Scales across Inter-Node InfiniBand and RoCE fabrics.
- Head Count Constraint: Completely unconstrained (P can exceed head counts, supports MQA).
- Causal Mask Handling: Requires Zigzag or Striped block scheduling to balance causal triangular loads.
- Communication Overlap: High degree of computation and communication overlap.
- Effective Context Limit: Scalable to 1M+ tokens.
Unified Sequence Parallelism (USP)
- Primary Communication Pattern: Hierarchical 2D: Intra-node All-to-All + Inter-node P2P ring.
- Hardware Topology: Multi-tier clusters (NVLink intra-node + InfiniBand inter-node).
- Head Count Constraint: P_ulysses <= H_KV; P_ring is unconstrained.
- Causal Mask Handling: Native causal masking within Ulysses groups combined with Zigzag inter-node schedules.
- Communication Overlap: Overlaps inter-node P2P transfers while leveraging NVLink All-to-All speed.
- Effective Context Limit: Scalable to 1M+ tokens with sustained high MFU.
7. Integration with 4D Parallelism and Production Serving
In large-scale production training clusters (such as training Llama-3-style 405B models or fine-tuning 70B models on 512k context), Sequence Parallelism operates as part of a 4D/5D parallel strategy:
Total GPUs = DP * PP * TP * CP * EP
Placement Ordering Rules
To minimize interconnect bottlenecks, parallelism dimensions must be mapped onto physical network hierarchy from highest bandwidth to lowest bandwidth:
- Tensor Parallelism (TP): Inner-most rank mapping. Strictly confined to intra-node NVLink (TP <= 8).
- Ulysses Sequence Parallelism (CP_ulysses): Mapped to intra-node NVLink alongside or in place of TP (TP * CP_ulysses <= 8).
- Pipeline Parallelism (PP): Mapped across adjacent nodes within a single network spine switch.
- Ring Context Parallelism (CP_ring): Mapped across cluster nodes over non-blocking InfiniBand rail-optimized fabrics.
- Data Parallelism (DP / ZeRO-3 / FSDP): Outer-most dimension, spanning the full cluster.
Context Parallelism in Production Inference
During inference (e.g., in engines like vLLM and TensorRT-LLM), context parallelism is leveraged primarily during the Prefill Phase of ultra-long prompts:
- In the prefill phase, computing attention over a 200,000-token prompt on a single GPU creates extreme Time-To-First-Token (TTFT) delays.
- Partitioning the prefill sequence across P GPUs via Ulysses or Ring Attention reduces TTFT by approximately P times.
- In the Decode Phase, memory bandwidth and KV cache routing dominate; serving engines transition from sequence-partitioned prefill to paged KV cache lookups or disaggregated decode workers.
8. Summary and Production Recommendations
- For Intra-Node Long Context (N <= 64k - 128k, 8 GPUs): DeepSpeed Ulysses provides the highest Model FLOPs Utilization (MFU) due to low NVLink All-to-All latency and direct compatibility with standard FlashAttention-2 kernels, provided H_KV >= 8.
- For Inter-Node Ultra-Long Context (N >= 256k - 1M): Unified Sequence Parallelism (USP) is the optimal architecture. It avoids the head-count scaling limit of Ulysses while avoiding the severe InfiniBand latency penalty of pure Ring Attention on small block sizes.
- For Causal Ring Attention Deployments: Always enforce Zigzag or Striped block assignment to eliminate the 50% idle compute bubble inherent in naive causal ring schedules.
Sources
- Jacobs, S. A., et al. (2023). DeepSpeed Ulysses: System Optimizations for Enabling High-Throughput Deep Learning on Large Models with Extremely Long Context. arXiv: 2309.14509
- Liu, H., et al. (2023). RingAttention with Blockwise Transformers for Near-Infinite Context. arXiv: 2310.01889
- Fang, J., et al. (2024). USP: A Unified Sequence Parallelism Approach for Long Context Generative AI. arXiv: 2405.07719
- Korthikanti, V. A., et al. (2022). Reducing Activation Recomputation in Large Transformer Models. arXiv: 2205.05198
- Dao, T. (2023). FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning. arXiv: 2307.08691
- NVIDIA Corporation. Transformer Engine Documentation: Context Parallelism and Sequence Parallelism APIs. NVIDIA Docs
- LoongTrain Framework. Hybrid Sequence Parallel Attention for Long-Context LLMs. GitHub: feifeibear/long-context-attention



