Large Model Training Adaptation: A Full-Link Practice Analysis of Sharded Parallelism
Training multi-trillion parameter multimodal models has become a core challenge in large model engineering. Adapting these models to target compute hardware requires a stable pipeline spanning model loading, parameter sharding, gradient synchronization, and optimizer updates. This article deconstructs this pipeline into reusable technical stages, detailing the principles, potential risks, and verification methods for each phase.
Recently, multi-modal large models with parameter scales reaching trillions have been open-sourced. Training such models has become a core challenge in large model engineering. Completing training adaptation on target compute hardware means ensuring the entire pipeline—from model loading, parameter sharding, and gradient synchronization to optimizer updates—runs stably. This article does not focus on a specific product but breaks down this pipeline into reusable technical stages, explaining the principles, risk points, and verification methods for each stage.
1. Problem Definition: Why Sharding is Essential for Super-Large Model Training
The memory of a single accelerator cannot accommodate all the states of a super-large model. Taking common mixed-precision training as an example, each parameter typically corresponds to three types of data: low-precision parameters (2 bytes), low-precision gradients (2 bytes), and optimizer states (including high-precision master weights, first-order momentum, and second-order momentum, totaling about 12 bytes). Together, these amount to approximately 16 bytes per parameter.
For a 2.8-trillion parameter model, the model states alone require about 44.8 TB (2.8 x 10^12 x 16 bytes). If evenly sharded across 1,024 accelerators, each card bears about 43.75 GB, not accounting for activations, communication buffers, and memory fragmentation. Thus, sharding is a prerequisite for training such models, not an optional performance optimization.
2. Overall Pipeline: From Loading to Convergence
The complete sharded training pipeline can be divided into six stages:
- Model Construction and Loading: Delay the initialization of the model structure on a meta device (without occupying actual memory), and read weights block by block according to sharding rules.
- Sharding Scheduling: Determine which devices hold each parameter block and where to temporarily gather complete parameters during forward computation.
- Forward and Backward Computation: Gather parameters, complete computations, and release them immediately to control peak memory.
- Gradient Synchronization: After backward computation, reduce gradients to their respective shards.
- Optimizer Update: Each device only updates the parameters and optimizer states it holds.
- Checkpoint and Recovery: Save states by shard and support resharding when the number of devices changes.
Errors in any stage will manifest as numerical anomalies or memory issues in subsequent stages. Therefore, each stage must be verified individually, rather than solely relying on the final loss curve.
3. Working Principles of Fully Sharded Data Parallel
Fully Sharded Data Parallel (FSDP) is a widely used sharding strategy. Its core idea is that each data-parallel process only permanently holds a slice of the parameters, gradients, and optimizer states.
The execution flow is as follows:
- Forward Phase: Before computing a specific module, use an AllGather operation to temporarily assemble the complete parameters of that module. After computation, immediately release the non-local parts.
- Backward Phase: Similarly, gather the parameters first. After computing local gradients, use a ReduceScatter operation to scatter the gradients back to the shards of each device.
- Update Phase: The optimizer directly executes updates on the local shard, requiring no full synchronization again.
The cost of this approach is increased communication. Traditional data parallelism requires only one AllReduce during the backward pass (equivalent to ReduceScatter plus AllGather, or twice the data volume). The FSDP approach requires an AllGather during both the forward and backward passes, plus a ReduceScatter during the backward pass, totaling about three times the data volume. Theoretically, the communication volume is about 1.5 times that of the traditional method. Engineering-wise, this overhead needs to be offset by overlapping communication with computation.
Below is the illustrative code for the main training loop. The function names are placeholders used to illustrate the structure and do not correspond to any specific API:
# Illustrative example
