Pytest + RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

Nick_McCarthy: <p>Hi all,</p>
<p>Is anyone aware of a problem when running pytest and getting the error:</p>
<p><code>RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn</code></p>
<p>The tests are for use within a large training pipeline (e.g. testing complete model training for a couple epochs, retraining of models, etc) so I’m not sure I can reduce it down to a simple snippet demonstrating the issue.</p>
<p>Basically, if I run the entire test suite using:</p>
<p><code>pytest tests/</code></p>
<p>then some of the tests will fail.</p>
<p>If I run those failing tests at the module level:</p>
<p><code>pytest tests/modulename/module/test_module.py</code></p>
<p>then they pass.</p>
<p>The tests all pass on my local machine (I have to exclude some as my machine can’t handle the overhead), and they pass when I run them individually on the remote machine. It’s pretty annoying as it’s interfering with our deployment procedure and I <em>know</em> the tests pass.</p>
<p>Local environment (windows):</p>
<pre><code class="lang-auto">NVIDIA-SMI 497.29       Driver Version: 497.29       CUDA Version: 11.5     

pytorch                   1.11.0          py3.8_cuda11.3_cudnn8_0    pytorch
pytorch-mutex             1.0                        cuda    pytorch
torchaudio                0.11.0               py38_cu113    pytorch
torchio                   0.18.43                  pypi_0    pypi
torchsummary              1.5.1                    pypi_0    pypi
torchvision               0.12.0               py38_cu113    pytorch
</code></pre>
<p>Remote environment (ubuntu):</p>
<pre><code class="lang-auto">NVIDIA-SMI 470.103.01   Driver Version: 470.103.01   CUDA Version: 11.4   

pytorch                   1.11.0          py3.8_cuda11.3_cudnn8.2.0_0    pytorch
pytorch-mutex             1.0                        cuda    pytorch
torchaudio                0.11.0               py38_cu113    pytorch
torchio                   0.18.43                  pypi_0    pypi
torchsummary              1.5.1                    pypi_0    pypi
torchvision               0.12.0               py38_cu113    pytorch
</code></pre>

ptrblck: <p>Could you check if you’ve disabled gradient computation in your test scripts?<br>
It could also be valid to check if previous tests were disabling it without a proper cleanup.<br>
Maybe try to shuffle the tests or exclude some previous tests to see if the problematic test would still fail.</p>

Nick_McCarthy: <p>There’s no <code>torch.no_grad()</code> set in the tests.</p>
<p>There were a few tests that had random Tensors input to models - I initially thought it might have been that so set everything to <code>requires_grad=True</code> where possible.</p>
<p>When this issue started it was just one module’s tests (call it module_A) that were causing the issue - couldn’t decipher the issue then (incl. driver and environment reinstall), so the workaround was to disable those module tests and run them locally for the time being. Last week the same error started with another module’s tests, that had definitely been running fine before (locally, remotely, and on our action-runner tests). I noticed that they only seemed to fail when running the entire test suite (as in original post), but not for individual modules.</p>
<p>I’ve re-written our test scripts to simply run as:</p>
<pre><code class="lang-auto">pytest tests/module_A
pytest tests/module_B
#etc 
</code></pre>
<p>… and now the originally failing tests pass (so hooray).</p>
<p>My immediate problem is resolved, but I hadn’t seen anyone posting with this particular issue before, so  there you go.</p>

ptrblck: <p>I wouldn’t check for <code>no_grad()</code> usages as they would be local (and your local tests seem to work) but for global switches such as <code>torch.enable_grad</code> etc.<br>
If any test disabled gradient calculations globally it should also reset it back to the default/previous settings. If that’s not the case other tests could of course fail.</p>