From 7b87558f2fc8d6eff511d9745516293bf8188539 Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Wed, 30 Dec 2020 21:36:26 +0800 Subject: [PATCH 1/7] modified: ge/graph/manager/graph_manager.cc modified: ge/graph/preprocess/graph_preprocess.cc modified: ge/graph/preprocess/graph_preprocess.h modified: inc/external/ge/ge_api_types.h --- ge/graph/manager/graph_manager.cc | 4 +- ge/graph/preprocess/graph_preprocess.cc | 135 ++++++++++++++++++++++-- ge/graph/preprocess/graph_preprocess.h | 6 +- inc/external/ge/ge_api_types.h | 5 + 4 files changed, 138 insertions(+), 12 deletions(-) diff --git a/ge/graph/manager/graph_manager.cc b/ge/graph/manager/graph_manager.cc index 030b864e..1aee79a4 100755 --- a/ge/graph/manager/graph_manager.cc +++ b/ge/graph/manager/graph_manager.cc @@ -687,7 +687,7 @@ Status GraphManager::PreRunOptimizeOriginalGraph(const GraphNodePtr &graph_node, CompilerStages &stages = GetCompilerStages(graph_node->GetGraphId()); GM_RUN_AND_DUMP_PERF("OptimizeGraphPrepare", stages.optimizer.OptimizeOriginalGraphForQuantize, compute_graph); GM_RUN_AND_DUMP_PERF("HandleSummaryOp", stages.optimizer.HandleSummaryOp, compute_graph); - GM_RUN_AND_DUMP_PERF("Prepare", stages.preparer.PrepareDynShape, graph_node->GetGraph(), inputs, compute_graph, + GM_RUN_AND_DUMP_PERF("Prepare", stages.preparer.PrepareDynShape, graph_node, inputs, compute_graph, session_id); GM_RUN_AND_DUMP_PERF("OptimizeOriginalGraph", stages.optimizer.OptimizeOriginalGraph, compute_graph); @@ -1173,7 +1173,7 @@ Status GraphManager::BuildGraphForUnregisteredOp(const GraphId &graph_id, const auto compute_graph = GraphUtils::GetComputeGraph(*graph_node->GetGraph()); GE_CHECK_NOTNULL(compute_graph); - GM_RUN_AND_DUMP_PERF("Prepare", GetCompilerStages(graph_id).preparer.PrepareDynShape, graph_node->GetGraph(), inputs, + GM_RUN_AND_DUMP_PERF("Prepare", GetCompilerStages(graph_id).preparer.PrepareDynShape, graph_node, inputs, compute_graph, session_id); for (auto &node : compute_graph->GetAllNodes()) { diff --git a/ge/graph/preprocess/graph_preprocess.cc b/ge/graph/preprocess/graph_preprocess.cc index 32f877cf..3f4c7f16 100644 --- a/ge/graph/preprocess/graph_preprocess.cc +++ b/ge/graph/preprocess/graph_preprocess.cc @@ -898,6 +898,117 @@ Status ProcessNetoutputNodeDynShape(NodePtr &node) { } return SUCCESS; } +/** + * Parser shape_range from string to vector + * shape_range from option normally is "[1~20],[3],[3~6],[-1]" + * @param shape_range + */ +void ParseDynamicInputShapeRange(const std::string &shape_range, + std::vector>> &range) { + if (shape_range.empty() || shape_range.size() < 2) { + GELOGW("Shape range %s is invalid.", shape_range); + return; + } + // different parameter sets are split by ';' + vector shape_set = ge::StringUtils::Split(shape_range, ']'); + if (shape_set.empty()) { + return; + } + for (auto shape_str : shape_set) { + if (shape_str.empty()) { + continue; + } + if (ge::StringUtils::StartWith(shape_str, "[")) { + shape_str = shape_str.substr(1, shape_str.size()); + } + if (ge::StringUtils::StartWith(shape_str, ",")) { + shape_str = shape_str.substr(2, shape_str.size()); + } + std::vector> range_of_single; + vector range_set = ge::StringUtils::Split(shape_str, ','); + for (auto range_str : range_set) { + vector pair_set = ge::StringUtils::Split(range_str, '~'); + pair range_pair; + if (pair_set.size() == 1) { + auto range_value = atoi(pair_set.at(0).c_str()); + if (range_value < 0) { + range_pair = std::make_pair(1, range_value); + } else { + range_pair = std::make_pair(range_value, range_value); + } + } else if (pair_set.size() == 2) { + auto range_left = atoi(pair_set.at(0).c_str()); + auto range_right = atoi(pair_set.at(1).c_str()); + range_pair = std::make_pair(range_left, range_right); + } + range_of_single.emplace_back(range_pair); + } + range.emplace_back(range_of_single); + } +} + +Status GetDynamicInputShapeRange(const std::vector &user_input, const std::map &graph_option, + vector>> &range_vec) { + auto mode_iter = graph_option.find(OPTION_EXEC_DYNAMIC_EXECUTE_MODE); + if (mode_iter == graph_option.end()) { + GELOGD("Graph Option: Can not find %s option in graph options.", OPTION_EXEC_DYNAMIC_EXECUTE_MODE); + return SUCCESS; + } + GELOGD("Graph Option: dynamic_input_mode value is %s.", mode_iter->second.c_str()); + if (mode_iter->second != "dynamic_execute") { + return SUCCESS; + } + auto iter = graph_option.find(OPTION_EXEC_DATA_INPUTS_SHAPE_RANGE); + if (iter == graph_option.end()) { + GELOGE(PARAM_INVALID, "Graph option %s is required when %s is dynamic_execute", OPTION_EXEC_DATA_INPUTS_SHAPE_RANGE, + OPTION_EXEC_DYNAMIC_EXECUTE_MODE); + return PARAM_INVALID; + } + GELOGD("GraphOption: dynamic_inputs_shape_range value is %s.", iter->second.c_str()); + ParseDynamicInputShapeRange(iter->second, range_vec); + if (range_vec.size() != user_input.size()) { + GELOGE(PARAM_INVALID, "Dynamic input shape range size is %zu, inputs size is %zu. Not match.", range_vec.size(), + user_input.size()); + return PARAM_INVALID; + } + return SUCCESS; +} + +Status UpdateDynamicInputShapeRange(const ge::GeAttrValue::INT index, + const ector>> &range_vec, OpDescPtr &op, + GeTensorDesc &desc) { + auto unkown_shape = desc.GetShape(); + auto shape_range = range_vec.at(index); + for (size_t i = 0; i < unkown_shape.GetDimNum(); ++i) { + if (shape_range.at(i).first == shape_range.at(i).second) { + unkown_shape.SetDim(i, shape_range.at(i).first); + } else { + unkown_shape.SetDim(i, -1); + } + } + desc.SetShape(unkown_shape); + desc.SetShapeRange(shape_range); + int64_t dynamic_shape_size = 1; + for (const auto range_pair : range_vec.at(index)) { + FMK_INT64_MULCHECK(dynamic_shape_size, range_pair.second); + dynamic_shape_size *= range_pair.second; + } + auto data_type_size = GetSizeByDataType(desc.GetDataType()); + if (data_type_size < 0) { + GELOGE(PARAM_INVALID, "Input data type is %s, is not supported.", + TypeUtils::DataTypeToSerialString(desc.GetDataType()).c_str()); + return PARAM_INVALID; + } + FMK_INT64_MULCHECK(dynamic_shape_size, data_type_size); + dynamic_shape_size *= data_type_size; + GELOGI("In dynamic_execute mode ,set input %s shape range size %ld", op->GetName().c_str(), dynamic_shape_size); + ge::TensorUtils::SetSize(desc, dynamic_shape_size); + graphStatus graph_ret = op->UpdateInputDesc(0, desc); + GE_CHK_STATUS_RET(graph_ret, "UpdateInputDesc fail, graph ret: %u", graph_ret); + graph_ret = op->UpdateOutputDesc(0, desc); + GE_CHK_STATUS_RET(graph_ret, "UpdateInputDesc fail, graph ret: %u", graph_ret); + return SUCCESS; +} } // namespace GraphPrepare::GraphPrepare() : compute_graph_(nullptr) {} @@ -1102,7 +1213,11 @@ Status GraphPrepare::AdjustDataOpOutput(const NodePtr &node) { return SUCCESS; } -Status GraphPrepare::UpdateInput(const std::vector &user_input) { +Status GraphPrepare::UpdateInput(const std::vector &user_input, const std::map &graph_option) { + // Get shape range of input in dynamic_execute mode + vector>> dynamic_shape_range_vec; + auto ret = GetDynamicInputShapeRange(user_input, graph_option, dynamic_shape_range_vec); + GE_CHK_STATUS_RET(ret, "Graph option is not right on Dynamic execute mode."); compute_graph_->SaveDataFormat(ge::TypeUtils::DomiFormatToFormat(GetLocalOmgContext().format)); for (NodePtr &input_node : compute_graph_->GetDirectNode()) { GE_CHECK_NOTNULL(input_node); @@ -1185,6 +1300,12 @@ Status GraphPrepare::UpdateInput(const std::vector &user_input) { return graph_ret; } + if (!dynamic_shape_range_vec.empty()) { + ret = UpdateDynamicInputShapeRange(index, dynamic_shape_range_vec, op, desc); + GE_CHK_STATUS_RET(ret, "Fail to update dynamic input shape range on %s.", op->GetName().c_str()); + continue; + } + if (!options_.train_graph_flag) { Status ret = AdjustDataOpOutput(input_node); GE_IF_BOOL_EXEC(ret != SUCCESS, GELOGE(ret, "AdjustDataOpOutput fail, ret:%u", ret); return ret); @@ -1358,17 +1479,17 @@ Status GraphPrepare::SaveOriginalGraphToOmModel() { GELOGI("Prepare %s on graph %s success.", name, compute_graph->GetName().c_str()); \ } while (0) -Status GraphPrepare::PrepareDynShape(ConstGraphPtr graph, const std::vector &user_input, +Status GraphPrepare::PrepareDynShape(const GraphNodePtr &graph_node, const std::vector &user_input, ge::ComputeGraphPtr &compute_graph, uint64_t session_id) { - GE_CHECK_NOTNULL(graph); + GE_CHECK_NOTNULL(graph_node->GetGraph()); GE_CHECK_NOTNULL(compute_graph); GetLocalOmgContext().type = static_cast(options_.framework_type); - const Graph &const_graph = *graph; + const Graph &const_graph = *graph_node->GetGraph(); PP_RUN("Init", Init, const_graph, session_id); PP_RUN("SetRtContext", SetRtContext, rtContext_t(), RT_CTX_GEN_MODE); - PP_RUN_AND_DUMP("CheckAndUpdateInput", CheckAndUpdateInput, user_input); + PP_RUN_AND_DUMP("CheckAndUpdateInput", CheckAndUpdateInput, user_input, graph_node->GetGraph()); PP_RUN_AND_DUMP("GraphEquivalentTransformation", GraphEquivalentTransformation); PP_RUN_AND_DUMP("ProcessOutput", ProcessNetOutput); PP_RUN_AND_DUMP("ProcessMultiBatch", multibatch::ProcessMultiBatch, compute_graph_); @@ -1831,7 +1952,7 @@ Status GraphPrepare::ProcessNetOutput() { return SUCCESS; } -Status GraphPrepare::CheckAndUpdateInput(const std::vector &user_input) { +Status GraphPrepare::CheckAndUpdateInput(const std::vector &user_input,const std::map &graph_option) { compute_graph_->SetInputSize(user_input.size()); if (user_input.empty()) { return SUCCESS; @@ -1843,7 +1964,7 @@ Status GraphPrepare::CheckAndUpdateInput(const std::vector &user_input return ret; } - ret = UpdateInput(user_input); + ret = UpdateInput(user_input, graph_option); if (ret != SUCCESS) { GELOGE(ret, "UpdateInput fail, ret:%u", ret); return ret; diff --git a/ge/graph/preprocess/graph_preprocess.h b/ge/graph/preprocess/graph_preprocess.h index a3bbf433..de755418 100755 --- a/ge/graph/preprocess/graph_preprocess.h +++ b/ge/graph/preprocess/graph_preprocess.h @@ -45,7 +45,7 @@ class GraphPrepare { virtual ~GraphPrepare(); GraphPrepare(const GraphPrepare &in) = delete; GraphPrepare &operator=(const GraphPrepare &in) = delete; - Status PrepareDynShape(ConstGraphPtr graph, + Status PrepareDynShape(const GraphNodePtr &graph_node, const std::vector &user_input, ge::ComputeGraphPtr &compute_graph, uint64_t session_id = 0); @@ -63,8 +63,8 @@ class GraphPrepare { Status CheckRefOp(); Status SetRtContext(rtContext_t rt_context, rtCtxMode_t mode); Status AdjustDataOpOutput(const NodePtr &node); - Status UpdateInput(const std::vector &user_input); - Status CheckAndUpdateInput(const std::vector &user_input); + Status UpdateInput(const std::vector &user_input, const std::map &graph_option); + Status CheckAndUpdateInput(const std::vector &user_input, const std::map &graph_option); Status CheckConstOp(); Status VerifyConstOp(const NodePtr &node); Status CheckUserInput(const std::vector &user_input); diff --git a/inc/external/ge/ge_api_types.h b/inc/external/ge/ge_api_types.h index d0f2105f..250252f9 100644 --- a/inc/external/ge/ge_api_types.h +++ b/inc/external/ge/ge_api_types.h @@ -61,6 +61,11 @@ const char *const OPTION_EXEC_HCCL_FLAG = "ge.exec.hcclFlag"; const char *const OPTION_EXEC_ATOMIC_FLAG = "ge.exec.enable_atomic"; const char *const OPTION_EXEC_DISABLE_REUSED_MEMORY = "ge.exec.disableReuseMemory"; const char *const OPTION_EXEC_ENABLE_TAILING_OPTIMIZATION = "ge.exec.isTailingOptimization"; +// Dynamic input flag. ge.exec.dynamicInput=1, means enable dynaimc input, +// ge.exec.dynamicGraphExecuteMode, dynamic_execute[default] +const char *const OPTION_EXEC_DYNAMIC_INPUT = "ge.exec.dynamicInput"; +const char *const OPTION_EXEC_DYNAMIC_EXECUTE_MODE = "ge.exec.dynamicGraphExecuteMode"; +const char *const OPTION_EXEC_DATA_INPUTS_SHAPE_RANGE = "ge.exec.dataInputsShapeRange"; // Option key: memory init const char *const GRAPH_MEMORY_MAX_SIZE = "ge.graphMemoryMaxSize"; From 7bf75b0f6722199da29ac915e99212a238986af9 Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Wed, 30 Dec 2020 21:48:45 +0800 Subject: [PATCH 2/7] modified: ge/graph/preprocess/graph_preprocess.cc --- ge/graph/preprocess/graph_preprocess.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ge/graph/preprocess/graph_preprocess.cc b/ge/graph/preprocess/graph_preprocess.cc index 3f4c7f16..d12be957 100644 --- a/ge/graph/preprocess/graph_preprocess.cc +++ b/ge/graph/preprocess/graph_preprocess.cc @@ -906,7 +906,7 @@ Status ProcessNetoutputNodeDynShape(NodePtr &node) { void ParseDynamicInputShapeRange(const std::string &shape_range, std::vector>> &range) { if (shape_range.empty() || shape_range.size() < 2) { - GELOGW("Shape range %s is invalid.", shape_range); + GELOGW("Shape range %s is invalid.", shape_range.c_str()); return; } // different parameter sets are split by ';' @@ -975,7 +975,7 @@ Status GetDynamicInputShapeRange(const std::vector &user_input, const } Status UpdateDynamicInputShapeRange(const ge::GeAttrValue::INT index, - const ector>> &range_vec, OpDescPtr &op, + const vector>> &range_vec, OpDescPtr &op, GeTensorDesc &desc) { auto unkown_shape = desc.GetShape(); auto shape_range = range_vec.at(index); From e88abcf961e33ee9bc5d3da234b0355586224868 Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Wed, 30 Dec 2020 22:18:01 +0800 Subject: [PATCH 3/7] modified: ge/graph/preprocess/graph_preprocess.cc --- ge/graph/preprocess/graph_preprocess.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ge/graph/preprocess/graph_preprocess.cc b/ge/graph/preprocess/graph_preprocess.cc index d12be957..c45f4db6 100644 --- a/ge/graph/preprocess/graph_preprocess.cc +++ b/ge/graph/preprocess/graph_preprocess.cc @@ -37,6 +37,7 @@ #include "graph/passes/addn_pass.h" #include "graph/passes/aicpu_constant_folding_pass.h" #include "graph/passes/assert_pass.h" +#include "ge/ge_api_types.h" #ifdef ONLY_COMPILE_OPEN_SRC #include "graph/passes/assign_remove_pass.h" #endif @@ -1489,7 +1490,7 @@ Status GraphPrepare::PrepareDynShape(const GraphNodePtr &graph_node, const std:: PP_RUN("Init", Init, const_graph, session_id); PP_RUN("SetRtContext", SetRtContext, rtContext_t(), RT_CTX_GEN_MODE); - PP_RUN_AND_DUMP("CheckAndUpdateInput", CheckAndUpdateInput, user_input, graph_node->GetGraph()); + PP_RUN_AND_DUMP("CheckAndUpdateInput", CheckAndUpdateInput, user_input, graph_node->GetOptions()); PP_RUN_AND_DUMP("GraphEquivalentTransformation", GraphEquivalentTransformation); PP_RUN_AND_DUMP("ProcessOutput", ProcessNetOutput); PP_RUN_AND_DUMP("ProcessMultiBatch", multibatch::ProcessMultiBatch, compute_graph_); From 3e6b21f6c17b54a40cd0e59c7b321f71775a402f Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Thu, 31 Dec 2020 14:47:44 +0800 Subject: [PATCH 4/7] modified: ge/graph/preprocess/graph_preprocess.cc --- ge/graph/preprocess/graph_preprocess.cc | 114 ++++++++++++++++-------- 1 file changed, 75 insertions(+), 39 deletions(-) diff --git a/ge/graph/preprocess/graph_preprocess.cc b/ge/graph/preprocess/graph_preprocess.cc index c45f4db6..57c2542a 100644 --- a/ge/graph/preprocess/graph_preprocess.cc +++ b/ge/graph/preprocess/graph_preprocess.cc @@ -901,51 +901,74 @@ Status ProcessNetoutputNodeDynShape(NodePtr &node) { } /** * Parser shape_range from string to vector - * shape_range from option normally is "[1~20],[3],[3~6],[-1]" + * shape_range from option normally is "[1~20,3,3~6,-1],[1~20,3,3~6,-1]" * @param shape_range */ -void ParseDynamicInputShapeRange(const std::string &shape_range, - std::vector>> &range) { - if (shape_range.empty() || shape_range.size() < 2) { +Status ParseDynamicInputShapeRange(const std::string &shape_range, + std::vector>> &range) { + if (shape_range.size() < 2) { GELOGW("Shape range %s is invalid.", shape_range.c_str()); return; } - // different parameter sets are split by ';' - vector shape_set = ge::StringUtils::Split(shape_range, ']'); - if (shape_set.empty()) { - return; + // different shape_ragne of single input are split by ']' + vector shape_range_set = ge::StringUtils::Split(shape_range, ']'); + if (shape_range_set.empty()) { + GELOGE("Shape range %s is not valid. Correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", shape_range.c_str()); + return PARAM_INVALID; } - for (auto shape_str : shape_set) { - if (shape_str.empty()) { - continue; - } - if (ge::StringUtils::StartWith(shape_str, "[")) { - shape_str = shape_str.substr(1, shape_str.size()); + for (const auto &shape_range_str : shape_range_set) { + if (shape_range_str.empty()) { + GELOGE("Shape range of input is empty. Given %s, while correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", + shape_range.c_str()); + return PARAM_INVALID; } - if (ge::StringUtils::StartWith(shape_str, ",")) { - shape_str = shape_str.substr(2, shape_str.size()); + // trim start bytes, after that, single input should be "1~20,3,3~6,-1" + if (ge::StringUtils::StartWith(shape_range_str, "[")) { + shape_range_str = shape_range_str.substr(1, shape_range_str.size()); + } else if (ge::StringUtils::StartWith(shape_range_str, ",")) { + shape_range_str = shape_range_str.substr(2, shape_range_str.size()); + } else { + GELOGE("Shape range of input is invalid. Given %s, while correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", + shape_range.c_str()); + return PARAM_INVALID; } - std::vector> range_of_single; - vector range_set = ge::StringUtils::Split(shape_str, ','); - for (auto range_str : range_set) { - vector pair_set = ge::StringUtils::Split(range_str, '~'); + // parse shape_range of single input. eg. "1~20,3,3~6,-1" + std::vector> range_of_single_input; + vector dim_range_set = ge::StringUtils::Split(shape_range_str, ','); + for (const auto &range_pair_str : dim_range_set) { + vector range_pair_set = ge::StringUtils::Split(range_pair_str, '~'); pair range_pair; - if (pair_set.size() == 1) { - auto range_value = atoi(pair_set.at(0).c_str()); + if (range_pair_set.size() == 1) { + // fix dim + auto range_value = stol(range_pair_set.at(0).c_str()); if (range_value < 0) { range_pair = std::make_pair(1, range_value); } else { range_pair = std::make_pair(range_value, range_value); } - } else if (pair_set.size() == 2) { - auto range_left = atoi(pair_set.at(0).c_str()); - auto range_right = atoi(pair_set.at(1).c_str()); - range_pair = std::make_pair(range_left, range_right); + } else if (range_pair_set.size() == 2) { + // unknown dim, should get range. + try { + auto range_left = stol(range_pair_set.at(0).c_str()); + auto range_right = stol(range_pair_set.at(1).c_str()); + range_pair = std::make_pair(range_left, range_right); + } catch (const std::invalid_argument) { + GELOGE( + "Parse shape range of input failed when transfer from string to int64. Given %s, while correct example: " + "\"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", + shape_range.c_str()); + return PARAM_INVALID; + } + } else { + GELOGE("Shape range of input is invalid. Given %s, while correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", + shape_range.c_str()); + return PARAM_INVALID; } - range_of_single.emplace_back(range_pair); + range_of_single_input.emplace_back(range_pair); } - range.emplace_back(range_of_single); + range.emplace_back(range_of_single_input); } + return SUCCESS; } Status GetDynamicInputShapeRange(const std::vector &user_input, const std::map &graph_option, @@ -966,7 +989,8 @@ Status GetDynamicInputShapeRange(const std::vector &user_input, const return PARAM_INVALID; } GELOGD("GraphOption: dynamic_inputs_shape_range value is %s.", iter->second.c_str()); - ParseDynamicInputShapeRange(iter->second, range_vec); + auto ret = ParseDynamicInputShapeRange(iter->second, range_vec); + GE_CHK_STATUS_RET(ret, "Parse dynamic input shape range failed."); if (range_vec.size() != user_input.size()) { GELOGE(PARAM_INVALID, "Dynamic input shape range size is %zu, inputs size is %zu. Not match.", range_vec.size(), user_input.size()); @@ -978,18 +1002,30 @@ Status GetDynamicInputShapeRange(const std::vector &user_input, const Status UpdateDynamicInputShapeRange(const ge::GeAttrValue::INT index, const vector>> &range_vec, OpDescPtr &op, GeTensorDesc &desc) { - auto unkown_shape = desc.GetShape(); - auto shape_range = range_vec.at(index); - for (size_t i = 0; i < unkown_shape.GetDimNum(); ++i) { - if (shape_range.at(i).first == shape_range.at(i).second) { - unkown_shape.SetDim(i, shape_range.at(i).first); + auto origin_shape = desc.GetShape(); + auto current_shape_range_vec = range_vec.at(index); + if (current_shape_range_vec.size() != origin_shape.GetDimNum()) { + GELOGE(PARAM_INVALID, "Given shape_range dim num is %zu, current dim num is %zu, not match.Pleace Check.", + current_shape_range_vec.size(), origin_shape.GetDimNum()); + return PARAM_INVALID; + } + for (size_t i = 0; i < origin_shape.GetDimNum(); ++i) { + if (current_shape_range_vec.at(i).first == current_shape_range_vec.at(i).second) { + // given shape_range is known dim, check is same as origin or not + if (origin_shape.GetDim(i) != current_shape_range_vec.at(i).first) { + GELOGE(PARAM_INVALID, "Given shape range is %ld, current dim shape is %ld, not match.Pleace Check.", + current_shape_range_vec.at(i).first, origin_shape.GetDim(i)); + return PARAM_INVALID; + } + origin_shape.SetDim(i, current_shape_range_vec.at(i).first); } else { - unkown_shape.SetDim(i, -1); + origin_shape.SetDim(i, -1); } } - desc.SetShape(unkown_shape); - desc.SetShapeRange(shape_range); - int64_t dynamic_shape_size = 1; + desc.SetShape(origin_shape); + desc.SetShapeRange(current_shape_range_vec); + + /*int64_t dynamic_shape_size = 1; for (const auto range_pair : range_vec.at(index)) { FMK_INT64_MULCHECK(dynamic_shape_size, range_pair.second); dynamic_shape_size *= range_pair.second; @@ -1003,7 +1039,7 @@ Status UpdateDynamicInputShapeRange(const ge::GeAttrValue::INT index, FMK_INT64_MULCHECK(dynamic_shape_size, data_type_size); dynamic_shape_size *= data_type_size; GELOGI("In dynamic_execute mode ,set input %s shape range size %ld", op->GetName().c_str(), dynamic_shape_size); - ge::TensorUtils::SetSize(desc, dynamic_shape_size); + ge::TensorUtils::SetSize(desc, dynamic_shape_size);*/ graphStatus graph_ret = op->UpdateInputDesc(0, desc); GE_CHK_STATUS_RET(graph_ret, "UpdateInputDesc fail, graph ret: %u", graph_ret); graph_ret = op->UpdateOutputDesc(0, desc); From c07359baedff338e46fc7f54dd259c1aaa556deb Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Thu, 31 Dec 2020 14:57:14 +0800 Subject: [PATCH 5/7] modified: ge/graph/preprocess/graph_preprocess.cc --- ge/graph/preprocess/graph_preprocess.cc | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/ge/graph/preprocess/graph_preprocess.cc b/ge/graph/preprocess/graph_preprocess.cc index 57c2542a..f6a9ea80 100644 --- a/ge/graph/preprocess/graph_preprocess.cc +++ b/ge/graph/preprocess/graph_preprocess.cc @@ -907,18 +907,20 @@ Status ProcessNetoutputNodeDynShape(NodePtr &node) { Status ParseDynamicInputShapeRange(const std::string &shape_range, std::vector>> &range) { if (shape_range.size() < 2) { - GELOGW("Shape range %s is invalid.", shape_range.c_str()); - return; + GELOGE(PARAM_INVALID, "Shape range %s is invalid.", shape_range.c_str()); + return PARAM_INVALID; } // different shape_ragne of single input are split by ']' vector shape_range_set = ge::StringUtils::Split(shape_range, ']'); if (shape_range_set.empty()) { - GELOGE("Shape range %s is not valid. Correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", shape_range.c_str()); + GELOGE(PARAM_INVALID, "Shape range %s is not valid. Correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", + shape_range.c_str()); return PARAM_INVALID; } - for (const auto &shape_range_str : shape_range_set) { + for (auto &shape_range_str : shape_range_set) { if (shape_range_str.empty()) { - GELOGE("Shape range of input is empty. Given %s, while correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", + GELOGE(PARAM_INVALID, + "Shape range of input is empty. Given %s, while correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", shape_range.c_str()); return PARAM_INVALID; } @@ -928,7 +930,8 @@ Status ParseDynamicInputShapeRange(const std::string &shape_range, } else if (ge::StringUtils::StartWith(shape_range_str, ",")) { shape_range_str = shape_range_str.substr(2, shape_range_str.size()); } else { - GELOGE("Shape range of input is invalid. Given %s, while correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", + GELOGE(PARAM_INVALID, + "Shape range of input is invalid. Given %s, while correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", shape_range.c_str()); return PARAM_INVALID; } @@ -940,7 +943,7 @@ Status ParseDynamicInputShapeRange(const std::string &shape_range, pair range_pair; if (range_pair_set.size() == 1) { // fix dim - auto range_value = stol(range_pair_set.at(0).c_str()); + auto range_value = std::stol(range_pair_set.at(0).c_str()); if (range_value < 0) { range_pair = std::make_pair(1, range_value); } else { @@ -949,18 +952,20 @@ Status ParseDynamicInputShapeRange(const std::string &shape_range, } else if (range_pair_set.size() == 2) { // unknown dim, should get range. try { - auto range_left = stol(range_pair_set.at(0).c_str()); - auto range_right = stol(range_pair_set.at(1).c_str()); + auto range_left = std::stol(range_pair_set.at(0).c_str()); + auto range_right = std::stol(range_pair_set.at(1).c_str()); range_pair = std::make_pair(range_left, range_right); } catch (const std::invalid_argument) { GELOGE( + PARAM_INVALID, "Parse shape range of input failed when transfer from string to int64. Given %s, while correct example: " "\"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", shape_range.c_str()); return PARAM_INVALID; } } else { - GELOGE("Shape range of input is invalid. Given %s, while correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", + GELOGE(PARAM_INVALID, + "Shape range of input is invalid. Given %s, while correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", shape_range.c_str()); return PARAM_INVALID; } From 0ed8136d003d3455ed0fba51ae6334a685d19fdf Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Mon, 4 Jan 2021 22:49:19 +0800 Subject: [PATCH 6/7] modified: ge/graph/preprocess/graph_preprocess.cc --- ge/graph/preprocess/graph_preprocess.cc | 52 +++++++++++++------------ 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/ge/graph/preprocess/graph_preprocess.cc b/ge/graph/preprocess/graph_preprocess.cc index f6a9ea80..2ae39b3c 100644 --- a/ge/graph/preprocess/graph_preprocess.cc +++ b/ge/graph/preprocess/graph_preprocess.cc @@ -899,6 +899,23 @@ Status ProcessNetoutputNodeDynShape(NodePtr &node) { } return SUCCESS; } +long StringToLongNoThrow(const string &str) { + try { + return std::stol(str); + } catch (const std::invalid_argument) { + GELOGE(PARAM_INVALID, + "Parse shape range of input failed when transfer from string to int64. Given %s, while correct example: " + "\"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", + str.c_str()); + return PARAM_INVALID; + } catch (const std::out_of_range) { + GELOGE(PARAM_INVALID, + "Parse shape range of input failed when transfer from string to int64. Given %s, while correct example: " + "\"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", + str.c_str()); + return PARAM_INVALID; + } +} /** * Parser shape_range from string to vector * shape_range from option normally is "[1~20,3,3~6,-1],[1~20,3,3~6,-1]" @@ -910,7 +927,7 @@ Status ParseDynamicInputShapeRange(const std::string &shape_range, GELOGE(PARAM_INVALID, "Shape range %s is invalid.", shape_range.c_str()); return PARAM_INVALID; } - // different shape_ragne of single input are split by ']' + // different shape_range of single input are split by ']' vector shape_range_set = ge::StringUtils::Split(shape_range, ']'); if (shape_range_set.empty()) { GELOGE(PARAM_INVALID, "Shape range %s is not valid. Correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", @@ -919,22 +936,16 @@ Status ParseDynamicInputShapeRange(const std::string &shape_range, } for (auto &shape_range_str : shape_range_set) { if (shape_range_str.empty()) { - GELOGE(PARAM_INVALID, - "Shape range of input is empty. Given %s, while correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", - shape_range.c_str()); - return PARAM_INVALID; + continue; } // trim start bytes, after that, single input should be "1~20,3,3~6,-1" if (ge::StringUtils::StartWith(shape_range_str, "[")) { shape_range_str = shape_range_str.substr(1, shape_range_str.size()); - } else if (ge::StringUtils::StartWith(shape_range_str, ",")) { + } + if (ge::StringUtils::StartWith(shape_range_str, ",")) { shape_range_str = shape_range_str.substr(2, shape_range_str.size()); - } else { - GELOGE(PARAM_INVALID, - "Shape range of input is invalid. Given %s, while correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", - shape_range.c_str()); - return PARAM_INVALID; } + // parse shape_range of single input. eg. "1~20,3,3~6,-1" std::vector> range_of_single_input; vector dim_range_set = ge::StringUtils::Split(shape_range_str, ','); @@ -943,26 +954,17 @@ Status ParseDynamicInputShapeRange(const std::string &shape_range, pair range_pair; if (range_pair_set.size() == 1) { // fix dim - auto range_value = std::stol(range_pair_set.at(0).c_str()); + auto range_value = StringToLongNoThrow(range_pair_set.at(0).c_str()); if (range_value < 0) { - range_pair = std::make_pair(1, range_value); + range_pair = std::make_pair(0, range_value); } else { range_pair = std::make_pair(range_value, range_value); } } else if (range_pair_set.size() == 2) { // unknown dim, should get range. - try { - auto range_left = std::stol(range_pair_set.at(0).c_str()); - auto range_right = std::stol(range_pair_set.at(1).c_str()); - range_pair = std::make_pair(range_left, range_right); - } catch (const std::invalid_argument) { - GELOGE( - PARAM_INVALID, - "Parse shape range of input failed when transfer from string to int64. Given %s, while correct example: " - "\"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", - shape_range.c_str()); - return PARAM_INVALID; - } + auto range_left = StringToLongNoThrow(range_pair_set.at(0).c_str()); + auto range_right = StringToLongNoThrow(range_pair_set.at(1).c_str()); + range_pair = std::make_pair(range_left, range_right); } else { GELOGE(PARAM_INVALID, "Shape range of input is invalid. Given %s, while correct example: \"[1~20,3,3~6,-1],[1~20,3,3~6,-1]\"", From 50b8b31008c7619c7521eb1e318523c52dd0c917 Mon Sep 17 00:00:00 2001 From: zhaoxinxin Date: Mon, 4 Jan 2021 23:20:38 +0800 Subject: [PATCH 7/7] modified: ge/graph/preprocess/graph_preprocess.cc --- ge/graph/preprocess/graph_preprocess.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ge/graph/preprocess/graph_preprocess.cc b/ge/graph/preprocess/graph_preprocess.cc index 2ae39b3c..9672c497 100644 --- a/ge/graph/preprocess/graph_preprocess.cc +++ b/ge/graph/preprocess/graph_preprocess.cc @@ -1032,7 +1032,7 @@ Status UpdateDynamicInputShapeRange(const ge::GeAttrValue::INT index, desc.SetShape(origin_shape); desc.SetShapeRange(current_shape_range_vec); - /*int64_t dynamic_shape_size = 1; + int64_t dynamic_shape_size = 1; for (const auto range_pair : range_vec.at(index)) { FMK_INT64_MULCHECK(dynamic_shape_size, range_pair.second); dynamic_shape_size *= range_pair.second; @@ -1046,7 +1046,7 @@ Status UpdateDynamicInputShapeRange(const ge::GeAttrValue::INT index, FMK_INT64_MULCHECK(dynamic_shape_size, data_type_size); dynamic_shape_size *= data_type_size; GELOGI("In dynamic_execute mode ,set input %s shape range size %ld", op->GetName().c_str(), dynamic_shape_size); - ge::TensorUtils::SetSize(desc, dynamic_shape_size);*/ + ge::TensorUtils::SetSize(desc, dynamic_shape_size); graphStatus graph_ret = op->UpdateInputDesc(0, desc); GE_CHK_STATUS_RET(graph_ret, "UpdateInputDesc fail, graph ret: %u", graph_ret); graph_ret = op->UpdateOutputDesc(0, desc);