61

I am running a python script and I get this error:

Intel MKL FATAL ERROR: Cannot load libmkl_avx2.so or libmkl_def.so.

Both files are present in the anaconda2/lib directory. How can I fix this error? Thanks.

Chris Parry
  • 2,937
  • 7
  • 30
  • 71
  • https://software.intel.com/en-us/articles/a-new-linking-model-single-dynamic-library-mkl_rt-since-intel-mkl-103 – Nehal J Wani Apr 27 '17 at 16:41
  • 1
    FYI - I also got this error, but it was happening in a conda environment when I ran any command, even ls or git commands. Somehow the dir i was in got considered deleted after a git checkout where it still existed(?), and I fixed it just by backing out of the directory and going back in. – Starman Apr 14 '20 at 21:09
  • @Starman thanks! backing out of the directory and going back solved the issue for me. how weird! – Jan Pisl Dec 29 '20 at 17:44
  • There are some answers below. I just want to add that I had the same problem, also running anaconda, but it turned out I just requested too little memory on the cluster I was running my job on. Requesting slightly more memory solved the problem. No need to change my environment. – Georg Dec 16 '21 at 09:33

14 Answers14

122

If you use conda, try with these two commands:

conda install nomkl numpy scipy scikit-learn numexpr
conda remove mkl mkl-service

It should fix your problem.

Zaikun Xu
  • 1,433
  • 1
  • 12
  • 7
34

I ran into this problem after installing anaconda3 (vesion 4.2.0). The fix for me was simple, and I was able to keep using mkl. Just update to the latest numpy version.

conda update numpy
Drew Swartz
  • 449
  • 4
  • 2
  • Worked with `anaconda2` (conda version 4.3.16) as well. – KT. Apr 24 '17 at 12:05
  • Success from anaconda2 version 4.3.14. See also here https://github.com/ContinuumIO/anaconda-issues/issues/720 for some info regarding this fix: try reinstall numpy with `conda install -f numpy` before removing mkl. – Jason Jun 07 '17 at 05:32
  • 1
    Just `conda install -f numpy` worked for me (without having to remove mkl) – Ansari Sep 09 '17 at 19:21
17

Just wanted to note that Anaconda 4.0.0, shipped with mkl enabled by default, has this issue. The problem is indeed with Anaconda, as it can be reproduced with the simple python test below.

The actual issue is that Anaconda linked with mkl, but not with libmkl_core.so, thus it has a missing symbol, and can be seen by running:

$ LD_DEBUG=symbols python -c 'import sklearn.linear_model.tests.test_randomized_l1' 2>&1 | grep -i error
      2200:     /opt/anaconda/lib/python2.7/site-packages/scipy/special/../../../../libmkl_avx.so: error: symbol lookup error: undefined symbol: mkl_dft_fft_fix_twiddle_table_32f (fatal)

I didn't want to uninstall mkl, as I'd like to have the performance boost, so I found a workaround which worked for me - preload libmkl_core.so before execution.

$ python -c 'import sklearn.linear_model.tests.test_randomized_l1'
Intel MKL FATAL ERROR: Cannot load libmkl_avx.so or libmkl_def.so.
$
$ LD_PRELOAD=/opt/anaconda/lib/libmkl_core.so python -c 'import sklearn.linear_model.tests.test_randomized_l1'
$
yanir
  • 171
  • 1
  • 3
  • The real good explanation of the cause, and the solution make sense, because MKL is Intel native compiler, and well known to have better performance than GCC. Yes, uninstalling gave me more problem. – Peter Teoh Oct 14 '17 at 07:21
  • 4
    Thanks for this--it worked for me once I added a second library: `LD_PRELOAD=$CONDA_PREFIX/lib/libmkl_core.so:$CONDA_PREFIX/lib/libmkl_sequential.so python ...`. – John Zwinck Feb 08 '18 at 06:22
  • Thank you! It worked for me. So much easier than navigating conda's package dependencies... – Masterfool Apr 24 '18 at 19:41
7

I had this same issue using scikit-learn 0.19 and numpy 1.13.3 when running MLPRegressor (and also with a package called pyearth running an algorithm called MARS). I believe the root of the problem was that our python is part of an Anaconda install, but scikit-learn and numpy were installed via pip, and their expectations for mkl must not agree.

