RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior

Simon_fei: <pre><code class="lang-auto">def calculate_gradient_penalty(self, real_sens, fake_sens):
        eta = torch.FloatTensor(opt.BATCH_SIZE, opt.MAX_SEQ_LEN).uniform_(0,1)

        if self.cuda:
            eta = eta.cuda()
        # CUDALongTensor---&gt;LongTensor---&gt;FloatTensor---&gt;CUDAFloatTensor
        new_real_sens = real_sens.cpu().type(torch.FloatTensor).cuda()
        new_fake_sens = fake_sens.cpu().type(torch.FloatTensor).cuda()
        interpolated = eta * new_real_sens + ((1 - eta) * new_fake_sens) # CUDAFloatTensor
        # calculate probability of interpolated examples
        prob_interpolated = self.D(interpolated.cpu().type(torch.LongTensor).cuda()) # batch_size, 1 (LongTensor)
        # define it to calculate gradient
        interpolated = Variable(interpolated, requires_grad = True)
        # calculate gradients of probabilities with respect to examples
        gradients = torch.autograd.grad(outputs=prob_interpolated, inputs=interpolated,
                                        grad_outputs=torch.ones(prob_interpolated.size()).cuda(),
                                        create_graph=True, retain_graph=True)
        grad_penalty = ((gradients.norm(2, dim=1)-1) ** 2).mean() * self.lambda_term
        return grad_penalty

</code></pre>
<p>When I write the calculate_gradient_penalty function of WGAN-GP, but I got the following problem:<br>
“RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.”<br>
I search the similar problem that has exists in other man’s work, the cause is that they use  ‘.detach()’  or  ‘.data’  or ‘.numpy()’  somewhere in the chain. After investigation, I don’t have this problem. So I want to know what the causes of problem I am facing?</p>

kshitij: <p>You should probably try what the <code>Error</code> says.</p>
<pre><code class="lang-auto">gradients = torch.autograd.grad(outputs=prob_interpolated, inputs=interpolated,
                                        grad_outputs=torch.ones(prob_interpolated.size()).cuda(),
                                        create_graph=True, retain_graph=True, allow_unused=True) 
</code></pre>
<p>From documentation<br>
<code>allow_unused (bool, optional) – If False, specifying inputs that were not used when computing outputs (and therefore their grad is always zero) is an error. Defaults to False.</code></p>
<p><a href="https://pytorch.org/docs/stable/autograd.html#torch.autograd.grad" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/docs/stable/autograd.html#torch.autograd.grad</a></p>

Simon_fei: <p>Thanks for your answer. This method I has tried and I failed before I put forward my topic. When I set allow_unused = True as the error says, the gradient I get is None, so it is also confused me.</p>

kshitij: <p>Oh, so if I am correct, you’ll get <code>None</code> for the unused input, for the ones that are used, you’ll probably get some gradients.</p>

Ramkumar_Sakthivel: <p>I am also facing the same issue and I get none for all the gradients. Did you solve this issue ? <a class="mention" href="/u/simon_fei">@Simon_fei</a></p>

Sahar_Nasser: <p>I am facing same issue. Did you solve it?</p>

zhaizhaizhai: <p>I am facing same issue.  Did you solve this issue ?</p>

arhouati: <aside class="quote no-group" data-username="Simon_fei" data-post="1" data-topic="43679">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/simon_fei/48/11482_2.png" class="avatar"> Simon_fei:</div>
<blockquote>
<p>I search the similar problem that has exists in other man’s work, the cause is that they use ‘.detach()’ or ‘.data’ or ‘.numpy()’ somewhere in the chain. After investigation, I don’t have this problem. So I want to know what the causes of problem I am facing?</p>
</blockquote>
</aside>
<p>How did you check this ? there is any tools or methods to verify computational graph ?<br>
I have the same problem, but it’s not easy for me to check since my model is little complicated.</p>

Brando_Miranda: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> <a class="mention" href="/u/alband">@albanD</a> apologies for the direct ping. Unsure who to ping.</p>
<p>I am facing this issue too but I know this should not be happening. All weights should be being used.</p>
<p>Is it possible to improve the error message and display the name of the weights causing this issue? It would help me to even start where to debug this.</p>
<p>My model is a simple 5CNN and it passes mini-imagenet data. There shouldn’t be this issue – but would love to get better messages <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<pre><code class="lang-auto">RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.
</code></pre>
<p>related: <a href="https://github.com/learnables/learn2learn/issues/314" class="inline-onebox" rel="noopener nofollow ugc">Error from maml, gradients var not found -- why? · Issue #314 · learnables/learn2learn · GitHub</a></p>

