AI State of the Art
MLOps/Infrastructure & Compute/31 min
Section 188

Advanced Parallelism: Composing TP, PP, DP, Context and Experts

Listen as a podcast →

What this page covers, and what it does not repeat

Every form of parallelism already has its own lesson in this course, and each answers the same question: what does the method split, and what does it communicate, when? This page does not re-explain them. It covers what belongs to none of them individually: their composition.

Because in a real training run you never use just one. You stack three, four or five, and the outcome depends as much on how you nest them as on which ones you pick.

Here is the recap, each row linking to the lesson that develops it:

MethodWhat it splitsWhat it communicatesIntensity
Tensor parallelism (TP)each weight matrix, inside a layerall-reduce of activations, several times per layervery high
Sequence parallelism (SP)the non-attention operations, along the sequenceall-gather and reduce-scatter, alongside TPhigh
Pipeline parallelism (PP)the layers, into successive stagesactivations, point to point, at the seamslow
Data parallelism (DP)the batch of examplesall-reduce of gradients, once per stepmedium
ZeRO and FSDPparameters, gradients and optimizer statesall-gather before each layer, reduce-scatter afterhigh
Ring Attentionthe sequence, for attention itselfK and V blocks circulating in a ringdepends on length
Mixture of experts (EP)the experts of an MoE modelall-to-all, to route tokenshigh and irregular

The right-hand column governs everything that follows. Keep it in mind.

The single principle: matching two hierarchies

All of composition rests on one idea, and it is simple.

On one side, the methods above form a hierarchy of communication intensity: TP exchanges enormously, PP almost nothing.

On the other, the hardware forms a hierarchy of bandwidth, established in Networking for distributed training: inside a server, NVLink offers 900 GB/s; between servers, InfiniBand NDR offers 50 GB/s. A ratio of 18.

The composition rule, in one sentence

Put the method that communicates most on the fastest link.

Everything else is application. TP, which exchanges several times per layer, must stay inside a server. PP, which exchanges only at stage boundaries, tolerates crossing the network perfectly well. So does DP, which exchanges once per step.

A configuration that respects this rule and one that ignores it can have exactly the same parallelism degrees and differ by a factor of three in real speed. That is the subject of this page.

The device mesh: how a composition is expressed

To compose, you need something that says which GPU belongs to which group. That is the device mesh: the GPUs are arranged in a multi-dimensional array, and each dimension carries one form of parallelism.

Take 32 GPUs, that is 4 servers of 8, and a two-dimensional mesh written (pp=4, tp=8). Each GPU then gets a pair of coordinates:

  • the GPU at (1, 5) is in pipeline stage 1 and holds shard 5 of the weight matrices;
  • its TP partners are the seven GPUs sharing its first coordinate: (1, 0) through (1, 7);
  • its PP partners are the three sharing its second: (0, 5), (2, 5), (3, 5).

A communication only ever happens within a group, that is, between GPUs differing in a single coordinate.

And here is why the order matters

GPUs are numbered 0 to 31 in the hardware, with numbers 0 to 7 in the first server, 8 to 15 in the second, and so on. The mesh must therefore decide which dimension varies fastest as you walk that numbering.

With the mesh (pp=4, tp=8), TP varies fastest. The eight TP partners carry numbers 8 to 15: they are in the same server, and their exchanges travel over NVLink. The PP partners are spaced 8 apart, hence in different servers, which suits them perfectly, since they only exchange at stage boundaries.

Now invert it, with the mesh (tp=8, pp=4). The degrees are identical, the GPU count is identical. But the TP partners are now spaced 4 apart and spread across all four servers. The most intense exchanges in the entire run, the ones repeating several times per layer, now cross the network instead of NVLink: 18 times slower.

The same computation, three times slower

This is the most important point on the page, and it always surprises. Two configurations with the same methods, the same degrees, the same model, the same card count, and a large performance gap, caused solely by the order of the dimensions in the mesh.

Nothing in the model code changes. Nothing in the logs flags the mistake: the run works, produces correct results, and is merely slow. It is a class of problem you find only by measuring, never by re-reading.

Hence the practical rule: the most communication-heavy dimension must be the fastest-varying one, the one grouping physically adjacent GPUs.

HSDP: the same method at two levels

The most instructive case of composition does not mix two different methods: it applies one method at one level and another at the level above.

The problem arises from FSDP at scale. FSDP shards parameters across all GPUs, and must reassemble them with an all-gather before each layer. Across 8 GPUs in one server, that exchange runs over NVLink and costs little. Across 256 GPUs in 32 servers, the same exchange crosses the network, and it happens at every layer. The cost becomes prohibitive.

HSDP (Hybrid Sharded Data Parallelism) resolves this by separating the two levels:

  • inside a server: full FSDP. The 8 GPUs share the parameters, and the frequent all-gathers stay on NVLink;
  • between servers: plain replication, as in classic data parallelism. Each server holds its own complete copy of the model, sharded across its 8 cards. The only exchange crossing the network is the all-reduce of gradients, once per step.
