Why This Matters
Training modern LLMs and vision models requires thousands of GPUs working in concert. Distributed training is not just about speed - it's about enabling models that cannot fit on a single device. Understanding parallelism strategies, communication patterns, and fault tolerance is essential for building and operating large-scale ML infrastructure.
System Architecture Overview
Data Parallelism with FSDP
Fully Sharded Data Parallel (FSDP) shards model parameters, gradients, and optimizer states across GPUs. It uses an auto-wrap policy to determine which layers to shard (typically transformer layers), mixed precision with BF16 for forward/backward passes, and AllGather operations to reconstruct parameters only when needed. ZeRO Stage 1 shards optimizer states (4x reduction), Stage 2 adds gradient sharding (8x), and Stage 3 shards parameters (linear scaling with GPU count).
Pipeline Parallelism
Pipeline parallelism splits the model into sequential stages, each assigned to a different GPU. The 1F1B (one forward, one backward) schedule alternates forward and backward passes to minimize pipeline bubbles. During the warmup phase, forward passes fill the pipeline. In steady state, each stage alternates between forward and backward. The bubble ratio is (stages - 1) / micro_batches, which decreases with more micro-batches.
Gradient Accumulation
Gradient accumulation simulates larger batch sizes by summing gradients over multiple micro-batches before performing a weight update. Loss is scaled by 1/accumulation_steps before backward, gradients are clipped (max norm 1.0), and the optimizer steps only after all accumulation steps complete. The effective batch size equals per_device_batch x world_size x accumulation_steps.
Mixed Precision Training
Mixed precision uses BF16 or FP16 for forward/backward passes while maintaining FP32 master weights. BF16 has the same exponent range as FP32 and doesn't require loss scaling. FP16 requires GradScaler to prevent gradient underflow. Both achieve ~2x memory reduction and 1.5-3x speedup on modern GPUs with Tensor Core support.
Fault Tolerance
Checkpointing saves model state, optimizer state, epoch/step, and metrics to disk, with cleanup of old checkpoints (keeping last N). Elastic training handles worker failures by reloading from the latest checkpoint, redistributing data across remaining workers, and resuming training. For large-scale training, use async checkpoint writes to avoid blocking training.
Interview Q&A
Q1: When would you use tensor parallelism vs. pipeline parallelism?
Answer: Tensor parallelism splits individual layers across GPUs - best for single-node training with fast interconnects (NVLink). Pipeline parallelism splits layers into stages across GPUs - best for multi-node training where inter-node bandwidth is limited. In practice, use tensor parallelism within nodes and pipeline parallelism across nodes (3D parallelism).
Q2: What is the communication overhead of FSDP vs. DDP?
Answer: DDP broadcasts full parameters and AllReduces gradients - communication doubles with model size. FSDP shards parameters and only AllGathers when needed - communication scales with world size but reduces memory. For very large models, FSDP is necessary despite higher communication volume, since DDP can't fit the model.
Q3: How does gradient accumulation affect training?
Answer: Gradient accumulation simulates larger batch sizes by summing gradients over multiple micro-batches before updating weights. It's mathematically equivalent to using the larger batch (assuming no batch norm), but with lower memory usage. Common for fitting large effective batch sizes on limited GPU memory.
Q4: What are the trade-offs of BF16 vs. FP16 for mixed precision?
Answer: BF16 has the same range as FP32 (8 exponent bits), so it doesn't need loss scaling. FP16 has limited range (5 exponent bits) and requires loss scaling to prevent gradient underflow. BF16 is preferred on Ampere+ GPUs; FP16 works on older hardware with better Tensor Core support.
Q5: Design a fault-tolerant training system for 1000 GPUs.
Answer: Use: 1) Periodic async checkpointing to distributed storage, 2) Elastic training with auto-scaling (TorchElastic), 3) Preemption-aware scheduling (spot instances), 4) Graceful degradation (train on remaining GPUs), 5) Health monitoring per GPU, 6) Automatic bad-worker detection and exclusion, 7) Checkpoint resume with proper RNG state restoration.
Quiz
-
What does FSDP stand for?
- A) Fast Shared Data Parallel
- B) Fully Sharded Data Parallel
- C) Fault-tolerant Synchronized Data Processing
- D) Forward-Scattered Data Pipeline
-
Which ZeRO stage shards parameters?
- A) Stage 1
- B) Stage 2
- C) Stage 3
- D) None
-
What is a pipeline bubble?
- A) Network congestion
- B) GPU idle time in pipeline parallelism
- C) Memory overflow
- D) Gradient explosion
-
Why is BF16 preferred over FP16?
- A) Faster computation
- B) Larger dynamic range, no loss scaling needed
- C) Smaller memory footprint
- D) Better accuracy
-
What does AllReduce do?
- A) Reduces model size
- B) Aggregates gradients across all GPUs
- C) Splits data across nodes
- D) Optimizes learning rate
Answers: 1-B, 2-C, 3-B, 4-B, 5-B