DDP, parameters didn't receive gradients for used module

Alethia: <p>Hi,</p>
<p>I’m training a model called VQVAE using DDP, when I ran the code, there was an error message:</p>
<pre><code class="lang-auto">RuntimeError: Expected to have finished reduction in the prior iteration before starting a new one. This error indicates that your module has parameters that were not used in producing loss. You can enable unused parameter detection by passing the keyword argument `find_unused_parameters=True` to `torch.nn.parallel.DistributedDataParallel`, and by 
making sure all `forward` function outputs participate in calculating loss. 
If you already have done the above, then the distributed data parallel module wasn't able to locate the output tensors in the return value of your module's `forward` function. Please include the loss function and the structure of the return value of `forward` of your module when reporting this issue (e.g. list, dict, iterable).
Parameter indices which did not receive grad for rank 1: 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
 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
    return forward_call(*input, **kwargs)
</code></pre>
<p>I used the following code to detect unused parameters:</p>
<pre><code class="lang-python">for n, p in model.named_parameters():
    if p.grad is None:
        print(f'{n} has no grad')
</code></pre>
<p>The above code gives the following messages:</p>
<pre><code class="lang-auto">module.postnet.convolutions.0.0.conv.weight has no grad
module.postnet.convolutions.0.0.conv.bias has no grad
module.postnet.convolutions.0.1.weight has no grad
module.postnet.convolutions.0.1.bias has no grad
module.postnet.convolutions.1.0.conv.weight has no grad
module.postnet.convolutions.1.0.conv.bias has no grad
module.postnet.convolutions.1.1.weight has no grad
module.postnet.convolutions.1.1.bias has no grad
module.postnet.convolutions.2.0.conv.weight has no grad
module.postnet.convolutions.2.0.conv.bias has no grad
module.postnet.convolutions.2.1.weight has no grad
module.postnet.convolutions.2.1.bias has no grad
module.postnet.convolutions.3.0.conv.weight has no grad
module.postnet.convolutions.3.0.conv.bias has no grad
module.postnet.convolutions.3.1.weight has no grad
module.postnet.convolutions.3.1.bias has no grad
module.postnet.convolutions.4.0.conv.weight has no grad
module.postnet.convolutions.4.0.conv.bias has no grad
module.postnet.convolutions.4.1.weight has no grad
module.postnet.convolutions.4.1.bias has no grad
</code></pre>
<p>I do have a postnet in my model and I’m pretty sure this module is used in the forward computation, so I don’t know how to solve this problem.</p>
<p>I’ve tried two methods to work around this bug:</p>
<ul>
<li>set <code>find_unused_parameters=True</code>, this makes the code work, but the model cannot converge.</li>
<li>single GPU training, no error, but training is slow.</li>
</ul>
<p>Is there any method to solve this problem?</p>

wanchaol: <p>Thanks for posting <a class="mention" href="/u/alethia">@Alethia</a>. Looking into the issue, it appears that your model didn’t have gradients produced for those postnet parameters after a backward call, is this normal or should the postnet actually produce gradients? If so, there might be some issues in your model, did you try it locally and the postnet could produce gradients correctly?</p>
<p>It would also be valuable if you can provide a minimal reproducible model so that we can help debugging.</p>

Alethia: <p>Thank you for your reply <a class="mention" href="/u/wanchaol">@wanchaol</a> , the bug has been solved.<br>
The reason is that my model will not use postnet to compute loss in some cases (there is a <code>if</code> condition in the code), so the back-propagation will fail.</p>