FSDP › FSDP Architecture

Michael BrenndoerferJanuary 22, 20265 min read
Language AI HandbookAll chapters
Part 31 of 67Training Infrastructure
Chapter 226FSDP › FSDP Architecture
Reading section 3 of 13 · FSDP Architecture · 5 minSection 3 of 13 · 5 min

Book contents

Section 3 of 13 · 5 min read

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 WW of shape (dout,din)(d_{\text{out}}, d_{\text{in}}), containing PW=dout×dinP_W = d_{\text{out}} \times d_{\text{in}} total parameters. With NN GPUs, FSDP flattens WW into a 1D tensor of length PWP_W and assigns an equal contiguous slice to each GPU:

  • GPU 0 holds elements 00 through ⌊PW/N⌋−1\lfloor P_W/N \rfloor - 1
  • GPU 1 holds elements ⌊PW/N⌋\lfloor P_W/N \rfloor through ⌊2PW/N⌋−1\lfloor 2P_W/N \rfloor - 1
  • GPU kk holds elements ⌊kPW/N⌋\lfloor kP_W/N \rfloor through ⌊(k+1)PW/N⌋−1\lfloor (k+1)P_W/N \rfloor - 1

Each GPU also holds a corresponding 1/N1/N shard of the gradient tensor for those same parameters, and a 1/N1/N shard of the Adam optimizer states (first moment mm and second moment vv) 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 kk 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 WW, not a shard of it. An input xx of shape (B,din)(B, d_{\text{in}}) needs to be multiplied by the full WW to produce output of shape (B,dout)(B, d_{\text{out}}). 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:

  1. 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.
  2. Each GPU uses the full parameter tensor to compute its portion of the forward pass (processing its slice of the data batch).
  3. 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:

  1. An all-gather reconstructs the full parameter tensor again (necessary for computing parameter gradients).
  2. The backward computation runs, producing full-parameter gradient tensors on each GPU.
  3. 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 kk receives only the portion of the gradient corresponding to its parameter shard.
  4. 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.

All-Gather vs. All-Reduce

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 NN GPUs, the memory breakdown per GPU changes dramatically compared to DDP:

  • Parameters (steady state): 4P/N4P/N bytes (only the local shard persists)
  • Gradients (steady state): 4P/N4P/N bytes (only the local gradient shard persists)
  • Optimizer states: 8P/N8P/N bytes (first and second moments for the local shard)
  • All-gather buffer during forward: 4P4P bytes temporarily (freed after each layer's computation)

The steady-state memory per GPU is approximately (4+4+8)P/N=16P/N(4 + 4 + 8)P/N = 16P/N bytes for the sharded quantities. The peak memory includes the temporary all-gather buffer of 4P4P bytes during computation for the current FSDP unit, but this is freed right after the unit completes. For a model split into MM FSDP units, the peak at any instant is 16P/N+4P/M16P/N + 4P/M bytes (steady-state plus one unit's full parameters).

Compare this to DDP, where each GPU holds 16P16P bytes at all times. The reduction factor from 16P16P to 16P/N16P/N for the steady state is exactly NN, 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 (16×1316 \times 13 GB). With FSDP on 8 GPUs of 40 GB each (320 GB total), each GPU holds only 208/8=26208/8 = 26 GB of steady-state memory, fitting comfortably within 40 GB when including activation buffers.