From 60cd6883668c6f6d6a82992bcfdba47ee10bda07 Mon Sep 17 00:00:00 2001 From: likun104 Date: Tue, 12 Nov 2024 19:20:08 +0800 Subject: [PATCH 1/2] split exe_graph --- CMakeLists.txt | 3 +- build.sh | 2 +- exe_graph/CMakeLists.txt | 58 ++++++++++++++++--- .../exe_graph/runtime/infer_shape_context.h | 19 +----- tests/benchmark/exe_graph/CMakeLists.txt | 2 +- tests/ut/exe_graph/CMakeLists.txt | 2 +- .../exe_graph/infer_shape_context_unittest.cc | 33 +---------- tests/ut/register/CMakeLists.txt | 2 +- .../testcase/shape_inference_unittest.cc | 16 ++++- 9 files changed, 72 insertions(+), 65 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4e99c1347..4194573aa8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -136,7 +136,7 @@ if (ENABLE_METADEF_UT OR ENABLE_METADEF_ST OR ENABLE_BENCHMARK) add_subdirectory(tests) endif() -install(TARGETS exe_graph error_manager graph graph_base flow_graph register rt2_registry_static metadef_headers +install(TARGETS exe_graph lowering error_manager graph graph_base flow_graph register rt2_registry_static metadef_headers EXPORT metadef-targets LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR} OPTIONAL COMPONENT opensdk ARCHIVE DESTINATION ${INSTALL_LIBRARY_DIR} OPTIONAL COMPONENT opensdk @@ -209,6 +209,7 @@ if (ENABLE_OPEN_SRC) install(TARGETS error_manager exe_graph + lowering graph graph_base register diff --git a/build.sh b/build.sh index c1d56e5575..7fee88ff1a 100755 --- a/build.sh +++ b/build.sh @@ -142,7 +142,7 @@ build_metadef() { echo "CMAKE_ARGS is: $CMAKE_ARGS" cmake_generate_make "${BUILD_PATH}" "${CMAKE_ARGS}" - make graph graph_base exe_graph register register_static rt2_registry_static error_manager error_manager_static ${VERBOSE} -j${THREAD_NUM} && \ + make graph graph_base exe_graph lowering register register_static rt2_registry_static error_manager error_manager_static ${VERBOSE} -j${THREAD_NUM} && \ make install && make package if [ 0 -ne $? ]; then diff --git a/exe_graph/CMakeLists.txt b/exe_graph/CMakeLists.txt index 8c449660bd..c137f62589 100644 --- a/exe_graph/CMakeLists.txt +++ b/exe_graph/CMakeLists.txt @@ -8,7 +8,8 @@ # ====================================================================================================================== include(${METADEF_DIR}/cmake/build_type.cmake) -file(GLOB_RECURSE EXE_GRAPH_SRCS CONFIGURE_DEPENDS "${METADEF_DIR}/exe_graph/*.cc") +file(GLOB_RECURSE EXE_GRAPH_SRCS CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/runtime/*.cc") +file(GLOB_RECURSE LOWERING_SRCS CONFIGURE_DEPENDS "${CMAKE_CURRENT_LIST_DIR}/lowering/*.cc") ######### libexe_graph.so ############# add_library(exe_graph SHARED ${EXE_GRAPH_SRCS}) @@ -46,21 +47,60 @@ target_compile_options(exe_graph PRIVATE $<$,$>:/MT> ) -if (${ENABLE_OPEN_SRC} STREQUAL "True") - target_link_libraries(exe_graph PRIVATE ascend_protobuf graph_base register) - target_compile_options(exe_graph PRIVATE -O2) -else() +######### libexe_graph_static.a ############# +if (NOT ENABLE_OPEN_SRC) target_clone(exe_graph exe_graph_static STATIC) + target_sources(exe_graph_static PRIVATE ${LOWERING_SRCS}) - target_link_libraries(exe_graph PRIVATE ascend_protobuf graph_base register) target_link_libraries(exe_graph_static PRIVATE - ascend_protobuf_static graph_static register_static - ) + ascend_protobuf_static graph_static register_static + ) - target_compile_options(exe_graph PRIVATE -O2) target_compile_options(exe_graph_static PRIVATE $<$:-O2 -fPIC -Wextra -Wfloat-equal>) endif() +######### liblowering.so ############# +add_library(lowering SHARED ${LOWERING_SRCS}) + +target_include_directories(lowering PRIVATE + ${METADEF_DIR} +) + +target_include_directories(lowering PRIVATE + ${CMAKE_CURRENT_LIST_DIR} + ${CMAKE_BINARY_DIR} + ${CMAKE_BINARY_DIR}/proto/metadef_protos +) + +target_link_libraries(lowering PRIVATE + intf_pub + mmpa_headers + metadef_headers + c_sec + slog + $<$>:-lrt> + -ldl +) + +target_compile_definitions(lowering PRIVATE + google=ascend_private + $<$:ONLY_COMPILE_OPEN_SRC> + $,OS_TYPE=WIN,OS_TYPE=0> + $<$:SECUREC_USING_STD_SECURE_LIB=0 NOMINMAX> +) + +target_compile_options(lowering PRIVATE + $<$:-O2 -fPIC -Wextra -Wfloat-equal -fno-common> + $<$,$>:/MTd> + $<$,$>:/MT> +) + +target_link_libraries(lowering PRIVATE + ascend_protobuf + graph_base + register +) + ############ exe_meta_device ############ set(STUB_ERROR_MANAGER_SRC ${CMAKE_CURRENT_BINARY_DIR}/stub_error_manager.cc diff --git a/inc/external/exe_graph/runtime/infer_shape_context.h b/inc/external/exe_graph/runtime/infer_shape_context.h index ec73928ee3..6cd26d918f 100644 --- a/inc/external/exe_graph/runtime/infer_shape_context.h +++ b/inc/external/exe_graph/runtime/infer_shape_context.h @@ -14,7 +14,7 @@ #include "tensor.h" #include "runtime_attrs.h" #include "extended_kernel_context.h" -#include "graph/inference_context.h" + namespace gert { /** * 在节点输入后的扩展输入的索引,若需要扩展,请新增枚举类型 @@ -112,23 +112,6 @@ class InferShapeContext : public ExtendedKernelContext { Shape *GetOutputShape(const size_t index) { return GetOutputPointer(index); } - - /** - * 获取InferenceContext指针 - * @param NA - * @return 输出InferenceContext指针 指针在节点输入地址后,仅编译态使用 - */ - const ge::InferenceContext *GetInferenceContextPtr() const { - const auto compute_node_info = reinterpret_cast(GetContext()->compute_node_info); - if (compute_node_info == nullptr) { - return nullptr; - } - const size_t offset = compute_node_info->GetInputsNum() + static_cast(InputExternLayout::kInferenceContext); - if (GetContext()->input_size < offset) { - return nullptr; - } - return GetInputPointer(offset - 1UL); - } }; static_assert(std::is_standard_layout::value, "The class InferShapeContext must be a POD"); } // namespace gert diff --git a/tests/benchmark/exe_graph/CMakeLists.txt b/tests/benchmark/exe_graph/CMakeLists.txt index f8bea837c9..c85da94682 100644 --- a/tests/benchmark/exe_graph/CMakeLists.txt +++ b/tests/benchmark/exe_graph/CMakeLists.txt @@ -22,7 +22,7 @@ target_include_directories(exec_graph_benchmark PRIVATE ${CMAKE_BINARY_DIR}/proto/metadef_protos ) -target_link_libraries(exec_graph_benchmark PRIVATE benchmark::benchmark exe_graph error_manager +target_link_libraries(exec_graph_benchmark PRIVATE benchmark::benchmark exe_graph lowering error_manager slog_stub ascend_protobuf c_sec mmpa_stub -lrt -ldl metadef_headers graph diff --git a/tests/ut/exe_graph/CMakeLists.txt b/tests/ut/exe_graph/CMakeLists.txt index e8378feb13..2d357bf7f2 100644 --- a/tests/ut/exe_graph/CMakeLists.txt +++ b/tests/ut/exe_graph/CMakeLists.txt @@ -39,7 +39,7 @@ target_link_libraries(ut_exe_graph PRIVATE platform_stub msprof_headers runtime_headers - exe_graph register error_manager + exe_graph lowering register error_manager mmpa GTest::gtest GTest::gtest_main slog_stub ascend_protobuf c_sec mmpa_stub -lrt -ldl -lgcov metadef_headers diff --git a/tests/ut/exe_graph/infer_shape_context_unittest.cc b/tests/ut/exe_graph/infer_shape_context_unittest.cc index 454c17c76a..a6c4d0bc8d 100644 --- a/tests/ut/exe_graph/infer_shape_context_unittest.cc +++ b/tests/ut/exe_graph/infer_shape_context_unittest.cc @@ -94,37 +94,6 @@ TEST_F(InferShapeContextUT, GetOutShapeOk) { EXPECT_EQ(context->GetOutputShape(1), nullptr); } -TEST_F(InferShapeContextUT, GetInferenceContextPtrOK) { - ge::OpDescPtr op_desc = std::make_shared("test0", "test1"); - const ge::GeTensorDesc tensor1(ge::GeShape({1,2,3,4})); - const ge::GeTensorDesc tensor2(ge::GeShape({2,2,3,4})); - const ge::GeTensorDesc tensor3(ge::GeShape({3,2,3,4})); - ASSERT_EQ(op_desc->AddInputDesc(tensor1), ge::GRAPH_SUCCESS); - ASSERT_EQ(op_desc->AddInputDesc(tensor2), ge::GRAPH_SUCCESS); - ASSERT_EQ(op_desc->AddOutputDesc(tensor3), ge::GRAPH_SUCCESS); - KernelRunContextBuilder builder; - gert::StorageShape shape1({1,2,3,4}, {1,2,3,4}); - gert::StorageShape shape2({2,2,3,4}, {2,2,3,4}); - gert::StorageShape shape3({3,2,3,4}, {3,2,3,4}); - auto inference_ctx = std::shared_ptr(ge::InferenceContext::Create()); - KernelRegistry::KernelFunc kernel_func = [](KernelContext *context)->ge::graphStatus { - return ge::GRAPH_SUCCESS; - }; - auto holder = builder.Inputs({{&shape1, nullptr}, - {&shape2, nullptr}, - {reinterpret_cast(kernel_func), nullptr}, - {inference_ctx.get(), nullptr}}) - .Outputs({&shape3}) - .Build(op_desc); - auto infer_shape_ctx = reinterpret_cast(holder.context_); - ASSERT_NE(infer_shape_ctx, nullptr); - const size_t inputs_num = infer_shape_ctx->GetComputeNodeInputNum(); - const size_t offset = inputs_num + static_cast(InputExternLayout::kInferShapeFunc); - EXPECT_EQ(reinterpret_cast(holder.context_->GetInputValue(offset - 1U)), - kernel_func); - EXPECT_EQ(infer_shape_ctx->GetInferenceContextPtr(), inference_ctx.get()); -} - TEST_F(InferShapeContextUT, GetOptionalInputTensorFailed_NotSetOptionalInput) { gert::StorageShape in_shape1 = {{8, 3, 224, 224}, {8, 1, 224, 224, 16}}; auto infer_shape_func_addr = reinterpret_cast(0x11); @@ -239,4 +208,4 @@ TEST_F(InferShapeContextUT, GetRequiredInputTensorOk) { EXPECT_EQ(context->GetRequiredInputTensor(3)->GetOriginShape(), in_tensor_4.GetOriginShape()); EXPECT_EQ(context->GetRequiredInputTensor(3)->GetAddr(), in_tensor_4.GetAddr()); } -} // namespace gert \ No newline at end of file +} // namespace gert diff --git a/tests/ut/register/CMakeLists.txt b/tests/ut/register/CMakeLists.txt index 8a5f3c2d77..ed6f5ec846 100644 --- a/tests/ut/register/CMakeLists.txt +++ b/tests/ut/register/CMakeLists.txt @@ -39,7 +39,7 @@ target_link_libraries(ut_register runtime_headers slog_headers msprof_headers - exe_graph register graph graph_base mmpa + exe_graph lowering register graph graph_base mmpa GTest::gtest GTest::gtest_main ascend_protobuf diff --git a/tests/ut/register/testcase/shape_inference_unittest.cc b/tests/ut/register/testcase/shape_inference_unittest.cc index 12887665be..0718b4e4c5 100644 --- a/tests/ut/register/testcase/shape_inference_unittest.cc +++ b/tests/ut/register/testcase/shape_inference_unittest.cc @@ -31,6 +31,7 @@ #undef protected #undef private #include "mmpa/mmpa_api.h" +#include "graph/inference_context.h" namespace ge{ REG_OP(Const) @@ -875,6 +876,19 @@ TEST_F(ShapeInferenceUT, CallInferV2Func_skip_shaperange_infer_when_input_withou ASSERT_EQ(status, GRAPH_SUCCESS); // success means not called infer_shape_range_func } +const ge::InferenceContext *GetInferenceContextPtr(gert::InferShapeContext *context) { + const auto compute_node_info = reinterpret_cast(context->GetComputeNodeInfo()); + if (compute_node_info == nullptr) { + return nullptr; + } + const size_t offset = compute_node_info->GetInputsNum() + static_cast(InputExternLayout::kInferenceContext); + gert::KernelContext *context_base = reinterpret_cast(context); + if (context_base->GetContext()->input_size < offset) { + return nullptr; + } + return context_base->GetInputPointer(offset - 1UL); +} + // 资源类算子测试 REG_OP(RegisterAndGetReiledOnResource) .INPUT(input1, "T") @@ -897,7 +911,7 @@ TEST_F(ShapeInferenceUT, CallInferV2Func_RegisterAndGetReiledOnResource) { tensor_desc1.SetOriginDataType(DT_FLOAT16); op_desc->UpdateInputDesc(0, tensor_desc1); const auto infer_shape_func = [](gert::InferShapeContext *context) -> graphStatus { - const auto &read_inference_context = context->GetInferenceContextPtr(); + const auto &read_inference_context = GetInferenceContextPtr(context); const auto &reiled_keys = read_inference_context->GetReliedOnResourceKeys(); const char_t *resource_key_ = "224"; // check result -- Gitee From c3802e1982b028332a0a9352a0303adcebb94a35 Mon Sep 17 00:00:00 2001 From: likun104 Date: Mon, 23 Dec 2024 19:13:36 +0800 Subject: [PATCH 2/2] update exe_graph/CMakeLists.txt --- exe_graph/CMakeLists.txt | 80 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 8 deletions(-) diff --git a/exe_graph/CMakeLists.txt b/exe_graph/CMakeLists.txt index c137f62589..d2f15f1cb4 100644 --- a/exe_graph/CMakeLists.txt +++ b/exe_graph/CMakeLists.txt @@ -136,30 +136,48 @@ target_compile_options(exe_meta_device PRIVATE ) ############################################################## -set(STUB_HEADER_LIST +set(STUB_HEADER_LIST_EXE_GRAPH ${METADEF_DIR}/inc/external/exe_graph/runtime/compute_node_info.h ${METADEF_DIR}/inc/external/exe_graph/runtime/runtime_attrs.h ${METADEF_DIR}/inc/external/exe_graph/runtime/tiling_data.h ${METADEF_DIR}/inc/exe_graph/runtime/device_tiling_context_builder.h +) + +list(TRANSFORM STUB_HEADER_LIST_EXE_GRAPH + REPLACE "^.*/([^/]+)\\.h$" "${CMAKE_CURRENT_BINARY_DIR}/stub_\\1.cc" + OUTPUT_VARIABLE STUB_SRC_LIST_EXE_GRAPH +) + +add_custom_command( + OUTPUT ${STUB_SRC_LIST_EXE_GRAPH} + COMMAND echo "Generating stub files." + && ${HI_PYTHON} ${METADEF_DIR}/tests/stub/gen_stubapi.py ${CMAKE_CURRENT_BINARY_DIR} ${STUB_HEADER_LIST_EXE_GRAPH} + && echo "Generating stub files end." +) + +add_custom_target(exe_graph_stub DEPENDS ${STUB_SRC_LIST_EXE_GRAPH}) + +############################################################## +set(STUB_HEADER_LIST_LOWERING ${METADEF_DIR}/inc/exe_graph/lowering/bg_kernel_context_extend.h ) -list(TRANSFORM STUB_HEADER_LIST +list(TRANSFORM STUB_HEADER_LIST_LOWERING REPLACE "^.*/([^/]+)\\.h$" "${CMAKE_CURRENT_BINARY_DIR}/stub_\\1.cc" - OUTPUT_VARIABLE STUB_SRC_LIST + OUTPUT_VARIABLE STUB_SRC_LIST_LOWERING ) add_custom_command( - OUTPUT ${STUB_SRC_LIST} + OUTPUT ${STUB_SRC_LIST_LOWERING} COMMAND echo "Generating stub files." - && ${HI_PYTHON} ${METADEF_DIR}/tests/stub/gen_stubapi.py ${CMAKE_CURRENT_BINARY_DIR} ${STUB_HEADER_LIST} + && ${HI_PYTHON} ${METADEF_DIR}/tests/stub/gen_stubapi.py ${CMAKE_CURRENT_BINARY_DIR} ${STUB_HEADER_LIST_LOWERING} && echo "Generating stub files end." ) -add_custom_target(exe_graph_stub DEPENDS ${STUB_SRC_LIST}) +add_custom_target(lowering_stub DEPENDS ${STUB_SRC_LIST_LOWERING}) ############ stub/libexe_graph.so ############ -add_library(stub_exe_graph SHARED ${STUB_SRC_LIST}) +add_library(stub_exe_graph SHARED ${STUB_SRC_LIST_EXE_GRAPH}) add_dependencies(stub_exe_graph exe_graph_stub) @@ -204,7 +222,53 @@ if (NOT ENABLE_OPEN_SRC) ) endif () +############ stub/liblowering.so ############ +add_library(stub_lowering SHARED ${STUB_SRC_LIST_LOWERING}) + +add_dependencies(stub_lowering lowering_stub) + +target_include_directories(stub_lowering PRIVATE + ${CMAKE_CURRENT_LIST_DIR} + ${CMAKE_BINARY_DIR} + ${CMAKE_BINARY_DIR}/proto/metadef_protos +) + +target_compile_options(stub_lowering PRIVATE + -Wfloat-equal + -fno-common + -Os +) + +target_link_libraries(stub_lowering + PRIVATE + intf_pub + c_sec_headers + PUBLIC + metadef_headers +) + +set_target_properties(stub_lowering PROPERTIES + OUTPUT_NAME lowering + LIBRARY_OUTPUT_DIRECTORY stub +) + +############ stub/liblowering.a ############ +if (NOT ENABLE_OPEN_SRC) + target_clone(stub_lowering stub_lowering_static STATIC) + + add_dependencies(stub_lowering_static lowering_stub) + + target_compile_options(stub_lowering_static PRIVATE + -ffunction-sections + -fdata-sections + ) + set_target_properties(stub_lowering_static PROPERTIES + OUTPUT_NAME lowering + ARCHIVE_OUTPUT_DIRECTORY stub + ) +endif () + ############ install ############ -install(TARGETS stub_exe_graph OPTIONAL +install(TARGETS stub_exe_graph stub_lowering OPTIONAL LIBRARY DESTINATION ${INSTALL_LIBRARY_DIR}/${CMAKE_SYSTEM_PROCESSOR}/stub ) -- Gitee