Training a Multimodal LLM with MoE on 16 GPUs Across Two Nodes with FSDP2
This article walks through a hands-on experiment using FSDP2 to shard parameters, gradients, and optimizer states when training a multimodal model—complete with MoE layers and a vision tower—on 16 accelerator cards across two servers. It covers the three main challenges of multimodal training, how to apply per-layer sharding with PyTorch's fully_shard API, practical ways to verify that vision-tower gradients actually flow, and the full experiment record: 400 optimizer updates with no numerical overflow or crashes. A checklist for reusing this setup in your own training jobs is included at the end.
Results First
On two servers with 16 accelerator cards in total, we used FSDP2 to shard parameters, gradients, and optimizer states to train a multimodal model containing MoE layers and a vision tower. The model completed 400 optimizer updates without numerical overflow or unexpected interruptions. These results come from a single internal experiment. The hardware model, compute precision, and communication library versions were not fully disclosed, so readers should treat the numbers as an order-of-magnitude reference rather than a benchmark that can be directly reproduced.
Three Challenges in Multimodal LLM Training
Training this type of model mainly involves three problems.
First, memory. The combined size of parameters, gradients, and optimizer states can exceed the capacity of a single card. With AdamW, the optimizer state is typically several times the parameter count, so even mid-sized models need to be sharded across devices.
Second, irregular communication caused by dynamic routing. In an MoE layer, which expert each token is assigned to depends on the input content. As a result, compute and communication loads are uneven across cards, and All-to-All communication can easily become a bottleneck.
Third, connecting the vision branch to the language branch. The feature dimensions and resolutions output by the vision encoder do not match the language model’s input requirements, so a projection module is needed for alignment. Gradients from the projection layer must backpropagate to the vision encoder; otherwise the vision tower is effectively not being trained at all.
Sharding Memory with FSDP2
FSDP2’s core idea is per-layer sharding. Before computing a layer, each card temporarily assembles the full parameters of that layer via all-gather. After the computation, these parameters are released. During backpropagation, gradients are distributed back to each card via reduce-scatter. Consequently, peak memory depends mainly on the sharding granularity, not just the number of cards.
PyTorch 2.6 and later provide a public fully_shard API. The following code is illustrative—the model class names and layer structure need to be replaced with your actual implementation and cannot be run as-is:
import torchfrom torch.distributed.fsdp import fully_shard, MixedPrecisionPolicy# Assume torch.distributed has been initialized via torchrunmodel = MultimodalMoEModel(...)mp_policy = MixedPrecisionPolicy(param_dtype=torch.bfloat16, # Parameter precision used in forward and backward passesreduce_dtype=torch.float32, # Higher precision for gradient reduction)# Shard layer by layer first, then the root module, so each layer performs all-gather independentlyfor layer in model.language_model.layers:fully_shard(layer, mp_policy=mp_policy)for block in model.vision_tower.blocks:fully_shard(block, mp_policy=mp_policy)fully_shard(model, mp_policy=mp_policy)# Create the optimizer after sharding, so the optimizer receives the sharded parametersoptimizer = torch.optim.AdamW(model.parameters(), lr=1e-5)
When putting this into practice, three parameters need to be decided:
- Sharding granularity: If the granularity is too coarse, the amount of data in a single all-gather grows, raising peak memory. If it is too fine, the number of communication rounds increases and throughput drops.
reshard_after_forward: When enabled, parameters are released as soon as the forward pass ends, saving memory, but the backward pass requires another all-gather. Keep it enabled when memory is tight; disable it when memory is plentiful to reduce communication.- Compute precision: bf16 computation combined with fp32 gradient reduction is a common configuration. The precision used in this experiment was not disclosed, so use your own configuration when reproducing.
Module Integration and Verifying Vision Gradients
Training a multimodal model requires four types of modules to share the same forward, backward, and parameter update pipeline.
- Attention operators: A hybrid attention structure needs FlashAttention or a similar efficient operator. Confirm that the operator supports the mask format you use, and check memory usage with long sequences.
- Dynamic routing: Top-K routing must ensure tokens are correctly assigned to their corresponding experts, and relies on efficient All-to-All communication.
- Vision encoding: The vision tower converts images into feature sequences. With a large parameter count, it needs to share the same sharding strategy as the language backbone.
- Cross-modal projection: This maps visual features into the language model’s semantic space, and gradients from the projection layer must flow back to the vision encoder.
A falling loss alone does not prove the vision branch is participating in training. Use the following three methods to verify:
- Gradient norms: Before each optimizer.step, iterate over the vision tower’s parameters and compute the L2 norm of their gradients. If the norm is consistently zero, or the gradient is None, gradients are not flowing back. For gradients of DTensor type, you can call
full_tensor()to consolidate them into a full tensor before computing. This step adds communication overhead, so enable it only during debugging. - Parameter deltas: Take snapshots of the vision tower’s parameters before and after a given step and compute the difference. If the difference is zero, the parameters were not updated.
- Frozen control: Freeze the vision tower in a short experiment and compare the loss curves of the two runs. Only if there is a measurable difference between the curves can you conclude the vision branch contributes to training.
Experiment Record: 16 GPUs Across Two Nodes
The table below lists the main configuration of this experiment.
| Item | Configuration |
|---|---|
| Nodes and cards | 2 servers, 16 accelerator cards in total |
| Hardware model | Not disclosed |
| Language backbone | 13 layers |
| Routing experts | 16 experts, Top-8 routing |
| Vision tower | 27 layers, with a cross-modal projection module |
| Global batch size | 16 |
| Sequence length | 1024 |
| Training data | Public image-text question-answering dataset |
| Training steps | 400 optimizer updates |
The main results are as follows.
- Stability: All 16 training processes completed 400 optimizer updates with no numerical overflow or unexpected interruptions.
- Training loss: The average over steps 21–70 was about 8.94; the average over the last 50 steps was about 2.18.
- Validation loss: About 6.53 at step 100 and about 4.69 at step 400. Validation was only recorded at these two steps, so the trend in between cannot be observed.
- Peak memory: Device sampling showed a peak of about 44.8 GiB. The sampling interval and measurement method were not documented.
These numbers show that both training loss and validation loss decrease with training steps, indicating the model is learning from this data. However, 400 steps is not enough to judge generalization. To evaluate generalization, validation frequency should be increased and the experiment repeated on larger datasets.
Risks and Troubleshooting in Production
- Out-of-memory (OOM): First estimate the amount of data in a single-layer all-gather, then check activation memory usage. Enabling activation checkpointing lowers peak memory but increases compute time.
- Communication timeouts: Check the NCCL version and network adapter configuration, and test all-reduce bandwidth node by node. When problems occur, enable NCCL debug logging to locate the specific link.
- Data throughput: Log the time spent waiting for data at each step. If waiting time accounts for a large share, data loading is the bottleneck.
- Interruption recovery: Use distributed checkpoints to save sharded state. Before restoring, confirm that the card count and parallelism configuration match those at save time; otherwise, verify that your version supports resharded loading.
- Data backup: Back up training data and checkpoints separately, and regularly verify that backup files can be read correctly.
Reuse Checklist
- Before training, record the PyTorch version, NCCL version, and hardware model, and save them alongside the experiment logs.
- Set the sharding granularity per layer, and log peak memory at each stage after launch.
- Check the vision tower’s gradient norms at fixed intervals; stop and investigate immediately if the gradient is zero.
- After an interruption, restore from a distributed checkpoint and compare the loss before and after to confirm continuity.
