From 2026faa695a9e683eb8dfe7542da275aa5c4ef74 Mon Sep 17 00:00:00 2001 From: dayschan Date: Sat, 2 Apr 2022 10:16:40 +0800 Subject: [PATCH] GraphKernel for MS-lite 1. move the lite_adapter files to lite directory. created a directory "graph_kernel" in "mindspore/lite/tools". we use 3 directory "converter", "runtime" and "common" to hold the different files. 2. modify lite makefiles to support GraphKernel. a) define a micro "MSLITE_ENABLE_GRAPH_KERNEL" when graphkernel is enabled by building option "-K on". b) converter's makefile: build the GraphKernel's core code and graph_kernel adapter for converter, includes some backend's code of mindspore. c) lite/src/CMakeLists: build graph_kernel adapter for benchmark 3. move the graph_kernel_pass_manager.cc from "graph_kernel/adapter" to "graph_kernel/core". the original two graph_kernel_pass_manager files are similar, so we unify them. --- .../ccsrc/common/graph_kernel/CMakeLists.txt | 4 - .../adapter/graph_kernel_optimization.cc | 2 +- .../graph_kernel_pass_manager.cc | 2 +- .../graph_kernel_pass_manager.h | 6 +- .../common/graph_kernel_op_parameter.h | 26 ------- .../lite_adapter/graph_kernel_pass_manager.cc | 75 ------------------- .../lite_adapter/graph_kernel_pass_manager.h | 50 ------------- .../plugin/device/cpu/kernel/nnacl/op_base.h | 3 +- mindspore/lite/CMakeLists.txt | 5 ++ mindspore/lite/src/CMakeLists.txt | 8 ++ .../lite/src/ops/populate/custom_populate.cc | 12 +++ mindspore/lite/tools/converter/CMakeLists.txt | 21 ++++++ mindspore/lite/tools/converter/converter.cc | 8 ++ mindspore/lite/tools/graph_kernel/OWNERS | 5 ++ .../tools/graph_kernel}/common/infer_shape.cc | 11 +-- .../graph_kernel/converter/akg}/akg_build.cc | 6 +- .../graph_kernel/converter/akg}/akg_build.h | 8 +- .../converter/akg/kernel_builder.cc} | 19 +++-- .../converter/akg/kernel_builder.h} | 8 +- .../graph_kernel/converter}/callback_impl.cc | 4 +- .../graph_kernel/converter}/callback_impl.h | 6 +- .../converter}/convert_const_input_to_attr.cc | 2 +- .../converter}/convert_const_input_to_attr.h | 6 +- .../converter}/expanders/activation.cc | 2 +- .../converter}/expanders/activation.h | 6 +- .../converter}/expanders/add_fusion.cc | 2 +- .../converter}/expanders/mul_fusion.cc | 2 +- .../converter}/expanders/reduce_fusion.cc | 0 .../converter}/expanders/sub_fusion.cc | 2 +- .../converter}/graph_kernel_expander_lite.cc | 2 +- .../converter}/graph_kernel_expander_lite.h | 6 +- .../converter}/graph_kernel_optimization.cc | 17 +++-- .../converter}/graph_kernel_optimization.h | 8 +- .../tools/graph_kernel}/runtime/akg_kernel.cc | 4 +- .../tools/graph_kernel}/runtime/akg_kernel.h | 13 ++-- .../optimizer/common/pass_manager_extends.cc | 56 +++++++------- tests/ut/cpp/CMakeLists.txt | 10 --- 37 files changed, 164 insertions(+), 263 deletions(-) rename mindspore/ccsrc/common/graph_kernel/{adapter => core}/graph_kernel_pass_manager.cc (95%) rename mindspore/ccsrc/common/graph_kernel/{adapter => core}/graph_kernel_pass_manager.h (84%) delete mode 100644 mindspore/ccsrc/common/graph_kernel/lite_adapter/common/graph_kernel_op_parameter.h delete mode 100644 mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_pass_manager.cc delete mode 100644 mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_pass_manager.h create mode 100644 mindspore/lite/tools/graph_kernel/OWNERS rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel}/common/infer_shape.cc (91%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter/akg}/akg_build.cc (97%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter/akg}/akg_build.h (75%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter/build_kernel.cc => lite/tools/graph_kernel/converter/akg/kernel_builder.cc} (90%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter/build_kernel.h => lite/tools/graph_kernel/converter/akg/kernel_builder.h} (76%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/callback_impl.cc (98%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/callback_impl.h (88%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/convert_const_input_to_attr.cc (95%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/convert_const_input_to_attr.h (76%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/expanders/activation.cc (96%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/expanders/activation.h (86%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/expanders/add_fusion.cc (95%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/expanders/mul_fusion.cc (95%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/expanders/reduce_fusion.cc (100%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/expanders/sub_fusion.cc (95%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/graph_kernel_expander_lite.cc (96%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/graph_kernel_expander_lite.h (84%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/graph_kernel_optimization.cc (86%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel/converter}/graph_kernel_optimization.h (77%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel}/runtime/akg_kernel.cc (97%) rename mindspore/{ccsrc/common/graph_kernel/lite_adapter => lite/tools/graph_kernel}/runtime/akg_kernel.h (80%) diff --git a/mindspore/ccsrc/common/graph_kernel/CMakeLists.txt b/mindspore/ccsrc/common/graph_kernel/CMakeLists.txt index 2d1a027f768..21277bc49b6 100644 --- a/mindspore/ccsrc/common/graph_kernel/CMakeLists.txt +++ b/mindspore/ccsrc/common/graph_kernel/CMakeLists.txt @@ -2,10 +2,6 @@ if(ENABLE_AKG AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux") file(GLOB_RECURSE _GK_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.cc" ) - file(GLOB_RECURSE _GK_LITE_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} - "lite_adapter/*.cc" - ) - list(REMOVE_ITEM _GK_SRC_LIST ${_GK_LITE_LIST}) list(APPEND _GRAPH_KERNEL_SRC_FILES ${_GK_SRC_LIST}) add_library(_mindspore_common_graph_kernel_obj OBJECT ${_GRAPH_KERNEL_SRC_FILES}) diff --git a/mindspore/ccsrc/common/graph_kernel/adapter/graph_kernel_optimization.cc b/mindspore/ccsrc/common/graph_kernel/adapter/graph_kernel_optimization.cc index 2717a5b4607..50e7200fb5a 100644 --- a/mindspore/ccsrc/common/graph_kernel/adapter/graph_kernel_optimization.cc +++ b/mindspore/ccsrc/common/graph_kernel/adapter/graph_kernel_optimization.cc @@ -47,7 +47,7 @@ #include "common/graph_kernel/tsa_atomic_add_to_first_tensor.h" #include "common/graph_kernel/uss_atomic_add.h" #include "backend/common/pass/getitem_tuple.h" -#include "common/graph_kernel/adapter/graph_kernel_pass_manager.h" +#include "common/graph_kernel/core/graph_kernel_pass_manager.h" #include "common/graph_kernel/transform_op_optimizer.h" #include "common/graph_kernel/rewrite_output_shape.h" #include "common/graph_kernel/graph_kernel_recompute.h" diff --git a/mindspore/ccsrc/common/graph_kernel/adapter/graph_kernel_pass_manager.cc b/mindspore/ccsrc/common/graph_kernel/core/graph_kernel_pass_manager.cc similarity index 95% rename from mindspore/ccsrc/common/graph_kernel/adapter/graph_kernel_pass_manager.cc rename to mindspore/ccsrc/common/graph_kernel/core/graph_kernel_pass_manager.cc index e2d1cd85d64..a54a3f56ece 100644 --- a/mindspore/ccsrc/common/graph_kernel/adapter/graph_kernel_pass_manager.cc +++ b/mindspore/ccsrc/common/graph_kernel/core/graph_kernel_pass_manager.cc @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "common/graph_kernel/adapter/graph_kernel_pass_manager.h" +#include "common/graph_kernel/core/graph_kernel_pass_manager.h" #include diff --git a/mindspore/ccsrc/common/graph_kernel/adapter/graph_kernel_pass_manager.h b/mindspore/ccsrc/common/graph_kernel/core/graph_kernel_pass_manager.h similarity index 84% rename from mindspore/ccsrc/common/graph_kernel/adapter/graph_kernel_pass_manager.h rename to mindspore/ccsrc/common/graph_kernel/core/graph_kernel_pass_manager.h index 94f8db41c49..28fc174faea 100644 --- a/mindspore/ccsrc/common/graph_kernel/adapter/graph_kernel_pass_manager.h +++ b/mindspore/ccsrc/common/graph_kernel/core/graph_kernel_pass_manager.h @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ADAPTER_GRAPH_KERNEL_PASS_MANAGER_H_ -#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ADAPTER_GRAPH_KERNEL_PASS_MANAGER_H_ +#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CORE_GRAPH_KERNEL_PASS_MANAGER_H_ +#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CORE_GRAPH_KERNEL_PASS_MANAGER_H_ #include #include @@ -46,4 +46,4 @@ class GraphKernelPassManager : public PassManager { const GraphKernelFlags &flags_; }; } // namespace mindspore::graphkernel -#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ADAPTER_GRAPH_KERNEL_PASS_MANAGER_H_ +#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CORE_GRAPH_KERNEL_PASS_MANAGER_H_ diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/common/graph_kernel_op_parameter.h b/mindspore/ccsrc/common/graph_kernel/lite_adapter/common/graph_kernel_op_parameter.h deleted file mode 100644 index 3b531ba5013..00000000000 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/common/graph_kernel_op_parameter.h +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright 2022 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_COMMON_GRAPH_KERNEL_OP_PARAMETER_H_ -#define MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_COMMON_GRAPH_KERNEL_OP_PARAMETER_H_ - -#include "nnacl/op_base.h" - -typedef struct GraphKernelParameter { - OpParameter op_parameter_; - void *prim_; -} GraphKernelParameter; -#endif // MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_COMMON_GRAPH_KERNEL_OP_PARAMETER_H_ diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_pass_manager.cc b/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_pass_manager.cc deleted file mode 100644 index e3adf0d7afb..00000000000 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_pass_manager.cc +++ /dev/null @@ -1,75 +0,0 @@ -/** - * Copyright 2022 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "common/graph_kernel/lite_adapter/graph_kernel_pass_manager.h" - -#include - -#include "ir/anf.h" -#include "ir/func_graph.h" -#include "ir/manager.h" -#include "include/common/debug/anf_ir_dump.h" - -namespace mindspore::graphkernel { -void GraphKernelPassManager::Add(const opt::PassPtr &pass, unsigned int pass_level, bool supported_device) { - MS_EXCEPTION_IF_NULL(pass); - auto pass_name = pass->name(); - auto pass_id = passes_.size(); - auto pass_in_list = [this, pass_id, &pass_name](const std::vector &pass_list) { - // config format can be "stage_id.pass_id" or "stage_name.pass_name" - return std::find(pass_list.begin(), pass_list.end(), - std::to_string(this->stage_) + "." + std::to_string(pass_id)) != pass_list.end() || - std::find(pass_list.begin(), pass_list.end(), this->name_ + "." + pass_name) != pass_list.end(); - }; - bool enable = supported_device && flags_.opt_level >= pass_level; - if (enable) { - // if it meets the condition to enable, check whether it's in the disabled pass list. - enable = !pass_in_list(flags_.disable_pass); - } else { - // if it doesn't meet the condition to enable, check whether it's in the enabled pass list. - enable = pass_in_list(flags_.enable_pass); - } - enabled_.push_back(enable); - passes_.push_back(pass); -} - -bool GraphKernelPassManager::RunPass(const FuncGraphPtr &func_graph, size_t pass_id, const opt::PassPtr &pass) const { - bool changed = pass->Run(func_graph); - return changed; -} - -std::string GraphKernelPassManager::GetPassFullname(size_t pass_id, const opt::PassPtr &pass) const { - return "stage" + std::to_string(stage_) + "_" + name() + "_" + std::to_string(pass_id) + "_" + pass->name(); -} - -bool GraphKernelPassManager::Run(const FuncGraphPtr &func_graph) const { - bool changed = false; - for (size_t i = 0; i < passes_.size(); i++) { - if (enabled_[i]) { - changed = RunPass(func_graph, i, passes_[i]) || changed; - // dump ir to a graph_kernel subdir, and set a global id in front of the filenames - std::ostringstream oss; - constexpr int id_length = 4; - static int g_id = 0; - oss << "graph_kernel/" << std::setfill('0') << std::setw(id_length) << g_id++ << "_" - << GetPassFullname(i, passes_[i]); - DumpPassIR(func_graph, oss.str()); - } else { - MS_LOG(INFO) << "pass " << GetPassFullname(i, passes_[i]) << " is disabled."; - } - } - return changed; -} -} // namespace mindspore::graphkernel diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_pass_manager.h b/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_pass_manager.h deleted file mode 100644 index 0271e8f263f..00000000000 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_pass_manager.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2022 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_PASS_MANAGER_H_ -#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_PASS_MANAGER_H_ - -#include -#include -#include -#include - -#include "backend/common/optimizer/pass_manager.h" -#include "common/graph_kernel/graph_kernel_flags.h" - -namespace mindspore::graphkernel { -using opt::PassManager; -class GraphKernelPassManager : public PassManager { - public: - GraphKernelPassManager(size_t stage, const std::string &name) - : PassManager(name, true), stage_(stage), flags_(GraphKernelFlags::GetInstance()) {} - ~GraphKernelPassManager() = default; - - // Add graph pass for lite, the pass object will be freed when pass manager freed. - void Add(const opt::PassPtr &pass, unsigned int pass_level, bool default_enable = true); - - // Run passes for lite on the func_graph - bool Run(const FuncGraphPtr &func_graph) const override; - - protected: - bool RunPass(const FuncGraphPtr &func_graph, size_t pass_id, const opt::PassPtr &pass) const override; - std::string GetPassFullname(size_t pass_id, const opt::PassPtr &pass) const override; - - size_t stage_; - std::vector enabled_; - const GraphKernelFlags &flags_; -}; -} // namespace mindspore::graphkernel -#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_PASS_MANAGER_H_ diff --git a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/op_base.h b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/op_base.h index 2fc10240beb..24f0fadde17 100644 --- a/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/op_base.h +++ b/mindspore/ccsrc/plugin/device/cpu/kernel/nnacl/op_base.h @@ -497,8 +497,9 @@ enum PrimType { PrimType_Inner_GltextureToOpencl = 10001, PrimType_Inner_Identity = 10002, PrimType_Inner_ShapeFusion = 10003, + PrimType_Inner_GraphKernel = 10004, PrimType_InnerOpMin = PrimType_Inner_ToFormat, - PrimType_InnerOpMax = PrimType_Inner_ShapeFusion + 1 + PrimType_InnerOpMax = PrimType_Inner_GraphKernel + 1 }; typedef enum FormatC { diff --git a/mindspore/lite/CMakeLists.txt b/mindspore/lite/CMakeLists.txt index 48fd514df5d..0c2790fa007 100644 --- a/mindspore/lite/CMakeLists.txt +++ b/mindspore/lite/CMakeLists.txt @@ -55,6 +55,7 @@ option(MSLITE_ENABLE_BFC_MEMORY "enable distribute BFC memory" off) option(MSLITE_ENABLE_PARALLEL_INFERENCE "enable parallel inference interface" off) option(MSLITE_ENABLE_SHARING_MODEL_WEIGHT "enable sharing model weight" off) option(MSLITE_ENABLE_EXPERIMENTAL_KERNEL "enable experimental kernel" off) +option(MSLITE_ENABLE_GRAPH_KERNEL "enable graph kernel" off) #Option that can be configured through manually option(ENABLE_VERBOSE "" off) @@ -97,6 +98,9 @@ endif() if(DEFINED ENV{MSLITE_ENABLE_CONVERTER}) set(MSLITE_ENABLE_CONVERTER $ENV{MSLITE_ENABLE_CONVERTER}) endif() +if(DEFINED ENV{ENABLE_AKG}) + set(MSLITE_ENABLE_GRAPH_KERNEL $ENV{ENABLE_AKG}) +endif() if(DEFINED ENV{MSLITE_ENABLE_TOOLS}) set(MSLITE_ENABLE_TOOLS $ENV{MSLITE_ENABLE_TOOLS}) endif() @@ -392,6 +396,7 @@ message(STATUS "\tMSLITE_ENABLE_BFC_MEMORY = \t${MSLITE_ENABLE message(STATUS "\tMSLITE_ENABLE_PARALLEL_INFERENCE = \t${MSLITE_ENABLE_PARALLEL_INFERENCE}") message(STATUS "\tMSLITE_ENABLE_SHARING_MODEL_WEIGHT = \t${MSLITE_ENABLE_SHARING_MODEL_WEIGHT}") message(STATUS "\tMSLITE_ENABLE_EXPERIMENT_KERNEL = \t${MSLITE_ENABLE_EXPERIMENT_KERNEL}") +message(STATUS "\tMSLITE_ENABLE_GRAPH_KERNEL = \t${MSLITE_ENABLE_GRAPH_KERNEL}") if((MSLITE_ENABLE_CONVERTER OR MSLITE_ENABLE_TESTCASES) AND ( NOT MSLITE_ENABLE_MINDRT diff --git a/mindspore/lite/src/CMakeLists.txt b/mindspore/lite/src/CMakeLists.txt index 6a2dfa448ef..596c9893772 100644 --- a/mindspore/lite/src/CMakeLists.txt +++ b/mindspore/lite/src/CMakeLists.txt @@ -334,6 +334,14 @@ else() ) endif() +if(MSLITE_ENABLE_GRAPH_KERNEL) + file(GLOB_RECURSE GRAPH_KERNEL_SRC + ${TOOLS_DIR}/graph_kernel/common/*.cc + ${TOOLS_DIR}/graph_kernel/runtime/*.cc + ) + set(LITE_SRC ${LITE_SRC} ${GRAPH_KERNEL_SRC}) +endif() + add_subdirectory(runtime/kernel/arm) add_library(lite_src_mid OBJECT ${LITE_SRC}) diff --git a/mindspore/lite/src/ops/populate/custom_populate.cc b/mindspore/lite/src/ops/populate/custom_populate.cc index 69147501ad3..375b55aadf1 100644 --- a/mindspore/lite/src/ops/populate/custom_populate.cc +++ b/mindspore/lite/src/ops/populate/custom_populate.cc @@ -16,6 +16,7 @@ #include "src/ops/populate/populate_register.h" #include "src/common/log_adapter.h" #include "src/tensor.h" +#include "nnacl/custom_parameter.h" using mindspore::schema::PrimitiveType_Custom; namespace mindspore { @@ -39,6 +40,17 @@ OpParameter *PopulateCustomParameter(const void *prim) { memset(param, 0, sizeof(OpParameter)); param->type_ = PrimType_Inner_ShapeFusion; return reinterpret_cast(param); + } else if (type == "GraphKernel") { + auto *param = static_cast(malloc(sizeof(CustomParameter))); + if (param == nullptr) { + MS_LOG(ERROR) << "malloc CustomParameter failed."; + return nullptr; + } + memset(param, 0, sizeof(CustomParameter)); + param->op_parameter_.type_ = PrimType_Inner_GraphKernel; + // Just use the attr_data pointer to save the prim directly, the inner value is parsed as necessary. + param->attr_data[0] = static_cast(const_cast(prim)); + return reinterpret_cast(param); } else { MS_LOG(ERROR) << "Unsupported custom type: " << type; } diff --git a/mindspore/lite/tools/converter/CMakeLists.txt b/mindspore/lite/tools/converter/CMakeLists.txt index da117422ab8..56bebea3778 100644 --- a/mindspore/lite/tools/converter/CMakeLists.txt +++ b/mindspore/lite/tools/converter/CMakeLists.txt @@ -5,6 +5,7 @@ if(MSLITE_ENABLE_MODEL_ENCRYPTION) add_compile_definitions(ENABLE_OPENSSL) endif() set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../src) +set(TOOLS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/..) set(CCSRC_SRC ${CCSRC_DIR}/backend/common/optimizer/pattern_engine.cc @@ -12,6 +13,26 @@ set(CCSRC_SRC ${CCSRC_DIR}/backend/common/optimizer/optimizer.cc ) +if(MSLITE_ENABLE_GRAPH_KERNEL) + add_compile_definitions(MSLITE_ENABLE_GRAPH_KERNEL) + file(GLOB_RECURSE GRAPH_KERNEL_SRC + ${TOOLS_DIR}/graph_kernel/common/*.cc + ${TOOLS_DIR}/graph_kernel/converter/*.cc + ${CCSRC_DIR}/common/graph_kernel/core/*.cc + ${CCSRC_DIR}/common/graph_kernel/expanders/*.cc + ${CCSRC_DIR}/common/graph_kernel/model/*.cc + ${CCSRC_DIR}/common/graph_kernel/split_model/*.cc + ${CCSRC_DIR}/common/graph_kernel/graph_kernel_flags.cc + ${CCSRC_DIR}/kernel/akg/akg_kernel_json_generator.cc + ${CCSRC_DIR}/backend/common/pass/getitem_tuple.cc + ${CCSRC_DIR}/backend/common/optimizer/const_input_to_attr.cc + ) + set(CCSRC_SRC + ${CCSRC_SRC} + ${GRAPH_KERNEL_SRC} + ) +endif() + include_directories(${TOP_DIR}/mindspore/ccsrc/plugin/device/cpu/kernel) file(GLOB_RECURSE CONVERTER_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} diff --git a/mindspore/lite/tools/converter/converter.cc b/mindspore/lite/tools/converter/converter.cc index 7f1dea9653b..18b1183584d 100644 --- a/mindspore/lite/tools/converter/converter.cc +++ b/mindspore/lite/tools/converter/converter.cc @@ -24,6 +24,8 @@ #include "tools/common/meta_graph_serializer.h" #include "tools/anf_exporter/anf_exporter.h" #include "include/version.h" +#include "common/graph_kernel/graph_kernel_flags.h" +#include "tools/graph_kernel/converter/graph_kernel_optimization.h" #ifdef SUPPORT_TRAIN #include "src/train/train_populate_parameter.h" #endif @@ -161,6 +163,12 @@ schema::MetaGraphT *Converter::TransferFuncGraph(const std::unique_ptr flatbuffer auto meta_graph = Export(func_graph, false, false, flag->trainModel); if (meta_graph == nullptr) { diff --git a/mindspore/lite/tools/graph_kernel/OWNERS b/mindspore/lite/tools/graph_kernel/OWNERS new file mode 100644 index 00000000000..fa8172e209f --- /dev/null +++ b/mindspore/lite/tools/graph_kernel/OWNERS @@ -0,0 +1,5 @@ +approvers: +- gaoxiong1 +- ckey_dou +- anyrenwei +- dayschan diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/common/infer_shape.cc b/mindspore/lite/tools/graph_kernel/common/infer_shape.cc similarity index 91% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/common/infer_shape.cc rename to mindspore/lite/tools/graph_kernel/common/infer_shape.cc index dfdf9f82b8a..bc33b80ccf2 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/common/infer_shape.cc +++ b/mindspore/lite/tools/graph_kernel/common/infer_shape.cc @@ -23,7 +23,7 @@ #include "src/common/utils.h" #include "nnacl/infer/common_infer.h" #include "nnacl/infer/infer_register.h" -#include "common/graph_kernel/lite_adapter/common/graph_kernel_op_parameter.h" +#include "nnacl/custom_parameter.h" namespace mindspore::graphkernel { namespace { @@ -112,10 +112,11 @@ int SetOutputsType(TensorC **outputs, size_t outputs_size, const std::string &ou } } // namespace -int InferShape(const TensorC **inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, +int InferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, OpParameter *parameter) { - auto param = reinterpret_cast(parameter); - auto prim = static_cast(param->prim_)->value_as_Custom(); + // in PopulateCustomParameter, the primitive is store in attr_data[0] + auto param = reinterpret_cast(parameter)->attr_data[0]; + auto prim = reinterpret_cast(param)->value_as_Custom(); for (size_t i = 0; i < prim->attr()->size(); i++) { auto attr = prim->attr()->Get(i); if (attr->name()->str() == "outputs_shape") { @@ -142,7 +143,7 @@ int InferShape(const TensorC **inputs, size_t inputs_size, TensorC **outputs, si #ifdef __cplusplus extern "C" { #endif -int GraphKernelInferShape(const TensorC **inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, +int GraphKernelInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size, OpParameter *parameter) { return mindspore::graphkernel::InferShape(inputs, inputs_size, outputs, outputs_size, parameter); } diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/akg_build.cc b/mindspore/lite/tools/graph_kernel/converter/akg/akg_build.cc similarity index 97% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/akg_build.cc rename to mindspore/lite/tools/graph_kernel/converter/akg/akg_build.cc index 84d60267777..0b697d643df 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/akg_build.cc +++ b/mindspore/lite/tools/graph_kernel/converter/akg/akg_build.cc @@ -1,5 +1,5 @@ /** - * Copyright 2021 Huawei Technologies Co., Ltd + * Copyright 2021-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "common/graph_kernel/lite_adapter/akg_build.h" +#include "tools/graph_kernel/converter/akg/akg_build.h" #include #include @@ -211,7 +211,7 @@ bool AkgKernelBuilder::CompileJsonsInAnfnodes(const AnfNodePtrList &node_list) { auto res = CompileJsonsInList(dir_path.value(), json_list); if (res) { CheckObjFiles(dir_path.value(), json_list); - auto cmd = "g++ -fPIC -shared -o" + dir_path.value() + "/akgkernels.so " + kernels_name; + auto cmd = "g++ -fPIC -shared -o akgkernels.so " + kernels_name; if (system(cmd.c_str()) == 0) { return true; } diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/akg_build.h b/mindspore/lite/tools/graph_kernel/converter/akg/akg_build.h similarity index 75% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/akg_build.h rename to mindspore/lite/tools/graph_kernel/converter/akg/akg_build.h index 83ecef74e22..850ee80265d 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/akg_build.h +++ b/mindspore/lite/tools/graph_kernel/converter/akg/akg_build.h @@ -1,5 +1,5 @@ /** - * Copyright 2021 Huawei Technologies Co., Ltd + * Copyright 2021-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_AKG_BUILD_H_ -#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_AKG_BUILD_H_ +#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_AKG_BUILD_H_ +#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_AKG_BUILD_H_ #include #include "utils/anf_utils.h" @@ -31,4 +31,4 @@ class AkgKernelBuilder { bool CompileJsonsInAnfnodes(const AnfNodePtrList &node_list); }; } // namespace mindspore::graphkernel -#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_AKG_BUILD_H_ +#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_AKG_BUILD_H_ diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/build_kernel.cc b/mindspore/lite/tools/graph_kernel/converter/akg/kernel_builder.cc similarity index 90% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/build_kernel.cc rename to mindspore/lite/tools/graph_kernel/converter/akg/kernel_builder.cc index 7003f37b7ae..774b84f81af 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/build_kernel.cc +++ b/mindspore/lite/tools/graph_kernel/converter/akg/kernel_builder.cc @@ -14,16 +14,18 @@ * limitations under the License. */ -#include "common/graph_kernel/lite_adapter/build_kernel.h" +#define USE_DEPRECATED_API +#include "tools/graph_kernel/converter/akg/kernel_builder.h" #include #include #include #include #include "common/graph_kernel/core/graph_kernel_callback.h" -#include "common/graph_kernel/lite_adapter/akg_build.h" +#include "tools/graph_kernel/converter/akg/akg_build.h" #include "ir/anf.h" #include "ir/func_graph.h" +#include "ops/primitive_c.h" #include "ops/custom.h" #include "utils/anf_utils.h" #include "utils/log_adapter.h" @@ -48,11 +50,8 @@ AnfNodePtr KernelBuilder::CreateCustomOp(const FuncGraphPtr &func_graph, const C if (func_graph == nullptr || cnode == nullptr) { return nullptr; } - auto primc = std::make_shared(); - if (primc == nullptr) { - return nullptr; - } - primc->set_type("GraphKernel"); + auto op = std::make_shared(); + op->set_type("GraphKernel"); std::map> custom_attrs; auto fg = GetCNodeFuncGraph(cnode); MS_EXCEPTION_IF_NULL(fg); @@ -82,10 +81,10 @@ AnfNodePtr KernelBuilder::CreateCustomOp(const FuncGraphPtr &func_graph, const C custom_attrs["outputs_shape"] = std::vector(output_shape_str.begin(), output_shape_str.end()); custom_attrs["outputs_format"] = std::vector(output_format_str.begin(), output_format_str.end()); custom_attrs["outputs_type"] = std::vector(output_type_str.begin(), output_type_str.end()); - primc->set_attr(custom_attrs); + op->set_attr(custom_attrs); auto inputs = cnode->inputs(); - inputs.erase(inputs.begin()); - auto custom_cnode = func_graph->NewCNode(primc, inputs); + inputs[0] = NewValueNode(op->GetPrim()); + auto custom_cnode = func_graph->NewCNode(inputs); custom_cnode->set_fullname_with_scope(cnode->fullname_with_scope()); custom_cnode->set_abstract(cnode->abstract()->Clone()); return custom_cnode; diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/build_kernel.h b/mindspore/lite/tools/graph_kernel/converter/akg/kernel_builder.h similarity index 76% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/build_kernel.h rename to mindspore/lite/tools/graph_kernel/converter/akg/kernel_builder.h index 293988931b4..4b5641ca577 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/build_kernel.h +++ b/mindspore/lite/tools/graph_kernel/converter/akg/kernel_builder.h @@ -14,15 +14,15 @@ * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_BUILD_KERNEL_H_ -#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_BUILD_KERNEL_H_ +#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_KERNEL_BUILDER_H_ +#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_KERNEL_BUILDER_H_ #include "ir/func_graph.h" #include "backend/common/optimizer/pass.h" namespace mindspore::graphkernel { class KernelBuilder : public opt::Pass { public: - KernelBuilder() : Pass("build_kernel_lite") {} + KernelBuilder() : Pass("akg_kernel_builder") {} ~KernelBuilder() override = default; AnfNodePtr CreateCustomOp(const FuncGraphPtr &func_graph, const CNodePtr &cnode); @@ -30,4 +30,4 @@ class KernelBuilder : public opt::Pass { bool Run(const FuncGraphPtr &func_graph) override; }; } // namespace mindspore::graphkernel -#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_BUILD_KERNEL_H_ +#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_KERNEL_BUILDER_H_ diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/callback_impl.cc b/mindspore/lite/tools/graph_kernel/converter/callback_impl.cc similarity index 98% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/callback_impl.cc rename to mindspore/lite/tools/graph_kernel/converter/callback_impl.cc index 1d9750d52a4..ac5dcdde7c3 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/callback_impl.cc +++ b/mindspore/lite/tools/graph_kernel/converter/callback_impl.cc @@ -1,5 +1,5 @@ /** - * Copyright 2021 Huawei Technologies Co., Ltd + * Copyright 2021-2022 Huawei Technologies Co., Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "common/graph_kernel/lite_adapter/callback_impl.h" +#include "tools/graph_kernel/converter/callback_impl.h" #include #include diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/callback_impl.h b/mindspore/lite/tools/graph_kernel/converter/callback_impl.h similarity index 88% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/callback_impl.h rename to mindspore/lite/tools/graph_kernel/converter/callback_impl.h index 2b8494cafb7..4766749afdc 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/callback_impl.h +++ b/mindspore/lite/tools/graph_kernel/converter/callback_impl.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CALLBACK_IMPL_H_ -#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CALLBACK_IMPL_H_ +#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_CALLBACK_IMPL_H_ +#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_CALLBACK_IMPL_H_ #include #include #include @@ -43,4 +43,4 @@ class CallbackImpl : public Callback { void ResetKernelInfo(const AnfNodePtr &node) override; }; } // namespace mindspore::graphkernel -#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CALLBACK_IMPL_H_ +#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_CALLBACK_IMPL_H_ diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/convert_const_input_to_attr.cc b/mindspore/lite/tools/graph_kernel/converter/convert_const_input_to_attr.cc similarity index 95% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/convert_const_input_to_attr.cc rename to mindspore/lite/tools/graph_kernel/converter/convert_const_input_to_attr.cc index 19f361c007a..8e98f63e605 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/convert_const_input_to_attr.cc +++ b/mindspore/lite/tools/graph_kernel/converter/convert_const_input_to_attr.cc @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "common/graph_kernel/lite_adapter/convert_const_input_to_attr.h" +#include "tools/graph_kernel/converter/convert_const_input_to_attr.h" #include "backend/common/optimizer/const_input_to_attr.h" #include "utils/anf_utils.h" diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/convert_const_input_to_attr.h b/mindspore/lite/tools/graph_kernel/converter/convert_const_input_to_attr.h similarity index 76% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/convert_const_input_to_attr.h rename to mindspore/lite/tools/graph_kernel/converter/convert_const_input_to_attr.h index 2210fe449a3..baed25947ff 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/convert_const_input_to_attr.h +++ b/mindspore/lite/tools/graph_kernel/converter/convert_const_input_to_attr.h @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CONVERT_CONST_INPUT_TO_ATTR_H_ -#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CONVERT_CONST_INPUT_TO_ATTR_H_ +#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_CONVERT_CONST_INPUT_TO_ATTR_H_ +#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_CONVERT_CONST_INPUT_TO_ATTR_H_ #include "ir/func_graph.h" #include "backend/common/optimizer/pass.h" @@ -27,4 +27,4 @@ class ConvertConstInputToAttr : public opt::Pass { }; } // namespace mindspore::graphkernel -#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CONVERT_CONST_INPUT_TO_ATTR_H_ +#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_CONVERT_CONST_INPUT_TO_ATTR_H_ diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/activation.cc b/mindspore/lite/tools/graph_kernel/converter/expanders/activation.cc similarity index 96% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/activation.cc rename to mindspore/lite/tools/graph_kernel/converter/expanders/activation.cc index 9347bd43a7f..e613fc6e591 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/activation.cc +++ b/mindspore/lite/tools/graph_kernel/converter/expanders/activation.cc @@ -19,7 +19,7 @@ #include #include "common/graph_kernel/expanders/expander_factory.h" -#include "common/graph_kernel/lite_adapter/expanders/activation.h" +#include "tools/graph_kernel/converter/expanders/activation.h" #include "mindapi/base/types.h" #include "ir/dtype.h" diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/activation.h b/mindspore/lite/tools/graph_kernel/converter/expanders/activation.h similarity index 86% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/activation.h rename to mindspore/lite/tools/graph_kernel/converter/expanders/activation.h index 496e2f54e18..2c0d34b89eb 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/activation.h +++ b/mindspore/lite/tools/graph_kernel/converter/expanders/activation.h @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_EXPANDERS_ACTIVATION_H_ -#define MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_EXPANDERS_ACTIVATION_H_ +#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_EXPANDERS_ACTIVATION_H_ +#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_EXPANDERS_ACTIVATION_H_ #include #include @@ -45,4 +45,4 @@ class CheckActivationType : public Validator { NodePtr GetActivationExpander(const inner::GraphBuilder &gb, const NodePtrList &inputs, int64_t activation_type); } // namespace mindspore::graphkernel::expanders -#endif // MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_EXPANDERS_ACTIVATION_H_ +#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_EXPANDERS_ACTIVATION_H_ diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/add_fusion.cc b/mindspore/lite/tools/graph_kernel/converter/expanders/add_fusion.cc similarity index 95% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/add_fusion.cc rename to mindspore/lite/tools/graph_kernel/converter/expanders/add_fusion.cc index 1706672d22f..961d41504bd 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/add_fusion.cc +++ b/mindspore/lite/tools/graph_kernel/converter/expanders/add_fusion.cc @@ -18,7 +18,7 @@ #include #include "common/graph_kernel/expanders/expander_factory.h" -#include "common/graph_kernel/lite_adapter/expanders/activation.h" +#include "tools/graph_kernel/converter/expanders/activation.h" #include "mindapi/base/types.h" #include "ir/dtype.h" diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/mul_fusion.cc b/mindspore/lite/tools/graph_kernel/converter/expanders/mul_fusion.cc similarity index 95% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/mul_fusion.cc rename to mindspore/lite/tools/graph_kernel/converter/expanders/mul_fusion.cc index e4b913570fe..946a40b8cac 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/mul_fusion.cc +++ b/mindspore/lite/tools/graph_kernel/converter/expanders/mul_fusion.cc @@ -18,7 +18,7 @@ #include #include "common/graph_kernel/expanders/expander_factory.h" -#include "common/graph_kernel/lite_adapter/expanders/activation.h" +#include "tools/graph_kernel/converter/expanders/activation.h" #include "mindapi/base/types.h" #include "ir/dtype.h" diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/reduce_fusion.cc b/mindspore/lite/tools/graph_kernel/converter/expanders/reduce_fusion.cc similarity index 100% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/reduce_fusion.cc rename to mindspore/lite/tools/graph_kernel/converter/expanders/reduce_fusion.cc diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/sub_fusion.cc b/mindspore/lite/tools/graph_kernel/converter/expanders/sub_fusion.cc similarity index 95% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/sub_fusion.cc rename to mindspore/lite/tools/graph_kernel/converter/expanders/sub_fusion.cc index 1f6525b6290..b6d3602fa88 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/expanders/sub_fusion.cc +++ b/mindspore/lite/tools/graph_kernel/converter/expanders/sub_fusion.cc @@ -18,7 +18,7 @@ #include #include "common/graph_kernel/expanders/expander_factory.h" -#include "common/graph_kernel/lite_adapter/expanders/activation.h" +#include "tools/graph_kernel/converter/expanders/activation.h" #include "mindapi/base/types.h" #include "ir/dtype.h" diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_expander_lite.cc b/mindspore/lite/tools/graph_kernel/converter/graph_kernel_expander_lite.cc similarity index 96% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_expander_lite.cc rename to mindspore/lite/tools/graph_kernel/converter/graph_kernel_expander_lite.cc index ded071f0679..3e667ee453b 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_expander_lite.cc +++ b/mindspore/lite/tools/graph_kernel/converter/graph_kernel_expander_lite.cc @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "common/graph_kernel/lite_adapter/graph_kernel_expander_lite.h" +#include "tools/graph_kernel/converter/graph_kernel_expander_lite.h" #include #include diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_expander_lite.h b/mindspore/lite/tools/graph_kernel/converter/graph_kernel_expander_lite.h similarity index 84% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_expander_lite.h rename to mindspore/lite/tools/graph_kernel/converter/graph_kernel_expander_lite.h index fdb766e6435..f5d3b39b1de 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_expander_lite.h +++ b/mindspore/lite/tools/graph_kernel/converter/graph_kernel_expander_lite.h @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_EXPANDER_LITE_H_ -#define MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_EXPANDER_LITE_H_ +#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_GRAPH_KERNEL_EXPANDER_LITE_H_ +#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_GRAPH_KERNEL_EXPANDER_LITE_H_ #include #include @@ -43,4 +43,4 @@ class GraphKernelExpanderLite : public GraphKernelExpander { ExpanderPtr GetExpander(const AnfNodePtr &node) override; }; } // namespace mindspore::graphkernel -#endif // MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_EXPANDER_LITE_H_ +#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_GRAPH_KERNEL_EXPANDER_LITE_H_ diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_optimization.cc b/mindspore/lite/tools/graph_kernel/converter/graph_kernel_optimization.cc similarity index 86% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_optimization.cc rename to mindspore/lite/tools/graph_kernel/converter/graph_kernel_optimization.cc index 81a830837fd..11af1c0d238 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_optimization.cc +++ b/mindspore/lite/tools/graph_kernel/converter/graph_kernel_optimization.cc @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "common/graph_kernel/lite_adapter/graph_kernel_optimization.h" +#include "tools/graph_kernel/converter/graph_kernel_optimization.h" #include #include @@ -27,10 +27,11 @@ #include "common/graph_kernel/core/eliminate_redundant_output.h" #include "common/graph_kernel/core/shape_ops_splitter.h" #include "common/graph_kernel/core/update_state_formatter.h" -#include "common/graph_kernel/lite_adapter/build_kernel.h" -#include "common/graph_kernel/lite_adapter/convert_const_input_to_attr.h" -#include "common/graph_kernel/lite_adapter/graph_kernel_expander_lite.h" -#include "common/graph_kernel/lite_adapter/graph_kernel_pass_manager.h" +#include "common/graph_kernel/core/graph_kernel_pass_manager.h" + +#include "tools/graph_kernel/converter/akg/kernel_builder.h" +#include "tools/graph_kernel/converter/convert_const_input_to_attr.h" +#include "tools/graph_kernel/converter/graph_kernel_expander_lite.h" namespace mindspore::graphkernel { using opt::GetitemTuple; @@ -72,8 +73,8 @@ PassManagerPtr GraphKernelOptimizer::Split() const { return pm; } -PassManagerPtr GraphKernelOptimizer::PostProcess() const { - auto pm = std::make_shared(2, "postprocess"); +PassManagerPtr GraphKernelOptimizer::BuildKernel() const { + auto pm = std::make_shared(2, "buildkernel"); // build akg and replace graph kernel nodes pm->Add(std::make_shared(), OptLevel_1); return pm; @@ -83,7 +84,7 @@ void GraphKernelOptimizer::Run(const FuncGraphPtr &kernel_graph) { auto optimizer = std::make_shared("graph_kernel_optimizer"); optimizer->AddPassManager(Cluster()); optimizer->AddPassManager(Split()); - optimizer->AddPassManager(PostProcess()); + optimizer->AddPassManager(BuildKernel()); auto mng = kernel_graph->manager(); if (mng == nullptr) { diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_optimization.h b/mindspore/lite/tools/graph_kernel/converter/graph_kernel_optimization.h similarity index 77% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_optimization.h rename to mindspore/lite/tools/graph_kernel/converter/graph_kernel_optimization.h index 27b15f85252..8c57e2ecb06 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_optimization.h +++ b/mindspore/lite/tools/graph_kernel/converter/graph_kernel_optimization.h @@ -13,8 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_OPTIMIZATION_H_ -#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_OPTIMIZATION_H_ +#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_GRAPH_KERNEL_OPTIMIZATION_H_ +#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_GRAPH_KERNEL_OPTIMIZATION_H_ #include "ir/anf.h" #include "ir/func_graph.h" @@ -33,9 +33,9 @@ class GraphKernelOptimizer { // Split kernels PassManagerPtr Split() const; // Post-process - PassManagerPtr PostProcess() const; + PassManagerPtr BuildKernel() const; }; void GraphKernelOptimize(const FuncGraphPtr &kernel_graph); } // namespace mindspore::graphkernel -#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_OPTIMIZATION_H_ +#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_GRAPH_KERNEL_OPTIMIZATION_H_ diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/runtime/akg_kernel.cc b/mindspore/lite/tools/graph_kernel/runtime/akg_kernel.cc similarity index 97% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/runtime/akg_kernel.cc rename to mindspore/lite/tools/graph_kernel/runtime/akg_kernel.cc index 6d08dcd88b0..bb88d97be23 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/runtime/akg_kernel.cc +++ b/mindspore/lite/tools/graph_kernel/runtime/akg_kernel.cc @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "common/graph_kernel/lite_adapter/runtime/akg_kernel.h" +#include "tools/graph_kernel/runtime/akg_kernel.h" #include #include #include "src/tensor.h" @@ -60,7 +60,7 @@ class AkgCallBack { } // namespace void AkgKernel::ExtractKernelName() { - auto prim = static_cast(params_->prim_)->value_as_Custom(); + auto prim = static_cast(params_)->value_as_Custom(); for (size_t i = 0; i < prim->attr()->size(); i++) { auto attr = prim->attr()->Get(i); if (attr->name()->str() == "kernel_name") { diff --git a/mindspore/ccsrc/common/graph_kernel/lite_adapter/runtime/akg_kernel.h b/mindspore/lite/tools/graph_kernel/runtime/akg_kernel.h similarity index 80% rename from mindspore/ccsrc/common/graph_kernel/lite_adapter/runtime/akg_kernel.h rename to mindspore/lite/tools/graph_kernel/runtime/akg_kernel.h index 886df7233f0..59933f62b66 100644 --- a/mindspore/ccsrc/common/graph_kernel/lite_adapter/runtime/akg_kernel.h +++ b/mindspore/lite/tools/graph_kernel/runtime/akg_kernel.h @@ -14,12 +14,12 @@ * limitations under the License. */ -#ifndef MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_RUNTIME_AKG_KERNEL_H_ -#define MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_RUNTIME_AKG_KERNEL_H_ +#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_RUNTIME_AKG_KERNEL_H_ +#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_RUNTIME_AKG_KERNEL_H_ #include #include #include "src/lite_kernel.h" -#include "common/graph_kernel/lite_adapter/common/graph_kernel_op_parameter.h" +#include "nnacl/custom_parameter.h" namespace mindspore::kernel { using AkgParallelLambda = int (*)(int task_id, int num_task, void *cdata); @@ -29,7 +29,8 @@ class AkgKernel : public LiteKernel { AkgKernel(OpParameter *parameter, const std::vector &inputs, const std::vector &outputs, const lite::InnerContext *ctx) : LiteKernel(parameter, inputs, outputs, ctx) { - params_ = reinterpret_cast(op_parameter_); + // in PopulateCustomParameter, the primitive is store in attr_data[0] + params_ = static_cast(reinterpret_cast(op_parameter_)->attr_data[0]); ExtractKernelName(); } ~AkgKernel() override; @@ -49,7 +50,7 @@ class AkgKernel : public LiteKernel { protected: void ExtractKernelName(); - GraphKernelParameter *params_{nullptr}; + void *params_{nullptr}; void *handle_{nullptr}; void *kernel_func_{nullptr}; std::string kernel_name_; @@ -58,4 +59,4 @@ class AkgKernel : public LiteKernel { void *cached_runtimeargs_ = nullptr; }; } // namespace mindspore::kernel -#endif // MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_RUNTIME_AKG_KERNEL_H_ +#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_RUNTIME_AKG_KERNEL_H_ diff --git a/mindspore/lite/tools/optimizer/common/pass_manager_extends.cc b/mindspore/lite/tools/optimizer/common/pass_manager_extends.cc index 3dca8e7d1b4..5424c5941a7 100644 --- a/mindspore/lite/tools/optimizer/common/pass_manager_extends.cc +++ b/mindspore/lite/tools/optimizer/common/pass_manager_extends.cc @@ -35,11 +35,35 @@ void PassManager::AddPass(const PassPtr &pass) { } } -// not implement for lite, just for api compatible -bool PassManager::RunPass(const FuncGraphPtr &func_graph, size_t pass_id, const PassPtr &pass) const { return false; } +bool PassManager::RunPass(const FuncGraphPtr &func_graph, size_t pass_id, const PassPtr &pass) const { + bool changed = false; +#if defined(_WIN32) || defined(_WIN64) + auto start_time = std::chrono::steady_clock::now(); +#else + struct timeval start_time {}; + struct timeval end_time {}; + (void)gettimeofday(&start_time, nullptr); +#endif + if (pass->Run(func_graph)) { + MS_LOG(DEBUG) << "Run pass and find change"; + changed = true; + } +#if defined(_WIN32) || defined(_WIN64) + auto end_time = std::chrono::steady_clock::now(); + std::chrono::duration> cost = end_time - start_time; + MS_LOG(INFO) << "Run pass " << GetPassFullname(pass_id, pass) << " in " << cost.count() << " us."; +#else + (void)gettimeofday(&end_time, nullptr); + uint64_t cost = kUSecondInSecond * static_cast(end_time.tv_sec - start_time.tv_sec); + cost += static_cast(end_time.tv_usec - start_time.tv_usec); + MS_LOG(INFO) << "Run pass " << GetPassFullname(pass_id, pass) << " in " << cost << " us."; +#endif + return changed; +} -// not implement for lite, just for api compatible -std::string PassManager::GetPassFullname(size_t pass_id, const PassPtr &pass) const { return ""; } +std::string PassManager::GetPassFullname(size_t pass_id, const PassPtr &pass) const { + return "hwopt_" + name() + "_" + std::to_string(pass_id) + "_" + pass->name(); +} // not implement for lite, just for api compatible void PassManager::DumpPassIR(const FuncGraphPtr &func_graph, const std::string &pass_fullname) const {} @@ -52,31 +76,11 @@ bool PassManager::Run(const FuncGraphPtr &func_graph, const std::vector size_t num = 0; for (const auto &pass : passes) { if (pass != nullptr) { -#if defined(_WIN32) || defined(_WIN64) - auto start_time = std::chrono::steady_clock::now(); -#else - struct timeval start_time {}; - struct timeval end_time {}; - (void)gettimeofday(&start_time, nullptr); -#endif - if (pass->Run(func_graph)) { - MS_LOG(DEBUG) << "Run pass and find change"; - changed = true; - } -#if defined(_WIN32) || defined(_WIN64) - auto end_time = std::chrono::steady_clock::now(); - std::chrono::duration> cost = end_time - start_time; - MS_LOG(INFO) << "Run pass hwopt_" + name() + "_" << num << "_" + pass->name() + " in " << cost.count() << " us"; -#else - (void)gettimeofday(&end_time, nullptr); - uint64_t cost = kUSecondInSecond * static_cast(end_time.tv_sec - start_time.tv_sec); - cost += static_cast(end_time.tv_usec - start_time.tv_usec); - MS_LOG(INFO) << "Run pass hwopt_" + name() + "_" << num << "_" + pass->name() + " in " << cost << " us"; -#endif - num++; + changed = RunPass(func_graph, num, pass) || changed; } else { MS_LOG(INFO) << "pass is null"; } + num++; } return changed; } diff --git a/tests/ut/cpp/CMakeLists.txt b/tests/ut/cpp/CMakeLists.txt index 2c07725d5c5..8ce571199b2 100644 --- a/tests/ut/cpp/CMakeLists.txt +++ b/tests/ut/cpp/CMakeLists.txt @@ -220,16 +220,6 @@ list(REMOVE_ITEM MINDSPORE_SRC_LIST list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/plugin/device/gpu/optimizer/batch_norm_relu_fusion.cc") list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/plugin/device/gpu/optimizer/batch_norm_relu_grad_fusion.cc") -list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/akg_build.cc") -list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/build_kernel.cc") -list(REMOVE_ITEM MINDSPORE_SRC_LIST - "../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/callback_impl.cc") -list(REMOVE_ITEM MINDSPORE_SRC_LIST - "../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_optimization.cc") -list(REMOVE_ITEM MINDSPORE_SRC_LIST - "../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_pass_manager.cc") -list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/runtime/akg_kernel.cc") -list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/common/infer_shape.cc") list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/plugin/device/ascend/kernel/tbe/tbe_kernel_compile.cc") list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/plugin/device/ascend/kernel/tbe/tiling/op_tiling_adapter.cc")