Repository: pytorch
Issue: Addmm is not always better than add + mm
Labels: module: performance,triaged,matrix multiplication

### 🐛 Describe the bug

I met a performance problem when training GPT-3-like models, which contain matrix multiplication like [b, 12288] @ [12288, 36864] (QKV) and [b, 12288] @ [12288, 49152] (FFN). And I found out that the MFU of matrix multiplication here is pretty low. So I went deeper and found out a strange phenomenon: the performance of addmm is worse than mm + add in some cases. 

For  [32768, 12288] @ [12288, 36864], when initializing the values with zeros, we got 155ms for addmm_op, 104.44ms for separate_ops. The MFU is 0.61 and 0.91, respectively.
For  [32768, 12288] @ [12288, 49152], when initializing the values with zeros, we got 206ms for addmm_op, 138.84 for separate_ops. The MFU is 0.61 and 0.91, respectively.

Below are my codes for this experiement. I think fused operators ought to be better than non-fusion ones. What could be wrong?


```python
import torch
from torch.utils.benchmark import Timer

def profile_addmm(data_size, h1, h2):
    # @torch.compile
    def addmm_op(input, mat1, mat2):
        return torch.addmm(input, mat1, mat2)

    # @torch.compile
    def separate_ops(input, mat1, mat2):
        return input + torch.mm(mat1, mat2)

    print(f'data_size: {data_size}, h1: {h1}, h2: {h2}')
    FLOPS = 2 * data_size * h1 * h2

    bias = torch.randn(h2, device='cuda', dtype=torch.half)
    mat1 = torch.randn(data_size, h1, device='cuda', dtype=torch.half)
    mat2 = torch.randn(h1, h2, device='cuda', dtype=torch.half)

    bias.zero_()
    mat1.zero_()
    mat2.zero_()

    t1 = Timer(stmt='addmm_op(bias, mat1, mat2)', globals=locals())
    t2 = Timer(stmt='separate_ops(bias, mat1, mat2)', globals=locals())

    addmm_time = t1.timeit(10).mean
    mm_add_time = t2.timeit(10).mean

    print(addmm_time)
    print(f"addmm MFU: {FLOPS / addmm_time  / 1e12 / 312}")
    print(f"mm_add MFU: {FLOPS / mm_add_time / 1e12 / 312}")



if __name__ == '__main__':
    profile_addmm(32768, 12288, 36864)
    # randn: 156ms for addmm_op, 122.55ms for separate_ops
    # zeros: 154ms for addmm_op, 104ms for separate_ops

    profile_addmm(32768, 12288, 49152)
    # randn: 208.6ms for addmm_op, 167.85ms for separate_ops
    # zeros: 206ms for addmm_op, 138.84ms for separate_ops

```

I also checked the kernels they called with torch profiler. For [32768, 12288] @ [12288, 49152], addmm calls the `sm80_xmma_gemm_f16f16_f16f32_f32_nn_n_tilesize160x128x32_stage4_warpsize2x2x1_tensor16x8x16_kernel`, and seperate_op calls the `ampere_fp16_s16816gemm_fp16_128x256_ldg8_f2f_stages_64x3_nn` and `void at::native::elementwise_kernel<128, 4, at::native::gpu_kernel_impl_nocast<at::native::CUDAFunctor_add<c10::Half> >(at::TensorIteratorBase&, at::native::CUDAFunctor_add<c10::Half> const&)::{lambda(int)#1}>(int, at::native::gpu_kernel_impl_nocast<at::native::CUDAFunctor_add<c10::Half> >(at::TensorIteratorBase&, at::native::CUDAFunctor_add<c10::Half> const&)::{lambda(int)#1})`.

If I compile the seprate_op function first, it will be fused to be an addmm operator, also resulting in performance degradation.

### Versions

Collecting environment information...
PyTorch version: 2.4.1+cu121
Is debug build: False
CUDA used to build PyTorch: 12.1
ROCM used to build PyTorch: N/A

OS: CentOS Linux 7 (Core) (x86_64)
GCC version: (GCC) 9.3.1 20200408 (Red Hat 9.3.1-2)
Clang version: Could not collect
CMake version: version 3.28.3
Libc version: glibc-2.17

Python version: 3.9.12 (main, Apr  5 2022, 06:56:58)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-4.18.0-147.mt20200626.413.el8_1.x86_64-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: 12.1.105
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA Graphics Device
GPU 1: NVIDIA Graphics Device

Nvidia driver version: 470.103.01
cuDNN version: Probably one of the following:
/usr/lib64/libcudnn.so.8.9.2
/usr/lib64/libcudnn_adv_infer.so.8.9.2
/usr/lib64/libcudnn_adv_train.so.8.9.2
/usr/lib64/libcudnn_cnn_infer.so.8.9.2
/usr/lib64/libcudnn_cnn_train.so.8.9.2
/usr/lib64/libcudnn_ops_infer.so.8.9.2
/usr/lib64/libcudnn_ops_train.so.8.9.2
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):                256
On-line CPU(s) list:   0-35
Off-line CPU(s) list:  36-255
Thread(s) per core:    0
Core(s) per socket:    64
Socket(s):             2
NUMA node(s):          2
Vendor ID:             AuthenticAMD
CPU family:            25
Model:                 1
Model name:            AMD EPYC 7713 64-Core Processor
Stepping:              1
CPU MHz:               3075.921
CPU max MHz:           2000.0000
CPU min MHz:           1500.0000
BogoMIPS:              3992.55
Virtualization:        AMD-V
L1d cache:             32K
L1i cache:             32K
L2 cache:              512K
L3 cache:              32768K
NUMA node0 CPU(s):     0-63,128-191
NUMA node1 CPU(s):     64-127,192-255
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 nopl xtopology nonstop_tsc cpuid extd_apicid aperfmperf pni pclmulqdq monitor ssse3 fma cx16 pcid 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 invpcid_single hw_pstate sme ssbd mba sev ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 invpcid cqm rdt_a rdseed adx smap clflushopt clwb sha_ni xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local clzero irperf xsaveerptr wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold v_vmsave_vmload vgif umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca

Versions of relevant libraries:
[pip3] denoising-diffusion-pytorch==2.0.17
[pip3] ema-pytorch==0.6.2
[pip3] flake8==7.0.0
[pip3] numpy==1.24.1
[pip3] nvidia-cublas-cu12==12.1.3.1
[pip3] nvidia-cuda-cupti-cu12==12.1.105
[pip3] nvidia-cuda-nvrtc-cu12==12.1.105
[pip3] nvidia-cuda-runtime-cu12==12.1.105
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-cufft-cu12==11.0.2.54
[pip3] nvidia-curand-cu12==10.3.2.106
[pip3] nvidia-cusolver-cu12==11.4.5.107
[pip3] nvidia-cusparse-cu12==12.1.0.106
[pip3] nvidia-nccl-cu12==2.20.5
[pip3] nvidia-nvjitlink-cu12==12.1.105
[pip3] nvidia-nvtx-cu12==12.1.105
[pip3] pytorch-fid==0.3.0
[pip3] torch==2.4.1
[pip3] torchaudio==2.4.1
[pip3] torchvision==0.19.1
[pip3] torchviz==0.0.2
[pip3] triton==3.0.0
[conda] denoising-diffusion-pytorch 2.0.17                   pypi_0    pypi
[conda] ema-pytorch               0.6.2                    pypi_0    pypi
[conda] numpy                     1.24.1                   pypi_0    pypi
[conda] nvidia-cublas-cu12        12.1.3.1                 pypi_0    pypi
[conda] nvidia-cuda-cupti-cu12    12.1.105                 pypi_0    pypi
[conda] nvidia-cuda-nvrtc-cu12    12.1.105                 pypi_0    pypi
[conda] nvidia-cuda-runtime-cu12  12.1.105                 pypi_0    pypi
[conda] nvidia-cudnn-cu12         9.1.0.70                 pypi_0    pypi
[conda] nvidia-cufft-cu12         11.0.2.54                pypi_0    pypi
[conda] nvidia-curand-cu12        10.3.2.106               pypi_0    pypi
[conda] nvidia-cusolver-cu12      11.4.5.107               pypi_0    pypi
[conda] nvidia-cusparse-cu12      12.1.0.106               pypi_0    pypi
[conda] nvidia-nccl-cu12          2.20.5                   pypi_0    pypi
[conda] nvidia-nvjitlink-cu12     12.1.105                 pypi_0    pypi
[conda] nvidia-nvtx-cu12          12.1.105                 pypi_0    pypi
[conda] pytorch-fid               0.3.0                    pypi_0    pypi
[conda] torch                     2.4.1                    pypi_0    pypi
[conda] torchaudio                2.4.1                    pypi_0    pypi
[conda] torchvision               0.19.1                   pypi_0    pypi
[conda] torchviz                  0.0.2                    pypi_0    pypi
[conda] triton                    3.0.0                    pypi_0    pypi


cc @msaroufim