MATLAB Answers

MATLAB Coder: How do I build the Intel MKL-DNN library for Deep Learning C++ code generation and deployment?

241 views (last 30 days)
Bill Chou
Bill Chou on 27 Feb 2019
Commented: JJ L on 13 Oct 2020 at 2:29
I see a few deep learning networks supported for code generation using MATLAB Coder:
I'm looking to generate code from my deep learning network (like AlexNet, GoogLeNet, ResNet, SqueezeNet, VGG-16/19, etc) to run on Intel CPUs using MATLAB Coder and the Intel MKL-DNN library. What are the steps to do this?

  0 Comments

Sign in to comment.

Accepted Answer

Bill Chou
Bill Chou on 27 Feb 2019
Edited: Bill Chou on 11 Jun 2020
Note: The following applies to R2018b, R2019a, and R2019b releases of MATLAB Coder and MKL-DNN v0.14 as described here. For newer releases, see the next answer below.
Background
To generate and run C++ code for Deep Learning, you must have the Intel Math Kernel Library for Deep Neural Networks (Intel MKL-DNN). Do not use a prebuilt library because some required files are missing. Instead, build the library from source code.
The following describes build instructions for MKL-DNN on Windows and Linux platforms.
Windows MKL-DNN build instructions
To follow these instructions, obtain a recommended compiler and IDE:
  • Microsoft Visual Studio 2017
  • Intel C++ Compiler 18.0 from Intel Parallel Studio
