-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Add linux and macos MKLDNN Building Instruction #11049
Changes from 9 commits
74be568
5966cb5
a87d357
5a73eea
46e5cea
3f3259f
2c503c2
a8b5e9b
cb2080b
4f23cbb
f477fa9
3f74b86
b7dc9a5
33def02
6e6472f
846d3bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
# Build/Install MXNet with MKL-DNN | ||
|
||
<h2 id="0">Contents</h2> | ||
|
||
* [1. Linux](#1) | ||
* [2. MacOS](#2) | ||
* [3. Windows](#3) | ||
* [4. Verify MXNet with python](#4) | ||
* [5. Enable MKL BLAS](#5) | ||
|
||
<h2 id="1">Linux</h2> | ||
|
||
### Prerequisites | ||
|
||
``` | ||
apt-get update && apt-get install -y build-essential git libopencv-dev curl gcc libopenblas-dev python python-pip python-dev python-opencv graphviz python-scipy python-sklearn | ||
``` | ||
|
||
### Clone MXNet sources | ||
|
||
``` | ||
git clone --recursive https://github.com/apache/incubator-mxnet.git | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about the different pip options? Since this PR started I added a table to the instructions and made a recommendation on the mkl install. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why pip? This is just a instruction for building with mkldnn or MKL blas from source. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The title of the doc is "Build/Install MXNet with MKL-DNN", so I thought you might want to cover the available options, or at least mention them. |
||
cd incubator-mxnet | ||
git submodule update --recursive --init | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason you use this extra step for Linux but not the other OSs? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. del There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i don't think we need this extra step. @xinyu-intel There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you please delete |
||
``` | ||
|
||
### Build MXNet with MKL-DNN | ||
|
||
``` | ||
make -j $(nproc) USE_OPENCV=1 USE_MKLDNN=1 USE_BLAS=mkl USE_INTEL_PATH=/opt/intel | ||
``` | ||
|
||
If you don't have full MKL library installed, you can use OpenBLAS by setting `USE_BLAS=openblas`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you clarify this state? Why wouldn't the user have the full MKL lib? That's not in the prerequisites? Can you link to installation instructions, so the user can upgrade to the full install? What happens if you don't have it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep consistent with https://mxnet.incubator.apache.org/install/windows_setup.html There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MKL is a faster implementation of openblas. one can replace the other. |
||
|
||
<h2 id="2">MacOS</h2> | ||
|
||
### Prerequisites | ||
|
||
Install the dependencies, required for MXNet, with the following commands: | ||
|
||
- [Homebrew](https://brew.sh/) | ||
- gcc (clang in macOS does not support OpenMP) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious if a specific version of CLT or XCode is expected.... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to have a try:) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should work for all versions. |
||
- OpenCV (for computer vision operations) | ||
|
||
``` | ||
# Paste this command in Mac terminal to install Homebrew | ||
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" | ||
|
||
# install dependency | ||
brew update | ||
brew install pkg-config | ||
brew install graphviz | ||
brew tap homebrew/core | ||
brew install opencv | ||
brew tap homebrew/versions | ||
brew install gcc49 | ||
brew link gcc49 | ||
``` | ||
|
||
### Enable OpenMP for MacOS | ||
|
||
If you want to enable OpenMP for better performance, you should modify these two files: | ||
|
||
1. Makefile L138: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where would I find this file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe you can add a path? |
||
|
||
``` | ||
ifeq ($(USE_OPENMP), 1) | ||
# ifneq ($(UNAME_S), Darwin) | ||
CFLAGS += -fopenmp | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the default clang compilers shipped in command line tools don't support this switch, but the one shipped with brew's llvm does. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo, 'set mac complier to gcc49', I've add them to the make command. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if you just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
# endif | ||
endif | ||
``` | ||
|
||
2. prepare_mkldnn.sh L96: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where would I find this file? Please provide relative locations or general instructions where it might be found. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe you can add a path? |
||
|
||
``` | ||
CC=gcc-4.9 CXX=g++-4.9 cmake $MKLDNN_ROOTDIR -DCMAKE_INSTALL_PREFIX=$MKLDNN_INSTALLDIR -B$MKLDNN_BUILDDIR -DARCH_OPT_FLAGS="-mtune=generic" -DWITH_TEST=OFF -DWITH_EXAMPLE=OFF >&2 | ||
``` | ||
|
||
### Build MXNet with MKL-DNN | ||
|
||
``` | ||
make -j $(sysctl -n hw.ncpu) USE_OPENCV=0 USE_OPENMP=1 USE_MKLDNN=1 USE_BLAS=apple USE_PROFILER=1 | ||
``` | ||
|
||
*Note: Temporarily disable OPENCV.* | ||
|
||
<h2 id="3">Windows</h2> | ||
|
||
To build and install MXNet yourself, you need the following dependencies. Install the required dependencies: | ||
|
||
1. If [Microsoft Visual Studio 2015](https://www.visualstudio.com/vs/older-downloads/) is not already installed, download and install it. You can download and install the free community edition. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there an issue with using the latest Visual Studio 2017 Community Edition? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To use VS2017, please follow this link to modify VC++ and change the version of the Visual studio 2017 to v14.11 before building. VS2015 is prefered. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so, do we need to clarify this point in the doc as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd mention it. People have asked about using the latest Visual Studio. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MKL-DNN officially supports VS2015 and I don't know whether VS2017 works. I'd like to ask MKL-DNN team and try it later because i'm suffering from cpu int8 now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've checked building with VS2017 without any issues. Could you take a review and can we merge if not any questions? Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a note about the VS2017 to the instructions here? It's what people see by default when they go to download VS, so it makes it easier for the user to try out. |
||
2. Download and Install [CMake](https://cmake.org/) if it is not already installed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any version? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep consistent with https://mxnet.incubator.apache.org/install/windows_setup.html There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That page is woefully out of date. I know it is being worked on. cc'ing @ankkhedia if there's any context he should know about as part of his updates. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add this. |
||
3. Download and install [OpenCV](http://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.0.0/opencv-3.0.0.exe/download). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Say OpenCV 3.0.0. What if you have v2 already? Will that work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep consistent with https://mxnet.incubator.apache.org/install/windows_setup.html There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean for you to put the version in the link text, instead of hidden in the link itself. People might skip the step if they think any version will do. Having the user base be your test subjects is less than ideal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll add this. |
||
4. Unzip the OpenCV package. | ||
5. Set the environment variable ```OpenCV_DIR``` to point to the ```OpenCV build directory``` (```C:\opencv\build\x64\vc14``` for example). Also, you need to add the OpenCV bin directory (```C:\opencv\build\x64\vc14\bin``` for example) to the ``PATH`` variable. | ||
6. If you have Intel Math Kernel Library (MKL) installed, set ```MKL_ROOT``` to point to ```MKL``` directory that contains the ```include``` and ```lib```. If you want to use MKL blas, you should set ```-DUSE_BLAS=mkl``` when cmake. Typically, you can find the directory in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to just use MKL, and it didn't work. Looks like mshadow still wants OpenBLAS. cmake -G "Visual Studio 15 Win64" .. -DUSE_CUDA=0 -DUSE_CUDNN=0 -DUSE_NVRTC=0 -DUSE_OPENCV=1 -DUSE_OPENMP=1 -DUSE_PROFILER=1 -DUSE_BLAS=mkl -DUSE_LAPACK=1 -DUSE_DIST_KVSTORE=0 -DCUDA_ARCH_NAME=All -DUSE_MKLDNN=1 -DCMAKE_BUILD_TYPE=Release There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
```C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2018\windows\mkl```. | ||
7. If you don't have the Intel Math Kernel Library (MKL) installed, download and install [OpenBLAS](http://sourceforge.net/projects/openblas/files/v0.2.14/). Note that you should also download ```mingw64.dll.zip`` along with openBLAS and add them to PATH. | ||
8. Set the environment variable ```OpenBLAS_HOME``` to point to the ```OpenBLAS``` directory that contains the ```include``` and ```lib``` directories. Typically, you can find the directory in ```C:\Program files (x86)\OpenBLAS\```. | ||
|
||
After you have installed all of the required dependencies, build the MXNet source code: | ||
|
||
1. Download the MXNet source code from [GitHub](https://github.com/apache/incubator-mxnet). Don't forget to pull the submodules: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep consistent with https://mxnet.incubator.apache.org/install/windows_setup.html |
||
``` | ||
git clone --recursive https://github.com/apache/incubator-mxnet.git | ||
``` | ||
|
||
2. Copy file `3rdparty/mkldnn/config_template.vcxproj` to incubator-mxnet root. | ||
|
||
3. Start a Visual Studio command prompt. | ||
|
||
4. Use [CMake](https://cmake.org/) to create a Visual Studio solution in ```./build``` or some other directory. Make sure to specify the architecture in the | ||
[CMake](https://cmake.org/) command: | ||
``` | ||
mkdir build | ||
cd build | ||
cmake -G "Visual Studio 14 Win64" .. -DUSE_CUDA=0 -DUSE_CUDNN=0 -DUSE_NVRTC=0 -DUSE_OPENCV=1 -DUSE_OPENMP=1 -DUSE_PROFILER=1 -DUSE_BLAS=open -DUSE_LAPACK=1 -DUSE_DIST_KVSTORE=0 -DCUDA_ARCH_NAME=All -DUSE_MKLDNN=1 -DCMAKE_BUILD_TYPE=Release | ||
``` | ||
|
||
5. In Visual Studio, open the solution file,```.sln```, and compile it. | ||
These commands produce a library called ```libmxnet.dll``` in the ```./build/Release/``` or ```./build/Debug``` folder. | ||
Also ```libmkldnn.dll``` with be in the ```./build/3rdparty/mkldnn/src/Release/``` | ||
|
||
6. Make sure that all the dll files used above(such as `libmkldnn.dll`, `libmklml.dll`, `libiomp5.dll`, `libopenblas.dll`, etc) are added to the system PATH. For convinence, you can put all of them to ```\windows\system32```. Or you will come across `Not Found Dependencies` when loading mxnet. | ||
|
||
<h2 id="4">Verify MXNet with python</h2> | ||
|
||
``` | ||
export PYTHONPATH=~/incubator-mxnet/python | ||
pip install --upgrade pip | ||
pip install --upgrade jupyter graphviz cython pandas bokeh matplotlib opencv-python requests | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. List and alphabetize... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. keep consistent with https://mxnet.incubator.apache.org/install/windows_setup.html |
||
python -c "import mxnet as mx;print((mx.nd.ones((2, 3))*2).asnumpy());" | ||
|
||
Expected Output: | ||
|
||
[[ 2. 2. 2.] | ||
[ 2. 2. 2.]] | ||
``` | ||
|
||
### Verify whether MKL-DNN works | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't tell if this section was a continuation of Windows or not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't you see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see those. It's just not a pattern of formatting for markdown that I'm used to. It works in the view, so it's fine. Thanks. |
||
|
||
After MXNet is installed, you can verify if MKL-DNN backend works well with a single Convolution layer. | ||
|
||
``` | ||
import mxnet as mx | ||
import numpy as np | ||
|
||
num_filter = 32 | ||
kernel = (3, 3) | ||
pad = (1, 1) | ||
shape = (32, 32, 256, 256) | ||
|
||
x = mx.sym.Variable('x') | ||
w = mx.sym.Variable('w') | ||
y = mx.sym.Convolution(data=x, weight=w, num_filter=num_filter, kernel=kernel, no_bias=True, pad=pad) | ||
exe = y.simple_bind(mx.cpu(), x=shape) | ||
|
||
exe.arg_arrays[0][:] = np.random.normal(size=exe.arg_arrays[0].shape) | ||
exe.arg_arrays[1][:] = np.random.normal(size=exe.arg_arrays[1].shape) | ||
|
||
exe.forward(is_train=False) | ||
o = exe.outputs[0] | ||
t = o.asnumpy() | ||
``` | ||
|
||
You can open the `MKLDNN_VERBOSE` flag by setting environment variable: | ||
``` | ||
export MKLDNN_VERBOSE=1 | ||
``` | ||
Then by running above code snippet, you probably will get the following output message which means `convolution` and `reorder` primitive from MKL-DNN are called. Layout information and primitive execution performance are also demonstrated in the log message. | ||
``` | ||
mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nChw16c,num:1,32x32x256x256,6.47681 | ||
mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_oihw out:f32_OIhw16i16o,num:1,32x32x3x3,0.0429688 | ||
mkldnn_verbose,exec,convolution,jit:avx512_common,forward_inference,fsrc:nChw16c fwei:OIhw16i16o fbia:undef fdst:nChw16c,alg:convolution_direct,mb32_g1ic32oc32_ih256oh256kh3sh1dh0ph1_iw256ow256kw3sw1dw0pw1,9.98193 | ||
mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_oihw out:f32_OIhw16i16o,num:1,32x32x3x3,0.0510254 | ||
mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nChw16c out:f32_nchw,num:1,32x32x256x256,20.4819 | ||
``` | ||
|
||
<h2 id="5">Enable MKL BLAS</h2> | ||
|
||
To make it convenient for customers, Intel introduced a new license called [Intel® Simplified license](https://software.intel.com/en-us/license/intel-simplified-software-license) that allows to redistribute not only dynamic libraries but also headers, examples and static libraries. | ||
|
||
Installing and enabling the full MKL installation enables MKL support for all operators under the linalg namespace. | ||
|
||
1. Download and install the latest full MKL version following instructions on the [intel website.](https://software.intel.com/en-us/mkl) | ||
|
||
2. Run `make -j ${nproc} USE_BLAS=mkl` | ||
|
||
3. Navigate into the python directory | ||
|
||
4. Run `sudo python setup.py install` | ||
|
||
### Verify whether MKL works | ||
|
||
After MXNet is installed, you can verify if MKL BLAS works well with a single dot layer. | ||
|
||
``` | ||
import mxnet as mx | ||
import numpy as np | ||
|
||
shape_x = (1, 10, 8) | ||
shape_w = (1, 12, 8) | ||
|
||
x_npy = np.random.normal(0, 1, shape_x) | ||
w_npy = np.random.normal(0, 1, shape_w) | ||
|
||
x = mx.sym.Variable('x') | ||
w = mx.sym.Variable('w') | ||
y = mx.sym.batch_dot(x, w, transpose_b=True) | ||
exe = y.simple_bind(mx.cpu(), x=x_npy.shape, w=w_npy.shape) | ||
|
||
exe.forward(is_train=False) | ||
o = exe.outputs[0] | ||
t = o.asnumpy() | ||
``` | ||
|
||
You can open the `MKL_VERBOSE` flag by setting environment variable: | ||
``` | ||
export MKL_VERBOSE=1 | ||
``` | ||
Then by running above code snippet, you probably will get the following output message which means `SGEMM` primitive from MKL are called. Layout information and primitive execution performance are also demonstrated in the log message. | ||
``` | ||
Numpy + Intel(R) MKL: THREADING LAYER: (null) | ||
Numpy + Intel(R) MKL: setting Intel(R) MKL to use INTEL OpenMP runtime | ||
Numpy + Intel(R) MKL: preloading libiomp5.so runtime | ||
MKL_VERBOSE Intel(R) MKL 2018.0 Update 1 Product build 20171007 for Intel(R) 64 architecture Intel(R) Advanced Vector Extensions 512 (Intel(R) AVX-512) enabled processors, Lnx 2.40GHz lp64 intel_thread NMICDev:0 | ||
MKL_VERBOSE SGEMM(T,N,12,10,8,0x7f7f927b1378,0x1bc2140,8,0x1ba8040,8,0x7f7f927b1380,0x7f7f7400a280,12) 8.93ms CNR:OFF Dyn:1 FastMM:1 TID:0 NThr:40 WDiv:HOST:+0.000 | ||
``` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any conclusion? Links to more info / help? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to Intel MKL There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a link to there - or if you have a support forum. You could also link to the discuss.mxnet.io. And you could link to https://github.com/apache/incubator-mxnet/labels/MKL and https://github.com/apache/incubator-mxnet/labels/MKLDNN Something like: Next Steps and Support
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good suggestions.I will add some related links. However, MKLDNN is a backed of MXNet and MKL is a optional BLAS library for MXNet. There are not many examples for themselves beside installation. For users, they can build MXNet with them following this instruction and then refer to MXNet's tutorials and examples directly. |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you format so it is easier to read? Maybe add sudo since most people would need that (unless this is intended to be docker instructions).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep consistent with https://mxnet.incubator.apache.org/install/index.html