Repository: pytorch/pytorch

Issue:
Initializing libiomp5.dylib, but found libomp.dylib already initialized.
### 🐛 Describe the bug

The issue appears on MacOS py3.8, it started after updating to the latest nightly `1.13.0.dev20220525-py3.8_0` from core (previously I was at `1.12.0.dev20220309-py3.8_0`, so the issue could have been introduced earlier than May 25th). I'm receiving the following after importing numpy and pytorch together:
```
$ python -c "import numpy;import torch"
OMP: Error pytorch/vision#15: Initializing libiomp5.dylib, but found libomp.dylib already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.
```


```
python3 -mtorch.utils.collect_env
Collecting environment information...
PyTorch version: 1.13.0.dev20220525
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.3.1 (x86_64)
GCC version: Could not collect
Clang version: 12.0.0 (clang-1200.0.32.21)
CMake version: version 3.18.4
Libc version: N/A

Python version: 3.8.12 | packaged by conda-forge | (default, Oct 12 2021, 21:50:38)  [Clang 11.1.0 ] (64-bit runtime)
Python platform: macOS-10.16-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
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: True

Versions of relevant libraries:
[pip3] efficientnet-pytorch==0.7.1
[pip3] mypy==0.931
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.22.4
[pip3] torch==1.13.0.dev20220525
[pip3] torchdata==0.4.0a0+652986b
[pip3] torchvision==0.14.0a0+9a72fd6
[pip3] torchviz==0.0.2
[conda] blas                      2.112                       mkl    conda-forge
[conda] blas-devel                3.9.0              12_osx64_mkl    conda-forge
[conda] efficientnet-pytorch      0.7.1                    pypi_0    pypi
[conda] libblas                   3.9.0              12_osx64_mkl    conda-forge
[conda] libcblas                  3.9.0              12_osx64_mkl    conda-forge
[conda] liblapack                 3.9.0              12_osx64_mkl    conda-forge
[conda] liblapacke                3.9.0              12_osx64_mkl    conda-forge
[conda] mkl                       2021.4.0           h89fa619_689    conda-forge
[conda] mkl-devel                 2021.4.0           h694c41f_690    conda-forge
[conda] mkl-include               2021.4.0           hf224eb6_689    conda-forge
[conda] numpy                     1.22.4           py38h3ad0702_0    conda-forge
[conda] pytorch                   1.13.0.dev20220525         py3.8_0    pytorch-nightly
[conda] torchdata                 0.4.0a0+652986b          pypi_0    pypi
[conda] torchvision               0.14.0a0+9a72fd6           dev_0    <develop>
[conda] torchviz                  0.0.2                    pypi_0    pypi
```
Strangely, importing first torch works:
```
python -c "import torch;import numpy;print('works')"
works
```

Setting `KMP_DUPLICATE_LIB_OK=TRUE` as env var solves the issue while invoking from console:
```
KMP_DUPLICATE_LIB_OK=TRUE python -c "import numpy;import torch;print('works')"                                         
works
```
Sometimes I get segfaults thought, this doesn't seem like a stable solution.

### Versions

Latest Core nightly (20220525).


Discussion:
I've ended "fixing" the issue by wiping my conda setup, moving to py3.10 and reinstalling dependencies. The issue was definitely not related to TorchVision as I managed to reproduce it only using Core. It could be related to some weird dependency on my system though.

As discussed with @atalman I'll leave the issue open just in case this is something suspicious that needs to be investigated on the future. I know there were a bunch of macOS changes recently on OK. Feel free to close or move to Core.
@datumbox you should have run `python3 -mtorch.utils.collect_env` and posted results here. What I suspect has happened: in your env you've installed PyTorch from wheels, but numpy from conda-forge

Looks like its conda issue, I was able to reproduce this:

conda install pytorch torchvision torchaudio -c pytorch-nightly

```
 python -c "import numpy;import torch"
OMP: Error pytorch/vision#15: Initializing libiomp5.dylib, but found libomp.dylib already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.
Abort trap: 6

```
@atalman can you please run `python3 -mtorch.utils.collect_env` and post results here?
Here they are, this config was failing

