Repository: pytorch
Issue: The cuda batched GEMM has a poor performance for bigger batch size with smaller matrix size
Labels: module: performance,module: cuda,triaged,matrix multiplication

### 🐛 Describe the bug

The batched GEMM has a poor performance for bigger batch size(`12*7*120*64*129`) with smaller matrix size(`3x3`, `3x1`):

```python
import torch
import time

points = torch.randn(12, 7, 120, 64, 120, 3).cuda()
bda = torch.randn(12, 3, 3).cuda()
  
B = 12

out = bda.view(B, 1, 1, 1, 1, 3, 3).mul(points.unsqueeze(-2)).sum(dim=-1, keepdim=True)
ref = bda.view(B, 1, 1, 1, 1, 3, 3).matmul(points.unsqueeze(-1))

# compare result
print(torch.allclose(ref, out, rtol=1e-05, atol=1e-06))

# warm up for torch.matmul
for _ in range(100):
    ref = bda.view(B, 1, 1, 1, 1, 3, 3).matmul(points.unsqueeze(-1))

# colect time for torch.matmul
torch.cuda.synchronize()
t0 = time.time()
for _ in range(100):
    ref = bda.view(B, 1, 1, 1, 1, 3, 3).matmul(points.unsqueeze(-1))
torch.cuda.synchronize()
t1 = time.time()

# warm up for mul+sum
for _ in range(100):
    out = bda.view(B, 1, 1, 1, 1, 3, 3).mul(points.unsqueeze(-2)).sum(dim=-1, keepdim=True)

# collect time for mul+sum
torch.cuda.synchronize()
t2 = time.time()
for _ in range(100):
    out = bda.view(B, 1, 1, 1, 1, 3, 3).mul(points.unsqueeze(-2)).sum(dim=-1, keepdim=True)
torch.cuda.synchronize()
t3 = time.time()

print("matmul time:", (t1 - t0) / 100)
print("mul and sum time:", (t3 - t2) / 100)
```

the matmul gets a poor performance compared with the mul+sum implementation(11x):

```bash
matmul time: 0.08466132402420044
mul and sum time: 0.0075583791732788085
```


### Versions

```bash
PyTorch version: 2.1.1+cu118
Is debug build: False
CUDA used to build PyTorch: 11.8
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.5 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Clang version: Could not collect
CMake version: version 3.24.1
Libc version: glibc-2.31

Python version: 3.8.10 (default, Jun 22 2022, 20:18:18)  [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-3.10.0-1160.el7.x86_64-x86_64-with-glibc2.29
Is CUDA available: True
CUDA runtime version: 11.8.89
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA Graphics Device
GPU 1: NVIDIA Graphics Device
GPU 2: NVIDIA Graphics Device
GPU 3: NVIDIA Graphics Device
GPU 4: NVIDIA Graphics Device
GPU 5: NVIDIA Graphics Device
GPU 6: NVIDIA Graphics Device
GPU 7: NVIDIA Graphics Device

Nvidia driver version: 470.103.01
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.7.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.7.0
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.7.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.7.0
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.7.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.7.0
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.7.0
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, 57 bits virtual
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:             3499.859
CPU max MHz:         3500.0000
CPU min MHz:         800.0000
BogoMIPS:            5800.00
Virtualization:      VT-x
L1d cache:           3 MiB
L1i cache:           2 MiB
L2 cache:            80 MiB
L3 cache:            96 MiB
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 aperfmperf eagerfpu 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 epb cat_l3 invpcid_single 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 hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq md_clear pconfig spec_ctrl intel_stibp flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] flake8==6.0.0
[pip3] functorch==1.13.0a0+936e930
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.24.4
[pip3] onnx==1.12.0
[pip3] onnx-graphsurgeon==0.3.25
[pip3] onnxruntime==1.13.1
[pip3] onnxsim==0.4.10
[pip3] pytorch-quantization==2.1.2
[pip3] torch==2.1.1+cu118
[pip3] torch-scatter==2.1.0
[pip3] torch-tb-profiler==0.4.3
[pip3] torch-tensorrt==1.3.0a0
[pip3] torchtext==0.13.0a0+fae8e8c
[pip3] torchvision==0.15.0a0
[pip3] triton==2.1.
```

Install PyTorch using ```pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118```

cc @ptrblck