skip to content

For a 70B decoder's 128K-token training phase, how do DeepSpeed-Ulysses and Megatron-Core context parallelism split attention, and what caps each one's degree?

level: seniorimportance: nice to knowfreq 3%

answer

  1. both split the sequence
  2. heads versus key-value chunks
  3. all-to-all before and after attention
  4. a ring of KV exchanges
  5. --cp-comm-type defaults to p2p

basics

~20 s

DeepSpeed-Ulysses all-to-alls Q, K and V from sequence shards to head shards, so key-value heads cap its degree. Megatron-Core context parallelism keeps sequence shards and passes KV chunks around a ring, capped by overlap and by sequence length divisibility.

solid answer

~50 s

Both split one sequence across a group of GPUs; they differ in how attention, which needs every token's keys and values, gets them. DeepSpeed-Ulysses wraps the attention module in `DistributedAttention`: an all-to-all turns sequence shards of Q, K and V into head shards with the full sequence, attention runs locally, and a second all-to-all turns the output back. Each rank needs at least one key-value head, so with 8 KV heads the degree tops out at 8. Megatron-Core context parallelism (`--context-parallel-size`) keeps every activation sequence-sharded and, with the default `--cp-comm-type p2p`, passes KV chunks around a ring while attention computes; it is not capped by heads, but the sequence length must be a multiple of 2 × CP and each chunk must carry enough compute to hide the exchange. CP is part of Megatron's dense grid, so it divides data parallelism.

go deeper

for a junior

Recall that both schemes split a long sequence across GPUs and that attention is the one layer that needs tokens from other ranks.

for a middle

Explain what each scheme sends — all-to-all of Q, K, V and output for Ulysses, a ring of key-value chunks for context parallelism — and where each runs attention.

for a senior

Pick a degree and communication type for a stated model and layout: count heads left after tensor parallelism, check sequence divisibility, and redo the data-parallel arithmetic.

for a principal

Weigh a long-context roadmap against model shape: key-value head count, tensor-parallel size and node size together decide which scheme scales, and they are cheaper to set before pretraining than after.

## Two ways to split one long sequence At 128K tokens a single sequence's activations no longer fit on one GPU even after tensor parallelism. **Sequence-splitting** schemes give each GPU in a group a slice of the tokens. Most layers — linear projections, norms, the MLP — treat tokens independently, so slices are enough. Attention is the exception: every query must see the keys and values of the tokens before it. The two engines solve that differently. ## DeepSpeed-Ulysses: switch from sequence shards to head shards - `deepspeed.sequence.layer.DistributedAttention(local_attention, sequence_process_group)` wraps the existing attention module; the rest of the model is unchanged. - Before attention, an **all-to-all** re-partitions Q, K and V: each rank gives up its slice of the sequence for all heads and receives the **full sequence for a subset of heads**. - Attention runs locally with whatever kernel the model already uses. - A second all-to-all returns the output to sequence shards. - DeepSpeed's analysis puts the per-link volume at 4Nh/P per layer (N tokens, hidden size h, degree P): constant when sequence length and degree grow together. - **The cap is heads.** DeepSpeed 0.19.7 asserts that the number of key-value heads is at least the sequence-parallel size, so every rank owns at least one head; uneven head counts are handled through `num_kv_heads`. A model with 8 KV heads cannot go beyond degree 8. - DeepSpeed counts the sequence-parallel group out of the data-parallel size: the batch arithmetic uses world size ÷ sequence-parallel size. ## Megatron-Core context parallelism: keep sequence shards, move key-value chunks - `--context-parallel-size` splits the input and **all** activations along the sequence; non-attention modules run unchanged. - `--cp-comm-type` chooses how attention gets the other ranks' keys and values. It defaults to `p2p`: KV chunks travel around a ring with asynchronous point-to-point sends that overlap attention compute. `allgather` gathers the full KV first and cannot overlap. `a2a` scatters heads across the CP group, like Ulysses. `a2a+p2p` is hierarchical — all-to-all inside a low level such as NVLink, ring across a high level such as InfiniBand — with sizes given by `--hierarchical-context-parallel-sizes`. - The sequence length must be a multiple of 2 × CP, because each rank takes two chunks so causal masking stays load-balanced. - In `p2p` mode heads do not cap the degree; the practical limit is that each rank's slice must hold enough attention work to hide the KV exchange. - CP is a factor of the dense grid: data parallel = world size ÷ (TP × PP × CP). ## Side by side | | DeepSpeed-Ulysses | Megatron-Core CP (`p2p`) | |---|---|---| | What moves | Q, K, V and output, by all-to-all | key-value chunks, point to point in a ring | | Attention runs on | full sequence, subset of heads | own query slice, every key-value chunk in turn | | Degree capped by | key-value heads (one per rank at least) | sequence divisibility and compute-communication overlap | | Overlap | all-to-all before and after attention | exchange overlaps attention compute | | Model change | wrap attention in `DistributedAttention` | flag on Megatron-Core's own GPT path | ## Choosing for the 128K phase 1. On the Megatron-Core layout with TP 8, 64 query heads and 8 KV heads, tensor parallelism already leaves one KV head per rank. That leaves no head for a head-scattering all-to-all to split further, so the ring is the natural option: `--context-parallel-size 4` with `--cp-comm-type p2p` gives 32K tokens per rank, 131,072 is a multiple of 2 × 4, and DP = 256 ÷ (8 × 4 × 4) = 2. 2. In a DeepSpeed ZeRO-3 run without tensor parallelism, Ulysses with degree 8 inside a node gives each GPU one KV head and 16K tokens before the all-to-all, keeps both all-to-alls on NVLink, and leaves 256 ÷ 8 = 32 data-parallel ranks. 3. When heads run out but the ring alone cannot hide its exchange, `a2a+p2p` puts the all-to-all where bandwidth is high and the ring where it is not. ## Mistakes interviewers listen for - Claiming Ulysses scales to any number of GPUs. - Calling context parallelism the same thing as Megatron's sequence parallelism, which splits only LayerNorm and dropout activations inside the TP group. - Forgetting that CP divides data parallelism. - Choosing `allgather` and expecting overlap.

  • Why does Megatron-Core require the sequence length to be a multiple of 2 × CP?
    Argument validation rejects a `--seq-length` that is not a multiple of 2 × `--context-parallel-size`. The factor of two serves load balance: under a causal mask late tokens attend to far more keys than early ones, so the sequence is cut into twice as many chunks as CP ranks and each rank holds a mix of early and late tokens instead of one contiguous slice.
  • When would `--cp-comm-type a2a+p2p` beat plain `p2p` in Megatron-Core?
    When the CP group spans nodes and a long ring would expose its slower inter-node hops. `a2a+p2p` runs a head-scattering all-to-all inside the fast level, for example the GPUs of one node, and a ring only across the slow level, with the two sizes set by `--hierarchical-context-parallel-sizes`. It still needs spare heads inside the all-to-all level.

  • Ulysses can use any degree regardless of the model's attention heads
  • Megatron's sequence parallelism and context parallelism split the same activations
  • Context parallelism leaves the data-parallel size unchanged
  • Megatron-Core's --cp-comm-type defaults to a2a
  • The allgather CP mode overlaps its exchange with attention compute

Assumes DeepSpeed 0.19.7 (DistributedAttention's key-value head check and uneven-head support) and Megatron-Core 0.19.2 (--cp-comm-type defaulting to p2p, four communication types).