Stable FSDP Training on Dual-Node Multi-GPU Setups: A Practical Guide
This article explains the key differences between inference adaptation and training adaptation for large language models, and provides a practical guide to stabilizing Fully Sharded Data Parallel (FSDP) training on dual-node, multi-GPU configurations. It covers the operational mechanics of FSDP, correct gradient accumulation and clipping sequences, and memory estimation formulas for large-scale models.
Inference Adaptation vs. Training Adaptation
The parameter counts of large language models are growing rapidly, and models with trillions of parameters are already being discussed in industry circles. These models place high demands on chips, drivers, communication libraries, and training frameworks. Many teams evaluating new hardware begin by checking whether it can quickly deploy the latest open-source models. However, running inference successfully does not mean training will also be stable. This article explains the difference and outlines the critical areas to check in dual-node, multi-GPU training.
Inference adaptation
Inference adaptation primarily validates forward computation. Engineers need to confirm that operators execute correctly on the target hardware, that output precision matches the reference implementation, and that throughput and latency meet expectations. Inference does not require saving gradients or optimizer states, so memory pressure is relatively low.
Training adaptation
Training adaptation demands much more. Forward propagation, backward propagation, gradient synchronization, and optimizer updates must all remain correct during long-running sessions. A training run often lasts for days or even weeks, and any intermittent error in any stage can waste enormous compute. Therefore, training adaptation typically requires verifying the following:
- Gradients from backward propagation match the reference implementation.
- Collective communication among GPUs (e.g., All-Gather, Reduce-Scatter) is stable.
- Loss and gradient norms remain within finite bounds throughout extended training.
- Training curves remain continuous after saving and restoring checkpoints.
How FSDP Works
Fully Sharded Data Parallel (FSDP) shards model parameters, gradients, and optimizer states across GPUs, with each GPU holding only its assigned shard. When computing a given layer, the full parameters for that layer are temporarily gathered to the current GPU, and then released after computation completes.
A single training step proceeds as follows:
- Forward pass: Each layer performs an All-Gather to collect the full parameters, completes the computation, and then releases non-local shards to reduce peak memory.
- Backward pass: All-Gather is again performed on demand to collect parameters. Local gradients are computed, then Reduce-Scatter sums gradients across GPUs and shards them so each GPU retains only its portion.
- Parameter update: Each GPU updates only its local shards of parameters, gradients, and optimizer states.
These communication steps are automatically scheduled by the framework. Users typically only need to wrap the model with FSDP; there is no need to manually call parameter-gathering or release functions after each layer.
The following code shows the correct ordering of gradient accumulation, gradient clipping, and optimizer updates. It assumes the model’s forward method directly returns the loss value; check your framework’s documentation for the exact API.
import contextlibimport torchdef train_step(fsdp_model, optimizer, micro_batches, max_norm=1.0):optimizer.zero_grad(set_to_none=True)num_micro = len(micro_batches)loss_value = 0.0for i, batch in enumerate(micro_batches):is_last = (i == num_micro - 1)# Skip gradient synchronization for all micro-batches except the last one.# no_sync only reduces communication; it does not change parameter sharding.sync_ctx = contextlib.nullcontext() if is_last else fsdp_model.no_sync()with sync_ctx:loss = fsdp_model(batch) / num_microloss.backward()loss_value += loss.item()# Gradient clipping provided by FSDP computes the global gradient norm across all shardsgrad_norm = fsdp_model.clip_grad_norm_(max_norm)if not torch.isfinite(grad_norm):raise RuntimeError("Gradient norm is NaN or Inf; skipping this step")optimizer.step()return loss_value, grad_norm.item()
Note that no_sync does not replace FSDP’s sharded communication. If the last micro-batch also uses no_sync, gradients will not be correctly reduced across GPUs, and model replicas will gradually diverge on different devices.
Memory Estimation
Estimate memory usage before training to determine whether a configuration is feasible. For mixed-precision training with the Adam optimizer, each parameter requires approximately 16 bytes, including the low-precision parameter, low-precision gradient, FP32 master weight, first moment, and second variance. The estimation formula is:
Single-GPU training state memory ≈ parameter count × 16 bytes ÷ number of sharded GPUs
For a 300-billion-parameter model, total training state is approximately 4.8 TB. With 16 GPUs doing full sharding, each GPU holds roughly 300 GB, which exceeds the memory capacity of most single GPUs. This scenario requires CPU offloading or more GPUs. For a 3-trillion-parameter model, total state is about 48 TB, and sharding across 16 GPUs gives roughly 3 TB per GPU, which is clearly infeasible.
This formula only accounts for training state
