FSDP2 Multi-Node Training Adaptation: Memory, Communication, and Stability
This article explains what changes when a platform moves from inference support to full training support for large language and multimodal models. It covers per-parameter memory accounting for BF16 mixed precision with Adam, the conditions needed for comparable memory estimates, how FSDP2 shards parameters, gradients, and optimizer states and communicates during forward and backward passes, and how BF16 and gradient clipping affect numerical stability. It also discusses prefetch, communication bucketing, and practical checks for training readiness.
Large language models and multimodal models have reached the trillion-parameter scale, and the demands on compute platforms have risen accordingly. Industry discussions of compute adaptation often stop at inference and deployment. Being able to run inference only shows that forward computation and basic operators are usable. Testing a platform’s training capability requires checking whether it can support the full training loop: backward propagation, optimizer updates, cross-GPU gradient synchronization, and state consistency during weeks of stable operation.
1. What Training Adaptation Requires Beyond Inference Adaptation
Inference adaptation usually means that on the day a new model is released, the platform can load the weights and complete forward computation. It mainly verifies operator coverage and forward accuracy. Training adaptation has higher requirements. The platform must simultaneously support backward propagation, optimizer state updates, and multi-GPU gradient synchronization, and keep these processes numerically stable over long runs.
When evaluating training capability, check the following items one by one:
- Backward propagation runs completely, and gradients match the reference implementation.
- Optimizer states update step by step, and learning rate scheduling and weight decay take effect as expected.
- Multi-GPU gradient synchronization is correct, and results align with a single-GPU or small-scale baseline.
- After an interruption, training can resume from a checkpoint, and the loss curve after recovery is continuous with the curve before the interruption.
A report that only shows inference results cannot prove that pretraining or continued training will run end to end.
2. Memory Estimation: Do the Accounting First, Then Decide on Sharding
During training, model weights are not the only thing on the GPU. With common BF16 mixed precision plus Adam, each parameter takes about 16 bytes: 2 bytes for the BF16 weight, 2 bytes for the BF16 gradient, 4 bytes for the FP32 master weight, and 4 bytes each for the Adam first moment and second moment.
For 3 trillion parameters, the calculation is as follows (1 TB is counted as 10^12 bytes):
| Item | Calculation | Result |
|---|---|---|
| BF16 weights | 3×10^12 × 2 bytes | 6 TB |
| BF16 gradients | 3×10^12 × 2 bytes | 6 TB |
| FP32 master weights | 3×10^12 × 4 bytes | 12 TB |
| Adam first moment | 3×10^12 × 4 bytes | 12 TB |
| Adam second moment | 3×10^12 × 4 bytes | 12 TB |
| Total | 48 TB |
Two machines with 16 80GB GPUs in total provide about 1.28 TB of GPU memory, which cannot even hold the training state. Even if the 48 TB were perfectly sharded across 80GB GPUs, about 600 GPUs would be required, and this does not include activations.
The 48 TB is a lower bound, not the actual requirement. Actual GPU memory also includes three parts: activations, which grow with batch size and sequence length; communication buffers and temporary buffers; and memory fragmentation. Activation recomputation can lower the activation peak but increases computation.
For an estimate to be usable for comparing platforms, the following six conditions must be stated clearly:
| Condition | Example Value | Impact on the Estimate |
|---|---|---|
| Parameter scale | 3×10^12 | Determines the base size of the training state |
| Weight precision | BF16 | Determines the number of bytes for weights and gradients |
| Gradient precision | BF16 or FP32 | Affects gradient caching and reduction overhead |
| Optimizer type | Adam (two FP32 states) | Determines optimizer state memory |
| Activation upper bound | Batch size times sequence length | Determines the activation peak; must be paired with whether recomputation is used |
| GPU count and per-GPU available memory | For example, 16 × 80GB | Determines whether sharded states fit |
Only when all six conditions are specified does the estimate become comparable.
Sharding is a prerequisite for large-model training. FSDP2 (Fully Sharded Data Parallel 2) shards parameters, gradients, and optimizer states across all participating GPUs. Each GPU holds only its own shard for the long term.
3. FSDP2 Forward and Backward Communication
FSDP2 manages parameters module by module; usually one Transformer layer is one module. During computation, each module temporarily gathers the full weights, releases them after computation, and keeps only the local shard. Therefore, the memory peak depends on the full parameter size of a single module and does not grow linearly with the entire model.
| Stage | Operation | Full Weights Present on Local GPU | Notes |
|---|---|---|---|
| Forward, layer i | All-Gather weights | Present | Shards from all GPUs are assembled into full weights |
| Forward computation | Compute the layer output | Held | Full weights exist only while the layer is in use |
| End of forward | Release full weights | Not held | Keep only the local shard |
| Backward, layer i | All-Gather again | Present | Required when reshard_after_forward=True |
| Backward computation | Compute local gradients on the GPU | Held | The results are local per-GPU results |
| Backward reduction | Reduce-Scatter | Held until the end of this step | Gradients are summed or averaged and then sharded back to each GPU |
| End of backward | Release full weights | Not held | The gradient shard remains on the GPU |
Reduce-Scatter applies only to gradients. The parameters themselves are not reduced.
The following is the basic FSDP2 wrapping approach. The code needs to be used with a real model, data loading, and optimizer.
import torchfrom torch.distributed.fsdp import fully_shard, MixedPrecisionPolicymp_policy = MixedPrecisionPolicy(param_dtype=torch.bfloat16, # Use BF16 for forward and backward computationreduce_dtype=torch.float32, # Use FP32 for gradient reduction to reduce accumulation error)for layer in model.layers:fully_shard(layer, mp_policy=mp_policy, reshard_after_forward=True)fully_shard(model, mp_policy=mp_policy)
reshard_after_forward=True can lower the memory peak, but it adds one more All-Gather during backward. False reduces communication, at the cost of retaining full weights after forward and using more memory. Both settings must be tested on the target network.
Communication and computation must overlap to reduce waiting. Prefetch is a common method: while computing layer N, launch the All-Gather for layer N+1 in the background. The more layers prefetched, the more complete the overlap, but the more full weights exist at the same time. For the specific prefetch interface, refer to the official documentation for the PyTorch version in use.
Communication bucketing determines how much data is transferred in each communication operation. If the bucket is too small, startup overhead becomes a larger share. If the bucket is too large, the system must wait for more parameters to be ready, leaving less room for overlap and increasing peak memory. There is no universal bucket size; it must be measured on the target network.
4. BF16 and Gradient Clipping
BF16 has the same exponent bits as FP32, so its numerical range is the same as FP32. Compared with FP16, BF16 usually does not require dynamic loss scaling to avoid overflow. However, it has fewer mantissa bits and lower precision than FP32, so keeping master weights in FP32 and using FP32 for gradient reduction can reduce accumulation error.
Gradient clipping is used to suppress loss spikes. A common practice is to call torch.nn.utils.clip_grad_norm_ before the optimizer update. There is no universal value for max_norm. First record the gradient norm distribution during normal training in small-scale experiments, and then set the threshold.
