Sequence parallelism

Even with Flash Attention and RoPE, physical hardware limits persist. An H100 GPU has 80 GB of memory. A 70B parameter model uses ~140 GB just for weights (FP16/BF16), and even with GQA (8 KV heads, as in Llama 3 70B), the KV cache for a 1 million token context adds another ~328 GB (or ~344 GB for 2202^{20} tokens). No single GPU can hold this.

To handle 1M+ token contexts, the sequence itself must be distributed across multiple GPUs — known as Sequence Parallelism (or Context Parallelism).

DeepSpeed Ulysses

  • 2023 — DeepSpeed Ulysses: System Optimizations for Enabling Training of Extreme Long Sequence Transformer Models
  • Partitions the sequence along the Attention Head dimension.
    • If a model has 64 heads and we have 8 GPUs, Ulysses ensures each GPU computes full attention for 8 specific heads over the entire sequence.
    • Requires an "All-to-All" transpose operation: before attention, GPUs exchange tokens so each GPU has the full sequence for its subset of heads; after attention, they transpose back.
    • Parallelism is hard-capped by the number of attention heads — and under GQA (e.g. 64 query heads with only 8 KV heads), standard Ulysses is effectively capped at the number of KV heads (8 GPUs) unless KV heads are replicated across devices. For extremely long sequences, the memory per GPU might still be too high even with maximum splitting.

Ring Attention

  • 2023 — Ring Attention with Blockwise Transformers for Near-Infinite Context
  • Partitions the sequence across any number of devices, arranged in a conceptual ring:
    1. The sequence Q,K,VQ, K, V is split into chunks. GPU 1 gets chunk 1, GPU 2 gets chunk 2, etc.
    2. Each GPU computes attention between its local Query chunk and its local KV chunk.
    3. GPU 1 sends its KV chunk to GPU 2, GPU 2 sends its KV chunk to GPU 3, ..., GPU NN sends to GPU 1.
    4. While the KV chunks are travelling over the NVLink/InfiniBand interconnect, the GPUs are simultaneously computing attention with the KV chunk they currently hold.
    5. This repeats NN times until every Query has attended to every Key in the distributed system.
  • Allows context length to scale linearly with the number of GPUs.
  • The only cost is the latency of the ring pass, which is effectively hidden by the compute-heavy attention calculation.
  • Meta's Context Parallelism for Scalable Million-Token Inference (2024) refined distributed context parallelism for production inference, achieving 1 million token pre-fill with Llama 3 405B in 77 seconds on 128 H100s at 93% efficiency.

LongNet

  • 2023 — LongNet: Scaling Transformers to 1,000,000,000 Tokens
  • Introduced Dilated Attention, an exponentially dilated sparse attention mechanism designed as a distributed sequence-parallel trainer across multiple GPUs (demonstrated up to 1 billion tokens in synthetic scaling experiments), taking advantage of constant-size communication per device.

Tags: AI