Skip to content

Commit 300dc31

Browse files
authored
yb/add python library, fix init, and reconstruct cmakelist (DeepLink-org#859)
* add python library, fix init, and reconstruct cmakelist
1 parent 011b74b commit 300dc31

File tree

3 files changed

+66
-45
lines changed

3 files changed

+66
-45
lines changed

impl/ascend/error.hpp

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* @file
3+
* @author DeepLink
4+
* @copyright (c) 2023, DeepLink.
5+
*/
6+
17
#ifndef IMPL_ASCEND_ERROR_HPP_
28
#define IMPL_ASCEND_ERROR_HPP_
39

impl/ascend/init.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @file
3+
* @author DeepLink
4+
* @copyright (c) 2024, DeepLink.
5+
*/
6+
7+
#include <Python.h>
8+
9+
#include <iostream>
10+
11+
class PyInterpreteInit {
12+
public:
13+
PyInterpreteInit() {
14+
if (!Py_IsInitialized()) {
15+
Py_Initialize();
16+
beInitedInDiopi_ = true;
17+
}
18+
}
19+
~PyInterpreteInit() {
20+
if (beInitedInDiopi_) {
21+
int exState = Py_FinalizeEx();
22+
if (exState == -1) {
23+
std::cerr << "Py_FinalizeEx failed in " << __FILE__ << ":" << __LINE__ << "in func:" << __FUNCTION__ << std::endl;
24+
}
25+
}
26+
}
27+
28+
private:
29+
bool beInitedInDiopi_ = false;
30+
};
31+
32+
class InitDiopi {
33+
public:
34+
InitDiopi() = default;
35+
36+
private:
37+
// init the module here.
38+
PyInterpreteInit pyInit_;
39+
};
40+
41+
InitDiopi initDiopi; // initialize diopi

impl/ascend_npu/CMakeLists.txt

+19-45
Original file line numberDiff line numberDiff line change
@@ -17,64 +17,37 @@ message(STATUS "op-plugin download done")
1717
add_custom_target(op_plugin_gen
1818
COMMAND cd ${op_plugin_SOURCE_DIR} && bash ./gencode.sh 2.0 python
1919
)
20-
2120
add_subdirectory(third_party/acl)
2221
include_directories(SYSTEM ${CMAKE_CURRENT_SOURCE_DIR}/third_party/acl/inc)
2322

24-
if(NOT DEFINED PYTHON_INCLUDE_DIR)
25-
execute_process(
26-
COMMAND sh -c "dirname $(find $(dirname $(which python))/../ -name Python.h)"
27-
OUTPUT_VARIABLE PYTHON_INCLUDE_DIR)
28-
endif()
29-
include_directories(SYSTEM ${PYTHON_INCLUDE_DIR})
30-
message(STATUS "PYTHON_INCLUDE_DIR: " ${PYTHON_INCLUDE_DIR})
23+
# find python3
24+
find_package(Python COMPONENTS Interpreter Development)
25+
MESSAGE(STATUS "Python_INCLUDE_DIRS: ${Python_INCLUDE_DIRS}")
26+
MESSAGE(STATUS "Python_LIBRARIES: ${Python_LIBRARIES}")
27+
include_directories(SYSTEM ${Python_INCLUDE_DIRS})
28+
link_directories(${Python_LIBRARIES})
3129

30+
# find torch
3231
if(NOT DEFINED PYTORCH_DIR)
3332
execute_process(
34-
COMMAND sh -c "dirname $(python -c 'import torch;print(torch.__path__[0])')"
33+
COMMAND sh -c "python -c 'import torch;print(torch.__path__[0])'"
3534
OUTPUT_VARIABLE PYTORCH_DIR
3635
OUTPUT_STRIP_TRAILING_WHITESPACE)
3736
endif()
38-
set(TORCH_LIBRARY_DIR "${PYTORCH_DIR}/torch/lib")
39-
link_directories(${TORCH_LIBRARY_DIR})
40-
list(APPEND TORCH_INCLUDE_DIRS ${PYTORCH_DIR}/torch/include/
41-
${PYTORCH_DIR}/torch/include/torch/csrc/api/include/)
42-
include_directories(SYSTEM ${TORCH_INCLUDE_DIRS})
43-
message(STATUS "Torch TORCH_INCLUDE_DIRS: ${TORCH_INCLUDE_DIRS}")
44-
45-
# if(NOT DEFINED DIPU_ABI_V)
46-
# execute_process(
47-
# COMMAND
48-
# sh -x -c
49-
# "python -c 'import torch, builtins; print(next(item[-4:-2] for item in dir(builtins) \
50-
# if \"__pybind11_internals_v4_gcc_libstdcpp_cxxabi10\" in item))'"
51-
# OUTPUT_VARIABLE DIPU_ABI_V)
52-
# endif()
53-
54-
if(NOT DEFINED DIPU_COMPILED_WITH_CXX11_ABI)
55-
execute_process(
56-
COMMAND
57-
sh -x -c
58-
"python -c 'import torch;print(1 if torch.compiled_with_cxx11_abi() else 0)'"
59-
OUTPUT_VARIABLE DIPU_COMPILED_WITH_CXX11_ABI)
60-
endif()
61-
62-
if(DIPU_COMPILED_WITH_CXX11_ABI GREATER 0)
63-
set(DIPU_COMPILED_WITH_CXX11_ABI 1)
64-
else()
65-
set(DIPU_COMPILED_WITH_CXX11_ABI 0)
66-
endif()
37+
find_package(Torch REQUIRED
38+
PATHS ${PYTORCH_DIR}
39+
)
40+
message(STATUS "TORCH_INSTALL_PREFIX:" ${TORCH_INSTALL_PREFIX})
41+
message(STATUS "TORCH_CXX_FLAGS:" ${TORCH_CXX_FLAGS}) # include -D_GLIBCXX_USE_CXX11_ABI
42+
message(STATUS "TORCH_INCLUDE_DIRS:" ${TORCH_INCLUDE_DIRS})
6743

68-
# add_definitions(-fabi-version=${DIPU_ABI_V})
69-
# message(STATUS "DIPU_ABI_V: ${DIPU_ABI_V}")
44+
include_directories(SYSTEM ${TORCH_INCLUDE_DIRS})
45+
link_directories(${TORCH_INSTALL_PREFIX}/lib)
7046

7147
# Because many interfaces have not yet been implemented, -Wreturn-type=0 option is temporarily added.
7248
set(CMAKE_CXX_FLAGS
73-
"${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=${DIPU_COMPILED_WITH_CXX11_ABI} -Wno-return-type -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -g -O0 "
49+
"${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS} -Wno-return-type -Wno-unused-function -Wno-unused-but-set-variable -Wno-unused-variable -g -O0 "
7450
)
75-
add_compile_options(-D_GLIBCXX_USE_CXX11_ABI=${DIPU_COMPILED_WITH_CXX11_ABI})
76-
message(STATUS "DIPU_COMPILED_WITH_CXX11_ABI:" ${DIPU_COMPILED_WITH_CXX11_ABI})
77-
7851

7952
set(OP_PLUGIN_DIR ${op_plugin_SOURCE_DIR}/op_plugin/ops/base_ops/aclops/)
8053
set(OP_PLUGIN_UTILS_DIR ${op_plugin_SOURCE_DIR}/op_plugin/utils/)
@@ -655,6 +628,7 @@ set(OLD_IMPL_SRC
655628
${OLD_IMPL_DIR}/common/generator_helper.cpp
656629
${OLD_IMPL_DIR}/common/stream_lock.cpp
657630
${OLD_IMPL_DIR}/env_vars.cpp
631+
${OLD_IMPL_DIR}/init.cpp
658632
)
659633

660634
list(APPEND IMPL_SRC ${OP_PLUGIN_UTILS_SRC} ${OP_PLUGIN_SRC} ${OP_PLUGIN_API_SRC} ${ASCEND_IMPL_SRC} ${DIOPI_IMPL_SRC} ${OLD_IMPL_SRC})
@@ -702,7 +676,7 @@ add_library(${DEVICEIMPL} SHARED ${IMPL_SRC})
702676
add_dependencies(${DEVICEIMPL} op_plugin_gen)
703677
set_target_properties(${DEVICEIMPL} PROPERTIES SUFFIX ".so")
704678
target_include_directories(${DEVICEIMPL} SYSTEM PUBLIC ${THIRD_PARTY_INCLUDE_DIRS})
705-
target_link_libraries(${DEVICEIMPL} ascendcl acl_op_compiler graph c10 torch_cpu)
679+
target_link_libraries(${DEVICEIMPL} ascendcl acl_op_compiler graph c10 torch_cpu ${Python_LIBRARIES})
706680

707681
if(USE_ADAPTOR)
708682
add_dependencies(${DEVICEIMPL} adaptor_code_gen)

0 commit comments

Comments
 (0)