A Practical Guide to Training Trillion-Parameter MoE Models with FSDP2
This guide covers the practical workflow for training trillion-parameter Mixture-of-Experts (MoE) models with PyTorch FSDP2: version verification, memory estimation, communication estimation, a four-stage training process, monitoring metrics, and failure recovery. All numbers are illustrative estimates, not benchmark results—measure everything on your own cluster before deployment.
1. Scope and Boundaries
This article discusses general methods applicable to training scenarios where the total parameter count reaches the trillion scale, the model uses a sparse expert architecture (MoE), and the training framework is PyTorch FSDP2. All figures in this article are estimates and do not represent measured performance on any specific hardware or product. Measure on your own cluster before deployment.
The estimates cover only parameters and optimizer states. Activations, communication buffers, intermediate tensors from MoE routing, and memory fragmentation are outside the scope of static estimation. They all push up per-GPU peak memory and must be calibrated separately through actual measurements.
Before you start, confirm three things: the total parameter count and the number of parameters actually activated per token; the number of available devices and per-GPU memory capacity; and the versions of the training framework and communication library. These three answers determine every parameter choice that follows.
2. Version Verification
Version numbers should follow the official compatibility matrix of the framework you use. This article does not prescribe any specific version combination. Record each item during verification and compare it against the actual versions inside your cluster image.
| Item | How to Check | Common Risks |
|---|---|---|
| PyTorch and FSDP2 API | Confirm the fully_shard API is available and review the docs for your version |
Missing API or different behavior on older versions |
| CUDA runtime and driver | Compare torch.version.cuda against the driver’s supported range |
Driver too old to load kernels |
| NCCL version | Print the NCCL version at runtime and compare with the PyTorch-bundled version | Inconsistent collective behavior when multiple NCCL instances are loaded |
| Container image | Record the image digest, not just the tag | Tag overwritten with a different image, breaking reproducibility |
| Operator libraries | Verify that Attention, grouped GEMM, and similar libraries match your PyTorch version | Mismatched operator libraries cause errors or degraded performance |
| Python and dependencies | Freeze the dependency list (lock file) | Hidden differences caused by dependency drift |
3. Memory Estimation
3.1 Bytes per Parameter
Take 2.8 trillion total parameters as an example. In this article, TB is decimal: 1 TB equals 10^12 bytes. Converted to the binary unit TiB, the values are roughly 0.91 times the decimal figures.
A common mixed-precision training configuration looks like this:
| Item | Bytes per Parameter | Total for 2.8T Parameters |
|---|---|---|
| BF16 weights | 2 | 5.6 TB |
| FP32 master weights | 4 | 11.2 TB |
| Adam first moment (FP32) | 4 | 11.2 TB |
| Adam second moment (FP32) | 4 | 11.2 TB |
| BF16 gradients | 2 | 5.6 TB |
| Total | 16 | 44.8 TB |
If gradients are stored in FP32 instead, add 2 bytes per parameter for a total of 18 bytes, i.e., 50.4 TB. In FSDP2, this choice is typically expressed through param_dtype and reduce_dtype in MixedPrecisionPolicy; refer to the documentation of your version for the exact fields.
3.2 Global vs. Per-GPU Figures
44.8 TB is the global total; per-GPU usage must be computed separately. FSDP2 shards parameters and optimizer states across a sharding group. Per-GPU static usage can be estimated as:
- Global bytes = total parameters × bytes per parameter
- Per-GPU static usage ≈ global bytes ÷ S, where S is the number of ranks participating in sharding
- Per-GPU peak ≈ static usage + activation peak + communication buffers + MoE routing temporaries + fragmentation headroom
At 16 bytes per parameter, per-GPU static usage under different sharding sizes:
| Sharding ranks S | Per-GPU static usage (decimal GB) |
|---|---|
| 512 | 87.5 |
| 1024 | 43.75 |
| 2048 | 21.875 |
If expert parameters are sharded via expert parallelism (EP), the S for the expert portion should be the size of the corresponding sharding group. Compute the parts separately and then add them.
3.3 Total Parameters vs. Activated Parameters
The total parameter count determines the size of static state. The activated parameter count mainly determines the compute per token, as well as expert compute and all-to-all traffic in MoE layers. Do not mix the two: estimating memory from activated parameters underestimates static usage, and estimating compute from total parameters overestimates training time.
4. Communication Estimation
The following estimates are for order-of-magnitude reasoning only. They assume all parameters belong to the same sharding group; adjust for your actual expert sharding layout.
4.1 Parameter All-Gather and Gradient Reduce-Scatter
Per step, the volume received per GPU is approximately:
- Forward all-gather: P × b × (S − 1) ÷ S
- If resharding after forward (
reshard_after_forward), the backward pass performs another all-gather of approximately the same size - Gradient reduce-scatter: P × b_grad × (S − 1) ÷ S
Where P is the number of sharded parameters and b is bytes per parameter at compute precision. With 2.8T BF16 parameters and S = 1024, one full all-gather is about 5.6 TB; forward and backward each contribute one, and gradient reduce-scatter adds roughly another 5.6 TB—about 16.8 TB per step per rank in total.
This figure is a data volume. Converting it to time requires measured effective bandwidth: time ≈ data volume ÷ effective bandwidth.
4.2 MoE All-to-All
Each MoE layer performs two all-to-all operations (dispatch and combine) in the forward pass, and two more in the backward pass. The per-layer, per-step volume is approximately:
Bytes per transfer = T × k × d × b × (1 − 1/E_ep)
Where T is the number of tokens per micro-batch per GPU, k is the number of experts each token is routed to, d is the hidden dimension, b is activation-precision bytes, and E_ep is the expert-parallel group size. The per-step total is roughly this value multiplied by 4 and by the number of MoE layers.
Example (all parameters are hypothetical, for calculation only): T = 8192, k = 8, d = 7168, b = 2, E_ep = 8, 60 MoE layers. One transfer is about 0.94 GB; the per-step total is roughly 0.94 × 4 × 60 × 0.875 ≈ 197 GB. When expert load is imbalanced, ranks hosting hot experts see traffic above the average—monitor them separately.
5. Four-Stage Training Process
| Stage | Key Parameters | Typical Failure Symptoms | Rollback Point |
|---|---|---|---|
| 1. Load shards | Initialization method (e.g., meta-device construction), fully_shard wrapping granularity, distributed checkpoint loading |
OOM during initialization; state dict key mismatch; load errors after changing rank count | Last validated checkpoint plus original config file |
| 2. Forward resharding | reshard_after_forward, prefetch policy, micro-batch size, activation recomputation toggle |
Forward peak memory exceeded; all-gather timeouts | Reduce micro-batch; enable activation recomputation; revert to the previous config |
| 3. Backward gradient reduction | reduce_dtype, gradient accumulation steps, gradient clipping method |
NaN or Inf gradients; loss mismatch across ranks; stragglers slowing the job | Skip the step and log it; roll back to the previous checkpoint; lower the learning rate |
| 4. Optimizer update | Optimizer state sharding, learning rate schedule, checkpoint save interval | Loss spikes after resume; disk fills up during saving | Restore from the previous checkpoint; keep at least two checkpoint generations |
Gradient clipping on sharded parameters requires computing a global norm. Use an implementation compatible with sharding; do not clip independently on each rank.
6. Monitoring Metrics
| Metric | Collection Method | Initial Alert Threshold | Notes |
|---|---|---|---|
| HBM allocated | torch.cuda.memory_allocated or nvidia-smi |
Approaching 90% of per-GPU capacity | Reflects actual usage |
| Gap between HBM reserved and allocated | torch.cuda.memory_reserved |
Gap keeps widening | Indicates memory fragmentation |
| NCCL collective bandwidth | Profiler or NCCL debug logs | Drop >20% from baseline | Compare against Section 4 estimates |
| All-to-all latency | Corresponding kernel time in torch.profiler |
Rising share of step time | Signal of expert routing or network topology issues |
| Step time and tokens/s | Timing inside the training loop | Drop >10% from baseline | Overall performance indicator |
| Expert load imbalance | Ratio of max to mean tokens per expert | Ratio persistently above 1.5 | Check routing and load-balancing loss |
| Gradient norm | Logged every step | Sudden spikes or NaN | Early sign of numerical instability |
| Cross-rank step time gap | Ratio of max to median rank timing | Above 1.1 | Helps locate straggler nodes |
| Checkpoint save duration | Timing around the save | Noticeably longer than historical baseline | Signal of storage bottlenecks |
These alert thresholds are only a starting point—adjust them against your own cluster’s baseline.
7. Failure Recovery Steps
- Stop the job
