Repository: pytorch
Issue: [Intel CPU] Same CPU operations on Torch 2.3+ many times slower those on Torch 2.1
Labels: needs reproduction,module: performance,module: cpu,triaged,module: regression,module: intel

### 🐛 Describe the bug

+ After installing Torch using the build_from_src method based on version **Torch 2.3+**(including 2.3.0\2.3.1\2.4.0), I noticed a significant slowdown in code performance compared to the original **Torch 2.1 version**. 
+ Through profiling, I eventually discovered that the slowdown was due to all CPU operations in Torch being much slower. 
+ I wrote a small unit test that also replicated this issue. 
+ Is this a CPU-related bug, or should I have included additional options during compilation?

#### my install cmd and options
```shell
MAKEFLAGS=-j6 PYTORCH_BUILD_VERSION=${TORCH_VERSION} PYTORCH_BUILD_NUMBER=1 USE_CUDA=1 BUILD_CAFFE2_OPS=0 BUILD_CAFFE2=0 TORCH_USE_CUDA_DSA=1 USE_DISTRIBUTED=1 USE_NCCL=1 USE_SYSTEM_NCCL=1 NCCL_INCLUDE_DIR=/usr/local/nccl/include NCCL_LIB_DIR=/usr/local/nccl/lib TORCH_CUDA_ARCH_LIST=${CUDA_ARCH_LIST} TORCH_NVCC_FLAGS="-Xfatbin -compress-all" DEBUG=1 USE_FLASH_ATTENTION=0         python3 setup.py bdist_wheel 2>&1 | tee trace.log     && pip3 install --no-cache-dir dist/*.whl
```

#### my unit test
```python3
import torch
import time

for i in range(5):
    t_a = time.time()
    a = torch.tril(torch.ones(50,50))
    t_b = time.time()
    b = torch.randn(2500)
    t_b_ = time.time()
    b_ = b.view(50,50)
    t_c = time.time()
    c = a * b_
    t_end = time.time()

    print(f"{(t_b - t_a)*1000 = }, {(t_b_ - t_b)*1000 = }, {(t_c - t_b_)*1000 = }, {(t_end - t_c)*1000 = }, {(t_end - t_a)*1000 = }")
```
#### unit test result on torch2.1
```
(t_b - t_a)*1000 = 20.094633102416992, (t_b_ - t_b)*1000 = 0.14066696166992188, (t_c - t_b_)*1000 = 0.06318092346191406, (t_end - t_c)*1000 = 0.07939338684082031, (t_end - t_a)*1000 = 20.37787437438965
(t_b - t_a)*1000 = 0.08153915405273438, (t_b_ - t_b)*1000 = 0.019788742065429688, (t_c - t_b_)*1000 = 0.0064373016357421875, (t_end - t_c)*1000 = 0.010967254638671875, (t_end - t_a)*1000 = 0.11873245239257812
(t_b - t_a)*1000 = 0.030994415283203125, (t_b_ - t_b)*1000 = 0.01621246337890625, (t_c - t_b_)*1000 = 0.0030994415283203125, (t_end - t_c)*1000 = 0.004291534423828125, (t_end - t_a)*1000 = 0.05459785461425781
(t_b - t_a)*1000 = 0.026941299438476562, (t_b_ - t_b)*1000 = 0.021219253540039062, (t_c - t_b_)*1000 = 0.0026226043701171875, (t_end - t_c)*1000 = 0.003814697265625, (t_end - t_a)*1000 = 0.05459785461425781
(t_b - t_a)*1000 = 0.027894973754882812, (t_b_ - t_b)*1000 = 0.014781951904296875, (t_c - t_b_)*1000 = 0.0026226043701171875, (t_end - t_c)*1000 = 0.003814697265625, (t_end - t_a)*1000 = 0.049114227294921875
```
#### unit test result on torch2.3
```
(t_b - t_a)*1000 = 10.82468032836914, (t_b_ - t_b)*1000 = 1.3396739959716797, (t_c - t_b_)*1000 = 0.24819374084472656, (t_end - t_c)*1000 = 0.26988983154296875, (t_end - t_a)*1000 = 12.682437896728516
(t_b - t_a)*1000 = 8.429288864135742, (t_b_ - t_b)*1000 = 0.9663105010986328, (t_c - t_b_)*1000 = 0.04506111145019531, (t_end - t_c)*1000 = 0.0743865966796875, (t_end - t_a)*1000 = 9.515047073364258
(t_b - t_a)*1000 = 3.325223922729492, (t_b_ - t_b)*1000 = 0.9529590606689453, (t_c - t_b_)*1000 = 0.028848648071289062, (t_end - t_c)*1000 = 0.07295608520507812, (t_end - t_a)*1000 = 4.379987716674805
(t_b - t_a)*1000 = 0.11420249938964844, (t_b_ - t_b)*1000 = 0.9503364562988281, (t_c - t_b_)*1000 = 0.027418136596679688, (t_end - t_c)*1000 = 0.06842613220214844, (t_end - t_a)*1000 = 1.1603832244873047
(t_b - t_a)*1000 = 0.12922286987304688, (t_b_ - t_b)*1000 = 0.9565353393554688, (t_c - t_b_)*1000 = 0.030279159545898438, (t_end - t_c)*1000 = 0.06771087646484375, (t_end - t_a)*1000 = 1.1837482452392578
```
#### my profile result
##### torch2.1
+ <img width="576" alt="image" src="https://github.com/user-attachments/assets/808045f9-5361-4c6a-af66-592cd5bd27b4">

