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:
- Python custom convolution kernel weight parameters
- [Solved] pytorch loss.backward() Error: RuntimeError: Function AddBackward0 returned an invalid gradient at index 1…
- [Solved] AttributeError: ‘_IncompatibleKeys‘ object has no attribute ‘parameters‘
- [Solved] Using summary to View network parameters Error: RuntimeError: Input type (torch.cuda.FloatTensor)
- How to Fix Errors encountered in executing Python scripts with command line parameters
- [Solved] AttributeError: module ‘distutils‘ has no attribute ‘version‘
- To solve the problem that the loss of verification set of resnet50 pre-training model remains unchanged
- [Solved] Training yolov5 Error: attributeerror: can get attribute sppf on Module
- [Solved] PyQt: RuntimeError: wrapped C/C++ object has been deleted & has no attribute of flush in python
- [Solved] AttributeError: module ‘tensorflow‘ has no attribute ‘distributions‘
- [Solved] AttributeError: module ‘logging‘ has no attribute ‘Handler‘
- [Solved] AttributeError: module ‘tensorflow._api.v2.train‘ has no attribute ‘AdampOptimizer‘
- [Solved]AttributeError: module ‘urllib’ has no attribute ‘quote’
- AttributeError: module ‘enum‘ has no attribute ‘IntFlag‘ [How to Solve]
- [Solved] AttributeError: module ‘setuptools._distutils‘ has no attribute ‘version‘
- [Perfectly Solved] attributeerror: module ‘SciPy. Misc’ has no attribute ‘toimage’ error
- [Solved] AttributeError: module ‘time‘ has no attribute ‘clock‘
- [How to Solve]AttributeError: module ‘scipy’ has no attribute ‘io’
- Python AttributeError: module ‘tensorflow‘ has no attribute ‘InteractiveSession‘
- Module not found error: no module named ‘filefolder’ appears when learning engineering knowledge