Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Interface in CMake for diopi_impl #398

Merged
merged 5 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions dipu/scripts/ci/droplet/ci_droplet_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@ function config_dipu_droplet_cmake() {
}

function config_all_droplet_cmake() {
mkdir -p build && cd ./build && rm -rf ./*
rm -rf ./build
mkdir -p build
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="none" \
-DPYTHON_INCLUDE_DIR=${PYTHON_INCLUDE_DIR}
# -DCMAKE_C_FLAGS_DEBUG="-g -O0" \
# -DCMAKE_CXX_FLAGS_DEBUG="-g -O0"
cd ../

args=(
"-DCMAKE_BUILD_TYPE=Debug"
"-DDEVICE=${DIPU_DEVICE}"
"-DPYTORCH_DIR=${PYTORCH_DIR}"
"-DPYTHON_INCLUDE_DIR=${PYTHON_INCLUDE_DIR}" )
[[ ! -z "$DIOPI_ROOT" ]] && args+=( "-DWITH_DIOPI_LIBRARY=${DIOPI_ROOT}" )
[[ ! -z "$DIOPI_PATH" ]] && args+=( "-DWITH_DIOPI_INCLUDE=${DIOPI_PATH}/include" )
cmake -S . -B build "${args[@]}"
}

function build_diopi_lib() {
Expand Down
139 changes: 77 additions & 62 deletions dipu/third_party/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,75 +1,90 @@
#require the external project to build as target
include(ExternalProject)

add_library(diopi_impl SHARED IMPORTED GLOBAL)
#[[ DIOPI ]]

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()
#--------------------------------------------------------------------------------------------------
# define some shared and cached options
set(WITH_DIOPI_LIBRARY "INTERNAL" CACHE STRING
"Decide how to use DIOPI library, it could be one of (case-sensitive) \
'INTERNAL', 'DISABLE' or '/directory/of/an/external/DIOPI/library'. \
It's INTERNAL by default and uses the internal DIOPI library. \
When it's DISABLE, DIPU won't link DIOPI. \
When it's an absolute path, a user-provided DIOPI library will be used.")
set(WITH_DIOPI_INCLUDE "" CACHE PATH # use "PATH" type to provide a GUI file selector and convert relative path into absolute path
"Provide a directory of external DIOPI header files, or use internal DIOPI if is empty. \
When it's a directory, make sure WITH_DIOPI_LIBRARY is not INTERNAL.")

# locate DIOPI_LIBRARY_PATH
if(WITH_DIOPI_LIBRARY STREQUAL "INTERNAL")
set(DIOPI_LIBRARY_PATH "${CMAKE_CURRENT_SOURCE_DIR}/DIOPI/impl/lib")
# the default path is hard-coded and not safe, better to use other methods
elseif(WITH_DIOPI_LIBRARY STREQUAL "DISABLE")
set(DIOPI_LIBRARY_PATH "")
elseif(EXISTS "${WITH_DIOPI_LIBRARY}" AND IS_DIRECTORY "${WITH_DIOPI_LIBRARY}")
set(DIOPI_LIBRARY_PATH "${WITH_DIOPI_LIBRARY}")
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")
message(FATAL_ERROR
"WITH_DIOPI_LIBRARY is invalid ('${WITH_DIOPI_LIBRARY}'), "
"it should be one of 'INTERNAL', 'DISABLE' or an absolute path")
endif()

# locate DIOPI_INCLUDE_PATH
if (WITH_DIOPI_INCLUDE STREQUAL "")
set(DIOPI_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/DIOPI/proto/include")
# the default path is hard-coded and not safe, better to use other methods
wiryls marked this conversation as resolved.
Show resolved Hide resolved
elseif(EXISTS "${WITH_DIOPI_INCLUDE}" AND IS_DIRECTORY "${WITH_DIOPI_INCLUDE}" AND NOT WITH_DIOPI_LIBRARY STREQUAL "INTERNAL")
set(DIOPI_INCLUDE_PATH "${WITH_DIOPI_INCLUDE}")
else()
message(FATAL_ERROR
"WITH_DIOPI_INCLUDE is invalid ('${WITH_DIOPI_INCLUDE}'). "
"It should be empty or '/directory/of/DIOPI/headers' if WITH_DIOPI_LIBRARY is not INTERNAL.")
endif()

# compile DIOPI if use internal one
if (WITH_DIOPI_LIBRARY STREQUAL "INTERNAL")
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()
find_library(FIND_DIOPI_IMPL_LIB_RESULTS
NAMES libdiopi_impl
libdiopi_impl.so
diopi_impl
diopi_impl.so
HINTS ${DIOPI_IMPL_LIB_PATH}
REQUIRED)

message(STATUS "Building internal DIOPI with DIOPI_IMPL_OPT: ${DIOPI_IMPL_OPT}")
ExternalProject_Add(diopi_internal
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/DIOPI"
SOURCE_SUBDIR "impl"
BINARY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/DIOPI/build"
DOWNLOAD_COMMAND ""
CMAKE_ARGS
# note: as CMAKE_ARGS is a list, do not add quotes to args (such as "${DIOPI_IMPL_OPT}").
-DIMPL_OPT=${DIOPI_IMPL_OPT}
-DENABLE_COVERAGE=${USE_COVERAGE}
-DCMAKE_PREFIX_PATH=${DIOPI_CMAKE_PREFIX_PATH}
BUILD_BYPRODUCTS "${DIOPI_LIBRARY_PATH}/libdiopi_impl.so"
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)
endif()

message(STATUS "DIOPI_PROTO_PATH: ${DIOPI_PROTO_PATH}")
message(STATUS "DIOPI_IMPL_LIB_PATH: ${DIOPI_IMPL_LIB_PATH}")
message(STATUS "Using DIOPI_LIBRARY_PATH='${DIOPI_LIBRARY_PATH}', DIOPI_INCLUDE_PATH='${DIOPI_INCLUDE_PATH}'")

set_target_properties(
diopi_impl PROPERTIES IMPORTED_LOCATION
${DIOPI_IMPL_LIB})
target_include_directories(diopi_impl SYSTEM INTERFACE ${DIOPI_PROTO_PATH}/include)
add_library(diopi_impl INTERFACE)
target_include_directories(diopi_impl SYSTEM INTERFACE ${DIOPI_INCLUDE_PATH})
target_compile_definitions(diopi_impl INTERFACE DIOPI_ATTR_WEAK)
#-----------------------------------------------------------------------------------------------

if(NOT WITH_DIOPI_LIBRARY STREQUAL "DISABLE")
add_library(diopi_impl_lib SHARED IMPORTED)
target_link_options(diopi_impl_lib INTERFACE "LINKER:-no-as-needed")
set_target_properties(diopi_impl_lib PROPERTIES IMPORTED_LOCATION "${DIOPI_LIBRARY_PATH}/libdiopi_impl.so")

add_dependencies(diopi_impl_lib diopi_internal-install)
target_link_libraries(diopi_impl INTERFACE diopi_impl_lib)
endif()

#-------------------------add kineto as an external project ------------------------------------
#-------------------------use the local submodule(without download)-----------------------------
Expand Down