RuntimeError: grad_output must be contiguous when backward

xingjici: <p>Hi,<br>
When I’m modifying a model, a new module（m2） is added that generates a loss value and it will be merged with the previous modules (e.g. L=Lm1+lm2…)</p>
<p>RuntimeError: grad_output must be contiguous when backpropagating.When I run it, it throws the following exception, and when I remove the newly added module (m2), it works fine. Where’s the problem?</p>
<p>Thank you for your time and consideration!<br>
Traceback (most recent call last):<br>
File “/home/xjc/anaconda3/envs/py37/lib/python3.7/site-packages/torch/utils/data/dataloader.py”, line 1101, in <strong>del</strong><br>
self._shutdown_workers()<br>
File “/home/xjc/anaconda3/envs/py37/lib/python3.7/site-packages/torch/utils/data/dataloader.py”, line 1075, in _shutdown_workers<br>
w.join(timeout=_utils.MP_STATUS_CHECK_INTERVAL)<br>
File “/home/xjc/anaconda3/envs/py37/lib/python3.7/multiprocessing/process.py”, line 140, in join<br>
res = self._popen.wait(timeout)<br>
File “/home/xjc/anaconda3/envs/py37/lib/python3.7/multiprocessing/popen_fork.py”, line 45, in wait<br>
if not wait([self.sentinel], timeout):<br>
File “/home/xjc/anaconda3/envs/py37/lib/python3.7/multiprocessing/connection.py”, line 921, in wait<br>
ready = selector.select(timeout)<br>
File “/home/xjc/anaconda3/envs/py37/lib/python3.7/selectors.py”, line 415, in select<br>
fd_event_list = self._selector.poll(timeout)<br>
KeyboardInterrupt:<br>
Traceback (most recent call last):<br>
File “/home/xjc/pycharm/helpers/pydev/pydevd.py”, line 1741, in <br>
main()<br>
File “/home/xjc/pycharm/helpers/pydev/pydevd.py”, line 1735, in main<br>
globals = debugger.run(setup[‘file’], None, None, is_module)<br>
File “/home/xjc/pycharm/helpers/pydev/pydevd.py”, line 1135, in run<br>
pydev_imports.execfile(file, globals, locals)  # execute the script<br>
File “/home/xjc/pycharm/helpers/pydev/_pydev_imps/_pydev_execfile.py”, line 18, in execfile<br>
exec(compile(contents+"\n", file, ‘exec’), glob, loc)<br>
File “/home/xjc/PycharmProjects/mmv2/mmdetection/tools/train1.py”, line 175, in <br>
main()<br>
File “/home/xjc/PycharmProjects/mmv2/mmdetection/tools/train1.py”, line 171, in main<br>
meta=meta)<br>
File “/home/xjc/PycharmProjects/mmv2/mmdetection/mmdet/apis/train.py”, line 143, in train_detector<br>
runner.run(data_loaders, cfg.workflow, cfg.total_epochs)<br>
File “/home/xjc/anaconda3/envs/py37/lib/python3.7/site-packages/mmcv/runner/epoch_based_runner.py”, line 122, in run<br>
epoch_runner(data_loaders[i], **kwargs)<br>
File “/home/xjc/anaconda3/envs/py37/lib/python3.7/site-packages/mmcv/runner/epoch_based_runner.py”, line 43, in train<br>
self.call_hook(‘after_train_iter’)<br>
File “/home/xjc/anaconda3/envs/py37/lib/python3.7/site-packages/mmcv/runner/base_runner.py”, line 298, in call_hook<br>
getattr(hook, fn_name)(self)<br>
File “/home/xjc/anaconda3/envs/py37/lib/python3.7/site-packages/mmcv/runner/hooks/optimizer.py”, line 24, in after_train_iter<br>
runner.outputs[‘loss’].backward()<br>
File “/home/xjc/anaconda3/envs/py37/lib/python3.7/site-packages/torch/tensor.py”, line 185, in backward<br>
torch.autograd.backward(self, gradient, retain_graph, create_graph)<br>
File “/home/xjc/anaconda3/envs/py37/lib/python3.7/site-packages/torch/autograd/<strong>init</strong>.py”, line 127, in backward<br>
allow_unreachable=True)  # allow_unreachable flag<br>
RuntimeError: grad_output must be contiguous</p>

ptrblck: <p>Could you post a minimal code snippet to reproduce this issue?</p>
<p>You can add code snippets by wrapping them into three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

X_Zhao: <p>Have you solve the problem, I meet the same error.</p>