[Solved] error indicates that your module has parameters that were not used in producing loss

Error Messages:
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 0: 160 161 182 183 204 205 230 231 252 253 274 275 330 331 414 415 438 439 462 463 486 487 512 513 536 537 560 561 584 585
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

Solution:

Original Code

 class AttentionBlock(nn.Module):
    def __init__(
        self,
    ):
        super().__init__()
        self.encoder_kv = conv_nd(1, 512, channels * 2, 1) #这行没有注释掉
        self.encoder_qkv = conv_nd(1, 512, channels * 3, 1)
        self.trans = nn.Linear(resolution*resolution*9+128,resolution*resolution*9)
    def forward(self, x, encoder_out=None):
        b, c, *spatial = x.shape
        x = x.reshape(b, c, -1)
        qkv = self.qkv(self.norm(x))
        if encoder_out is not None:
            # encoder_out = self.encoder_kv(encoder_out)  #这行代码注释了,没有用self.encoder_kv
            encoder_out = self.encoder_qkv(encoder_out)
        return encode_out

Error reason:

self.encoder_kv is written in def__init__, but not used in forward, resulting in an error in torch.nn.parallel.DistributedDataParallel. Correction method

Modified code

class AttentionBlock(nn.Module):
   def __init__(
       self,
   ):
       super().__init__()
       #self.encoder_kv = conv_nd(1, 512, channels * 2, 1) #这行在forward中没有用到注释掉
       self.encoder_qkv = conv_nd(1, 512, channels * 3, 1)
       self.trans = nn.Linear(resolution*resolution*9+128,resolution*resolution*9)
   def forward(self, x, encoder_out=None):
       b, c, *spatial = x.shape
       x = x.reshape(b, c, -1)
       qkv = self.qkv(self.norm(x))
       if encoder_out is not None:
           # encoder_out = self.encoder_kv(encoder_out)  
           encoder_out = self.encoder_qkv(encoder_out)
       return encode_out

Comment out the function self.encoder_kv = conv_nd(1, 512, channels * 2, 1) that is not used in the forward, and the program will run normally.

Read More: