mkl compatible with pytorch

20 次查看(过去 30 天)
Mehdi Koochak
Mehdi Koochak 2022-7-6
回答: Abhas 2024-10-28,9:34
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 个评论
Oliver Malki
Oliver Malki 2022-7-21
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 2023-3-20
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.

请先登录,再进行评论。

回答(1 个)

Abhas
Abhas 2024-10-28,9:34
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!

类别

Help CenterFile Exchange 中查找有关 Python with MATLAB 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by