Applying operator loses causes loss of gradient

Joe_Harrison: <p>I’m trying to apply a hook to a tensor which should require a gradient, but I get the error:</p>
<blockquote>
<p>RuntimeError: cannot register a hook on a tensor that doesn’t require gradient</p>
</blockquote>
<p>This happens on the line where I register the hook.</p>
<pre><code class="lang-auto">    def forward(self, x):
        c_outputs = [c(x) for c in self.children]
        output = torch.exp(c_outputs[0])
        print(c_outputs[0].requires_grad)
        print(output.requires_grad)
        if self.clip_grad:
            output.register_hook(modify_grad)
        return output
</code></pre>
<p>Output:</p>
<pre><code class="lang-auto">True
False
[...]
File /tree.py", line 251, in forward
    output.register_hook(modify_grad)
  File "xxx/anaconda3/envs/deep/lib/python3.8/site-packages/torch/_tensor.py", line 430, in register_hook
    raise RuntimeError("cannot register a hook on a tensor that "
RuntimeError: cannot register a hook on a tensor that doesn't require gradient
</code></pre>
<p>However the print statements indicate that whatever I’m putting inside the exp operator requires a gradient while the output after applying the exp operator does not. I found similar behaviour for the repeat operator, but switching it out for the expand operator helped. Any idea how I can make the output require a gradient?</p>

KFrank: <p>Hi Joe!</p>
<aside class="quote no-group quote-modified" data-username="Joe_Harrison" data-post="1" data-topic="159034" data-full="true">
<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/joe_harrison/48/5534_2.png" class="avatar"> Joe_Harrison:</div>
<blockquote>
<blockquote>
<p>RuntimeError: cannot register a hook on a tensor that doesn’t require gradient</p>
</blockquote>
</blockquote>
</aside>
<p>Your reasoning is correct that applying <code>exp() </code> to a tensor should not<br>
reset its <code>requires_grad</code> property to <code>False</code>.</p>
<p>I cannot reproduce your issue.  Here is a minimal, fully self-contained,<br>
runnable script (based on your code) that <em>does not</em> reproduce your<br>
issue:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.12.0'
&gt;&gt;&gt;
&gt;&gt;&gt; _ = torch.manual_seed (2022)
&gt;&gt;&gt;
&gt;&gt;&gt; def dummy_hook (grad):
...     print ('dummy_hook: grad = ...')
...     print (grad)
...     return None
...
&gt;&gt;&gt; children = [torch.nn.Linear (2, 2), torch.nn.Linear (2, 2), torch.nn.Linear (2, 2)]
&gt;&gt;&gt; t = torch.randn (5, 2)
&gt;&gt;&gt; clip_grad = True
&gt;&gt;&gt;
&gt;&gt;&gt; def forward(x):
...     c_outputs = [c(x) for c in children]
...     output = torch.exp(c_outputs[0])
...     print(c_outputs[0].requires_grad)
...     print(output.requires_grad)
...     if clip_grad:
...         output.register_hook (dummy_hook)
...     return output
...
&gt;&gt;&gt; out = forward (t)
True
True
&gt;&gt;&gt; print (out)
tensor([[0.2549, 1.3924],
        [0.3643, 1.0232],
        [0.4743, 1.4320],
        [0.7217, 0.5773],
        [0.2918, 0.4463]], grad_fn=&lt;ExpBackward0&gt;)
&gt;&gt;&gt;
&gt;&gt;&gt; out.sum().backward()
dummy_hook: grad = ...
tensor([[1., 1.],
        [1., 1.],
        [1., 1.],
        [1., 1.],
        [1., 1.]])
&gt;&gt;&gt; print (children[0].weight.grad)
tensor([[-0.0651, -0.4404],
        [ 2.6920, -1.8137]])
</code></pre>
<p>Could you post a minimal, fully self-contained, runnable script that <em>does</em><br>
reproduce your issue?</p>
<aside class="quote no-group">
<blockquote>
<p>Output:</p>
<pre><code class="lang-auto">True
False
[...]
File /tree.py", line 251, in forward
    output.register_hook(modify_grad)
  File "xxx/anaconda3/envs/deep/lib/python3.8/site-packages/torch/_tensor.py", line 430, in register_hook
    raise RuntimeError("cannot register a hook on a tensor that "
RuntimeError: cannot register a hook on a tensor that doesn't require gradient
</code></pre>
</blockquote>
</aside>
<p>The <code>[...]</code> in your output catches my eye and suggests that you have<br>
edited away some of your output.  Is it possible that the cause of your<br>
problem lies somewhere in those magic ellipses?</p>
<p>Best.</p>
<p>K. Frank</p>