MATLAB Answers

Bill Chou
1

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

Asked by Bill Chou
on 27 Feb 2019
Latest activity Commented on by Usatyuk Vasiliy on 30 Aug 2019
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.

1 Answer

Answer by Bill Chou
on 27 Feb 2019
Edited by Bill Chou
on 27 Feb 2019
 Accepted Answer

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. For MATLAB Coder R2018b and R2019a releases, use MKL-DNN version v0.14. For other MATLAB Coder releases, use the MKL-DNN library specific for the release, as described in the release documentation: https://www.mathworks.com/help/coder/ug/prerequisites-for-deep-learning-with-matlab-coder.html
Note that on Windows® operating systems, special characters and spaces are allowed in the library paths only if 8.3 file names are enabled. If they are not enabled, then replace paths with paths that do not contain spaces. For more information on 8.3 file names, refer to the Windows documentation.
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.