Fully sharded
Fully sharded data parallelism (FSDP) trains like data parallelism, every rank on its own micro-batch with the same weights, but no rank stores the whole model. Each holds a shard of every weight, of its gradient and of its optimizer state, and puts the full weights of one part of the model together only while that part computes. It is the idea of ZeRO stage 3 (the ZeRO tab), built into PyTorch.
This page runs FSDP2, PyTorch's fully_shard, on GPT-2 with four CPU ranks talking through gloo. Every GPT-2 block is one FSDP unit, and the root unit holds the rest: the token embedding (which is also the output layer), the position embedding and the final LayerNorm. FSDP2 shards each tensor by rows: dim 0 is padded up to a multiple of the ranks and cut into equal chunks, and rank keeps chunk . 's 50,257 rows become four chunks of 12,565 rows, the last holding three rows of padding. The A quarter each panel measures what each rank holds: its rows of every weight, their gradients and AdamW's moments of them.
All-gather, compute, free
Before a unit runs forward, every rank sends its chunk of the unit's weights to all the others, an all-gather, so that each has the whole unit. After the unit's forward FSDP frees the gathered weights (reshard after forward) and gathers them again before the unit's backward, so at any moment a rank holds the full weights of one or two units only. The root unit is the exception: it is needed again as soon as backward starts, so FSDP keeps it gathered until its backward is done.
The block card draws one unit's tensors as the Dense tab lays them out, the Q, K and V projections side by side, the output projection, the MLP's up and down matrices, with each rank's rows in its colour. An all-gather of bytes costs each rank bytes sent around a ring of ranks.
Reduce-scatter after backward
A unit's backward gives each rank the gradient of the whole unit for its own micro-batch. A rank only needs the gradient of its own rows, averaged over all ranks, so FSDP reduce-scatters it: every rank cuts its gradient into the same row chunks, and chunk of all ranks is summed and handed to rank alone. The strip The reduce-scatter shows it on the first rows of each rank's chunk of the unit's first matrix: every rank's own gradient, the averaged values each rank keeps, and one process's gradient for the same rows. Then each rank's AdamW updates its own rows, and nothing needs to be gathered after the step.
Prefetching
A rank that waits for each all-gather before computing leaves its processor idle. FSDP prefetches: it asks for the next unit's weights before the current unit computes, so the transfer overlaps the computation. In backward FSDP2 does this by default, entering a unit's backward it first asks for the previous unit's weights. On GPUs forward prefetching comes almost for free, because collectives run on their own stream while the CPU runs ahead issuing kernels.
This page records the same four steps two ways. As it comes, an all-gather on this CPU blocks the rank until it is done, so forward waits at every unit and backward's prefetch only moves the wait. With async all-gathers, prefetch 1 ahead, the all-gathers return at once and run on gloo's own threads, and every unit asks for the next one's weights before computing. The One step timeline shows both: each rank's compute, the time it spent waiting for weights (dashed), every all-gather (↗ when prefetched) and reduce-scatter, and under each lane the bytes of gathered weights alive. Prefetching, in forward or in backward, holds two blocks at once besides the root to save the wait: in both runs rank 0's peak is the root and two blocks. The times are from this Mac's CPU; the pattern carries over to GPUs, the speed does not.
The same step as one process
Both runs take exactly the same steps: prefetching changes when weights arrive, not what is computed. The table under the page compares the weights after each step, and the reduce-scattered gradients, with one process training on the whole batch; they differ by float32 summation order, less than one process differs from itself when it adds its micro-batches one after another.
Where it came from
Xu et al. (2020) sharded the weight update across data-parallel replicas in TPU training, and ZeRO (Rajbhandari et al., 2019) sharded the weights, gradients and optimizer state across GPUs in DeepSpeed. FairScale brought fully sharded data parallelism to PyTorch, and PyTorch made it native (Zhao et al., 2023). FSDP2 rebuilt it on DTensor, sharding each parameter by rows instead of flattening a unit's parameters into one buffer, as used in TorchTitan (Liang et al., 2024). The tensor and pipeline parallel tabs split the model itself instead.
Papers
- PyTorch FSDP: Experiences on Scaling Fully Sharded Data Parallel PyTorch's fully sharded data parallelism: units, all-gather before compute, reduce-scatter after backward, prefetching.
- ZeRO: Memory Optimizations Toward Training Trillion Parameter Models Sharding weights, gradients and optimizer state across data-parallel ranks; FSDP is its stage 3.
- Automatic Cross-Replica Sharding of Weight Update in Data-Parallel Training The weight update sharded across replicas with a reduce-scatter and an all-gather, in XLA.
- TorchTitan: One-stop PyTorch native solution for production ready LLM pre-training Large-model training on FSDP2, the per-parameter DTensor sharding this page runs.
- PyTorch Distributed: Experiences on Accelerating Data Parallel Training DDP, the replicated data parallelism FSDP shards.