Diagram on a black background. At the top, two diamonds Data shard n and Data shard n plus 1 each feed a Batches box. Each data shard splits into two vertical columns outlined in red, labelled Model shard n and Model shard n plus 1, four columns in all. Each column chains All-Gather, Local Forward Pass, Release Full Weights, All Gather, Local Backward Pass, Release Full Weights, Reduce-Scatter, All-Reduce then Local Weights Update. Horizontal dashed green arrows, annotated All-Gather Weights and Reduce scatter across shard groups, join the two columns of the same data shard. Two crossed blue arrows at the bottom, annotated All reduce across replicate groups, join columns belonging to the two different data shards.
The two levels of HSDP on a four-GPU example. The horizontal dashed green arrows are the FSDP exchanges within a sharding group: those are the ones that stay inside the node, on NVLink. The blue arrows at the bottom are the single all-reduce that crosses the network, once per step, between replicated groups.Source : Hugging Face, Accelerating ND-Parallel: A Guide to Efficient Multi-GPU Training

The arithmetic is direct. On 256 GPUs in 32 servers of 8, the mesh is (replicate=32, shard=8), and 32×8=25632 \times 8 = 256. Each card stores only an eighth of the model states, while sending just one all-reduce per step over the network.

The teams analogy

Several teams work on the same project. Within a team, in one office, each person keeps only part of the documents and the others ask out loud when they need them, that is FSDP over NVLink, frequent and cheap.

Between offices, raw documents do not circulate. You send a summary of conclusions at the end of the meeting, that is the gradient all-reduce, once per step.

HSDP's trade-off is visible in the analogy: each office must have room for the full set of documents, since it holds a complete copy. You trade memory savings for network savings.

When to switch: on a single server, plain FSDP is enough, everything already being on NVLink. From a few servers up, HSDP starts to pay, and beyond eight it becomes hard to do without. The switch is decided by measurement, never a priori: if the time spent in cross-server all-gathers becomes visible in a performance trace, the answer is HSDP.

Context parallelism: one more dimension

As sequences grow longer, an extra dimension appears, cutting along the length of the sequence itself.

The problem is attention's quadratic cost. A sequence of nn tokens produces an n×nn \times n score matrix: doubling the length quadruples the area. For 128,000 tokens:

128,0002≈1.64×1010 elements, that is ≈33 GB in BF16128{,}000^2 \approx 1.64 \times 10^{10} \text{ elements}, \text{ that is } \approx 33 \text{ GB in BF16}

at 2 bytes per element, and that is for a single attention head in a single layer. With dozens of heads and dozens of layers, no card suffices.

FlashAttention, described in GPU architecture, avoids writing that matrix to main memory and so removes this particular cost. But the K and V tensors do still have to be stored, and their size grows linearly with length. Beyond a certain point, the sequence must be spread across cards.

That is context parallelism: each card takes a segment of the sequence. The mechanism that nonetheless lets each token "see" the whole sequence (K and V blocks circulating in a ring) is explained in Ring Attention and is not repeated here.

Do not confuse sequence parallelism with context parallelism

Both cut along the sequence, and the names are similar, but they solve different problems.

Sequence parallelism distributes the non-attention operations (normalisations, dropout) that tensor parallelism left duplicated on every card. It complements TP and lives alongside it. See Sequence parallelism.

Context parallelism distributes the attention computation itself. It targets the memory cost of very long sequences.

The two can coexist in one configuration, on different mesh dimensions.

Causal imbalance, and its fix

A detail specific to composition, which appears in no individual lesson. In a model that generates text, attention is causal: each token looks only at those before it.

The consequence on a split sequence: the card holding the start of the sequence has almost nothing to compute, since its tokens have few predecessors. The card holding the end must compare its tokens against the whole sequence. The load is therefore very uneven, and since all cards synchronise at every step, the group advances at the speed of the busiest.

The fix is called zigzag sharding: instead of giving each card one contiguous segment, you give it two symmetric pieces, one from the start and one from the end. Card 0 takes the first and last chunk, card 1 the second and second-to-last, and so on. Each then receives a mix of easy and hard tokens, and the load evens out.

The arithmetic of a full composition

All these dimensions multiply. The total card count is the product of the degrees:

Ntotal=TP×PP×CP×DPN_{\text{total}} = \text{TP} \times \text{PP} \times \text{CP} \times \text{DP}

Where each factor is that dimension's degree, the number of cards across which the method spreads its work.

Let's check on an example with 512 cards, that is 64 servers of 8:

  • TP = 8: tensor parallelism occupies exactly one server, which is the placement rule stated above;
  • PP = 8: eight servers form a pipeline chain. One complete copy of the model therefore occupies 8×8=648 \times 8 = 64 cards;
  • DP = 8: that leaves 512÷64=8512 \div 64 = 8 model copies to feed with data.

