From 89a4158038028c1a278ddec791e15bcff8307460 Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Tue, 18 Jul 2017 16:15:36 +0800 Subject: [PATCH 01/12] enable MKLDNN library and MKL small package --- CMakeLists.txt | 7 +++ cmake/cblas.cmake | 40 +++++++++++---- cmake/configure.cmake | 6 +++ cmake/external/mkldnn.cmake | 78 +++++++++++++++++++++++++++++ paddle/math/MathFunctions.cpp | 93 ++++++++++++++++++----------------- paddle/math/MathFunctions.h | 6 +++ 6 files changed, 176 insertions(+), 54 deletions(-) create mode 100644 cmake/external/mkldnn.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index dcff6b54cafce..5e664d1415399 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,6 +37,7 @@ include(simd) ################################ Configurations ####################################### option(WITH_GPU "Compile PaddlePaddle with NVIDIA GPU" ${CUDA_FOUND}) option(WITH_AVX "Compile PaddlePaddle with AVX intrinsics" ${AVX_FOUND}) +option(WITH_MKLDNN "Compile PaddlePaddle with mkl-dnn support." ON) option(WITH_DSO "Compile PaddlePaddle with dynamic linked CUDA" ON) option(WITH_TESTING "Compile PaddlePaddle with unit testing" ON) option(WITH_SWIG_PY "Compile PaddlePaddle with inference api" ON) @@ -94,6 +95,7 @@ include(external/glog) # download, build, install glog include(external/gtest) # download, build, install gtest include(external/protobuf) # download, build, install protobuf include(external/python) # download, build, install python +include(external/mkldnn) # download, build, install mkldnn include(external/openblas) # download, build, install openblas include(external/swig) # download, build, install swig include(external/warpctc) # download, build, install warpctc @@ -136,6 +138,11 @@ if(WITH_GPU) endif(NOT WITH_DSO) endif(WITH_GPU) +if(WITH_MKLDNN) + message(STATUS "MKLDNN_LIBRARY: ${MKLDNN_LIBRARY}") + list(APPEND EXTERNAL_LIBS ${MKLDNN_LIBRARY} ${MKL_LITE_LIB_IOMP}) +endif() + if(USE_NNPACK) include(external/nnpack) list(APPEND EXTERNAL_LIBS ${NNPACK_LIBS}) diff --git a/cmake/cblas.cmake b/cmake/cblas.cmake index 913f711afff3b..ee654e64bd0d5 100644 --- a/cmake/cblas.cmake +++ b/cmake/cblas.cmake @@ -16,22 +16,42 @@ set(CBLAS_FOUND OFF) ## Find MKL First. -set(INTEL_ROOT "/opt/intel" CACHE PATH "Folder contains intel libs") -set(MKL_ROOT ${INTEL_ROOT}/mkl CACHE PATH "Folder contains MKL") +set(INTEL_MKL_ROOT "/opt/intel/mkl" CACHE PATH "Folder contains intel mkl libs") +set(MKL_ROOT $ENV{MKL_ROOT} CACHE PATH "Folder contains env MKL") + +set(MKL_INCLUDE_SEARCH_PATHS + ${MKL_ROOT}/include + ${INTEL_MKL_ROOT}/include) +set(MKL_LIB_SEARCH_PATHS + ${MKL_ROOT}/lib + ${MKL_ROOT}/lib/intel64 + ${INTEL_MKL_ROOT}/lib + ${INTEL_MKL_ROOT}/lib/intel64) + +if(MKL_LITE_INC_DIR AND MKL_LITE_LIB) + set(CBLAS_FOUND ON) + set(CBLAS_PROVIDER MKL_LITE) + set(CBLAS_INC_DIR ${MKL_LITE_INC_DIR}) + set(CBLAS_LIBRARIES ${MKL_LITE_LIB}) + + add_definitions(-DPADDLE_USE_MKL_LITE) + add_definitions(-DLAPACK_FOUND) + + message(STATUS "Found cblas and lapack in MKL Lite " + "(include: ${MKL_LITE_INC_DIR}, library: ${CBLAS_LIBRARIES})") + return() +endif() find_path(MKL_INC_DIR mkl.h PATHS - ${MKL_ROOT}/include) + ${MKL_INCLUDE_SEARCH_PATHS}) find_path(MKL_LAPACK_INC_DIR mkl_lapacke.h PATHS - ${MKL_ROOT}/include) + ${MKL_INCLUDE_SEARCH_PATHS}) find_library(MKL_CORE_LIB NAMES mkl_core PATHS - ${MKL_ROOT}/lib - ${MKL_ROOT}/lib/intel64) + ${MKL_LIB_SEARCH_PATHS}) find_library(MKL_SEQUENTIAL_LIB NAMES mkl_sequential PATHS - ${MKL_ROOT}/lib - ${MKL_ROOT}/lib/intel64) + ${MKL_LIB_SEARCH_PATHS}) find_library(MKL_INTEL_LP64 NAMES mkl_intel_lp64 PATHS - ${MKL_ROOT}/lib - ${MKL_ROOT}/lib/intel64) + ${MKL_LIB_SEARCH_PATHS}) if(MKL_LAPACK_INC_DIR AND MKL_INC_DIR AND MKL_CORE_LIB AND MKL_SEQUENTIAL_LIB AND MKL_INTEL_LP64) set(CBLAS_FOUND ON) diff --git a/cmake/configure.cmake b/cmake/configure.cmake index 7afab5d5344b7..87191976821dd 100644 --- a/cmake/configure.cmake +++ b/cmake/configure.cmake @@ -67,6 +67,12 @@ else() include_directories(${CUDA_TOOLKIT_INCLUDE}) endif(NOT WITH_GPU) +if(WITH_MKLDNN) + add_definitions(-DPADDLE_USE_MKLDNN) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") +endif(WITH_MKLDNN) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SIMD_FLAG}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SIMD_FLAG}") diff --git a/cmake/external/mkldnn.cmake b/cmake/external/mkldnn.cmake new file mode 100644 index 0000000000000..834f5ae230c9b --- /dev/null +++ b/cmake/external/mkldnn.cmake @@ -0,0 +1,78 @@ +# Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve. +# +# Licensed 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. + +IF(NOT ${WITH_MKLDNN}) + return() +ENDIF(NOT ${WITH_MKLDNN}) + +INCLUDE(ExternalProject) + +SET(MKLDNN_PROJECT "extern_mkldnn") +SET(MKLDNN_SOURCES_DIR ${THIRD_PARTY_PATH}/mkldnn) +SET(MKLDNN_INSTALL_DIR ${THIRD_PARTY_PATH}/install/mkldnn) +SET(MKLDNN_INCLUDE_DIR "${MKLDNN_INSTALL_DIR}/include" CACHE PATH "mkldnn include directory." FORCE) + +# The following magic numbers should be updated regularly to keep latest version +SET(MKLDNN_TAG "v0.9") +SET(MKLDNN_MKL_VER "mklml_lnx_2018.0.20170425") + +IF(WIN32) + MESSAGE(WARNING "It is not supported compiling with mkldnn in windows Paddle yet." + "Force WITH_MKLDNN=OFF") + SET(WITH_MKLDNN OFF) + return() +ELSE(WIN32) + SET(MKLDNN_LIBRARY "${MKLDNN_INSTALL_DIR}/lib/libmkldnn.so" CACHE FILEPATH "mkldnn library." FORCE) + MESSAGE(STATUS "Set ${MKLDNN_INSTALL_DIR}/lib to runtime path") + SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) + #SET(CMAKE_MACOSX_RPATH 1) # hold for MacOS + SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${MKLDNN_INSTALL_DIR}/lib") +ENDIF(WIN32) + +INCLUDE_DIRECTORIES(${MKLDNN_INCLUDE_DIR}) + +SET(MKLDNN_CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") +SET(MKLDNN_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") + +ExternalProject_Add( + ${MKLDNN_PROJECT} + ${EXTERNAL_PROJECT_LOG_ARGS} + GIT_REPOSITORY "https://github.com/01org/mkl-dnn.git" + GIT_TAG "${MKLDNN_TAG}" + PREFIX ${MKLDNN_SOURCES_DIR} + PATCH_COMMAND cd /scripts && ./prepare_mkl.sh + UPDATE_COMMAND "" + CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + CMAKE_ARGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + CMAKE_ARGS -DCMAKE_CXX_FLAGS=${MKLDNN_CMAKE_CXX_FLAGS} + CMAKE_ARGS -DCMAKE_C_FLAGS=${MKLDNN_CMAKE_C_FLAGS} + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${MKLDNN_INSTALL_DIR} + CMAKE_ARGS -DCMAKE_INSTALL_LIBDIR=${MKLDNN_INSTALL_DIR}/lib + CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release + CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${MKLDNN_INSTALL_DIR} + -DCMAKE_INSTALL_LIBDIR:PATH=${MKLDNN_INSTALL_DIR}/lib + -DCMAKE_BUILD_TYPE:STRING=Release +) + +SET(MKL_LITE_DIR ${MKLDNN_SOURCES_DIR}/src/${MKLDNN_PROJECT}/external/${MKLDNN_MKL_VER}) +SET(MKL_LITE_INC_DIR ${MKL_LITE_DIR}/include) +SET(MKL_LITE_LIB ${MKL_LITE_DIR}/lib/libmklml_intel.so) +SET(MKL_LITE_LIB_IOMP ${MKL_LITE_DIR}/lib/libiomp5.so) +SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${MKL_LITE_DIR}/lib") + +ADD_LIBRARY(mkldnn STATIC IMPORTED GLOBAL) +SET_PROPERTY(TARGET mkldnn PROPERTY IMPORTED_LOCATION ${MKLDNN_LIBRARY}) +ADD_DEPENDENCIES(mkldnn ${MKLDNN_PROJECT}) + +LIST(APPEND external_project_dependencies mkldnn) diff --git a/paddle/math/MathFunctions.cpp b/paddle/math/MathFunctions.cpp index 7045562dd44f8..999b72cc1597b 100644 --- a/paddle/math/MathFunctions.cpp +++ b/paddle/math/MathFunctions.cpp @@ -202,7 +202,7 @@ double dotProduct(const int n, const double* x, const double* y) { return cblas_ddot(n, x, 1, y, 1); } -#ifdef PADDLE_USE_MKL +#if defined(PADDLE_USE_MKL) || defined(PADDLE_USE_MKL_LITE) template <> void vExp(const int n, const float* a, float* r) { @@ -243,7 +243,55 @@ template <> void vAdd(const int n, const double* a, const double* b, double* r) { vdAdd(n, a, b, r); } +#else + +DEFINE_MATRIX_BINARY_OP(vExp, b = std::exp(a)); +template +void vExp(const int n, const T* a, T* r) { + hl_cpu_apply_binary_op, 0, 0>( + binary::vExp(), const_cast(a), r, 1, n, n, n); +} + +DEFINE_MATRIX_BINARY_OP(vLog, b = std::log(a)); +template +void vLog(const int n, const T* a, T* r) { + hl_cpu_apply_binary_op, 0, 0>( + binary::vLog(), const_cast(a), r, 1, n, n, n); +} + +DEFINE_MATRIX_BINARY_PARAMETER_OP(vPow, ONE_PARAMETER, b = std::pow(a, p)); +template +void vPow(const int n, const T* a, const T b, T* r) { + hl_cpu_apply_binary_op, 0, 0>( + binary::vPow(b), const_cast(a), r, 1, n, n, n); +} + +DEFINE_MATRIX_TERNARY_OP(vAdd, c = a + b); +template +void vAdd(const int n, const T* a, const T* b, T* r) { + hl_cpu_apply_ternary_op, 0, 0>(ternary::vAdd(), + const_cast(a), + const_cast(b), + r, + 1, + n, + n, + n, + n); +} + +template void vExp(const int n, const float* a, float* r); +template void vExp(const int n, const double* a, double* r); +template void vLog(const int n, const float* a, float* r); +template void vLog(const int n, const double* a, double* r); +template void vPow(const int n, const float* a, const float b, float* r); +template void vPow(const int n, const double* a, const double b, double* r); +template void vAdd(const int n, const float* a, const float* b, float* r); +template void vAdd(const int n, const double* a, const double* b, double* r); +#endif + +#ifdef PADDLE_USE_MKL template <> void vInvSqrt(const int n, const float* a, float* r) { vsInvSqrt(n, a, r); @@ -275,20 +323,6 @@ void vTanh(const int n, const double* a, double* r) { } #else -DEFINE_MATRIX_BINARY_OP(vExp, b = std::exp(a)); -template -void vExp(const int n, const T* a, T* r) { - hl_cpu_apply_binary_op, 0, 0>( - binary::vExp(), const_cast(a), r, 1, n, n, n); -} - -DEFINE_MATRIX_BINARY_OP(vLog, b = std::log(a)); -template -void vLog(const int n, const T* a, T* r) { - hl_cpu_apply_binary_op, 0, 0>( - binary::vLog(), const_cast(a), r, 1, n, n, n); -} - DEFINE_MATRIX_BINARY_OP(vInvSqrt, b = 1.0f / std::sqrt(a)); template void vInvSqrt(const int n, const T* a, T* r) { @@ -312,41 +346,12 @@ void vTanh(const int n, const T* a, T* r) { binary::vTanh(), const_cast(a), r, 1, n, n, n); } -DEFINE_MATRIX_BINARY_PARAMETER_OP(vPow, ONE_PARAMETER, b = std::pow(a, p)); -template -void vPow(const int n, const T* a, const T b, T* r) { - hl_cpu_apply_binary_op, 0, 0>( - binary::vPow(b), const_cast(a), r, 1, n, n, n); -} - -DEFINE_MATRIX_TERNARY_OP(vAdd, c = a + b); -template -void vAdd(const int n, const T* a, const T* b, T* r) { - hl_cpu_apply_ternary_op, 0, 0>(ternary::vAdd(), - const_cast(a), - const_cast(b), - r, - 1, - n, - n, - n, - n); -} - -template void vExp(const int n, const float* a, float* r); -template void vExp(const int n, const double* a, double* r); -template void vLog(const int n, const float* a, float* r); -template void vLog(const int n, const double* a, double* r); template void vInvSqrt(const int n, const double* a, double* r); template void vInvSqrt(const int n, const float* a, float* r); template void vLog1p(const int n, const float* a, float* r); template void vLog1p(const int n, const double* a, double* r); template void vTanh(const int n, const float* a, float* r); template void vTanh(const int n, const double* a, double* r); -template void vPow(const int n, const float* a, const float b, float* r); -template void vPow(const int n, const double* a, const double b, double* r); -template void vAdd(const int n, const float* a, const float* b, float* r); -template void vAdd(const int n, const double* a, const double* b, double* r); #endif diff --git a/paddle/math/MathFunctions.h b/paddle/math/MathFunctions.h index 8ada0d34c6733..799948cf08b4f 100644 --- a/paddle/math/MathFunctions.h +++ b/paddle/math/MathFunctions.h @@ -15,6 +15,12 @@ limitations under the License. */ #ifndef MATHFUNCTIONS_H_ #define MATHFUNCTIONS_H_ +#ifdef PADDLE_USE_MKL_LITE +#include +#include +#include +#endif + #ifdef PADDLE_USE_MKL #include #include From f490d94210d29e8474319c87ed1fb6cacda63693 Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Wed, 19 Jul 2017 21:51:22 +0800 Subject: [PATCH 02/12] separate MKL_LITE from MKLDNN --- CMakeLists.txt | 11 +++++-- cmake/cblas.cmake | 17 +++++++++- cmake/configure.cmake | 22 +++++++++++-- cmake/external/mkldnn.cmake | 44 ++++++++++---------------- cmake/external/mkllite.cmake | 61 ++++++++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 34 deletions(-) create mode 100644 cmake/external/mkllite.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e664d1415399..fedf5db0b77f9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,8 @@ include(simd) ################################ Configurations ####################################### option(WITH_GPU "Compile PaddlePaddle with NVIDIA GPU" ${CUDA_FOUND}) option(WITH_AVX "Compile PaddlePaddle with AVX intrinsics" ${AVX_FOUND}) -option(WITH_MKLDNN "Compile PaddlePaddle with mkl-dnn support." ON) +option(WITH_MKLDNN "Compile PaddlePaddle with mkl-dnn support." ${AVX_FOUND}) +option(WITH_MKL_LITE "Compile PaddlePaddle with mkl lite package." ${AVX_FOUND}) option(WITH_DSO "Compile PaddlePaddle with dynamic linked CUDA" ON) option(WITH_TESTING "Compile PaddlePaddle with unit testing" ON) option(WITH_SWIG_PY "Compile PaddlePaddle with inference api" ON) @@ -76,6 +77,10 @@ if(ANDROID) "Disable PYTHON when cross-compiling for Android" FORCE) set(WITH_RDMA OFF CACHE STRING "Disable RDMA when cross-compiling for Android" FORCE) + set(WITH_MKLDNN OFF CACHE STRING + "Disable MKLDNN when cross-compiling for Android" FORCE) + set(WITH_MKL_LITE OFF CACHE STRING + "Disable MKL lite package when cross-compiling for Android" FORCE) endif(ANDROID) set(THIRD_PARTY_PATH "${CMAKE_BINARY_DIR}/third_party" CACHE STRING @@ -89,14 +94,15 @@ endif() ######################################################################################## +include(external/mkllite) # download mkl minimal lite package include(external/zlib) # download, build, install zlib include(external/gflags) # download, build, install gflags include(external/glog) # download, build, install glog include(external/gtest) # download, build, install gtest include(external/protobuf) # download, build, install protobuf include(external/python) # download, build, install python -include(external/mkldnn) # download, build, install mkldnn include(external/openblas) # download, build, install openblas +include(external/mkldnn) # download, build, install mkldnn include(external/swig) # download, build, install swig include(external/warpctc) # download, build, install warpctc include(external/any) # download libn::any @@ -139,7 +145,6 @@ if(WITH_GPU) endif(WITH_GPU) if(WITH_MKLDNN) - message(STATUS "MKLDNN_LIBRARY: ${MKLDNN_LIBRARY}") list(APPEND EXTERNAL_LIBS ${MKLDNN_LIBRARY} ${MKL_LITE_LIB_IOMP}) endif() diff --git a/cmake/cblas.cmake b/cmake/cblas.cmake index ee654e64bd0d5..52556b1b40cb5 100644 --- a/cmake/cblas.cmake +++ b/cmake/cblas.cmake @@ -15,7 +15,22 @@ set(CBLAS_FOUND OFF) -## Find MKL First. +## Find MKL Lite First. +if(WITH_MKL_LITE AND MKL_LITE_INC_DIR AND MKL_LITE_LIB) + set(CBLAS_FOUND ON) + set(CBLAS_PROVIDER MKL_LITE) + set(CBLAS_INC_DIR ${MKL_LITE_INC_DIR}) + set(CBLAS_LIBRARIES ${MKL_LITE_LIB}) + + add_definitions(-DPADDLE_USE_MKL_LITE) + add_definitions(-DLAPACK_FOUND) + + message(STATUS "Found cblas and lapack in MKL Lite " + "(include: ${CBLAS_INC_DIR}, library: ${CBLAS_LIBRARIES})") + return() +endif() + +## Then find MKL. set(INTEL_MKL_ROOT "/opt/intel/mkl" CACHE PATH "Folder contains intel mkl libs") set(MKL_ROOT $ENV{MKL_ROOT} CACHE PATH "Folder contains env MKL") diff --git a/cmake/configure.cmake b/cmake/configure.cmake index 87191976821dd..37eececfd58b8 100644 --- a/cmake/configure.cmake +++ b/cmake/configure.cmake @@ -69,8 +69,26 @@ endif(NOT WITH_GPU) if(WITH_MKLDNN) add_definitions(-DPADDLE_USE_MKLDNN) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") + if (WITH_MKL_LITE AND MKLDNN_IOMP_DIR) + message(STATUS "Enable Intel OpenMP at ${MKLDNN_IOMP_DIR}") + set(OPENMP_FLAGS "-fopenmp") + set(CMAKE_C_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS ${OPENMP_FLAGS}) + set(CMAKE_CXX_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS ${OPENMP_FLAGS}) + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -L${MKLDNN_IOMP_DIR} -liomp5 -Wl,--as-needed") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L${MKLDNN_IOMP_DIR} -liomp5 -Wl,--as-needed") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENMP_FLAGS}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPENMP_FLAGS}") + else() + find_package(OpenMP) + if(OPENMP_FOUND) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") + else() + message(WARNING "Can not find OpenMP." + "Some performance features in MKLDNN may not be available") + endif() + endif() + endif(WITH_MKLDNN) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SIMD_FLAG}") diff --git a/cmake/external/mkldnn.cmake b/cmake/external/mkldnn.cmake index 834f5ae230c9b..28a753e19a0ee 100644 --- a/cmake/external/mkldnn.cmake +++ b/cmake/external/mkldnn.cmake @@ -23,10 +23,6 @@ SET(MKLDNN_SOURCES_DIR ${THIRD_PARTY_PATH}/mkldnn) SET(MKLDNN_INSTALL_DIR ${THIRD_PARTY_PATH}/install/mkldnn) SET(MKLDNN_INCLUDE_DIR "${MKLDNN_INSTALL_DIR}/include" CACHE PATH "mkldnn include directory." FORCE) -# The following magic numbers should be updated regularly to keep latest version -SET(MKLDNN_TAG "v0.9") -SET(MKLDNN_MKL_VER "mklml_lnx_2018.0.20170425") - IF(WIN32) MESSAGE(WARNING "It is not supported compiling with mkldnn in windows Paddle yet." "Force WITH_MKLDNN=OFF") @@ -42,37 +38,29 @@ ENDIF(WIN32) INCLUDE_DIRECTORIES(${MKLDNN_INCLUDE_DIR}) -SET(MKLDNN_CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") -SET(MKLDNN_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") +IF(${CBLAS_PROVIDER} STREQUAL "MKL_LITE") + SET(MKLDNN_DEPENDS ${MKL_LITE_PROJECT}) + SET(MKLDNN_MKLROOT ${MKL_LITE_ROOT}) + SET(MKLDNN_IOMP_DIR ${MKL_LITE_LIB_DIR}) +ENDIF() ExternalProject_Add( ${MKLDNN_PROJECT} ${EXTERNAL_PROJECT_LOG_ARGS} - GIT_REPOSITORY "https://github.com/01org/mkl-dnn.git" - GIT_TAG "${MKLDNN_TAG}" - PREFIX ${MKLDNN_SOURCES_DIR} - PATCH_COMMAND cd /scripts && ./prepare_mkl.sh - UPDATE_COMMAND "" - CMAKE_ARGS -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} - CMAKE_ARGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} - CMAKE_ARGS -DCMAKE_CXX_FLAGS=${MKLDNN_CMAKE_CXX_FLAGS} - CMAKE_ARGS -DCMAKE_C_FLAGS=${MKLDNN_CMAKE_C_FLAGS} - CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${MKLDNN_INSTALL_DIR} - CMAKE_ARGS -DCMAKE_INSTALL_LIBDIR=${MKLDNN_INSTALL_DIR}/lib - CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release - CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${MKLDNN_INSTALL_DIR} - -DCMAKE_INSTALL_LIBDIR:PATH=${MKLDNN_INSTALL_DIR}/lib - -DCMAKE_BUILD_TYPE:STRING=Release + DEPENDS ${MKLDNN_DEPENDS} + GIT_REPOSITORY "https://github.com/01org/mkl-dnn.git" + GIT_TAG "v0.9" + PREFIX ${MKLDNN_SOURCES_DIR} + CONFIGURE_COMMAND mkdir -p /build + BUILD_COMMAND cd /build + && cmake .. -DCMAKE_INSTALL_PREFIX=${MKLDNN_INSTALL_DIR} -DMKLROOT=${MKLDNN_MKLROOT} + && make all -j${CPU_CORES} + INSTALL_COMMAND cd /build && make install + UPDATE_COMMAND "" ) -SET(MKL_LITE_DIR ${MKLDNN_SOURCES_DIR}/src/${MKLDNN_PROJECT}/external/${MKLDNN_MKL_VER}) -SET(MKL_LITE_INC_DIR ${MKL_LITE_DIR}/include) -SET(MKL_LITE_LIB ${MKL_LITE_DIR}/lib/libmklml_intel.so) -SET(MKL_LITE_LIB_IOMP ${MKL_LITE_DIR}/lib/libiomp5.so) -SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${MKL_LITE_DIR}/lib") - ADD_LIBRARY(mkldnn STATIC IMPORTED GLOBAL) SET_PROPERTY(TARGET mkldnn PROPERTY IMPORTED_LOCATION ${MKLDNN_LIBRARY}) ADD_DEPENDENCIES(mkldnn ${MKLDNN_PROJECT}) - +MESSAGE(STATUS "Mkldnn library: ${MKLDNN_LIBRARY}") LIST(APPEND external_project_dependencies mkldnn) diff --git a/cmake/external/mkllite.cmake b/cmake/external/mkllite.cmake new file mode 100644 index 0000000000000..e889290e36e35 --- /dev/null +++ b/cmake/external/mkllite.cmake @@ -0,0 +1,61 @@ +# Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve. +# +# Licensed 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. + +IF(NOT ${WITH_MKL_LITE}) + return() +ENDIF(NOT ${WITH_MKL_LITE}) + +INCLUDE(ExternalProject) + +SET(MKL_LITE_PROJECT "extern_mkllite") +SET(MKL_LITE_VER "mklml_lnx_2018.0.20170425") +SET(MKL_LITE_URL "https://github.com/01org/mkl-dnn/releases/download/v0.9/${MKL_LITE_VER}.tgz") +SET(MKL_LITE_DOWNLOAD_DIR ${THIRD_PARTY_PATH}/mkllite) + +SET(MKL_LITE_ROOT ${MKL_LITE_DOWNLOAD_DIR}/${MKL_LITE_VER}) +SET(MKL_LITE_INC_DIR ${MKL_LITE_ROOT}/include) +SET(MKL_LITE_LIB_DIR ${MKL_LITE_ROOT}/lib) +SET(MKL_LITE_LIB ${MKL_LITE_LIB_DIR}/libmklml_intel.so) +SET(MKL_LITE_IOMP_LIB ${MKL_LITE_LIB_DIR}/libiomp5.so) +SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${MKL_LITE_ROOT}/lib") + +INCLUDE_DIRECTORIES(${MKL_LITE_INC_DIR}) + +ExternalProject_Add( + ${MKL_LITE_PROJECT} + ${EXTERNAL_PROJECT_LOG_ARGS} + PREFIX ${MKL_LITE_DOWNLOAD_DIR} + DOWNLOAD_DIR ${MKL_LITE_DOWNLOAD_DIR} + DOWNLOAD_COMMAND wget --no-check-certificate ${MKL_LITE_URL} + && tar -xzf ${MKL_LITE_DOWNLOAD_DIR}/${MKL_LITE_VER}.tgz + DOWNLOAD_NO_PROGRESS 1 + UPDATE_COMMAND "" + PATCH_COMMAND "" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) + +IF (${CMAKE_VERSION} VERSION_LESS "3.3.0") + SET(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/mkllite_dummy.c) + FILE(WRITE ${dummyfile} "const char * dummy_mkllite = \"${dummyfile}\";") + ADD_LIBRARY(mkllite STATIC ${dummyfile}) +ELSE() + ADD_LIBRARY(mkllite INTERFACE) +ENDIF() + +ADD_DEPENDENCIES(mkllite ${MKL_LITE_PROJECT}) + +LIST(APPEND external_project_dependencies mkllite) From 1601c34aa580cceb9ccc2ca4f1a82a9299f0c887 Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Wed, 19 Jul 2017 22:17:09 +0800 Subject: [PATCH 03/12] fix ld bug when enable WITH_TESTING --- cmake/external/gtest.cmake | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cmake/external/gtest.cmake b/cmake/external/gtest.cmake index 77e06e983e9f8..8ac68aa325f97 100644 --- a/cmake/external/gtest.cmake +++ b/cmake/external/gtest.cmake @@ -34,9 +34,15 @@ IF(WITH_TESTING) "${GTEST_INSTALL_DIR}/lib/libgtest_main.a" CACHE FILEPATH "gtest main libraries." FORCE) ENDIF(WIN32) + IF(WITH_MKL_LITE) + # wait for mkl downloading completed + SET(GTEST_DEPENDS ${MKL_LITE_PROJECT}) + ENDIF() + ExternalProject_Add( extern_gtest ${EXTERNAL_PROJECT_LOG_ARGS} + DEPENDS ${GTEST_DEPENDS} GIT_REPOSITORY "https://github.com/google/googletest.git" GIT_TAG "release-1.8.0" PREFIX ${GTEST_SOURCES_DIR} From 577bb4e3467aebf07118c69b85c6a246db235be8 Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Thu, 20 Jul 2017 11:22:38 +0800 Subject: [PATCH 04/12] rename mkllite to mklml --- CMakeLists.txt | 10 +++--- cmake/cblas.cmake | 28 ++++------------ cmake/configure.cmake | 2 +- cmake/external/gtest.cmake | 6 ++-- cmake/external/mkldnn.cmake | 8 ++--- cmake/external/mkllite.cmake | 61 ----------------------------------- cmake/external/mklml.cmake | 61 +++++++++++++++++++++++++++++++++++ paddle/math/MathFunctions.cpp | 2 +- paddle/math/MathFunctions.h | 2 +- 9 files changed, 83 insertions(+), 97 deletions(-) delete mode 100644 cmake/external/mkllite.cmake create mode 100644 cmake/external/mklml.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index fedf5db0b77f9..62ab4669cba55 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,7 @@ include(simd) option(WITH_GPU "Compile PaddlePaddle with NVIDIA GPU" ${CUDA_FOUND}) option(WITH_AVX "Compile PaddlePaddle with AVX intrinsics" ${AVX_FOUND}) option(WITH_MKLDNN "Compile PaddlePaddle with mkl-dnn support." ${AVX_FOUND}) -option(WITH_MKL_LITE "Compile PaddlePaddle with mkl lite package." ${AVX_FOUND}) +option(WITH_MKLML "Compile PaddlePaddle with mklml package." ${AVX_FOUND}) option(WITH_DSO "Compile PaddlePaddle with dynamic linked CUDA" ON) option(WITH_TESTING "Compile PaddlePaddle with unit testing" ON) option(WITH_SWIG_PY "Compile PaddlePaddle with inference api" ON) @@ -79,8 +79,8 @@ if(ANDROID) "Disable RDMA when cross-compiling for Android" FORCE) set(WITH_MKLDNN OFF CACHE STRING "Disable MKLDNN when cross-compiling for Android" FORCE) - set(WITH_MKL_LITE OFF CACHE STRING - "Disable MKL lite package when cross-compiling for Android" FORCE) + set(WITH_MKLML OFF CACHE STRING + "Disable MKLML package when cross-compiling for Android" FORCE) endif(ANDROID) set(THIRD_PARTY_PATH "${CMAKE_BINARY_DIR}/third_party" CACHE STRING @@ -94,7 +94,7 @@ endif() ######################################################################################## -include(external/mkllite) # download mkl minimal lite package +include(external/mklml) # download mklml package include(external/zlib) # download, build, install zlib include(external/gflags) # download, build, install gflags include(external/glog) # download, build, install glog @@ -145,7 +145,7 @@ if(WITH_GPU) endif(WITH_GPU) if(WITH_MKLDNN) - list(APPEND EXTERNAL_LIBS ${MKLDNN_LIBRARY} ${MKL_LITE_LIB_IOMP}) + list(APPEND EXTERNAL_LIBS ${MKLDNN_LIBRARY} ${MKLML_LIB_IOMP}) endif() if(USE_NNPACK) diff --git a/cmake/cblas.cmake b/cmake/cblas.cmake index 52556b1b40cb5..854066fd1d205 100644 --- a/cmake/cblas.cmake +++ b/cmake/cblas.cmake @@ -15,17 +15,17 @@ set(CBLAS_FOUND OFF) -## Find MKL Lite First. -if(WITH_MKL_LITE AND MKL_LITE_INC_DIR AND MKL_LITE_LIB) +## Find MKLML First. +if(WITH_MKLML AND MKLML_INC_DIR AND MKLML_LIB) set(CBLAS_FOUND ON) - set(CBLAS_PROVIDER MKL_LITE) - set(CBLAS_INC_DIR ${MKL_LITE_INC_DIR}) - set(CBLAS_LIBRARIES ${MKL_LITE_LIB}) + set(CBLAS_PROVIDER MKLML) + set(CBLAS_INC_DIR ${MKLML_INC_DIR}) + set(CBLAS_LIBRARIES ${MKLML_LIB}) - add_definitions(-DPADDLE_USE_MKL_LITE) + add_definitions(-DPADDLE_USE_MKLML) add_definitions(-DLAPACK_FOUND) - message(STATUS "Found cblas and lapack in MKL Lite " + message(STATUS "Found cblas and lapack in MKLML " "(include: ${CBLAS_INC_DIR}, library: ${CBLAS_LIBRARIES})") return() endif() @@ -43,20 +43,6 @@ set(MKL_LIB_SEARCH_PATHS ${INTEL_MKL_ROOT}/lib ${INTEL_MKL_ROOT}/lib/intel64) -if(MKL_LITE_INC_DIR AND MKL_LITE_LIB) - set(CBLAS_FOUND ON) - set(CBLAS_PROVIDER MKL_LITE) - set(CBLAS_INC_DIR ${MKL_LITE_INC_DIR}) - set(CBLAS_LIBRARIES ${MKL_LITE_LIB}) - - add_definitions(-DPADDLE_USE_MKL_LITE) - add_definitions(-DLAPACK_FOUND) - - message(STATUS "Found cblas and lapack in MKL Lite " - "(include: ${MKL_LITE_INC_DIR}, library: ${CBLAS_LIBRARIES})") - return() -endif() - find_path(MKL_INC_DIR mkl.h PATHS ${MKL_INCLUDE_SEARCH_PATHS}) find_path(MKL_LAPACK_INC_DIR mkl_lapacke.h PATHS diff --git a/cmake/configure.cmake b/cmake/configure.cmake index 37eececfd58b8..69220e03fe8e3 100644 --- a/cmake/configure.cmake +++ b/cmake/configure.cmake @@ -69,7 +69,7 @@ endif(NOT WITH_GPU) if(WITH_MKLDNN) add_definitions(-DPADDLE_USE_MKLDNN) - if (WITH_MKL_LITE AND MKLDNN_IOMP_DIR) + if (WITH_MKLML AND MKLDNN_IOMP_DIR) message(STATUS "Enable Intel OpenMP at ${MKLDNN_IOMP_DIR}") set(OPENMP_FLAGS "-fopenmp") set(CMAKE_C_CREATE_SHARED_LIBRARY_FORBIDDEN_FLAGS ${OPENMP_FLAGS}) diff --git a/cmake/external/gtest.cmake b/cmake/external/gtest.cmake index 8ac68aa325f97..e3970073a1a0b 100644 --- a/cmake/external/gtest.cmake +++ b/cmake/external/gtest.cmake @@ -34,9 +34,9 @@ IF(WITH_TESTING) "${GTEST_INSTALL_DIR}/lib/libgtest_main.a" CACHE FILEPATH "gtest main libraries." FORCE) ENDIF(WIN32) - IF(WITH_MKL_LITE) - # wait for mkl downloading completed - SET(GTEST_DEPENDS ${MKL_LITE_PROJECT}) + IF(WITH_MKLML) + # wait for mklml downloading completed + SET(GTEST_DEPENDS ${MKLML_PROJECT}) ENDIF() ExternalProject_Add( diff --git a/cmake/external/mkldnn.cmake b/cmake/external/mkldnn.cmake index 28a753e19a0ee..9066b5abd5e73 100644 --- a/cmake/external/mkldnn.cmake +++ b/cmake/external/mkldnn.cmake @@ -38,10 +38,10 @@ ENDIF(WIN32) INCLUDE_DIRECTORIES(${MKLDNN_INCLUDE_DIR}) -IF(${CBLAS_PROVIDER} STREQUAL "MKL_LITE") - SET(MKLDNN_DEPENDS ${MKL_LITE_PROJECT}) - SET(MKLDNN_MKLROOT ${MKL_LITE_ROOT}) - SET(MKLDNN_IOMP_DIR ${MKL_LITE_LIB_DIR}) +IF(${CBLAS_PROVIDER} STREQUAL "MKLML") + SET(MKLDNN_DEPENDS ${MKLML_PROJECT}) + SET(MKLDNN_MKLROOT ${MKLML_ROOT}) + SET(MKLDNN_IOMP_DIR ${MKLML_LIB_DIR}) ENDIF() ExternalProject_Add( diff --git a/cmake/external/mkllite.cmake b/cmake/external/mkllite.cmake deleted file mode 100644 index e889290e36e35..0000000000000 --- a/cmake/external/mkllite.cmake +++ /dev/null @@ -1,61 +0,0 @@ -# Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve. -# -# Licensed 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. - -IF(NOT ${WITH_MKL_LITE}) - return() -ENDIF(NOT ${WITH_MKL_LITE}) - -INCLUDE(ExternalProject) - -SET(MKL_LITE_PROJECT "extern_mkllite") -SET(MKL_LITE_VER "mklml_lnx_2018.0.20170425") -SET(MKL_LITE_URL "https://github.com/01org/mkl-dnn/releases/download/v0.9/${MKL_LITE_VER}.tgz") -SET(MKL_LITE_DOWNLOAD_DIR ${THIRD_PARTY_PATH}/mkllite) - -SET(MKL_LITE_ROOT ${MKL_LITE_DOWNLOAD_DIR}/${MKL_LITE_VER}) -SET(MKL_LITE_INC_DIR ${MKL_LITE_ROOT}/include) -SET(MKL_LITE_LIB_DIR ${MKL_LITE_ROOT}/lib) -SET(MKL_LITE_LIB ${MKL_LITE_LIB_DIR}/libmklml_intel.so) -SET(MKL_LITE_IOMP_LIB ${MKL_LITE_LIB_DIR}/libiomp5.so) -SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${MKL_LITE_ROOT}/lib") - -INCLUDE_DIRECTORIES(${MKL_LITE_INC_DIR}) - -ExternalProject_Add( - ${MKL_LITE_PROJECT} - ${EXTERNAL_PROJECT_LOG_ARGS} - PREFIX ${MKL_LITE_DOWNLOAD_DIR} - DOWNLOAD_DIR ${MKL_LITE_DOWNLOAD_DIR} - DOWNLOAD_COMMAND wget --no-check-certificate ${MKL_LITE_URL} - && tar -xzf ${MKL_LITE_DOWNLOAD_DIR}/${MKL_LITE_VER}.tgz - DOWNLOAD_NO_PROGRESS 1 - UPDATE_COMMAND "" - PATCH_COMMAND "" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" -) - -IF (${CMAKE_VERSION} VERSION_LESS "3.3.0") - SET(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/mkllite_dummy.c) - FILE(WRITE ${dummyfile} "const char * dummy_mkllite = \"${dummyfile}\";") - ADD_LIBRARY(mkllite STATIC ${dummyfile}) -ELSE() - ADD_LIBRARY(mkllite INTERFACE) -ENDIF() - -ADD_DEPENDENCIES(mkllite ${MKL_LITE_PROJECT}) - -LIST(APPEND external_project_dependencies mkllite) diff --git a/cmake/external/mklml.cmake b/cmake/external/mklml.cmake new file mode 100644 index 0000000000000..45b3f9d85bec6 --- /dev/null +++ b/cmake/external/mklml.cmake @@ -0,0 +1,61 @@ +# Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserve. +# +# Licensed 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. + +IF(NOT ${WITH_MKLML}) + return() +ENDIF(NOT ${WITH_MKLML}) + +INCLUDE(ExternalProject) + +SET(MKLML_PROJECT "extern_mklml") +SET(MKLML_VER "mklml_lnx_2018.0.20170425") +SET(MKLML_URL "https://github.com/01org/mkl-dnn/releases/download/v0.9/${MKLML_VER}.tgz") +SET(MKLML_DOWNLOAD_DIR ${THIRD_PARTY_PATH}/mklml) + +SET(MKLML_ROOT ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}) +SET(MKLML_INC_DIR ${MKLML_ROOT}/include) +SET(MKLML_LIB_DIR ${MKLML_ROOT}/lib) +SET(MKLML_LIB ${MKLML_LIB_DIR}/libmklml_intel.so) +SET(MKLML_IOMP_LIB ${MKLML_LIB_DIR}/libiomp5.so) +SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${MKLML_ROOT}/lib") + +INCLUDE_DIRECTORIES(${MKLML_INC_DIR}) + +ExternalProject_Add( + ${MKLML_PROJECT} + ${EXTERNAL_PROJECT_LOG_ARGS} + PREFIX ${MKLML_DOWNLOAD_DIR} + DOWNLOAD_DIR ${MKLML_DOWNLOAD_DIR} + DOWNLOAD_COMMAND wget --no-check-certificate -O ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}.tgz ${MKLML_URL} + && tar -xzf ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}.tgz + DOWNLOAD_NO_PROGRESS 1 + UPDATE_COMMAND "" + PATCH_COMMAND "" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) + +IF (${CMAKE_VERSION} VERSION_LESS "3.3.0") + SET(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/mklml_dummy.c) + FILE(WRITE ${dummyfile} "const char * dummy_mklml = \"${dummyfile}\";") + ADD_LIBRARY(mklml STATIC ${dummyfile}) +ELSE() + ADD_LIBRARY(mklml INTERFACE) +ENDIF() + +ADD_DEPENDENCIES(mklml ${MKLML_PROJECT}) + +LIST(APPEND external_project_dependencies mklml) diff --git a/paddle/math/MathFunctions.cpp b/paddle/math/MathFunctions.cpp index 999b72cc1597b..c8ba1074a1555 100644 --- a/paddle/math/MathFunctions.cpp +++ b/paddle/math/MathFunctions.cpp @@ -202,7 +202,7 @@ double dotProduct(const int n, const double* x, const double* y) { return cblas_ddot(n, x, 1, y, 1); } -#if defined(PADDLE_USE_MKL) || defined(PADDLE_USE_MKL_LITE) +#if defined(PADDLE_USE_MKL) || defined(PADDLE_USE_MKLML) template <> void vExp(const int n, const float* a, float* r) { diff --git a/paddle/math/MathFunctions.h b/paddle/math/MathFunctions.h index 799948cf08b4f..637643838ff43 100644 --- a/paddle/math/MathFunctions.h +++ b/paddle/math/MathFunctions.h @@ -15,7 +15,7 @@ limitations under the License. */ #ifndef MATHFUNCTIONS_H_ #define MATHFUNCTIONS_H_ -#ifdef PADDLE_USE_MKL_LITE +#ifdef PADDLE_USE_MKLML #include #include #include From f6dd8ae1644608f7b3b986c9a3bc53c0e36d2fa6 Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Thu, 20 Jul 2017 14:15:35 +0800 Subject: [PATCH 05/12] fix make warning compile with mkldnn --- cmake/external/mkldnn.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/external/mkldnn.cmake b/cmake/external/mkldnn.cmake index 9066b5abd5e73..fba0756f3dcd1 100644 --- a/cmake/external/mkldnn.cmake +++ b/cmake/external/mkldnn.cmake @@ -54,8 +54,8 @@ ExternalProject_Add( CONFIGURE_COMMAND mkdir -p /build BUILD_COMMAND cd /build && cmake .. -DCMAKE_INSTALL_PREFIX=${MKLDNN_INSTALL_DIR} -DMKLROOT=${MKLDNN_MKLROOT} - && make all -j${CPU_CORES} - INSTALL_COMMAND cd /build && make install + && $(MAKE) + INSTALL_COMMAND cd /build && $(MAKE) install UPDATE_COMMAND "" ) From a7e23a4cc2a91859c295569164c9a9d2e576daa1 Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Thu, 20 Jul 2017 16:47:37 +0800 Subject: [PATCH 06/12] fix link error static to shared --- CMakeLists.txt | 2 +- cmake/external/mkldnn.cmake | 3 ++- cmake/external/mklml.cmake | 11 ++--------- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 62ab4669cba55..4cdd8dbd77b98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,7 +145,7 @@ if(WITH_GPU) endif(WITH_GPU) if(WITH_MKLDNN) - list(APPEND EXTERNAL_LIBS ${MKLDNN_LIBRARY} ${MKLML_LIB_IOMP}) + list(APPEND EXTERNAL_LIBS ${MKLDNN_LIBRARY} ${MKLDNN_IOMP_LIB}) endif() if(USE_NNPACK) diff --git a/cmake/external/mkldnn.cmake b/cmake/external/mkldnn.cmake index fba0756f3dcd1..2b744792738f5 100644 --- a/cmake/external/mkldnn.cmake +++ b/cmake/external/mkldnn.cmake @@ -41,6 +41,7 @@ INCLUDE_DIRECTORIES(${MKLDNN_INCLUDE_DIR}) IF(${CBLAS_PROVIDER} STREQUAL "MKLML") SET(MKLDNN_DEPENDS ${MKLML_PROJECT}) SET(MKLDNN_MKLROOT ${MKLML_ROOT}) + SET(MKLDNN_IOMP_LIB ${MKLML_IOMP_LIB}) SET(MKLDNN_IOMP_DIR ${MKLML_LIB_DIR}) ENDIF() @@ -59,7 +60,7 @@ ExternalProject_Add( UPDATE_COMMAND "" ) -ADD_LIBRARY(mkldnn STATIC IMPORTED GLOBAL) +ADD_LIBRARY(mkldnn SHARED IMPORTED GLOBAL) SET_PROPERTY(TARGET mkldnn PROPERTY IMPORTED_LOCATION ${MKLDNN_LIBRARY}) ADD_DEPENDENCIES(mkldnn ${MKLDNN_PROJECT}) MESSAGE(STATUS "Mkldnn library: ${MKLDNN_LIBRARY}") diff --git a/cmake/external/mklml.cmake b/cmake/external/mklml.cmake index 45b3f9d85bec6..dff51baec3455 100644 --- a/cmake/external/mklml.cmake +++ b/cmake/external/mklml.cmake @@ -48,14 +48,7 @@ ExternalProject_Add( TEST_COMMAND "" ) -IF (${CMAKE_VERSION} VERSION_LESS "3.3.0") - SET(dummyfile ${CMAKE_CURRENT_BINARY_DIR}/mklml_dummy.c) - FILE(WRITE ${dummyfile} "const char * dummy_mklml = \"${dummyfile}\";") - ADD_LIBRARY(mklml STATIC ${dummyfile}) -ELSE() - ADD_LIBRARY(mklml INTERFACE) -ENDIF() - +ADD_LIBRARY(mklml SHARED IMPORTED GLOBAL) +SET_PROPERTY(TARGET mklml PROPERTY IMPORTED_LOCATION ${MKLML_LIB}) ADD_DEPENDENCIES(mklml ${MKLML_PROJECT}) - LIST(APPEND external_project_dependencies mklml) From 19ec08fa866e6c4054dc16167c1e42ab13f22c20 Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Thu, 20 Jul 2017 23:43:31 +0800 Subject: [PATCH 07/12] change install path of mkldnn and mklml --- cmake/external/mkldnn.cmake | 2 +- cmake/external/mklml.cmake | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/external/mkldnn.cmake b/cmake/external/mkldnn.cmake index 2b744792738f5..615b1ddf35019 100644 --- a/cmake/external/mkldnn.cmake +++ b/cmake/external/mkldnn.cmake @@ -20,7 +20,7 @@ INCLUDE(ExternalProject) SET(MKLDNN_PROJECT "extern_mkldnn") SET(MKLDNN_SOURCES_DIR ${THIRD_PARTY_PATH}/mkldnn) -SET(MKLDNN_INSTALL_DIR ${THIRD_PARTY_PATH}/install/mkldnn) +SET(MKLDNN_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/opt/paddle/third_party/mkldnn") SET(MKLDNN_INCLUDE_DIR "${MKLDNN_INSTALL_DIR}/include" CACHE PATH "mkldnn include directory." FORCE) IF(WIN32) diff --git a/cmake/external/mklml.cmake b/cmake/external/mklml.cmake index dff51baec3455..e1925a07d00c2 100644 --- a/cmake/external/mklml.cmake +++ b/cmake/external/mklml.cmake @@ -21,7 +21,7 @@ INCLUDE(ExternalProject) SET(MKLML_PROJECT "extern_mklml") SET(MKLML_VER "mklml_lnx_2018.0.20170425") SET(MKLML_URL "https://github.com/01org/mkl-dnn/releases/download/v0.9/${MKLML_VER}.tgz") -SET(MKLML_DOWNLOAD_DIR ${THIRD_PARTY_PATH}/mklml) +SET(MKLML_DOWNLOAD_DIR "${CMAKE_INSTALL_PREFIX}/opt/paddle/third_party/mklml") SET(MKLML_ROOT ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}) SET(MKLML_INC_DIR ${MKLML_ROOT}/include) From 2fd43fc5a0b701ce1a097c7267dab3145276fea6 Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Fri, 21 Jul 2017 00:33:00 +0800 Subject: [PATCH 08/12] separate mklml download path and install path --- cmake/external/mklml.cmake | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cmake/external/mklml.cmake b/cmake/external/mklml.cmake index e1925a07d00c2..84629f01ac168 100644 --- a/cmake/external/mklml.cmake +++ b/cmake/external/mklml.cmake @@ -21,9 +21,10 @@ INCLUDE(ExternalProject) SET(MKLML_PROJECT "extern_mklml") SET(MKLML_VER "mklml_lnx_2018.0.20170425") SET(MKLML_URL "https://github.com/01org/mkl-dnn/releases/download/v0.9/${MKLML_VER}.tgz") -SET(MKLML_DOWNLOAD_DIR "${CMAKE_INSTALL_PREFIX}/opt/paddle/third_party/mklml") +SET(MKLML_DOWNLOAD_DIR "${THIRD_PARTY_PATH}/mklml") +SET(MKLML_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/opt/paddle/third_party/mklml") -SET(MKLML_ROOT ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}) +SET(MKLML_ROOT ${MKLML_INSTALL_DIR}/${MKLML_VER}) SET(MKLML_INC_DIR ${MKLML_ROOT}/include) SET(MKLML_LIB_DIR ${MKLML_ROOT}/lib) SET(MKLML_LIB ${MKLML_LIB_DIR}/libmklml_intel.so) @@ -38,7 +39,8 @@ ExternalProject_Add( PREFIX ${MKLML_DOWNLOAD_DIR} DOWNLOAD_DIR ${MKLML_DOWNLOAD_DIR} DOWNLOAD_COMMAND wget --no-check-certificate -O ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}.tgz ${MKLML_URL} - && tar -xzf ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}.tgz + && mkdir -p ${MKLML_INSTALL_DIR} + && tar -xzf ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}.tgz -C ${MKLML_INSTALL_DIR} DOWNLOAD_NO_PROGRESS 1 UPDATE_COMMAND "" PATCH_COMMAND "" From 75f66768bb94bf09f6b61ddfddaf35cadd2bc48b Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Fri, 21 Jul 2017 13:30:56 +0800 Subject: [PATCH 09/12] fix the permission issue of installing mklml --- cmake/external/mklml.cmake | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/cmake/external/mklml.cmake b/cmake/external/mklml.cmake index 84629f01ac168..c8a20ad46437b 100644 --- a/cmake/external/mklml.cmake +++ b/cmake/external/mklml.cmake @@ -21,33 +21,37 @@ INCLUDE(ExternalProject) SET(MKLML_PROJECT "extern_mklml") SET(MKLML_VER "mklml_lnx_2018.0.20170425") SET(MKLML_URL "https://github.com/01org/mkl-dnn/releases/download/v0.9/${MKLML_VER}.tgz") -SET(MKLML_DOWNLOAD_DIR "${THIRD_PARTY_PATH}/mklml") -SET(MKLML_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/opt/paddle/third_party/mklml") +SET(MKLML_SOURCE_DIR "${THIRD_PARTY_PATH}/mklml") +SET(MKLML_DOWNLOAD_DIR "${MKLML_SOURCE_DIR}/src/${MKLML_PROJECT}") +SET(MKLML_DST_DIR "opt/paddle/third_party/mklml") +SET(MKLML_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${MKLML_DST_DIR}") SET(MKLML_ROOT ${MKLML_INSTALL_DIR}/${MKLML_VER}) SET(MKLML_INC_DIR ${MKLML_ROOT}/include) SET(MKLML_LIB_DIR ${MKLML_ROOT}/lib) SET(MKLML_LIB ${MKLML_LIB_DIR}/libmklml_intel.so) SET(MKLML_IOMP_LIB ${MKLML_LIB_DIR}/libiomp5.so) -SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${MKLML_ROOT}/lib") +SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}" "${MKLML_ROOT}/lib") INCLUDE_DIRECTORIES(${MKLML_INC_DIR}) +SET(mklml_cmakefile ${MKLML_DOWNLOAD_DIR}/CMakeLists.txt) +FILE(WRITE ${mklml_cmakefile} "PROJECT(MKLML)\n" + "cmake_minimum_required(VERSION 3.0)\n" + "install(DIRECTORY ${MKLML_VER}\n" + " DESTINATION ${MKLML_DST_DIR})\n") + ExternalProject_Add( ${MKLML_PROJECT} ${EXTERNAL_PROJECT_LOG_ARGS} - PREFIX ${MKLML_DOWNLOAD_DIR} + PREFIX ${MKLML_SOURCE_DIR} DOWNLOAD_DIR ${MKLML_DOWNLOAD_DIR} DOWNLOAD_COMMAND wget --no-check-certificate -O ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}.tgz ${MKLML_URL} - && mkdir -p ${MKLML_INSTALL_DIR} - && tar -xzf ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}.tgz -C ${MKLML_INSTALL_DIR} + && tar -xzf ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}.tgz DOWNLOAD_NO_PROGRESS 1 UPDATE_COMMAND "" - PATCH_COMMAND "" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} + CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX} ) ADD_LIBRARY(mklml SHARED IMPORTED GLOBAL) From 1cf50574c34167e9c9a0de85fa4dc99b777ad4cd Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Fri, 21 Jul 2017 13:50:20 +0800 Subject: [PATCH 10/12] defualt disable mkldnn and mklml --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4cdd8dbd77b98..8012a0df899aa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,8 +37,8 @@ include(simd) ################################ Configurations ####################################### option(WITH_GPU "Compile PaddlePaddle with NVIDIA GPU" ${CUDA_FOUND}) option(WITH_AVX "Compile PaddlePaddle with AVX intrinsics" ${AVX_FOUND}) -option(WITH_MKLDNN "Compile PaddlePaddle with mkl-dnn support." ${AVX_FOUND}) -option(WITH_MKLML "Compile PaddlePaddle with mklml package." ${AVX_FOUND}) +option(WITH_MKLDNN "Compile PaddlePaddle with mkl-dnn support." OFF) +option(WITH_MKLML "Compile PaddlePaddle with mklml package." OFF) option(WITH_DSO "Compile PaddlePaddle with dynamic linked CUDA" ON) option(WITH_TESTING "Compile PaddlePaddle with unit testing" ON) option(WITH_SWIG_PY "Compile PaddlePaddle with inference api" ON) From 95ce961cef4166f69da78d42fff3633b62d3bbc5 Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Fri, 21 Jul 2017 16:04:13 +0800 Subject: [PATCH 11/12] fix permission issue --- CMakeLists.txt | 6 +++--- cmake/external/mkldnn.cmake | 13 +++++++++---- cmake/external/mklml.cmake | 10 +++++++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8012a0df899aa..127493bce844a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,8 +37,8 @@ include(simd) ################################ Configurations ####################################### option(WITH_GPU "Compile PaddlePaddle with NVIDIA GPU" ${CUDA_FOUND}) option(WITH_AVX "Compile PaddlePaddle with AVX intrinsics" ${AVX_FOUND}) -option(WITH_MKLDNN "Compile PaddlePaddle with mkl-dnn support." OFF) -option(WITH_MKLML "Compile PaddlePaddle with mklml package." OFF) +option(WITH_MKLDNN "Compile PaddlePaddle with mkl-dnn support." ${AVX_FOUND}) +option(WITH_MKLML "Compile PaddlePaddle with mklml package." ${AVX_FOUND}) option(WITH_DSO "Compile PaddlePaddle with dynamic linked CUDA" ON) option(WITH_TESTING "Compile PaddlePaddle with unit testing" ON) option(WITH_SWIG_PY "Compile PaddlePaddle with inference api" ON) @@ -94,7 +94,7 @@ endif() ######################################################################################## -include(external/mklml) # download mklml package +include(external/mklml) # download mklml package include(external/zlib) # download, build, install zlib include(external/gflags) # download, build, install gflags include(external/glog) # download, build, install glog diff --git a/cmake/external/mkldnn.cmake b/cmake/external/mkldnn.cmake index 615b1ddf35019..eff15de73f23d 100644 --- a/cmake/external/mkldnn.cmake +++ b/cmake/external/mkldnn.cmake @@ -18,10 +18,15 @@ ENDIF(NOT ${WITH_MKLDNN}) INCLUDE(ExternalProject) -SET(MKLDNN_PROJECT "extern_mkldnn") -SET(MKLDNN_SOURCES_DIR ${THIRD_PARTY_PATH}/mkldnn) -SET(MKLDNN_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/opt/paddle/third_party/mkldnn") -SET(MKLDNN_INCLUDE_DIR "${MKLDNN_INSTALL_DIR}/include" CACHE PATH "mkldnn include directory." FORCE) +SET(MKLDNN_PROJECT "extern_mkldnn") +SET(MKLDNN_SOURCES_DIR ${THIRD_PARTY_PATH}/mkldnn) +SET(MKLDNN_INSTALL_ROOT ${CMAKE_INSTALL_PREFIX}) +IF(NOT "$ENV{HOME}" STREQUAL "/root") + SET(MKLDNN_INSTALL_ROOT "$ENV{HOME}") +ENDIF() + +SET(MKLDNN_INSTALL_DIR "${MKLDNN_INSTALL_ROOT}/opt/paddle/third_party/mkldnn") +SET(MKLDNN_INCLUDE_DIR "${MKLDNN_INSTALL_DIR}/include" CACHE PATH "mkldnn include directory." FORCE) IF(WIN32) MESSAGE(WARNING "It is not supported compiling with mkldnn in windows Paddle yet." diff --git a/cmake/external/mklml.cmake b/cmake/external/mklml.cmake index c8a20ad46437b..3f940756a4abb 100644 --- a/cmake/external/mklml.cmake +++ b/cmake/external/mklml.cmake @@ -24,8 +24,12 @@ SET(MKLML_URL "https://github.com/01org/mkl-dnn/releases/download/v0.9 SET(MKLML_SOURCE_DIR "${THIRD_PARTY_PATH}/mklml") SET(MKLML_DOWNLOAD_DIR "${MKLML_SOURCE_DIR}/src/${MKLML_PROJECT}") SET(MKLML_DST_DIR "opt/paddle/third_party/mklml") -SET(MKLML_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/${MKLML_DST_DIR}") +SET(MKLML_INSTALL_ROOT "${CMAKE_INSTALL_PREFIX}") +IF(NOT "$ENV{HOME}" STREQUAL "/root") + SET(MKLML_INSTALL_ROOT "$ENV{HOME}") +ENDIF() +SET(MKLML_INSTALL_DIR ${MKLML_INSTALL_ROOT}/${MKLML_DST_DIR}) SET(MKLML_ROOT ${MKLML_INSTALL_DIR}/${MKLML_VER}) SET(MKLML_INC_DIR ${MKLML_ROOT}/include) SET(MKLML_LIB_DIR ${MKLML_ROOT}/lib) @@ -50,8 +54,8 @@ ExternalProject_Add( && tar -xzf ${MKLML_DOWNLOAD_DIR}/${MKLML_VER}.tgz DOWNLOAD_NO_PROGRESS 1 UPDATE_COMMAND "" - CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} - CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX} + CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${MKLML_INSTALL_ROOT} + CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${MKLML_INSTALL_ROOT} ) ADD_LIBRARY(mklml SHARED IMPORTED GLOBAL) From 19c465bb5b2b3405dce3c725e2c7aedba4e35117 Mon Sep 17 00:00:00 2001 From: tensor-tang Date: Fri, 21 Jul 2017 17:23:51 +0800 Subject: [PATCH 12/12] as aligned, defualt set mkldnn and mklml OFF --- CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 127493bce844a..c6d2ce57f3a83 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,8 +37,8 @@ include(simd) ################################ Configurations ####################################### option(WITH_GPU "Compile PaddlePaddle with NVIDIA GPU" ${CUDA_FOUND}) option(WITH_AVX "Compile PaddlePaddle with AVX intrinsics" ${AVX_FOUND}) -option(WITH_MKLDNN "Compile PaddlePaddle with mkl-dnn support." ${AVX_FOUND}) -option(WITH_MKLML "Compile PaddlePaddle with mklml package." ${AVX_FOUND}) +option(WITH_MKLDNN "Compile PaddlePaddle with mkl-dnn support." OFF) +option(WITH_MKLML "Compile PaddlePaddle with mklml package." OFF) option(WITH_DSO "Compile PaddlePaddle with dynamic linked CUDA" ON) option(WITH_TESTING "Compile PaddlePaddle with unit testing" ON) option(WITH_SWIG_PY "Compile PaddlePaddle with inference api" ON)