FSDP › Worked Example: Memory Savings on a 7B Model

Michael BrenndoerferJanuary 22, 20262 min read
Language AI HandbookAll chapters
Part 31 of 67Training Infrastructure
Chapter 226FSDP › Worked Example: Memory Savings on a 7B Model
Reading section 8 of 13 · Worked Example: Memory Savings on a 7B Model · 2 minSection 8 of 13 · 2 min

Book contents

Section 8 of 13 · 2 min read

FSDP › Worked Example: Memory Savings on a 7B ModelOne section · Sections in this chapter8 / 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

To build intuition for the magnitudes involved, let's trace through the memory accounting for a concrete scenario: training a 7 billion parameter model on 8 A100-40GB GPUs.

Without FSDP (DDP Baseline)

With standard DDP, every GPU holds the complete model state:

  • Parameters: 4×7×109=284 \times 7 \times 10^9 = 28 GB
  • Gradients: 2828 GB
  • Optimizer states (Adam): 5656 GB
  • Total model state: 112112 GB

This already exceeds the 40 GB GPU capacity by 2.8×2.8\times. DDP cannot run this configuration at all.

With FSDP FULL_SHARD on 8 GPUs

With FULL_SHARD, each GPU holds 1/81/8 of each component:

  • Parameters (shard): 28/8=3.528 / 8 = 3.5 GB
  • Gradients (shard): 3.53.5 GB
  • Optimizer states (shard): 77 GB
  • Total steady-state: 1414 GB

During the forward and backward passes, a single FSDP unit's all-gather buffer temporarily consumes an additional 28/M28/M GB where MM is the number of FSDP units. For a 32-layer model wrapped at the layer level, each all-gather buffer is 28/32≈0.87528/32 \approx 0.875 GB, bringing peak memory to about 14.87514.875 GB per GPU, well within the 40 GB limit.

The remaining memory budget of approximately 25 GB can accommodate activations from long sequence lengths and larger batch sizes. Activations for a typical transformer layer scale as O(B×S×d)O(B \times S \times d) where BB is batch size, SS is sequence length, and dd is hidden dimension. For a 7B model (d=4096d = 4096), a batch of 4 sequences of length 2048 in bfloat16 generates roughly 4×2048×4096×2≈674 \times 2048 \times 4096 \times 2 \approx 67 MB per layer before considering the activations needed for attention score computation. With activation checkpointing (recomputing rather than storing activations), this budget extends further.

The Practical Significance

This example illustrates why FSDP enabled more open-source large model training in 2023 and 2024. Before FSDP became widely available and well-documented, training 7B models required either expensive 80 GB GPUs (A100-80GB or H100), proprietary systems like Google's TPU pods, or careful hand-engineering with DeepSpeed. With FSDP on standard 40 GB A100 GPUs, which were broadly available through cloud providers, the research community could train and fine-tune these models on accessible hardware.