From 69686593a19d8eeed5448dd11fbbfdd3f4eb603b Mon Sep 17 00:00:00 2001 From: baojun <32073718+baojun-nervana@users.noreply.github.com> Date: Thu, 4 Jan 2018 10:03:33 -0800 Subject: [PATCH] Picking up mklml 20171227 (#1126) * update to mklml 20171227 * update mklmk to 20171227 * update mkl version to v0.12 * update mklml artifacts * enabled mkl mac install * ignore mac and win mkl dynamic link files * fix style * add case selection for darwin * disabled openblas warning on mac --- .gitignore | 5 ++++- Makefile | 2 +- docker/Dockerfile | 2 +- install_mkl.sh | 18 +++++++++++++++++- neon/backends/mklEngine/Makefile | 10 +++++++++- neon/backends/mklEngine/make_msys64.bat | 6 +++--- neon/backends/nervanacpu.py | 6 ++++-- neon/backends/nervanamkl.py | 13 +++++++++++++ neon/backends/util/check_mkl.py | 20 ++++++++++++++++++++ prepare_mkl.sh | 15 +++++++++++---- setup.py | 3 ++- 11 files changed, 85 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index 876aa8fb..97fb91c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +*.dll +*.dylib +*.dSYM *.sublime-project *.sublime-workspace *.pyc @@ -34,4 +37,4 @@ neon/data/loader/loader .idea/ aeon dist -mklml_lnx_* +mklml_* diff --git a/Makefile b/Makefile index 7375232e..3975ec92 100644 --- a/Makefile +++ b/Makefile @@ -194,7 +194,7 @@ clean_so: clean: clean_py clean_so @echo "Removing virtual environment files..." - @rm -rf aeon build dist mklml_lnx* + @rm -rf aeon build dist mklml_* @rm -rf $(VIRTUALENV_DIR_BASE) $(VIRTUALENV_DIR_BASE)2 $(VIRTUALENV_DIR_BASE)3 $(STYLEVIRTUALENV_DIR_BASE) @echo diff --git a/docker/Dockerfile b/docker/Dockerfile index 883352e9..f093367c 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -39,7 +39,7 @@ RUN pip --no-cache-dir install --upgrade \ RUN git clone https://github.com/NervanaSystems/neon /neon && \ make -C /neon sysinstall && \ - rm -rf /neon/mklml_lnx_*.tgz + rm -rf /neon/mklml_*.tgz WORKDIR /neon diff --git a/install_mkl.sh b/install_mkl.sh index 854683ae..78d29c3e 100755 --- a/install_mkl.sh +++ b/install_mkl.sh @@ -57,6 +57,22 @@ if [[ `uname` == 'Linux' ]]; then cd $MKL_ENGINE_PATH && make clean && make cd $THIS_DIR +elif [[ `uname` == 'Darwin' ]]; then + echo -e "Mac detected" + RETURN_STRING=`./prepare_mkl.sh 2` + export MKLROOT=`echo $RETURN_STRING | awk -F "=" '{print $2}' | awk '{print $1}'` + echo -e "mkl root: ${GREEN}$MKLROOT${NORM}" + export LD_LIBRARY_PATH=$MKLROOT/lib:$LD_LIBRARY_PATH + export LIBRARY_PATH=$MKLROOT/lib:$LIBRARY_PATH + export CPATH=$MKLROOT/include:$CPATH + #build neon mklEngine + MKL_ENGINE_PATH='neon/backends/mklEngine' + export DYLD_LIBRARY_PATH=$MKLROOT/lib:$THIS_DIR/$MKL_ENGINE_PATH:$DYLD_LIBRARY_PATH + export MAC_BUILD=1 + cd $MKL_ENGINE_PATH && make clean && make + cp ${MKLROOT}/lib/*.dylib . + cd $THIS_DIR + elif [[ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" || "$(expr substr $(uname -s) 1 7)" == "MSYS_NT" ]]; then echo -e "Windows detected" #build neon mklEngine @@ -65,5 +81,5 @@ elif [[ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" || "$(expr substr $(un cd $THIS_DIR else - echo -e "Currently only Linux Environment supported, skipping MKL install" + echo -e "Environment not supported, skipping MKL install" fi diff --git a/neon/backends/mklEngine/Makefile b/neon/backends/mklEngine/Makefile index 6af7c6a0..7eaac36d 100644 --- a/neon/backends/mklEngine/Makefile +++ b/neon/backends/mklEngine/Makefile @@ -28,6 +28,14 @@ else CFLAGS := -fopenmp -lmklml_gnu -fPIC -march=native -g -liomp5 endif +ifeq ($(MAC_BUILD), 1) + CFLAGS := -fopenmp -lmklml -fPIC -march=native -g -liomp5 + COMMON_FLAGS := -std=c99 -O3 -I$(MKL_PATH)/include -L$(MKL_PATH)/lib + MKL_ENGINE_SO := mklEngine.dylib + MATH_CPU_SO := cmath.dylib + CC := gcc-7 +endif + ifeq ($(WIN_BUILD), 1) CFLAGS := -fopenmp -lmklml -fPIC -march=native -g -liomp5md endif @@ -45,5 +53,5 @@ $(MATH_CPU_SO): src/math_cpu.c $(CC) $^ -shared -o $@ $(COMMON_FLAGS) $(CFLAGS) clean: - @rm -vf *.o *.so *.dll + @rm -vrf *.o *.so *.dylib *.dSYM *.dll diff --git a/neon/backends/mklEngine/make_msys64.bat b/neon/backends/mklEngine/make_msys64.bat index add104b7..a36a839f 100644 --- a/neon/backends/mklEngine/make_msys64.bat +++ b/neon/backends/mklEngine/make_msys64.bat @@ -1,10 +1,10 @@ set WIN_BUILD=1 -set MKLROOT=mklml_win_2018.0.20170908 -wget https://github.com/01org/mkl-dnn/releases/download/v0.10/%MKLROOT%.zip +set MKLROOT=mklml_win_2018.0.1.20171227 +wget https://github.com/01org/mkl-dnn/releases/download/v0.12/%MKLROOT%.zip pacman -S --needed unzip unzip %MKLROOT%.zip make copy "%MKLROOT%\lib\mklml.dll" ren *.so *.dll rmdir /q /s %MKLROOT% -rm %MKLROOT%.zip \ No newline at end of file +rm %MKLROOT%.zip diff --git a/neon/backends/nervanacpu.py b/neon/backends/nervanacpu.py index 131d3e4d..a8caf49d 100644 --- a/neon/backends/nervanacpu.py +++ b/neon/backends/nervanacpu.py @@ -19,6 +19,7 @@ from __future__ import division from builtins import object, round, str, zip import numpy as np +import sys import logging import time import functools @@ -563,8 +564,9 @@ def __init__(self, "performance may suffer. Consider installing " "one of openblas, Atlas, MKL, or vecLib") except (AttributeError, KeyError): - logger.warn("Problems inferring BLAS info, CPU performance may " - "be suboptimal") + if sys.platform != 'darwin': + logger.warn("Problems inferring BLAS info, CPU performance may " + "be suboptimal") self.device_type = 0 self.device_id = 0 diff --git a/neon/backends/nervanamkl.py b/neon/backends/nervanamkl.py index ac382498..99ceb35c 100644 --- a/neon/backends/nervanamkl.py +++ b/neon/backends/nervanamkl.py @@ -194,6 +194,19 @@ def __init__(self, 'mklEngine.dll') self.mklEngine = ctypes.windll.LoadLibrary(mkl_engine_path) math_engine_path = os.path.join(os.path.dirname(__file__), 'mklEngine', 'cmath.dll') + + elif sys.platform == 'darwin': + mkl_ml_path = os.path.join(path, os.pardir, 'backends', 'mklEngine', + 'libmklml.dylib') + ctypes.cdll.LoadLibrary(mkl_ml_path) + iomp5_path = os.path.join(path, os.pardir, 'backends', 'mklEngine', + 'libiomp5.dylib') + ctypes.cdll.LoadLibrary(iomp5_path) + mkl_engine_path = os.path.join(path, os.pardir, 'backends', 'mklEngine', + 'mklEngine.dylib') + self.mklEngine = ctypes.cdll.LoadLibrary(mkl_engine_path) + math_engine_path = os.path.join(os.path.dirname(__file__), 'mklEngine', 'cmath.dylib') + else: mkl_engine_path = os.path.join(path, os.pardir, 'backends', 'mklEngine', 'mklEngine.so') diff --git a/neon/backends/util/check_mkl.py b/neon/backends/util/check_mkl.py index f44cc05d..4e855f90 100644 --- a/neon/backends/util/check_mkl.py +++ b/neon/backends/util/check_mkl.py @@ -44,6 +44,26 @@ def get_mkl_lib(device_id=None, verbose=False): return 0 return 1 + elif sys.platform == 'darwin': + # find *.dylib + current_path = os.path.dirname(os.path.realpath(__file__)) + mkl_engine_path = os.path.join(current_path, os.pardir, 'mklEngine', 'mklEngine.dylib') + if not os.path.isfile(mkl_engine_path): + neon_logger.display("mklEngine.dylib not found") + return 0 + + math_engine_path = os.path.join(current_path, os.pardir, 'mklEngine', 'cmath.dylib') + if not os.path.isfile(math_engine_path): + neon_logger.display("cmath.dylib not found") + return 0 + + header_path = os.path.join(os.path.dirname(__file__), 'mklEngine', + 'src', 'math_cpu.header') + if os.path.isfile(header_path): + neon_logger.display("math_cpu.header not found") + return 0 + return 1 + else: # find *.so current_path = os.path.dirname(os.path.realpath(__file__)) diff --git a/prepare_mkl.sh b/prepare_mkl.sh index a29cd5d2..be3ebc33 100755 --- a/prepare_mkl.sh +++ b/prepare_mkl.sh @@ -20,7 +20,10 @@ FindLibrary() intel|1) LOCALMKL=`find $DST -name libmklml_intel.so` # name of MKL SDL lib ;; - *) + 2) + LOCALMKL=`find $DST -name libmklml.dylib` + ;; + *) LOCALMKL=`find $DST -name libmklml_gnu.so` # name of MKL SDL lib ;; esac @@ -40,12 +43,16 @@ echo $VERSION_LINE # Return Version Line } # MKL +PLATFORM=lnx +if [ `uname` = 'Darwin' ]; then + PLATFORM=mac +fi DST=`dirname $0` OMP=0 -VERSION_MATCH=20171007 -ARCHIVE_BASE=mklml_lnx_2018.0.1.$VERSION_MATCH +VERSION_MATCH=20171227 +ARCHIVE_BASE=mklml_${PLATFORM}_2018.0.1.$VERSION_MATCH ARCHIVE_BASENAME=$ARCHIVE_BASE.tgz -GITHUB_RELEASE_TAG=v0.11 +GITHUB_RELEASE_TAG=v0.12 MKLURL="https://github.com/01org/mkl-dnn/releases/download/$GITHUB_RELEASE_TAG/$ARCHIVE_BASENAME" # there are diffrent MKL lib to be used for GCC and for ICC reg='^[0-9]+$' diff --git a/setup.py b/setup.py index e8b5a1c1..27e9032d 100755 --- a/setup.py +++ b/setup.py @@ -110,8 +110,9 @@ 'backends/kernels/maxas/MaxAs/*.pm', 'backends/mklEngine/*.so', 'backends/mklEngine/*.dll', + 'backends/mklEngine/*.dylib', 'backends/mklEngine/src/*.header', - '../mklml_lnx_*/lib/*.so', + '../mklml_*/lib/*.so', '../loader/bin/*.so']}, classifiers=['Development Status :: 3 - Alpha', 'Environment :: Console',