Repository: pytorch
Issue: Backward hook execution order changes when input.requires_grad is False
Labels: module: docs,module: autograd,module: nn,triaged,actionable

### 🐛 Describe the bug

When using the `register_module_full_backward_pre_hook` and `register_module_full_backward_hook` functions, the order of hooks execution is affected by whether the input requires gradient or not. If the input does not require a gradient, then the order of execution becomes incorrect.

```
------ train_data.requires_grad = True ------
Push:  MSELoss
Pop:  MSELoss
-->Push:  Sequential
Push:  Sequential<--
Push:  Linear
Pop:  Linear
Push:  ReLU
Pop:  ReLU
Push:  Linear
Pop:  Linear
Pop:  Sequential
Push:  Sequential
Push:  Linear
Pop:  Linear
Push:  ReLU
Pop:  ReLU
Push:  Linear
Pop:  Linear
Pop:  Sequential
Pop:  Sequential
------ train_data.requires_grad = False ------
Push:  MSELoss
Pop:  MSELoss
-->Push:  Sequential
Pop:  Sequential
Push:  Sequential<--
Push:  Linear
Pop:  Linear
Push:  ReLU
Pop:  ReLU
Push:  Linear
Pop:  Linear
Pop:  Sequential
Push:  Sequential
Pop:  Sequential
Push:  Linear
Pop:  Linear
Push:  ReLU
Pop:  ReLU
Push:  Linear
Pop:  Linear
Finished Training
```

Code to reproduce:
```python
import torch
import torch.nn as nn
import torch.optim as optim

# Define the neural network architecture
net = nn.Sequential(nn.Sequential(
    nn.Linear(10, 20),
    nn.ReLU(),
    nn.Linear(20, 1)
), nn.Sequential(
    nn.Linear(1, 20),
    nn.ReLU(),
    nn.Linear(20, 1)))

# Create dummy data
train_data = torch.randn(100, 10, requires_grad=True)
train_labels = torch.randn(100, 1)

# Define the loss function and optimizer
criterion = nn.MSELoss()

# Train the neural network
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
net = net.to(device)
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

def _pre_hook(module, *_, **__):
    print('Push: ', f'{module.__class__.__name__}')
def _after_hook(module, *_, **__):
    print('Pop: ', f'{module.__class__.__name__}')
torch.nn.modules.module.register_module_full_backward_pre_hook(_pre_hook)
torch.nn.modules.module.register_module_full_backward_hook(_after_hook)

print('------ train_data.requires_grad = True ------')

for epoch in range(1):
    running_loss = 0.0
    for i in range(1):
        inputs, labels = train_data[i].to(device), train_labels[i].to(device)
        optimizer.zero_grad()
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()
    # print('Epoch %d loss: %.3f' % (epoch + 1, running_loss / 100))
    running_loss = 0.0

print('------ train_data.requires_grad = False ------')

train_data.requires_grad = False
for epoch in range(1):
    running_loss = 0.0
    for i in range(1):
        inputs, labels = train_data[i].to(device), train_labels[i].to(device)
        optimizer.zero_grad()
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        running_loss += loss.item()
    # print('Epoch %d loss: %.3f' % (epoch + 1, running_loss / 100))
    running_loss = 0.0

print('Finished Training')
```

### Versions

```
Collecting environment information...
PyTorch version: 1.14.0a0+44dac51
Is debug build: False
CUDA used to build PyTorch: 12.0
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.5 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: version 3.24.1
Libc version: glibc-2.31

Python version: 3.8.10 (default, Nov 14 2022, 12:59:47)  [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-5.4.0-77-generic-x86_64-with-glibc2.29
Is CUDA available: True
CUDA runtime version: 12.0.140
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA A30
GPU 1: NVIDIA A30
GPU 2: NVIDIA A30
GPU 3: NVIDIA A30
GPU 4: NVIDIA A30
GPU 5: NVIDIA A30
GPU 6: NVIDIA A30
GPU 7: NVIDIA A30

Nvidia driver version: 525.85.12
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.7.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.7.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.7.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.7.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.7.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.7.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.7.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Byte Order:                      Little Endian
Address sizes:                   43 bits physical, 48 bits virtual
CPU(s):                          256
On-line CPU(s) list:             0-255
Thread(s) per core:              2
Core(s) per socket:              64
Socket(s):                       2
NUMA node(s):                    8
Vendor ID:                       AuthenticAMD
CPU family:                      23
Model:                           49
Model name:                      AMD EPYC 7742 64-Core Processor
Stepping:                        0
Frequency boost:                 enabled
CPU MHz:                         1500.048
CPU max MHz:                     2250.0000
CPU min MHz:                     1500.0000
BogoMIPS:                        4500.40
Virtualization:                  AMD-V
L1d cache:                       4 MiB
L1i cache:                       4 MiB
L2 cache:                        64 MiB
L3 cache:                        512 MiB
NUMA node0 CPU(s):               0-15,128-143
NUMA node1 CPU(s):               16-31,144-159
NUMA node2 CPU(s):               32-47,160-175
NUMA node3 CPU(s):               48-63,176-191
NUMA node4 CPU(s):               64-79,192-207
NUMA node5 CPU(s):               80-95,208-223
NUMA node6 CPU(s):               96-111,224-239
NUMA node7 CPU(s):               112-127,240-255
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Vulnerable, IBPB: disabled, STIBP: disabled
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good nopl nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate sme ssbd mba sev ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif umip rdpid overflow_recov succor smca

Versions of relevant libraries:
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.22.2
[pip3] pytorch-lightning==1.9.4
[pip3] pytorch-quantization==2.1.2
[pip3] torch==1.14.0a0+44dac51
[pip3] torch-automated-profiler==1.10.0
[pip3] torch-performance-linter==0.2.1.dev36+gd3906c3
[pip3] torch-tb-profiler==0.4.1
[pip3] torch-tensorrt==1.4.0.dev0
[pip3] torchmetrics==0.9.1
[pip3] torchtext==0.13.0a0+fae8e8c
[pip3] torchvision==0.15.0a0
[pip3] triton==2.0.0
[pip3] tritonclient==2.22.4
[conda] Could not collect
```

cc @svekars @carljparker @ezyang @albanD @zou3519 @gqchen @pearu @nikitaved @soulitzer @Lezcano @Varal7 @mruberry @jbschlosser @walterddr @mikaylagawarecki