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

168 views (last 30 days)
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?

Accepted Answer

Bill Chou
Bill Chou on 13 May 2021
Edited: Bill Chou on 29 Jul 2022
Note: The following applies to R2021a and newer releases of MATLAB Coder and MKL-DNN v1.4 as described here. For newer releases, see the next answer below.
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
These MKL-DNN build steps have been validated by using Visual Studio 2017 version 15.0 on 64-bit Windows platform. If you want to use a newer version of Visual Studio to build a working MKL-DNN library, use it at your own discretion.
1. Download the MKL-DNN v1.4 source code from this link (https://github.com/oneapi-src/oneDNN/archive/v1.4.zip) and extract the source code. This action creates the folder oneDNN-1.4.
2. At the Windows command line, change the current directory to oneDNN-1.4. Generate a Microsoft Visual Studio solution “Intel(R) MKL-DNN.sln” by running these commands:
mkdir build
cd build
cmake -G "Visual Studio 15 2017 Win64" ..
3. Change the current directory to oneDNN-1.4\build. Run this command:
cmake --build . --config Release
This action creates these libraries in the folder oneDNN-1.4\build\src\Release:
  • dnnl.lib
  • dnnl.dll
Inside the folder C:\Program Files, create a folder named mkl-dnn. Then, inside the folder C:\Program Files\mkl-dnn, create a folder named lib. Copy the generated libraries to the folder C:\Program Files\mkl-dnn\lib.
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 by a path that does not include spaces. For more information on 8.3 file names, refer to the Windows documentation.
Copy the include files from oneDNN-1.4\include and oneDNN-1.4\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 by running:
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 line, enter:
set PATH=%PATH%; C:\Program Files\mkl-dnn\lib
Also, create and set the INTEL_MKLDNN environment variable in Windows that points 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. The versions include 4.8, 5.4, 6.1, 7.2, and 8.1.
1. Download the MKL-DNN v1.4 source code from this link (https://github.com/oneapi-src/oneDNN/archive/v1.4.tar.gz) and extract the source code by running this command at the Linux terminal:
tar -xvzf oneDNN-1.4.tar.gz
This action creates the folder oneDNN-1.4.
2. To generate the makefile for compilation, run these commands at the terminal from the oneDNN-1.4 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.4/build folder:
make -j
These commands creates these libraries in the folder oneDNN-1.4/build/src:
  • libdnnl.so
  • libdnnl.so.1
  • libdnnl.so.1.4
Copy these libraries to /usr/local/mkl-dnn/lib.
Copy the include files from oneDNN-1.4/include and oneDNN-1.4/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 line, 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 at 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.
macOS MKL-DNN build instructions
C++ Compiler requirements:
1. Download the MKL-DNN v1.4 source code from this link (https://github.com/oneapi-src/oneDNN/archive/v1.4.tar.gz) and extract this source code by running this command at the terminal:
tar -xvzf oneDNN-1.4.tar
This action creates the folder oneDNN-1.4.
2. The macOS clang compiler does not ship with OpenMP. To get an OpenMP enabled mkldnn library, you must install brew and libomp:
a. Install home brew on host mac PC by running this command at the bash terminal
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
b. Install OpenMP by using brew with command:
$(BREW_INSTALL_PATH)/brew install libomp
3. To generate the makefile for compilation, run these commands at the terminal from the oneDNN-1.4 folder:
mkdir -p build
cd build
$(CMAKE_INSTALL_PATH)cmake .. -DOpenMP_CXX_FLAGS="-Xclang -fopenmp -I$(OMP_INSTALL_PATH)/include" -DOpenMP_C_FLAGS="-Xclang -fopenmp -I$(OMP_INSTALL_PATH)/include" -DOpenMP_CXX_LIB_NAMES=libomp -DOpenMP_C_LIB_NAMES=libomp -DOpenMP_libomp_LIBRARY=$(OMP_INSTALL_PATH)/lib/libomp.dylib -DCMAKE_SHARED_LINKER_FLAGS="-L$(OMP_INSTALL_PATH)/lib -lomp"
4. To build the library, run this command at the terminal from the oneDNN-1.4/build folder:
make -j
These commands create these libraries in the folder oneDNN-1.4/build/src:
  • libdnnl.dylib
  • libdnnl.1.dylib
  • libdnnl.1.4.dylib
Copy these libraries to /usr/local/mkl-dnn/lib.
Copy the include files from oneDNN-1.4/include to /usr/local/mkl-dnn/include.
5. Copy the OpenMP dependencies required by MKL-DNN:
  • OpenMP libraries from $(OMP_INSTALL_PATH)/lib to /usr/local/mkl-dnn/lib
  • OpenMP include files from $(OMP_INSTALL_PATH)/include to /usr/local/mkl-dnn/include
6. The OpenMP library path is hard bound to mkldnn library. To list this hard binding, run the below command:
otool -l libdnnl.dylib
7. This hard binding of the paths must be changed for portability to other mac machines. To do this, run:
install_name_tool -change /usr/local/opt/libomp/lib/libomp.dylib @rpath/libomp.dylib libdnnl.1.dylib
install_name_tool -id "@rpath/libomp.dylib" libomp.dylib
Changing to rpath enables portability of the mkldnn/OpenMP libraries to other mac machines. This avoids any runtime linking issues.
8. Set the MATLAB environment variable INTEL_MKLDNN to /usr/local/mkl-dnn. At the MATLAB command line, enter:
setenv('INTEL_MKLDNN', /usr/local/mkl-dnn')
  11 Comments
Praveen Kumar Gajula
Praveen Kumar Gajula on 24 Sep 2021
Moved: Bill Chou on 14 Feb 2023
Hi,
Could you elaborate the issues that you are facing. Is this related to mkldnn mex codegen?
We simplified the workflow for mex codegen and you no longer have to download and build mkldnn for mex. We ship them along with MATLAB from R2020a .Codegen for mex should work out of the box
Thanks,
Praveen
Naga Sai Pavan Swaroop Ainapurapu
Moved: Bill Chou on 14 Feb 2023
I have an error
Code generation for FeatureInputLayer input is not supported for mkldnn target. See
documentation for a list of supported layers with each target.

Sign in to comment.

More Answers (2)

Bill Chou
Bill Chou on 27 Feb 2019
Edited: Bill Chou on 14 Feb 2023
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 answer above and 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 MKL-DNN source code for version 0.14 (available at https://github.com/oneapi-src/oneDNN/archive/refs/tags/v0.14.zip) that is required by the MATLAB Coder release shown in the documentation (https://www.mathworks.com/help/releases/R2019b/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 MKL-DNN source code for version 0.14 (available at https://github.com/oneapi-src/oneDNN/archive/refs/tags/v0.14.tar.gz) that is required by the MATLAB Coder release shown in the documentation (https://www.mathworks.com/help/releases/R2019b/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.
  21 Comments

Sign in to comment.


Bill Chou
Bill Chou on 11 Jun 2020
Edited: Bill Chou on 14 Feb 2023
Note: The following applies to R2020a and R2020b releases of MATLAB Coder and MKL-DNN v1.0 as described here. For newer releases, see the other answer above.
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.
macOS MKL-DNN build instructions (for R2020b and newer)
C++ Compiler requirements:
  • Install supported Xcode versions mentioned in https://www.mathworks.com/support/requirements/supported-compilers.html
1. Download the mkldnn v1.0 source code from the link and extract source code by running this command at the terminal:
tar -xvzf mkl-dnn-1.0.tar.gz
This action creates the folder mkl-dnn-1.0.
2. macOS clang compiler does not ship with OpenMp. To have an OpenMp enabled mkldnn library, we need to install brew,libomp
a. Install home brew in host mac PC with below command using bash terminal
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
b. Install OpenMp using brew with command
$(BREW_INSTALL_PATH)/brew install libomp
3. To generate the makefile for compilation, run these commands in the terminal from the mkl-dnn-1.0 folder:
mkdir -p build
cd build
$(CMAKE_INSTALL_PATH)cmake .. -DOpenMP_CXX_FLAGS="-Xclang -fopenmp -I$(OMP_INSTALL_PATH)/include" -DOpenMP_C_FLAGS="-Xclang -fopenmp -I$(OMP_INSTALL_PATH)/include" -DOpenMP_CXX_LIB_NAMES=libomp -DOpenMP_C_LIB_NAMES=libomp -DOpenMP_libomp_LIBRARY=$(OMP_INSTALL_PATH)/lib/libomp.dylib -DCMAKE_SHARED_LINKER_FLAGS="-L$(OMP_INSTALL_PATH)/lib -lomp"
These commands creates these libraries in the folder mkl-dnn-1.0/build/src:
  • libmkldnn.dylib
  • libmkldnn.0.dylib
  • libmkldnn.1.0.0.0.dylib
Copy these libraries to /usr/local/mkl-dnn/lib.
Copy the include files from mkld-dnn-1.0/include to /usr/local/mkl-dnn/include.
4. Copy the OpenMp dependencies required by mkldnn
  • OpenMP libraries from $(OMP_INSTALL_PATH)/lib to /usr/local/mkl-dnn/lib
  • OpenMP include files from $(OMP_INSTALL_PATH)/include to /usr/local/mkl-dnn/include
5. The OpenMp library path is hardbinded to mkldnn library. Below command would list out this aspect
otool -l libmkldnn.dylib
6. This hard binding of the paths must be changed for portability to other mac machines. Use the below command
install_name_tool -change /usr/local/opt/libomp/lib/libomp.dylib @rpath/libomp.dylib libmkldnn.0.dylib
install_name_tool -id "@rpath/libomp.dylib" libomp.dylib
Changing to rpath would enable portability of mkldnn/OpenMP libraries to different mac machines. This avoids any runtime linking issues.
7. 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')
  9 Comments
Junyu Quan
Junyu Quan on 11 Aug 2021
Hi Praveen
I have the same problem with Bonan. For"all built library files in fold oneDNN-1.0. should be copied to C:\Program Files\mkl-dnn" I find that I don't have the folder named mkl-dnn, dose it means I have error in installation? Or I should create the folder by my self?
Praveen Kumar Gajula
Praveen Kumar Gajula on 16 Aug 2021
Hi Junyu,
You need to create folders "mkl-dnn", "lib" and copy the libraries to C:\Program Files\mkl-dnn\lib.
Thanks,
Praveen

Sign in to comment.

Categories

Find more on Generating Code in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!