These instructions assume the use of the Microsoft Visual Studio IDE and compiler. cmake is also required.
1. Download the latest Intel MKL-DNN source code (https://github.com/intel/mkl-dnn/archive/master.zip) or copy source code for a required version for your MATLAB Coder release from https://github.com/intel/mkl-dnn/releases. The version required by the latest MATLAB Coder release is shown in the documentation: https://www.mathworks.com/help/coder/ug/prerequisites-for-deep-learning-with-matlab-coder.html
2. Unzip the source code to a folder, MKLDNN. From the MKLDNN folder, from the Command Prompt, enter:
cd scripts
.\prepare_mkl.bat
cd ..
This creates these libraries at MKLDNN\external\mklml_win_*\lib:
  • libiomp5md.dll
  • libiomp5md.lib
  • mklml.dll
  • mklml.lib
3. Run these commands from the MKLDNN folder:
mkdir -p build
cd build
cmake -G “Visual Studio 15 2017 Win64” ..
This creates this Visual Studio solution file in /build:
  • Intel(R) MKL-DNN.sln
4. Open the solution file in Visual Studio. Set the Solution Configurations dropdown to Release. Click on Build > Build Solution. This creates these libraries in MKLDNN\build\src\Release:
  • mkldnn.dll
  • mkldnn.lib
Copy these libraries and the libraries generated in step 2 to C:\Program Files\mkl-dnn\lib.
Note that on Windows® operating systems, special characters and spaces are allowed in the path only if 8.3 file names are enabled. If they are not enabled, replace 'C:\Program Files\mkl-dnn\lib’ with a path that does not include a space. For more information on 8.3 file names, refer to the Windows documentation.
Copy the following files from MKLDNN\include to C:\Program Files\mkl-dnn\include:
  • mkldnn.h
  • mkldnn.hpp
  • mkldnn_debug.h
  • mkldnn_types.h
5. Set the MATLAB environmental variable INTEL_MKLDNN to C:\Program Files\mkl-dnn. From the MATLAB Command Window, enter:
setenv('INTEL_MKLDNN', 'C:\Program Files\mkl-dnn\')
Add C:\Program Files\mkl-dnn\lib to the PATH variable.
setenv('PATH', [getenv('INTEL_MKLDNN') filesep 'lib' pathsep getenv('PATH')])
6. If you have not done so already, you should also set the environment variable for Windows. From the Windows Command Prompt, enter:
set PATH=%PATH%; C:\Program Files\mkl-dnn\
Linux MKL-DNN build instructions
To follow these instructions, obtain a recommended compiler:
  • GNU g++ with C++11 support
cmake is also required.
1. Download the latest Intel MKL-DNN source code (https://github.com/intel/mkl-dnn/archive/master.zip) or copy source code for a required version of your MATLAB Coder release from https://github.com/intel/mkl-dnn/releases. The version required by the latest MATLAB Coder release is shown in the documentation: https://www.mathworks.com/help/coder/ug/prerequisites-for-deep-learning-with-matlab-coder.html
2. Unzip the source code to a folder, MKLDNN. From the MKLDNN folder, from the Command Prompt, enter:
cd scripts
chmod +x prepare_mkl.sh
./prepare_mkl.sh
This creates these libraries at MKLDNN/external/mklml_lnx_*/lib:
libiomp5.so
libmklml_intel.so.
3. Run these commands from the MKLDNN folder:
mkdir -p build
cd build
cmake ..
make
4. This creates these libraries under MKLDNN/build/src:
  1. libmkldnn.so
  2. libmkldnn.so.0
  3. libmkldnn.so.0.14.0.
Copy these libraries and thelibraries generated in step 2 to /usr/local/mkl-dnn/lib.
Copy the following files from MKLDNN/include to /usr/local/mkl-dnn/include:
  • mkldnn.h
  • mkldnn.hpp
  • mkldnn_debug.h
  • mkldnn_types.h
5. Set the MATLAB environmental variable INTEL_MKLDNN to /usr/local/mkl-dnn. From the MATLAB Command Window, enter:
setenv('INTEL_MKLDNN', /usr/local/mkl-dnn')
Add /usr/local/mkl-dnn/lib to the PATH variable:
setenv('LD_LIBRARY_PATH', [getenv('INTEL_MKLDNN') filesep 'lib' pathsep getenv('LD_LIBRARY_PATH')]);
If you have not done so already, you should also set the environment variables for Linux. Use Linux syntax in the Linux terminal to set the variable LD_LIBRARY_PATH to /usr/local/mkl-dnn/lib, and the environment variable INTEL_MKLDNN to /usr/local/mkl-dnn.

More Answers (8)

Xiaoqi
Xiaoqi on 9 Jan 2020
Hi, Bill
I believe your steps need to be updated, because the mkl-dnn library in the github is changed. I can not make it work by following your steps.
Thanks
Charlie

  2 Comments

Praveen Kumar Gajula
Praveen Kumar Gajula on 24 Jan 2020
Hi Xiaoqi,
As per the R2019b pre-requisites documentation we use the mkldnn v0.14 . The above build steps will suffice for the mkldnn v0.14.
We will update the steps as you mentioned once we support latest mkldnn version.
Thank you,
Praveen.

Sign in to comment.


Sviatoslav Klos
Sviatoslav Klos on 4 Feb 2020
I have such an error: Undefined function or variable 'IsAvx512Proc'.
I thought that if my Intel proc does not support Avx512 then it would just use Avx2. Does it make sense? Had someone try MKL-DNN with Intel i5-8265u?

  0 Comments

Sign in to comment.


Praveen Kumar Gajula
Praveen Kumar Gajula on 7 Feb 2020
Hi Sviatoslav,
We are sorry to hear that you are facing issues.
You are right that if Intel proc doesnot support Avx512 mkldnn should use Avx2.
And processor i5-8265u should be able to use mkldnn with out any issues.
There is a package installer issue from our end , where it didnot ship the 'IsAvx512Proc' method. Hence you are seeing this error . Could you let me know what is the MATLAB version you are using and did you do any recent upgrade to it.
Thank you,
Praveen.

  4 Comments

Show 1 older comment
Jaya Shankar
Jaya Shankar on 11 Feb 2020
Hi Sviatoslav,
Deep learning toolbox has a few scaling layer implementations. For instance , there is one implemented for inceptionV3 networks and there is a scaling layer for reinforcement learning toolbox. Can you clarify which one you are using ?
Note that codegen is supported for the scaling layer in inceptionV3 networks since 19a.
Thanks
Jaya

Sign in to comment.


Praveen Kumar Gajula
Praveen Kumar Gajula on 14 Mar 2020
Hi ,
Error is not clear from this image. Could you share us Error report.
Could you also share the MATLAB release details and example link that you are trying out.
Thank you,
Praveen.

  1 Comment

sara alamri
sara alamri on 15 Mar 2020
here is the report of the error in the attachment
i use Matlab R2019b
i also tried to this instructions to install mkl dnn
and your instruction but didn't work
i also tried all example in matlab website that related to YOLO code genrator.. all same error
all i want from this is to convert my code"which contain a yolo detector" to C so i can combine it with an android app.

Sign in to comment.


Praveen Kumar Gajula
Praveen Kumar Gajula on 16 Mar 2020
The compilation errors are becuase of mkldnn.hpp file not found.
" c:\users\witch\documents\matlab\examples\r2019b\deeplearning_shared\yolov2objectdetectionusingintelmkldnnexample\codegen\mex\yolov2_detection\MWCNNLayerImpl.hpp(10): fatal error C1083: Cannot open include file: 'mkldnn.hpp': No such file or directory
"
Could you check if you setup the INTEL_MKLDNN path correctly as mentioned in step -
setenv('INTEL_MKLDNN', 'C:\Program Files\mkl-dnn\')
Also could you make sure that there are no spaces in the folders
  1. Where you are trying codegen.
  2. Where MKLDNN libraries reside.
Thank you,
Praveen.

  3 Comments

sara alamri
sara alamri on 16 Mar 2020
the file is already exist
and there is no space in both paths
I FIX this bug by move this file into project folder
but another error happend : fatal error LNK1181: cannot open input file 'lib\mkldnn.lib'
ninja: build stopped: subcommand failed.
my CPU is intel core i7 7th Gen 2.7 GHz
sara alamri
sara alamri on 16 Mar 2020
now this error
compilation aborted for MWElementwiseAffineLayer.cpp (code 4)
.....
catastrophic error: cannot open source file "mkldnn.hpp"
...
compilation aborted for MWExponentialLayer.cpp (code 4)
ninja: build stopped: subcommand failed.
Praveen Kumar Gajula
Praveen Kumar Gajula on 16 Mar 2020
We tried the same example and found the same log as yours if we donot set the INTEL_MKLDNN path.
Currently you are seeing the log similar to log present in withOutMKLDNNPath.txt where the INTEL_MKLDNN is not set in the MATLAB. If you set it appropriately you should see log similar to withMKLDNNPATH
Thank you,
Praveen.

Sign in to comment.


sara alamri
sara alamri on 17 Mar 2020
Edited: sara alamri on 17 Mar 2020
now i had this error in the attachment using matlab R2019b
and this error using matlab R2019a
??? Error generating code for network yoloV2CatDetector_0. Code generation for
YOLOv2TransformLayer yolov2Transform is not supported for mkldnn target. See documentation
for a list of supported layers with each target.
i use mkldnn V0.14
i also installed minGW v17 which i belive it supports openMP

  1 Comment

Praveen Kumar Gajula
Praveen Kumar Gajula on 18 Mar 2020
We donot support the yolov2 for R2019a release.
Regarding the errors in R2019b ,we are unable to reproduce .
It would be great if you could forward the steps that you have used for codegen. This will help us to corner out if there are any steps that are missed out.
Than kyou,
Praveen.

Sign in to comment.


Mahmoud Abdulsalam
Mahmoud Abdulsalam on 9 Jun 2020
I have generated a source code for deep learning and found a lot of files in codegen folder. How do I go about integrating it and compile it in Linux.Do I need to make changes to the main.cpp? Because I saw some instructions within the main file asking me to change variable size function argument.

Bill Chou
Bill Chou on 11 Jun 2020
Edited: Bill Chou on 1 Jul 2020
Note: The following applies to R2020a release of MATLAB Coder and MKL-DNN v1.0 as described here.
Note: Intel recently renamed the library from MKL-DNN to oneDNN, so we use MKL-DNN and oneDNN interchangeably.
Background
To generate and run C++ code for Deep Learning, you must have the Intel Math Kernel Library for Deep Neural Networks (Intel MKL-DNN). Do not use a prebuilt library because some required files are missing. Instead, build the library from source code.
The following describes build instructions for MKL-DNN on Windows and Linux platforms.
To build the Intel MKL-DNN library from source code, you must have:
  • Operating system with Intel 64 architecture support
  • C++ compiler with C++11 standard support
  • CMake 2.8.11 or later version
Windows MKL-DNN build instructions
C++ Compiler requirements :
  • Microsoft Visual C++ 14.0 (Visual Studio 2015 Update 3)
  • If you face compilation errors while building MKL-DNN, please use MSVC Compiler version 19.16.xx or newer
1. Download the mkldnn v1.0 source code from the link (https://github.com/oneapi-src/oneDNN/archive/v1.0.zip) and extract the source code. This action creates the folder oneDNN-1.0.
2. Generate a Microsoft Visual Studio solution “Intel(R) MKL-DNN.sln” by running these commands in command line at oneDNN-1.0 folder:
mkdir build
cd build
cmake -G "Visual Studio 15 2017 Win64" ..
3. Run below command in command line at oneDNN-1.0\build folder:
cmake --build . --config Release
This action creates these libraries in the folder oneDNN-1.0\build\src\Release:
  • mkldnn.lib
  • mkldnn.dll
Copy these generated libraries to C:\Program Files\mkl-dnn\lib.
Note that on Windows operating systems, special characters and spaces are allowed in the path only if 8.3 file names are enabled. If they are not enabled, replace C:\Program Files\mkl-dnn\lib with a path that does not include a space. For more information on 8.3 file names, refer to the Windows documentation.
Copy the include files from oneDNN-1.0\include and oneDNN-1.0\build\include to C:\Program Files\mkl-dnn\include.
4. Set the MATLAB environment variable INTEL_MKLDNN to C:\Program Files\mkl-dnn. At the MATLAB command prompt, enter:
setenv('INTEL_MKLDNN', 'C:\Program Files\mkl-dnn')
Add C:\Program Files\mkl-dnn\lib to the PATH variable.
setenv('PATH', [getenv('INTEL_MKLDNN') filesep 'lib' pathsep getenv('PATH')])
5. If you have not done so already, you must set the environment variable for Windows. At the Windows Command Prompt, enter:
set PATH=%PATH%; C:\Program Files\mkl-dnn\lib
Also create and set the INTEL_MKLDNN environment variable in windows that point to C:\Program Files\mkl-dnn
Linux MKL-DNN build instructions
C++ Compiler requirements :
  • Install any one of the GNU compiler versions from GNU Compiler Collection 4.8, 5.4, 6.1, 7.2, and 8.1
1. Download the mkldnn v1.0 source code from the link (https://github.com/oneapi-src/oneDNN/archive/v1.0.tar.gz) and extract source code by running this command at the terminal:
tar -xvzf oneDNN-1.0.tar.gz
This action creates the folder oneDNN-1.0.
2. To generate the makefile for compilation, run these commands at the terminal from the oneDNN-1.0 folder:
mkdir -p build
cd build
cmake -DCMAKE_BUILD_TYPE=Release ..
3. To build the library, run this command at the terminal from the oneDNN-1.0/build folder:
make -j
These commands creates these libraries in the folder oneDNN-1.0/build/src:
  • libmkldnn.so
  • libmkldnn.so.0
  • libmkldnn.so.1.0.0.0
Copy these libraries to /usr/local/mkl-dnn/lib.
Copy the include files from oneDNN-1.0/include and oneDNN-1.0/build/include to /usr/local/mkl-dnn/include.
4. Set the MATLAB environment variable INTEL_MKLDNN to /usr/local/mkl-dnn. At the MATLAB command prompt, enter:
setenv('INTEL_MKLDNN', /usr/local/mkl-dnn')
5. Add /usr/local/mkl-dnn/lib to the PATH variable:
setenv('LD_LIBRARY_PATH', [getenv('INTEL_MKLDNN') filesep 'lib' pathsep getenv('LD_LIBRARY_PATH')]);
If you have not done so already, you must set the environment variables for Linux. Use Linux syntax in the Linux terminal to set the variable LD_LIBRARY_PATH to /usr/local/mkl-dnn/lib, and the environment variable INTEL_MKLDNN to /usr/local/mkl-dnn.

  3 Comments

JJ L
JJ L on 11 Oct 2020 at 12:52
Hello, Bill Chou,
I am using the matlab 2020b, and I just installed the Visual Studio 2019 and Intel Parallel Studio XE.
With in the folder of oneDNN-1.0, when I run the commands, the "cmake" cannot be found, so I cannot start your process at all. What should I do?
mkdir build
cd build
cmake -G "Visual Studio 15 2017 Win64" ..
Thank you very much!
Jaya Shankar
Jaya Shankar on 12 Oct 2020 at 16:33
Hi JJ,
cmake needs to be run as a system command. Please run the cmake commands from your Windows terminal instead of running them within MATLAB.
Jaya

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!