Repository: pytorch
Issue: Performance degradation for certain input using Conv2D
Labels: module: performance,module: cuda,module: convolution,triaged

### 🐛 Describe the bug

Hi, here is a reproduction case which encounters severe performance issue:

```
import time

import torch

dtype = torch.bfloat16
sizes = (64, 128, 513, 513)
t = torch.rand(sizes, device='cuda', dtype=dtype)
conv = torch.nn.Conv2d(128, 128, kernel_size=3, stride=2, padding=0)
conv = conv.to(device='cuda', dtype=dtype)

torch.cuda.cudart().cudaProfilerStart()
avg_time = 0
n = 5
for i in range(n):
    torch.cuda.synchronize()
    start = time.time()
    with torch.cuda.nvtx.range('conv'):
        out = conv(t)
    print(out.shape)
    torch.cuda.synchronize()
    if i > 0:
        avg_time += time.time() - start
torch.cuda.cudart().cudaProfilerStop()
print(f'Avg time: {avg_time / (n - 1):.3f} s')
```

The average time is 1.193 s.

If I change the `dtype` to `torch.float32`, the average time becomes 0.028 s. Or if I change the `sizes` to `(63, 128, 513, 513)` or `(64, 128, 512, 512)`, the average time becomes 0.019 s and 0.115 s respectively. Obviously, the original repro has a severe performance degradation.

Then, I checked the nsys timeline of each case.  I found the original repro use `cudnn::cnn::conv2d_grouped_direct_kernel`:

![image](https://github.com/pytorch/pytorch/assets/863306/f73c5303-287a-4baf-ad1e-3e96bd7eb8c1)

But `torch.float32` case and `(63, 128, 513, 513)` case seem to use cutlass implementation:

![image](https://github.com/pytorch/pytorch/assets/863306/2b0d9d54-8c18-443e-a31e-c3764587a7bf)

![image](https://github.com/pytorch/pytorch/assets/863306/4b2bd800-e248-4a7c-a610-50c00973ae5b)

And `(64, 128, 512, 512)` uses another implementation which I can't recognize:

![image](https://github.com/pytorch/pytorch/assets/863306/c3242c1c-3a73-448b-8b99-1017bb1a1daa)


So, what happened in the original case? How can I avoid the performance degradation?












### Versions

cuDNN version is 8700 (I don't know why the script cannot collect it)

```
python -c 'import torch;print(torch.backends.cudnn.version())'
8700
```


```
Collecting environment information...
PyTorch version: 2.3.0+cu118
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Alibaba Group Enterprise Linux Server 7.2 (Paladin) (x86_64)
GCC version: (GCC) 4.9.2
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.17

Python version: 3.10.14 (main, May  6 2024, 19:42:50) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.10.112-005.ali5000.alios7.x86_64-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: 11.8.89
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA A100-SXM4-80GB
GPU 1: NVIDIA A100-SXM4-80GB
GPU 2: NVIDIA A100-SXM4-80GB
GPU 3: NVIDIA A100-SXM4-80GB
GPU 4: NVIDIA A100-SXM4-80GB
GPU 5: NVIDIA A100-SXM4-80GB
GPU 6: NVIDIA A100-SXM4-80GB
GPU 7: NVIDIA A100-SXM4-80GB

Nvidia driver version: 470.154
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):                128
On-line CPU(s) list:   0-127
Thread(s) per core:    2
Core(s) per socket:    32
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 106
Model name:            Intel(R) Xeon(R) Platinum 8369B CPU @ 2.90GHz
Stepping:              6
CPU MHz:               3500.000
CPU max MHz:           3500.0000
CPU min MHz:           800.0000
BogoMIPS:              5800.00
Virtualization:        VT-x
L1d cache:             48K
L1i cache:             32K
L2 cache:              1280K
L3 cache:              49152K
NUMA node0 CPU(s):     0-31,64-95
NUMA node1 CPU(s):     32-63,96-127
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 cpuid aperfmperf pni pclmulqdq dtes64 monitor 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 cpuid_fault epb cat_l3 invpcid_single intel_ppin ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq rdpid fsrm md_clear pconfig flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] pytorch-lightning==1.5.10
[pip3] torch==2.3.0+cu118
[pip3] torch-fidelity==0.3.0
[pip3] torchaudio==2.3.0+cu118
[pip3] torchmetrics==0.6.0
[pip3] torchvision==0.18.0+cu118
[pip3] triton==2.3.0
[conda] numpy                     1.26.4          py310hb13e2d6_0    conda-forge
[conda] pytorch-lightning         1.5.10                   pypi_0    pypi
[conda] torch                     2.3.0+cu118              pypi_0    pypi
[conda] torch-fidelity            0.3.0                    pypi_0    pypi
[conda] torchaudio                2.3.0+cu118              pypi_0    pypi
[conda] torchmetrics              0.6.0                    pypi_0    pypi
[conda] torchvision               0.18.0+cu118             pypi_0    pypi
[conda] triton                    2.3.0                    pypi_0    pypi
```

cc @msaroufim @ptrblck