Training Trillion-Parameter Multimodal Models with FSDP2

    This article provides a technical guide on training trillion-parameter multimodal models using FSDP2. It covers VRAM estimation, parallel strategy selection, core differences between FSDP2 and FSDP1, and practical configuration approaches with a PyTorch code example for multimodal architectures.

    1. Calculate VRAM Before Choosing a Sharding Strategy

    Under mixed precision training, the Adam optimizer consumes about 16 bytes per parameter: 2 bytes for BF16 weights, 2 bytes for BF16 gradients, 4 bytes for FP32 master weights, 4 bytes for first-order momentum, and 4 bytes for second-order momentum. For a 2.8 trillion parameter model, these states alone require approximately 44.8 TB, not including activations, communication buffers, and memory fragmentation.

    The estimation formula is as follows:

    Per-GPU state memory ≈ Parameter count × 16 bytes ÷ Number of shards

    Using 80 GB GPUs, at least 560 GPUs are required just to fit the weights and optimizer states. Actual training demands more GPUs because activations and communication buffers also consume VRAM. If a pure BF16 optimizer state is used instead, the calculation differs significantly; therefore, it is crucial to confirm the optimizer and precision configuration before estimating.

    This type of training faces three main challenges. First, communication volume increases rapidly as the number of nodes grows. Second, memory fragmentation can cause OOM (Out of Memory) errors even when the total memory appears sufficient. Third, the substantial structural difference between the vision encoder and the language model makes it difficult for a single parallel strategy to accommodate both effectively.

    2. Why Choose FSDP2?

    Tensor Parallelism (TP) and Pipeline Parallelism (PP) are common combinations for 100-billion-parameter models. TP requires frequent collective communication within each layer, which becomes a bottleneck as the parameter scale grows. PP introduces pipeline bubbles, reducing hardware utilization.

    Fully Sharded Data Parallel (FSDP) shards parameters, gradients, and optimizer states across processes within a data parallel group. Each process only holds its own portion long-term. This allows memory consumption to decrease linearly with the number of GPUs, making it ideal for scenarios where the parameter volume far exceeds single-GPU capacity.

    The primary difference between FSDP2 and the first-generation FSDP lies in the sharding approach. FSDP1 concatenates all parameters of a module into a single FlatParameter and then shards it as a whole. FSDP2 shards parameter by parameter, with each parameter represented as a DTensor and sharded along dimension 0. Consequently, original parameter names and shapes remain visible, making weight saving, optimizer configuration, and combination with TP much simpler. When a parameter dimension cannot be evenly divided by the number of GPUs, the framework automatically handles uneven sharding.

    Regarding versions, PyTorch 2.6 and above provide the stable interface torch.distributed.fsdp.fully_shard. In PyTorch 2.4 to 2.5, the same functionality is located at torch.distributed._composable.fsdp. As interface details may vary, consult the official documentation for the corresponding version before upgrading.

    3. Group Configuration for Multimodal Models

    FSDP2 does not automatically determine the optimal sharding strategy for vision and language modules. Developers must manually specify the grouping configuration and adjust it through empirical testing.

    The basic principle of grouping is inside-out. First apply fully_shard to inner Transformer blocks, then to outer modules, and finally to the root module. During the forward pass, each block only gathers full parameters temporarily when it is computing, and releases them after computation.

    The following example assumes the model contains vision_encoder and llm submodules, each with a list of layers. It requires PyTorch 2.6 or higher and should be run in a CUDA environment on a single machine with multiple GPUs or multiple machines with multiple GPUs.

    1. import torch
    2. from torch.distributed.device_mesh import init_device_mesh
    3. from torch.distributed.fsdp import fully_shard, MixedPrecisionPolicy
    4. def build_fsdp2_model(model_cls, cfg, world_size):
    5. mesh = init_device_mesh("cuda", (world_size,))
    6. mp = MixedPrecisionPolicy(
    7. param_dtype=torch.bfloat16, # Parameter precision used during computation
    8. reduce_dtype=torch.float32, # Higher precision for gradient reduction to reduce numerical errors
    9. )
    10. # The meta device records structure only without allocating actual VRAM
    11. with torch.device("meta"):
    12. model = model_cls(cfg)
    13. # Shard inner blocks first, then outer modules
    14. for block in model.vision_encoder.blocks:
    15. fully_shard(block, mesh=mesh, mp_policy=mp)
    16. fully_shard(model.vision_encoder, mesh=mesh, mp_policy=mp)
    17. for layer in model.llm.layers:
    18. fully_shard(layer, mesh=mesh, mp_policy=mp)
    19. fully_shard(model.llm, mesh=mesh, mp_policy=mp)
    20. fully_shard(model, mesh=mesh, mp_policy=mp)
    21. # Allocate uninitialized memory on the GPU, then fill weights from checkpoint
    22. model.to_empty(device="cuda")
    23. return model

    There are a few areas in this example that need adjustment based on actual conditions. When the vision encoder has fewer parameters, sharding it separately may yield minimal benefits. It can be combined with an adjacent module for sharding to reduce communication overhead. The last layer typically does not need to release its parameters immediately after the forward pass. You can set reshard_after_forward=False for that module, saving one parameter gather at the start of the backward pass. Whether to reshard and how to group modules should always be benchmarked against peak memory usage and throughput performance.