MXNet back end - QueensGambit/CrazyAra GitHub Wiki

Linux

2.1. Build the MXNet C++ package. Building with OpenCV is not required:

Install the prerequesites (e.g. for IntelMKL):

sudo apt-get update
sudo apt-get install -y build-essential git
sudo apt-get install -y libopenblas-dev liblapack-dev
sudo apt-get install -y graphviz
sudo apt-get install -y google-perftools

Download the latest source code of the MXNet release page:

or pull the latest updates from GitHub:

git clone https://github.com/apache/incubator-mxnet --recursive

IntelMKL:

make -j $(nproc) USE_CPP_PACKAGE=1 USE_OPENCV=0 USE_MKLDNN=1 USE_BLAS=mkl USE_INTEL_PATH=/opt/intel/oneapi USE_GPERFTOOLS=1

CUDA / cuDNN:

make -j $(nproc) USE_CPP_PACKAGE=1 USE_OPENCV=0 USE_MKL=0 USE_CUDA=1 USE_CUDNN=1

You might have to reduce the number of jobs to run in paralell e.g. -j 4 if you run out of memory (RAM) during building. Building the MXNet-C++ examples is not required.

Detailed build instruction can be found here:

2.2. Setup the MXNET_PATH

export MXNET_PATH=<path_to_mxnet>/incubator-mxnet/

Windows

2.1. Build the MXNet C++ package

Install all preliminaries and depending on your preference follow the CUDA or MKL guide.

Make sure to install Visual Studio together with the VC++ toolset and enable Visual Studio support when installing CUDA.

Clone the MXNet library:

git clone https://github.com/apache/incubator-mxnet --recursive

Make sure that msbuild.exe is found from the command line:

  • Windows->Edit System Environment Variables: Path
  • add C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin

Configure the library with the options you need (e.g. CUDA, CUDNN). Building with OpenCV is not required:

mkdir build
cd build

CUDA/cuDNN backend

cmake -G "Visual Studio 15 2017 Win64" -T cuda=10.1,host=x64^
 -DCMAKE_BUILD_TYPE=Release -DUSE_CUDA=1 -DUSE_CUDNN=1 -DUSE_CPP_PACKAGE=1^
 -DUSE_NVRTC=1 -DUSE_OPENCV=0 -DUSE_OPENMP=1 -DUSE_BLAS=open -DUSE_LAPACK=1^
 -DUSE_DIST_KVSTORE=0 -DCUDA_ARCH_LIST=Common -DCUDA_TOOLSET=10.1^
 -DCUDNN_INCLUDE="C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\include"^
 -DCUDNN_LIBRARY="C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\cudnn.lib" ..

MKL-Backend

set OpenBLAS_HOME=C:\utils\OpenBLAS
cmake -G "Visual Studio 15 Win64" -T host=x64 -DUSE_CUDA=0 -DUSE_CUDNN=0 -DUSE_NVRTC=0^
 -DUSE_CPP_PACKAGE=1 -DUSE_OPENCV=0 -DUSE_OPENMP=1 -DUSE_PROFILER=1^
 -DUSE_BLAS=mkl -DUSE_LAPACK=0 -DUSE_DIST_KVSTORE=0 -DCUDA_ARCH_NAME=All^
 -DUSE_MKLDNN=1 -DUSE_BLAS=mkl -DCMAKE_BUILD_TYPE=Release^
 -DCMAKE_INSTALL_PREFIX="C:\lib\googletest-release-1.10.0\build"^
 -DCMAKE_INSTALL_PREFIX="C:\lib\pthreads\Pre-built.2"^
 -DBUILD_CPP_EXAMPLES=1^
 -DMKL_INCLUDE_DIR="C:\Program Files (x86)\Intel\oneAPI\mkl\latest\include"^
 -DMKL_ROOT="C:\Program Files (x86)\Intel\oneAPI\mkl\latest" ..

Start the building process:

msbuild mxnet.sln /p:Configuration=Release;Platform=x64 /maxcpucount

Trouble Shoouting

  • If you encounter a out of heap memory error, you should remove /maxcpucount and add the following target properties in the CMakeLists.txt file.
set_target_properties(mxnet PROPERTIES COMPILE_FLAGS "/bigobj")
set_target_properties(mxnet PROPERTIES COMPILE_FLAGS "-d2SSAOptimizer-")

Generate the "op.h" file:

python OpWrapperGenerator.py "\your\build\Release\libmxnet.dll"

If this doesn't work on your system due to encoding errors, you can try downloading the file instead:

2.2. Setup the MXNET_PATH

export MXNET_PATH=<path_to_mxnet>/incubator-mxnet/

Mac

MKL-Backend

LIBRARY_PATH=$(brew --prefix llvm)/lib/ make -j $(sysctl -n hw.ncpu) CC=$(brew --prefix llvm)/bin/clang CXX=$(brew --prefix llvm)/bin/clang++ USE_OPENMP=1 USE_MKLDNN=1 USE_BLAS=apple USE_INTEL_PATH=/opt/intel/oneapi USE_CPP_PACKAGE=1 CPATH=$CPATH:/usr/local/opt/openblas/include MKL_ROOT=/opt/intel/oneapi/mkl/latest USE_OPENCV=0 BUILD_CPP_EXAMPLES=0 USE_CUDA=0 USE_CUDNN=0

Change library search path (optional)

By default, the libmxnet.so file, will refer to /usr/local/opt/llvm/lib/ which is not ideal when publishing a release.

We can use install_name_tool to update the library search path.

install_name_tool -add_rpath @executable_path/. libmxnet.so
install_name_tool -change /usr/local/opt/llvm/lib/libomp.dylib @rpath/libomp.dylib libmxnet.so
install_name_tool -change /usr/local/opt/llvm/lib/libc++.1.dylib @rpath/libc++.1.dylib libmxnet.so
install_name_tool -add_rpath @executable_path/. libc++abi.1.dylib

Before:

otool -L libmxnet.so 
libmxnet.so:
	@rpath/libmxnet.so (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)
	/System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate (compatibility version 1.0.0, current version 4.0.0)
	/usr/local/opt/llvm/lib/libomp.dylib (compatibility version 5.0.0, current version 5.0.0)
	/usr/local/opt/llvm/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)

After:

otool -L libmxnet.so
libmxnet.so:
	@rpath/libmxnet.so (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)
	/System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate (compatibility version 1.0.0, current version 4.0.0)
	@rpath/libomp.dylib (compatibility version 5.0.0, current version 5.0.0)
	@rpath/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)

Next part: