Repository: pytorch
Issue: Memory allocation for Conv2d
Labels: module: cuda,module: convolution,triaged

### 🐛 Describe the bug

```Python
import torch

import torch.nn as nn

from diffusers.models.lora import LoRACompatibleConv

import os

device = torch.device("cuda:0")

def printMemInfo(str_:str = ''):

    allocated_memory = torch.cuda.memory_allocated(device)

    cached_memory = torch.cuda.memory_reserved(device)

    print(f"{str_} Allocated memory: {allocated_memory / (1024 ** 3):.2f} GB; Cached memory: {cached_memory / (1024 ** 3):.2f} GB")

conv = LoRACompatibleConv(in_channels=256, out_channels=256,

                     kernel_size=(3,3), stride=(1,1),

                     padding=(1,1)).half()

conv.to(device)

for i in range(1024, 5000, 128):

    tensor_input = torch.zeros((1, 256, 2048, i)).half().to(device)

    conv(tensor_input,1)

    printMemInfo(str(i))

    torch.cuda.empty_cache()
````
Output:
```Shell
1024 Allocated memory: 1.00 GB; Cached memory: 4.02 GB

1152 Allocated memory: 1.13 GB; Cached memory: 5.52 GB

1280 Allocated memory: 1.25 GB; Cached memory: 6.15 GB

1408 Allocated memory: 1.38 GB; Cached memory: 6.77 GB

1536 Allocated memory: 1.50 GB; Cached memory: 7.40 GB

1664 Allocated memory: 1.63 GB; Cached memory: 8.02 GB

1792 Allocated memory: 1.75 GB; Cached memory: 8.65 GB

1920 Allocated memory: 1.88 GB; Cached memory: 9.27 GB

2048 Allocated memory: 2.00 GB; Cached memory: 9.90 GB

2176 Allocated memory: 2.13 GB; Cached memory: 10.52 GB

2304 Allocated memory: 2.25 GB; Cached memory: 11.15 GB

2432 Allocated memory: 2.38 GB; Cached memory: 11.77 GB

2560 Allocated memory: 2.50 GB; Cached memory: 12.40 GB

2688 Allocated memory: 2.63 GB; Cached memory: 13.02 GB

2816 Allocated memory: 2.75 GB; Cached memory: 13.65 GB

2944 Allocated memory: 2.88 GB; Cached memory: 14.27 GB

3072 Allocated memory: 3.00 GB; Cached memory: 14.90 GB

3200 Allocated memory: 3.13 GB; Cached memory: 15.52 GB

3328 Allocated memory: 3.25 GB; Cached memory: 16.15 GB

3456 Allocated memory: 3.38 GB; Cached memory: 16.77 GB

3584 Allocated memory: 3.50 GB; Cached memory: 17.40 GB

3712 Allocated memory: 3.63 GB; Cached memory: 18.02 GB

3840 Allocated memory: 3.75 GB; Cached memory: 18.65 GB

3968 Allocated memory: 3.88 GB; Cached memory: 19.27 GB

4096 Allocated memory: 4.01 GB; Cached memory: 44.02 GB

4224 Allocated memory: 4.13 GB; Cached memory: 45.40 GB

 ```
![image](https://github.com/pytorch/pytorch/assets/20874560/9b0c0f0b-8c8c-4f05-8cb2-9b7ac9d8590d)


I expect the GPU memory usage should be the linear increasing with the width channel. But around 4000 value, it increases hugely. It is werid. Hope get help and explanation from community. 

### Versions
```Shell
python collect_env.py

Collecting environment information...

PyTorch version: 2.1.2+cu121

Is debug build: False

CUDA used to build PyTorch: 12.1

ROCM used to build PyTorch: N/A

 

OS: Ubuntu 22.04.4 LTS (x86_64)

GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0

Clang version: 14.0.0-1ubuntu1.1

CMake version: version 3.22.1

Libc version: glibc-2.35

 

Python version: 3.11.5 (main, Sep 11 2023, 13:54:46) [GCC 11.2.0] (64-bit runtime)

Python platform: Linux-6.5.0-26-generic-x86_64-with-glibc2.35

Is CUDA available: True

CUDA runtime version: 11.8.89

CUDA_MODULE_LOADING set to: LAZY

GPU models and configuration: GPU 0: NVIDIA RTX A6000

Nvidia driver version: 535.161.07

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

Address sizes:                      48 bits physical, 48 bits virtual

Byte Order:                         Little Endian

CPU(s):                             32

On-line CPU(s) list:                0-31

Vendor ID:                          AuthenticAMD

Model name:                         AMD Ryzen 9 7950X 16-Core Processor

CPU family:                         25

Model:                              97

Thread(s) per core:                 2

Core(s) per socket:                 16

Socket(s):                          1

Stepping:                           2

CPU max MHz:                        5881.0000

CPU min MHz:                        400.0000

BogoMIPS:                           8983.18

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 amd_lbr_v2 nopl nonstop_tsc cpuid extd_apicid aperfmperf rapl 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 ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid overflow_recov succor smca fsrm flush_l1d

Virtualization:                     AMD-V

L1d cache:                          512 KiB (16 instances)

L1i cache:                          512 KiB (16 instances)

L2 cache:                           16 MiB (16 instances)

L3 cache:                           64 MiB (2 instances)

NUMA node(s):                       1

NUMA node0 CPU(s):                  0-31

Vulnerability Gather data sampling: Not affected

Vulnerability Itlb multihit:        Not affected

Vulnerability L1tf:                 Not affected

Vulnerability Mds:                  Not affected

Vulnerability Meltdown:             Not affected

Vulnerability Mmio stale data:      Not affected

Vulnerability Retbleed:             Not affected

Vulnerability Spec rstack overflow: Vulnerable: Safe RET, no microcode

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:           Mitigation; Enhanced / Automatic IBRS, IBPB conditional, STIBP always-on, RSB filling, PBRSB-eIBRS Not affected

Vulnerability Srbds:                Not affected

Vulnerability Tsx async abort:      Not affected

 

Versions of relevant libraries:

[pip3] numpy==1.26.2

[pip3] open-clip-torch==2.20.0

[pip3] pytorch-lightning==1.9.4

[pip3] torch==2.1.2+cu121

[pip3] torchdiffeq==0.2.3

[pip3] torchmetrics==1.3.1

[pip3] torchsde==0.2.6

[pip3] torchvision==0.16.2+cu121

[pip3] triton==2.1.0

[conda] _anaconda_depends         2023.09             py311_mkl_1

[conda] blas                      1.0                         mkl

[conda] mkl                       2023.1.0         h213fc3f_46343

[conda] mkl-service               2.4.0           py311h5eee18b_1

[conda] mkl_fft                   1.3.8           py311h5eee18b_0

[conda] mkl_random                1.2.4           py311hdb19cb5_0

[conda] numpy                     1.26.2                   pypi_0    pypi

[conda] numpydoc                  1.5.0           py311h06a4308_0

[conda] open-clip-torch           2.20.0                   pypi_0    pypi

[conda] pytorch-lightning         1.9.4                    pypi_0    pypi

[conda] torch                     2.2.1                    pypi_0    pypi

[conda] torchdiffeq               0.2.3                    pypi_0    pypi

[conda] torchmetrics              1.3.2                    pypi_0    pypi

[conda] torchsde                  0.2.6                    pypi_0    pypi

[conda] torchvision               0.17.1                   pypi_0    pypi

[conda] triton                    2.2.0                    pypi_0    pypi
```

cc @ptrblck