Repository: pytorch
Issue: 2D inputs to linear layers run up to 25% slower than 4D ones on some Nvidia GPUs
Labels: triaged,module: cublas

### 🐛 Describe the bug

On an Nvidia RTX 3090 GPU, 2D inputs to linear layers result in up to 25% slower performance than 4D inputs -- even though the number of elements in the input is the same (e.g. input shapes `[B, C]` vs `[B, 1, 1, C]`). The difference is smaller on an RTX A5000 GPU, and almost disappears on an A100 80GB GPU.

Averages of 100 runs:

| Input shape    | RTX 3090 |  RTX A5000  | A100 80GB |
| -------- | ------- | ------- | ------- |
| [524288, 256]         | 51.9 ms    | 59.0 ms |  35.1 ms |
| [524288, 1, 256]      | 51.4 ms     | 59.3 ms |  34.9 ms |
| [524288, 1, 1, 256]    |   40.1 ms  | 54.8 ms | 34.5 ms |


The numbers above are for Pytorch 2.0.1 with CUDA 11.7; for RTX 3090, the same behavior was observed for Pytorch 1.13.1, both with CUDA 11.7 and CUDA 11.8.

Benchmark code:

```
import torch
from torch.utils import benchmark


def main():
    torch.manual_seed(1)
    device = torch.device('cuda')
    print(torch.__version__, torch.version.cuda, torch.cuda.get_device_name(device))

    net = torch.nn.Sequential(*[torch.nn.Linear(256, 256) for _ in range(8)])
    net = net.to(device)
    x = torch.rand(2 ** 19, 256, device=device)

    for _ in range(3):
        print(f'\n{x.shape=}, {x.is_contiguous()=}')
        tmr = benchmark.Timer('net(x)', globals={'net': net, 'x': x})
        print(tmr.timeit(100))
        x = x.unsqueeze(1)


if __name__ == '__main__':
    main()
```

### Versions

```
Collecting environment information...
PyTorch version: 2.0.1+cu117
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Debian GNU/Linux 11 (bullseye) (x86_64)
GCC version: (Debian 10.2.1-6) 10.2.1 20210110
Clang version: Could not collect
CMake version: version 3.18.4
Libc version: glibc-2.31

Python version: 3.10.11 (main, Apr 20 2023, 19:02:41) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.10.0-21-amd64-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 3090
Nvidia driver version: 470.129.06
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
Address sizes:                   46 bits physical, 48 bits virtual
CPU(s):                          24
On-line CPU(s) list:             0-23
Thread(s) per core:              2
Core(s) per socket:              12
Socket(s):                       1
NUMA node(s):                    1
Vendor ID:                       GenuineIntel
CPU family:                      6
Model:                           85
Model name:                      Intel(R) Core(TM) i9-10920X CPU @ 3.50GHz
Stepping:                        7
CPU MHz:                         1278.993
CPU max MHz:                     4800.0000
CPU min MHz:                     1200.0000
BogoMIPS:                        6999.82
Virtualization:                  VT-x
L1d cache:                       384 KiB
L1i cache:                       384 KiB
L2 cache:                        12 MiB
L3 cache:                        19.3 MiB
NUMA node0 CPU(s):               0-23
Vulnerability Itlb multihit:     KVM: Mitigation: VMX disabled
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:          Mitigation; Enhanced IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Mitigation; TSX disabled
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 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 cdp_l3 invpcid_single ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512_vnni md_clear flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] numpy==1.24.2
[pip3] torch==2.0.1
[pip3] torch-tb-profiler==0.4.1
[pip3] triton==2.0.0
[conda] Could not collect
```

cc @csarofeen @ptrblck @xwang233