Repository: pytorch
Issue: Memory Leak in v2.0.1 
Labels: module: cuda,module: memory usage,triaged

### 🐛 Describe the bug

In pytorch v2.0.1, I encountered an memory leak when trying to input tensors in different shapes to the model. 

Here are codes to reproduce:
```
from torchvision.models import resnet
import torch
from memory_profiler import profile

net = resnet.resnet50(pretrained=True)

net = net.cuda()

net.train()

@profile(precision=4,stream=open('resnet.log','w'))
def infer(width, height):
    data = torch.randn(2, 3, width, height)
    x = data.clone().cuda()
    out = net(x)


for width in range(100, 1000, 10):
    print(width)
    for height in range(100, 1000 ,10):
        infer(width, height)

```
When executing the codes, memory occupancy will raise to more than 10GB. And if you continuously input more shapes, there will finally be an OOM.
![torch2 0 1](https://github.com/pytorch/pytorch/assets/38578239/b5f47be5-7407-4899-89a0-0b58dd45837a)

On the contrary, there will not be an OOM in earlier version of pytorch(e.g. v1.13.1)
![torch1 13 1](https://github.com/pytorch/pytorch/assets/38578239/44f91a05-af40-4c00-b117-e112bcecd32f)

And also normal in later version of pytorch(e.g. v2.2.0)
![torch2 2 0](https://github.com/pytorch/pytorch/assets/38578239/a91b7d09-c75d-4113-9eb6-8a6890bbdc10)

So this seems to be a unique bug of v2.0.1.



### Versions

```
Collecting environment information...
PyTorch version: 2.0.1+cu118
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: CentOS Linux release 7.6.1810 (Core)  (x86_64)
GCC version: (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Clang version: Could not collect
CMake version: version 3.28.1
Libc version: glibc-2.17

Python version: 3.10.0 (default, Mar  3 2022, 09:58:08) [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-3.10.0-957.el7.x86_64-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA A100 80GB PCIe
GPU 1: NVIDIA A100 80GB PCIe
GPU 2: NVIDIA A100 80GB PCIe
GPU 3: NVIDIA A100 80GB PCIe
GPU 4: NVIDIA A100 80GB PCIe
GPU 5: NVIDIA A100 80GB PCIe
GPU 6: NVIDIA A100 80GB PCIe
GPU 7: NVIDIA A100 80GB PCIe

Nvidia driver version: 535.54.03
cuDNN version: Could not collect
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
CPU(s):                64
On-line CPU(s) list:   0-63
Thread(s) per core:    1
Core(s) per socket:    32
座：                 2
NUMA 节点：         2
厂商 ID：           GenuineIntel
CPU 系列：          6
型号：              106
型号名称：        Intel(R) Xeon(R) Platinum 8358P CPU @ 2.60GHz
步进：              6
CPU MHz：             800.000
CPU max MHz:           3400.0000
CPU min MHz:           800.0000
BogoMIPS：            5200.00
虚拟化：           VT-x
L1d 缓存：          48K
L1i 缓存：          32K
L2 缓存：           1280K
L3 缓存：           49152K
NUMA 节点0 CPU：    0-31
NUMA 节点1 CPU：    32-63
Flags:                 fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch epb cat_l3 intel_pt ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq spec_ctrl intel_stibp flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] numpy==1.26.3
[pip3] open-clip-torch==2.20.0
[pip3] pytorch-lightning==1.9.4
[pip3] torch==2.0.1+cu118
[pip3] torchdiffeq==0.2.3
[pip3] torchmetrics==1.3.0
[pip3] torchsde==0.2.6
[pip3] torchvision==0.15.2+cu118
[pip3] triton==2.0.0
[conda] numpy                     1.26.3                   pypi_0    pypi
[conda] open-clip-torch           2.20.0                   pypi_0    pypi
[conda] pytorch-lightning         1.9.4                    pypi_0    pypi
[conda] torch                     2.0.1+cu118              pypi_0    pypi
[conda] torchdiffeq               0.2.3                    pypi_0    pypi
[conda] torchmetrics              1.3.0                    pypi_0    pypi
[conda] torchsde                  0.2.6                    pypi_0    pypi
[conda] torchvision               0.15.2+cu118             pypi_0    pypi
[conda] triton                    2.0.0                    pypi_0    pypi
```

cc @ptrblck