Advanced

Data parallelism, ZeRO & FSDP

Data parallelism increases throughput, while ZeRO and FSDP shard parameters, gradients, and optimizer state to fit larger models.

Updated

1

Concept

One accelerator can process only a limited batch and hold only a limited model state. Data parallelism increases training throughput by giving several workers identical model replicas and different examples. If rank rr computes gradient grg_r, a collective reduction forms the mean or sum used by every replica. Starting from equal weights and applying equal gradients keeps replicas synchronized.

The global batch is the combined work across ranks and gradient-accumulation steps. If there are PP ranks, local microbatch bb, sequence length TT, and aa accumulation steps, the global batch contains PbaTPbaT tokens, assuming no padding loss. Scaling worker count without accounting for global batch changes optimization, not only speed.

Replication is memory-expensive. Mixed-precision Adam training may keep low-precision parameters, gradients, higher-precision master parameters, and two optimizer moment tensors. Exact bytes depend on implementation and dtype, but optimizer state can exceed the memory for inference weights. Ordinary data parallelism duplicates all of it on every rank.

ZeRO decomposes that redundancy in stages. Stage 1 shards optimizer state. Stage 2 also shards gradients. Stage 3 also shards parameters. Each rank owns only a fraction of the persistent state, while collectives make the necessary pieces available for computation. The names describe memory ownership, not a new optimization algorithm.

Fully Sharded Data Parallel (FSDP) implements the fully sharded pattern around modules. Before a module’s forward pass, ranks all-gather its parameter shards to materialize the full parameters needed locally. After use, parameters can be resharded. During backward, communication reconstructs parameters as needed and reduce-scatter distributes gradient shards to their owners. Communication can overlap with computation when ordering and network permit.

Wrapping policy matters. If the sharded unit is too large, peak all-gather memory is large and overlap is limited. If it is too small, collectives become numerous and latency dominates. Transformer-block boundaries are common because they offer substantial compute between communications. Prefetching the next block can hide latency but increases simultaneous memory.

Checkpointing becomes distributed too. A full state-dict gather may exceed host memory. Sharded checkpoints let ranks write their owned tensors, accompanied by metadata that reconstructs the global names and shapes. Resuming under a different world size requires a format and loader that support resharding. Saving only weights is insufficient for exact training continuation; optimizer, scheduler, scaler, random-number, and data-loader states matter.

Failures must be collective. If one rank encounters bad data while others enter an all-reduce, the job can hang rather than throw a clean exception. Timeouts, coordinated error propagation, rank-aware logs, and reproducible batch IDs are operational necessities. Network topology also matters: fast links inside a node and slower links across nodes may favor hierarchical collectives.

The 3D visual for this lesson should distinguish ownership from temporary materialization. Show persistent parameter, gradient, and optimizer shards as colored slices on ranks. Then animate an all-gather around one block, local compute on different data, and reduce-scatter of gradients. Leaving the full model displayed on every rank would misrepresent the memory benefit.

The stable model is replicated computation over different data with increasingly sharded state. Data parallelism buys throughput. ZeRO and FSDP remove redundant memory. They do not provide free capacity: the price is communication, orchestration, and more demanding checkpoint and failure semantics.

2

Explain it like I am five

Several kitchens receive different customer orders but use the same recipe. Ordinary data parallelism gives every kitchen a full pantry and averages recipe corrections after service. Sharding divides pantry stock, correction ledgers, and even recipe cards across kitchens; ingredients are gathered just before a dish and released afterward. More storage fits collectively, but delivery traffic becomes part of cooking.

3

Teach it back

Compare replicated data parallelism with ZeRO/FSDP sharding and name the communication needed in one training step.

Minimum: 80 characters and 15 words. Your text stays only in this browser.

Saved only on this device.

Show a model answer

Replicated data parallelism keeps a full model and optimizer state on every rank, processes different microbatches, then all-reduces gradients so replicas update identically. ZeRO progressively shards optimizer state, gradients, and parameters. FSDP all-gathers a layer's parameter shards before computation, reduce-scatters gradients after backward, and can reshard parameters, lowering per-rank memory at the cost of communication and scheduling complexity.

4

Check your understanding

1. What is replicated in ordinary data parallel training?
Answer and explanation

The full model on every rank — Each rank owns a full parameter copy and sees a different portion of the batch.

2. What does FSDP typically all-gather before a sharded module computes?
Answer and explanation

Its full parameters for that computation — Ranks exchange parameter shards just in time so each can execute the local forward or backward operation.

Complete the teach-back and answer the quiz correctly to finish this lesson.

Sources

  1. Samyam Rajbhandari et al. (2019). ZeRO: Memory Optimizations Toward Training Trillion Parameter Models.
  2. Yanli Zhao et al. (2023). PyTorch FSDP: Experiences on Scaling Fully Sharded Data Parallel.