From 08968c2744c3b71ce950fec76e94b5d200ea2e55 Mon Sep 17 00:00:00 2001 From: dengwentao Date: Tue, 21 Apr 2020 10:49:59 +0800 Subject: [PATCH] modify tvm build --- CMakeLists.txt | 2 - cmake/external_libs/dmlc_core.cmake | 2 +- cmake/external_libs/tvm_gpu.cmake | 13 +- cmake/package.cmake | 12 +- cmake/utils.cmake | 28 +++-- mindspore/ccsrc/CMakeLists.txt | 111 ------------------ .../patch/incubator-tvm/CMakeLists.txt | 100 ++++++++++++++++ .../patch/incubator-tvm/find_library.patch | 8 +- 8 files changed, 145 insertions(+), 131 deletions(-) create mode 100644 third_party/patch/incubator-tvm/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 46804c8dde0..7dceca7ad7d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,6 @@ cmake_minimum_required(VERSION 3.14) project (MindSpore) - include(${CMAKE_SOURCE_DIR}/cmake/options.cmake) - set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/") if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") diff --git a/cmake/external_libs/dmlc_core.cmake b/cmake/external_libs/dmlc_core.cmake index 386a52429d4..e07df83fd6e 100644 --- a/cmake/external_libs/dmlc_core.cmake +++ b/cmake/external_libs/dmlc_core.cmake @@ -1,4 +1,4 @@ -mindspore_add_pkg(dmlc_core +mindspore_add_pkg(dmlc-core VER 0.3 HEAD_ONLY ./ URL https://github.com/dmlc/dmlc-core/archive/808f485387f9a03f78fa9f1159f387d0d91b7a28.zip diff --git a/cmake/external_libs/tvm_gpu.cmake b/cmake/external_libs/tvm_gpu.cmake index 57a045cb035..834e2d159df 100644 --- a/cmake/external_libs/tvm_gpu.cmake +++ b/cmake/external_libs/tvm_gpu.cmake @@ -2,7 +2,14 @@ set(incubator_tvm_gpu_CXXFLAGS "-D_FORTIFY_SOURCE=2 -O2") set(incubator_tvm_gpu_CFLAGS "-D_FORTIFY_SOURCE=2 -O2") mindspore_add_pkg(incubator_tvm_gpu VER 0.6.0 - HEAD_ONLY ./ + LIBS tvm URL https://github.com/apache/incubator-tvm/archive/v0.6.0.tar.gz - MD5 9cbbd32545a776023acabbba270449fe) - + MD5 9cbbd32545a776023acabbba270449fe + CUSTOM_CMAKE ${CMAKE_SOURCE_DIR}/third_party/patch/incubator-tvm/ + SUBMODULES ${dlpack_DIRPATH} ${dmlc-core_DIRPATH} ${rang_DIRPATH} + SOURCEMODULES topi/python/topi python/tvm + PATCHES ${CMAKE_SOURCE_DIR}/third_party/patch/incubator-tvm/find_library.patch + ${CMAKE_SOURCE_DIR}/third_party/patch/incubator-tvm/include.patch + ${CMAKE_SOURCE_DIR}/third_party/patch/incubator-tvm/src_pass.patch + CMAKE_OPTION " ") +add_library(mindspore::tvm ALIAS incubator_tvm_gpu::tvm) \ No newline at end of file diff --git a/cmake/package.cmake b/cmake/package.cmake index 531dff29ca7..d35ce0463b9 100644 --- a/cmake/package.cmake +++ b/cmake/package.cmake @@ -191,11 +191,17 @@ if (ENABLE_GPU) DESTINATION ${INSTALL_PY_DIR}/../ COMPONENT mindspore ) - if (EXISTS ${CMAKE_BINARY_DIR}/incubator-tvm) + if (EXISTS ${incubator_tvm_gpu_ROOT}) + file(GLOB_RECURSE GLOG_LIB_LIST ${incubator_tvm_gpu_LIBPATH}/lib*) + install( + FILES ${GLOG_LIB_LIST} + DESTINATION ${INSTALL_LIB_DIR} + COMPONENT mindspore + ) install( DIRECTORY - ${CMAKE_BINARY_DIR}/incubator-tvm/topi/python/topi - ${CMAKE_BINARY_DIR}/incubator-tvm/python/tvm + ${incubator_tvm_gpu_ROOT}/topi/python/topi + ${incubator_tvm_gpu_ROOT}/python/tvm DESTINATION ${INSTALL_PY_DIR}/../_akg COMPONENT mindspore ) diff --git a/cmake/utils.cmake b/cmake/utils.cmake index 894a0de1b8d..f0a5dc594c5 100644 --- a/cmake/utils.cmake +++ b/cmake/utils.cmake @@ -205,8 +205,8 @@ set(MS_FIND_NO_DEFAULT_PATH ${MS_FIND_NO_DEFAULT_PATH} PARENT_SCOPE) function(mindspore_add_pkg pkg_name ) set(options ) - set(oneValueArgs URL MD5 GIT_REPOSITORY GIT_TAG VER EXE DIR HEAD_ONLY CMAKE_PATH RELEASE LIB_PATH) - set(multiValueArgs CMAKE_OPTION LIBS PRE_CONFIGURE_COMMAND CONFIGURE_COMMAND BUILD_OPTION INSTALL_INCS INSTALL_LIBS PATCHES) + set(oneValueArgs URL MD5 GIT_REPOSITORY GIT_TAG VER EXE DIR HEAD_ONLY CMAKE_PATH RELEASE LIB_PATH CUSTOM_CMAKE) + set(multiValueArgs CMAKE_OPTION LIBS PRE_CONFIGURE_COMMAND CONFIGURE_COMMAND BUILD_OPTION INSTALL_INCS INSTALL_LIBS PATCHES SUBMODULES SOURCEMODULES) cmake_parse_arguments(PKG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) if (NOT PKG_LIB_PATH) @@ -270,11 +270,17 @@ function(mindspore_add_pkg pkg_name ) endif () if (NOT PKG_DIR) - if (PKG_GIT_REPOSITORY) - __download_pkg_with_git(${pkg_name} ${PKG_GIT_REPOSITORY} ${PKG_GIT_TAG} ${PKG_MD5}) - else() + if (PKG_GIT_REPOSITORY) + __download_pkg_with_git(${pkg_name} ${PKG_GIT_REPOSITORY} ${PKG_GIT_TAG} ${PKG_MD5}) + else() __download_pkg(${pkg_name} ${PKG_URL} ${PKG_MD5}) - endif() + endif() + foreach(_SUBMODULE_FILE ${PKG_SUBMODULES}) + STRING( REGEX REPLACE "(.+)_(.+)" "\\1" _SUBMODEPATH ${_SUBMODULE_FILE}) + STRING( REGEX REPLACE "(.+)/(.+)" "\\2" _SUBMODENAME ${_SUBMODEPATH}) + file(GLOB ${pkg_name}_INSTALL_SUBMODULE ${_SUBMODULE_FILE}/*) + file(COPY ${${pkg_name}_INSTALL_SUBMODULE} DESTINATION ${${pkg_name}_SOURCE_DIR}/3rdparty/${_SUBMODENAME}) + endforeach (_SUBMODULE_FILE) else() set(${pkg_name}_SOURCE_DIR ${PKG_DIR}) endif () @@ -294,12 +300,20 @@ function(mindspore_add_pkg pkg_name ) message(FATAL_ERROR "Failed patch: ${_LF_PATCH_FILE}") endif() endforeach(_PATCH_FILE) - + foreach(_SOURCE_DIR ${PKG_SOURCEMODULES}) + file(GLOB ${pkg_name}_INSTALL_SOURCE ${${pkg_name}_SOURCE_DIR}/${_SOURCE_DIR}/*) + file(COPY ${${pkg_name}_INSTALL_SOURCE} DESTINATION ${${pkg_name}_BASE_DIR}/${_SOURCE_DIR}/) + endforeach (_SUBMODULE_FILE) file(LOCK ${${pkg_name}_BASE_DIR} DIRECTORY GUARD FUNCTION RESULT_VARIABLE ${pkg_name}_LOCK_RET TIMEOUT 600) if(NOT ${pkg_name}_LOCK_RET EQUAL "0") message(FATAL_ERROR "error! when try lock ${${pkg_name}_BASE_DIR} : ${${pkg_name}_LOCK_RET}") endif() + if (PKG_CUSTOM_CMAKE) + file(GLOB ${pkg_name}_cmake ${PKG_CUSTOM_CMAKE}/CMakeLists.txt) + file(COPY ${${pkg_name}_cmake} DESTINATION ${${pkg_name}_SOURCE_DIR}) + endif () + if(${pkg_name}_SOURCE_DIR) if (PKG_HEAD_ONLY) file(GLOB ${pkg_name}_SOURCE_SUBDIRS ${${pkg_name}_SOURCE_DIR}/*) diff --git a/mindspore/ccsrc/CMakeLists.txt b/mindspore/ccsrc/CMakeLists.txt index 9b615b0dadf..eb33de1c4bd 100644 --- a/mindspore/ccsrc/CMakeLists.txt +++ b/mindspore/ccsrc/CMakeLists.txt @@ -394,117 +394,6 @@ if(USE_GLOG) target_link_libraries(_c_expression PRIVATE mindspore::glog) endif() -if(ENABLE_GPU) - execute_process(COMMAND bash ${CMAKE_SOURCE_DIR}/third_party/apply_patches.sh - ${CMAKE_BINARY_DIR} - ${dlpack_DIRPATH} - ${dmlc_core_DIRPATH} - ${rang_DIRPATH} - ${incubator_tvm_gpu_DIRPATH}) - set(TVM_DIR "${CMAKE_BINARY_DIR}/incubator-tvm") - # Utility functions - include(${TVM_DIR}/cmake/util/Util.cmake) - include(${TVM_DIR}/cmake/util/FindCUDA.cmake) - - # include directories - include_directories(AFTER "${TVM_DIR}/include") - include_directories(AFTER "${TVM_DIR}/src") - include_directories(AFTER "${TVM_DIR}") - include_directories(AFTER "${TVM_DIR}/src/schedule") - - include_directories(AFTER "${TVM_DIR}/3rdparty/dmlc-core/include") - include_directories(AFTER "${TVM_DIR}/3rdparty/dlpack/include") - include_directories(AFTER "${TVM_DIR}/3rdparty/compiler-rt") - include_directories(AFTER "${TVM_DIR}/3rdparty/rang/include") - - # lib contain dlopen and dlclose - set(TVM_RUNTIME_LINKER_LIBS ${CMAKE_DL_LIBS}) - - # add source group - file(GLOB_RECURSE GROUP_SOURCE "${TVM_DIR}/src/*.cc" "src/*.cc") - file(GLOB_RECURSE GROUP_INCLUDE "${TVM_DIR}/src/*.h" - "${TVM_DIR}/include/*.h" "src/*.h" "include/*.h") - assign_source_group("Source" ${GROUP_SOURCE}) - assign_source_group("Include" ${GROUP_INCLUDE}) - - file(GLOB COMPILER_SRCS - "pre_activate/gpu/*.cc" - ${TVM_DIR}/src/api/*.cc - ${TVM_DIR}/src/arithmetic/*.cc - ${TVM_DIR}/src/autotvm/*.cc - ${TVM_DIR}/src/codegen/*.cc - ${TVM_DIR}/src/lang/*.cc - ${TVM_DIR}/src/pass/*.cc - ${TVM_DIR}/src/op/*.cc - ${TVM_DIR}/src/node/*.cc - ${TVM_DIR}/src/schedule/*.cc - ${TVM_DIR}/src/runtime/*.cc - ${TVM_DIR}/src/runtime/vm/*.cc - ${TVM_DIR}/src/runtime/vm/profiler/*.cc - ${TVM_DIR}/src/codegen/stackvm/*.cc) - - file(GLOB_RECURSE RELAY_SRCS ${TVM_DIR}/src/relay/*.cc) - list(APPEND COMPILER_SRCS ${RELAY_SRCS}) - - file(GLOB DATATYPE_SRCS ${TVM_DIR}/src/codegen/datatype/*.cc) - list(APPEND COMPILER_SRCS ${DATATYPE_SRCS}) - - file(GLOB COMPILER_VERILOG_SRCS ${TVM_DIR}/src/codegen/verilog/*.cc) - list(APPEND COMPILER_SRCS ${COMPILER_VERILOG_SRCS}) - - file(GLOB TOPI_SRCS ${TVM_DIR}/topi/src/*.cc) - - file(GLOB RUNTIME_SRCS - ${TVM_DIR}/src/runtime/*.cc - ${TVM_DIR}/src/runtime/vm/*.cc - ${TVM_DIR}/src/runtime/stub/*.cc - ${TVM_DIR}/src/runtime/stackvm/*.cc) - - - file(GLOB COMPILER_OFF_SRCS - ${TVM_DIR}/src/codegen/opt/build_*_off.cc) - set(USE_CUDA "OFF") - if(ENABLE_GPU) - list(REMOVE_ITEM COMPILER_OFF_SRCS - ${TVM_DIR}/src/codegen/opt/build_cuda_off.cc) - set(USE_CUDA "ON") - endif() - list(APPEND COMPILER_SRCS ${COMPILER_OFF_SRCS}) - # Module rules - include(${TVM_DIR}/cmake/modules/CUDA.cmake) - - set(CMAKE_C_FLAGS_AKG -pipe -Wall -fPIC -fstack-protector-all) - set(CMAKE_C_FLAGS_AKG ${CMAKE_C_FLAGS_AKG} -Wl,-z,relro,-z,now,-z,noexecstack) - - set(CMAKE_CXX_FLAGS_AKG -std=c++11 -pipe -Wall -fPIC -fstack-protector-all) - set(CMAKE_CXX_FLAGS_AKG ${CMAKE_CXX_FLAGS_AKG} -Wl,-z,relro,-z,now,-z,noexecstack) - - if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") - message("-- Build in Debug mode") - set(CMAKE_C_FLAGS_AKG ${CMAKE_C_FLAGS_AKG} -O0 -g -rdynamic) - set(CMAKE_CXX_FLAGS_AKG ${CMAKE_CXX_FLAGS_AKG} -O0 -g -rdynamic) - else() - message("-- Build in Release mode") - set(CMAKE_C_FLAGS_AKG ${CMAKE_C_FLAGS_AKG} -O2 -Werror) - set(CMAKE_CXX_FLAGS_AKG ${CMAKE_CXX_FLAGS_AKG} -O2 -Werror) - endif() - if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION - VERSION_GREATER 7.0) - set(CMAKE_CXX_FLAGS_AKG ${CMAKE_CXX_FLAGS_AKG} -faligned-new) - endif() - - add_library(akg OBJECT ${COMPILER_SRCS} ${RUNTIME_SRCS} ${TOPI_SRCS}) - - target_link_libraries(akg ${TVM_LINKER_LIBS} ${TVM_RUNTIME_LINKER_LIBS}) - target_compile_options(akg PRIVATE - $<$:${CMAKE_C_FLAGS_AKG}> - $<$:${CMAKE_CXX_FLAGS_AKG}>) - target_include_directories(akg PRIVATE "${TVM_DIR}/topi/include") - - add_dependencies(_c_expression akg) - target_link_libraries(_c_expression PRIVATE akg) -endif() - if(ENABLE_DUMP_PROTO) target_link_libraries(_c_expression PRIVATE mindspore::protobuf) endif() diff --git a/third_party/patch/incubator-tvm/CMakeLists.txt b/third_party/patch/incubator-tvm/CMakeLists.txt new file mode 100644 index 00000000000..d8964579cdc --- /dev/null +++ b/third_party/patch/incubator-tvm/CMakeLists.txt @@ -0,0 +1,100 @@ +cmake_minimum_required(VERSION 3.2) +project(tvm C CXX) +set(TVM_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +# Utility functions +include(${TVM_DIR}/cmake/util/Util.cmake) +include(${TVM_DIR}/cmake/util/FindCUDA.cmake) + +# include directories +include_directories(AFTER "${TVM_DIR}/include") +include_directories(AFTER "${TVM_DIR}/src") +include_directories(AFTER "${TVM_DIR}") +include_directories(AFTER "${TVM_DIR}/src/schedule") + +include_directories(AFTER "${TVM_DIR}/3rdparty/dmlc-core/include") +include_directories(AFTER "${TVM_DIR}/3rdparty/dlpack/include") +include_directories(AFTER "${TVM_DIR}/3rdparty/compiler-rt") +include_directories(AFTER "${TVM_DIR}/3rdparty/rang/include") + +# lib contain dlopen and dlclose +set(TVM_RUNTIME_LINKER_LIBS ${CMAKE_DL_LIBS}) + +# add source group +file(GLOB_RECURSE GROUP_SOURCE "${TVM_DIR}/src/*.cc" "src/*.cc") +file(GLOB_RECURSE GROUP_INCLUDE "${TVM_DIR}/src/*.h" + "${TVM_DIR}/include/*.h" "src/*.h" "include/*.h") +assign_source_group("Source" ${GROUP_SOURCE}) +assign_source_group("Include" ${GROUP_INCLUDE}) + +file(GLOB COMPILER_SRCS + "pre_activate/gpu/*.cc" + ${TVM_DIR}/src/api/*.cc + ${TVM_DIR}/src/arithmetic/*.cc + ${TVM_DIR}/src/autotvm/*.cc + ${TVM_DIR}/src/codegen/*.cc + ${TVM_DIR}/src/lang/*.cc + ${TVM_DIR}/src/pass/*.cc + ${TVM_DIR}/src/op/*.cc + ${TVM_DIR}/src/node/*.cc + ${TVM_DIR}/src/schedule/*.cc + ${TVM_DIR}/src/runtime/*.cc + ${TVM_DIR}/src/runtime/vm/*.cc + ${TVM_DIR}/src/runtime/vm/profiler/*.cc + ${TVM_DIR}/src/codegen/stackvm/*.cc) + +file(GLOB_RECURSE RELAY_SRCS ${TVM_DIR}/src/relay/*.cc) +list(APPEND COMPILER_SRCS ${RELAY_SRCS}) + +file(GLOB DATATYPE_SRCS ${TVM_DIR}/src/codegen/datatype/*.cc) +list(APPEND COMPILER_SRCS ${DATATYPE_SRCS}) + +file(GLOB COMPILER_VERILOG_SRCS ${TVM_DIR}/src/codegen/verilog/*.cc) +list(APPEND COMPILER_SRCS ${COMPILER_VERILOG_SRCS}) + +file(GLOB TOPI_SRCS ${TVM_DIR}/topi/src/*.cc) + +file(GLOB RUNTIME_SRCS + ${TVM_DIR}/src/runtime/*.cc + ${TVM_DIR}/src/runtime/vm/*.cc + ${TVM_DIR}/src/runtime/stub/*.cc + ${TVM_DIR}/src/runtime/stackvm/*.cc) + + +file(GLOB COMPILER_OFF_SRCS + ${TVM_DIR}/src/codegen/opt/build_*_off.cc) + +list(REMOVE_ITEM COMPILER_OFF_SRCS + ${TVM_DIR}/src/codegen/opt/build_cuda_off.cc) +set(USE_CUDA "ON") +list(APPEND COMPILER_SRCS ${COMPILER_OFF_SRCS}) +# Module rules +include(${TVM_DIR}/cmake/modules/CUDA.cmake) + +set(CMAKE_C_FLAGS_AKG -pipe -Wall -fPIC -fstack-protector-all) +set(CMAKE_C_FLAGS_AKG ${CMAKE_C_FLAGS_AKG} -Wl,-z,relro,-z,now,-z,noexecstack) + +set(CMAKE_CXX_FLAGS_AKG -std=c++11 -pipe -Wall -fPIC -fstack-protector-all) +set(CMAKE_CXX_FLAGS_AKG ${CMAKE_CXX_FLAGS_AKG} -Wl,-z,relro,-z,now,-z,noexecstack) + +if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") + message("-- Build in Debug mode") + set(CMAKE_C_FLAGS_AKG ${CMAKE_C_FLAGS_AKG} -O0 -g -rdynamic) + set(CMAKE_CXX_FLAGS_AKG ${CMAKE_CXX_FLAGS_AKG} -O0 -g -rdynamic) +else() + message("-- Build in Release mode") + set(CMAKE_C_FLAGS_AKG ${CMAKE_C_FLAGS_AKG} -O2 -Werror) + set(CMAKE_CXX_FLAGS_AKG ${CMAKE_CXX_FLAGS_AKG} -O2 -Werror) +endif() +if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION + VERSION_GREATER 7.0) + set(CMAKE_CXX_FLAGS_AKG ${CMAKE_CXX_FLAGS_AKG} -faligned-new) +endif() + +add_library(tvm SHARED ${COMPILER_SRCS} ${RUNTIME_SRCS} ${TOPI_SRCS}) + +target_link_libraries(tvm ${TVM_LINKER_LIBS} ${TVM_RUNTIME_LINKER_LIBS}) +target_compile_options(tvm PRIVATE + $<$:${CMAKE_C_FLAGS_AKG}> + $<$:${CMAKE_CXX_FLAGS_AKG}>) +target_include_directories(tvm PRIVATE "${TVM_DIR}/topi/include") +install(TARGETS tvm) \ No newline at end of file diff --git a/third_party/patch/incubator-tvm/find_library.patch b/third_party/patch/incubator-tvm/find_library.patch index e54df2c7cfc..f7b2f9af0a9 100644 --- a/third_party/patch/incubator-tvm/find_library.patch +++ b/third_party/patch/incubator-tvm/find_library.patch @@ -18,11 +18,11 @@ - lib_path = libinfo.find_lib_path() + """Load library by searching possible path.""" + pwd = os.path.dirname(os.path.realpath(__file__)) -+ path = os.path.realpath(pwd+"/../../../mindspore") ++ path = os.path.realpath(pwd+"/../../../mindspore/lib") + lib_path = [] + files = os.listdir(path) + for f in files: -+ if f.startswith("_c_expression.") and f.endswith(".so"): ++ if f.startswith("libtvm.") and f.endswith(".so"): + lib_path.append(path+"/"+f) + break + if not lib_path: @@ -56,11 +56,11 @@ diff -Npur tvm/topi/python/topi/cpp/impl.py tvm_new/topi/python/topi/cpp/impl.py - return None, None + """Load library by searching possible path.""" + pwd = os.path.dirname(os.path.realpath(__file__)) -+ path = os.path.realpath(pwd+"/../../../mindspore") ++ path = os.path.realpath(pwd+"/../../../mindspore/lib") + lib_path = [] + files = os.listdir(path) + for f in files: -+ if f.startswith("_c_expression.") and f.endswith(".so"): ++ if f.startswith("libtvm.") and f.endswith(".so"): + lib_path.append(path+"/"+f) + break + if not lib_path: