Trillion-Parameter Model Training with Fully Sharded Data Parallelism: A Practical Guide
This article discusses how to adapt training systems for trillion-parameter models using fully sharded data parallelism. It explains the memory pressure created by model weights, gradients, and optimizer states; describes the all-gather and reduce-scatter workflow for per-layer execution; and examines model loading, shard scheduling, and gradient synchronization. It also covers special considerations for multimodal training and hardware-software co-adaptation checkpoints.
1. Background: Training Challenges at Trillion-Parameter Scale
Recently, an AI company open-sourced a flagship model with 2.8 trillion parameters. Each order-of-magnitude increase in model size changes the problems that the training system must solve. For dense models, training requires holding three types of state at the same time: model weights, gradients, and optimizer states. In common mixed-precision training, each parameter corresponds to roughly 16 bytes of storage overhead. These static states alone exceed 40 TB, not including activations produced during forward computation. No single accelerator card has enough memory to hold this data, so the training system must partition and distribute it across many devices.
Therefore, the core work of adapting large-model training comes down to three things:
- Let each card hold only a portion of the states, reducing per-card memory usage.
- Deliver the data required for computation on time, avoiding long periods of idle devices.
- Keep gradients across cards numerically consistent so that training results are reproducible.
Multimodal models add further complexity. Encoders for images, audio, and other modalities differ greatly from the language backbone in structure, parameter count, and compute volume, so the training pipeline must accommodate these modules at the same time.
2. How Fully Sharded Data Parallelism Works
Fully sharded data parallelism is a widely adopted approach in the industry. Its core idea is that each card permanently stores only one shard of the model parameters, gradients, and optimizer states. When computing a given layer, the cards temporarily aggregate the full parameters of that layer; after computation, they immediately release the non-local portions.
The forward and backward process for a single layer can be broken down into the following steps:
- Before forward computation, the cards all-gather the full parameters of the layer.
- Execute the forward computation for the layer to obtain activations.
- After forward computation, release non-local shards to lower peak memory.
- During backward propagation, gather the parameters again, compute local gradients, and use reduce-scatter to split the gradients back into the shards owned by each card.
The illustrative code below shows the execution flow for a single layer. Error handling and optimization details are omitted, and the collective communication primitives are assumed to be provided by the underlying system:
# Illustrative code: single-layer fully sharded execution flow, not a complete implementationdef forward_with_sharding(layer, inputs, shard_group):# Temporarily assemble the full parameters for this layerfull_params = all_gather(layer.local_shard, group=shard_group)layer.load_params(full_params)outputs = layer.compute(inputs)# Keep only the local shard and release the restlayer.release_params(keep=layer.local_shard)return outputsdef backward_with_sharding(layer, grad_outputs, shard_group):full_params = all_gather(layer.local_shard, group=shard_group)layer.load_params(full_params)local_full_grads = layer.backward(grad_outputs)# Reduce and split gradients back into the shard owned by each cardgrad_shard = reduce_scatter(local_full_grads, group=shard_group)layer.release_params(keep=layer.local_shard)return grad_shard
This approach trades communication for memory. Each layer requires one parameter all-gather in forward and one in backward, so communication volume grows linearly with parameter scale. Therefore, the degree of communication-computation overlap, the parameter prefetching strategy, and the choice of shard granularity directly determine training throughput.
3. Three Stages of the Training Pipeline
3.1 Model Loading
The goal of model loading is to avoid each process holding a complete model while also avoiding disk-read congestion during startup. In practice, the following methods are commonly used:
- Lazy initialization: Build the model structure logically first without allocating real storage immediately, and allocate storage after the sharding plan is determined.
- Sharded reading: Each process reads only the weight ranges belonging to its own shard, avoiding hundreds of processes reading complete weight files at the same time.
- Checkpoint mapping: Record the correspondence between shards and global parameter names when saving, so training can resume with a different number of cards.
Storage bandwidth during startup is often overlooked. If all processes issue many read requests simultaneously, loading time can far exceed the per-step time of actual training, so read requests need to be staggered or grouped.
3.2 Shard Scheduling
Shard scheduling determines when and at what granularity data moves between devices. Key decisions include:
- Shard granularity: Usually divided by Transformer layer. If the granularity is too coarse, the parameter count aggregated in a single operation is large and peak memory rises; if it is too fine, the number of communication operations increases and the fixed overhead of small messages becomes a larger share.
- Parameter prefetching: While computing layer i, start collecting parameters for layer i+1 in advance. A prefetch window that is too large consumes extra memory; one that is too small cannot hide communication latency.
- Communication-computation overlap: Place communication operations in independent execution streams and advance them in parallel with computation.
- Small-parameter merging: Normalization layers and other small parameters are numerous, and communicating them one by one is inefficient. Merging them into a single communication reduces startup overhead.
3.3 Gradient Synchronization
Gradient synchronization is critical for numerical correctness and mainly involves the following issues:
- Global gradient clipping: Clipping depends on the global gradient norm. Because gradients have been sharded, the norm must first be computed as a local sum of squares and then reduced across shards to obtain the global value. If this step is missed, clipping ratios will differ across cards.
- Gradient accumulation: Accumulate gradients over multiple micro-batches and synchronize only in the final step, reducing synchronization frequency.
- Precision control: Low-precision gradients can easily lose small-value information during accumulation. A common practice is to use a higher-precision accumulation buffer and convert back to storage precision after synchronization.
- Synchronization validation: After key steps, check gradient statistics for each shard to detect communication errors or data misalignment as early as possible.
4. Special Issues in Multimodal Training
On top of single-modality approaches, multimodal training also needs to handle the following issues:
- Grouped sharding: Encoders and the backbone differ greatly in parameter scale. If they are sharded uniformly, communication overhead for small modules accounts for too high a proportion. A better approach is to divide shard groups by module and schedule each group independently.
- Load balancing: Image-text and audio-video samples vary widely in sequence length, so compute loads across cards are uneven, and slow cards delay the entire synchronization step. Length-based bucketing or dynamic batching can keep per-step compute volume close.
- Frozen module handling: When some modules are frozen during training, confirm that their parameters do not participate in gradient reduction. Otherwise, bandwidth is wasted and errors may be introduced because gradients are empty.
- Data pipeline: Decoding and preprocessing for multimodal data are expensive and should run asynchronously with training computation to avoid data supply becoming a bottleneck.
5. Hardware-Software Co-Adaptation
Training adaptation is not only a framework-level task; it also depends on coordination between acceleration hardware and the software stack. Key checkpoints include:
- Operator coverage: Inventory all operators used during training and confirm that the target acceleration platform has a corresponding implementation for each. For missing operators, write replacement implementations and compare their numerical results item by item against reference results.
- Communication topology: Intra-node and inter-node communication paths should be examined.
