Repository: pytorch
Issue: Build with MKL and OpenMP produces libtorch_global_deps.so with undefined symbol omp_get_max_active_levels
Labels: module: dependency bug,module: binaries,triaged

### 🐛 Describe the bug

Hi, as a part of https://bugs.gentoo.org/921129 I'm adding support of MKL into caffe2 ebuild. While testing the resulting build, attempt to import pytorch failed in _load_global_deps(), while loading libtorch_global_deps.so at https://github.com/pytorch/pytorch/blob/v2.1.2/torch/__init__.py#L196 with
```
OSError: /opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_intel_thread.so.2: undefined symbol: omp_get_max_active_levels
```

and indeed, this symbol is [u]ndefined in `libmkl_intel_thread.so.2` and `libtorch_global_deps.so`  is not linked to openmp:
```bash
$ nm /opt/intel/oneapi/mkl/latest/lib/intel64/libmkl_intel_thread.so.2 | grep omp_get_max_active_levels
                 U omp_get_max_active_levels

$ ldd /usr/lib64/libtorch_cpu.so | grep omp
<nothing>
```

The underlying cmake configuration command was the following:
```bash
cmake -C /var/tmp/portage/sci-libs/caffe2-2.1.2-r2/work/pytorch-2.1.2_build/gentoo_common_config.cmake -G Ninja -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_CUSTOM_PROTOBUF=OFF -DBUILD_SHARED_LIBS=ON -DUSE_CCACHE=OFF -DUSE_CUDA=no -DUSE_CUDNN=no -DTORCH_CUDA_ARCH_LIST=3.5 7.0 -DBUILD_NVFUSER=no -DUSE_DISTRIBUTED=no -DUSE_MPI=no -DUSE_FAKELOWP=OFF -DUSE_FBGEMM=no -DUSE_FFMPEG=no -DUSE_GFLAGS=ON -DUSE_GLOG=ON -DUSE_GLOO=no -DUSE_KINETO=OFF -DUSE_LEVELDB=OFF -DUSE_MAGMA=OFF -DUSE_MKLDNN=OFF -DUSE_NCCL=OFF -DUSE_NNPACK=no -DUSE_QNNPACK=no -DUSE_XNNPACK=no -DUSE_SYSTEM_XNNPACK=no -DUSE_TENSORPIPE=no -DUSE_PYTORCH_QNNPACK=OFF -DUSE_NUMPY=yes -DUSE_OPENCL=no -DUSE_OPENCV=no -DUSE_OPENMP=yes -DUSE_ROCM=OFF -DUSE_SYSTEM_CPUINFO=ON -DUSE_SYSTEM_PYBIND11=ON -DUSE_UCC=OFF -DUSE_VALGRIND=OFF -DPYBIND11_PYTHON_VERSION=3.11 -DPYTHON_EXECUTABLE=/usr/bin/python3.11 -DUSE_ITT=OFF -DUSE_SYSTEM_PTHREADPOOL=ON -DUSE_SYSTEM_FXDIV=ON -DUSE_SYSTEM_FP16=ON -DUSE_SYSTEM_GLOO=ON -DUSE_SYSTEM_ONNX=ON -DUSE_SYSTEM_SLEEF=ON -DUSE_METAL=OFF -Wno-dev -DTORCH_INSTALL_LIB_DIR=/usr/lib64 -DLIBSHM_INSTALL_LIB_SUBDIR=/usr/lib64 -DBLAS=Generic -DBLAS_LIBRARIES= -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_TOOLCHAIN_FILE=/var/tmp/portage/sci-libs/caffe2-2.1.2-r2/work/pytorch-2.1.2_build/gentoo_toolchain.cmake /var/tmp/portage/sci-libs/caffe2-2.1.2-r2/work/pytorch-2.1.2
```

As a workaround, I linked `libomp.so` to `libtorch_global_deps.so`
```diff
--- a/caffe2/CMakeLists.txt
+++ b/caffe2/CMakeLists.txt
@@ -1575,6 +1575,10 @@ if(BUILD_SHARED_LIBS)
     target_link_libraries(torch_global_deps TBB::tbb)
   endif()
 
+  if(USE_OPENMP)
+    target_link_libraries(torch_global_deps OpenMP::OpenMP_CXX)
+  endif()
+
   install(TARGETS torch_global_deps DESTINATION "${TORCH_INSTALL_LIB_DIR}")
 endif()
```

Could you include a fix into caffe2 or suggest any other solution so that it works without additional patches? Thanks!

### Versions

Collecting environment information...
PyTorch version: 2.1.0a0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Gentoo Linux (x86_64)
GCC version: (Gentoo 13.2.1_p20230826 p7) 13.2.1 20230826
Clang version: 17.0.6
CMake version: version 3.27.7
Libc version: glibc-2.37

Python version: 3.11.7 (main, Dec 25 2023, 14:17:18) [Clang 17.0.3 ] (64-bit runtime)
Python platform: Linux-6.5.7-gentoo-dist-x86_64-AMD_Ryzen_9_7950X3D_16-Core_Processor-with-glibc2.37
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: False

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):                             32
On-line CPU(s) list:                0-31
Vendor ID:                          AuthenticAMD
Model name:                         AMD Ryzen 9 7950X3D 16-Core Processor
CPU family:                         25
Model:                              97
Thread(s) per core:                 2
Core(s) per socket:                 16
Socket(s):                          1
Stepping:                           2
CPU(s) scaling MHz:                 38%
CPU max MHz:                        5759.0000
CPU min MHz:                        400.0000
BogoMIPS:                           8403.57
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 amd_lbr_v2 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 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 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid overflow_recov succor smca fsrm flush_l1d
Virtualization:                     AMD-V
L1d cache:                          512 KiB (16 instances)
L1i cache:                          512 KiB (16 instances)
L2 cache:                           16 MiB (16 instances)
L3 cache:                           128 MiB (2 instances)
NUMA node(s):                       1
NUMA node0 CPU(s):                  0-31
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 Retbleed:             Not affected
Vulnerability Spec rstack overflow: Mitigation; safe RET, no microcode
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; Enhanced / Automatic IBRS, IBPB conditional, STIBP always-on, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] numpy==1.26.2
[pip3] torch==2.1.2
[conda] Could not collect

cc @seemethere @malfet @osalpekar @atalman