From df896449a4079ff033dfb2e1a489a5100e509ad6 Mon Sep 17 00:00:00 2001 From: ling Date: Tue, 10 Aug 2021 14:15:24 +0800 Subject: [PATCH] [MSLITE] MSLITE_ENABLE_RUNTIME_PASS --- mindspore/lite/CMakeLists.txt | 8 ++++++ mindspore/lite/src/runtime/runtime_pass.cc | 12 ++++++--- mindspore/lite/src/runtime/runtime_pass.h | 3 ++- mindspore/lite/src/scheduler.cc | 31 +++++++++++++++------- mindspore/lite/src/scheduler.h | 1 + 5 files changed, 40 insertions(+), 15 deletions(-) diff --git a/mindspore/lite/CMakeLists.txt b/mindspore/lite/CMakeLists.txt index 17d306a98de..acb563438e6 100644 --- a/mindspore/lite/CMakeLists.txt +++ b/mindspore/lite/CMakeLists.txt @@ -31,6 +31,7 @@ option(MSLITE_ENABLE_CONVERTER "enable converter, only x86_64 support" on) option(MSLITE_ENABLE_TOOLS "enable tools" on) option(MSLITE_ENABLE_TESTCASES "enable testcase" off) option(MSLITE_ENABLE_NNIE "enable NNIE" off) +option(MSLITE_ENABLE_RUNTIME_PASS "enable runtime pass" on) option(MSLITE_COMPILE_NNIE "compile NNIE" off) option(MSLITE_ENABLE_HIGH_PERFORMANCE "enable high performance" on) option(MSLITE_STRING_KERNEL "enable string kernel" on) @@ -83,6 +84,9 @@ endif() if(DEFINED ENV{MSLITE_COMPILE_NNIE}) set(MSLITE_COMPILE_NNIE $ENV{MSLITE_COMPILE_NNIE}) endif() +if(DEFINED ENV{MSLITE_ENABLE_RUNTIME_PASS}) + set(MSLITE_ENABLE_RUNTIME_PASS $ENV{MSLITE_ENABLE_RUNTIME_PASS}) +endif() if(DEFINED ENV{MSLITE_ENABLE_HIGH_PERFORMANCE}) set(MSLITE_ENABLE_HIGH_PERFORMANCE $ENV{MSLITE_ENABLE_HIGH_PERFORMANCE}) endif() @@ -112,6 +116,9 @@ endif() if(MSLITE_HUFFMAN_DECODE) add_compile_definitions(ENABLE_HUFFMAN_DECODE) endif() +if(MSLITE_ENABLE_RUNTIME_PASS) + add_compile_definitions(ENABLE_RUNTIME_PASS) +endif() if(PLATFORM_ARM64) if(MSLITE_GPU_BACKEND STREQUAL "") @@ -183,6 +190,7 @@ message(STATUS "\tMSLITE_ENABLE_CONVERTER = \t${MSLITE_ENABLE_CONVERTER}") message(STATUS "\tMSLITE_ENABLE_TOOLS = \t${MSLITE_ENABLE_TOOLS}") message(STATUS "\tMSLITE_ENABLE_TESTCASES = \t${MSLITE_ENABLE_TESTCASES}") message(STATUS "\tMSLITE_ENABLE_HIGH_PERFORMANCE = \t${MSLITE_ENABLE_HIGH_PERFORMANCE}") +message(STATUS "\tMSLITE_ENABLE_RUNTIME_PASS = \t${MSLITE_ENABLE_RUNTIME_PASS}") if(MSLITE_ENABLE_HIGH_PERFORMANCE) add_compile_definitions(ENABLE_HIGH_PERFORMANCE) diff --git a/mindspore/lite/src/runtime/runtime_pass.cc b/mindspore/lite/src/runtime/runtime_pass.cc index 8bb988e3338..68f0738238a 100644 --- a/mindspore/lite/src/runtime/runtime_pass.cc +++ b/mindspore/lite/src/runtime/runtime_pass.cc @@ -14,6 +14,7 @@ * limitations under the License. */ +#ifdef ENABLE_RUNTIME_PASS #include "src/runtime/runtime_pass.h" #include "nnacl/conv_parameter.h" @@ -95,9 +96,11 @@ bool Nc4hw4PassValid(const InnerContext *context, std::vectorop_parameter()->quant_type_ == schema::QuantType_AwareTraining || - kernel->op_parameter()->quant_type_ == schema::QuantType_PostTraining) { - return false; + if (kernel->op_parameter() != nullptr) { + if (kernel->op_parameter()->quant_type_ == schema::QuantType_AwareTraining || + kernel->op_parameter()->quant_type_ == schema::QuantType_PostTraining) { + return false; + } } } return true; @@ -106,7 +109,7 @@ bool Nc4hw4PassValid(const InnerContext *context, std::vector *kernels, std::vector *tensors) { size_t kernel_size = kernels->size(); size_t index = 0; - for (; index < kernel_size - 2; index++) { + for (; index + 2 < kernel_size; index++) { kernel::LiteKernel *kernel = kernels->at(index); if (kernel->subgraph_type() != kernel::kNotSubGraph) { @@ -124,3 +127,4 @@ void Nc4hw4Pass(std::vector *kernels, std::vector #include "src/lite_kernel.h" #include "src/sub_graph_kernel.h" @@ -37,5 +38,5 @@ bool Nc4hw4PassValid(const InnerContext *context, std::vector *kernels, std::vector *tensors); } // namespace mindspore::lite - +#endif #endif // MINDSPORE_LITE_SRC_RUNTIME_RUNTIME_PASS_H_ diff --git a/mindspore/lite/src/scheduler.cc b/mindspore/lite/src/scheduler.cc index 6b6793d2d0c..59b2e4b6e33 100644 --- a/mindspore/lite/src/scheduler.cc +++ b/mindspore/lite/src/scheduler.cc @@ -88,16 +88,10 @@ int Scheduler::InitKernels(std::vector dst_kernels) { } int Scheduler::Schedule(std::vector *dst_kernels) { - if (dst_kernels == nullptr) { - return RET_ERROR; - } - if (src_model_ == nullptr) { - MS_LOG(ERROR) << "Input model is nullptr"; - return RET_PARAM_INVALID; - } - if (src_model_->sub_graphs_.empty()) { - MS_LOG(ERROR) << "Model should have a subgraph at least"; - return RET_PARAM_INVALID; + int check_input_ret = CheckInputParam(dst_kernels); + if (check_input_ret != RET_OK) { + MS_LOG(ERROR) << "CheckInputParam failed! ret: " << check_input_ret; + return check_input_ret; } this->graph_output_node_indexes_ = GetGraphOutputNodes(src_model_); @@ -137,9 +131,11 @@ int Scheduler::Schedule(std::vector *dst_kernels) { } } +#ifdef ENABLE_RUNTIME_PASS if (Nc4hw4PassValid(context_, dst_kernels)) { Nc4hw4Pass(dst_kernels, src_tensors_); } +#endif FindAllInoutKernels(*dst_kernels); #ifdef ENABLE_CONTROL_TENSORLIST @@ -173,6 +169,21 @@ int Scheduler::Schedule(std::vector *dst_kernels) { return RET_OK; } +int Scheduler::CheckInputParam(std::vector *dst_kernels) { + if (dst_kernels == nullptr) { + return RET_ERROR; + } + if (src_model_ == nullptr) { + MS_LOG(ERROR) << "Input model is nullptr"; + return RET_PARAM_INVALID; + } + if (src_model_->sub_graphs_.empty()) { + MS_LOG(ERROR) << "Model should have a subgraph at least"; + return RET_PARAM_INVALID; + } + return RET_OK; +} + int Scheduler::ReplaceDelegateKernels(std::vector *dst_kernels) { std::vector kernels; for (size_t i = 0; i < dst_kernels->size(); i++) { diff --git a/mindspore/lite/src/scheduler.h b/mindspore/lite/src/scheduler.h index 077e1d65836..ace2ba5bed7 100644 --- a/mindspore/lite/src/scheduler.h +++ b/mindspore/lite/src/scheduler.h @@ -53,6 +53,7 @@ class Scheduler { void SetupSchedulerCb(std::unique_ptr cb) { sched_cb_ = std::move(cb); } private: + int CheckInputParam(std::vector *dst_kernels); void FindNodeInoutTensors(const Model::Node &node, std::vector *inputs, std::vector *outputs); Model::Node *NodeInputIsPartial(const Model::Node *node); int InferPartialShape(const Model::Node *node);