Unfortunately my framework is managed by some dedicated company admins, not by me, so I haven't gotten my guy to try recompiling numpy yet. But I was able to find a workaround based on this thread: Adding export LD_PRELOAD=/path/to/anaconda/lib/libmkl_def.so:/path/to/anaconda/lib/libmkl_avx.so:/path/to/anaconda/lib/libmkl_core.so:/path/to/anaconda/lib/libmkl_intel_lp64.so:/path/to/anaconda/lib/libmkl_intel_thread.so:/path/to/anaconda/lib/libiomp5.so to my ~/.bashrc causes the problem to disappear. It's super hacky, and I'd be lying if I said I knew exactly what it's doing (but this is helpful), so I'm hoping a recompile of numpy is a cleaner fix. But at least it works.

Note it is better performance-wise to have the versions of these packages that use mkl. Installing the nomkl versions is a workaround but not a true solution.

Pavel Komarov
  • 1,153
  • 12
  • 26
5

Got the same problem and solved by using the command:

conda install nomkl

I got the solution from this discussion https://github.com/BVLC/caffe/issues/3884

Masud
  • 51
  • 2
  • 5
3

It is also possible that you are running Python in a folder which does not exist.

Solution is simply go to another folder.

See this question: sh: 0: getcwd() failed: No such file or directory on cited drive

null
  • 1,167
  • 1
  • 12
  • 30
2

try export LD_DEBUG=libs and execute your program again.

It will tell you which .so is causing the problem.

Then you can do something, e.g. upgrading the package.

For me, I find upgrade numexpr useful as it prints the following message just before the error:

calling init: xxxxx/python3.6/site-packages/numexpr/interpreter.cpython-36m-x86_64-linux-gnu.so
JasonWayne
  • 1,724
  • 1
  • 19
  • 16
  • this was really useful. for anyone else who's lost basic abilities from hours of conda induced madness - after you delete anaconda and vow to never use it again, remember to `export LD_DEBUG=` to undo the debug output – lys Jul 27 '21 at 08:52
1

Wanted to add on Valilutzik and Zaikun Xu's thread. I can't add comments since I don't have enough points yet.
conda install nomkl numpy scipy scikit-learn numexpr worked for me without having to remove mkl and mkl-service.

Adding an answer to Lee's question if nomkl will be slower: mkl is a Intel math kernel library and is hand-optimized for intel cpus. nomkl uses OpenBlas according to this: https://docs.continuum.io/mkl-optimizations/ It seems that mkl is quite faster on many matrix operations for intel cpus (see https://software.intel.com/en-us/articles/performance-comparison-of-openblas-and-intel-math-kernel-library-in-r)
I've seen someone saying nomkl is faster for AMD cpus (maybe because mkl doesn't work correctly in AMD?)

libphy
  • 121
  • 5
1

All the solutions provided above did not work for me, but I found a good compromise:

for someone who has the same error and want's to keep Anaconda on his computer, and steel use mkl for (numpy and scipy) good process performance, the solution I propose:

Edit your .bashrc file.

Look for something like export PATH="/home/anaconda2/bin:$PATH" in the file.

Put a # at the beginning to comment it from the script: #export PATH="/home/anaconda2/bin:$PATH"

Open a new terminal and you should be running the base python installation.

It worked for me, hope it will be helpfull.

Klipiklop
  • 31
  • 4
1

In case anyone has a similar issue and gets errors that libmkl_p4m.so or libmkl_p4.so cannot be found (this occurred for me when calling certain numpy functions), I tried reinstalling / updating different python modules and reverting / updating to various versions of Anaconda, and neither worked. However I found that completely uninstalling anaconda and re-installing it (to version 4.4.10) solved the issue.

KamKam
  • 546
  • 6
  • 14
0

The following worked for me

conda install  -f  numpy
0

None of the above answers worked for me. In my case, @Nehal J Wani's comment below the OP's question led to the solution: As stated in the linked Intel support document, a single dynamic library can be used to link to the MKL.

After editing my project's Makefile to replace the following libraries -lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core with -lmkl_rt and re-building, the error disappeared.

0

For me downgrading mkl solved it:

conda install mkl=2021.2.0

(Ubuntu 21.04, Python 3.8)

gorogm
  • 41
  • 3
0

solved by reinstallation: pip install tensorflow

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31071118) – Simas Joneliunas Feb 19 '22 at 14:50