Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't import PyTorch, libomp.dylib can't be loaded. #20030

Closed
kaszperro opened this issue May 1, 2019 · 30 comments
Closed

I can't import PyTorch, libomp.dylib can't be loaded. #20030

kaszperro opened this issue May 1, 2019 · 30 comments
Labels
high priority module: binaries Anything related to official binaries that we release to users module: build Build system issues triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@kaszperro
Copy link

kaszperro commented May 1, 2019

🐛 Bug

I tried to install PyTorch, but I can't use it. It can't link dynamic library libomp.dylib

To Reproduce

Steps to reproduce the behavior:

  1. pipenv install torch torchvision
  2. from torch.utils.data import Dataset
Traceback (most recent call last):
  File "/Users/kaspersapala/Documents/Airly/new-pollution-forecast/Datasets/__init__.py", line 3, in <module>
    from torch.utils.data import Dataset
  File "/Users/kaspersapala/.local/share/virtualenvs/new-pollution-forecast-BSetKF9E/lib/python3.7/site-packages/torch/__init__.py", line 79, in <module>
    from torch._C import *
ImportError: dlopen(/Users/kaspersapala/.local/share/virtualenvs/new-pollution-forecast-BSetKF9E/lib/python3.7/site-packages/torch/_C.cpython-37m-darwin.so, 9): Library not loaded: /usr/local/opt/libomp/lib/libomp.dylib
  Referenced from: /Users/kaspersapala/.local/share/virtualenvs/new-pollution-forecast-BSetKF9E/lib/python3.7/site-packages/torch/lib/libshm.dylib
  Reason: image not found

Expected behavior

Being able to use and import PyTorch

Environment

  • PyTorch Version (e.g., 1.0): 1.1.0
  • OS (e.g., Linux): Mac OS Mojave
  • How you installed PyTorch (conda, pip, source): pipenv
  • Python version: 3.7.1
@kaszperro
Copy link
Author

brew install libomp solves the problem.

HiromuHota pushed a commit to HiromuHota/fonduer that referenced this issue May 2, 2019
senwu pushed a commit to HazyResearch/fonduer that referenced this issue May 2, 2019
* Fix the error when importing torch (1.1.0) on osx

pytorch/pytorch#20030

* Update torch version to 1.1.0 for linux

* Update the doc for OS X about an external dependency on libomp
@soumith soumith reopened this May 3, 2019
@ezyang
Copy link
Contributor

ezyang commented May 6, 2019

cc @JerryShih

@ezyang ezyang added module: binaries Anything related to official binaries that we release to users high priority triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module and removed high priority labels May 6, 2019
@ezyang
Copy link
Contributor

ezyang commented May 6, 2019

cc @pjh5 @kostmo

@JerryShih
Copy link
Contributor

brew install libomp solves the problem.

@ezyang
How to add the libomp dependency in pytorch pip installation?

Will all pytorch dependent libraries be backed into wheel package?
If yes, I will add the libomp files like these code:

pytorch/setup.py

Lines 738 to 825 in 2356fac

