Repository: pytorch
Issue: bundled libiomp5 causing segfaults in other libraries that use libomp
Labels: needs reproduction,module: binaries,triaged,module: macos,module: openmp

## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->

I'm seeing segfaults from Wand if torch is imported before wand.

This has started happening since the system libomp library was upgraded to 12.0.0

## To Reproduce

Running the following test script results in a Segmentation fault:

```python
import torch
from wand.image import Image

testfile = 'test.pdf'

with open(testfile, 'rb') as f:
    print("about to wand.image")
    with Image(file=f, resolution=120) as source_img:
        print("opened document")
print("done")
```
where `test.pdf` is a very simple PDF with one word on it generated by Google Docs.

```
$ python test.py
about to wand.image
Segmentation fault: 11
```

If I remove the `import torch` line, or even move it after the wand imports, the script runs correctly.

## Expected behavior

I'd expect importing torch to have no effect on other libraries like wand.

## Environment

PyTorch version: 1.9.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 11.4 (x86_64)
GCC version: Could not collect
Clang version: 12.0.5 (clang-1205.0.22.9)
CMake version: Could not collect
Libc version: N/A

Python version: 3.8 (64-bit runtime)
Python platform: macOS-11.4-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

Versions of relevant libraries:
[pip3] numpy==1.20.3
[pip3] torch==1.9.0
[conda] Could not collect

Additionally:
libomp 12.0.0 (from homebrew)
wand 0.6.6 (from pip)

## Additional context

I've run it under lldb to get some more detail about the segfault:

```
(lldb) run test.py
Process 84327 launched: '/Users/alex/.pyenv/versions/iomp_bug/bin/python' (x86_64)
about to wand.image
Process 84327 stopped
* thread #5, stop reason = EXC_BAD_ACCESS (code=1, address=0x48)
    frame #0: 0x0000000103b6aa6c libiomp5.dylib`void __kmp_suspend_64<false, true>(int, kmp_flag_64<false, true>*) + 28
libiomp5.dylib`__kmp_suspend_64<false, true>:
->  0x103b6aa6c <+28>: movq   (%rdx,%rdi,8), %r13
    0x103b6aa70 <+32>: movq   %r13, %rdi
    0x103b6aa73 <+35>: callq  0x103b5d570               ; __kmp_suspend_initialize_thread
    0x103b6aa78 <+40>: leaq   0x5c0(%r13), %r14
  thread #6, stop reason = EXC_BAD_ACCESS (code=1, address=0x50)
    frame #0: 0x0000000103b6aa6c libiomp5.dylib`void __kmp_suspend_64<false, true>(int, kmp_flag_64<false, true>*) + 28
libiomp5.dylib`__kmp_suspend_64<false, true>:
->  0x103b6aa6c <+28>: movq   (%rdx,%rdi,8), %r13
    0x103b6aa70 <+32>: movq   %r13, %rdi
    0x103b6aa73 <+35>: callq  0x103b5d570               ; __kmp_suspend_initialize_thread
    0x103b6aa78 <+40>: leaq   0x5c0(%r13), %r14
Target 0: (python) stopped.
(lldb) bt
* thread #5, stop reason = EXC_BAD_ACCESS (code=1, address=0x48)
  * frame #0: 0x0000000103b6aa6c libiomp5.dylib`void __kmp_suspend_64<false, true>(int, kmp_flag_64<false, true>*) + 28
    frame #1: 0x00000001276a7ef0 libomp.dylib`kmp_flag_64<false, true>::wait(kmp_info*, int, void*) + 1458
    frame #2: 0x00000001276a4e87 libomp.dylib`__kmp_hyper_barrier_release(barrier_type, kmp_info*, int, int, int, void*) + 151
    frame #3: 0x00000001276a760e libomp.dylib`__kmp_fork_barrier(int, int) + 445
    frame #4: 0x00000001276905f2 libomp.dylib`__kmp_launch_thread + 186
    frame #5: 0x00000001276ba9f4 libomp.dylib`__kmp_launch_worker(void*) + 278
    frame #6: 0x00007fff204128fc libsystem_pthread.dylib`_pthread_start + 224
    frame #7: 0x00007fff2040e443 libsystem_pthread.dylib`thread_start + 15
(lldb) image list -a 0x0000000103b6aa6c
6934E91E-BB7D-3812-A269-50DB7D644483 0x0000000103a40000 /Users/alex/.pyenv/versions/3.8.10/envs/iomp_bug/lib/python3.8/site-packages/torch/.dylibs/libiomp5.dylib
(lldb) image list -a 0x00000001276a7ef0
B25EE72F-00AB-3F23-89BD-634A0B322662 0x0000000127675000 /usr/local/opt/libomp/lib/libomp.dylib
(lldb)
```

This shows the backtrace starting at `libiomp5.dylib` within torch, which is unexpected as this code doesn't invoke torch at all other than importing it.

cc @ezyang @seemethere @malfet @walterddr @albanD