01-data-parallelism-gradient-averaging implemented the idea of data parallelism, but PyTorch actually ships two different APIs for it — torch.nn.DataParallel (DP) and torch.nn.parallel.DistributedDataParallel (DDP) — and PyTorch's own documentation is explicit that DDP should always be preferred. This question quantifies exactly why, in terms of real communication volume and memory imbalance, rather than just citing the recommendation.
Implement dp_communication_volume, ddp_communication_volume, dp_gpu0_memory_multiplier, ddp_gpu0_memory_multiplier, and communication_reduction_factor, quantifying the two APIs' genuinely different communication and memory-imbalance costs.
dp_communication_volume(model_size_bytes, num_gpus) returns 2 * (num_gpus - 1) * model_size_bytes (0.0 when num_gpus <= 1) — DP re-broadcasts the model and gathers outputs to GPU0 on every single forward pass.ddp_communication_volume(model_size_bytes, num_gpus) returns 2 * (num_gpus - 1) / num_gpus * model_size_bytes (0.0 when num_gpus <= 1) — the standard ring all-reduce volume for one gradient sync.dp_gpu0_memory_multiplier(num_gpus) returns float(num_gpus); ddp_gpu0_memory_multiplier(num_gpus) returns 1.0 regardless of num_gpus.communication_reduction_factor returns dp_communication_volume / ddp_communication_volume (1.0 when num_gpus <= 1), and must depend only on num_gpus, not on the actual model_size_bytes passed in.Open one at a time. Each gives away a little more than the last.
DP has no persistent state on the other GPUs — it treats them as scratch compute, re-copying the model to them every forward pass. DDP sets each GPU up once, so the only thing that has to travel between GPUs afterward is the (much smaller, and already needed-anyway) gradient.
communication_reduction_factor simplifies to exactly num_gpus — the model_size_bytes and 2 * (num_gpus - 1) terms cancel out of the ratio algebraically. If your implementation's result changes when you change model_size_bytes, something's wrong.
No results yet
Run your code to check it against the examples, or submit to run every test.