FSDP2 Multimodal Training on Domestic Accelerators: Key Adaptation Considerations
This article examines the practical adaptations required for FSDP2-based multimodal large model training on domestic AI accelerators. It explains why sharding is mandatory for trillion-scale models, details the per-parameter sharding mechanism of FSDP2, and walks through critical aspects of the training pipeline including meta-device initialization, sharding granularity, communication overlapping via CUDA streams, and multimodal data loading. The article also covers software stack coordination, performance tuning strategies such as HSDP evaluation and activation recomputation, and checkpointing for fault tolerance—offering engineers a focused reference for porting large-scale training workloads to non-CUDA hardware backends.
Why Sharding Is Mandatory in Large Model Training
Consider a 2.8-trillion-parameter model. Assuming BF16 weights and gradients, FP32 master weights, and two FP32 Adam optimizer states, each parameter accounts for roughly 16 bytes. Total model states amount to approximately 44.8 TB for this configuration alone, excluding activations, communication buffers, and memory fragmentation. Since a single accelerator typically provides tens of gigabytes of on-device memory, model states must be sharded across many devices. With a sharding group size of 1024, each device holds about 43.75 GB of model states—still leaving room needed for activations and temporary buffers.
Once sharded, both forward and backward passes require frequent parameter gathering and gradient synchronization. All-reduce and all-gather times grow with cluster size and message volume. If communication overlaps poorly with computation, it quickly becomes the throughput bottleneck. Multimodal models compound this challenge: encoder parameters for text, images, and video differ in shapes and activation scales, so a fixed sharding configuration cannot be applied uniformly across all submodules.
The Sharding Mechanism of FSDP2
FSDP2 uses fully_shard to shard modules. Each parameter is individually split along dimension 0 into N partitions, and each rank keeps only its local shard, represented as a DTensor. FSDP1 flattens parameter groups into a single one-dimensional tensor; FSDP2 preserves per-parameter shapes. This keeps parameter-to-module mapping intact, simplifying module-level sharding strategies, partial parameter freezing, and state dictionary handling.
During the forward pass, FSDP2 performs an all-gather for the current module to temporarily materialize the full parameters locally. After computation, if reshard_after_forward is True, non-local shards are released. Steady-state memory stays near 1/N of the full model, while peak memory still includes the current module’s full parameters and the prefetched next module’s parameters. Setting this flag to False retains full parameters after the forward pass, saving one all-gather during backward at the cost of higher memory usage—behaving closer to ZeRO-2.
During backward, gradients are reduced via reduce-scatter, keeping only the local shard per parameter. Gradient memory therefore also drops to roughly 1/N.
| Approach | Sharded Objects | Parameter Representation | Use Case |
|---|---|---|---|
| FSDP1 | Parameters, gradients, optimizer states | Flattened 1D tensor | Maintaining existing FSDP1 codebases |
| FSDP2 | Parameters, gradients, optimizer states | Per-parameter sharding, DTensor | New training code; module-level control required |
| ZeRO-3 | Parameters, gradients, optimizer states | DeepSpeed-specific sharded implementation | Teams already invested in the DeepSpeed ecosystem |
During selection, verify that the target accelerator’s backend supports the collective communication primitives and DTensor paths used by FSDP2. Some operators or communication primitives may be missing; validate on a small scale first.
Critical Stages in the Training Pipeline
Meta-Device Initialization
A trillion-scale model cannot be fully initialized on a single device. The standard practice is to build the model structure on the meta device, where parameters only record shapes and dtypes without consuming memory. Submodules are wrapped with fully_shard bottom-up, followed by a final wrap of the root module. After sharding, parameters are materialized to the local device via to_empty, then loaded from sharded weights or randomly initialized. When resuming from a distributed checkpoint, each rank should directly read its own shard to prevent any device from materializing the full weights.
For multimodal models, the materialization order across submodules depends on inter-module dependencies and peak memory at each stage. It is recommended to record memory usage after materializing each module group and adjust the order accordingly.
Sharding Granularity and Graph Partitioning
The goal of sharding scheduling is to overlap all-gather and reduce-scatter time with computation time. Layers with large parameter counts and heavy computation—such as Transformer FFN blocks—fit well in their own sharding groups. Layers with low communication volume but frequent invocation, like normalization and embedding layers, can be merged into adjacent groups to reduce the number of communication launches.
Whether a grouping is appropriate needs to be confirmed through profiling. If a group’s all-gather time consistently exceeds its forward computation time, the overlap cannot be achieved and the grouping must be adjusted. Merging groups reduces communication counts but increases each aggregate message size and raises peak memory. For vision encoders where convolutions and attention layers differ significantly in structure, measure each layer type separately before deciding on granularity.
Gradient Synchronization and Communication Overlap
During backward propagation, once a module’s gradient is computed, its reduce-scatter can start immediately while earlier layers continue backward computation. Communication should be placed on a separate CUDA stream, with dependencies enforced through event synchronization. Confirm via profiler that reduce-scatter windows actually fall within computation windows.
In multimodal settings, separate communication groups can be created for vision and text branches—provided profiling shows that sharing one group causes blocking between them. All ranks must create process groups in the same order, or deadlocks can occur.
Multimodal Data Loading and Preprocessing
Text data can be tokenized offline and packed into fixed-length sequences. When packing, record document boundaries and mask attention across document segments. Image and video decoding and resizing should run in DataLoader worker processes. Normalization can be placed on the GPU to reduce CPU-to-GPU transfer. Prefetch counts and pinned memory settings should be tuned iteratively based on I/O and memory conditions, validated by throughput metrics.
Batch sizes can be configured independently per modality, but note one constraint: every rank must execute the same sequence of collective communication operations at each step. If one rank sees a different modality mix than another at a given step, communication mismatch can occur. Modality assignment per step should therefore be controlled uniformly during sampling.
Software Stack Coordination and Performance Tuning
Operator fusion. On custom software stacks, enable fused kernels for attention and FFN as a priority. Compare numerical outputs before and after enabling to confirm the loss curve stays within tolerance, then proceed to long-running training.
Precision strategy. Keep backbone parameters in BF16 while preserving FP32 for normalization and loss computation to balance throughput and numerical stability.
Communication topology. When cross-node bandwidth is significantly lower than intra-node bandwidth and all-gather time dominates the step time, evaluate HSDP—sharding within a node and replication across nodes. A persistent high communication time ratio is the starting signal for evaluating HSDP. Base the final decision on measured step time and memory footprint. HSDP combined with tensor parallelism (TP) and pipeline parallelism (PP) should be designed based on topology and available bandwidth.
Peak memory and fragmentation. Continuously track each rank’s allocated memory, reserved memory, and peak usage. The gap between allocated and reserved reflects fragmentation. If activations dominate peak memory, enable activation recomputation at the cost of one extra forward pass. If the framework supports extendable segment allocation, enable it and compare fragmentation rates.
Monitoring metrics. At minimum, record: step time, per-rank peak memory, all-gather and reduce-scatter durations, tokens/s, and loss. Keep these metrics on a single dashboard to simplify bottleneck identification.
Fault Tolerance and Checkpoint Resume
Periodic checkpointing is a mandatory component for trillion-parameter training. The save interval should be set based on the training workload, failure frequency, and the overhead of checkpoint I/O, so that the system can resume with minimal loss of progress when failures occur.
