Data Parallelism
PremiumUnderstanding communication primitives and DDP's gradient synchronization mechanism
Get code accessThe first step in training a large model is to get multiple GPUs working together. Data Parallelism is the most intuitive approach: each GPU holds a complete copy of the model, processes different data independently, and finally aggregates the gradients. In this chapter, we first understand the memory bottleneck of a single GPU, then learn the basics of multi-GPU communication, and finally dive into the implementation of DDP.
Memory Composition of Single-GPU Training
A 7B-parameter model needs only 14 GB for its fp16 weights (7B × 2 bytes), yet it cannot be trained on an 80GB A100. Where did all the memory go?
The answer is: during training, the GPU stores not only the parameters but also the gradients and optimizer states.
Memory Requirements of Mixed-Precision Training
Modern training commonly uses Mixed Precision: forward and backward passes are computed in fp16 (fast), but parameter updates use fp32 (accurate). Taking the Adam optimizer as an example, assume the model has parameters:
fp16 part (forward/backward):
- Parameters: bytes
- Gradients: bytes
fp32 part (optimizer):
- Parameter copy: bytes (used for accurate updates)
- First moment : bytes (exponential moving average of the gradient)
- Second moment : bytes (moving average of the squared gradient)
Total: bytes
| Component | Precision | Memory (bytes) |
|---|---|---|
| Parameters | fp16 | |
| Gradients | fp16 | |
| Parameter copy | fp32 | |
| First moment | fp32 | |
| Second moment | fp32 | |
| Total |
Why is an fp32 parameter copy needed?
During training, both forward and backward passes are computed in fp16 (fast and memory-efficient), but fp16 has only about 3 significant digits of precision. When the learning rate is very small, the update term in param += learning_rate × gradient may be so tiny that fp16 rounds it directly to zero, and the model stops learning. So Adam internally maintains an fp32-precision copy of the parameters, performs the update in fp32, and then converts the result back to fp16 for the next forward pass.
Why are and needed?
Adam does not simply "take a step along the gradient." It needs to maintain two cross-step historical statistics: (the exponential moving average of the gradient, equivalent to momentum) and (the moving average of the squared gradient, used to adaptively adjust the learning rate for each parameter). The gradient is "which way to go this step," while and are "the accumulated experience from all past steps."
Actual memory for a 7B model:
- Parameters:
14 GB - Gradients:
14 GB - Optimizer states:
84 GB(parameter copy 28GB + 28GB + 28GB) - Total: about 112 GB (not counting activations yet)
A single A100 has only 80 GB of memory, which cannot even hold the model's training state.
Training Memory per GPU
Mixed Precision + AdamLog in to continue reading
This is premium content. Please log in to access the full article.
CookLLM Docs