Context Parallelism
Ring Attention and Ulysses β splitting long-sequence attention across GPUs when the sequence is too long for one GPU's memory.
Context parallelism (CP) splits the sequence dimension of attention across GPUs β enabling sequences too long for a single GPUβs KV cache memory. While TP splits the model and DP splits the batch, CP splits the context.
1. The Problem: Long Sequences Break Single-GPU Memory
For a 7B model with MHA serving 128K context: KV cache = 64 GB. Thatβs nearly all of an H100βs memory β no room for the model weights.
For training with full attention: the attention matrix is billion elements Γ 2 bytes = 32 GB per head. With 32 heads: 1 TB. Impossible on any single GPU.
Context parallelism distributes this across GPUs: each GPU handles tokens of the sequence.
Ring Attention uses the same online softmax technique as FlashAttention to merge partial attention results across tiles.
2. Ring Attention
How It Works
Split the sequence into chunks, one per GPU. Each GPU has Q for its chunk. K and V are rotated through the ring:
Step 0: GPU i computes attention(Q_i, K_i, V_i) β local KV
Step 1: Rotate K,V: GPU i receives K_{i-1}, V_{i-1} from left neighbor
GPU i computes attention(Q_i, K_{i-1}, V_{i-1})
Step 2: Rotate again: GPU i receives K_{i-2}, V_{i-2}
...
Step P-1: All KV chunks have rotated through all GPUs
After P-1 rotations, each GPU has computed attention against all KV positions β using online softmax to merge partial results (same technique as FlashAttention).
Communication
Each step: send tokens of K and V to the next GPU.
- Per step: bytes (K + V, BF16)
- Total: bytes
Key: Communication of K/V rotation can be overlapped with the attention computation of the current step. While GPU computes attention with , itβs simultaneously receiving for the next step.
Memory per GPU
- Q: tokens (local only)
- K, V: tokens (current rotation β only one chunk at a time)
- Attention: per head (within each tile)
Total attention memory: per head β quadratically reduced by CP degree.
Worked Example
7B model, MHA (32 heads, dk=128), S=128K, CP=4:
- Without CP: KV cache per request = 128K Γ 512 KB/token = 64 GB (doesnβt fit on one H100)
- With CP=4: each GPU handles S/4 = 32K tokens β KV cache per GPU = 32K Γ 512 KB = 16 GB β
- Attention memory per GPU: elements Γ 32 heads Γ 2 bytes = 2 GB (vs 128 GB without CP)
- Communication per layer: rotating 32K Γ 128 Γ 2 bytes Γ 2 (K+V) = 16 MB per step, 3 steps = 48 MB
- On NVLink (450 GB/s): 48 MB / 450 GB/s = 0.1 ms per layer β easily overlapped with attention compute
3. Ulysses (DeepSpeed)
An alternative to Ring Attention that uses All-to-All instead of rotation:
Step 1: Each GPU has Q_i, K_i, V_i for its S/P tokens
Step 2: All-to-All on K and V: redistribute by head instead of by sequence
β Each GPU now has full S tokens but only N_h/P heads
Step 3: Each GPU computes full attention for its heads (all S tokens)
Step 4: All-to-All on output: redistribute back by sequence
Ring Attention vs Ulysses
| Ring Attention | Ulysses | |
|---|---|---|
| Communication | Point-to-point rotation (P-1 steps) | Two All-to-All operations |
| Overlap potential | Excellent (rotate while computing) | Limited (All-to-All is blocking) |
| Memory per GPU | attention | attention (full sequence, fewer heads) |
| Best for | Very long sequences, limited NVLink | Moderate sequences, fast All-to-All |
| Used by | Llama 3.1, various | DeepSpeed |
4. When to Use Context Parallelism
| Condition | Use CP? |
|---|---|
| Sequence β€ 8K | No β single GPU handles it |
| Sequence 8K-32K | Maybe β if KV cache is tight |
| Sequence 32K-128K | Yes β single GPU canβt hold KV cache + model |
| Sequence > 128K | Yes β CP=4-8 typical |
CP is typically the innermost parallelism dimension (within a TP group), because it has the highest communication frequency (every attention layer).
Composition: DP Γ TP Γ CP. Example: 64 GPUs = DP=8 Γ TP=4 Γ CP=2.
Self-Check
- Explain why long sequences break single-GPU memory (calculate KV cache for 128K)
- Describe Ring Attentionβs rotation mechanism and how it uses online softmax
- Explain why Ring Attention communication can overlap with attention compute
- Compare Ring Attention vs Ulysses (communication pattern, memory, best use case)
- Explain how CP composes with TP and DP