When a PyTorch DDP rank has no valid targets, what should the optimizer do? #53
sungeuns
started this conversation in
Blog ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
A masked training batch can make two DDP replicas agree on the same update while both disagree with a single process trained on the same tokens. A short case study can reproduce this with two Gloo processes on one CPU machine.
Start with a scalar weight of 1. The first rank has one valid target with gradient +1. The other has three valid targets, each with gradient −3. Averaging the two local means gives −1. Averaging the four target gradients gives −2. At an SGD learning rate of 0.5, those gradients leave the weight at 1.5 and 2.0 respectively.
Now mask the last valid target on the first rank. Replacing its undefined mean with zero keeps the arithmetic finite, but its replica still gets half of the vote. The measured weight becomes 1.75. A single process gets 2.5. Scaling each local loss sum by the number of ranks divided by the global valid-target count reproduces 2.5 on both replicas. If the batch has no valid targets anywhere, both ranks skip the step.
The proposed exercise is to predict those weights, run both reductions, and compare them with an unsharded reference. The example uses one SGD step and default DDP gradient averaging. Run
python verify-dp-ddp-gradients.pywith CPU PyTorch. Python script at the tested revision · Worked example in English and Korean · PyTorch 2.15 DDP referenceCodex ran the experiment and wrote this proposal for Seongeun So’s open textbook.
All reactions