FSDP2 Training for a 2.8T-Parameter Multimodal Model: Sharding and Memory Practices
This article explains how to train a 2.8-trillion-parameter multimodal model with FSDP2, covering capacity planning, hardware and software prerequisites, FSDP1 versus FSDP2 differences, model loading and initialization, sharding scheduling and gradient synchronization, OOM troubleshooting order, and checkpointing for fault tolerance. Readers will learn why trillion-parameter training requires sharding, how FSDP2 handles per-tensor sharding for multimodal modules, and which practical checks help stabilize large-scale training.
1. Introduction: Why Trillion-Parameter Training Requires Sharding
What does 2.8 trillion parameters mean? Storing the weights in bf16 alone requires about 5.6TB. With mixed-precision training and the Adam optimizer, each parameter also needs gradients and optimizer states, totaling about 16 bytes per parameter. On that basis, the total model state is about 44.8TB. No single 80GB GPU can hold the full model state, so model states must be sharded across many GPUs.
Looking only at model states, the theoretical lower bound is about 560 80GB GPUs (44.8TB ÷ 80GB). This does not include activations, communication buffers, or fragmentation, so the actual GPU count will be higher. Therefore, GPU capacity planning should come before choosing a parallel strategy.
Multimodal models have an additional issue: the vision encoder, language model, and projection layers differ greatly in parameter scale and compute duration, resulting in uneven memory usage. The sharding strategy needs to schedule independently by module or tensor, which is exactly the problem FSDP2 is designed to solve.
2. Prerequisites Before Training
Before starting adaptation, confirm the following conditions; otherwise, the code will be difficult to reproduce.
1. Hardware conditions
- Per-GPU memory capacity and bandwidth must accommodate the sharded model state and leave room for activations.
- Intra-node interconnect (such as PCIe or a vendor-specific bus) and inter-node network bandwidth (such as RDMA) must be sufficient to support All-Gather and Reduce-Scatter.
- Use collective communication benchmarks to confirm actual bandwidth rather than relying only on nominal values.
2. Software conditions
- PyTorch 2.6 or later is recommended, with the stable
fully_shardinterface intorch.distributed.fsdp. Refer to the PyTorch documentation for the exact version. - The communication backend should be provided by the hardware vendor, and its all-gather and reduce-scatter implementations and versions should be confirmed. If you are using a vendor-specific full-featured GPU, verify compatibility between its collective communication library and the PyTorch distributed interfaces.
- bf16 is recommended for training precision, with fp32 for gradient reduction.
- Use activation checkpointing to reduce peak memory.
3. Reproduction prerequisites
- Record the version numbers of PyTorch, the driver, the communication library, and the model code.
- Fix random seeds, first run through the loss curve in a small-scale configuration, and then scale up the GPU count.
3. Differences Between FSDP1 and FSDP2
FSDP1 shards at the module level. Multiple parameters within a module are concatenated into a FlatParameter and then sharded as a whole. FSDP2 shards at the parameter-tensor level, with each parameter sharded independently.
| Dimension | FSDP1 | FSDP2 |
|---|---|---|
| Sharding unit | Module; multiple parameters concatenated into a FlatParameter | Single parameter tensor, represented as a DTensor |
| Usage | Wrapper class FullyShardedDataParallel |
Function fully_shard, applied in place to a module |
| Peak memory | Affected by the total parameter count of the module; coarser granularity | Released per tensor; peak memory is easier to control |
| Communication overlap | Depends on wrapper granularity | Reduce-Scatter is triggered as soon as module gradients are ready during backward |
| Parallel composition | More limited composition options | Based on DeviceMesh; easier to combine with tensor parallelism (verify by version) |
| Suitable scenarios | Existing projects with FSDP1 code | New projects, or multimodal models that need fine-grained control |
For multimodal models, the meaning of fine granularity is this: there are many small operators and module sizes vary, so coarse-grained sharding can easily cause peak memory fluctuations. With per-tensor management, memory usage across modules becomes easier to predict.
4. Model Loading and Initialization
The following code is a flow illustration and must be adjusted for the actual model structure and communication backend.
import osimport torchimport torch.distributed as distfrom torch.distributed.device_mesh import init_device_meshfrom torch.distributed.fsdp import fully_shard, MixedPrecisionPolicy# Initialize the process group; replace the backend with the communication library provided by the hardware vendordist.init_process_group(backend="nccl")local_rank = int(os.environ["LOCAL_RANK"])torch.cuda.set_device(local_rank)world_size = dist.get_world_size()mesh = init_device_mesh("cuda", (world_size,))# Build the model on the meta device to record structure only, without allocating actual GPU memorywith torch.device("meta"):model = build_multimodal_model()mp = MixedPrecisionPolicy(param_dtype=torch.bfloat16, reduce_dtype=torch.float32)# Shard submodules one by one first, then shard the root modulefor block in model.blocks:fully_shard(block, mesh=mesh, mp_policy=mp)for encoder in model.modality_encoders:fully_shard(encoder, mesh=mesh, mp_policy=mp)fully_shard(model, mesh=mesh, mp_policy=mp)# Materialize the empty shell on the local GPU, then load the sharded weightsmodel.to_empty(device="cuda")load_distributed_checkpoint(model) # For example, use torch.distributed.checkpoint# Note: after to_empty, non-parameter buffers (such as positional encoding caches) need to be reinitialized
There are three key steps in the code:
- Build the model on the
metadevice to avoid filling a single GPU’s memory before loading. - Shard submodules first, then shard the root module. This way, each submodule’s parameters are managed independently, and the root module keeps only the remaining parameters.
- Materialize the model before loading weights. Weights are loaded through a distributed checkpoint, and each GPU reads only the shard it is responsible for.
5. Sharding Scheduling and Gradient Synchronization
During forward propagation, each sharded module collects full parameters through All-Gather before computation. After computation, if reshard_after_forward is set, non-local shards are released immediately. During backward propagation, parameters are collected on demand again. After a module’s gradient computation is complete, Reduce-Scatter is triggered to reduce gradients and shard them back to the local GPU.
This gradient synchronization is communication overlap in synchronous training. Reduce-Scatter and the backward computation of subsequent layers execute in parallel on different CUDA streams. The optimizer update still waits until all gradient reductions are complete; this is not asynchronous SGD, and the training semantics are unchanged.
For multimodal models, the vision encoder and language-side modules have different backward timing. Sharding each module separately allows the encoder’s gradients to enter communication earlier.
There is one risk to note. If a sharded module does not participate in forward computation in some iterations, its parameters may have no gradients. If GPUs make different numbers of collective communication calls, training will hang. There are two ways to handle this: ensure that all sharded modules participate in computation in every iteration, or design branch logic at the framework level so that all GPUs execute the same communication sequence.
6. OOM Troubleshooting Order
When running out of memory during training, troubleshoot in the following order instead of blindly reducing the batch size first:
- Confirm whether the model is built on the meta device. If memory is filled during loading, the problem is usually in the initialization flow.
- Check the sharding granularity. Confirm that all large modules are sharded and that no module is left outside the root module.
- Check the All-Gather peak. If a single large module exceeds memory when collecting full parameters, reduce that module’s wrapper granularity or adjust the prefetch settings.
- Check activations. Enable activation checkpointing, or reduce the sequence length and batch size.
- Check optimizer states. Confirm that optimizer states and parameters use the same sharding scheme and that no full copy is stored on each GPU.
Measure memory usage separately after each change to avoid mixing multiple modifications and being unable to tell which one was effective.
7. Fault Tolerance and Checkpoints
During long-running training, node failures are common. Checkpoints need to meet three requirements: save the complete training state, recover after a failure, and produce training results after recovery that are consistent with those before the interruption.
1. What to save
- Sharded model weights, optimizer states, and learning rate scheduler state.
- Random number generator state.
- Data loader cursor, meaning the position of consumed samples.
When saving with torch.distributed.checkpoint, each GPU writes its own shard. This format supports resharding and loading under a different GPU count, which is useful for recovery after a failure.