```
python3 -mtorch.utils.collect_env
Collecting environment information...
PyTorch version: 1.13.0.dev20220525
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.3.1 (x86_64)
GCC version: Could not collect
Clang version: 13.1.6 (clang-1316.0.21.2.3)
CMake version: Could not collect
Libc version: N/A

Python version: 3.9.7 (default, Sep 16 2021, 08:50:36)  [Clang 10.0.0 ] (64-bit runtime)
Python platform: macOS-10.16-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
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: True

Versions of relevant libraries:
[pip3] mypy-extensions==0.4.3
[pip3] numpy==1.20.3
[pip3] numpydoc==1.1.0
[pip3] torch==1.13.0.dev20220525
[pip3] torchaudio==0.12.0.dev20220525
[pip3] torchvision==0.14.0.dev20220525
[conda] blas                      1.0                         mkl  
[conda] mkl                       2021.4.0           hecd8cb5_637  
[conda] mkl-service               2.4.0            py39h9ed2024_0  
[conda] mkl_fft                   1.3.1            py39h4ab4a9b_0  
[conda] mkl_random                1.2.2            py39hb2f4e1b_0  
[conda] numpy                     1.20.3           py39h4b4dc7a_0  
[conda] numpy-base                1.20.3           py39he0bd621_0  
[conda] numpydoc                  1.1.0              pyhd3eb1b0_1  
[conda] pytorch                   1.13.0.dev20220525         py3.9_0    pytorch-nightly
[conda] torch                     1.12.0                   pypi_0    pypi
[conda] torchaudio                0.12.0.dev20220511          pypi_0    pypi
[conda] torchvision               0.13.0.dev20220511          pypi_0    pypi
```
@malfet I've updated the issue with the requested output. In my case your suspicion seems correct which is different from what @atalman reports. Hope it helps. Feel free to move this issue on PyTorch core if you want, as this is not related to TorchVision.
@datumbox @malfet 

Looks like I was able to mitigate this issue by doing following

```
conda conda uninstall intel-openmp
conda install -c intel openmp
conda install numpy
conda install pytorch torchvision torchaudio -c pytorch-nightly
```


So looks like problem could be related to openmp

```
python3 -mtorch.utils.collect_env
Collecting environment information...
PyTorch version: 1.13.0.dev20220525
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 12.3.1 (x86_64)
GCC version: Could not collect
Clang version: 13.1.6 (clang-1316.0.21.2.3)
CMake version: Could not collect
Libc version: N/A

Python version: 3.9.7 (default, Sep 16 2021, 08:50:36)  [Clang 10.0.0 ] (64-bit runtime)
Python platform: macOS-10.16-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
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: True

Versions of relevant libraries:
[pip3] numpy==1.22.3
[pip3] torch==1.13.0.dev20220525
[pip3] torchaudio==0.12.0.dev20220525
[pip3] torchvision==0.14.0.dev20220525
[conda] blas                      1.0                         mkl  
[conda] mkl                       2021.4.0           hecd8cb5_637  
[conda] mkl-service               2.4.0            py39h9ed2024_0  
[conda] mkl_fft                   1.3.1            py39h4ab4a9b_0  
[conda] mkl_random                1.2.2            py39hb2f4e1b_0  
[conda] numpy                     1.22.3           py39h2e5f0a9_0  
[conda] numpy-base                1.22.3           py39h3b1a694_0  
[conda] pytorch                   1.13.0.dev20220525         py3.9_0    pytorch-nightly
[conda] torch                     1.12.0                   pypi_0    pypi
[conda] torchaudio                0.12.0.dev20220511          pypi_0    pypi
[conda] torchvision               0.13.0.dev20220511          pypi_0    pypi
```
Ok, can we please clarify the following: what conda package provides libiomp5.dylib? If it's provided by MKL base, that we do not need to bundle it with the package, but rather take it from conda, i.e. following function should not be called when building conda packages for MacOS https://github.com/pytorch/pytorch/blob/18d46ea9fd5071820cdbe7595b99396a9153f716/setup.py#L432
After doing some more research I found out that this error is resolved by 

```
conda remove mkl
```

 however we can reintroduce this issue by installing 

```
conda install numpy-base=1.22.3
```
which installs following packages:

```
  numpy-base         pkgs/main/osx-64::numpy-base-1.22.3-py310hfd2de13_0
```



Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -310,6 +310,7 @@ def report(*args):
 # Version, create_version_file, and package_name
 ################################################################################
 package_name = os.getenv('TORCH_PACKAGE_NAME', 'torch')
+package_type = os.getenv('PACKAGE_TYPE', 'wheel')
 version = get_torch_version()
 report("Building wheel {}-{}".format(package_name, version))
 
@@ -457,8 +458,7 @@ class build_ext(setuptools.command.build_ext.build_ext):
 
     # Copy libiomp5.dylib inside the wheel package on OS X
     def _embed_libiomp(self):
-        if not IS_DARWIN:
-            return
+
         lib_dir = os.path.join(self.build_lib, 'torch', 'lib')
         libtorch_cpu_path = os.path.join(lib_dir, 'libtorch_cpu.dylib')
         if not os.path.exists(libtorch_cpu_path):
@@ -550,7 +550,8 @@ def run(self):
         # It's an old-style class in Python 2.7...
         setuptools.command.build_ext.build_ext.run(self)
 
-        self._embed_libiomp()
+        if IS_DARWIN and package_type != 'conda':
+            self._embed_libiomp()
 
         # Copy the essential export library to compile C++ extensions.
         if IS_WINDOWS:


Test patch: