Repository: pytorch
Issue: Internal assert when ctx.saved_tensors fails when saving results of an intermediate view tensor with torch.utils.checkpoint and use_reentrant=False
Labels: module: checkpoint,module: autograd,triaged,has workaround,actionable

### 🐛 Describe the bug

Whenever we try to save a reshaped tensor (even without actually changing the shape) in the context, an error occurs at the point of retrieving it during backward when we use it with `torch.utils.checkpoint` and `use_reentrant=False`:

```python
import torch
from torch.utils.checkpoint import checkpoint


class CustomOp(torch.autograd.Function):
    @staticmethod
    def forward(ctx, x):
        ctx.save_for_backward(x.reshape(x.shape))  # error due to this reshape
        return x
        
    @staticmethod
    def backward(ctx, grad):
        x = ctx.saved_tensors  # this fails
        return grad


input = torch.tensor(0., requires_grad=True)
op = CustomOp()

checkpoint(lambda x: op.apply(x), input, use_reentrant=False).backward()
```

```
Traceback (most recent call last):                                                                                                                                                            
  File "reproduce_checkpoint.py", line 20, in <module>                                                                                                                           
    checkpoint(lambda x: op.apply(x), input, use_reentrant=False).backward()                                                                                                                  
  File ".../lib/python3.10/site-packages/torch/_tensor.py", line 487, in backward                                                                   
    torch.autograd.backward(                                                                                                                                                                  
  File ".../lib/python3.10/site-packages/torch/autograd/__init__.py", line 197, in backward                                                         
    Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass                                                                                            
  File ".../lib/python3.10/site-packages/torch/autograd/function.py", line 267, in apply                                                            
    return user_fn(self, *args)                                                                                                                                                               
  File "reproduce_checkpoint.py", line 13, in backward                                                                                                                           
    x = ctx.saved_tensors  # this fails                                                                                                                                                       
RuntimeError: !grad_accumulator_.expired() INTERNAL ASSERT FAILED at "/opt/conda/conda-bld/pytorch_1666643003845/work/torch/csrc/autograd/saved_variable.cpp":216, please report a bug to PyTorch. No grad accumulator for a saved leaf
```

A couple of notes:
- If the reshape operation is removed, it works okay.
- If the result of the reshape is multiplied by a constant, it also works fine.
- If `use_reentrant` is set to `True`, then error also disappears.

### Versions

Collecting environment information...
PyTorch version: 1.13.0
Is debug build: False
CUDA used to build PyTorch: 11.6
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.4 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: 10.0.0-4ubuntu1 
CMake version: version 3.22.4
Libc version: glibc-2.31

Python version: 3.10.8 (main, Nov 24 2022, 14:13:03) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.13.0-1025-aws-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: 11.6.124
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA A100-SXM4-40GB

Nvidia driver version: 510.47.03
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

Versions of relevant libraries:
[pip3] numpy==1.23.4
[pip3] torch==1.13.0
[pip3] torchaudio==0.13.0
[pip3] torchvision==0.14.0
[conda] blas                      1.0                         mkl  
[conda] ffmpeg                    4.3                  hf484d3e_0    pytorch
[conda] mkl                       2021.4.0           h06a4308_640  
[conda] mkl-service               2.4.0           py310h7f8727e_0  
[conda] mkl_fft                   1.3.1           py310hd6ae3a3_0  
[conda] mkl_random                1.2.2           py310h00e6091_0  
[conda] numpy                     1.23.4          py310hd5efca6_0  
[conda] numpy-base                1.23.4          py310h8e6c178_0  
[conda] pytorch                   1.13.0          py3.10_cuda11.6_cudnn8.3.2_0    pytorch
[conda] pytorch-cuda              11.6                 h867d48c_0    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchaudio                0.13.0              py310_cu116    pytorch
[conda] torchvision               0.14.0              py310_cu116    pytorch

cc @ezyang @albanD @zou3519 @gqchen @pearu @nikitaved @soulitzer @Lezcano @Varal7