Repository: pytorch
Issue: torch.argmax slow on CPU
Labels: module: performance,module: cpu,triaged

### 🐛 Describe the bug

torch.argmax is slow on CPU relative to the numpy implementation. I would expect these two to be approximately equally fast. Here is a small script to reproduce this:

```python
import torch
import numpy as np
from time import time

if __name__ == '__main__':
    torch.set_num_threads(1)
    tmp = np.random.random((2, 256, 256, 256))

    tmp_torch = torch.tensor(tmp)

    start = time()
    _ = np.argmax(tmp, 0)
    print(f'numpy:\t\t {time() - start}')
    start = time()
    _ = torch.argmax(tmp_torch, 0)
    print(f'torch:\t\t {time() - start}')

    tmp_torch = tmp_torch.to(torch.device('cuda'))
    _ = torch.argmax(tmp_torch, 0)

    start = time()
    _ = torch.argmax(tmp_torch, 0)
    print(f'torch (CUDA):\t {time() - start}')


    tmp = np.random.random((2, 256, 256))
    tmp_torch = torch.tensor(tmp)

    start = time()
    for _ in range(250):
        _ = np.argmax(tmp, 0)
    print(f'numpy:\t\t {time() - start}')
    start = time()
    for _ in range(250):
        _ = torch.argmax(tmp_torch, 0)
    print(f'torch:\t\t {time() - start}')
    tmp_torch = tmp_torch.to(torch.device('cuda'))
    _ = torch.argmax(tmp_torch, 0)

    start = time()
    for _ in range(250):
        _ = torch.argmax(tmp_torch, 0)
    print(f'torch (CUDA):\t {time() - start}')
```

> numpy:		 0.21234750747680664
> torch:		 1.553704023361206
> torch (CUDA):	 2.7418136596679688e-05
> numpy:		 0.17934107780456543
> torch:		 1.4539289474487305
> torch (CUDA):	 0.0014312267303466797

The numpy implementation is about 7x faster (while also using just one thread).
Best,
Fabian



### Versions

Collecting environment information...
PyTorch version: 2.5.0
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.5 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.12.7 | packaged by conda-forge | (main, Oct  4 2024, 16:05:46) [GCC 13.3.0] (64-bit runtime)
Python platform: Linux-6.8.0-48-generic-x86_64-with-glibc2.35
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 4090
Nvidia driver version: 565.57.01
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):                               16
On-line CPU(s) list:                  0-15
Vendor ID:                            AuthenticAMD
Model name:                           AMD Ryzen 7 5800X3D 8-Core Processor
CPU family:                           25
Model:                                33
Thread(s) per core:                   2
Core(s) per socket:                   8
Socket(s):                            1
Stepping:                             2
Frequency boost:                      enabled
CPU max MHz:                          4548,8281
CPU min MHz:                          2200,0000
BogoMIPS:                             6800.20
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 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 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 ibrs ibpb stibp vmmcall fsgsbase bmi1 avx2 smep bmi2 erms 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 user_shstk clzero irperf xsaveerptr rdpru wbnoinvd arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif v_spec_ctrl umip pku ospke vaes vpclmulqdq rdpid overflow_recov succor smca fsrm debug_swap
L1d cache:                            256 KiB (8 instances)
L1i cache:                            256 KiB (8 instances)
L2 cache:                             4 MiB (8 instances)
L3 cache:                             96 MiB (1 instance)
NUMA node(s):                         1
NUMA node0 CPU(s):                    0-15
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 Reg file data sampling: Not affected
Vulnerability Retbleed:               Not affected
Vulnerability Spec rstack overflow:   Mitigation; Safe RET
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; Retpolines; IBPB conditional; IBRS_FW; STIBP always-on; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected
Vulnerability Srbds:                  Not affected
Vulnerability Tsx async abort:        Not affected

Versions of relevant libraries:
[pip3] fft-conv-pytorch==1.2.0
[pip3] numpy==2.0.2
[pip3] numpydoc==1.8.0
[pip3] torch==2.5.0
[pip3] torchaudio==2.5.0
[pip3] torchvision==0.20.0
[pip3] triton==3.1.0
[conda] blas                      2.116                       mkl    conda-forge
[conda] blas-devel                3.9.0            16_linux64_mkl    conda-forge
[conda] cuda-cudart               12.4.127                      0    nvidia
[conda] cuda-cupti                12.4.127                      0    nvidia
[conda] cuda-libraries            12.4.1                        0    nvidia
[conda] cuda-nvrtc                12.4.127                      0    nvidia
[conda] cuda-nvtx                 12.4.127                      0    nvidia
[conda] cuda-opencl               12.6.77                       0    nvidia
[conda] cuda-runtime              12.4.1                        0    nvidia
[conda] fft-conv-pytorch          1.2.0                    pypi_0    pypi
[conda] libblas                   3.9.0            16_linux64_mkl    conda-forge
[conda] libcblas                  3.9.0            16_linux64_mkl    conda-forge
[conda] libcublas                 12.4.5.8                      0    nvidia
[conda] libcufft                  11.2.1.3                      0    nvidia
[conda] libcurand                 10.3.7.77                     0    nvidia
[conda] libcusolver               11.6.1.9                      0    nvidia
[conda] libcusparse               12.3.1.170                    0    nvidia
[conda] liblapack                 3.9.0            16_linux64_mkl    conda-forge
[conda] liblapacke                3.9.0            16_linux64_mkl    conda-forge
[conda] libnvjitlink              12.4.127                      0    nvidia
[conda] mkl                       2022.1.0           h84fe81f_915    conda-forge
[conda] mkl-devel                 2022.1.0           ha770c72_916    conda-forge
[conda] mkl-include               2022.1.0           h84fe81f_915    conda-forge
[conda] numpy                     2.0.2                    pypi_0    pypi
[conda] numpydoc                  1.8.0                    pypi_0    pypi
[conda] pytorch                   2.5.0           py3.12_cuda12.4_cudnn9.1.0_0    pytorch
[conda] pytorch-cuda              12.4                 hc786d27_7    pytorch
[conda] pytorch-mutex             1.0                        cuda    pytorch
[conda] torchaudio                2.5.0               py312_cu124    pytorch
[conda] torchtriton               3.1.0                     py312    pytorch
[conda] torchvision               0.20.0              py312_cu124    pytorch


cc @msaroufim @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10