mkl compatible with pytorch

5 views (last 30 days)
Mehdi Koochak
Mehdi Koochak on 6 Jul 2022
Answered: Abhas on 28 Oct 2024
I'm trying to use build a mex function in cpp which use pytorch for doing some matrix multiplication, all part of converting an arry to torch tensor works fine. But when try to multiply two torch tensors matlab carshes and from trace I get that's because of different mkl.so that pytorch and matlab are using.
how Can I either change the matlab mkl to compatible with pytorch or vice versa.
I've no problem with building pytorch from source.
Thanks in advance
Mehdi
  2 Comments
Oliver Malki
Oliver Malki on 21 Jul 2022
Hi Mehdi,
we are somehow facing the same issues with LibTorch <=> MATLAB DLLs.
For us it works, when we start MATLAB from a command line, where we have already set the correct environment variables. Under Windows this means to set "SET PATH=%MY_WAY_TO_LIBTORCH%;%PATH%". Under Linux you can additionally set the LD_PRELOAD environment variable to enforce loading a specific DLL.
Oliver
Surya
Surya on 20 Mar 2023
Hi,
To make Matlab use a specific version of MKL, you can set the environment variable MKLROOT to the path of the desired MKL version before starting Matlab. For example, if you want Matlab to use the MKL version installed with PyTorch, you can set MKLROOT to the path of the PyTorch installation directory (e.g., /path/to/pytorch/lib).
Alternatively, you can try using the MKL-DNN library instead of MKL for PyTorch, as it is designed to be compatible with different versions of MKL used by other applications. You can build PyTorch with MKL-DNN support by specifying the USE_MKLDNN flag during the build process.

Sign in to comment.

Answers (1)

Abhas
Abhas on 28 Oct 2024
To resolve the compatibility issue between MATLAB and PyTorch due to different versions of the MKL library you can follow the below steps:
  • Set MKLROOT for MATLAB: You can try setting the "MKLROOT" environment variable to point to the MKL version that PyTorch uses. This might help MATLAB to pick up the same version. You can do this by exporting the MKLROOT path in your shell before starting MATLAB. For example:
export MKLROOT=/path/to/pytorch/lib
matlab
  • Build PyTorch with MKL-DNN: Alternatively, you can build PyTorch with MKL-DNN (now known as oneDNN), which is designed to be more flexible with different MKL versions. When building PyTorch from source, you can enable MKL-DNN by setting the "USE_MKLDNN" flag. Here’s a basic outline of how you might do this:
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
export USE_MKLDNN=1
python setup.py install
  • Ensure Consistent Environment: Make sure that any environment variables related to MKL (like "LD_LIBRARY_PATH" or "DYLD_LIBRARY_PATH" on macOS) are consistent across both MATLAB and your PyTorch environment.
You may refer to the below MathWorks documentation links that might be helpful for resolving the MKL compatibility issue:
  1. https://www.mathworks.com/help/matlab/external-language-interfaces.html
  2. https://www.mathworks.com/help/matlab/ref/setenv.html
I hope this helps!

Categories

Find more on Python with MATLAB in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!