##### torch2.3
+ <img width="693" alt="image" src="https://github.com/user-attachments/assets/a72bf0aa-fe33-49d3-8229-73e124934542">


### Versions
Collecting environment information...
PyTorch version: 2.3.1
Is debug build: True
CUDA used to build PyTorch: 12.3
ROCM used to build PyTorch: N/A

OS: Tencent tlinux 2.2 (Final) (x86_64)
GCC version: (GCC) 9.3.0
Clang version: Could not collect
CMake version: version 3.18.5
Libc version: glibc-2.17

Python version: 3.8.12 (default, Jul 12 2024, 10:30:21)  [GCC 9.3.0] (64-bit runtime)
Python platform: Linux-5.4.203-1-tlinux4-0011.spr.0001-x86_64-with-glibc2.2.5
Is CUDA available: True
CUDA runtime version: 12.3.107
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: 
GPU 0: NVIDIA H800
GPU 1: NVIDIA H800
GPU 2: NVIDIA H800
GPU 3: NVIDIA H800
GPU 4: NVIDIA H800
GPU 5: NVIDIA H800
GPU 6: NVIDIA H800
GPU 7: NVIDIA H800

Nvidia driver version: 525.105.17
cuDNN version: Probably one of the following:
/usr/lib64/libcudnn.so.9.0.0
/usr/lib64/libcudnn_adv.so.9.0.0
/usr/lib64/libcudnn_cnn.so.9.0.0
/usr/lib64/libcudnn_engines_precompiled.so.9.0.0
/usr/lib64/libcudnn_engines_runtime_compiled.so.9.0.0
/usr/lib64/libcudnn_graph.so.9.0.0
/usr/lib64/libcudnn_heuristic.so.9.0.0
/usr/lib64/libcudnn_ops.so.9.0.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
CPU(s):                192
On-line CPU(s) list:   0-191
Thread(s) per core:    2
Core(s) per socket:    48
Socket(s):             2
NUMA node(s):          2
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 143
Model name:            Intel(R) Xeon(R) Platinum 8476C
Stepping:              8
CPU MHz:               3100.000
CPU max MHz:           2601.0000
CPU min MHz:           800.0000
BogoMIPS:              5200.00
Virtualization:        VT-x
L1d cache:             48K
L1i cache:             32K
L2 cache:              2048K
L3 cache:              99840K
NUMA node0 CPU(s):     0-47,96-143
NUMA node1 CPU(s):     48-95,144-191
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 tsc_known_freq 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 cat_l2 cdp_l3 invpcid_single cdp_l2 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 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 avx_vnni avx512_bf16 wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b enqcmd fsrm md_clear serialize tsxldtrk pconfig arch_lbr amx_bf16 avx512_fp16 amx_tile amx_int8 flush_l1d arch_capabilities

Versions of relevant libraries:
[pip3] flamingo-pytorch==0.1.2
[pip3] numpy==1.24.4
[pip3] open_clip_torch==2.26.1
[pip3] optree==0.12.1
[pip3] torch==2.3.1
[pip3] torchvision==0.18.1
[conda] Could not collect

cc @msaroufim @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @frank-wei