Brando_Miranda: <p>here is the gitissue for this</p>
<aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/73697">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/73697" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/73697" target="_blank" rel="noopener nofollow ugc">Improving error message RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2022-03-02" data-time="22:28:46" data-timezone="UTC">10:28PM - 02 Mar 22 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/brando90" target="_blank" rel="noopener nofollow ugc">
          <img alt="brando90" src="https://avatars.githubusercontent.com/u/1855278?v=4" class="onebox-avatar-inline" width="20" height="20">
          brando90
        </a>
      </div>
    </div>

    <div class="labels">
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">### 🐛 Describe the bug

I get this error:
```
RuntimeError: One of the differe<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">ntiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior.
```

but it doesn't tell me the name of the tensors throwing that issue. Can be displayed in the error to make easier to debug?

### Versions

```
(meta_learning) brandomiranda~ ❯ python collect_env.py
Collecting environment information...
PyTorch version: 1.9.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.2.1 (x86_64)
GCC version: Could not collect
Clang version: 13.0.0 (clang-1300.0.29.30)
CMake version: Could not collect
Libc version: N/A

Python version: 3.9.7 (default, Sep 16 2021, 08:50:36)  [Clang 10.0.0 ] (64-bit runtime)
Python platform: macOS-10.16-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.21.4
[pip3] torch==1.9.1
[pip3] torchaudio==0.9.1
[pip3] torchmeta==1.8.0
[pip3] torchtext==0.10.1
[pip3] torchvision==0.10.1
[conda] numpy                     1.21.4                   pypi_0    pypi
[conda] torch                     1.9.1                    pypi_0    pypi
[conda] torchaudio                0.9.1                    pypi_0    pypi
[conda] torchmeta                 1.8.0                    pypi_0    pypi
[conda] torchtext                 0.10.1                   pypi_0    pypi
[conda] torchvision               0.10.1                   pypi_0    pypi

```</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


albanD: <p>Hi,</p>
<p>You can’t print which error is problematic directly no.</p>
<p>But you can pass the “allow_unused=True” flag to remove the error and then check which outputs are “None”. These are the ones that were not connected to the graph.</p>

Brando_Miranda: <p>Hi!</p>
<p>it should be trivial from the pytorch dev side. If you PyTorch already knows some params are NOT in the forward pass since it failed to do backprop – then instead of giving me an existential quantifier tell me which one concretely is not being used. Then I can debug it. Otherwise, where do I even start in a large model?</p>
<p>Correct me if Im wrong <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

Brando_Miranda: <p>Hi!</p>
<p>it should be trivial from the pytorch dev side. If you PyTorch already knows some params are NOT in the forward pass since it failed to do backprop – then instead of giving me an existential quantifier tell me which one concretely is not being used. Then I can debug it. Otherwise, where do I even start in a large model?</p>
<p>Correct me if I am wrong <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

albanD: <p>Lets discuss in the issue to avoid duplicate!</p>

zhiyuanpeng: <p>Thans for your reply. The doc of allow_unused should be updated. It is confused about the consequence of setting allow_unused true.</p>

Ts_M: <p>Hi, I encountered a similar problem when I reproduced the gradient penalty of WGAN-gp. It appears that changing the view/shape of the tensor, say <code>X</code>, that is passed to <code>inputs</code> arguments causes this problem. Don’t know if this is true for the original post and whether this is desired behavior. This can be replicated by following codes:</p>
<pre><code class="lang-auto">import torch
from torch.autograd import grad
a=torch.randn((3,4),requires_grad=True)
b=a @ (torch.arange(4).float()+1).reshape(4,1)
c=b.sum()
gs=grad(c,a.view(-1),torch.ones_like(c),True,True)[0]
# ^ will fail with RuntimeError:...Tensors appears to not have been used in the graph. Set allow_unused=True...
gs=grad(c,a,torch.ones_like(c),True,True)[0] # success
</code></pre>
<p>The PyTorch version that I used is 1.10.2.</p>

ptrblck: <p>I think the failure in your code is expected as you are passing a <code>view</code> of <code>a</code> (with a valid <code>.grad_fn</code>) as the <code>input</code> to <code>torch.autograd.grad</code>. <code>a.view(-1)</code> is thus also not a leaf tensor anymore.</p>

Ts_M: <p>Sounds Reasonable! Thank you for the reply.</p>

Yeshwanth_V: <p>I am facing the same issue. Did anyone find a workaround for this?</p>

Brando_Miranda: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> <a class="mention" href="/u/alband">@albanD</a> how do I set <code>allow_unused=True</code> globally? I just have a normal <code>.backward()</code> so I never actually use the <code>torch.autograd.grad(outputs, inputs, grad_outputs=None, retain_graph=None, create_graph=False, only_inputs=True, allow_unused=False, is_grads_batched=False)</code> directly myself as the docs suggest.</p>
<p>How do I set this true? (hoping this will remove the fact that it’s likely that the params for my vit/transformer aren’t being used when I do a forward pass and that’s fine)</p>