Question about distributed data parallel

Jack_Shi: <p>I am using <code>torch.nn.parallel.DistributedDataParallel</code> to parallelize the training loop in a one-node multiple GPUs setting. When I’m just using <code>torch.nn.parallel.DistributedDataParallel</code> to wrap the model, I would get the following error</p>
<blockquote>
<p>RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one.<br>
This error indicates that your module has parameters that were not used in producing loss.<br>
You can enable unused parameter detection by passing the keyword argument<br>
<code>find_unused_parameters=True</code> to <code>torch.nn.parallel.DistributedDataParallel</code>,<br>
and by making sure all <code>forward</code> function outputs participate in calculating<br>
loss.  If you already have done the above, then the distributed data parallel<br>
module wasn’t able to locate the output tensors in the return value of your module’s <code>forward</code> function. Please include the loss function and the structure of the return value of <code>forward</code> of your module when reporting this issue (e.g. list, dict, iterable).<br>
Parameter indices which did not receive grad for rank 2: 0 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56<br>
In addition, you can set the environment variable TORCH_DISTRIBUTED_DEBUG to either INFO or DETAIL to print out information about which particular parameters did not receive gradient on this rank as part of this error</p>
</blockquote>
<p>However, once I turn the <code>find_unused_parameters=True</code> as suggested in the error message, the error goes away. This concerns me a bit as if I’m understanding correctly, <code>find_unused_parameters=True</code> is only supposed to show me variables that were not used for calculating the final loss (which I do not think there are any), and not supposed to “fix” anything. Does anyone have any idea why the wrapper behaves this way?</p>

Jack_Shi: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> do you have any idea why <code>torch.nn.parallel.DistributedDataParallel</code> behaved this way?</p>

ptrblck: <aside class="quote no-group" data-username="Jack_Shi" data-post="1" data-topic="169370">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jack_shi/48/26592_2.png" class="avatar"> Jack_Shi:</div>
<blockquote>
<p><code>find_unused_parameters=True</code> is only supposed to show me variables that were not used for calculating the final loss (which I do not think there are any), and not supposed to “fix” anything.</p>
</blockquote>
</aside>
<p>That’s not the case, as this argument will trigger an additional pass through the computation graph and mark unused parameters as ready to be reduced.<br>
From the <a href="https://pytorch.org/docs/stable/generated/torch.nn.parallel.DistributedDataParallel.html?highlight=find_unused_parameters">docs</a>:</p>
<blockquote>
<p><strong>find_unused_parameters</strong> (<a href="https://docs.python.org/3/library/functions.html#bool"><em>bool</em></a>) – Traverse the autograd graph from all tensors contained in the return value of the wrapped module’s <code>forward</code> function. Parameters that don’t receive gradients as part of this graph are preemptively marked as being ready to be reduced. In addition, parameters that may have been used in the wrapped module’s <code>forward</code> function but were not part of loss computation and thus would also not receive gradients are preemptively marked as ready to be reduced. (default: <code>False</code>)</p>
</blockquote>
<p>Based on the behavior, you should check why these parameters are not used in <code>rank2</code> as it doesn’t seem to be expected.</p>

Jack_Shi: <p>I see. Thank you so much again for your help, I should have read the doc a bit more carefully!</p>