Search

Search papers and lessons

DeepSpeed Ulysses: System Optimizations for Extreme Long Sequence Training

Sam Ade Jacobs, Masahiro Tanaka, Chengming Zhang et al. · 2023

DeepSpeed-Ulysses (2023) splits a long sequence across GPUs and uses two all-to-all exchanges so that each GPU computes full-length attention for a few heads, keeping communication flat as the sequence grows.

On this page

  • Colour = topic mix
  • Systems
  • Discrete math
  • Probability
  • Calculus
  • Linear algebra
  • Neural network basics
  • ReadReady to readNot yet

TL;DR

DeepSpeed-Ulysses is a method for training Transformer language models on very long inputs, up to about a million tokens. It splits the input along the sequence dimension, so each GPU holds a slice of the tokens. Right before attention, one all-to-all exchange gives each GPU the full sequence for a subset of the attention heads, and a second exchange returns the results to the token slices. The paper reports up to 2.5 times the throughput of the previous best system and up to 4 times the sequence length, with communication that stays flat when the sequence and the number of GPUs grow together.

Builds on: Multi-Head Attention, Model Parallelism, All-to-All Communication · Leads to: long-context training stacks that combine sequence splitting with data parallelism and ZeRO-style state sharding

Why it mattered

Long inputs are now a practical need, not a research curiosity. The paper's motivating examples are a chat application that must keep a long history, summarisation of books of tens to hundreds of thousands of words, and biology, where a human genome has 6.4 billion letters and a model that reads genes as sequences needs long context. Clinical models conditioned on a whole patient record face the same issue.

Existing parallelism methods did not cover the sequence axis well. Data parallelism splits batches, tensor parallelism splits operators inside a layer, and pipeline parallelism splits layers. None of these shrinks the memory that one long sequence needs for its attention. The paper's contribution is a sequence-parallel method that keeps communication low enough to scale, and that works with the attention kernels people already use, such as FlashAttention, and with sparse attention.

The problem

Attention compares every token with every other token, so the work and the intermediate memory grow quickly with sequence length N. Splitting the sequence across P GPUs cuts the per-GPU token count, but attention needs every token's keys and values, so the GPUs must exchange data. The paper names two earlier ways to do this, and says both are inefficient in different ways.

The first, ring-style attention, passes key and value blocks around a ring of GPUs, which makes the communication grow with the message size. The second, the sequence parallelism inside Megatron-LM, splits the sequence but gathers and reduces the full activations at each layer, so the volume each link carries does not fall as more GPUs are added. Both also tie the method to one attention type or to one tensor-parallel code base.

The design goal the paper sets is that communication per link should fall as P grows, that any attention variant should work, and that the change to existing code should be small.

How it works

Take a tiny case: 2 GPUs, a sequence of 4 tokens, and 2 attention heads. GPU 0 holds tokens 1 and 2, and GPU 1 holds tokens 3 and 4. Each GPU projects its own tokens into queries, keys and values for both heads.

flowchart LR
  A["GPU 0: tokens 1-2, all heads"] -->|all-to-all| B["GPU 0: all tokens 1-4, head 1"]
  C["GPU 1: tokens 3-4, all heads"] -->|all-to-all| D["GPU 1: all tokens 1-4, head 2"]
  B --> E[Attention for head 1 over 4 tokens]
  D --> F[Attention for head 2 over 4 tokens]
  E -->|all-to-all| G["GPU 0: tokens 1-2, all heads"]
  F -->|all-to-all| H["GPU 1: tokens 3-4, all heads"]
  1. Partition by tokens. Each GPU keeps its own slice of the input, here tokens 1 and 2 on GPU 0 and tokens 3 and 4 on GPU 1. Projections such as the query, key and value matrices are computed locally, so no communication is needed yet.
  2. First all-to-all. Each GPU sends the parts of its queries, keys and values that belong to heads the other GPU will compute. GPU 0 keeps head 1 for its own tokens and sends head 2 for tokens 1 and 2 to GPU 1. GPU 1 does the same in reverse. Afterwards GPU 0 holds head 1 for all four tokens, and GPU 1 holds head 2 for all four.
  3. Attention per head. Each GPU runs ordinary attention for its head over the full sequence. The arithmetic is identical to single-GPU attention, because each head sees every token. Only the number of heads per GPU is reduced.
  4. Second all-to-all. The attention outputs go back to the layout by tokens. GPU 0 gets the output of both heads for tokens 1 and 2, and GPU 1 gets the output for tokens 3 and 4.
  5. Continue the layer locally. The feed-forward block and the other per-token operations run on each GPU's token slice again, with no further communication, until the next attention layer.
  6. Check the numbers. With two GPUs, each GPU sends half of its query, key and value data in the first exchange: the half that belongs to the other GPU's head.

