Skip to content

Commit

Permalink
[MXNET-343]fix Mkldnn with msvc (apache#10629)
Browse files Browse the repository at this point in the history
* fix mkldnn with msvc
add linux

* fix sum name

* jk ip

* add test

* fixed missing depend

* fix tools url
use arc to all
remove redundant test

* updata mkldnn to 0.14

* change mkldnn to v0.14

* close "-mtune=generic" with msvc, turn off test example

* up mkldnn

* change mkldnn to good v0.14 tag

* fix mklml download

* fix  MKLDNN_UTIL_FUNC.MemFormat
  • Loading branch information
yajiedesign authored and Jin Huang committed May 29, 2018
1 parent 96f3d98 commit e3d5e62
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 42 deletions.
2 changes: 1 addition & 1 deletion 3rdparty/mkldnn
2 changes: 1 addition & 1 deletion 3rdparty/mshadow
44 changes: 15 additions & 29 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ mxnet_option(USE_SSE "Build with x86 SSE instruction support" ON)
mxnet_option(USE_F16C "Build with x86 F16C instruction support" ON) # autodetects support if ON
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 MKLDNN variant of MKL (if MKL found)" ON IF USE_MKL_IF_AVAILABLE AND UNIX AND (NOT APPLE))
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 (NOT APPLE))
mxnet_option(USE_MKLDNN "Use MKLDNN variant of MKL (if MKL found)" ON IF USE_MKL_IF_AVAILABLE AND (NOT APPLE))
mxnet_option(USE_OPERATOR_TUNING "Enable auto-tuning of operators" ON IF 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 @@ -87,7 +87,6 @@ if(MSVC)
add_definitions(-DNNVM_EXPORTS)
add_definitions(-DDMLC_STRICT_CXX11)
add_definitions(-DNOMINMAX)
set(SUPPORT_F16C FALSE)
if(USE_F16C)
message("F16C instruction set is not yet supported for MSVC")
endif()
Expand Down Expand Up @@ -185,34 +184,21 @@ if(USE_VTUNE)
list(APPEND mxnet_LINKER_LIBS dl)
endif()

if(USE_MKL_IF_AVAILABLE)
if(USE_MKLDNN)
# We need to use generic archtecture. Otherwise, MKLDNN compiled in one
# CPU architecture (e.g., C5) can't run on another architecture (e.g., g3).
if(USE_MKLDNN)
include(cmake/MklDnn.cmake)
# CPU architecture (e.g., C5) can't run on another architecture (e.g., g3).
if(NOT MSVC)
set(ARCH_OPT_FLAGS "-mtune=generic")
add_subdirectory(3rdparty/mkldnn)
include_directories(3rdparty/mkldnn/include)
add_definitions(-DMXNET_USE_MKLDNN=1)
list(APPEND mxnet_LINKER_LIBS mkldnn)
endif()
find_package(MKL)

if(MKL_FOUND)
include_directories(${MKL_INCLUDE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/operator/mkl)

add_definitions(-DUSE_MKL=1)
add_definitions(-DCUB_MKL=1)
list(APPEND mxnet_LINKER_LIBS ${MKL_LIBRARIES})

if(NOT MSVC)
list(APPEND mxnet_LINKER_LIBS dl)
endif()
# If using MKL, use the Intel OMP libraries
list(APPEND mxnet_LINKER_LIBS iomp5)
else()
message(STATUS " MKL not found")
endif()
set(WITH_TEST OFF)
set(WITH_EXAMPLE OFF)
add_subdirectory(3rdparty/mkldnn)

include_directories(3rdparty/mkldnn/include)
add_definitions(-DUSE_MKL=1)
add_definitions(-DCUB_MKL=1)
add_definitions(-DMXNET_USE_MKLDNN=1)
list(APPEND mxnet_LINKER_LIBS mkldnn)
endif()

# Allow Cuda compiles outside of src tree to find things in 'src' and 'include'
Expand Down
63 changes: 61 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ try {
bat """mkdir build_vc14_cpu
call "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\x86_amd64\\vcvarsx86_amd64.bat"
cd build_vc14_cpu
cmake -G \"Visual Studio 14 2015 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 ${env.WORKSPACE}"""
cmake -G \"Visual Studio 14 2015 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 -DUSE_MKL_IF_AVAILABLE=0 ${env.WORKSPACE}"""
bat 'C:\\mxnet\\build_vc14_cpu.bat'

bat '''rmdir /s/q pkg_vc14_cpu
Expand Down Expand Up @@ -347,7 +347,7 @@ try {
bat """mkdir build_vc14_gpu
call "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\x86_amd64\\vcvarsx86_amd64.bat"
cd build_vc14_gpu
cmake -G \"NMake Makefiles JOM\" -DUSE_CUDA=1 -DUSE_CUDNN=1 -DUSE_NVRTC=1 -DUSE_OPENCV=1 -DUSE_OPENMP=1 -DUSE_PROFILER=1 -DUSE_BLAS=open -DUSE_LAPACK=1 -DUSE_DIST_KVSTORE=0 -DCUDA_ARCH_NAME=All -DCMAKE_CXX_FLAGS_RELEASE="/FS /MD /O2 /Ob2 /DNDEBUG" -DCMAKE_BUILD_TYPE=Release ${env.WORKSPACE}"""
cmake -G \"NMake Makefiles JOM\" -DUSE_CUDA=1 -DUSE_CUDNN=1 -DUSE_NVRTC=1 -DUSE_OPENCV=1 -DUSE_OPENMP=1 -DUSE_PROFILER=1 -DUSE_BLAS=open -DUSE_LAPACK=1 -DUSE_DIST_KVSTORE=0 -DCUDA_ARCH_NAME=All -DCMAKE_CXX_FLAGS_RELEASE="/FS /MD /O2 /Ob2 /DNDEBUG" -DCMAKE_BUILD_TYPE=Release -DUSE_MKL_IF_AVAILABLE=0 ${env.WORKSPACE}"""
bat 'C:\\mxnet\\build_vc14_gpu.bat'
bat '''rmdir /s/q pkg_vc14_gpu
mkdir pkg_vc14_gpu\\lib
Expand All @@ -370,6 +370,47 @@ try {
}
}
},
'Build GPU MKLDNN windows':{
node('mxnetwindows-cpu') {
timeout(time: max_time, unit: 'MINUTES') {
ws('workspace/build-gpu') {
withEnv(['OpenBLAS_HOME=C:\\mxnet\\openblas', 'OpenCV_DIR=C:\\mxnet\\opencv_vc14', 'CUDA_PATH=C:\\CUDA\\v8.0','BUILD_NAME=vc14_gpu_mkldnn']) {
init_git_win()
bat """mkdir build_%BUILD_NAME%
call "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\x86_amd64\\vcvarsx86_amd64.bat"
cd build_%BUILD_NAME%
copy ${env.WORKSPACE}\\3rdparty\\mkldnn\\config_template.vcxproj.user ${env.WORKSPACE}\\config_template.vcxproj.user /y
cmake -G \"NMake Makefiles JOM\" -DUSE_CUDA=1 -DUSE_CUDNN=1 -DUSE_NVRTC=1 -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_CXX_FLAGS_RELEASE="/FS /MD /O2 /Ob2 /DNDEBUG" -DCMAKE_BUILD_TYPE=Release ${env.WORKSPACE}"""
bat '''
call "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\x86_amd64\\vcvarsx86_amd64.bat"
cd build_%BUILD_NAME%
set /a cores=%NUMBER_OF_PROCESSORS% * 2
jom -j %cores%
'''
bat '''rmdir /s/q pkg_%BUILD_NAME%
mkdir pkg_%BUILD_NAME%\\lib
mkdir pkg_%BUILD_NAME%\\python
mkdir pkg_%BUILD_NAME%\\include
mkdir pkg_%BUILD_NAME%\\build
copy build_%BUILD_NAME%\\libmxnet.lib pkg_%BUILD_NAME%\\lib
copy build_%BUILD_NAME%\\libmxnet.dll pkg_%BUILD_NAME%\\build
copy build_%BUILD_NAME%\\3rdparty\\mkldnn\\src\\mkldnn.dll pkg_%BUILD_NAME%\\build
copy build_%BUILD_NAME%\\libiomp5md.dll pkg_%BUILD_NAME%\\build
copy build_%BUILD_NAME%\\mklml.dll pkg_%BUILD_NAME%\\build
xcopy python pkg_%BUILD_NAME%\\python /E /I /Y
xcopy include pkg_%BUILD_NAME%\\include /E /I /Y
xcopy 3rdparty\\dmlc-core\\include pkg_%BUILD_NAME%\\include /E /I /Y
xcopy 3rdparty\\mshadow\\mshadow pkg_%BUILD_NAME%\\include\\mshadow /E /I /Y
xcopy 3rdparty\\nnvm\\include pkg_%BUILD_NAME%\\nnvm\\include /E /I /Y
del /Q *.7z
7z.exe a %BUILD_NAME%.7z pkg_%BUILD_NAME%\\
'''
stash includes: 'vc14_gpu_mkldnn.7z', name: 'vc14_gpu_mkldnn'
}
}
}
}
},
'NVidia Jetson / ARMv8':{
node('mxnetlinux-cpu') {
ws('workspace/build-jetson-armv8') {
Expand Down Expand Up @@ -679,6 +720,24 @@ try {
}
}
}
},
'Python 3: MKLDNN-GPU Win':{
node('mxnetwindows-gpu') {
timeout(time: max_time, unit: 'MINUTES') {
ws('workspace/ut-python-gpu') {
init_git_win()
unstash 'vc14_gpu_mkldnn'
bat '''rmdir /s/q pkg_vc14_gpu_mkldnn
7z x -y vc14_gpu_mkldnn.7z'''
bat """xcopy C:\\mxnet\\data data /E /I /Y
xcopy C:\\mxnet\\model model /E /I /Y
call activate py3
set PYTHONPATH=${env.WORKSPACE}\\pkg_vc14_gpu_mkldnn\\python
del /S /Q ${env.WORKSPACE}\\pkg_vc14_gpu_mkldnn\\python\\*.pyc
C:\\mxnet\\test_gpu.bat"""
}
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmake/FirstClassLangCuda.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ endif ()
function(mshadow_select_nvcc_arch_flags out_variable)

set(CUDA_ARCH_LIST "Auto" CACHE STRING "Select target NVIDIA GPU achitecture.")
set_property( CACHE CUDA_ARCH_LIST PROPERTY STRINGS "" "All" "Common" ${CUDA_KNOWN_GPU_ARCHITECTURES} )
set_property( CACHE CUDA_ARCH_LIST PROPERTY STRINGS "" "Auto" "All" "Common" ${CUDA_KNOWN_GPU_ARCHITECTURES} )
mark_as_advanced(CUDA_ARCH_NAME)


Expand Down
44 changes: 44 additions & 0 deletions cmake/MklDnn.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

#this file download mklml

message(STATUS "download mklml")
if(MSVC)
set(MKL_NAME "mklml_win_2018.0.3.20180406")
file(DOWNLOAD "https://github.com/intel/mkl-dnn/releases/download/v0.14/${MKL_NAME}.zip" "${CMAKE_CURRENT_BINARY_DIR}/mklml/${MKL_NAME}.zip" EXPECTED_MD5 "8DD73E7D3F19F004551809824C4E8970" SHOW_PROGRESS)
file(DOWNLOAD "https://github.com/apache/incubator-mxnet/releases/download/utils/7z.exe" "${CMAKE_CURRENT_BINARY_DIR}/mklml/7z2.exe" EXPECTED_MD5 "E1CF766CF358F368EC97662D06EA5A4C" SHOW_PROGRESS)

execute_process(COMMAND "${CMAKE_CURRENT_BINARY_DIR}/mklml/7z2.exe" "-o${CMAKE_CURRENT_BINARY_DIR}/mklml/" "-y")
execute_process(COMMAND "${CMAKE_CURRENT_BINARY_DIR}/mklml/7z.exe" "x" "${CMAKE_CURRENT_BINARY_DIR}/mklml/${MKL_NAME}.zip" "-o${CMAKE_CURRENT_BINARY_DIR}/mklml/" "-y")
set(MKLROOT "${CMAKE_CURRENT_BINARY_DIR}/mklml/${MKL_NAME}")
include_directories(${MKLROOT}/include)
file(COPY ${MKLROOT}/lib/libiomp5md.dll DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ${MKLROOT}/lib/mklml.dll DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ${CMAKE_SOURCE_DIR}/3rdparty/mkldnn/config_template.vcxproj.user DESTINATION ${CMAKE_SOURCE_DIR})
elseif(UNIX)
set(MKL_NAME "mklml_lnx_2018.0.3.20180406")
file(DOWNLOAD "https://github.com/intel/mkl-dnn/releases/download/v0.14/${MKL_NAME}.tgz" "${CMAKE_CURRENT_BINARY_DIR}/mklml/${MKL_NAME}.tgz" EXPECTED_MD5 "DAF7EFC3C1C0036B447213004467A8AE" SHOW_PROGRESS)
execute_process(COMMAND "tar" "-xzf" "${CMAKE_CURRENT_BINARY_DIR}/mklml/${MKL_NAME}.tgz" "-C" "${CMAKE_CURRENT_BINARY_DIR}/mklml/")
set(MKLROOT "${CMAKE_CURRENT_BINARY_DIR}/mklml/${MKL_NAME}")
include_directories(${MKLROOT}/include)
file(COPY ${MKLROOT}/lib/libiomp5.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ${MKLROOT}/lib/libmklml_gnu.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ${MKLROOT}/lib/libmklml_intel.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
else()
message(FATAL_ERROR "not support now")
endif()
7 changes: 6 additions & 1 deletion python/mxnet/libinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import platform
import logging


def find_lib_path():
"""Find MXNet dynamic library files.
Expand All @@ -37,6 +38,8 @@ def find_lib_path():
logging.warning("MXNET_LIBRARY_PATH should be an absolute path, instead of: %s",
lib_from_env)
else:
if os.name == 'nt':
os.environ['PATH'] = os.environ['PATH'] + ';' + os.path.dirname(lib_from_env)
return [lib_from_env]
else:
logging.warning("MXNET_LIBRARY_PATH '%s' doesn't exist", lib_from_env)
Expand All @@ -60,7 +63,7 @@ def find_lib_path():
os.environ['PATH'] = os.path.dirname(__file__) + ';' + os.environ['PATH']
dll_path = [os.path.join(p, 'libmxnet.dll') for p in dll_path]
elif platform.system() == 'Darwin':
dll_path = [os.path.join(p, 'libmxnet.dylib') for p in dll_path]+ \
dll_path = [os.path.join(p, 'libmxnet.dylib') for p in dll_path] + \
[os.path.join(p, 'libmxnet.so') for p in dll_path]
else:
dll_path.append('../../../')
Expand All @@ -69,6 +72,8 @@ def find_lib_path():
if len(lib_path) == 0:
raise RuntimeError('Cannot find the MXNet library.\n' +
'List of candidates:\n' + str('\n'.join(dll_path)))
if os.name == 'nt':
os.environ['PATH'] = os.environ['PATH'] + ';' + os.path.dirname(lib_path[0])
return lib_path


Expand Down
8 changes: 7 additions & 1 deletion src/operator/nn/mkldnn/mkldnn_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ void CommitOutput(const NDArray &arr, const mkldnn_output_t &res) {
// We have to allocate new memory for the sum result.
auto sum_res = TmpMemMgr::Get()->Alloc(
res.second->get_primitive_desc());
op::Sum(*res.second, *mem, *sum_res);
op::MKLDNNSum(*res.second, *mem, *sum_res);
const_cast<NDArray &>(arr).CopyFrom(*sum_res);
}
}
Expand Down Expand Up @@ -219,6 +219,7 @@ mkldnn_memory_format_t GetDefaultFormat(const mkldnn::memory::desc &desc) {
case mkldnn_hwio:
case mkldnn_OIhw8i8o:
case mkldnn_OIhw16i16o:
case mkldnn_OIhw4i16o4i:
case mkldnn_OIhw8i16o2i:
case mkldnn_OIhw8o16i2o:
case mkldnn_OIhw8o8i:
Expand All @@ -239,6 +240,7 @@ mkldnn_memory_format_t GetDefaultFormat(const mkldnn::memory::desc &desc) {
case mkldnn_goihw:
case mkldnn_gOIhw8i8o:
case mkldnn_gOIhw16i16o:
case mkldnn_gOIhw4i16o4i:
case mkldnn_gOIhw8i16o2i:
case mkldnn_gOIhw8o16i2o:
case mkldnn_gOIhw8o8i:
Expand Down Expand Up @@ -349,7 +351,11 @@ static bool SimilarArray(const mxnet::NDArray &arr1, const mxnet::NDArray &arr2,
arr2.IsMKLDNNData() ? buf2.data().dptr_: arr2.data().dptr_);
std::atomic<bool> success(true);
#pragma omp parallel for
#ifdef _MSC_VER
for (int64_t i = 0; i < arr1.shape().Size(); i++) {
#else
for (size_t i = 0; i < arr1.shape().Size(); i++) {
#endif
if (std::abs(data1[i] - data2[i]) > atol + rtol * std::abs(data2[i]))
success.store(false);
}
Expand Down
2 changes: 1 addition & 1 deletion src/operator/nn/mkldnn/mkldnn_copy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void MKLDNNCopy(const nnvm::NodeAttrs& attrs, const OpContext &ctx,
if (out_mem == nullptr)
out_mem = out_data.GetMKLDNNData();
auto sum_res = TmpMemMgr::Get()->Alloc(out_mem->get_primitive_desc());
Sum(*in_mem, *out_mem, *sum_res);
MKLDNNSum(*in_mem, *out_mem, *sum_res);
const_cast<NDArray &>(out_data).CopyFrom(*sum_res);
} else {
const_cast<NDArray &>(out_data).CopyFrom(*in_mem);
Expand Down
2 changes: 1 addition & 1 deletion src/operator/nn/mkldnn/mkldnn_ops-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void MKLDNNActivationBackward(const nnvm::NodeAttrs& attrs, const OpContext &ctx
const NDArray &out_grad, const NDArray &in_data,
const OpReqType &req, const NDArray &in_grad);

void Sum(const mkldnn::memory &arr1, const mkldnn::memory &arr2,
void MKLDNNSum(const mkldnn::memory &arr1, const mkldnn::memory &arr2,
const mkldnn::memory &out);

} // namespace op
Expand Down
2 changes: 1 addition & 1 deletion src/operator/nn/mkldnn/mkldnn_sum.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
namespace mxnet {
namespace op {

void Sum(const mkldnn::memory &arr1, const mkldnn::memory &arr2,
void MKLDNNSum(const mkldnn::memory &arr1, const mkldnn::memory &arr2,
const mkldnn::memory &out) {
std::vector<mkldnn::memory::primitive_desc> input_pds(2);
std::vector<float> scales(2, 1);
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ if(GTEST_FOUND AND NOT MSVC)

add_test(AllTestsIn${PROJECT_NAME}UnitTests ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${PROJECT_NAME}_unit_tests)
else()
message(WARNING "Google Test not found")
message(STATUS "Google Test not found")
endif()

4 changes: 2 additions & 2 deletions tests/cpp/operator/mkldnn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ TEST(MKLDNN_UTIL_FUNC, AlignMem) {

TEST(MKLDNN_UTIL_FUNC, MemFormat) {
// Check whether the number of format is correct.
CHECK_EQ(mkldnn_format_last, 56);
CHECK_EQ(mkldnn_format_last, 67);
CHECK_EQ(mkldnn_nchw, 5);
CHECK_EQ(mkldnn_oihw, 12);
CHECK_EQ(mkldnn_oihw, 15);
}

// Init arrays with the default layout.
Expand Down

0 comments on commit e3d5e62

Please sign in to comment.