Skip to content

1.5. Data parallelism and FSDP

1.5.1. Where we are

This page serves step 5 (classify) of the pre-training workflow when the first divergence points to a gradient that several ranks sum together. It also serves step 2 (control) for any run that uses data parallelism. The training step is already pinned on one rank; what remains is the sum across ranks. DDP all-reduces the gradient in buckets, FSDP and ZeRO reduce-scatter a flattened copy of it, and both reduce a gradient norm and a mean loss across ranks. Each of those sums has an order, and that order can depend on things that have nothing to do with the parameter itself.

1.5.2. The idea: the position in the message decides the order, and the degree decides which sums exist

A small example about position. Four ranks hold the gradients g0, g1, g2 and g3 of one parameter. A ring all-reduce cuts the message into chunks, and the sum for each chunk starts at a different rank:

Element in the chunk Order of the sum (one example)
0 ((g1 + g2) + g3) + g0
2 ((g3 + g0) + g1) + g2

If the same parameter sits at another offset in the message (because of another bucket size, another parameter order, or one padding element in front of it), its elements land in other chunks, so they are summed in another order and get other bits. DDP even changes its own packing: its first iteration puts every gradient in one bucket, later iterations rebuild the buckets, and every restart starts over. Reduce-scatter has the same property through the rank that owns a slice: the slice a rank keeps is summed starting at the next rank, so resharding moves the order.

A small example about degree. Take 16 sequences. At data-parallel degree d = 2, each rank runs a backward pass over 8 sequences, and the two partial gradients are added. At d = 4, four partial gradients of 4 sequences each are added. These are different sums, not the same sum in another order, and no collective setting can make them equal.

16 sequences at data-parallel degreed = 2d = 4
one backward per rank
rank 0rank 1
rank 0rank 1rank 2rank 3
0123456789101112131415
Σ 0–7Σ 8–15
Σ 0–3Σ 4–7Σ 8–11Σ 12–15
x₂
x₄ ≠ x₂
logical shards, at any d
rank 0rank 1
rank 0rank 1rank 2rank 3
0123456789101112131415
++++++++
++++
++
x
  1. One global batch holds 16 sequences.
  2. At d = 2, each rank owns 8 sequences. With one backward per rank, each rank produces one partial sum. With logical shards, each rank sums its own subtree of one fixed tree.
  3. The ranks then add their partial sums, which gives x₂. The logical shards finish the same fixed tree, which gives x.
  4. At d = 4, each rank owns 4 sequences. With one backward per rank there are now four partial sums, so the sums themselves are different. With logical shards it is the same tree, and each rank simply holds a smaller subtree.
  5. With one backward per rank, x₄ ≠ x₂: the result follows the degree. With logical shards, the tree is the same, and so are the bits of x.
Figure: with one backward per rank, the partial sums at d = 2 and d = 4 depend on the degree. With logical shards, the same tree is summed at every degree.

Rule. The order of a sum across ranks must depend only on the element and on a fixed logical split of the batch, never on the element's position in the message, on which rank owns it, or on the degree. This is part 0's principle at L5: the parallel degree may change who computes a partial sum, but not which partial sums exist.

1.5.3. The step

Fix the order of every sum across ranks

A parameter's gradient, the gradient norm, the loss and the updated weights should not depend on the bucketing, on the sharding layout or on the degree. Pick the options your run needs. The code is in training_dp.py (the ordered collectives, the DDP comm hook Worker.ordered_hook, logical shards and the logical norm), and every comparison is made bit for bit with bi_check.py.

