You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

multi_batch_clone_pass.cc 65 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "graph/passes/multi_batch_clone_pass.h"
  17. #include "common/formats/utils/formats_trans_utils.h"
  18. #include "common/ge/ge_util.h"
  19. #include "graph/common/local_context.h"
  20. #include "graph/preprocess/multi_batch_options.h"
  21. #include "graph/utils/node_utils.h"
  22. #include "graph/utils/op_desc_utils.h"
  23. #include "graph/utils/tensor_utils.h"
  24. #include "graph/utils/type_utils.h"
  25. #include "register/op_registry.h"
  26. #include "graph/common/omg_util.h"
  27. namespace ge {
  28. namespace {
  29. constexpr uint8_t kDataInIndex = 0;
  30. constexpr uint8_t kDataOutIndex = 0;
  31. constexpr uint8_t kCaseArgIndex = 1;
  32. const int kDivisionConst = 2;
  33. const size_t kNumOfGetnextNode = 1;
  34. const std::string kMultiBatchCaseNode = "ascend_mbatch_shape_case";
  35. const std::string kMultiBatchDataNode = "ascend_mbatch_shape_data";
  36. const std::string kMultiBatchGetDynamicDimsNode = "ascend_mbatch_get_dynamic_dims_node";
  37. const std::string kMultiBatchConstNode = "ascend_mbatch_shape_const";
  38. const std::string kMultiBatchMapIndexNode = "ascend_mbatch_shape_mapindex";
  39. const std::string kMultiBatchNodePostfix = "_ascend_mbatch_batch_";
  40. const char *const kGetNextName = "IteratorV2";
  41. const char *const kMbatchCaseName = "mbatch-switch-name";
  42. } // namespace
  43. inline bool IsGetNextType(const NodePtr &node) {
  44. std::string original_type;
  45. GE_IF_BOOL_EXEC(GetOriginalType(node, original_type) != SUCCESS,
  46. GELOGW("Get original type failed."); return false);
  47. return (original_type == kGetNextName);
  48. }
  49. Status MultiBatchClonePass::Run(ComputeGraphPtr graph) {
  50. GE_IF_BOOL_EXEC(graph == nullptr,
  51. REPORT_INNER_ERROR("E19999", "Param graph is nullptr, check invalid");
  52. GELOGE(FAILED, "Original graph is nullptr"); return FAILED);
  53. if (graph->GetParentGraph() != nullptr) {
  54. GELOGD("Subgraph %s skip the MultiBatchClonePass", graph->GetName().c_str());
  55. return SUCCESS;
  56. }
  57. if (!GetLocalOmgContext().need_multi_batch) {
  58. GELOGI("No need to process_multi for no_train graph.");
  59. return SUCCESS;
  60. }
  61. std::vector<NodePtr> data_nodes;
  62. std::vector<NodePtr> getnext_nosink_nodes;
  63. std::vector<NodePtr> getnext_sink_nodes;
  64. if (multibatch::CheckSequenceOfOptions(graph, data_nodes, getnext_nosink_nodes, getnext_sink_nodes) != SUCCESS) {
  65. GELOGE(PARAM_INVALID, "[Train_Dynamic] CheckSequenceOfOptions failed.");
  66. return PARAM_INVALID;
  67. }
  68. if (multibatch::UpdateNameOfInputShape(graph, data_nodes, getnext_nosink_nodes, getnext_sink_nodes) != SUCCESS) {
  69. GELOGE(PARAM_INVALID, "[Train_Dynamic] UpdateNameForInputShapeOfOption failed.");
  70. return PARAM_INVALID;
  71. }
  72. if (multibatch::DeleteIdentityInsertByAdapter(graph) != SUCCESS) {
  73. GELOGE(PARAM_INVALID, "[Train_Dynamic] DeleteIdentityInsertByAdapter failed.");
  74. return PARAM_INVALID;
  75. }
  76. if (!multibatch::InitDynamicParams(batch_shapes_)) {
  77. GELOGD("There is no multi-batch options, no need clone multi-batch graph");
  78. return SUCCESS;
  79. }
  80. if (multibatch::CheckNegativeCountOfOptions(batch_shapes_) != SUCCESS) {
  81. GELOGE(PARAM_INVALID, "[Train_Dynamic] Input_shape and dynamic_dims should set correct params.");
  82. return PARAM_INVALID;
  83. }
  84. GELOGD("Begin to run Multi-batch clone on graph: %s", graph->GetName().c_str());
  85. GE_CHK_STATUS_RET(multibatch::CheckDynamicParams(batch_shapes_), "Invalid multi-batch param");
  86. if (CollectIoNodes(graph) != SUCCESS) {
  87. GELOGE(INTERNAL_ERROR, "Collect input output nodes failed");
  88. return INTERNAL_ERROR;
  89. }
  90. // parser data dynamic info from atc parameter --input_shape
  91. if (CheckAndParseDynamicData() != SUCCESS) {
  92. GELOGE(PARAM_INVALID, "Parse each data's own dynamic info failed");
  93. return PARAM_INVALID;
  94. }
  95. (void)AttrUtils::GetStr(graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id_);
  96. ComputeGraphPtr branch = MakeShared<ComputeGraph>(graph->GetName());
  97. GE_IF_BOOL_EXEC(branch == nullptr,
  98. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  99. GELOGE(OUT_OF_MEMORY, "Create multi batch graph failed"); return OUT_OF_MEMORY);
  100. (void)AttrUtils::SetStr(branch, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id_);
  101. graph->InValid(); // Will modify, need topological again.
  102. graph->Swap(*branch);
  103. GE_CHK_STATUS_RET(CreateRootGraph(graph), "Construct root graph failed.");
  104. GE_CHK_STATUS_RET(CreateOriGraph(branch), "Construct original graph failed.")
  105. GE_CHK_STATUS_RET(CreateSubgraphs(graph, branch), "Construct subgraph failed.");
  106. GE_CHK_STATUS_RET(PruneDirectOutput(graph), "Prune direct output failed");
  107. GE_CHK_STATUS_RET(UpdateSubgraphOutput(), "Update subgraph output failed");
  108. GELOGD("MultiBatchClonePass Leave");
  109. return SUCCESS;
  110. }
  111. ///
  112. /// @ingroup ge
  113. /// @brief Collect input output node from original graph.
  114. /// @param [in] const ComputeGraphPtr &graph: original graph.
  115. /// @return 0: SUCCESS / others: FAILED
  116. ///
  117. Status MultiBatchClonePass::CollectIoNodes(const ComputeGraphPtr &graph) {
  118. for (const auto &node : graph->GetDirectNode()) {
  119. if (!GetLocalOmgContext().dynamic_node_type.empty() && IsGetNextType(node)) {
  120. all_data_nodes_.emplace_back(node);
  121. GE_CHK_STATUS_RET(InitParamsOfGetNext(node), "Init params of %s failed.", node->GetName().c_str());
  122. }
  123. if (node->GetType() == DATA) {
  124. all_data_nodes_.emplace_back(node);
  125. } else if (node->GetType() == CONSTANT || node->GetType() == CONSTANTOP) {
  126. all_const_nodes_.emplace_back(node);
  127. } else if (node->GetType() == NETOUTPUT) {
  128. all_output_nodes_.emplace_back(node);
  129. }
  130. // If the node save as input/output node, delete record.
  131. (void)graph->RemoveInputNode(node);
  132. (void)graph->RemoveOutputNode(node);
  133. }
  134. if (all_data_nodes_.empty() || all_output_nodes_.size() != 1) {
  135. REPORT_INNER_ERROR("E19999", "Data node num is 0 or output node num != 1, graph:%s, check invalid",
  136. graph->GetName().c_str());
  137. GELOGE(FAILED, "data nodes: %zu, output nodes: %zu", all_data_nodes_.size(), all_output_nodes_.size());
  138. return FAILED;
  139. }
  140. int64_t data_index = 0;
  141. size_t getnext_node_count = 0;
  142. for (size_t i = 0; i < all_data_nodes_.size(); ++i) {
  143. if (IsGetNextType(all_data_nodes_[i])) {
  144. // just one getnext node in graph
  145. getnext_node_count++;
  146. continue;
  147. }
  148. const auto &op_desc = all_data_nodes_[i]->GetOpDesc();
  149. if (!AttrUtils::GetInt(op_desc, ATTR_NAME_INDEX, data_index)) {
  150. (void)AttrUtils::SetInt(op_desc, ATTR_NAME_INDEX, i - getnext_node_count);
  151. }
  152. }
  153. const auto &output = all_output_nodes_[0];
  154. for (size_t i = 0; i < output->GetAllInDataAnchorsSize(); ++i) {
  155. const auto in_anchor = output->GetInDataAnchor(i);
  156. const auto out_anchor = in_anchor->GetPeerOutAnchor();
  157. const auto data_node = out_anchor->GetOwnerNode();
  158. if (data_node->GetType() == DATA) {
  159. direct_output_[i] = data_node->GetName();
  160. GE_CHK_GRAPH_STATUS_RET(
  161. GraphUtils::RemoveEdge(data_node->GetOutDataAnchor(kDataOutIndex), output->GetInDataAnchor(i)),
  162. "Remove edge failed");
  163. }
  164. }
  165. GELOGD("Data count is %zu, const count is %zu, getnext count is %zu, output count is %zu, direct out count is %zu.",
  166. all_data_nodes_.size(), all_const_nodes_.size(), getnext_node_count, all_output_nodes_.size(),
  167. direct_output_.size());
  168. return SUCCESS;
  169. }
  170. Status MultiBatchClonePass::CheckAndParseDynamicData() {
  171. size_t unknown_shape_count = 0;
  172. auto data_name_and_shape = GetLocalOmgContext().user_input_dims;
  173. std::vector<std::string> data_name_order;
  174. for (auto &item : data_name_and_shape) {
  175. data_name_order.push_back(item.first);
  176. }
  177. if (!getnext_sink_dynamic_dims_) {
  178. for (const auto &node : all_data_nodes_) {
  179. auto data_desc = NodeUtils::GetOutputDesc(*node, kDataOutIndex);
  180. auto data_shape = data_desc.GetShape();
  181. auto data_format = data_desc.GetFormat() == Format::FORMAT_NCHW ? "NCHW" :
  182. data_desc.GetFormat() == Format::FORMAT_NHWC ? "NHWC" : "Others";
  183. auto data_name = node->GetName();
  184. const auto &data_shape_dims = data_shape.GetDims();
  185. if (std::all_of(data_shape_dims.begin(), data_shape_dims.end(), [](int64_t val) { return val >= 0; })) {
  186. continue;
  187. }
  188. ++unknown_shape_count;
  189. auto iter = find(data_name_order.begin(), data_name_order.end(), data_name);
  190. if (iter == data_name_order.end()) {
  191. if (!GetLocalOmgContext().dynamic_batch_size.empty()) {
  192. auto ret = multibatch::CheckDynamicBatchShape(data_shape_dims, data_name);
  193. GE_IF_BOOL_EXEC(ret == false, GELOGE(PARAM_INVALID, "Failed to check dynamic batch shape of %s.",
  194. data_name.c_str()); return PARAM_INVALID);
  195. } else if (!GetLocalOmgContext().dynamic_image_size.empty()) {
  196. auto ret = multibatch::CheckDynamicImageSizeShape(data_shape_dims, data_name, data_format);
  197. GE_IF_BOOL_EXEC(ret == false, GELOGE(PARAM_INVALID, "Failed to check dynamic image size shape of %s.",
  198. data_name.c_str()); return PARAM_INVALID);
  199. } else if (!GetLocalOmgContext().dynamic_dims.empty()) {
  200. ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
  201. {"--dynamic_dims", data_name, "all dynamic node must be set in --input_shape, please check"});
  202. GELOGE(INTERNAL_ERROR, "data: %s shape:%s must be set int --input_shape",
  203. node->GetName().c_str(), data_shape.ToString().c_str());
  204. return INTERNAL_ERROR;
  205. }
  206. data_name_and_shape.emplace_back(data_name, data_shape_dims);
  207. }
  208. }
  209. }
  210. auto ret = multibatch::ParserDataToDynamicInfo(batch_shapes_, data_name_and_shape, data_to_dynamic_info_);
  211. GE_CHK_STATUS_RET(ret, "Failed to parse data to dynamic info.");
  212. if (!getnext_sink_dynamic_dims_ && unknown_shape_count == 0) {
  213. ErrorManager::GetInstance().ATCReportErrMessage("E10040");
  214. GELOGE(PARAM_INVALID,
  215. "Need unknow shape data when user set --dynamic_batch_size, --dynamic_image_size or --dynamic_dims");
  216. return PARAM_INVALID;
  217. }
  218. return SUCCESS;
  219. }
  220. Status MultiBatchClonePass::InitParamsOfGetNext(const NodePtr &node) {
  221. data_count_from_getnext_ = 0;
  222. getnext_sink_dynamic_dims_ = false;
  223. GE_CHECK_NOTNULL(node->GetOpDesc());
  224. data_count_from_getnext_ = node->GetOpDesc()->GetOutputsSize();
  225. if (GetLocalOmgContext().dynamic_node_type == GETNEXT) {
  226. data_count_from_getnext_ = data_count_from_getnext_ / kDivisionConst;
  227. for (size_t i = 0; i < data_count_from_getnext_; ++i) {
  228. GeTensorDesc output_desc = node->GetOpDesc()->GetOutputDesc(i);
  229. GELOGD("The %zu data shape from getnext sink is %s.", i,
  230. formats::JoinToString(output_desc.GetShape().GetDims()).c_str());
  231. const auto &dims = output_desc.GetShape().GetDims();
  232. if (std::all_of(dims.begin(), dims.end(), [](int64_t val) {return val >= 0; })) {
  233. GELOGD("The %zu data from %s is static.", i, node->GetName().c_str());
  234. } else {
  235. getnext_sink_dynamic_dims_ = true;
  236. GELOGD("Dynamic dims in the pattern of getnext sink.");
  237. }
  238. }
  239. }
  240. if (node->GetOutControlAnchor() != nullptr) {
  241. for (const auto &peer_in_control_anchor : node->GetOutControlAnchor()->GetPeerInControlAnchors()) {
  242. NodePtr next_node = peer_in_control_anchor->GetOwnerNode();
  243. GE_CHECK_NOTNULL(next_node);
  244. if (next_node->GetType() == CONSTANTOP) {
  245. out_control_nodes_.insert(next_node);
  246. GELOGD("Control edge: %s connect with %s.", node->GetName().c_str(), next_node->GetName().c_str());
  247. }
  248. }
  249. }
  250. return SUCCESS;
  251. }
  252. ///
  253. /// @ingroup ge
  254. /// @brief Create nodes for root graph.
  255. /// @param [in] const ComputeGraphPtr &graph: Root/Case graph.
  256. /// @return 0: SUCCESS / others: FAILED
  257. ///
  258. Status MultiBatchClonePass::CreateRootGraph(const ComputeGraphPtr &graph) {
  259. GELOGD("Start create root graph of %s.", graph->GetName().c_str());
  260. uint32_t input_num = all_data_nodes_.size() + all_const_nodes_.size();
  261. if (data_count_from_getnext_ != 0) {
  262. input_num = input_num + data_count_from_getnext_ - kNumOfGetnextNode;
  263. }
  264. uint32_t output_num = all_output_nodes_[0]->GetAllInDataAnchorsSize();
  265. OpDescBuilder op_builder(kMultiBatchCaseNode, CASE);
  266. op_builder.AddInput("branch_index").AddDynamicInput("input", input_num).AddDynamicOutput("output", output_num);
  267. const OpDescPtr op_desc = op_builder.Build();
  268. if (op_desc == nullptr) {
  269. REPORT_INNER_ERROR("E19999", "Build op:%s(%s) failed", kMultiBatchCaseNode.c_str(), CASE);
  270. GELOGE(OUT_OF_MEMORY, "Create multi-batch case desc failed");
  271. return OUT_OF_MEMORY;
  272. }
  273. op_desc->RegisterSubgraphIrName("branches", kDynamic);
  274. case_node_ = graph->AddNode(op_desc);
  275. if (case_node_ == nullptr) {
  276. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  277. op_desc->GetName().c_str(), op_desc->GetType().c_str(), graph->GetName().c_str());
  278. GELOGE(OUT_OF_MEMORY, "Create multi-batch case node failed");
  279. return OUT_OF_MEMORY;
  280. }
  281. uint32_t batch_num = static_cast<uint32_t>(batch_shapes_.size());
  282. if (!AttrUtils::SetInt(op_desc, ATTR_NAME_BATCH_NUM, batch_num)) {
  283. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NAME_BATCH_NUM.c_str(),
  284. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  285. GELOGE(FAILED, "Set attr ATTR_NAME_BATCH_NUM failed, Case: %s.", op_desc->GetName().c_str());
  286. return FAILED;
  287. }
  288. for (uint32_t i = 0; i < batch_num; i++) {
  289. const std::string &attr_name = ATTR_NAME_PRED_VALUE + "_" + std::to_string(i);
  290. if (!AttrUtils::SetListInt(op_desc, attr_name, batch_shapes_[i])) {
  291. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", attr_name.c_str(),
  292. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  293. GELOGE(FAILED, "Set attr ATTR_NAME_PRED_VALUE failed, Case: %s.", op_desc->GetName().c_str());
  294. return FAILED;
  295. }
  296. }
  297. std::vector<std::string> data_name_order;
  298. for (auto &item : GetLocalOmgContext().user_input_dims) {
  299. data_name_order.push_back(item.first);
  300. }
  301. if (!AttrUtils::SetListStr(op_desc, ATTR_USER_DESIGNEATE_SHAPE_ORDER, data_name_order)) {
  302. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_USER_DESIGNEATE_SHAPE_ORDER.c_str(),
  303. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  304. GELOGE(FAILED, "Failed to add user designate shape order attr on case node %s",
  305. op_desc->GetName().c_str());
  306. return FAILED;
  307. }
  308. if (!AttrUtils::SetBool(op_desc, ATTR_INSERT_BY_MBATCH, true)) {
  309. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_INSERT_BY_MBATCH.c_str(),
  310. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  311. GELOGE(INTERNAL_ERROR, "Failed to add insert attr on case node %s", op_desc->GetName().c_str());
  312. return INTERNAL_ERROR;
  313. }
  314. GE_CHK_STATUS_RET(multibatch::StampDynamicType(op_desc), "Set dynamic type failed");
  315. GE_CHK_STATUS_RET(CreateIndexNode(graph), "Create index node failed");
  316. GE_CHK_STATUS_RET(CreateInputNode(graph), "Create input node failed");
  317. GE_CHK_STATUS_RET(CreateConstNode(graph), "Create const node failed");
  318. GE_CHK_STATUS_RET(CreateOutputNode(graph), "Create output node failed");
  319. return SUCCESS;
  320. }
  321. ///
  322. /// @ingroup ge
  323. /// @brief Create index data node for root graph.
  324. /// @param [in] const ComputeGraphPtr &graph: Root/Case graph.
  325. /// @param [in] NodePtr node: index data node.
  326. /// @return 0: SUCCESS / others: FAILED
  327. ///
  328. Status MultiBatchClonePass::CreateIndexDataNode(const ComputeGraphPtr &graph, NodePtr &shape_node) {
  329. const OpDescPtr data_desc = MakeShared<OpDesc>(kMultiBatchDataNode, DATA);
  330. if (data_desc == nullptr) {
  331. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  332. GELOGE(OUT_OF_MEMORY, "Create multi-batch data node failed");
  333. return FAILED;
  334. }
  335. GeTensorDesc data_tensor(GeShape({static_cast<int64_t>(batch_shapes_[0].size())}), FORMAT_ND, DT_INT32);
  336. if (data_desc->AddInputDesc(data_tensor) != GRAPH_SUCCESS) {
  337. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  338. data_desc->GetName().c_str(), data_desc->GetType().c_str());
  339. GELOGE(FAILED, "Add input desc failed");
  340. return FAILED;
  341. }
  342. if (data_desc->AddOutputDesc(data_tensor) != GRAPH_SUCCESS) {
  343. REPORT_CALL_ERROR("E19999", "Add ouput desc to op:%s(%s) failed",
  344. data_desc->GetName().c_str(), data_desc->GetType().c_str());
  345. GELOGE(FAILED, "Add output desc failed");
  346. return FAILED;
  347. }
  348. size_t data_index = all_data_nodes_.size();
  349. data_index = data_count_from_getnext_ != 0 ? data_index - kNumOfGetnextNode : data_index;
  350. (void)AttrUtils::SetInt(data_desc, ATTR_NAME_INDEX, data_index);
  351. (void)AttrUtils::SetBool(data_desc, ATTR_INSERT_BY_MBATCH, true);
  352. shape_node = graph->AddNode(data_desc);
  353. if (shape_node == nullptr) {
  354. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  355. data_desc->GetName().c_str(), data_desc->GetType().c_str(), graph->GetName().c_str());
  356. GELOGE(OUT_OF_MEMORY, "Create multi-batch data node failed");
  357. return OUT_OF_MEMORY;
  358. }
  359. return SUCCESS;
  360. }
  361. ///
  362. /// @ingroup ge
  363. /// @brief Create index const node for root graph.
  364. /// @param [in] const ComputeGraphPtr &graph: Root/Case graph.
  365. /// @param [in] NodePtr node: index const node.
  366. /// @return 0: SUCCESS / others: FAILED
  367. ///
  368. Status MultiBatchClonePass::CreateIndexConstNode(const ComputeGraphPtr &graph, NodePtr &node) {
  369. const OpDescPtr const_desc = MakeShared<OpDesc>(kMultiBatchConstNode, CONSTANT);
  370. if (const_desc == nullptr) {
  371. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  372. GELOGE(OUT_OF_MEMORY, "Create multi-batch const node failed");
  373. return FAILED;
  374. }
  375. int64_t count = batch_shapes_.size() * batch_shapes_[0].size();
  376. std::unique_ptr<int32_t[]> addr(new (std::nothrow) int32_t[count]);
  377. GE_CHECK_NOTNULL(addr);
  378. size_t i = 0;
  379. for (auto &batch_shape : batch_shapes_) {
  380. for (int64_t dim : batch_shape) {
  381. addr[i++] = static_cast<int32_t>(dim);
  382. }
  383. }
  384. GeTensorDesc const_tensor(GeShape({count}), FORMAT_ND, DT_INT32);
  385. GeTensor tensor(const_tensor);
  386. (void)tensor.SetData(reinterpret_cast<uint8_t *>(addr.get()), count * sizeof(int32_t));
  387. if (!AttrUtils::SetTensor(const_desc, ATTR_NAME_WEIGHTS, tensor)) {
  388. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NAME_WEIGHTS.c_str(),
  389. const_desc->GetName().c_str(), const_desc->GetType().c_str());
  390. GELOGE(OUT_OF_MEMORY, "Failed to init tensor value for const %s", const_desc->GetName().c_str());
  391. return FAILED;
  392. }
  393. if (const_desc->AddOutputDesc(const_tensor) != GRAPH_SUCCESS) {
  394. REPORT_CALL_ERROR("E19999", "Add ouput desc to op:%s(%s) failed",
  395. const_desc->GetName().c_str(), const_desc->GetType().c_str());
  396. GELOGE(OUT_OF_MEMORY, "Failed to add output desc for const node %s", const_desc->GetName().c_str());
  397. return FAILED;
  398. }
  399. node = graph->AddNode(const_desc);
  400. if (node == nullptr) {
  401. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  402. const_desc->GetName().c_str(), const_desc->GetType().c_str(), graph->GetName().c_str());
  403. GELOGE(OUT_OF_MEMORY, "Create multi-batch const node failed");
  404. return OUT_OF_MEMORY;
  405. }
  406. return SUCCESS;
  407. }
  408. ///
  409. /// @ingroup ge
  410. /// @brief Create index node for root graph.
  411. /// @param [in] const ComputeGraphPtr &graph: Root/Case graph.
  412. /// @return 0: SUCCESS / others: FAILED
  413. ///
  414. Status MultiBatchClonePass::CreateIndexNode(const ComputeGraphPtr &graph) {
  415. // Data/GetDynamicDims --> MapIndex --> Case
  416. if (!getnext_sink_dynamic_dims_) {
  417. GE_CHK_STATUS_RET(CreateIndexDataNode(graph, shape_node_), "Create data node failed");
  418. } else {
  419. GE_CHK_STATUS_RET(CreateGetDynamicDimsNode(graph, shape_node_), "Create get dynamic dims node failed");
  420. }
  421. NodePtr const_node;
  422. GE_CHK_STATUS_RET(CreateIndexConstNode(graph, const_node), "Create const node failed");
  423. GELOGD("Shape node name is %s, type is %s, const node name is %s.", shape_node_->GetName().c_str(),
  424. shape_node_->GetType().c_str(), const_node->GetName().c_str());
  425. OpDescBuilder op_builder(kMultiBatchMapIndexNode, "MapIndex");
  426. op_builder.AddInput("x", shape_node_->GetOpDesc()->GetOutputDesc(0))
  427. .AddInput("data_seq", const_node->GetOpDesc()->GetOutputDesc(0))
  428. .AddOutput("y", GeTensorDesc(GeShape(), FORMAT_ND, DT_INT32));
  429. const OpDescPtr op_desc = op_builder.Build();
  430. if (op_desc == nullptr) {
  431. REPORT_INNER_ERROR("E19999", "Build op:%s(%s) failed", kMultiBatchMapIndexNode.c_str(), "MapIndex");
  432. GELOGE(OUT_OF_MEMORY, "Create multi-batch index desc failed");
  433. return FAILED;
  434. }
  435. NodePtr index_node = graph->AddNode(op_desc);
  436. if (index_node == nullptr) {
  437. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  438. op_desc->GetName().c_str(), op_desc->GetType().c_str(), graph->GetName().c_str());
  439. GELOGE(OUT_OF_MEMORY, "Create multi-batch index node failed");
  440. return OUT_OF_MEMORY;
  441. }
  442. GE_CHK_STATUS_RET(AddAttrForGetDynamicDims(shape_node_), "Failed to add attr for %s.",
  443. shape_node_->GetName().c_str());
  444. if (GraphUtils::AddEdge(shape_node_->GetOutDataAnchor(0), index_node->GetInDataAnchor(0)) != GRAPH_SUCCESS) {
  445. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:0) and op:%s(%s)(index:0) failed",
  446. shape_node_->GetName().c_str(), shape_node_->GetType().c_str(),
  447. index_node->GetName().c_str(), index_node->GetType().c_str());
  448. GELOGE(FAILED, "Failed to add edge between node:%s to MapIndex:%s", shape_node_->GetName().c_str(),
  449. index_node->GetName().c_str());
  450. return FAILED;
  451. }
  452. if (GraphUtils::AddEdge(const_node->GetOutDataAnchor(0), index_node->GetInDataAnchor(1)) != GRAPH_SUCCESS) {
  453. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:0) and op:%s(%s)(index:1) failed",
  454. const_node->GetName().c_str(), const_node->GetType().c_str(),
  455. index_node->GetName().c_str(), index_node->GetType().c_str());
  456. GELOGE(FAILED, "Failed to add edge between node:%s to MapIndex:%s", const_node->GetName().c_str(),
  457. index_node->GetName().c_str());
  458. return FAILED;
  459. }
  460. if (GraphUtils::AddEdge(index_node->GetOutDataAnchor(0), case_node_->GetInDataAnchor(0)) != GRAPH_SUCCESS) {
  461. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:0) and op:%s(%s)(index:0) failed",
  462. index_node->GetName().c_str(), index_node->GetType().c_str(),
  463. case_node_->GetName().c_str(), case_node_->GetType().c_str());
  464. GELOGE(FAILED, "Failed to add edge between MapIndex:%s to Case:%s", index_node->GetName().c_str(),
  465. case_node_->GetName().c_str());
  466. return FAILED;
  467. }
  468. return SUCCESS;
  469. }
  470. Status MultiBatchClonePass::CreateGetDynamicDimsNode(const ComputeGraphPtr &graph, NodePtr &shape_node) {
  471. const OpDescPtr data_desc = MakeShared<OpDesc>(kMultiBatchGetDynamicDimsNode, GETDYNAMICDIMS);
  472. if (data_desc == nullptr) {
  473. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  474. GELOGE(OUT_OF_MEMORY, "Create multi-batch get dynamic dims node failed");
  475. return OUT_OF_MEMORY;
  476. }
  477. // input of GetDynamicDims is shape_of_each_data, output is gear_info
  478. for (size_t i = 0; i < GetLocalOmgContext().user_input_dims.size(); ++i) {
  479. size_t input_shape_dims = GetLocalOmgContext().user_input_dims.at(i).second.size();
  480. // add input desc without GeShape for const input, value of input_shape is 1 transferred by adapter
  481. if (input_shape_dims == 1 && GetLocalOmgContext().user_input_dims.at(i).second.at(0) == 0) {
  482. GeTensorDesc tensor_desc;
  483. tensor_desc.SetFormat(FORMAT_ND);
  484. tensor_desc.SetDataType(DT_INT32);
  485. auto ret = data_desc->AddInputDesc(tensor_desc);
  486. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  487. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  488. data_desc->GetName().c_str(), data_desc->GetType().c_str());
  489. GELOGE(INTERNAL_ERROR, "Failed to add input desc for created data");
  490. return FAILED);
  491. continue;
  492. }
  493. GeTensorDesc tensor_desc(GeShape({static_cast<int32_t>(input_shape_dims)}), FORMAT_ND, DT_INT32);
  494. auto ret = data_desc->AddInputDesc(tensor_desc);
  495. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  496. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  497. data_desc->GetName().c_str(), data_desc->GetType().c_str());
  498. GELOGE(INTERNAL_ERROR, "Failed to add input desc for created data");
  499. return FAILED);
  500. }
  501. GeTensorDesc tensor_desc(GeShape({static_cast<int32_t>(batch_shapes_.at(0).size())}), FORMAT_ND, DT_INT32);
  502. auto ret = data_desc->AddOutputDesc(tensor_desc);
  503. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  504. REPORT_CALL_ERROR("E19999", "Add output desc to op:%s(%s) failed",
  505. data_desc->GetName().c_str(), data_desc->GetType().c_str());
  506. GELOGE(INTERNAL_ERROR, "Failed to add output desc for created data");
  507. return FAILED);
  508. (void)AttrUtils::SetBool(data_desc, ATTR_INSERT_BY_MBATCH, true);
  509. shape_node = graph->AddNode(data_desc);
  510. if (shape_node == nullptr) {
  511. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  512. data_desc->GetName().c_str(), data_desc->GetType().c_str(), graph->GetName().c_str());
  513. GELOGE(OUT_OF_MEMORY, "Create multi-batch dynamic dims node failed");
  514. return OUT_OF_MEMORY;
  515. }
  516. return SUCCESS;
  517. }
  518. Status MultiBatchClonePass::AddAttrForGetDynamicDims(const NodePtr &shape_node) {
  519. if (!getnext_sink_dynamic_dims_) {
  520. GELOGD("No need to add attr when not insert get dynamic dims node.");
  521. return SUCCESS;
  522. }
  523. GELOGD("Add attr for :%s, type is %s:", shape_node->GetName().c_str(), shape_node->GetType().c_str());
  524. if (!AttrUtils::SetInt(shape_node->GetOpDesc(), ATTR_GETNEXT_SINK_DATA_COUNT, data_count_from_getnext_)) {
  525. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_GETNEXT_SINK_DATA_COUNT.c_str(),
  526. shape_node->GetName().c_str(), shape_node->GetType().c_str());
  527. GELOGE(INTERNAL_ERROR, "set ATTR_GETNEXT_SINK_DATA_COUNT failed");
  528. return INTERNAL_ERROR;
  529. }
  530. vector<int64_t> shape_info;
  531. for (size_t i = 0; i < GetLocalOmgContext().user_input_dims.size(); ++i) {
  532. if (GetLocalOmgContext().user_input_dims.at(i).second.size() == 1 &&
  533. GetLocalOmgContext().user_input_dims.at(i).second.at(0) == 0) {
  534. shape_info.emplace_back(0);
  535. continue;
  536. }
  537. shape_info.emplace_back(GetLocalOmgContext().user_input_dims.at(i).second.size());
  538. for (size_t j = 0; j < GetLocalOmgContext().user_input_dims.at(i).second.size(); ++j) {
  539. shape_info.emplace_back(GetLocalOmgContext().user_input_dims.at(i).second.at(j));
  540. }
  541. }
  542. if (!AttrUtils::SetListInt(shape_node->GetOpDesc(), ATTR_GETNEXT_SINK_SHAPE_INFO, shape_info)) {
  543. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_GETNEXT_SINK_SHAPE_INFO.c_str(),
  544. shape_node->GetName().c_str(), shape_node->GetType().c_str());
  545. GELOGE(INTERNAL_ERROR, "set ATTR_GETNEXT_SINK_SHAPE_INFO failed");
  546. return INTERNAL_ERROR;
  547. }
  548. return SUCCESS;
  549. }
  550. Status MultiBatchClonePass::LinkGetNextToGetDynamicDims(const NodePtr &getnext_node, const NodePtr &shape_node) {
  551. GELOGD("Start relink shape anchor of %s to %s.", getnext_node->GetName().c_str(), shape_node->GetName().c_str());
  552. size_t input_index = 0;
  553. size_t data_count = getnext_node->GetAllOutDataAnchors().size() / kDivisionConst;
  554. for (size_t out_index = data_count; out_index < getnext_node->GetAllOutDataAnchors().size(); ++out_index,
  555. ++input_index) {
  556. GELOGD("Start add %s of %zu out_anchor to %s of %zu in_anchor.", getnext_node->GetName().c_str(), out_index,
  557. shape_node->GetName().c_str(), input_index);
  558. auto out_data_anchor = getnext_node->GetOutDataAnchor(out_index);
  559. auto ret = GraphUtils::AddEdge(out_data_anchor, shape_node->GetInDataAnchor(input_index));
  560. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  561. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%zu) and op:%s(%s)(index:%zu) failed",
  562. getnext_node->GetName().c_str(), getnext_node->GetType().c_str(), out_index,
  563. shape_node->GetName().c_str(), shape_node->GetType().c_str(), input_index);
  564. GELOGE(INTERNAL_ERROR, "Failed to link getnext %s to getdynamicdims %s",
  565. getnext_node->GetName().c_str(), shape_node->GetName().c_str());
  566. return INTERNAL_ERROR);
  567. }
  568. return SUCCESS;
  569. }
  570. Status MultiBatchClonePass::LinkGetDynamicDimsToNetOutput(const NodePtr &output_node) {
  571. if (!GetLocalOmgContext().dynamic_node_type.empty()) {
  572. if (!AttrUtils::SetStr(output_node->GetOpDesc(), ATTR_ALL_GEARS_INFO, GetLocalOmgContext().dynamic_dims)) {
  573. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_ALL_GEARS_INFO.c_str(),
  574. output_node->GetName().c_str(), output_node->GetType().c_str());
  575. GELOGE(INTERNAL_ERROR, "Failed to set all gears info attr on netoutput %s.", output_node->GetName().c_str());
  576. return INTERNAL_ERROR;
  577. }
  578. }
  579. if (getnext_sink_dynamic_dims_) {
  580. GELOGD("Start link %s to %s.", shape_node_->GetName().c_str(), output_node->GetName().c_str());
  581. size_t input_index = output_node->GetAllInDataAnchors().size();
  582. if (NodeUtils::AppendInputAnchor(output_node, input_index + 1) != GRAPH_SUCCESS) {
  583. REPORT_CALL_ERROR("E19999", "Append input anchor to op:%s(%s) failed, size:%zu",
  584. output_node->GetName().c_str(), output_node->GetType().c_str(), input_index + 1);
  585. GELOGE(INTERNAL_ERROR, "Append input anchor of %s of %zu failed.", output_node->GetName().c_str(), input_index);
  586. return INTERNAL_ERROR;
  587. }
  588. auto ret = GraphUtils::AddEdge(shape_node_->GetOutDataAnchor(kDataOutIndex),
  589. output_node->GetInDataAnchor(input_index));
  590. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  591. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%zu) failed",
  592. shape_node_->GetName().c_str(), shape_node_->GetType().c_str(), kDataOutIndex,
  593. output_node->GetName().c_str(), output_node->GetType().c_str(), input_index);
  594. GELOGE(INTERNAL_ERROR, "Failed to link netoutput %s to getdynamicdims %s",
  595. output_node->GetName().c_str(), shape_node_->GetName().c_str());
  596. return INTERNAL_ERROR);
  597. if (!AttrUtils::SetBool(output_node->GetOpDesc(), ATTR_GETNEXT_SINK_DYNMAIC, true)) {
  598. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_GETNEXT_SINK_DYNMAIC.c_str(),
  599. output_node->GetName().c_str(), output_node->GetType().c_str());
  600. GELOGE(INTERNAL_ERROR, "Failed to set getnext sink dynamic attr on netoutput %s.",
  601. output_node->GetName().c_str());
  602. return INTERNAL_ERROR;
  603. }
  604. }
  605. return SUCCESS;
  606. }
  607. ///
  608. /// @ingroup ge
  609. /// @brief Create input node for root graph.
  610. /// @param [in] const ComputeGraphPtr &graph: Root/Case graph.
  611. /// @return 0: SUCCESS / others: FAILED
  612. ///
  613. Status MultiBatchClonePass::CreateInputNode(const ComputeGraphPtr &graph) {
  614. // Data --> Case
  615. std::vector<NodePtr> all_data_nodes;
  616. size_t case_input_index = kCaseArgIndex;
  617. NodePtr getnext_node = nullptr;
  618. size_t input_index_of_getnext = 0;
  619. for (size_t i = 0; i < all_data_nodes_.size(); ++i, ++case_input_index) {
  620. const auto &node = all_data_nodes_[i];
  621. const OpDescPtr op_desc = AttrUtils::CopyOpDesc(node->GetOpDesc());
  622. if (op_desc == nullptr) {
  623. REPORT_CALL_ERROR("E19999", "Copy op_desc from op:%s(%s) failed",
  624. node->GetName().c_str(), node->GetType().c_str());
  625. GELOGE(OUT_OF_MEMORY, "Create multi-batch Data node failed, name: %s", node->GetName().c_str());
  626. return FAILED;
  627. }
  628. if (GraphUtils::CopyTensorAttrs(op_desc, node) != GRAPH_SUCCESS) {
  629. REPORT_CALL_ERROR("E19999", "Copy tensor attr from op:%s(%s) failed",
  630. node->GetName().c_str(), node->GetType().c_str());
  631. return FAILED;
  632. }
  633. op_desc->SetName(node->GetName());
  634. const NodePtr &data = graph->AddNode(op_desc);
  635. GE_CHK_BOOL_EXEC(data != nullptr,
  636. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  637. op_desc->GetName().c_str(), op_desc->GetType().c_str(),
  638. graph->GetName().c_str());
  639. return FAILED, "Add node[%s] to graph failed", op_desc->GetName().c_str());
  640. if (IsGetNextType(node)) {
  641. getnext_node = data;
  642. input_index_of_getnext = case_input_index;
  643. case_input_index = case_input_index + data_count_from_getnext_;
  644. continue;
  645. } else {
  646. if (GraphUtils::AddEdge(data->GetOutDataAnchor(0), case_node_->GetInDataAnchor(case_input_index)) !=
  647. GRAPH_SUCCESS) {
  648. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:0) and op:%s(%s)(index:%zu) failed",
  649. data->GetName().c_str(), data->GetType().c_str(),
  650. case_node_->GetName().c_str(), case_node_->GetType().c_str(), case_input_index);
  651. GELOGE(FAILED, "Failed to add edge between Data:%s to Case:%s", data->GetName().c_str(),
  652. case_node_->GetName().c_str());
  653. return FAILED;
  654. }
  655. }
  656. if (SetMaxShape(data) != SUCCESS) {
  657. GELOGE(FAILED, "Set max shape of %s failed.", data->GetName().c_str());
  658. return FAILED;
  659. }
  660. all_data_nodes.emplace_back(data);
  661. }
  662. if (getnext_node != nullptr) {
  663. if (LinkEdgeForGetNext(getnext_node, input_index_of_getnext) != SUCCESS) {
  664. GELOGE(FAILED, "Failed to link edge for %s.", getnext_node->GetName().c_str());
  665. return FAILED;
  666. }
  667. if (SetMaxShape(getnext_node) != SUCCESS) {
  668. GELOGE(FAILED, "Set max shape of %s failed.", getnext_node->GetName().c_str());
  669. return FAILED;
  670. }
  671. all_data_nodes.emplace_back(getnext_node);
  672. }
  673. all_data_nodes_.swap(all_data_nodes);
  674. return SUCCESS;
  675. }
  676. Status MultiBatchClonePass::LinkEdgeForGetNext(const NodePtr &getnext_node, size_t &case_input_index) {
  677. GELOGD("Start link edge for %s, which is the %zu input of %s.", getnext_node->GetName().c_str(),
  678. case_input_index, case_node_->GetName().c_str());
  679. for (size_t out_index = 0; out_index < data_count_from_getnext_; ++out_index, ++case_input_index) {
  680. if (GraphUtils::AddEdge(getnext_node->GetOutDataAnchor(out_index),
  681. case_node_->GetInDataAnchor(case_input_index)) != GRAPH_SUCCESS) {
  682. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%zu) and op:%s(%s)(index:%zu) failed",
  683. getnext_node->GetName().c_str(), getnext_node->GetType().c_str(), out_index,
  684. case_node_->GetName().c_str(), case_node_->GetType().c_str(), case_input_index);
  685. GELOGE(FAILED, "Failed to add data edge between %zu Data:%s to %zu Case:%s", out_index,
  686. getnext_node->GetName().c_str(), case_input_index, case_node_->GetName().c_str());
  687. return FAILED;
  688. }
  689. }
  690. if (getnext_sink_dynamic_dims_) {
  691. GE_CHK_STATUS_RET(LinkGetNextToGetDynamicDims(getnext_node, shape_node_), "Failed to add link for %s.",
  692. shape_node_->GetName().c_str());
  693. }
  694. return SUCCESS;
  695. }
  696. ///
  697. /// @ingroup ge
  698. /// @brief Create Const node for root graph.
  699. /// @param [in] const ComputeGraphPtr &graph: Root/Case graph.
  700. /// @return 0: SUCCESS / others: FAILED
  701. ///
  702. Status MultiBatchClonePass::CreateConstNode(const ComputeGraphPtr &graph) {
  703. // Const --> Case
  704. std::vector<NodePtr> all_const_nodes;
  705. size_t arg_index = kCaseArgIndex + all_data_nodes_.size();
  706. if (data_count_from_getnext_ != 0) {
  707. arg_index = arg_index + data_count_from_getnext_ - kNumOfGetnextNode;
  708. }
  709. for (size_t i = 0; i < all_const_nodes_.size(); ++i) {
  710. const auto &node = all_const_nodes_[i];
  711. const OpDescPtr op_desc = AttrUtils::CopyOpDesc(node->GetOpDesc());
  712. if (op_desc == nullptr) {
  713. REPORT_CALL_ERROR("E19999", "Copy op_desc from op:%s(%s) failed",
  714. node->GetName().c_str(), node->GetType().c_str());
  715. GELOGE(OUT_OF_MEMORY, "Create multi-batch Const node failed, name: %s", node->GetName().c_str());
  716. return FAILED;
  717. }
  718. op_desc->SetName(node->GetName());
  719. if (GraphUtils::CopyTensorAttrs(op_desc, node) != GRAPH_SUCCESS) {
  720. REPORT_CALL_ERROR("E19999", "Copy tensor attr from op:%s(%s) failed",
  721. node->GetName().c_str(), node->GetType().c_str());
  722. return FAILED;
  723. }
  724. const NodePtr &data = graph->AddNode(op_desc);
  725. GE_CHK_BOOL_EXEC(data != nullptr,
  726. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  727. op_desc->GetName().c_str(), op_desc->GetType().c_str(),
  728. graph->GetName().c_str());
  729. return FAILED, "Add node[%s] to graph failed", op_desc->GetName().c_str());
  730. if (GraphUtils::AddEdge(data->GetOutDataAnchor(0), case_node_->GetInDataAnchor(arg_index + i)) != GRAPH_SUCCESS) {
  731. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:0) and op:%s(%s)(index:%zu) failed",
  732. data->GetName().c_str(), data->GetType().c_str(),
  733. case_node_->GetName().c_str(), case_node_->GetType().c_str(), arg_index + i);
  734. GELOGE(FAILED, "Failed to add edge between Const:%s to Case:%s", data->GetName().c_str(),
  735. case_node_->GetName().c_str());
  736. return FAILED;
  737. }
  738. all_const_nodes.emplace_back(data);
  739. }
  740. ChangeConstToData();
  741. all_const_nodes_.swap(all_const_nodes);
  742. return SUCCESS;
  743. }
  744. void MultiBatchClonePass::ChangeConstToData() {
  745. size_t data_index = all_data_nodes_.size();
  746. if (data_count_from_getnext_ != 0) {
  747. data_index = data_index + data_count_from_getnext_ - kNumOfGetnextNode;
  748. }
  749. for (size_t i = 0; i < all_const_nodes_.size(); ++i, ++data_index) { // Trans subgraph Const to Data.
  750. auto &const_node = all_const_nodes_[i];
  751. bool need_change_type = true;
  752. if (out_control_nodes_.find(const_node) != out_control_nodes_.end()) {
  753. GELOGD("No need to change %s to data type.", const_node->GetName().c_str());
  754. need_change_type = false;
  755. break;
  756. }
  757. if (!need_change_type) {
  758. continue;
  759. }
  760. const OpDescPtr &op_desc = all_const_nodes_[i]->GetOpDesc();
  761. op_desc->SetType(DATA);
  762. (void)op_desc->DelAttr(ATTR_NAME_WEIGHTS); // Delete weight.
  763. // Const no InputDesc, Data need InputDesc.
  764. (void)op_desc->AddInputDesc(op_desc->GetOutputDesc(kDataOutIndex));
  765. (void)AttrUtils::SetInt(op_desc, ATTR_NAME_INDEX, data_index);
  766. (void)NodeUtils::AppendInputAnchor(all_const_nodes_[i], 1);
  767. }
  768. }
  769. ///
  770. /// @ingroup ge
  771. /// @brief Create output node for root graph.
  772. /// @param [in] const ComputeGraphPtr &graph: Root/Case graph.
  773. /// @return 0: SUCCESS / others: FAILED
  774. ///
  775. Status MultiBatchClonePass::CreateOutputNode(const ComputeGraphPtr &graph) {
  776. const auto &output = all_output_nodes_[0];
  777. const OpDescPtr op_desc = AttrUtils::CopyOpDesc(output->GetOpDesc());
  778. if (op_desc == nullptr) {
  779. REPORT_CALL_ERROR("E19999", "Copy op_desc from op:%s(%s) failed",
  780. output->GetName().c_str(), output->GetType().c_str());
  781. GELOGE(OUT_OF_MEMORY, "Create multi-batch output node failed");
  782. return FAILED;
  783. }
  784. if (GraphUtils::CopyTensorAttrs(op_desc, output) != GRAPH_SUCCESS) {
  785. REPORT_CALL_ERROR("E19999", "Copy tensor attr from op:%s(%s) failed",
  786. output->GetName().c_str(), output->GetType().c_str());
  787. return FAILED;
  788. }
  789. op_desc->SetName(output->GetName());
  790. const NodePtr &node = graph->AddNode(op_desc);
  791. GE_CHK_BOOL_EXEC(node != nullptr,
  792. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  793. op_desc->GetName().c_str(), op_desc->GetType().c_str(),
  794. graph->GetName().c_str());
  795. return FAILED, "Add node[%s] to graph failed", op_desc->GetName().c_str());
  796. for (size_t i = 0; i < case_node_->GetAllOutDataAnchorsSize(); ++i) {
  797. const auto it = direct_output_.find(i);
  798. if (it == direct_output_.end()) {
  799. if (GraphUtils::AddEdge(case_node_->GetOutDataAnchor(i), node->GetInDataAnchor(i)) != GRAPH_SUCCESS) {
  800. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%zu) and op:%s(%s)(index:%zu) failed",
  801. case_node_->GetName().c_str(), case_node_->GetType().c_str(), i,
  802. node->GetName().c_str(), node->GetType().c_str(), i);
  803. GELOGE(FAILED, "Failed to add edge between Case:%s to NetOutput:%s",
  804. case_node_->GetName().c_str(), node->GetName().c_str());
  805. return FAILED;
  806. }
  807. } else {
  808. const auto data_node = graph->FindNode(it->second);
  809. if (data_node == nullptr) {
  810. REPORT_CALL_ERROR("E19999", "Find node:%s from graph:%s failed", it->second.c_str(), graph->GetName().c_str());
  811. GELOGE(GE_GRAPH_GRAPH_NODE_NULL, "Data node:%s not found", it->second.c_str());
  812. return GE_GRAPH_GRAPH_NODE_NULL;
  813. }
  814. if (GraphUtils::AddEdge(data_node->GetOutDataAnchor(kDataOutIndex), node->GetInDataAnchor(i)) != GRAPH_SUCCESS) {
  815. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%zu) failed",
  816. data_node->GetName().c_str(), data_node->GetType().c_str(), kDataOutIndex,
  817. node->GetName().c_str(), node->GetType().c_str(), i);
  818. GELOGE(FAILED, "Failed to add edge between Data:%s to NetOutput:%s",
  819. data_node->GetName().c_str(), node->GetName().c_str());
  820. return FAILED;
  821. }
  822. }
  823. }
  824. GE_CHK_STATUS_RET(LinkGetDynamicDimsToNetOutput(node), "Failed to add edge between %s to netoutput: %s.",
  825. shape_node_->GetName().c_str(), output->GetName().c_str());
  826. all_output_nodes_.clear();
  827. all_output_nodes_.emplace_back(node);
  828. return SUCCESS;
  829. }
  830. ///
  831. /// @ingroup ge
  832. /// @brief Set max shape to Data node in root graph.
  833. /// @param [in] const NodePtr &data: data in Root/Case graph.
  834. /// @return 0: SUCCESS / others: FAILED
  835. ///
  836. Status MultiBatchClonePass::SetMaxShape(const NodePtr &data) {
  837. GELOGD("Start set max shape for %s.", data->GetName().c_str());
  838. if (!IsGetNextType(data)) {
  839. if (SetMaxShapeToData(data, kDataOutIndex) != SUCCESS) {
  840. GELOGE(PARAM_INVALID, "Failed to update max shape of %s.", data->GetName().c_str());
  841. return PARAM_INVALID;
  842. }
  843. } else {
  844. for (size_t out_anchor_index = 0; out_anchor_index < data_count_from_getnext_; ++out_anchor_index) {
  845. if (SetMaxShapeToData(data, out_anchor_index) != SUCCESS) {
  846. GELOGE(PARAM_INVALID, "Failed to update max shape of %s.", data->GetName().c_str());
  847. return PARAM_INVALID;
  848. }
  849. }
  850. }
  851. return SUCCESS;
  852. }
  853. Status MultiBatchClonePass::SetMaxShapeToData(const NodePtr &node, size_t out_anchor_index) {
  854. GELOGD("Start update max shape of %s, %zu output.", node->GetName().c_str(), out_anchor_index);
  855. auto data_shape = NodeUtils::GetOutputDesc(*node, out_anchor_index).GetShape();
  856. string data_name = node->GetName();
  857. if (IsGetNextType(node)) {
  858. data_name.append("_").append(std::to_string(out_anchor_index));
  859. }
  860. GELOGD("Update max shape of %s, shape dims is %s.", data_name.c_str(),
  861. formats::JoinToString(data_shape.GetDims()).c_str());
  862. const auto &dims = data_shape.GetDims();
  863. if (!IsGetNextType(node)) {
  864. if (std::all_of(dims.begin(), dims.end(), [](int64_t val) { return val >= 0; })) {
  865. GELOGD("No need to do anything for static data.");
  866. return SUCCESS;
  867. }
  868. } else {
  869. if (std::all_of(dims.begin(), dims.end(), [](int64_t val) { return val >= 0; })) {
  870. if (getnext_sink_dynamic_dims_) {
  871. // need to update shape of Shape_node when getnext node has dynamic data
  872. GE_CHK_STATUS_RET(UpdateShapeOfShapeNode(node, out_anchor_index), "Failed to update shape of shape node");
  873. }
  874. return SUCCESS;
  875. }
  876. }
  877. (void)AttrUtils::SetListInt(node->GetOpDesc(), ATTR_MBATCH_ORIGIN_INPUT_DIMS, data_shape.GetDims());
  878. if (!AttrUtils::SetStr(node->GetOpDesc(), kMbatchCaseName, case_node_->GetName())) {
  879. REPORT_CALL_ERROR("E19999", "Set Attr:%s to node:%s(%s) failed",
  880. kMbatchCaseName, node->GetName().c_str(), node->GetType().c_str());
  881. GELOGE(INTERNAL_ERROR, "Failed to add switchn attr on data node %s", node->GetName().c_str());
  882. return INTERNAL_ERROR;
  883. }
  884. GeTensorDesc tensor(NodeUtils::GetOutputDesc(*node, kDataOutIndex));
  885. std::vector<std::string> input_dims_str;
  886. for (size_t i = 0; i < batch_shapes_.size(); ++i) {
  887. auto shape = data_shape;
  888. auto ret = multibatch::CalcShape(data_to_dynamic_info_.at(data_name).at(i), shape);
  889. if (ret != SUCCESS) {
  890. GELOGE(ret, "Failed to calculate the shape for data node %s, the shape may not match", node->GetName().c_str());
  891. return ret;
  892. }
  893. tensor.SetShape(shape);
  894. int64_t tensor_size = 0;
  895. (void)TensorUtils::GetTensorSizeInBytes(tensor, tensor_size);
  896. string input_str = TypeUtils::FormatToSerialString(tensor.GetFormat()) + ":" +
  897. TypeUtils::DataTypeToSerialString(tensor.GetDataType()) + ":" + node->GetName() + ":" +
  898. std::to_string(tensor_size) + ":" + std::to_string(tensor.GetShape().GetDimNum()) + ":" +
  899. formats::JoinToString(tensor.GetShape().GetDims());
  900. input_dims_str.emplace_back(input_str);
  901. }
  902. (void)AttrUtils::SetListStr(node->GetOpDesc(), "_all_origin_gears_inputs", input_dims_str);
  903. size_t max_shape_index = 0;
  904. int64_t max_size = 0;
  905. for (size_t i = 0; i < batch_shapes_.size(); ++i) {
  906. int64_t size = 1;
  907. for (auto dim : data_to_dynamic_info_.at(data_name).at(i)) {
  908. if (INT64_MAX / dim < size) {
  909. REPORT_INNER_ERROR("E19999", "The shape %s size will overflow after multi",
  910. formats::ShapeToString(data_to_dynamic_info_.at(data_name).at(i)).c_str());
  911. GELOGE(PARAM_INVALID, "The shape %s size overflow",
  912. formats::ShapeToString(data_to_dynamic_info_.at(data_name).at(i)).c_str());
  913. return PARAM_INVALID;
  914. }
  915. size *= dim;
  916. }
  917. if (size > max_size) {
  918. max_size = size;
  919. max_shape_index = i;
  920. }
  921. }
  922. return SetShapeToData(data_to_dynamic_info_.at(data_name).at(max_shape_index), node, data_shape, out_anchor_index);
  923. }
  924. ///
  925. /// @ingroup ge
  926. /// @brief Set max shape to Data/GetNext node in root graph.
  927. /// @param [in] const std::vector<int64_t> &shapes: dims of shape.
  928. /// @param [in] const NodePtr &data: data in Root/Case graph.
  929. /// @param [in] GeShape &data_shape: dims of data node.
  930. /// @param [in] size_t out_anchor_index: out anchor index of data node.
  931. /// @return 0: SUCCESS / others: FAILED
  932. ///
  933. Status MultiBatchClonePass::SetShapeToData(const std::vector<int64_t> &shapes, const NodePtr &data, GeShape &data_shape,
  934. size_t out_anchor_index) {
  935. GELOGD("Start set shape to %zu out of %s.", out_anchor_index, data->GetName().c_str());
  936. if (multibatch::CalcShape(shapes, data_shape) != SUCCESS) {
  937. GELOGE(INTERNAL_ERROR, "Failed to calculate the batched shape for data node %s, the shapes may not match",
  938. data->GetName().c_str());
  939. return INTERNAL_ERROR;
  940. }
  941. if (NodeUtils::UpdateOutputShape(*data, out_anchor_index, data_shape) != GRAPH_SUCCESS) {
  942. REPORT_CALL_ERROR("E19999", "Update ouput desc shape to op:%s(%s) failed, index:%zu",
  943. data->GetName().c_str(), data->GetType().c_str(), out_anchor_index);
  944. GELOGE(INTERNAL_ERROR, "Failed to update output shape for data %s", data->GetName().c_str());
  945. return INTERNAL_ERROR;
  946. }
  947. if (!IsGetNextType(data)) {
  948. if (NodeUtils::UpdateInputShape(*data, kDataInIndex, data_shape) != GRAPH_SUCCESS) {
  949. REPORT_CALL_ERROR("E19999", "Update input desc shape to op:%s(%s) failed, index:%u",
  950. data->GetName().c_str(), data->GetType().c_str(), kDataInIndex);
  951. GELOGE(INTERNAL_ERROR, "Failed to update input shape for data %s", data->GetName().c_str());
  952. return INTERNAL_ERROR;
  953. }
  954. } else {
  955. if (getnext_sink_dynamic_dims_) {
  956. // need to update shape of Shape_node when getnext_sink_dynamic
  957. GE_CHK_STATUS_RET(UpdateShapeOfShapeNode(data, out_anchor_index), "Failed to update shape of shape node");
  958. }
  959. }
  960. GELOGI("Update the data %s input/output shape to the max %s", data->GetName().c_str(),
  961. formats::ShapeToString(data_shape).c_str());
  962. return SUCCESS;
  963. }
  964. Status MultiBatchClonePass::UpdateShapeOfShapeNode(const NodePtr &node, size_t out_anchor_index) {
  965. GELOGD("Start update output shape of shape node insert by adapter, which is the %zu out of %s.", out_anchor_index,
  966. node->GetName().c_str());
  967. auto data_shape = NodeUtils::GetOutputDesc(*node, out_anchor_index).GetShape();
  968. size_t shape_index = out_anchor_index + (node->GetAllOutDataAnchors().size() / kDivisionConst);
  969. GeTensorDesc output_desc = node->GetOpDesc()->GetOutputDesc(shape_index);
  970. std::vector<int64_t> output_dims = {static_cast<int64_t>(data_shape.GetDims().size())};
  971. GeShape output_shape(output_dims);
  972. output_desc.SetShape(output_shape);
  973. if (node->GetOpDesc()->UpdateOutputDesc(shape_index, output_desc) != SUCCESS) {
  974. REPORT_CALL_ERROR("E19999", "Update ouput desc to op:%s(%s) failed, index:%zu",
  975. node->GetName().c_str(), node->GetType().c_str(), shape_index);
  976. GELOGE(FAILED, "Update output desc fail.");
  977. return FAILED;
  978. }
  979. return SUCCESS;
  980. }
  981. ///
  982. /// @ingroup ge
  983. /// @brief Update Data node in Subgraph.
  984. /// @param [in] const NodePtr &data: data in Subgraph.
  985. /// @param [in] size_t batch_index: The batch index.
  986. /// @return 0: SUCCESS / others: FAILED
  987. ///
  988. Status MultiBatchClonePass::UpdateSubgraphData(const NodePtr &data, size_t batch_index) {
  989. int node_index = -1;
  990. if (!AttrUtils::GetInt(data->GetOpDesc(), ATTR_NAME_INDEX, node_index)) {
  991. REPORT_CALL_ERROR("E19999", "Get Attr:%s from op:%s(%s) failed", ATTR_NAME_INDEX.c_str(),
  992. data->GetName().c_str(), data->GetType().c_str());
  993. GELOGE(FAILED, "Failed to get index from data[%s]", data->GetName().c_str());
  994. return FAILED;
  995. }
  996. int parent_index = node_index + 1;
  997. if (!AttrUtils::SetInt(data->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  998. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  999. data->GetName().c_str(), data->GetType().c_str());
  1000. GELOGE(FAILED, "Failed to set parent index for node %s", data->GetName().c_str());
  1001. return FAILED;
  1002. }
  1003. auto data_shape = NodeUtils::GetOutputDesc(*data, kDataOutIndex).GetShape();
  1004. const auto &dims = data_shape.GetDims();
  1005. GELOGD("Start update shape of %s , batch index is %zu, dims is %s.", data->GetName().c_str(), batch_index,
  1006. formats::JoinToString(dims).c_str());
  1007. if (std::all_of(dims.begin(), dims.end(), [](int64_t val) { return val >= 0; })) {
  1008. return SUCCESS;
  1009. }
  1010. (void)AttrUtils::SetListInt(data->GetOpDesc(), ATTR_MBATCH_ORIGIN_INPUT_DIMS, data_shape.GetDims());
  1011. auto data_name = data->GetName();
  1012. size_t pos = data_name.find(kMultiBatchNodePostfix);
  1013. if (pos == string::npos) {
  1014. REPORT_INNER_ERROR("E19999", "Cannot find key string [%s] of multi-batch in name of virtual input node:%s(%s)",
  1015. kMultiBatchNodePostfix.c_str(), data->GetName().c_str(), data->GetType().c_str());
  1016. GELOGE(FAILED, "Cannot find key string [%s] of multi-batch in name of virtual input node, node name: %s.",
  1017. kMultiBatchNodePostfix.c_str(), data_name.c_str());
  1018. return FAILED;
  1019. }
  1020. auto parent_name = data_name.substr(0, pos);
  1021. return SetShapeToData(data_to_dynamic_info_.at(parent_name).at(batch_index), data, data_shape, kDataOutIndex);
  1022. }
  1023. Status MultiBatchClonePass::CreateOriGraph(const ComputeGraphPtr &graph) {
  1024. if (data_count_from_getnext_ == 0) {
  1025. GELOGD("No need to change original graph without getnext node.");
  1026. return SUCCESS;
  1027. }
  1028. GELOGD("Start change original graph: %s when exit getnext node.", graph->GetName().c_str());
  1029. size_t data_index = all_data_nodes_.size() - kNumOfGetnextNode;
  1030. for (const auto &node : graph->GetDirectNode()) {
  1031. if (IsGetNextType(node)) {
  1032. for (size_t out_index = 0; out_index < data_count_from_getnext_; ++out_index, ++data_index) {
  1033. auto out_data_anchor = node->GetOutDataAnchor(out_index);
  1034. GE_IF_BOOL_EXEC(out_data_anchor == nullptr, continue);
  1035. NodePtr data_node = CreateDataNode(graph, out_data_anchor, data_index);
  1036. GE_IF_BOOL_EXEC(data_node == nullptr,
  1037. REPORT_CALL_ERROR("E19999", "Create data node in graph:%s failed", graph->GetName().c_str());
  1038. GELOGE(INTERNAL_ERROR, "Create %d data node failed.", out_data_anchor->GetIdx());
  1039. return INTERNAL_ERROR);
  1040. for (auto &in_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  1041. GE_IF_BOOL_EXEC(in_anchor == nullptr, continue);
  1042. NodePtr dst_node = in_anchor->GetOwnerNode();
  1043. if (GraphUtils::RemoveEdge(out_data_anchor, in_anchor) != GRAPH_SUCCESS) {
  1044. REPORT_CALL_ERROR("E19999", "Remove edge between op:%s(%s)(index:%zu) and op:%s(%s)(index:%d) failed",
  1045. node->GetName().c_str(), node->GetType().c_str(), out_index,
  1046. dst_node->GetName().c_str(), dst_node->GetType().c_str(), in_anchor->GetIdx());
  1047. GELOGE(INTERNAL_ERROR, "Failed to remove edge between %s to %s", node->GetName().c_str(),
  1048. dst_node->GetName().c_str());
  1049. return INTERNAL_ERROR;
  1050. }
  1051. if (GraphUtils::AddEdge(data_node->GetOutDataAnchor(0), dst_node->GetInDataAnchor(in_anchor->GetIdx())) !=
  1052. GRAPH_SUCCESS) {
  1053. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:0) and op:%s(%s)(index:%d) failed",
  1054. data_node->GetName().c_str(), data_node->GetType().c_str(),
  1055. dst_node->GetName().c_str(), dst_node->GetType().c_str(), in_anchor->GetIdx());
  1056. GELOGE(INTERNAL_ERROR, "Failed to add edge between %s to %s", data_node->GetName().c_str(),
  1057. dst_node->GetName().c_str());
  1058. return INTERNAL_ERROR;
  1059. }
  1060. }
  1061. }
  1062. if (graph->RemoveNode(node) != GRAPH_SUCCESS) {
  1063. REPORT_CALL_ERROR("E19999", "Remove node:%s(%s) from graph:%s failed",
  1064. node->GetName().c_str(), node->GetType().c_str(), graph->GetName().c_str());
  1065. GELOGE(GRAPH_FAILED, "Remove node %s failed!", node->GetName().c_str());
  1066. return GRAPH_FAILED;
  1067. }
  1068. break;
  1069. }
  1070. }
  1071. return SUCCESS;
  1072. }
  1073. NodePtr MultiBatchClonePass::CreateDataNode(const ComputeGraphPtr &graph, const OutDataAnchorPtr &out_data_anchor,
  1074. size_t data_index) {
  1075. size_t out_anchor_index = out_data_anchor->GetIdx();
  1076. std::string node_name = out_data_anchor->GetOwnerNode()->GetName() + "_" + std::to_string(out_anchor_index);
  1077. OpDescPtr op_desc = MakeShared<OpDesc>(node_name, DATA);
  1078. if (op_desc == nullptr) {
  1079. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  1080. GELOGE(OUT_OF_MEMORY, "Create data node failed.");
  1081. return nullptr;
  1082. }
  1083. (void)AttrUtils::SetInt(op_desc, ATTR_NAME_INDEX, data_index);
  1084. OpDescPtr getnext_op_desc = out_data_anchor->GetOwnerNode()->GetOpDesc();
  1085. if (getnext_op_desc == nullptr) {
  1086. REPORT_INNER_ERROR("E19999", "Param out_data_anchor's owner node is nullptr, check invalid");
  1087. GELOGE(OUT_OF_MEMORY, "Op desc of %s is nullptr.", out_data_anchor->GetOwnerNode()->GetName().c_str());
  1088. return nullptr;
  1089. }
  1090. if (op_desc->AddInputDesc(getnext_op_desc->GetOutputDesc(out_anchor_index)) != GRAPH_SUCCESS) {
  1091. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  1092. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  1093. GELOGE(INTERNAL_ERROR, "Add %s input desc failed.", op_desc->GetName().c_str());
  1094. return nullptr;
  1095. }
  1096. if (op_desc->AddOutputDesc(getnext_op_desc->GetOutputDesc(out_anchor_index)) != GRAPH_SUCCESS) {
  1097. REPORT_CALL_ERROR("E19999", "Add output desc to op:%s(%s) failed",
  1098. getnext_op_desc->GetName().c_str(), getnext_op_desc->GetType().c_str());
  1099. GELOGE(INTERNAL_ERROR, "Add %s output desc failed.", op_desc->GetName().c_str());
  1100. return nullptr;
  1101. }
  1102. NodePtr data_node = graph->AddNode(op_desc);
  1103. GELOGD("Success create %s node.", data_node->GetName().c_str());
  1104. return data_node;
  1105. }
  1106. ///
  1107. /// @ingroup ge
  1108. /// @brief Create nodes for root graph.
  1109. /// @param [in] const ComputeGraphPtr &graph: Root/Case graph.
  1110. /// @param [in] const ComputeGraphPtr &branch: original graph.
  1111. /// @return 0: SUCCESS / others: FAILED
  1112. ///
  1113. Status MultiBatchClonePass::CreateSubgraphs(const ComputeGraphPtr &graph, const ComputeGraphPtr &branch) {
  1114. GELOGD("Start create subgraphs for %s.", graph->GetName().c_str());
  1115. const auto &op_desc = case_node_->GetOpDesc();
  1116. for (size_t i = 0; i < batch_shapes_.size(); ++i) {
  1117. std::vector<NodePtr> input_nodes;
  1118. std::vector<NodePtr> output_nodes;
  1119. const std::string postfix = kMultiBatchNodePostfix + std::to_string(i);
  1120. ComputeGraphPtr subgraph = (i == 0) ? branch : GraphUtils::CloneGraph(branch, postfix, input_nodes, output_nodes);
  1121. GE_IF_BOOL_EXEC(subgraph == nullptr,
  1122. REPORT_CALL_ERROR("E19999", "Clone graph from graph:%s failed", branch->GetName().c_str());
  1123. GELOGE(FAILED, "Create multi-batch case node failed"); return FAILED);
  1124. subgraph->SetName("Batch_" + std::to_string(i));
  1125. subgraph->SetParentNode(case_node_);
  1126. subgraph->SetParentGraph(graph);
  1127. graph->AddSubgraph(subgraph->GetName(), subgraph);
  1128. all_branch_output_[subgraph] = subgraph->FindFirstNodeMatchType(NETOUTPUT);
  1129. const string key_name = "branches" + std::to_string(i);
  1130. op_desc->AddSubgraphName(key_name);
  1131. op_desc->SetSubgraphInstanceName(i, subgraph->GetName());
  1132. GELOGD("The %s has %zu input, %zu output.", subgraph->GetName().c_str(), input_nodes.size(), output_nodes.size());
  1133. for (const auto &data : input_nodes) {
  1134. GE_CHK_STATUS_RET(UpdateSubgraphData(data, i), "Update %s failed", subgraph->GetName().c_str());
  1135. }
  1136. }
  1137. // Origninal graph take as first subgraph, update node name.
  1138. for (const auto &n : branch->GetDirectNode()) {
  1139. const auto &op_desc = n->GetOpDesc();
  1140. op_desc->SetName(n->GetName() + kMultiBatchNodePostfix + "0");
  1141. if (n->GetType() == DATA) {
  1142. GE_CHK_STATUS_RET(UpdateSubgraphData(n, 0), "Update %s failed", branch->GetName().c_str());
  1143. }
  1144. }
  1145. return SUCCESS;
  1146. }
  1147. ///
  1148. /// @ingroup ge
  1149. /// @brief Update output_node in Subgraph.
  1150. /// @return 0: SUCCESS / others: FAILED
  1151. ///
  1152. Status MultiBatchClonePass::UpdateSubgraphOutput() {
  1153. for (const auto &item : all_branch_output_) {
  1154. const auto &output_node = item.second;
  1155. const auto &op_desc = output_node->GetOpDesc();
  1156. GE_CHECK_NOTNULL(op_desc);
  1157. for (size_t index = 0; index < op_desc->GetInputsSize(); ++index) {
  1158. GeTensorDescPtr tensor = op_desc->MutableInputDesc(index);
  1159. GE_CHECK_NOTNULL(tensor);
  1160. if (!AttrUtils::SetInt(tensor, ATTR_NAME_PARENT_NODE_INDEX, index)) {
  1161. REPORT_CALL_ERROR("E19999", "Set Attr:%s to input:%zu tensor of op:%s(%s) failed",
  1162. ATTR_NAME_PARENT_NODE_INDEX.c_str(), index,
  1163. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  1164. GELOGE(FAILED, "Failed to set parent index for node %s", output_node->GetName().c_str());
  1165. return FAILED;
  1166. }
  1167. }
  1168. }
  1169. return SUCCESS;
  1170. }
  1171. ///
  1172. /// @ingroup ge
  1173. /// @brief Remove subgraph suspend output anchor.
  1174. /// @param [in] ComputeGraphPtr &graph: Parent compute graph.
  1175. /// @return 0: SUCCESS / others: FAILED
  1176. ///
  1177. Status MultiBatchClonePass::PruneDirectOutput(const ComputeGraphPtr &graph) {
  1178. GELOGD("Start prune direct output.");
  1179. const auto &func_desc = case_node_->GetOpDesc();
  1180. uint32_t unused_num = 0;
  1181. uint32_t output_num = func_desc->GetOutputsSize();
  1182. for (size_t i = 0; i < output_num; ++i) {
  1183. bool is_unused_tensor = true;
  1184. for (const auto &item : all_branch_output_) {
  1185. const auto &netoutput = item.second;
  1186. GE_CHECK_NOTNULL(netoutput);
  1187. const auto in_anchor = netoutput->GetInDataAnchor(i);
  1188. if (in_anchor->GetPeerOutAnchor() != nullptr) {
  1189. is_unused_tensor = false;
  1190. break;
  1191. }
  1192. }
  1193. if (is_unused_tensor) {
  1194. unused_num++;
  1195. continue;
  1196. }
  1197. GE_CHK_STATUS_RET(UpdateOutputTensor(i, unused_num), "Graph:%s Update output failed", graph->GetName().c_str());
  1198. }
  1199. if (unused_num == 0) {
  1200. return SUCCESS;
  1201. }
  1202. GE_CHK_GRAPH_STATUS_RET(NodeUtils::RemoveOutputAnchor(case_node_, output_num - unused_num), "Remove output failed");
  1203. for (const auto &item : all_branch_output_) {
  1204. GE_CHK_GRAPH_STATUS_RET(NodeUtils::RemoveInputAnchor(item.second, output_num - unused_num), "Remove input failed");
  1205. }
  1206. return SUCCESS;
  1207. }
  1208. ///
  1209. /// @ingroup ge
  1210. /// @brief Update subgraph suspend output tensor.
  1211. /// @param [in] parent_index: parent index for check.
  1212. /// @param [in] unused_num: total unused tensor.
  1213. /// @return 0: SUCCESS / others: FAILED
  1214. ///
  1215. Status MultiBatchClonePass::UpdateOutputTensor(uint32_t parent_index, uint32_t unused_num) {
  1216. if (unused_num == 0) {
  1217. GELOGD("No need to update output tensor.");
  1218. return SUCCESS;
  1219. }
  1220. uint32_t update_index = parent_index - unused_num;
  1221. for (const auto &item : all_branch_output_) {
  1222. const auto &node = item.second;
  1223. const auto &new_anchor = node->GetInDataAnchor(update_index);
  1224. const auto &old_anchor = node->GetInDataAnchor(parent_index);
  1225. const auto &out_anchor = old_anchor->GetPeerOutAnchor();
  1226. const auto &out_node = out_anchor->GetOwnerNode();
  1227. const auto &op_desc = node->GetOpDesc();
  1228. (void)op_desc->UpdateInputDesc(update_index, op_desc->GetInputDesc(parent_index));
  1229. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(out_anchor, new_anchor), "Add edge failed");
  1230. GELOGI("Add edge success, func node: %s, node: %s, parent index: %u, update index: %u",
  1231. case_node_->GetName().c_str(), out_node->GetName().c_str(), parent_index, update_index);
  1232. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, old_anchor), "Remove edge failed");
  1233. GELOGI("Remove edge success, func node: %s, node: %s", case_node_->GetName().c_str(), out_node->GetName().c_str());
  1234. }
  1235. const auto &new_anchor = case_node_->GetOutDataAnchor(update_index);
  1236. const auto &old_anchor = case_node_->GetOutDataAnchor(parent_index);
  1237. for (const auto in_anchor : old_anchor->GetPeerInDataAnchors()) {
  1238. const auto &in_node = in_anchor->GetOwnerNode();
  1239. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(old_anchor, in_anchor), "Remove edge failed");
  1240. GELOGI("Remove edge success, func node: %s, node: %s", case_node_->GetName().c_str(), in_node->GetName().c_str());
  1241. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(new_anchor, in_anchor), "Add edge failed");
  1242. GELOGI("Add edge success, func node: %s, node: %s, parent index: %u, update index: %u",
  1243. case_node_->GetName().c_str(), in_node->GetName().c_str(), parent_index, update_index);
  1244. }
  1245. return SUCCESS;
  1246. }
  1247. } // namespace ge

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示