DDP, parameters didn't receive gradients for used module


I’m training a model called VQVAE using DDP, when I ran the code, there was an error message:

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)

I used the following code to detect unused parameters:

for n, p in model.named_parameters():
    if p.grad is None:
        print(f'{n} has no grad')

The above code gives the following messages:

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

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.

I’ve tried two methods to work around this bug:

  • set find_unused_parameters=True, this makes the code work, but the model cannot converge.
  • single GPU training, no error, but training is slow.

Is there any method to solve this problem?

Thanks for posting @Alethia. 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?

It would also be valuable if you can provide a minimal reproducible model so that we can help debugging.

Thank you for your reply @wanchaol , the bug has been solved.
The reason is that my model will not use postnet to compute loss in some cases (there is a if condition in the code), so the back-propagation will fail.

1 Like