FSDP2 Training for Ultra-Large Multimodal Models: Memory Sizing, Sharding, and Checkpoint Loading
This article explains how to estimate GPU memory requirements for a 2.8-trillion-parameter multimodal model before choosing a parallelism strategy, then shows how to apply FSDP2 with `fully_shard`, configure mixed precision, launch distributed training, control sharding granularity and prefetching, and load sharded checkpoints with `torch.distributed.checkpoint`.
1. Size the Memory First, Then Choose a Parallelism Strategy
Memory requirements for a 2.8-trillion-parameter model are easy to underestimate. The bf16 or FP16 weights alone are about 5.6 TB (2.8 × 10^12 parameters × 2 bytes). Under a common mixed-precision training estimate, each parameter also needs 2 bytes for bf16 gradients, 4 bytes for FP32 master weights, and 8 bytes for Adam’s first- and second-order moments, for a total of about 16 bytes per parameter. That puts model states, excluding activations, at roughly 44.8 TB. The following table uses decimal units to roughly estimate the model state carried by each GPU:
| Number of GPUs | Model state per GPU (theoretical) |
|---|---|
| 256 | About 175 GB |
| 512 | About 87.5 GB |
| 1024 | About 43.75 GB |
Taking an accelerator with 80 GB of memory as an example, 256 GPUs cannot hold the model state, and 512 GPUs cannot either, because the model state alone already exceeds per-GPU capacity. With 1024 GPUs, model state takes about 44 GB, leaving the remaining space for activations and communication buffers. The actual scale must also be evaluated together with activation recomputation, CPU offload, and similar techniques. The numbers above are only a lower bound; real values vary with implementation details, so you should validate them with a small-scale model before launching full training.
2. Basic Usage of FSDP2
FSDP2 is located in torch.distributed.fsdp, and its core interface is fully_shard. It annotates sharding directly on the original module and does not require replacing the model with a wrapper class as the first-generation FSDP did. Check the PyTorch version before use. PyTorch 2.6 and later can import fully_shard directly from torch.distributed.fsdp. In PyTorch 2.4 to 2.5, the interface is located in torch.distributed._composable.fsdp, and parameter details may differ, so refer to the official documentation for the version you are using.
The following example assumes the model contains two groups of repeated sublayers, vision_encoder.blocks and llm_backbone.layers, as well as a modality alignment layer in between. Actual attribute names need to be changed according to your model structure.
import torchimport torch.distributed as distfrom torch.distributed.device_mesh import init_device_meshfrom torch.distributed.fsdp import fully_shard, MixedPrecisionPolicydef setup_fsdp2(model: torch.nn.Module) -> torch.nn.Module:dist.init_process_group(backend="nccl")world_size = dist.get_world_size()mesh = init_device_mesh("cuda", (world_size,))mp_policy = MixedPrecisionPolicy(param_dtype=torch.bfloat16, # Parameter precision used for forward and backward computationreduce_dtype=torch.float32, # Use higher precision for gradient reduction to reduce numerical error)# Shard bottom-up: process repeated sublayers first, then outer modulesfor block in model.vision_encoder.blocks:fully_shard(block, mesh=mesh, mp_policy=mp_policy)fully_shard(model.vision_encoder, mesh=mesh, mp_policy=mp_policy)for layer in model.llm_backbone.layers:fully_shard(layer, mesh=mesh, mp_policy=mp_policy)fully_shard(model.llm_backbone, mesh=mesh, mp_policy=mp_policy)# The root module takes over parameters that have not yet been sharded, such as the modality alignment layerfully_shard(model, mesh=mesh, mp_policy=mp_policy)return model
Launch training as follows, filling in the number of nodes and GPUs per node according to the actual cluster configuration:
torchrun --nnodes=32 --nproc_per_node=8 --rdzv_backend=c10d \--rdzv_endpoint=$MASTER_ADDR:29500 train.py
Sharding granularity is a parameter that requires trade-offs. The finer the per-layer sharding, the smaller the data in each All-Gather, but the more communication operations there are. The vision encoder has many layers and relatively small parameters per layer, so finer granularity is suitable. LLM layers have large parameters per layer, so sharding by layer is usually sufficient. The final choice should be based on the ratio of communication time to computation time in the profiler.
If you need to control prefetching manually, you can set prefetch relationships between adjacent layers. FSDP2 also performs implicit prefetching by default, so this step is an optional optimization:
layers = list(model.llm_backbone.layers)for prev, nxt in zip(layers[:-1], layers[1:]):prev.set_modules_to_forward_prefetch([nxt]) # Collect the next layer's parameters earlier during forwardnxt.set_modules_to_backward_prefetch([prev]) # Collect the previous layer's parameters earlier during backward
3. Model Loading: Load by Shard, Not as a Whole
The weights of a 2.8-trillion-parameter model cannot be fully constructed in a single process. The recommended workflow has four steps. First, build the model on the meta device, which does not allocate real GPU memory. Second, run fully_shard as described in the previous section. Third, call to_empty(device="cuda") to allocate empty sharded memory on each GPU. Fourth, use torch.distributed.checkpoint (DCP) so that each rank reads only the part of the weights it is responsible for.
One advantage of DCP is that it supports resharding. If the number of GPUs changes during training, the checkpoint can be loaded into the new shard layout without conversion. This is useful for long training runs that require elastic scaling.
When CPU offload is required, you can pass offload_policy=CPUOffloadPolicy to fully_shard.
