Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Refactor operators & MKLDNN #8302

Merged
merged 50 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
3a90190
Use NNVM interface.
zheng-da Nov 27, 2017
4ecf3a1
Remove MKL code.
zheng-da Oct 20, 2017
709ec84
Integrate MKLDNN.
zheng-da Oct 21, 2017
d625b31
revert modification in test_executor.
zheng-da Jan 17, 2018
3cb6606
Fix a bug in FlattenStorageType.
zheng-da Jan 17, 2018
197fff2
Remove BN debug.
zheng-da Jan 17, 2018
785fa5f
Remove remaining MXNET_USE_MKL2017
zheng-da Jan 17, 2018
09eca97
Remove unused code in pooling.
zheng-da Jan 18, 2018
8af167e
Fixing bugs in gtests.
zheng-da Jan 18, 2018
5752525
Fix lint errors.
zheng-da Jan 18, 2018
853a28f
a lot of minor updates to address comments.
zheng-da Jan 19, 2018
c76d3ed
Fix coding style in MKLDNN Pooling (#22)
TaoLv Jan 19, 2018
8ba9736
revert the code change in the previous code refactor.
zheng-da Jan 19, 2018
6c7f2de
Fix a bug in pooling.
zheng-da Jan 19, 2018
d3dac5f
LRN coding style changes (#21)
Jan 19, 2018
e015f11
cast storage with MKLDNN properly.
zheng-da Jan 19, 2018
386dc55
Minor updates to address comments.
zheng-da Jan 19, 2018
cb1d630
Merge branch 'refactor' of https://github.com/zheng-da/incubator-mxne…
zheng-da Jan 19, 2018
399ff52
some minor updates.
zheng-da Jan 19, 2018
62995b6
Switch to the master branch of MKLDNN.
zheng-da Jan 19, 2018
4c3f112
Minor updates to address comments.
zheng-da Jan 19, 2018
1078898
Update activation.cc
zheng-da Jan 20, 2018
c5de4ed
Fix a bug in convert NDArray.
zheng-da Jan 23, 2018
fce31ed
Add gluon model zoo tests.
zheng-da Jan 23, 2018
7e8537c
Update GPU tests on model zoo.
zheng-da Jan 23, 2018
4723054
Avoid using mobilenet for GPU tests with gluon models.
zheng-da Jan 23, 2018
8cb1970
Update GPU tests on gluon.
zheng-da Jan 24, 2018
2f8028a
change cmake to compile MKLDNN.
zheng-da Jan 25, 2018
682cec6
Merge branch 'refactor' of https://github.com/zheng-da/incubator-mxne…
zheng-da Jan 25, 2018
619fe85
update cmake for MKLDNN.
zheng-da Jan 25, 2018
d2181d4
Implement align myself.
zheng-da Jan 26, 2018
4b84308
Merge branch 'refactor' of https://github.com/zheng-da/incubator-mxne…
zheng-da Jan 26, 2018
46a7209
Switch to intel/mkl-dnn.
zheng-da Jan 26, 2018
26ecfb8
Fix errors in align unittest.
zheng-da Jan 26, 2018
839441a
Add unit test for LRN.
zheng-da Jan 26, 2018
b9cc3d9
Merge branch 'refactor' of https://github.com/zheng-da/incubator-mxne…
zheng-da Jan 26, 2018
4d781a1
fix a compilation error.
zheng-da Jan 26, 2018
10619fe
Merge branch 'refactor' of https://github.com/zheng-da/incubator-mxne…
zheng-da Jan 26, 2018
2ac3918
use storage_type_assign to determine storage type.
zheng-da Jan 26, 2018
7caa0db
avoid global pooling in mkldnn.
zheng-da Jan 29, 2018
5a037bd
compare all MKLDNN ops with native impls.
zheng-da Jan 30, 2018
9560775
Fix a bug in testing correctness.
zheng-da Jan 30, 2018
06e0ab6
print the name of buggy operator.
zheng-da Jan 30, 2018
e4b4fc5
undo some modifications.
zheng-da Jan 30, 2018
6668727
Fix a bug on reshaped array.
zheng-da Jan 30, 2018
b5e210d
avoid testing outputs with NullOp.
zheng-da Jan 30, 2018
7c8456e
turn on MKLDNN tests in Jenkins.
zheng-da Jan 30, 2018
050b95c
print each operator in MKLDNN tests.
zheng-da Jan 30, 2018
5b3f647
rename test_gluon_model_zoo.py
zheng-da Jan 31, 2018
e22b2e1
Create hashcode for operator parameters properly.
zheng-da Jan 31, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@
[submodule "3rdparty/googletest"]
path = 3rdparty/googletest
url = https://github.com/google/googletest.git
[submodule "3rdparty/mkldnn"]
path = 3rdparty/mkldnn
url = https://github.com/intel/mkl-dnn.git
branch = master
1 change: 1 addition & 0 deletions 3rdparty/mkldnn
Submodule mkldnn added at 3e1f8f
14 changes: 3 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ mxnet_option(USE_OPENMP "Build with Openmp support" ON)
mxnet_option(USE_CUDNN "Build with cudnn support" ON) # one could set CUDNN_ROOT for search path
mxnet_option(USE_LAPACK "Build with lapack support" ON IF NOT MSVC)
mxnet_option(USE_MKL_IF_AVAILABLE "Use MKL if found" ON)
mxnet_option(USE_MKLML_MKL "Use MKLML variant of MKL (if MKL found)" ON IF USE_MKL_IF_AVAILABLE AND UNIX AND (NOT APPLE))
mxnet_option(USE_MKL_EXPERIMENTAL "Use experimental MKL (if MKL enabled and found)" OFF)
mxnet_option(USE_MKLDNN "Use MKLDNN variant of MKL (if MKL found)" ON IF USE_MKL_IF_AVAILABLE AND UNIX AND (NOT APPLE))
mxnet_option(USE_MKLML_MKL "Use MKLDNN variant of MKL (if MKL found)" ON IF USE_MKL_IF_AVAILABLE AND UNIX AND (NOT APPLE))
mxnet_option(USE_OPERATOR_TUNING "Enable auto-tuning of operators" ON AND NOT MSVC)
mxnet_option(USE_GPERFTOOLS "Build with GPerfTools support (if found)" ON)
mxnet_option(USE_JEMALLOC "Build with Jemalloc support" ON)
Expand Down Expand Up @@ -138,14 +138,11 @@ if(USE_VTUNE)
endif()

if(USE_MKL_IF_AVAILABLE)
if(USE_MKL_EXPERIMENTAL AND NOT USE_MKLML_MKL)
message(ERROR " USE_MKL_EXPERIMENTAL can only be used when USE_MKL_EXPERIMENTAL is enabled")
endif()
find_package(MKL)
if(MKL_FOUND)
include_directories(${MKL_INCLUDE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/operator/mkl)
add_definitions(-DMXNET_USE_MKL2017=1)
add_definitions(-DMXNET_USE_MKLDNN=1)
add_definitions(-DUSE_MKL=1)
add_definitions(-DCUB_MKL=1)
list(APPEND mxnet_LINKER_LIBS ${MKL_LIBRARIES})
Expand All @@ -154,11 +151,6 @@ if(USE_MKL_IF_AVAILABLE)
endif()
# If using MKL, use the Intel OMP libraries
list(APPEND mxnet_LINKER_LIBS iomp5)
if(USE_MKL_EXPERIMENTAL)
add_definitions(-DMKL_EXPERIMENTAL=1)
else()
add_definitions(-DMKL_EXPERIMENTAL=0)
endif()
else()
message(STATUS " MKL not found")
endif()
Expand Down
59 changes: 29 additions & 30 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
mx_lib = 'lib/libmxnet.so, lib/libmxnet.a, dmlc-core/libdmlc.a, nnvm/lib/libnnvm.a'
// mxnet cmake libraries, in cmake builds we do not produce a libnvvm static library by default.
mx_cmake_lib = 'build/libmxnet.so, build/libmxnet.a, build/dmlc-core/libdmlc.a'
mx_mkldnn_lib = 'lib/libmxnet.so, lib/libmxnet.a, lib/libiomp5.so, lib/libmklml_gnu.so, lib/libmkldnn.so, lib/libmkldnn.so.0, lib/libmklml_intel.so, dmlc-core/libdmlc.a, nnvm/lib/libnnvm.a'
// command to start a docker container
docker_run = 'tests/ci_build/ci_build.sh'
// timeout in minutes
Expand Down Expand Up @@ -143,18 +144,18 @@ def python3_gpu_ut(docker_type) {
}

// Python 2
def python2_mklml_ut(docker_type) {
def python2_mkldnn_ut(docker_type) {
timeout(time: max_time, unit: 'MINUTES') {
sh "${docker_run} ${docker_type} find . -name '*.pyc' -type f -delete"
sh "${docker_run} ${docker_type} PYTHONPATH=./python/ nosetests-2.7 --with-timer --verbose tests/python/cpu"
sh "${docker_run} ${docker_type} PYTHONPATH=./python/ MXNET_MKLDNN_DEBUG=1 nosetests-2.7 --with-timer --verbose tests/python/cpu"
}
}

// Python 3
def python3_mklml_ut(docker_type) {
def python3_mkldnn_ut(docker_type) {
timeout(time: max_time, unit: 'MINUTES') {
sh "${docker_run} ${docker_type} find . -name '*.pyc' -type f -delete"
sh "${docker_run} ${docker_type} PYTHONPATH=./python/ nosetests-3.4 --with-timer --verbose tests/python/cpu"
sh "${docker_run} ${docker_type} PYTHONPATH=./python/ MXNET_MKLDNN_DEBUG=1 nosetests-3.4 --with-timer --verbose tests/python/cpu"
}
}

Expand Down Expand Up @@ -225,21 +226,20 @@ try {
}
}
},
'CPU: MKLML': {
'CPU: MKLDNN': {
node('mxnetlinux-cpu') {
ws('workspace/build-mklml-cpu') {
ws('workspace/build-mkldnn-cpu') {
init_git()
def flag = """ \
DEV=1 \
USE_PROFILER=1 \
USE_CPP_PACKAGE=1 \
USE_BLAS=openblas \
USE_MKL2017=1 \
USE_MKL2017_EXPERIMENTAL=1 \
USE_MKLDNN=1 \
-j\$(nproc)
"""
make("cpu_mklml", flag)
pack_lib('mklml_cpu')
pack_lib('mkldnn_cpu', mx_mkldnn_lib)
}
}
},
Expand All @@ -260,24 +260,23 @@ try {
}
}
},
'GPU: MKLML': {
'GPU: MKLDNN': {
node('mxnetlinux-cpu') {
ws('workspace/build-mklml-gpu') {
ws('workspace/build-mkldnn-gpu') {
init_git()
def flag = """ \
DEV=1 \
USE_PROFILER=1 \
USE_CPP_PACKAGE=1 \
USE_BLAS=openblas \
USE_MKL2017=1 \
USE_MKL2017_EXPERIMENTAL=1 \
USE_MKLDNN=1 \
USE_CUDA=1 \
USE_CUDA_PATH=/usr/local/cuda \
USE_CUDNN=1 \
-j\$(nproc)
"""
make("build_cuda", flag)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So have you been able to use MKLDNN without the need of a separate dockerfile?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it works fine. except that using MKLDNN and GPU in Jenkins still fails. It's not a linking problem. It's most likely caused by the conflict of MKLDNN and GPU in Jenkins.

pack_lib('mklml_gpu')
pack_lib('mkldnn_gpu', mx_mkldnn_lib)
}
}
},
Expand Down Expand Up @@ -424,43 +423,43 @@ try {
}
}
},
'Python2: MKLML-CPU': {
'Python2: MKLDNN-CPU': {
node('mxnetlinux-cpu') {
ws('workspace/ut-python2-mklml-cpu') {
ws('workspace/ut-python2-mkldnn-cpu') {
init_git()
unpack_lib('mklml_cpu')
unpack_lib('mkldnn_cpu', mx_mkldnn_lib)
python2_ut('cpu_mklml')
python2_mklml_ut('cpu_mklml')
python2_mkldnn_ut('cpu_mklml')
}
}
},
'Python2: MKLML-GPU': {
'Python2: MKLDNN-GPU': {
node('mxnetlinux-gpu') {
ws('workspace/ut-python2-mklml-gpu') {
ws('workspace/ut-python2-mkldnn-gpu') {
init_git()
unpack_lib('mklml_gpu')
unpack_lib('mkldnn_gpu', mx_mkldnn_lib)
python2_gpu_ut('gpu_mklml')
python2_mklml_ut('gpu_mklml')
python2_mkldnn_ut('gpu_mklml')
}
}
},
'Python3: MKLML-CPU': {
'Python3: MKLDNN-CPU': {
node('mxnetlinux-cpu') {
ws('workspace/ut-python3-mklml-cpu') {
ws('workspace/ut-python3-mkldnn-cpu') {
init_git()
unpack_lib('mklml_cpu')
unpack_lib('mkldnn_cpu', mx_mkldnn_lib)
python3_ut('cpu_mklml')
python3_mklml_ut('cpu_mklml')
python3_mkldnn_ut('cpu_mklml')
}
}
},
'Python3: MKLML-GPU': {
'Python3: MKLDNN-GPU': {
node('mxnetlinux-gpu') {
ws('workspace/ut-python3-mklml-gpu') {
ws('workspace/ut-python3-mkldnn-gpu') {
init_git()
unpack_lib('mklml_gpu')
unpack_lib('mkldnn_gpu', mx_mkldnn_lib)
python3_gpu_ut('gpu_mklml')
python3_mklml_ut('gpu_mklml')
python3_mkldnn_ut('gpu_mklml')
}
}
},
Expand Down
40 changes: 17 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ endif
# use customized config file
include $(config)

ifeq ($(USE_MKL2017), 1)
# must run ./prepare_mkl before including mshadow.mk
RETURN_STRING := $(shell ./prepare_mkl.sh $(MKLML_ROOT))
MKLROOT := $(firstword $(RETURN_STRING))
export USE_MKLML = $(lastword $(RETURN_STRING))
ifeq ($(USE_MKLDNN), 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is cmake also adjusted for this type of build?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet. I'll update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have changed cmake to compile with MKLDNN.

RETURN_STRING := $(shell ./prepare_mkldnn.sh $(MKLDNN_ROOT))
MKLDNNROOT := $(firstword $(RETURN_STRING))
MKLROOT := $(lastword $(RETURN_STRING))
export USE_MKLML = 1
endif

include mshadow/make/mshadow.mk
Expand Down Expand Up @@ -114,23 +114,16 @@ ifeq ($(USE_NNPACK), 1)
LDFLAGS += -lnnpack
endif

ifeq ($(USE_MKL2017), 1)
CFLAGS += -DMXNET_USE_MKL2017=1
ifeq ($(USE_MKLDNN), 1)
CFLAGS += -DMXNET_USE_MKLDNN=1
CFLAGS += -DUSE_MKL=1
CFLAGS += -I$(ROOTDIR)/src/operator/mkl/
CFLAGS += -I$(MKLML_ROOT)/include
LDFLAGS += -L$(MKLML_ROOT)/lib
ifeq ($(USE_MKL2017_EXPERIMENTAL), 1)
CFLAGS += -DMKL_EXPERIMENTAL=1
else
CFLAGS += -DMKL_EXPERIMENTAL=0
endif
ifeq ($(UNAME_S), Darwin)
LDFLAGS += -lmklml
else
LDFLAGS += -Wl,--as-needed -lmklml_intel -lmklml_gnu
CFLAGS += -I$(ROOTDIR)/src/operator/nn/mkldnn/
ifneq ($(MKLDNNROOT), $(MKLROOT))
CFLAGS += -I$(MKLROOT)/include
LDFLAGS += -L$(MKLROOT)/lib
endif
LDFLAGS += -liomp5
CFLAGS += -I$(MKLDNNROOT)/include
LDFLAGS += -L$(MKLDNNROOT)/lib -lmkldnn -Wl,-rpath,'$${ORIGIN}'
endif

ifeq ($(USE_OPERATOR_TUNING), 1)
Expand All @@ -144,7 +137,7 @@ endif
# - for Ubuntu, installing atlas will not automatically install the atlas provided lapack library
# silently switching lapack off instead of letting the build fail because of backward compatibility
ifeq ($(USE_LAPACK), 1)
ifeq ($(USE_BLAS),$(filter $(USE_BLAS),blas openblas atlas))
ifeq ($(USE_BLAS),$(filter $(USE_BLAS),blas openblas atlas mkl))
ifeq (,$(wildcard /lib/liblapack.a))
ifeq (,$(wildcard /usr/lib/liblapack.a))
ifeq (,$(wildcard /usr/lib64/liblapack.a))
Expand All @@ -162,7 +155,7 @@ ifeq ($(USE_LAPACK), 1)
ifneq ($(USE_LAPACK_PATH), )
LDFLAGS += -L$(USE_LAPACK_PATH)
endif
ifeq ($(USE_BLAS),$(filter $(USE_BLAS),blas openblas atlas))
ifeq ($(USE_BLAS),$(filter $(USE_BLAS),blas openblas atlas mkl))
LDFLAGS += -llapack
endif
CFLAGS += -DMXNET_USE_LAPACK
Expand Down Expand Up @@ -552,7 +545,8 @@ clean: cyclean $(EXTRA_PACKAGES_CLEAN)
else
clean: cyclean testclean $(EXTRA_PACKAGES_CLEAN)
$(RM) -r build lib bin *~ */*~ */*/*~ */*/*/*~ R-package/NAMESPACE R-package/man R-package/R/mxnet_generated.R \
R-package/inst R-package/src/image_recordio.h R-package/src/*.o R-package/src/*.so mxnet_*.tar.gz
R-package/inst R-package/src/image_recordio.h R-package/src/*.o R-package/src/*.so mxnet_*.tar.gz \
external/mkldnn/install/*
cd $(DMLC_CORE); $(MAKE) clean; cd -
cd $(PS_PATH); $(MAKE) clean; cd -
cd $(NNVM_PATH); $(MAKE) clean; cd -
Expand Down
2 changes: 1 addition & 1 deletion amalgamation/mxnet_predict0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
#include "src/operator/operator_util.cc"
#include "src/operator/nn/activation.cc"
#include "src/operator/nn/batch_norm.cc"
#include "src/operator/concat.cc"
#include "src/operator/nn/concat.cc"
#include "src/operator/nn/convolution.cc"
#include "src/operator/nn/deconvolution.cc"
#include "src/operator/nn/dropout.cc"
Expand Down
4 changes: 2 additions & 2 deletions cmake/ChooseBlas.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ if(USE_MKL_IF_AVAILABLE)
find_package(MKL)
endif()
if(MKL_FOUND)
if(USE_MKLML_MKL)
if(USE_MKLDNN)
set(BLAS "open")
else()
set(BLAS "MKL")
Expand Down Expand Up @@ -55,4 +55,4 @@ elseif(BLAS STREQUAL "apple")
list(APPEND mshadow_LINKER_LIBS ${Accelerate_LIBRARIES})
add_definitions(-DMSHADOW_USE_MKL=0)
add_definitions(-DMSHADOW_USE_CBLAS=1)
endif()
endif()
18 changes: 9 additions & 9 deletions cmake/Modules/FindMKL.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#
# Options:
#
# USE_MKLML_MKL : Search for MKL:ML library variant
# USE_MKLDNN : Search for MKL:ML library variant
#
# MKL_USE_SINGLE_DYNAMIC_LIBRARY : use single dynamic library interface
# MKL_USE_STATIC_LIBS : use static libraries
Expand All @@ -33,7 +33,7 @@
# MKL_INCLUDE_DIR : unclude directory
# MKL_LIBRARIES : the libraries to link against.
#
# cjolivier01: Changed to also look for MKLML library (subset of mkl) instead of standard MKL package
# cjolivier01: Changed to also look for MKLDNN library (subset of mkl) instead of standard MKL package
#

if(MKL_FOUND)
Expand All @@ -43,7 +43,7 @@ endif()
# ---[ Root folders
set(INTEL_ROOT "/opt/intel" CACHE PATH "Folder contains intel libs")

if(USE_MKLML_MKL)
if(USE_MKLDNN)

find_path(MKL_ROOT include/mkl_blas.h
PATHS $ENV{MKL_ROOT}
Expand All @@ -66,13 +66,14 @@ if(USE_MKLML_MKL)
set(__mkl_libs "")

if(WIN32)
list(APPEND __mkl_libs intel)
list(APPEND __mkl_libs mklml_intel)
else()
list(APPEND __mkl_libs gnu)
list(APPEND __mkl_libs mklml_gnu)
endif()
list(APPEND __mkl_libs mkldnn)

foreach (__lib ${__mkl_libs})
set(__mkl_lib "mklml_${__lib}")
set(__mkl_lib "${__lib}")
string(TOUPPER ${__mkl_lib} __mkl_lib_upper)

if(MKL_USE_STATIC_LIBS)
Expand All @@ -90,8 +91,7 @@ if(USE_MKLML_MKL)
list(APPEND MKL_LIBRARIES ${${__mkl_lib_upper}_LIBRARY})
endforeach()


else(USE_MKLML_MKL)
else(USE_MKLDNN)

# ---[ Options
mxnet_option(MKL_USE_SINGLE_DYNAMIC_LIBRARY "Use single dynamic library interface" ON)
Expand Down Expand Up @@ -193,7 +193,7 @@ else(USE_MKLML_MKL)
list(APPEND MKL_LIBRARIES ${MKL_RTL_LIBRARY})
endif()

endif(USE_MKLML_MKL)
endif(USE_MKLDNN)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(MKL DEFAULT_MSG ${__looked_for})
Expand Down
3 changes: 2 additions & 1 deletion example/image-classification/common/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def get_rec_iter(args, kv=None):
image_shape = tuple([int(l) for l in args.image_shape.split(',')])
if 'benchmark' in args and args.benchmark:
data_shape = (args.batch_size,) + image_shape
train = SyntheticDataIter(args.num_classes, data_shape, 500, np.float32)
train = SyntheticDataIter(args.num_classes, data_shape,
args.num_examples / args.batch_size, np.float32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change related to this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. It seems to me this is a bug, so I fixed it.

return (train, None)
if kv:
(rank, nworker) = (kv.rank, kv.num_workers)
Expand Down
Loading