package_data={
'torch': [
'py.typed',
'bin/*',
'test/*',
'__init__.pyi',
'cuda/*.pyi',
'optim/*.pyi',
'autograd/*.pyi',
'utils/data/*.pyi',
'lib/*.so*',
'lib/*.dylib*',
'lib/*.dll',
'lib/*.lib',
'lib/*.pdb',
'lib/torch_shm_manager',
'lib/*.h',
'include/ATen/*.h',
'include/ATen/cpu/*.h',
'include/ATen/cpu/vec256/*.h',
'include/ATen/core/*.h',
'include/ATen/cuda/*.cuh',
'include/ATen/cuda/*.h',
'include/ATen/cuda/detail/*.cuh',
'include/ATen/cuda/detail/*.h',
'include/ATen/cudnn/*.h',
'include/ATen/detail/*.h',
'include/caffe2/utils/*.h',
'include/c10/*.h',
'include/c10/macros/*.h',
'include/c10/core/*.h',
'include/ATen/core/dispatch/*.h',
'include/ATen/core/op_registration/*.h',
'include/c10/core/impl/*.h',
'include/c10/util/*.h',
'include/c10/cuda/*.h',
'include/c10/cuda/impl/*.h',
'include/c10/hip/*.h',
'include/c10/hip/impl/*.h',
'include/caffe2/**/*.h',
'include/torch/*.h',
'include/torch/csrc/*.h',
'include/torch/csrc/api/include/torch/*.h',
'include/torch/csrc/api/include/torch/data/*.h',
'include/torch/csrc/api/include/torch/data/dataloader/*.h',
'include/torch/csrc/api/include/torch/data/datasets/*.h',
'include/torch/csrc/api/include/torch/data/detail/*.h',
'include/torch/csrc/api/include/torch/data/samplers/*.h',
'include/torch/csrc/api/include/torch/data/transforms/*.h',
'include/torch/csrc/api/include/torch/detail/*.h',
'include/torch/csrc/api/include/torch/detail/ordered_dict.h',
'include/torch/csrc/api/include/torch/nn/*.h',
'include/torch/csrc/api/include/torch/nn/modules/*.h',
'include/torch/csrc/api/include/torch/nn/parallel/*.h',
'include/torch/csrc/api/include/torch/optim/*.h',
'include/torch/csrc/api/include/torch/serialize/*.h',
'include/torch/csrc/autograd/*.h',
'include/torch/csrc/autograd/functions/*.h',
'include/torch/csrc/autograd/generated/*.h',
'include/torch/csrc/autograd/utils/*.h',
'include/torch/csrc/cuda/*.h',
'include/torch/csrc/jit/*.h',
'include/torch/csrc/jit/generated/*.h',
'include/torch/csrc/jit/passes/*.h',
'include/torch/csrc/jit/passes/utils/*.h',
'include/torch/csrc/jit/script/*.h',
'include/torch/csrc/jit/testing/*.h',
'include/torch/csrc/onnx/*.h',
'include/torch/csrc/utils/*.h',
'include/pybind11/*.h',
'include/pybind11/detail/*.h',
'include/TH/*.h*',
'include/TH/generic/*.h*',
'include/THC/*.cuh',
'include/THC/*.h*',
'include/THC/generic/*.h',
'include/THCUNN/*.cuh',
'include/THCUNN/generic/*.h',
'include/THNN/*.h',
'include/THNN/generic/*.h',
'share/cmake/ATen/*.cmake',
'share/cmake/Caffe2/*.cmake',
'share/cmake/Caffe2/public/*.cmake',
'share/cmake/Caffe2/Modules_CUDA_fix/*.cmake',
'share/cmake/Caffe2/Modules_CUDA_fix/upstream/*.cmake',
'share/cmake/Caffe2/Modules_CUDA_fix/upstream/FindCUDA/*.cmake',
'share/cmake/Gloo/*.cmake',
'share/cmake/Torch/*.cmake',

How about other dependent libraries? Are they all in third_party folder and built from source?

@ezyang
Copy link
Contributor

ezyang commented May 7, 2019

pip doesn't know anything about brew, you can't ask it to brew install a package. Also, while this resolution "solves" the issue, I would love to better understand how this dependency came to be (in particular, why are we building binaries with a dependency on brew libraries? That sounds bad.)

In some cases where we can't assume end users have a library installed, we distribute the library with our binaries. However, I'm not sure that we should do this for libomp.dylib. cc @soumith

@soumith
Copy link
Member

soumith commented May 7, 2019

this is something that needs to be fixed. I can take a look at fixing binaries and re-uploading.
The problem is that our macos CI installs libomp over here:

, I think for CI / testing. But binary builds also pick it up and use it.

@pjh5
Copy link
Contributor

pjh5 commented May 7, 2019

I can remove this dependency for the binaries. I'll put out a PR soon

@dudongge
Copy link

brew install libomp solves the proble
this solve my problem

@gongenbo
Copy link

brew install libomp solves the problem.

It work!

@adrienchaton
Copy link

when trying to upgrade pytorch from 1.0.1 to 1.1, I ran into the same issue and found this thread

the update on linux server worked fine but not on my macbook (OSX)

I tried brew install libomp but the brew formula could not be found, hence this did not fix the issue and I had to downgrade back to 1.0.1 on OSX to be able to import torch again

did anyone have the same issue regarding both the pip install of pytorch and the brew install of libomp ?

@akirchhoff-modular
Copy link

@soumith

I can take a look at fixing binaries and re-uploading.

Is there still any plan to do this, or are new binary releases going to wait for the next PyTorch release?

@kaszperro
Copy link
Author

when trying to upgrade pytorch from 1.0.1 to 1.1, I ran into the same issue and found this thread

the update on linux server worked fine but not on my macbook (OSX)

I tried brew install libomp but the brew formula could not be found, hence this did not fix the issue and I had to downgrade back to 1.0.1 on OSX to be able to import torch again

did anyone have the same issue regarding both the pip install of pytorch and the brew install of libomp ?

You need to install brew first, go to brew site and follow install instructions.

@adrienchaton
Copy link

thanks for your reply, I had brew installed but it was not enough up to date
so I updated everything and now it works

note for other users, when I did brew install libomp, it went through but other applications did not work anymore (Spyder IDE) ; at this moment my OS was still 10.11 (Capitan) and I had to upgrade to 10.12 (Sierra) to be able to solve the problem

so from my experience, I would recommend upgrading to Sierra at least before installing pytorch 1.1 and libomp (I use pip and brew for all install)

@soumith
Copy link
Member

soumith commented Jun 21, 2019

uploaded new binaries that remove dependency on libomp.dylib

@sukuya
Copy link

sukuya commented Jul 11, 2019

Now I get clang: error: unsupported option '-fopenmp' error.

@akirchhoff-modular
Copy link

@sukuya That sounds like a different problem, since it sounds like you're trying to compile PyTorch yourself, but this issue concerned a problem with the pre-compiled binaries.

@torrmal
Copy link

torrmal commented Jul 13, 2019

this issue still present in torch 1.1.0.post2, do you know when the fix will be available on stable release?

@akirchhoff-modular
Copy link

@torrmal How are you making sure that you are using 1.1.0.post2? In my testing the changes introduced by 1.1.0.post2 fixed this problem completely, so I wonder whether you have a 1.1.0 non-post2 lying around somewhere that's getting picked up inadvertently.

@sukuya
Copy link

sukuya commented Jul 13, 2019

@sukuya That sounds like a different problem, since it sounds like you're trying to compile PyTorch yourself, but this issue concerned a problem with the pre-compiled binaries.

@alex-xnor I was compiling a c++ program which used prebuilt libtorch and I get those errors.

@pjh5
Copy link
Contributor

pjh5 commented Jul 15, 2019

@sukuya can you make a new issue with more details on your problem? In particular, can you include the cmake/makefile that you are running and the compiler command that you are using?

@GPPyeye
Copy link

GPPyeye commented May 12, 2020

brew install libomp solves the problem.

Thx bro I've solved my problem

@leedrake5
Copy link

Does anyone have a suggestion for when brew install libomp doesn't work? I've reinstalled, relinked, but to no avail.

@MattioCh
Copy link

MattioCh commented Jul 8, 2020

put this in terminal,hope it works. It took me 2hrs to fix this and I hope you can fix it faster, jeeez...

install_name_tool -change @rpath/libc++.1.dylib /usr/lib/libc++.1.dylib /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_cvxcore.cpython-37m-darwin.so

@PearlW1
Copy link

PearlW1 commented Dec 22, 2020

brew install libomp solves the problem.

Don't run brew install libomp, it destroyed my environment, then i create a new env, install other configuration, so sad.

@callmejliu
Copy link

I also have the same issue when Launching Web UI with arguments:... Anyone have the same issue being solved?
I tried v1.5, 2.0 and 2.1 also have the .yaml file installed in the same folder.

Installing requirements for Web UI
Launching Web UI with arguments: --opt-sub-quad-attention --skip-torch-cuda-test --no-half-vae --use-cpu interrogate
Traceback (most recent call last):
File "/Users/jeffreyhomeimac/stable-diffusion-webui/launch.py", line 356, in
start()
File "/Users/jeffreyhomeimac/stable-diffusion-webui/launch.py", line 347, in start
import webui
File "/Users/jeffreyhomeimac/stable-diffusion-webui/webui.py", line 16, in
from modules import paths, timer, import_hook, errors
File "/Users/jeffreyhomeimac/stable-diffusion-webui/modules/paths.py", line 5, in
import modules.safe
File "/Users/jeffreyhomeimac/stable-diffusion-webui/modules/safe.py", line 9, in
import torch
File "/Users/jeffreyhomeimac/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/init.py", line 202, in
from torch._C import * # noqa: F403
ImportError: dlopen(/Users/jeffreyhomeimac/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/_C.cpython-310-darwin.so, 2): Library not loaded: @loader_path/../.dylibs/libomp.dylib
Referenced from: /Users/jeffreyhomeimac/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/lib/libtorch_cpu.dylib
Reason: no suitable image found. Did find:
/Users/jeffreyhomeimac/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/lib/../.dylibs/libomp.dylib: cannot load 'libomp.dylib' (load command 0x80000034 is unknown)
/Users/jeffreyhomeimac/stable-diffusion-webui/venv/lib/python3.10/site-packages/torch/lib/../.dylibs/libomp.dylib: cannot load 'libomp.dylib' (load command 0x80000034 is unknown)
JEFFREYs-MBP-2:stable-diffusion-webui jeffreyhomeimac$

@wbf22
Copy link

wbf22 commented May 6, 2024

I did the brew install libomp but didn't work for me. I'm working in c++ with libtorch though. But copying the installed limbomp files to the directory where my executable was solved the problem. I found them with 'brew info libomp'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
high priority module: binaries Anything related to official binaries that we release to users module: build Build system issues triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging a pull request may close this issue.