Check: 8×8×8=5128 \times 8 \times 8 = 512. ✓

And if you want HSDP on the data dimension, you subdivide it in turn: replicate = 4 and shard = 2 do give 4×2=84 \times 2 = 8.

The check that catches errors

This product is the most useful verification you can run on a parallelism configuration, and it catches inconsistencies in seconds.

The product of the degrees must equal the card count exactly. If it does not, the configuration is wrong: either a dimension was forgotten, or two dimensions overlap. A configuration that announces four dimensions but multiplies only three describes a fleet four or eight times larger than the one it is talking about.

Run this check on any configuration you are shown, published articles included.

Diagram titled Transformer Layer with TP plus CP. Four horizontal lines labelled GPU0 to GPU3. GPU0 and GPU1 start from a vertical red block seq chunk 0, GPU2 and GPU3 from a red seq chunk 1 block. Each GPU pair then goes through the same steps left to right: a yellow AG slash RS column, then QKV, a dark blue AG slash RS column, Attn, a slash AG column, Attn out, a yellow RS slash AG column, Dropout, LN, a yellow AG slash RS column, then FC1, GeLU, FC2 and a final yellow RS slash AG column. GPU0 and GPU2 blocks are light green, GPU1 and GPU3 light blue, marking the two halves of Tensor Parallelism.
Two dimensions superimposed on a single Transformer layer, with 4 GPUs. Vertically, tensor parallelism: GPU0 and GPU1 share the weight matrices (different colours on the same QKV, FC1, FC2 blocks). Horizontally, context parallelism: the top pair handles sequence chunk 0, the bottom pair chunk 1. The yellow and blue columns are the communication points (AG for all-gather, RS for reduce-scatter).Source : NVIDIA Technical Blog, Scaling to Millions of Tokens with Efficient Long-Context LLM Training

This figure shows two dimensions superimposed on a single layer. Vertically, two cards share the weight matrices; horizontally, two pairs handle two sequence segments. The coloured columns are the communication points, and there are several per layer, which restates why those dimensions must stay on the fast link.

The order in which you decide

In practice you do not choose all five degrees at once. You fix them in an order imposed by the constraints, from most rigid to most flexible:

  1. Tensor parallelism first, sized to a server. It is the most communication-heavy method, so its placement is not negotiable.
  2. Context parallelism next, if sequence length demands it. That is a memory constraint, not a performance tuning knob: either the sequence fits or it does not.
  3. Pipeline parallelism to fit the model into the fleet when TP and CP are not enough. It tolerates the network well, but introduces idle time at the ends of the chain, see Pipeline parallelism.
  4. Data parallelism last, with whatever cards remain. It converts leftover hardware into throughput, and it is the one degree you can adjust freely once the others are set.
  5. Expert parallelism, for a mixture-of-experts model, composes with the rest but imposes its own all-to-all exchange, highly irregular since it depends on token routing, see MoE principles.

The order of this list is not a preference ranking: it is an order of decreasing constraint. The first items are non-negotiable; the last absorbs the remainder.

At inference time: data-parallel attention, distributed experts, and the expert load balancer

To serve a large mixture-of-experts model, DeepSeek has combined since V3 data-parallel attention (each GPU has its own copy of the attention and handles its own requests) with expert-parallel experts (the experts are spread over dozens of GPUs, and each token travels to the ones it needs). The weak point is balance: a heavily requested expert overloads the GPU that hosts it, and the whole system waits for that GPU. EPLB (Expert Parallelism Load Balancer, released by DeepSeek in February 2025) duplicates the busiest experts and spreads the copies to even out the work. In March 2025, DeepSeek described 32 redundant experts, with experts spread over 32 GPUs for prefill and 144 GPUs for decode. The DeepInfer engine provides the production implementation.

What to take away

Three ideas, and they are enough to read any training configuration.

The methods are not competitors, they are complements. Each cuts a different axis, the matrix, the layer, the sequence, the batch, the expert. You stack them, and the card count is the product of the degrees.

Placement matters as much as choice. Matching the hierarchy of communication intensity to the hardware's hierarchy of bandwidth is the only rule. Two configurations identical on paper can differ by a factor of three depending on the order of dimensions in the mesh.

All of it serves one goal: maximising the share of time spent computing rather than waiting, the utilization measure introduced in NVIDIA H100, B200, GB200. A well-composed training run is not the one using the most techniques, it is the one whose cards wait the least.

Check your understanding0 / 5

1. Two teams launch the same training run: same methods, same degrees, same card count. One is three times faster. What could differ?

2. What does HSDP change compared with FSDP applied uniformly across 256 cards in 32 servers?

3. A configuration claims TP = 8, PP = 8, CP = 2 and DP = 8 on a 512-card fleet. What should you make of it?

4. A sequence is split into contiguous segments across several cards, with causal attention. What happens?

5. Sequence parallelism and context parallelism both split along the sequence. What separates them?