From 012f5fe6029cfde76e5114915904fa7f5998dcd5 Mon Sep 17 00:00:00 2001 From: HuayiL <442488254@qq.com> Date: Mon, 6 Nov 2023 16:53:35 +0800 Subject: [PATCH] One step install (#358) * This change is used to help users build and install DIPU from source code and pip in one step. * simplify the job of ci_nv_script.sh * fix a bug with the ENV DIPU_COMPILED_WITH_CXX11 * fix a bug with direct CMake and remove some unused import in setup.py * rebase to new main and add kineto in CMake * add DIOPI as an external project * add "import torch" in setup.py for setting related CMake args. Also pipeclean for "pip install ." * make some code improvement and precise dependency for ninja. * update build script for other device type * use two whitespace for indentation and add correct diopi build targets for other devices. * Split the CMake files to let the third_party/CMakeLists.txt build the third_party lib. * update kineto * code improvement based on git review conmments --------- Co-authored-by: caikun --- dipu/CMakeLists.txt | 135 ++++++++---------- dipu/pyproject.toml | 9 ++ dipu/scripts/ci/ascend/ci_ascend_script.sh | 19 ++- dipu/scripts/ci/camb/ci_camb_script.sh | 20 ++- dipu/scripts/ci/droplet/ci_droplet_script.sh | 28 +++- dipu/scripts/ci/nv/ci_nv_script.sh | 31 ++-- .../ci/topsrider/ci_topsrider_script.sh | 23 ++- dipu/setup.py | 61 +++++++- dipu/third_party/CMakeLists.txt | 118 +++++++++++++++ dipu/third_party/kineto | 2 +- dipu/torch_dipu/csrc_dipu/CMakeLists.txt | 10 +- 11 files changed, 341 insertions(+), 115 deletions(-) create mode 100644 dipu/pyproject.toml create mode 100644 dipu/third_party/CMakeLists.txt diff --git a/dipu/CMakeLists.txt b/dipu/CMakeLists.txt index ab8d6a51a..d94770c28 100644 --- a/dipu/CMakeLists.txt +++ b/dipu/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.10) project(TorchDIPU) +set(CMAKE_BUILD_TYPE "Release" CACHE STRING "default build type is Release") option(TESTS "Whether to build unit tests" OFF) option(LIBS "Whether to build dipu lib, default on" ON) @@ -21,6 +22,40 @@ list(APPEND DEVICE_DROPLET "DROPLET" "droplet") execute_process(COMMAND git rev-parse --short HEAD OUTPUT_VARIABLE DIPU_GIT_HASH) + +set(UsedVendor "") +set(DIOPI_IMPL_OPT "") +if (${DEVICE} IN_LIST DEVICE_CUDA) + set(USE_CUDA ON) + set(UsedVendor cuda) + set(DIOPI_IMPL_OPT "torch") +elseif (${DEVICE} IN_LIST DEVICE_CAMB) + set(USE_CAMB ON) + set(UsedVendor camb) + set(DIOPI_IMPL_OPT "camb") +elseif (${DEVICE} IN_LIST DEVICE_ASCEND) + set(USE_ASCEND ON) + set(UsedVendor ascend) + set(DIOPI_IMPL_OPT "ascend") +elseif (${DEVICE} IN_LIST DEVICE_TOPSRIDER) + set(USE_TOPSRIDER ON) + set(UsedVendor topsrider) + set(DIOPI_IMPL_OPT "tops") +elseif (${DEVICE} IN_LIST DEVICE_SUPA) + set(USE_SUPA ON) + set(UsedVendor supa) + set(DIOPI_IMPL_OPT "") + #SUPA DEVICE DOES NOT NEED TO BUILD DIOPI, so set the target to "" to control the workflow. +elseif (${DEVICE} IN_LIST DEVICE_DROPLET) + set(USE_DROPLET ON) + set(UsedVendor droplet) + set(DIOPI_IMPL_OPT "droplet") +else() + message(FATAL_ERROR "No implementation module is compiled, cmake requires option -DDEVICE=CAMB or CUDA or ASCEND or SUPA") +endif() + + + string(REGEX MATCH "[a-z0-9_]+" DIPU_GIT_HASH ${DIPU_GIT_HASH}) execute_process(COMMAND sh -c "git status --porcelain | egrep '^(M| M)' | wc -l" OUTPUT_VARIABLE DIPU_MODIFY_LEN) @@ -37,25 +72,32 @@ execute_process( "grep -Po 'diopi[a-zA-Z0-9]+(?=\\()' ${CMAKE_CURRENT_SOURCE_DIR}/scripts/autogen_diopi_wrapper/diopi_functions.yaml | sort -uf > ${CMAKE_CURRENT_SOURCE_DIR}/SupportedDiopiFunctions.txt" ) -execute_process( - COMMAND - sh -x -c - "python -c 'import torch, builtins; print(next(item[-4:-2] for item in dir(builtins) \ - if \"__pybind11_internals_v4_gcc_libstdcpp_cxxabi10\" in item))'" - OUTPUT_VARIABLE DIPU_ABI_V) -add_definitions(-fabi-version=${DIPU_ABI_V}) -message(STATUS "DIPU_ABI_V: ${DIPU_ABI_V}") +if(NOT DEFINED DIPU_ABI_V) + execute_process( + COMMAND + sh -x -c + "python -c 'import torch, builtins; print(next(item[-4:-2] for item in dir(builtins) \ + if \"__pybind11_internals_v4_gcc_libstdcpp_cxxabi10\" in item))'" + OUTPUT_VARIABLE DIPU_ABI_V) +endif() -execute_process( - COMMAND - sh -x -c - "python -c 'import torch;print(1 if torch.compiled_with_cxx11_abi() else 0)'" - OUTPUT_VARIABLE DIPU_COMPILED_WITH_CXX11_ABI) +if(NOT DEFINED DIPU_COMPILED_WITH_CXX11_ABI) + execute_process( + COMMAND + sh -x -c + "python -c 'import torch;print(1 if torch.compiled_with_cxx11_abi() else 0)'" + OUTPUT_VARIABLE DIPU_COMPILED_WITH_CXX11_ABI) +endif() + if(DIPU_COMPILED_WITH_CXX11_ABI GREATER 0) set(DIPU_COMPILED_WITH_CXX11_ABI 1) else() set(DIPU_COMPILED_WITH_CXX11_ABI 0) endif() + +add_definitions(-fabi-version=${DIPU_ABI_V}) +message(STATUS "DIPU_ABI_V: ${DIPU_ABI_V}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=${DIPU_COMPILED_WITH_CXX11_ABI}" ) @@ -67,43 +109,19 @@ execute_process( OUTPUT_VARIABLE PYTHON_INCLUDE_DIR) message(STATUS "PYTHON_INCLUDE_DIR: " ${PYTHON_INCLUDE_DIR}) -execute_process( - COMMAND sh -c "dirname $(python -c 'import torch;print(torch.__path__[0])')" - OUTPUT_VARIABLE PYTORCH_DIR) -string(REPLACE "\n" "" PYTORCH_DIR "${PYTORCH_DIR}") -string(REPLACE "\r" "" PYTORCH_DIR "${PYTORCH_DIR}") +if(NOT DEFINED PYTORCH_DIR) + execute_process( + COMMAND sh -c "dirname $(python -c 'import torch;print(torch.__path__[0])')" + OUTPUT_VARIABLE PYTORCH_DIR + OUTPUT_STRIP_TRAILING_WHITESPACE) +endif() + message(STATUS "PYTORCH_DIR: " ${PYTORCH_DIR}) # config include(cmake/BaseFuncions.cmake) _set_cpp_flags() -set(UsedVendor "") -if(${DEVICE} IN_LIST DEVICE_CUDA) - set(USE_CUDA ON) - set(UsedVendor cuda) -elseif(${DEVICE} IN_LIST DEVICE_CAMB) - set(USE_CAMB ON) - set(UsedVendor camb) -elseif(${DEVICE} IN_LIST DEVICE_ASCEND) - set(USE_ASCEND ON) - set(UsedVendor ascend) -elseif(${DEVICE} IN_LIST DEVICE_TOPSRIDER) - set(USE_TOPSRIDER ON) - set(UsedVendor topsrider) -elseif(${DEVICE} IN_LIST DEVICE_SUPA) - set(USE_SUPA ON) - set(UsedVendor supa) -elseif(${DEVICE} IN_LIST DEVICE_DROPLET) - set(USE_DROPLET ON) - set(UsedVendor droplet) -else() - message( - FATAL_ERROR - "No implementation module is compiled, cmake requires option -DDEVICE=CAMB or CUDA or ASCEND or SUPA" - ) -endif() - if(ENABLE_COVERAGE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage") @@ -130,34 +148,7 @@ list(APPEND TORCH_INCLUDE_DIRS ${PYTORCH_DIR}/torch/include/ include_directories(SYSTEM ${TORCH_INCLUDE_DIRS}) message(STATUS "Torch TORCH_INCLUDE_DIRS: ${TORCH_INCLUDE_DIRS}") -# diopi -set(WITH_DIOPI - "${PROJECT_SOURCE_DIR}/third_party/DIOPI" - CACHE STRING "DIOPI project root path") -# TODO(lljbash): use cache variable instead of env -if(EXISTS $ENV{DIOPI_PATH}) - set(DIOPI_PROTO_PATH $ENV{DIOPI_PATH}) -else() - set(DIOPI_PROTO_PATH "${WITH_DIOPI}/proto") -endif() -if(EXISTS $ENV{DIOPI_ROOT}) - set(DIOPI_IMPL_LIB_PATH $ENV{DIOPI_ROOT}) -else() - set(DIOPI_IMPL_LIB_PATH "${WITH_DIOPI}/impl/lib") -endif() -message(STATUS "DIOPI_PROTO_PATH: ${DIOPI_PROTO_PATH}") -message(STATUS "DIOPI_IMPL_LIB_PATH: ${DIOPI_IMPL_LIB_PATH}") -add_library(diopi_impl SHARED IMPORTED) -set_target_properties( - diopi_impl PROPERTIES IMPORTED_LOCATION - "${DIOPI_IMPL_LIB_PATH}/libdiopi_impl.so") -target_include_directories(diopi_impl INTERFACE "${DIOPI_PROTO_PATH}/include") -target_compile_definitions(diopi_impl INTERFACE DIOPI_ATTR_WEAK) - -include_directories(${PROJECT_SOURCE_DIR}/third_party/kineto/libkineto/third_party/fmt/include) -include_directories(${PROJECT_SOURCE_DIR}/third_party/kineto/libkineto/include) -include_directories(${PROJECT_SOURCE_DIR}/third_party/kineto/libkineto/src) -add_definitions(-DUSE_KINETO) +add_subdirectory(third_party) if(LIBS) add_subdirectory(torch_dipu/csrc_dipu) diff --git a/dipu/pyproject.toml b/dipu/pyproject.toml new file mode 100644 index 000000000..069068ed0 --- /dev/null +++ b/dipu/pyproject.toml @@ -0,0 +1,9 @@ +[build-system] +requires = [ + "setuptools>=42", + "scikit-build>=0.13", + "cmake>=3.1", + "ninja", + "pyyaml" +] +build-backend = "setuptools.build_meta" diff --git a/dipu/scripts/ci/ascend/ci_ascend_script.sh b/dipu/scripts/ci/ascend/ci_ascend_script.sh index d16c527c8..57d8dedc3 100644 --- a/dipu/scripts/ci/ascend/ci_ascend_script.sh +++ b/dipu/scripts/ci/ascend/ci_ascend_script.sh @@ -16,6 +16,14 @@ function config_dipu_ascend_cmake() { cd ../ } +function config_all_ascend_cmake() { + mkdir -p build && cd ./build && rm -rf ./* + cmake ../ -DCMAKE_BUILD_TYPE=Debug \ + -DDEVICE=ascend \ + -DWITH_DIOPI=INTERNAL + cd ../ +} + function build_dipu_lib() { echo "building dipu_lib:$(pwd)" echo "DIOPI_ROOT:${DIOPI_ROOT}" @@ -23,12 +31,17 @@ function build_dipu_lib() { cd build && make -j8 2>&1 | tee ./build1.log && cd .. } +function build_all() { + echo "building dipu_lib:$(pwd)" + echo "DIOPI_ROOT:${DIOPI_ROOT}" + config_all_ascend_cmake 2>&1 | tee ./build1.log + cd build && make -j8 2>&1 | tee ./build1.log && cd .. +} + case $1 in build_dipu) ( - bash scripts/ci/ci_build_third_party.sh - build_diopi_lib - build_dipu_lib + build_all ) \ || exit -1;; *) diff --git a/dipu/scripts/ci/camb/ci_camb_script.sh b/dipu/scripts/ci/camb/ci_camb_script.sh index f6df86d15..6db1ed97e 100644 --- a/dipu/scripts/ci/camb/ci_camb_script.sh +++ b/dipu/scripts/ci/camb/ci_camb_script.sh @@ -11,6 +11,14 @@ function config_dipu_camb_cmake() { cd ../ } +function config_all_camb_cmake() { + mkdir -p build && cd ./build && rm -rf ./* + cmake ../ -DCMAKE_BUILD_TYPE=Release \ + -DDEVICE=camb \ + -DENABLE_COVERAGE=${USE_COVERAGE} \ + -DWITH_DIOPI=INTERNAL + cd ../ +} function build_diopi_lib() { cd third_party/DIOPI/impl @@ -26,18 +34,22 @@ function build_dipu_lib() { cd build && make -j8 2>&1 | tee ./build.log && cd .. } +function build_all() { + echo "building dipu_lib:$(pwd)" + echo "DIOPI_ROOT:${DIOPI_ROOT}" + config_all_camb_cmake 2>&1 | tee ./cmake_camb.log + cd build && make -j8 2>&1 | tee ./build.log && cd .. +} + case $1 in build_dipu) ( - bash scripts/ci/ci_build_third_party.sh - build_diopi_lib - build_dipu_lib + build_all ) || exit -1;; build_diopi) build_diopi_lib || exit -1;; build_dipu_only) ( - bash scripts/ci/ci_build_third_party.sh build_dipu_lib ) || exit -1;; *) diff --git a/dipu/scripts/ci/droplet/ci_droplet_script.sh b/dipu/scripts/ci/droplet/ci_droplet_script.sh index de1443b74..b5fd13c72 100644 --- a/dipu/scripts/ci/droplet/ci_droplet_script.sh +++ b/dipu/scripts/ci/droplet/ci_droplet_script.sh @@ -16,6 +16,19 @@ function config_dipu_droplet_cmake() { cd ../ } +function config_all_droplet_cmake() { + mkdir -p build && cd ./build && rm -rf ./* + echo "config_dipu_nv_cmake PYTORCH_DIR: ${PYTORCH_DIR}" + echo "config_dipu_nv_cmake PYTHON_INCLUDE_DIR: ${PYTHON_INCLUDE_DIR}" + cmake ../ -DCMAKE_BUILD_TYPE=Debug \ + -DDEVICE=${DIPU_DEVICE} -DPYTORCH_DIR=${PYTORCH_DIR} \ + -DWITH_DIOPI=INTERNAL \ + -DPYTHON_INCLUDE_DIR=${PYTHON_INCLUDE_DIR} + # -DCMAKE_C_FLAGS_DEBUG="-g -O0" \ + # -DCMAKE_CXX_FLAGS_DEBUG="-g -O0" + cd ../ +} + function build_diopi_lib() { echo "开始编译DIOPI..." cd third_party/DIOPI/ @@ -39,6 +52,16 @@ function build_dipu_lib() { cd build && make -j8 2>&1 | tee ./build.log && cd .. } +function build_all() { + echo "building dipu_lib:$(pwd)" + echo "DIOPI_ROOT:${DIOPI_ROOT}" + echo "PYTORCH_DIR:${PYTORCH_DIR}" + echo "PYTHON_INCLUDE_DIR:${PYTHON_INCLUDE_DIR}" + export LIBRARY_PATH=$DIOPI_ROOT:$LIBRARY_PATH; + config_all_droplet_cmake 2>&1 | tee ./cmake_droplet.log + cd build && make -j8 2>&1 | tee ./build.log && cd .. +} + case $1 in build_diopi) ( @@ -47,14 +70,11 @@ case $1 in || exit -1;; build_dipu) ( - bash scripts/ci/ci_build_third_party.sh - build_diopi_lib - build_dipu_lib + build_all ) \ || exit -1;; build_dipu_only) ( - bash scripts/ci/ci_build_third_party.sh build_dipu_lib ) \ || exit -1;; diff --git a/dipu/scripts/ci/nv/ci_nv_script.sh b/dipu/scripts/ci/nv/ci_nv_script.sh index 48b945f5d..c8a23ad5a 100644 --- a/dipu/scripts/ci/nv/ci_nv_script.sh +++ b/dipu/scripts/ci/nv/ci_nv_script.sh @@ -2,7 +2,6 @@ set -e echo "pwd: $(pwd)" - function config_dipu_nv_cmake() { # export NCCL_ROOT="you nccl path should exist" @@ -13,15 +12,15 @@ function config_dipu_nv_cmake() { cd ../ } -function build_diopi_lib() { - cd third_party/DIOPI/ - cd impl - which cmake - sh scripts/build_impl.sh clean - sh scripts/build_impl.sh torch || exit -1 - - cd ../../.. - unset Torch_DIR +function config_all_nv_cmake() { + # export NCCL_ROOT="you nccl path should exist" + + mkdir -p build && cd ./build && rm -rf ./* + cmake ../ -DCMAKE_BUILD_TYPE=Release \ + -DDEVICE=cuda \ + -DENABLE_COVERAGE=${USE_COVERAGE} \ + -DWITH_DIOPI=INTERNAL + cd ../ } function build_dipu_lib() { @@ -32,17 +31,21 @@ function build_dipu_lib() { cd build && make -j8 2>&1 | tee ./build.log && cd .. } +function build_all(){ + echo "building dipu_lib:$(pwd)" + echo "DIOPI_ROOT:${DIOPI_ROOT}" + export DIOPI_BUILD_TESTRT=1 + config_all_nv_cmake 2>&1 | tee ./cmake_nv.log + cd build && make -j8 2>&1 | tee ./build.log && cd .. +} case $1 in build_dipu) ( - bash scripts/ci/ci_build_third_party.sh - build_diopi_lib - build_dipu_lib + build_all ) \ || exit -1;; build_dipu_only) ( - bash scripts/ci/ci_build_third_party.sh build_dipu_lib ) \ || exit -1;; diff --git a/dipu/scripts/ci/topsrider/ci_topsrider_script.sh b/dipu/scripts/ci/topsrider/ci_topsrider_script.sh index f4ef7c8e1..ef3855c84 100644 --- a/dipu/scripts/ci/topsrider/ci_topsrider_script.sh +++ b/dipu/scripts/ci/topsrider/ci_topsrider_script.sh @@ -11,14 +11,26 @@ function config_dipu_cmake() { cd ../ } - +function config_all_cmake() { + mkdir -p build && cd ./build && rm -rf ./* + cmake ../ -DCMAKE_BUILD_TYPE=Debug \ + -DDEVICE=tops \ + # -DCMAKE_C_FLAGS_DEBUG="-g -O0" \ + # -DCMAKE_CXX_FLAGS_DEBUG="-g -O0" + -DWITH_DIOPI=INTERNAL + cd ../ +} function build_dipu_lib() { - autogen_diopi_wrapper config_dipu_cmake cd build && make -j8 2>&1 | tee ./build1.log && cd .. } +function build_all() { + config_all_cmake + cd build && make -j8 2>&1 | tee ./build1.log && cd .. +} + function build_diopi_lib() { cd third_party/DIOPI/impl sh scripts/build_impl.sh tops || exit -1 @@ -34,14 +46,11 @@ function build_diopi_clean() { if [[ "$1" == "builddl" ]]; then build_dipu_lib elif [[ "$1" == "build_dipu" ]]; then - bash scripts/ci/ci_build_third_party.sh - build_dipu_lib + build_all elif [[ "$1" == "build_diopi" ]]; then build_diopi_lib elif [[ "$1" == "build" ]]; then - build_diopi_lib - bash scripts/ci/ci_build_third_party.sh - build_dipu_lib + build_all elif [[ "$1" == "clean" ]]; then build_diopi_clean fi diff --git a/dipu/setup.py b/dipu/setup.py index 22a0217f4..499fa8b9d 100644 --- a/dipu/setup.py +++ b/dipu/setup.py @@ -9,21 +9,74 @@ import sys import platform import setuptools -from setuptools import setup, distutils, Extension +from skbuild import setup BASE_DIR = os.path.dirname(os.path.abspath(__file__)) VERSION = '0.1' -def get_pytorch_dir(): +try: import torch - return os.path.dirname(os.path.abspath(torch.__file__)) +except: + sys.path.append(os.environ['PYTORCH_DIR']) + print("PYTHONPATH: "+str(sys.path)) +import torch +import builtins + +def get_DIPU_ABI_V(): + return (next(item[-4:-2] for item in dir(builtins) if "__pybind11_internals_v4_gcc_libstdcpp_cxxabi10" in item)) + +def get_DIPU_COMPILED_WITH_CXX11_ABI(): + return (1 if torch.compiled_with_cxx11_abi() else 0) + +def get_DIOPI_CMAKE_PREFIX_PATH(): + return torch.utils.cmake_prefix_path + +def get_PYTORCH_DIR(): + return os.path.dirname(torch.__path__[0]) + +def customized_cmake_args(): + cmake_args = list() + cmake_device = "cuda" + if(os.getenv("DIPU_DEVICE")): + cmake_device = os.getenv("DIPU_DEVICE") + cmake_args.append("-DCMAKE_BUILD_TYPE=Release") + cmake_args.append("-DDEVICE="+cmake_device) + cmake_args.append("-DENABLE_COVERAGE=${USE_COVERAGE}") + cmake_args.append("-DDIPU_ABI_V="+get_DIPU_ABI_V()) + cmake_args.append("-DDIPU_COMPILED_WITH_CXX11_ABI="+str(get_DIPU_COMPILED_WITH_CXX11_ABI())) + cmake_args.append("-DDIOPI_CMAKE_PREFIX_PATH="+get_DIOPI_CMAKE_PREFIX_PATH()) + cmake_args.append("-DPYTORCH_DIR="+get_PYTORCH_DIR()) + cmake_args.append("-DWITH_DIOPI=INTERNAL") + return cmake_args + +def torch_dipu_headers(): + headers_pattern = list() + headers_pattern.append("csrc_dipu/*.h") + headers_pattern.append("csrc_dipu/aten/*.h") + headers_pattern.append("csrc_dipu/base/*.h") + headers_pattern.append("csrc_dipu/binding/*.h") + headers_pattern.append("csrc_dipu/diopirt/*.h") + headers_pattern.append("csrc_dipu/profiler/*.h") + headers_pattern.append("csrc_dipu/utils/*.h") + headers_pattern.append("csrc_dipu/runtime/*.h") + headers_pattern.append("csrc_dipu/runtime/core/*.h") + headers_pattern.append("csrc_dipu/runtime/core/allocator/*.h") + headers_pattern.append("csrc_dipu/runtime/core/guardimpl/*.h") + headers_pattern.append("csrc_dipu/runtime/device/*.h") + headers_pattern.append("csrc_dipu/runtime/devproxy/*.h") + headers_pattern.append("csrc_dipu/runtime/distributed/*.h") + headers_pattern.append("csrc_dipu/vendor/*/*.h") + return headers_pattern setup( name="torch_dipu", version=VERSION, description='DIPU extension for PyTorch', url='https://github.com/DeepLink-org/dipu', - packages=["torch_dipu"], + packages=setuptools.find_packages(), + cmake_args=customized_cmake_args(), + cmake_install_target="all", + package_data = {'torch_dipu':["*.lib","*.so","*.pylib","../third_party/DIOPI/impl/lib/*.lib","../third_party/DIOPI/impl/lib/*.so","../third_party/DIOPI/impl/lib/*.pylib",]+torch_dipu_headers(),}, ext_modules=[ ], cmdclass={ diff --git a/dipu/third_party/CMakeLists.txt b/dipu/third_party/CMakeLists.txt new file mode 100644 index 000000000..78e788ecf --- /dev/null +++ b/dipu/third_party/CMakeLists.txt @@ -0,0 +1,118 @@ +#require the external project to build as target +include(ExternalProject) + +add_library(diopi_impl SHARED IMPORTED GLOBAL) + +if((${WITH_DIOPI} STREQUAL "INTERNAL") OR (${WITH_DIOPI} STREQUAL "internal")) + if(NOT ${DIOPI_IMPL_OPT} STREQUAL "") +#----------------------------------Build DIOPI submodule------------------------------------------- + if(NOT DEFINED DIOPI_CMAKE_PREFIX_PATH) + execute_process( + COMMAND + sh -x -c + "python -c 'import torch;print(torch.utils.cmake_prefix_path)'" + OUTPUT_VARIABLE DIOPI_CMAKE_PREFIX_PATH + OUTPUT_STRIP_TRAILING_WHITESPACE) + endif() + + message(STATUS "building internal DIOPI") + set(DIOPI_SRC_PATH "${PROJECT_SOURCE_DIR}/third_party/DIOPI") + set(DIOPI_BUILD_PATH "${DIOPI_SRC_PATH}/build") + set(DIOPI_PROTO_PATH "${DIOPI_SRC_PATH}/proto") + set(DIOPI_IMPL_LIB_PATH "${DIOPI_SRC_PATH}/impl/lib") + set(DIOPI_IMPL_LIB "${DIOPI_IMPL_LIB_PATH}/libdiopi_impl.so") + ExternalProject_Add(diopi_internal + SOURCE_DIR ${DIOPI_SRC_PATH} + SOURCE_SUBDIR impl + BINARY_DIR ${DIOPI_BUILD_PATH} + DOWNLOAD_COMMAND "" + CMAKE_ARGS -DIMPL_OPT=${DIOPI_IMPL_OPT} + -DENABLE_COVERAGE=${USE_COVERAGE} + -DCMAKE_PREFIX_PATH=${DIOPI_CMAKE_PREFIX_PATH} + BUILD_BYPRODUCTS ${DIOPI_IMPL_LIB} + INSTALL_COMMAND cmake -E echo "Skipping install step for diopi_internal." + ) + ## The following code is a work around to avoid make file to run multiple externalProject-build when using make -j N + ExternalProject_Add_StepTargets(diopi_internal configure build install) + ExternalProject_Add_StepDependencies(diopi_internal install diopi_internal-build) + ExternalProject_Add_StepDependencies(diopi_internal build diopi_internal-configure) + add_dependencies(diopi_impl diopi_internal-install) + endif() +#-------------------------------------------------------------------------------------------------- +else() + if(EXISTS $ENV{DIOPI_PATH}) + set(DIOPI_PROTO_PATH $ENV{DIOPI_PATH}) + else() + set(DIOPI_PROTO_PATH "${WITH_DIOPI}/proto") + endif() + if(EXISTS $ENV{DIOPI_ROOT}) + set(DIOPI_IMPL_LIB_PATH $ENV{DIOPI_ROOT}) + set(DIOPI_IMPL_LIB "${DIOPI_IMPL_LIB_PATH}/libdiopi_impl.so") + else() + set(DIOPI_IMPL_LIB_PATH "${WITH_DIOPI}/impl/lib") + set(DIOPI_IMPL_LIB "${DIOPI_IMPL_LIB_PATH}/libdiopi_impl.so") + endif() + find_library(FIND_DIOPI_IMPL_LIB_RESULTS + NAMES libdiopi_impl + libdiopi_impl.so + diopi_impl + diopi_impl.so + HINTS ${DIOPI_IMPL_LIB_PATH} + REQUIRED) +endif() + +message(STATUS "DIOPI_PROTO_PATH: ${DIOPI_PROTO_PATH}") +message(STATUS "DIOPI_IMPL_LIB_PATH: ${DIOPI_IMPL_LIB_PATH}") + +set_target_properties( + diopi_impl PROPERTIES IMPORTED_LOCATION + ${DIOPI_IMPL_LIB}) +target_include_directories(diopi_impl SYSTEM INTERFACE ${DIOPI_PROTO_PATH}/include) +target_compile_definitions(diopi_impl INTERFACE DIOPI_ATTR_WEAK) +#----------------------------------------------------------------------------------------------- + +#-------------------------add kineto as an external project ------------------------------------ +#-------------------------use the local submodule(without download)----------------------------- +set(KINETO_BUILD_TARGET "kineto_internal-build") +set(KINETO_SRC_PATH "${PROJECT_SOURCE_DIR}/third_party/kineto") +set(KINETO_BUILD_PATH "${KINETO_SRC_PATH}/build") +ExternalProject_Add(kineto_internal + PREFIX "third_party/kineto/" + SOURCE_DIR ${KINETO_SRC_PATH} + SOURCE_SUBDIR libkineto + BINARY_DIR ${KINETO_BUILD_PATH} + DOWNLOAD_COMMAND "" + CMAKE_ARGS "-DKINETO_BUILD_TESTS=OFF" + "-DKINETO_USE_DEVICE_ACTIVITY=ON" + "-DKINETO_COMPILED_WITH_CXX11_ABI=${DIPU_COMPILED_WITH_CXX11_ABI}" + BUILD_BYPRODUCTS "${KINETO_BUILD_PATH}/fmt/libfmt.a" + "${KINETO_BUILD_PATH}/libkineto.a" + INSTALL_COMMAND cmake -E echo "Skipping install step for kineto_internal." +) +## The following code is a work around to avoid make file to run multiple externalProject-build when using make -j N +ExternalProject_Add_StepTargets(kineto_internal configure build) +ExternalProject_Add_StepDependencies(kineto_internal build kineto_internal-configure) +#----------------------------------------------------------------------------------------------- +add_library(kineto_lib STATIC IMPORTED) +add_library(kineto_fmt STATIC IMPORTED) +add_dependencies(kineto_lib kineto_internal-build) +add_dependencies(kineto_fmt kineto_internal-build) +set_target_properties( + kineto_lib PROPERTIES IMPORTED_LOCATION + "${KINETO_BUILD_PATH}/libkineto.a") +set_target_properties( + kineto_fmt PROPERTIES IMPORTED_LOCATION + "${KINETO_BUILD_PATH}/fmt/libfmt.a") +target_include_directories(kineto_fmt SYSTEM INTERFACE + ${KINETO_SRC_PATH}/libkineto/third_party/fmt/include +) +target_include_directories(kineto_lib SYSTEM INTERFACE + ${KINETO_SRC_PATH}/libkineto/include + ${KINETO_SRC_PATH}/libkineto/src + ${KINETO_SRC_PATH}/libkineto/third_party/fmt/include +) +add_library(kineto INTERFACE) +target_link_libraries(kineto INTERFACE kineto_fmt kineto_lib) +target_compile_definitions(kineto INTERFACE USE_KINETO) +#----------------------------------------------------------------------------------------------- + diff --git a/dipu/third_party/kineto b/dipu/third_party/kineto index 83dd677ef..eeebc7a1b 160000 --- a/dipu/third_party/kineto +++ b/dipu/third_party/kineto @@ -1 +1 @@ -Subproject commit 83dd677ef3fc754165400ab492c32600ab10d4e9 +Subproject commit eeebc7a1b7962d98e56c8505de0c1c35a6bf3f92 diff --git a/dipu/torch_dipu/csrc_dipu/CMakeLists.txt b/dipu/torch_dipu/csrc_dipu/CMakeLists.txt index e7344248c..46d49302e 100644 --- a/dipu/torch_dipu/csrc_dipu/CMakeLists.txt +++ b/dipu/torch_dipu/csrc_dipu/CMakeLists.txt @@ -80,11 +80,11 @@ add_library(${DIPU_LIB} SHARED ${SOURCE_FILES}) # link target_link_libraries(${DIPU_LIB} ${DIPU_VENDOR_LIB}) -target_link_libraries(${DIPU_LIB} ${PROJECT_SOURCE_DIR}/third_party/kineto/libkineto/build/fmt/libfmt.a) -target_link_libraries(${DIPU_LIB} ${PROJECT_SOURCE_DIR}/third_party/kineto/libkineto/build/libkineto.a) - -# target_link_libraries(${DIPU_LIB} -Wl,--no-as-needed diopi_impl -Wl,--as-needed) +target_link_libraries(${DIPU_LIB} kineto) target_link_libraries(${DIPU_LIB} diopi_impl) +# target_link_libraries(${DIPU_LIB} -Wl,--no-as-needed diopi_impl -Wl,--as-needed) + + target_link_libraries(${DIPU_LIB} c10 torch torch_cpu) set(CMAKE_THREAD_PREFER_PTHREAD ON) @@ -112,9 +112,7 @@ else() endif() add_custom_target(copy_include DEPENDS vendor_include) add_dependencies(${DIPU_LIB} copy_include) - add_dependencies(${DIPU_LIB} ${DIPU_AUTOGENED_KERNELS}) - # ---------------------- build bind in python ---------------------------------- file(GLOB BIND_SRC_FILES binding/Export*.cpp binding/patch*.cpp) set(BIND_FILES ${BIND_SRC_FILES})