For posterity - this bug has been fixed in torch==1.10

  

      github.com/pytorch/pytorch
  

  
    
  
	  
  

  
    
      RuntimeError: Error while setting up backward hooks. Please open an issue with a code sample to reproduce this  1
    

    
      
        opened 
        
          
        
        Oct 4, 2021
      
      

        
          closed 
        
          
        
        Oct 5, 2021
      
        

      
        
          
          AlphaBetaGamma96
        
      
    

    
        
          module: nn
        
        
          triaged
        
    
  


  
    ## 🐛 Bug



Setting up backward hooks fails when the loss function depends o…n a detached Tensor. 

## To Reproduce
```
import torch
import torch.nn as nn

x = torch.randn(4,2,requires_grad=True)
fc = nn.Linear(2, 1)

#x.detach_().requires_grad_()

def _save_output(module, grad_input, grad_output) -> None:
  print("grad_output[0]: ",grad_output[0])

fc.register_full_backward_hook(_save_output)

with torch.no_grad():
  y = fc(x)

z = x.pow(2).sum(dim=-1)

#loss is defined as the sum of 2 terms
#first term, y has no gradient to it (purely scales)
#second term, z has a gradient. (in short, y scales z)
loss = torch.sum(y+z)

loss.backward()
```


```
Traceback (most recent call last):
  File "backward_hook_setup_error_standalone.py", line 15, in <module>
    y = fc(x)
  File "~/.local/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1081, in _call_impl
    input = bw_hook.setup_input_hook(input)
  File "~/.local/lib/python3.8/site-packages/torch/utils/hooks.py", line 158, in setup_input_hook
    res, input_idx = self._apply_on_tensors(fn, args)
  File "~/.local/lib/python3.8/site-packages/torch/utils/hooks.py", line 142, in _apply_on_tensors
    raise RuntimeError("Error while setting up backward hooks. Please open "
RuntimeError: Error while setting up backward hooks. Please open an issue with a code sample to reproduce this.
```
## Expected behavior

The ability to set up backward hooks on layers where the loss partially contains detached Tensors.

## Environment
```
Collecting environment information...
PyTorch version: 1.10.0.dev20210824+cu111
Is debug build: False
CUDA used to build PyTorch: 11.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.3 LTS (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.31

Python version: 3.8.8 (default, Apr 13 2021, 19:58:26)  [GCC 7.3.0] (64-bit runtime)
Python platform: Linux-5.11.0-37-generic-x86_64-with-glibc2.10
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: GeForce RTX 2060
Nvidia driver version: 460.91.03
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] functorch==0.0.1a0+0ae60dd
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.21.2
[pip3] numpydoc==1.1.0
[pip3] pytorch-lightning==1.3.5
[pip3] torch==1.10.0.dev20210824+cu111
[pip3] torch-kfac==0.0.1
[pip3] torchaudio==0.9.0
[pip3] torchdiffeq==0.2.2
[pip3] torchdyn==0.2.2.1
[pip3] torchmetrics==0.3.2
[pip3] torchsde==0.2.5
[pip3] torchvision==0.10.0+cu111
[pip3] torchviz==0.0.2
[conda] blas                      1.0                         mkl  
[conda] mkl                       2021.2.0           h06a4308_296  
[conda] mkl-service               2.3.0            py38h27cfd23_1  
[conda] mkl_fft                   1.3.0            py38h42c9631_2  
[conda] mkl_random                1.2.1            py38ha9443f7_2  
[conda] mypy_extensions           0.4.3                    py38_0  
[conda] numpy                     1.19.5                   pypi_0    pypi
[conda] numpy-base                1.20.1           py38h7d8b39e_0  
[conda] numpydoc                  1.1.0              pyhd3eb1b0_1  
[conda] pytorch-lightning         1.3.5                    pypi_0    pypi
[conda] torch                     1.9.0+cu111              pypi_0    pypi
[conda] torchaudio                0.9.0                    pypi_0    pypi
[conda] torchdiffeq               0.2.2                    pypi_0    pypi
[conda] torchdyn                  0.2.2.1                  pypi_0    pypi
[conda] torchmetrics              0.3.2                    pypi_0    pypi
[conda] torchsde                  0.2.5                    pypi_0    pypi
[conda] torchvision               0.10.0+cu111             pypi_0    pypi
[conda] torchviz                  0.0.2                    pypi_0    pypi
```

## Additional context


This was originally opened as a thread on the forums, the link to it is here: https://discuss.pytorch.org/t/runtimeerror-error-while-setting-up-backward-hooks-please-open-an-issue-with-a-code-sample-to-reproduce-this/133302

cc @albanD @mruberry @jbschlosser @walterddr
  

  

  
    
    
  

  


So you want:
import torch
from packaging import version
[...]
    def _register_backward_hook(self, module):
        if version.parse(torch.__version__) >= version.parse("1.10"):
            module.register_full_backward_hook(self.backward_hook)
        else:
            module.register_backward_hook(self.backward_hook)