Option What it gives Cost class When to choose it
NCCL_ALGO=Tree for all-reduce One gradient per parameter whatever the bucketing, at a fixed degree small to moderate for large messages, large for small ones DDP at a fixed degree; the quickest fix
An ordered all-reduce (an all-to-all, then a sum in rank order in fp32, then an all-gather; installed as a DDP comm hook) The same, and independent of NCCL's algorithm small to moderate on one node You need it to hold under any NCCL setting
An ordered reduce-scatter (rank r receives slice r of every message and adds the d slices in rank order) FSDP-style gradients that do not depend on padding, wrapping or resharding moderate (under 2x) on one node; across nodes, use the two-level route Sharded training; any change of layout
Logical shards: S fixed micro-batches, one backward pass for each, and one pairwise tree over the shard index that also covers the sum across ranks Identical gradients, weights and Adam state at every degree; elastic resume moderate with several sequences per shard; large when the shards are tiny The degree changes, or runs at two different degrees must agree
A logical norm and loss: fixed blocks, sums of squares in fp64, and a pairwise tree; the loss as a tree over the shard losses A clipping norm and a loss that depend only on the values moderate for the norm itself; small within a whole step Always, when you use logical shards
Single-tensor AdamW (foreach=False, fused=False) An elementwise update, so replicated and sharded weights get the same bits small Always; pin one code path

Logical shards need the training step to be pinned first: each shard's backward pass must repeat itself on every rank, which with stock operators needs strict deterministic mode. Across two nodes, route the exchange within each node first (the two-level route); a flat all-to-all over the slower link between nodes costs much more.

It worked when, on every rank, the gradients, losses, norms, weights and Adam state equal those of the d = 1 run bit for bit, and an elastic resume at another degree matches a run that never stopped.

This was shown with fp32 gradients, on up to 16 GPUs on two nodes, both with replicated weights and with FSDP emulated (a flat gradient, reduce-scatter and a sharded AdamW; the framework's own FSDP was not run). Tree and the ordered collectives removed the effect of bucketing, the ordered reduce-scatter removed the effect of which rank owns a slice, and logical shards gave identical results at every degree tested and across an elastic resume (report).

Find the cause of a first divergence in a gradient sum

What you observe Cause
The gradients change after you change the bucket size, the parameter order or the model, at the same degree the order of a ring all-reduce follows the parameter's offset in the message
The first iteration, or the first one after a restart, differs from the rest DDP's first iteration puts every gradient in one bucket
Every element that differs has moved to another owning rank the order of a reduce-scatter follows the owning rank
Almost every element differs at another degree, under any collective the backward pass on each rank is part of the arithmetic, so use logical shards
The clipping norm differs only for some placements NCCL adds up the sums of squares of the shards

1.5.4. What can go wrong

DDP is not even consistent with itself

DDP's first iteration after it is constructed uses a single bucket, and every restart constructs DDP again. So under Ring, a restart alone changes the gradient. Use Tree or the ordered hook, not a bucket-size setting.

Tree does not apply to reduce-scatter

NCCL_ALGO=Tree changes only all-reduce, so sharded training still follows the owning rank. Use the ordered reduce-scatter for FSDP-style sharding.

Handing the sum across ranks back to NCCL

Logical shards fail from three ranks on if the last step of the tree is an NCCL all-reduce or reduce-scatter, whether Ring or Tree, because the tree is then no longer the same tree. Every sum across ranks must be part of the fixed tree.

A scalar that agrees by luck

A sum of a few dozen terms often rounds to the same value, so a stock sharded norm can agree for many placements. It still depends on the sharding, and it fails for others. Use the logical norm with its aligned blocks.

Logical shards do not give the batched gradient

Logical shards at d = 1 give a different result from one batched backward pass over the same sequences. Both are correct roundings of different arithmetic. Pick one of them as the reference, and use it everywhere.

1.5.5. Summary

  • Under Ring, a gradient's bits depend on its position in the message; under reduce-scatter, they depend on the rank that owns it. Tree or the ordered collectives fix the first, and an ordered reduce-scatter fixes the second.
  • Another degree means another set of partial sums, so only logical shards with one fixed tree give L5 and an elastic resume.
  • Norms and mean losses are sums across ranks too, so give them the same fixed tree.
  • Pin the optimizer's code path.

1.5.6. Go deeper