FSDP › FSDP Architecture
Language AI HandbookAll chapters FSDP › FSDP ArchitectureOne section · Sections in this chapter3 / 13 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
FSDP works by sharding the three memory-intensive quantities, namely parameters, gradients, and optimizer states, across all participating GPUs. The elegance of the design is that the sharding is largely transparent to the model code: from the perspective of each individual layer, it receives its full parameter tensor when it needs it, computes normally, and returns its result. The orchestration of gathering and scattering tensors happens behind the scenes.
What Sharding Means Mathematically
Consider a linear layer with a weight matrix of shape , containing total parameters. With GPUs, FSDP flattens into a 1D tensor of length and assigns an equal contiguous slice to each GPU:
- GPU 0 holds elements through
- GPU 1 holds elements through
- GPU holds elements through
Each GPU also holds a corresponding shard of the gradient tensor for those same parameters, and a shard of the Adam optimizer states (first moment and second moment ) for those same parameter positions. This three-way alignment is what makes the local optimizer step possible: when it is time to update parameters, GPU has all the information it needs to perform the Adam update for its slice without any communication.
The critical constraint is that computing the forward pass through a linear layer requires the complete weight matrix , not a shard of it. An input of shape needs to be multiplied by the full to produce output of shape . FSDP handles this requirement with explicit communication phases.
The All-Gather and Reduce-Scatter Cycle
FSDP wraps each module (or group of modules) in a unit called an FSDP unit. These units are the atoms of the sharding system: all-gather and reduce-scatter operations occur at FSDP unit boundaries. Understanding the communication pattern for one unit clarifies the entire system.
During the forward pass, the following sequence occurs for each FSDP unit:
- Every GPU broadcasts its local parameter shard to all other GPUs via an all-gather. After this operation, all GPUs temporarily hold the complete parameter tensor for this unit.
- Each GPU uses the full parameter tensor to compute its portion of the forward pass (processing its slice of the data batch).
- Immediately after the computation completes, FSDP discards the reconstructed parameter tensor on each GPU, leaving only the original local shard. This is the key memory reclamation step.
During the backward pass, the cycle repeats with one addition:
- An all-gather reconstructs the full parameter tensor again (necessary for computing parameter gradients).
- The backward computation runs, producing full-parameter gradient tensors on each GPU.
- A reduce-scatter aggregates gradients across all GPUs and distributes the result. Unlike all-reduce (which gives every GPU the full summed gradient), reduce-scatter performs the sum and simultaneously shards it: GPU receives only the portion of the gradient corresponding to its parameter shard.
- The reconstructed parameter tensor is discarded again.
The optimizer step then runs locally on each GPU: each GPU updates its own parameter shard using its own gradient shard and its own optimizer state shard. No communication is required at this step, because the data for each parameter's update resides entirely on the GPU responsible for that parameter.
This complete cycle, all-gather then compute then discard (forward), all-gather then compute then reduce-scatter (backward), local optimizer step, is what defines FSDP's operation. The name "fully sharded" reflects the fact that all three memory-intensive components remain sharded at rest; they are only temporarily gathered when computation demands it.
Standard data parallelism uses all-reduce at the end of backward to sum gradients across all GPUs, giving each GPU the full summed gradient. FSDP uses reduce-scatter instead, which sums gradients but also shards the result. Each GPU receives only the portion it owns. This saves memory because no GPU ever holds a complete gradient tensor.
Memory Profile Comparison
Under FSDP with GPUs, the memory breakdown per GPU changes dramatically compared to DDP:
- Parameters (steady state): bytes (only the local shard persists)
- Gradients (steady state): bytes (only the local gradient shard persists)
- Optimizer states: bytes (first and second moments for the local shard)
- All-gather buffer during forward: bytes temporarily (freed after each layer's computation)
The steady-state memory per GPU is approximately bytes for the sharded quantities. The peak memory includes the temporary all-gather buffer of bytes during computation for the current FSDP unit, but this is freed right after the unit completes. For a model split into FSDP units, the peak at any instant is bytes (steady-state plus one unit's full parameters).
Compare this to DDP, where each GPU holds bytes at all times. The reduction factor from to for the steady state is exactly , the number of GPUs. This linear scaling in memory capacity with GPU count is what makes FSDP so powerful for scaling to large models.
To see this concretely: a 13B parameter model requires about 208 GB of training memory ( GB). With FSDP on 8 GPUs of 40 GB each (320 GB total), each GPU holds only GB of steady-state memory, fitting comfortably within 40 GB when including activation buffers.