Using non-full backward hooks on a Module that does not return a single Tensor

Sia_Rezaei: <p>I’m getting this warning since I upgraded to 1.8. I have a custom module that returns <code>tensor, (tensor tensor)</code> which triggers this.<br>
What is this warning trying to say.</p><aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/f5a7b2e641a124751dfa3fdf1926eb4135d3aad5/torch/nn/modules/module.py#L903" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/f5a7b2e641a124751dfa3fdf1926eb4135d3aad5/torch/nn/modules/module.py#L903" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/f5a7b2e641a124751dfa3fdf1926eb4135d3aad5/torch/nn/modules/module.py#L903</a></h4>
<pre class="onebox"><code class="lang-py"><ol class="start lines" start="893" style="counter-reset: li-counter 892 ;">
<li>    if (_global_is_full_backward_hook is False):</li>
<li>        non_full_backward_hooks += _global_backward_hooks.values()</li>
<li>    if (self._is_full_backward_hook is False):</li>
<li>        non_full_backward_hooks += self._backward_hooks.values()</li>
<li>
<li>    return full_backward_hooks, non_full_backward_hooks</li>
<li>
<li>def _maybe_warn_non_full_backward_hook(self, inputs, result, grad_fn):</li>
<li>    if not isinstance(result, torch.Tensor):</li>
<li>        if not (isinstance(result, tuple) and all([isinstance(r, torch.Tensor) for r in result])):</li>
<li class="selected">            warnings.warn("Using non-full backward hooks on a Module that does not return a "</li>
<li>                          "single Tensor or a tuple of Tensors is deprecated and will be removed "</li>
<li>                          "in future versions. This hook will be missing some of the grad_output. "</li>
<li>                          "Please use register_full_backward_hook to get the documented behavior.")</li>
<li>            return</li>
<li>    else:</li>
<li>        result = (result,)</li>
<li>
<li>    if not isinstance(inputs, torch.Tensor):</li>
<li>        if not (isinstance(inputs, tuple) and all([isinstance(i, torch.Tensor) for i in inputs])):</li>
<li>            warnings.warn("Using non-full backward hooks on a Module that does not take as input a "</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


ptrblck: <p>The warning is raised, if you are trying to use <a href="https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.register_backward_hook"><code>register_backward_hook</code></a>, which is deprecated in favor of <a href="https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.register_full_backward_hook"><code>register_full_backward_hook</code></a>. Could you swap these methods and rerun your script?</p>

Sia_Rezaei: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> hmm, the thing is I’m not explicitly registering a backward hook anywhere in my module.</p>
<p>My module consists of a bunch of normal PyTorch modules/functions which have their own backward methods. So I don’t see the need to write my own backward method. Do I need to write one now?</p>

ptrblck: <p>That’s strange, as this warning would be called in <a href="https://github.com/pytorch/pytorch/blob/f5a7b2e641a124751dfa3fdf1926eb4135d3aad5/torch/nn/modules/module.py#L1047-L1061">these lines of code</a>, which shouldn’t be triggered, if no backward hooks are used.<br>
Could you post a minimal executable code snippet, which would raise this warning without the explicit usage of backward hooks, please?</p>

songyuc: <p>Hi, I use this code snippet and trigger this warning,</p>
<pre><code class="lang-python">    def hook_intermediate_layers(self):
        for name, m in self.backbone.named_modules():
            if name in self.return_layers:
                m.register_forward_hook(partial(self.copy_output, feat_id=self.return_layers[name]))
</code></pre>
<p>What should I do to eliminate the warning?</p>

ptrblck: <p>Could you post an executable code snippet to reproduce this issue, as I’m still unable to reproduce and debug it?</p>

songyuc: <p>Please give me some time working on the reproduction.</p>