forked from DeepLink-org/DIOPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ascend]Fdy change dyn load (DeepLink-org#1301)
Fdy change dyn load. --------- Co-authored-by: yangbofun <[email protected]> Co-authored-by: wiryls <[email protected]
- Loading branch information
Showing
16 changed files
with
465 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
function(diopi_use_adapter adaptor_dir diopi_impl_dir config_device base_device | ||
out_src_files) | ||
# NB: all augements passed by func parameters instead of global variables. | ||
file(GLOB ADAPTOR_TEMPLATE_CODE RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${adaptor_dir}/codegen/*.py) | ||
add_custom_target(adaptor_gen_dependency DEPENDS ${ADAPTOR_TEMPLATE_CODE}) | ||
set(ADAPTOR_CSRC_PATH "${ADAPTOR_DIR}/csrc") | ||
|
||
set(ADAPTER_GEN_FILES ${ADAPTOR_CSRC_PATH}/diopi_adaptor.cpp ${ADAPTOR_CSRC_PATH}/impl_functions.hpp) | ||
add_custom_target(adaptor_code_gen | ||
COMMAND python3 ${ADAPTOR_DIR}/codegen/gen.py --diopi_dir=${diopi_impl_dir}/../ --output_dir=${ADAPTOR_CSRC_PATH} --config_device=${config_device} --base_device=${base_device} | ||
BYPRODUCTS ${ADAPTER_GEN_FILES} | ||
DEPENDS adaptor_gen_dependency | ||
VERBATIM | ||
) | ||
list(APPEND ${out_src_files} ${ADAPTOR_CSRC_PATH}/convert.cpp ${ADAPTOR_CSRC_PATH}/diopi_adaptor.cpp ${ADAPTOR_CSRC_PATH}/composite_ops.cpp) | ||
set(${out_src_files} ${${out_src_files}} PARENT_SCOPE) | ||
endfunction() | ||
|
||
|
||
function(prep_dyn_load diopi_impl_dir device_impl) | ||
set(DYN_GEN_FILE ${CMAKE_BINARY_DIR}/src/impl/wrap_function.cpp) | ||
set(DYN_HELP_DIR ${diopi_impl_dir}/scripts/dyn_load_helper) | ||
file(GLOB DYN_GEN_DEPS ${DYN_HELP_DIR}/dyn_wrap_gen.py) | ||
|
||
add_custom_target(dyn_wrap_gen ALL | ||
COMMAND python ${DYN_HELP_DIR}/dyn_wrap_gen.py -o ${DYN_GEN_FILE} | ||
DEPENDS ${DYN_GEN_DEPS} | ||
BYPRODUCTS ${DYN_GEN_FILE} | ||
WORKING_DIRECTORY ${DYN_HELP_DIR}) | ||
set(DYN_SRC ${DYN_GEN_FILE} ${DYN_HELP_DIR}/dyn_helper.cpp) | ||
|
||
add_library(${device_impl} SHARED ${DYN_SRC}) | ||
target_link_libraries(${device_impl} -ldl) | ||
target_include_directories(${device_impl} PRIVATE ${DYN_HELP_DIR}) | ||
add_dependencies(${device_impl} dyn_wrap_gen) | ||
endfunction() | ||
|
||
function(handle_dyn_torch diopi_impl_dir real_impl torch_dir device_impl) | ||
add_custom_target(dyn_torch | ||
COMMAND ${diopi_impl_dir}/scripts/dyn_load_helper/dyn_torch_handler.sh patch_diopi | ||
${LIBRARY_OUTPUT_PATH} ${torch_dir}/lib | ||
DEPENDS ${real_impl} | ||
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} | ||
) | ||
message(STATUS "handle_dyn_torch with torch: ${torch_dir}") | ||
add_dependencies(${device_impl} dyn_torch) | ||
endfunction() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
macro(diopi_find_torch) | ||
execute_process( | ||
COMMAND sh -c "python -c 'import torch;print(torch.utils.cmake_prefix_path)'" | ||
OUTPUT_VARIABLE DIOPI_TORCH_CMAKE_PREFIX | ||
OUTPUT_STRIP_TRAILING_WHITESPACE) | ||
if(DIOPI_TORCH_CMAKE_PREFIX) | ||
# this config is appened to existing CMAKE_PREFIX_PATH and not overwrite | ||
# user provided CMAKE_PREFIX_PATH. | ||
list(APPEND CMAKE_PREFIX_PATH ${DIOPI_TORCH_CMAKE_PREFIX}) | ||
endif() | ||
message(STATUS "diopi CMAKE_PREFIX_PATH:${CMAKE_PREFIX_PATH}") | ||
|
||
|
||
find_package(Torch REQUIRED) | ||
if (Torch_FOUND) | ||
message(STATUS "TORCH_CXX_FLAGS: ${TORCH_CXX_FLAGS}") | ||
message(STATUS "TORCH_LIBRARIES: ${TORCH_LIBRARIES}") | ||
|
||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}") | ||
add_definitions(-DTORCH_VERSION_MAJOR=${Torch_VERSION_MAJOR}) | ||
add_definitions(-DTORCH_VERSION_MINOR=${Torch_VERSION_MINOR}) | ||
add_definitions(-DTORCH_VERSION_PATCH=${Torch_VERSION_PATCH}) | ||
add_definitions(-DTORCH_VERSION=${Torch_VERSION}) | ||
message(STATUS "Found Torch Version: ${Torch_VERSION}") | ||
endif() | ||
|
||
endmacro() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <dlfcn.h> | ||
|
||
#include <cstdio> | ||
#include <filesystem> | ||
#include <stdexcept> | ||
|
||
void* dynLoadFile(const char* diopiRealName) { | ||
namespace fs = std::filesystem; | ||
void* handle = dlopen(diopiRealName, RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND); | ||
if (!handle) { | ||
Dl_info info; | ||
if (dladdr(reinterpret_cast<void*>(dynLoadFile), &info) != 0 && info.dli_fname != nullptr) { | ||
fs::path fpath(info.dli_fname); | ||
auto diopiInLoader = fpath.parent_path().append(diopiRealName).string(); | ||
printf( | ||
"diopi dyload fail, seems LD_LIBRARAY_PATH not contains %s, try to load " | ||
"from loader current dir's %s \n", | ||
diopiRealName, | ||
diopiInLoader.c_str()); | ||
|
||
handle = dlopen(diopiInLoader.c_str(), RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND); | ||
} | ||
} | ||
if (!handle) { | ||
fprintf(stderr, | ||
"! please note that dynamic loaded diopi_impl.so need explictly link to it's \ | ||
diopi_rt (now is torch_dipu), so it cannot be used for diopi-test now \n"); | ||
fprintf(stderr, "%s \n", dlerror()); | ||
throw std::runtime_error("diopi_init err"); | ||
} | ||
return handle; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
void* dynLoadFile(const char* diopiRealName); |
Oops, something went wrong.