The point of the method is step 6. Each all-to-all moves the share of the data that belongs to other GPUs, and the amount each link carries shrinks as the number of GPUs grows (see the math below).

The math

Definition: DeepSpeed-Ulysses sequence parallelism

Let the sequence have N tokens, the hidden size be h, the number of heads be H with per-head size dk=h/Hd_{k} = h / H, and the number of GPUs be P. Each GPU holds N/PN/P tokens. Two all-to-all exchanges re-layout the query, key and value tensors from a token split to a head split and back, so that attention for each head is computed over all N tokens on one GPU.

Step 1 in symbols: the token split

Let X∈RN×hX \in \mathbb{R}^{N \times h} be the input. GPU p holds the rows Xp∈R(N/P)×hX_{p} \in \mathbb{R}^{(N/P)\times h}. It computes its local queries, keys and values QpQ_{p}, KpK_{p} and VpV_{p}, each of size (N/P)×h(N/P) \times h. Because each row depends only on its own token, this step is local.

Step 2 in symbols: the head split

After the first all-to-all, GPU p holds Q(p)Q^{(p)}, K(p)K^{(p)} and V(p)V^{(p)} restricted to a subset of the heads. Write Hp\mathcal{H}_{p} for that subset, with ∣Hp∣=H/P|\mathcal{H}_{p}| = H / P heads, so that the subsets for different GPUs do not overlap. For each head ii in Hp\mathcal{H}_{p}, the attention output is

Attni=softmax ⁣(QiKi⊤dk)Vi,Qi,Ki,Vi∈RN×dk\mathrm{Attn}_{i} = \mathrm{softmax}\!\left(\frac{Q_{i} K_{i}^{\top}}{\sqrt{d_{k}}}\right) V_{i}, \qquad Q_{i}, K_{i}, V_{i} \in \mathbb{R}^{N \times d_{k}}

Each QiQ_{i} has all N rows, so the softmax runs over the full sequence. This is the same formula as single-GPU attention, applied to fewer heads.

Step 3 in symbols: the communication volume

The paper counts the data an all-to-all sends over one link as M/PM/P, where M is the aggregate message size. For one transformer layer, the first all-to-all carries the stacked query, key and value tensors, of aggregate size 3Nh3Nh, and the second carries the output, of size NhNh. The per-link total is

3NhP+NhP=4NhP\frac{3Nh}{P} + \frac{Nh}{P} = \frac{4Nh}{P}

Megatron-LM's sequence parallelism uses all-gather and reduce-scatter operations whose cost per link stays at the full message size M rather than M / P. The paper gives its per-link volume as 4Nh4Nh, which is P times the Ulysses figure. The ratio, P, is the reason the method scales.

Step 4 in symbols: the constant-volume property

If the sequence grows in proportion to the GPUs, so that N/PN/P stays fixed, then 4Nh/P=4h⋅(N/P)4Nh/P = 4h \cdot (N/P) is also fixed. That is the sense in which the paper says the communication volume stays constant when N and P increase together.

Worked examples

Example
1. Tiny: two GPUs, four tokens, two heads

GPU 0 holds tokens 1 and 2; GPU 1 holds tokens 3 and 4; there are 2 heads, so each GPU should end up with 1 head.

  1. Before the first exchange, GPU 0 has queries for tokens 1 and 2 for both heads: 2 tokens times 2 heads, that is 4 head-token blocks.
  2. GPU 0 keeps the blocks for head 1 (2 blocks) and sends the blocks for head 2 (2 blocks) to GPU 1. It sends half of its data.
  3. GPU 1 does the same in reverse. Now GPU 0 has head 1 for tokens 1 to 4, which is 4 blocks, and GPU 1 has head 2 for tokens 1 to 4.
  4. Each GPU runs attention on its one head over all four tokens. The output is 4 blocks on each GPU.
  5. The second exchange sends each GPU's outputs for the other GPU's tokens. Each GPU now holds the output of both heads for its own two tokens: 2 tokens times 2 heads, back to 4 blocks.
  6. Interpretation: each exchange moved half of the data on each GPU, and each GPU did full-length attention for its own head with no approximation. Adding more heads and GPUs would keep the same pattern.
Example
2. Standard: per-link communication against Megatron-LM

Take a sequence of N = 8,192 tokens, hidden size h = 4,096, and P = 8 GPUs. Count the values carried per link for one transformer layer, using the paper's expressions.

  1. Ulysses: 4Nh/P=4×8,192×4,096/84Nh / P = 4 \times 8{,}192 \times 4{,}096 / 8.
  2. Multiply the numbers: 4×8,192=32,7684 \times 8{,}192 = 32{,}768, and 32,768×4,096=134,217,72832{,}768 \times 4{,}096 = 134{,}217{,}728, and dividing by 8 gives 16,777,21616{,}777{,}216.
  3. Megatron-LM sequence parallelism: 4Nh=134,217,7284Nh = 134{,}217{,}728.
  4. Compare: 134,217,728/16,777,216=8134{,}217{,}728 / 16{,}777{,}216 = 8.
  5. Interpretation: the ratio equals P, so on 8 GPUs Megatron-LM's sequence parallelism carries 8 times the per-link volume. Doubling P doubles the gap, which is why the advantage grows with scale.
Example
3. Realistic: scaling a 7-billion-parameter model from 64 to 256 GPUs

The paper's Table 2 holds the sequence at 131,072 tokens and reports iteration time and throughput per GPU for a 7B dense GPT. The TFLOPs column is per GPU.

  1. Iteration time at 64 GPUs is 32,432.13, and at 128 GPUs it is 17,052.51. The speedup is 32,432.13/17,052.51≈1.9032{,}432.13 / 17{,}052.51 \approx 1.90.
  2. Ideal scaling from 64 to 128 GPUs is 2. The efficiency is 1.90/2=0.951.90 / 2 = 0.95, or 95%.
  3. From 128 to 256 GPUs the time goes to 9,886.70, a speedup of 17,052.51/9,886.70≈1.7217{,}052.51 / 9{,}886.70 \approx 1.72, or 86% of ideal.
  4. Throughput per GPU drops from 165.53 TFLOPs to 157.41 and then 136.09. Relative to 64 GPUs, that is 95% and 82%.
  5. Interpretation: the job got faster as GPUs were added, but per-GPU efficiency fell as communication grew with the number of GPUs.

Try it: Table 3 lists 147.4 TFLOPs per GPU at 262,144 tokens on 256 GPUs. Compared with 161.36 TFLOPs at 65,536 tokens on 64 GPUs, what fraction of per-GPU throughput is kept? Answer: 147.4/161.36≈0.914147.4 / 161.36 \approx 0.914, so about 91%. The sequence length went up 4 times with the GPUs, and most of the per-GPU throughput was kept.

Results

The paper reports these results from its own experiments. The experiments use A100 GPUs, with up to 256 of them.

  • Sequence length: the paper scales the sequence length up to 1 million tokens on a 1.2-billion-parameter GPT, with sequence length growing linearly with GPU count.
  • Dense attention, 7B and 30B GPT: against Megatron-LM's sequence parallelism on 32 and 64 A100 GPUs, DeepSpeed-Ulysses is faster at every sequence length both can run, and it can run longer sequences. The paper credits its memory savings from ZeRO-3 and its use of all-to-all rather than all-gather.
  • Sparse attention, 7B and 30B: more than 2 times the throughput of Megatron-LM, and sequences up to 4 times longer than Megatron-LM can run. The paper notes that its throughput here is limited by the local sparse-attention kernel, not by communication.
  • Scaling, fixed sequence of 131,072 tokens: per-GPU throughput of 165.53, 157.41 and 136.09 TFLOPs on 64, 128 and 256 GPUs.
  • Scaling, sequence growing with GPUs: 161.36 TFLOPs at 65,536 tokens on 64 GPUs, 157.41 at 131,072 on 128, and 147.4 at 262,144 on 256.
  • Convergence: a 1.3-billion-parameter GPT trained at 32K tokens on 8 A100 GPUs, with sequence-parallel degree 4, is compared with Megatron-LM's sequence parallelism and with several ZeRO stages, and the paper reports no loss of model quality from the method.
  • Headline: the introduction reports more than 10 times less communication than existing systems, up to 2.5 times the throughput, training on up to 4 times longer sequences, and sustained throughput above 175 TFLOPs per GPU (over 54 percent of hardware peak).

These numbers mean that the method pays off most for long sequences and large GPU counts, where the all-to-all volume is small per link. The limits are real: the work is evaluated on one GPU family and one model family, the sparse-attention results depend on the local kernel, and the paper's communication analysis is a volume count rather than a full timing model.

Reading guide

Read the paper in this order, with the lessons it depends on.

  1. Section 1, Introduction. Needs Transformer for the setting and Model Parallelism for the list of parallelism types. The two challenges in the introduction are the reason for the design.
  2. Section 2.1, Background. Needs Multi-Head Attention for the Q, K, V layout, Data Parallelism for the batch split, and Model Parallelism for tensor and pipeline parallelism.
  3. Section 2.2, Related Work and Table 1. Needs All-Reduce for the family of collective operations, including the all-gather and reduce-scatter that Megatron-LM's sequence parallelism uses, and ring attention for the ring alternative.
  4. Section 3.1, System Design. Needs All-to-All Communication. Figure 2 is the design in one picture: follow the token slices in and the head slices out.
  5. Section 3.2, Communication Analysis. Needs the volume counting from the step-by-step walkthrough above. Check the 4Nh/P4Nh/P and 4Nh4Nh expressions against the worked example.
  6. Section 3.3, Memory Efficiency. Needs ZeRO. The point is that sequence parallelism does not shrink model states, so ZeRO-3 is added.
  7. Section 3.4 (attention-agnostic design) and Section 4, Evaluation. Needs FlashAttention and Sparse Attention for the attention kernels. Read the tables against the Results section above.

Check yourself

QuestionIn the first all-to-all of DeepSpeed-Ulysses, what is exchanged, and what does each GPU hold afterwards?Show answer

The local query, key and value slices for each head are exchanged, so that each GPU goes from holding a slice of tokens for all heads to holding the full sequence for a subset of the heads. Afterwards each GPU can run attention for its own heads over all N tokens, with no approximation.

QuestionA layer has N = 8,192 tokens, h = 4,096 and P = 16 GPUs. Using the per-link expressions in the paper, how many times larger is Megatron-LM's per-link volume than Ulysses'?Show answer

The ratio is P, so the answer is 16. Ulysses carries 4Nh/P4Nh/P per link, which is 4×8,192×4,096/16=8,388,6084 \times 8{,}192 \times 4{,}096 / 16 = 8{,}388{,}608, and Megatron-LM carries 4Nh=134,217,7284Nh = 134{,}217{,}728. The ratio is 134,217,728/8,388,608=16134{,}217{,}728 / 8{,}388{,}608 = 16.

QuestionWhy does the paper say the Ulysses communication volume stays constant when the sequence length and the number of GPUs both double?Show answer

Because the per-link volume is 4Nh/P4Nh/P, and doubling both N and P leaves N/PN/P unchanged, so 4Nh/P4Nh/P is the same. Each GPU still holds the same number of tokens and sends the same share of its data, whatever the total sequence length.

QuestionYou want to train with 128K tokens on 48 GPUs, but the model has 32 attention heads. Can the head split described here work as written?Show answer

Not as written. The method gives each GPU a non-overlapping subset of the heads, so with 32 heads and 48 GPUs some GPUs would receive no head to compute. The degree of sequence parallelism would have to be at most the number of heads, for example 32 GPUs, or the work would need to be combined with another form of parallelism. This follows from the head split in the design, not from a figure in the paper.

Sources

  • Jacobs, Tanaka, Zhang, Zhang, Song, Rajbhandari, He. DeepSpeed Ulysses: System Optimizations for Enabling Training of Extreme Long Sequence Transformer Models. arXiv:2309.14509 (2023). arXiv abstract page, DOI 10.48550/arXiv.2309.14509.
  • Shoeybi, Patwary, Puri, LeGresley, Casper, Catanzaro. Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism. arXiv:1909.08053 (2019), the sequence-parallel baseline the paper compares against. arXiv abstract page.

Read the full paper

Sam Ade Jacobs, Masahiro Tanaka, Chengming Zhang et al., 2023. arXiv:2309.14509