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_copy_graph.cc 99 kB

5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 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
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 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
5 years ago
5 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
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121
  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/preprocess/multi_batch_copy_graph.h"
  17. #include <queue>
  18. #include <set>
  19. #include <string>
  20. #include "common/formats/utils/formats_trans_utils.h"
  21. #include "common/ge/ge_util.h"
  22. #include "common/util/error_manager/error_manager.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "framework/common/ge_inner_error_codes.h"
  25. #include "framework/common/string_util.h"
  26. #include "framework/common/types.h"
  27. #include "framework/omg/omg_inner_types.h"
  28. #include "graph/debug/ge_attr_define.h"
  29. #include "graph/ge_context.h"
  30. #include "graph/passes/multi_batch_clone_pass.h"
  31. #include "graph/passes/prune_pass.h"
  32. #include "graph/preprocess/multi_batch_options.h"
  33. #include "graph/utils/attr_utils.h"
  34. #include "graph/utils/graph_utils.h"
  35. #include "graph/utils/node_utils.h"
  36. #include "graph/utils/tensor_utils.h"
  37. #include "graph/utils/type_utils.h"
  38. #include "inc/pass_manager.h"
  39. #include "graph/common/local_context.h"
  40. #include "graph/common/omg_util.h"
  41. using std::set;
  42. using std::string;
  43. using std::vector;
  44. using std::map;
  45. using std::queue;
  46. namespace ge {
  47. namespace multibatch {
  48. namespace {
  49. const char *const kMbatchSwitchnName = "mbatch-switch-name";
  50. const char *const kGetNextName = "IteratorV2";
  51. const int kSwitchNDataIndex = 0;
  52. const int kSwitchNPredIndex = 1;
  53. const int kDataOutIndex = 0;
  54. const int kDataInIndex = 0;
  55. const int kMergeDataOutIndex = 0;
  56. const int kStaticOutput = -1;
  57. const int kDivisionConst = 2;
  58. const int32_t kOneInDataNode = 1;
  59. const int32_t kFindNoMatch = 0;
  60. inline bool IsDataLikeType(const std::string &node_type) { return (node_type == DATA) || (node_type == AIPP); }
  61. inline bool IsEnterType(const string &node_type) { return (node_type == ENTER) || (node_type == REFENTER); }
  62. const set<string> unchange_types({CONSTANT, CONSTANTOP, ENTER, REFENTER});
  63. inline bool IsGetNextType(const NodePtr &node) {
  64. std::string original_type;
  65. GE_IF_BOOL_EXEC(GetOriginalType(node, original_type) != SUCCESS,
  66. GELOGW("Get original type failed"); return false);
  67. return (original_type == kGetNextName);
  68. }
  69. NodePtr InsertMergeNodeToGraph(const std::string &name, size_t input_num, const ComputeGraphPtr &graph) {
  70. OpDescPtr desc = MakeShared<OpDesc>();
  71. if (desc == nullptr) {
  72. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  73. GELOGE(OUT_OF_MEMORY, "Failed to insert merge node, name %s", name.c_str());
  74. return nullptr;
  75. }
  76. desc->SetName(name);
  77. desc->SetType(MERGE);
  78. GeTensorDesc tensor_desc;
  79. for (size_t i = 0; i < input_num; ++i) {
  80. auto ret = desc->AddInputDesc("x" + std::to_string(i), tensor_desc);
  81. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  82. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed, input desc name:%s,",
  83. desc->GetName().c_str(), desc->GetType().c_str(),
  84. ("x" + std::to_string(i)).c_str());
  85. GELOGE(INTERNAL_ERROR, "Failed to create merge node %s, failed to add input %zu, error-code %u",
  86. name.c_str(), i, ret);
  87. return nullptr);
  88. }
  89. auto ret = desc->AddOutputDesc("y", tensor_desc);
  90. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  91. REPORT_CALL_ERROR("E19999", "Add output desc to op:%s(%s) failed, output desc name:%s,",
  92. desc->GetName().c_str(), desc->GetType().c_str(), "y");
  93. GELOGE(INTERNAL_ERROR, "Failed to create merge node %s, failed to add output 'y', error-code %u",
  94. name.c_str(), ret);
  95. return nullptr);
  96. tensor_desc.SetDataType(DT_INT32);
  97. ret = desc->AddOutputDesc("value_index", tensor_desc);
  98. if (ret != GRAPH_SUCCESS) {
  99. REPORT_CALL_ERROR("E19999", "Add output desc to op:%s(%s) failed, output desc name:%s,",
  100. desc->GetName().c_str(), desc->GetType().c_str(), "value_index");
  101. GELOGE(INTERNAL_ERROR, "Failed to create merge node %s, failed to add output 'value_index', error-code %u",
  102. name.c_str(), ret);
  103. return nullptr;
  104. }
  105. if (!AttrUtils::SetBool(desc, ATTR_INSERT_BY_MBATCH, true)) {
  106. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_INSERT_BY_MBATCH.c_str(),
  107. desc->GetName().c_str(), desc->GetType().c_str());
  108. GELOGE(INTERNAL_ERROR, "Failed to create merge node %s, failed to add attr", name.c_str());
  109. return nullptr;
  110. }
  111. return graph->AddNode(desc);
  112. }
  113. NodePtr InsertCopyNode(const NodePtr &node, size_t n) {
  114. const std::string &name = node->GetName() + "_ascend_mbatch_batch_" + std::to_string(n);
  115. auto src_op_desc = node->GetOpDesc();
  116. GE_IF_BOOL_EXEC(src_op_desc == nullptr,
  117. REPORT_INNER_ERROR("E19999", "Param opdesc in node is nullptr, check invalid");
  118. GELOGE(INTERNAL_ERROR, "Failed to copy node %s to %s, the OpDesc is null",
  119. node->GetName().c_str(), name.c_str());
  120. return nullptr);
  121. auto desc = AttrUtils::CopyOpDesc(src_op_desc);
  122. GE_IF_BOOL_EXEC(desc == nullptr,
  123. REPORT_CALL_ERROR("E19999", "Copy OpDesc from op:%s(%s) failed",
  124. src_op_desc->GetName().c_str(), src_op_desc->GetType().c_str());
  125. GELOGE(OUT_OF_MEMORY, "Failed to create op desc for copy node for node %s name %s",
  126. node->GetName().c_str(), name.c_str());
  127. return nullptr);
  128. desc->SetName(name);
  129. desc->CopyAttrsFrom(*src_op_desc);
  130. for (uint32_t i = 0; i < node->GetAllInDataAnchorsSize(); ++i) {
  131. auto input_desc = desc->MutableInputDesc(i);
  132. GE_IF_BOOL_EXEC(input_desc == nullptr,
  133. REPORT_INNER_ERROR("E19999", "Input desc of op:%s(%s) not exist, index:%u, check invalid",
  134. desc->GetName().c_str(), desc->GetType().c_str(), i);
  135. GELOGW("Get null input desc by index %u from node %s when copy from %s", i,
  136. desc->GetName().c_str(), node->GetName().c_str());
  137. continue);
  138. input_desc->CopyAttrsFrom(src_op_desc->GetInputDesc(i));
  139. }
  140. for (uint32_t i = 0; i < node->GetAllOutDataAnchorsSize(); ++i) {
  141. auto output_desc = desc->MutableOutputDesc(i);
  142. GE_IF_BOOL_EXEC(output_desc == nullptr,
  143. REPORT_INNER_ERROR("E19999", "Ouput desc of op:%s(%s) not exist, index:%u, check invalid",
  144. desc->GetName().c_str(), desc->GetType().c_str(), i);
  145. GELOGE(INTERNAL_ERROR, "Failed to get output desc by index %u from node %s when copy from %s", i,
  146. desc->GetName().c_str(), node->GetName().c_str());
  147. return nullptr);
  148. output_desc->CopyAttrsFrom(src_op_desc->GetOutputDesc(i));
  149. }
  150. const std::string &batch_label = "Batch_" + std::to_string(n);
  151. if (!AttrUtils::SetStr(desc, ATTR_NAME_BATCH_LABEL, batch_label)) {
  152. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NAME_BATCH_LABEL.c_str(),
  153. desc->GetName().c_str(), desc->GetType().c_str());
  154. GELOGE(FAILED, "set attr ATTR_NAME_BATCH_LABEL failed, node:%s.", name.c_str());
  155. return nullptr;
  156. }
  157. (void)AttrUtils::SetListStr(desc, ATTR_NAME_DATA_DUMP_ORIGIN_OP_NAMES, {node->GetName()});
  158. auto graph = node->GetOwnerComputeGraph();
  159. return graph->AddNode(desc);
  160. }
  161. bool IsAllDimsPositive(const std::vector<int64_t> &dims) {
  162. for (auto dim : dims) {
  163. if (dim < 0) {
  164. return false;
  165. }
  166. }
  167. return true;
  168. }
  169. NodePtr InsertConst(const std::string &name, const ComputeGraphPtr &graph) {
  170. auto desc = MakeShared<OpDesc>();
  171. if (desc == nullptr) {
  172. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  173. GELOGE(OUT_OF_MEMORY, "Failed to create const op %s, out of memory", name.c_str());
  174. return nullptr;
  175. }
  176. desc->SetName(name);
  177. desc->SetType(CONSTANT);
  178. GeTensor tensor;
  179. tensor.SetData(std::vector<uint8_t>({0}));
  180. if (!AttrUtils::SetTensor(desc, ATTR_NAME_WEIGHTS, tensor)) {
  181. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NAME_WEIGHTS.c_str(),
  182. desc->GetName().c_str(), desc->GetType().c_str());
  183. GELOGE(OUT_OF_MEMORY, "Failed to init tensor value for const %s", name.c_str());
  184. return nullptr;
  185. }
  186. if (!AttrUtils::SetBool(desc, ATTR_INSERT_BY_MBATCH, true)) {
  187. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_INSERT_BY_MBATCH.c_str(),
  188. desc->GetName().c_str(), desc->GetType().c_str());
  189. GELOGE(OUT_OF_MEMORY, "Failed to set insert flag for const node %s", name.c_str());
  190. return nullptr;
  191. }
  192. if (desc->AddOutputDesc(GeTensorDesc()) != GRAPH_SUCCESS) {
  193. REPORT_CALL_ERROR("E19999", "Add output desc to op:%s(%s) failed",
  194. desc->GetName().c_str(), desc->GetType().c_str());
  195. GELOGE(OUT_OF_MEMORY, "Failed to add output desc for const node %s", name.c_str());
  196. return nullptr;
  197. }
  198. return graph->AddNode(desc);
  199. }
  200. bool IsOnlyOutputToAipp(const NodePtr &node) {
  201. for (const auto &out_node : node->GetOutDataNodes()) {
  202. if (out_node->GetType() != AIPP) {
  203. return false;
  204. }
  205. }
  206. return true;
  207. }
  208. } // namespace
  209. Status MultiBatchGraphCopyer::CopyGraph() {
  210. auto ret = Init();
  211. if (ret != SUCCESS) {
  212. return ret;
  213. }
  214. if (LabelStatus() != SUCCESS) {
  215. GELOGE(INTERNAL_ERROR, "Failed to label status for all nodes.");
  216. return INTERNAL_ERROR;
  217. }
  218. ret = CheckAndParseDynamicData();
  219. if (ret != SUCCESS) {
  220. return ret;
  221. }
  222. ret = CreateNewNodes();
  223. if (ret != SUCCESS) {
  224. return ret;
  225. }
  226. ret = LinkEdges();
  227. if (ret != SUCCESS) {
  228. return ret;
  229. }
  230. GELOGI("Begin to remove useless nodes by prune pass after copy process");
  231. PrunePass prune_pass;
  232. ret = prune_pass.Run(graph_);
  233. if (ret != SUCCESS) {
  234. GELOGE(ret, "Failed to prune");
  235. return ret;
  236. }
  237. return CheckCopyResult(origin_data_nodes_);
  238. }
  239. Status MultiBatchGraphCopyer::Init() {
  240. auto ret = CheckArguments();
  241. if (ret != SUCCESS) {
  242. return ret;
  243. }
  244. ret = RelinkConstCtrlEdge();
  245. if (ret != SUCCESS) {
  246. GELOGE(FAILED, "Relink const's control edge failed.");
  247. return FAILED;
  248. }
  249. ret = ExtractUnchangedStructureOutofCycle();
  250. if (ret != SUCCESS) {
  251. GELOGE(FAILED, "Extract unchanged structure out of cycle failed.");
  252. return FAILED;
  253. }
  254. for (auto &node : graph_->GetAllNodes()) {
  255. origin_all_nodes_.emplace_back(node);
  256. if (IsDataLikeType(node->GetType())) {
  257. origin_data_nodes_.emplace_back(node);
  258. }
  259. if (!GetLocalOmgContext().dynamic_node_type.empty() && IsGetNextType(node)) {
  260. origin_data_nodes_.emplace_back(node);
  261. }
  262. }
  263. return SUCCESS;
  264. }
  265. Status MultiBatchGraphCopyer::RelinkConstCtrlEdge() {
  266. for (auto &node : graph_->GetAllNodes()) {
  267. GE_CHECK_NOTNULL(node);
  268. if ((node->GetType() == CONSTANT) || (node->GetType() == CONSTANTOP)) {
  269. if (node->GetOutDataNodes().empty()) {
  270. continue;
  271. }
  272. if (!node->GetInControlNodes().empty()) {
  273. auto in_ctrl_nodes = node->GetInControlNodes();
  274. auto out_nodes = node->GetOutAllNodes();
  275. bool has_merge_out = false;
  276. for (const auto &out_node : out_nodes) {
  277. GE_CHECK_NOTNULL(out_node);
  278. if (out_node->GetType() == MERGE || out_node->GetType() == REFMERGE) {
  279. has_merge_out = true;
  280. break;
  281. }
  282. }
  283. if (has_merge_out) {
  284. continue;
  285. }
  286. auto in_ctrl_anchor = node->GetInControlAnchor();
  287. GE_CHECK_NOTNULL(in_ctrl_anchor);
  288. in_ctrl_anchor->UnlinkAll();
  289. for (auto &in_ctrl_node : in_ctrl_nodes) {
  290. auto out_ctrl_anchor_of_in_ctrl_node = in_ctrl_node->GetOutControlAnchor();
  291. GE_CHECK_NOTNULL(out_ctrl_anchor_of_in_ctrl_node);
  292. for (auto &out_node : out_nodes) {
  293. if (IsEnterType(out_node->GetType())) {
  294. continue;
  295. }
  296. if (!out_ctrl_anchor_of_in_ctrl_node->IsLinkedWith(out_node->GetInControlAnchor())) {
  297. GE_CHK_GRAPH_STATUS_RET(out_ctrl_anchor_of_in_ctrl_node->LinkTo(out_node->GetInControlAnchor()))
  298. }
  299. }
  300. }
  301. }
  302. auto out_ctrl_anchor = node->GetOutControlAnchor();
  303. if (out_ctrl_anchor != nullptr) {
  304. out_ctrl_anchor->UnlinkAll();
  305. }
  306. }
  307. }
  308. return SUCCESS;
  309. }
  310. Status MultiBatchGraphCopyer::ExtractUnchangedStructureOutofCycle() {
  311. map<string, vector<NodePtr>> frame_enter;
  312. if (GetEnterNodesGroupByFrame(frame_enter) != SUCCESS) {
  313. GELOGE(FAILED, "Get enter nodes grouped by frame_name failed.");
  314. return FAILED;
  315. }
  316. queue<NodePtr> nodes_to_extract;
  317. if (GetNodeNeedExtract(frame_enter, nodes_to_extract) != SUCCESS) {
  318. GELOGE(FAILED, "Get nodes needed to extract failed.");
  319. return FAILED;
  320. }
  321. while (!nodes_to_extract.empty()) {
  322. auto node = nodes_to_extract.front();
  323. nodes_to_extract.pop();
  324. OpDescPtr enter_desc = nullptr;
  325. if (MoveInEntersInDataAnchorDown(node, enter_desc) != SUCCESS) {
  326. GELOGE(FAILED, "Move in enter nodes' in data anchors down of %s failed.", node->GetName().c_str());
  327. return FAILED;
  328. }
  329. set<NodePtr> out_nodes;
  330. if (InsertEnterAfterNode(node, enter_desc, out_nodes) != SUCCESS) {
  331. GELOGE(FAILED, "Insert enter node after %s failed.", node->GetName().c_str());
  332. return FAILED;
  333. }
  334. if (MoveCtrlEdgeToOutNodes(node, out_nodes) != SUCCESS) {
  335. GELOGE(FAILED, "Move %s's control edge to out nodes failed.", node->GetName().c_str());
  336. return FAILED;
  337. }
  338. for (auto &out_node : out_nodes) {
  339. GE_CHECK_NOTNULL(out_node);
  340. if (AllInDataNodesUnchangeAndNoMergeOut(out_node)) {
  341. nodes_to_extract.push(out_node);
  342. }
  343. }
  344. }
  345. if (DeleteEnterWithoutDataOut() != SUCCESS) {
  346. GELOGE(FAILED, "Delete enter node without out data nodes failed.");
  347. return FAILED;
  348. }
  349. return SUCCESS;
  350. }
  351. Status MultiBatchGraphCopyer::GetEnterNodesGroupByFrame(map<string, vector<NodePtr>> &frame_enter) {
  352. for (auto &node : graph_->GetAllNodes()) {
  353. GE_CHECK_NOTNULL(node);
  354. if (IsEnterType(node->GetType())) {
  355. if (!node->GetInControlNodes().empty() || !node->GetOutControlNodes().empty()) {
  356. continue;
  357. }
  358. auto op_desc = node->GetOpDesc();
  359. GE_CHECK_NOTNULL(op_desc);
  360. string frame_name;
  361. if (!AttrUtils::GetStr(op_desc, ENTER_ATTR_FRAME_NAME, frame_name)) {
  362. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed",
  363. ENTER_ATTR_FRAME_NAME.c_str(),
  364. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  365. GELOGE(FAILED, "Get attr frame_name of enter[%s] failed.", node->GetName().c_str());
  366. return FAILED;
  367. }
  368. frame_enter[frame_name].emplace_back(node);
  369. }
  370. }
  371. return SUCCESS;
  372. }
  373. Status MultiBatchGraphCopyer::GetNodeNeedExtract(const map<string, vector<NodePtr>> &frame_enter,
  374. queue<NodePtr> &nodes_to_extract) {
  375. for (const auto &one_group : frame_enter) {
  376. auto enters = one_group.second;
  377. for (const auto &enter : enters) {
  378. auto out_data_nodes = enter->GetOutDataNodes();
  379. for (const auto &out_data_node : out_data_nodes) {
  380. GE_CHECK_NOTNULL(out_data_node);
  381. if (AllInDataNodesUnchangeAndNoMergeOut(out_data_node)) {
  382. nodes_to_extract.push(out_data_node);
  383. }
  384. }
  385. }
  386. }
  387. return SUCCESS;
  388. }
  389. bool MultiBatchGraphCopyer::AllInDataNodesUnchangeAndNoMergeOut(const NodePtr &node) {
  390. auto out_data_nodes = node->GetOutDataNodes();
  391. for (const auto &out_data_node : out_data_nodes) {
  392. if (out_data_node == nullptr) {
  393. return false;
  394. }
  395. if (out_data_node->GetType() == MERGE || out_data_node->GetType() == REFMERGE) {
  396. return false;
  397. }
  398. }
  399. auto in_data_nodes = node->GetInDataNodes();
  400. if (in_data_nodes.size() == kOneInDataNode) {
  401. return true;
  402. }
  403. for (const auto &in_data_node : in_data_nodes) {
  404. if (in_data_node == nullptr) {
  405. return false;
  406. }
  407. if (unchange_types.count(in_data_node->GetType()) == kFindNoMatch) {
  408. return false;
  409. }
  410. }
  411. return true;
  412. }
  413. Status MultiBatchGraphCopyer::MoveInEntersInDataAnchorDown(NodePtr &node, OpDescPtr &enter_desc) {
  414. auto in_data_anchors = node->GetAllInDataAnchors();
  415. for (auto &in_data_anchor : in_data_anchors) {
  416. auto peer_out_data_anchor = in_data_anchor->GetPeerOutAnchor();
  417. GE_CHECK_NOTNULL(peer_out_data_anchor);
  418. auto peer_in_data_node = peer_out_data_anchor->GetOwnerNode();
  419. if (IsEnterType(peer_in_data_node->GetType())) {
  420. GE_CHK_GRAPH_STATUS_RET(peer_out_data_anchor->Unlink(in_data_anchor))
  421. GELOGD("Unlink data edge from %s to %s.", peer_in_data_node->GetName().c_str(), node->GetName().c_str());
  422. auto enter_in_data_anchors = peer_in_data_node->GetAllInDataAnchors();
  423. for (auto &enter_in_data_anchor : enter_in_data_anchors) {
  424. auto peer_out_data_anchor_of_enter = enter_in_data_anchor->GetPeerOutAnchor();
  425. GE_CHECK_NOTNULL(peer_out_data_anchor_of_enter);
  426. if (peer_out_data_anchor_of_enter->IsLinkedWith(in_data_anchor)) {
  427. continue;
  428. }
  429. GE_CHK_GRAPH_STATUS_RET(peer_out_data_anchor_of_enter->LinkTo(in_data_anchor))
  430. GELOGD("Relink data edge from %s to %s.", peer_out_data_anchor_of_enter->GetOwnerNode()->GetName().c_str(),
  431. node->GetName().c_str());
  432. }
  433. enter_desc = peer_in_data_node->GetOpDesc();
  434. GE_CHECK_NOTNULL(enter_desc);
  435. }
  436. }
  437. return SUCCESS;
  438. }
  439. Status MultiBatchGraphCopyer::InsertEnterAfterNode(NodePtr &node, const OpDescPtr &copy_desc, set<NodePtr> &out_nodes) {
  440. if (copy_desc == nullptr) {
  441. return SUCCESS;
  442. }
  443. map<OutDataAnchorPtr, vector<std::pair<InDataAnchorPtr, NodePtr>>> outanchors_inanchors_nodes;
  444. auto out_data_anchors = node->GetAllOutDataAnchors();
  445. for (auto &out_data_anchor : out_data_anchors) {
  446. auto peer_in_data_anchors = out_data_anchor->GetPeerInDataAnchors();
  447. for (auto peer_in_data_anchor : peer_in_data_anchors) {
  448. GE_CHECK_NOTNULL(peer_in_data_anchor);
  449. auto peer_in_data_node = peer_in_data_anchor->GetOwnerNode();
  450. out_nodes.emplace(peer_in_data_node);
  451. outanchors_inanchors_nodes[out_data_anchor].emplace_back(std::make_pair(peer_in_data_anchor, peer_in_data_node));
  452. }
  453. }
  454. int32_t i = 0;
  455. auto node_desc = node->GetOpDesc();
  456. GE_CHECK_NOTNULL(node_desc);
  457. // Insert one enter node after node's per out data anchor
  458. for (auto &outanchor_inanchors_nodes : outanchors_inanchors_nodes) {
  459. string name = node->GetName() + "_" + ENTER + "_" + std::to_string(i++);
  460. GELOGD("Create Enter op %s after %s.", name.c_str(), node->GetName().c_str());
  461. auto enter_desc = AttrUtils::CopyOpDesc(copy_desc);
  462. enter_desc->SetName(name);
  463. GE_CHK_GRAPH_STATUS_RET(
  464. enter_desc->UpdateInputDesc("x", node_desc->GetOutputDesc(outanchor_inanchors_nodes.first->GetIdx())))
  465. GE_CHK_GRAPH_STATUS_RET(
  466. enter_desc->UpdateOutputDesc("y", node_desc->GetOutputDesc(outanchor_inanchors_nodes.first->GetIdx())))
  467. auto enter_node = graph_->AddNode(enter_desc);
  468. GE_CHECK_NOTNULL(enter_node);
  469. GE_CHK_GRAPH_STATUS_RET(outanchor_inanchors_nodes.first->LinkTo(enter_node->GetInDataAnchor(kDataInIndex)))
  470. GE_CHECK_NOTNULL(enter_node->GetOutDataAnchor(kDataInIndex));
  471. for (auto &inanchor_node : outanchor_inanchors_nodes.second) {
  472. GE_CHK_GRAPH_STATUS_RET(outanchor_inanchors_nodes.first->Unlink(inanchor_node.first))
  473. GE_CHK_GRAPH_STATUS_RET(enter_node->GetOutDataAnchor(kDataInIndex)->LinkTo(inanchor_node.first))
  474. GELOGD("Unlink from %s to %s, link from %s to %s then to %s.", node->GetName().c_str(),
  475. inanchor_node.second->GetName().c_str(), node->GetName().c_str(), enter_node->GetName().c_str(),
  476. inanchor_node.second->GetName().c_str());
  477. }
  478. }
  479. return SUCCESS;
  480. }
  481. // Move node's in control edges to out data nodes
  482. Status MultiBatchGraphCopyer::MoveCtrlEdgeToOutNodes(NodePtr &node, set<NodePtr> &out_nodes) {
  483. auto in_ctrl_anchor = node->GetInControlAnchor();
  484. GE_CHECK_NOTNULL(in_ctrl_anchor);
  485. auto peer_out_ctrl_anchors = in_ctrl_anchor->GetPeerOutControlAnchors();
  486. for (auto &peer_out_ctrl_anchor : peer_out_ctrl_anchors) {
  487. GE_CHK_GRAPH_STATUS_RET(peer_out_ctrl_anchor->Unlink(in_ctrl_anchor))
  488. GELOGD("Unlink control edge from %s to %s.", peer_out_ctrl_anchor->GetOwnerNode()->GetName().c_str(),
  489. node->GetName().c_str());
  490. for (auto &out_node : out_nodes) {
  491. auto in_ctrl_anchor_of_out_node = out_node->GetInControlAnchor();
  492. GE_CHECK_NOTNULL(in_ctrl_anchor_of_out_node);
  493. if (!peer_out_ctrl_anchor->IsLinkedWith(in_ctrl_anchor_of_out_node)) {
  494. GE_CHK_GRAPH_STATUS_RET(peer_out_ctrl_anchor->LinkTo(in_ctrl_anchor_of_out_node))
  495. GELOGD("Link control edge from %s to %s.", peer_out_ctrl_anchor->GetOwnerNode()->GetName().c_str(),
  496. out_node->GetName().c_str());
  497. }
  498. }
  499. }
  500. return SUCCESS;
  501. }
  502. Status MultiBatchGraphCopyer::DeleteEnterWithoutDataOut() {
  503. for (auto &node : graph_->GetAllNodes()) {
  504. GE_CHECK_NOTNULL(node);
  505. if (IsEnterType(node->GetType())) {
  506. auto out_nodes = node->GetOutAllNodes();
  507. if (out_nodes.empty()) {
  508. GELOGD("Delete enter node: %s which has no output.", node->GetName().c_str());
  509. GE_CHK_GRAPH_STATUS_RET(GraphUtils::IsolateNode(node, {}))
  510. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveNodeWithoutRelink(graph_, node))
  511. }
  512. }
  513. }
  514. return SUCCESS;
  515. }
  516. void MultiBatchGraphCopyer::LabelStatusForData(const NodePtr &data) {
  517. auto data_shape = NodeUtils::GetOutputDesc(*data, kDataOutIndex).GetShape();
  518. GELOGI("Label status for %s, shape_dims is %s.", data->GetName().c_str(),
  519. formats::JoinToString(data_shape.GetDims()).c_str());
  520. if (!IsAllDimsPositive(data_shape.GetDims())) {
  521. origin_nodes_status_[data.get()] = kNodeInBatchBranch;
  522. }
  523. }
  524. void MultiBatchGraphCopyer::LabelStatusForGetNextSink(const NodePtr &data) {
  525. auto op_desc = data->GetOpDesc();
  526. GELOGI("Out count of %s is %zu.", data->GetName().c_str(), op_desc->GetOutputsSize());
  527. size_t data_count = op_desc->GetOutputsSize() / kDivisionConst;
  528. for (size_t i = 0; i < data_count; ++i) {
  529. GeTensorDesc output_desc = op_desc->GetOutputDesc(i);
  530. GELOGD("The %zu data shape from getnext sink is %s.", i,
  531. formats::JoinToString(output_desc.GetShape().GetDims()).c_str());
  532. const auto &out_data_anchor = data->GetOutDataAnchor(i);
  533. if (out_data_anchor == nullptr) {
  534. continue;
  535. }
  536. size_t reference_times = out_data_anchor->GetPeerInDataAnchors().size();
  537. GELOGD("The %zu data has %zu referenced times.", i, reference_times);
  538. getnext_sink_dynamic_out_mapping_.emplace_back(std::make_pair(i, reference_times));
  539. if (!IsAllDimsPositive(output_desc.GetShape().GetDims())) {
  540. getnext_sink_dynamic_dims_ = true;
  541. }
  542. }
  543. if (getnext_sink_dynamic_dims_) {
  544. origin_nodes_status_[data.get()] = kNodeInBatchBranch;
  545. }
  546. }
  547. Status MultiBatchGraphCopyer::LabelInBatchBranchStatus() {
  548. GELOGD("Start label in batch branch status.");
  549. for (const auto &data : origin_data_nodes_) {
  550. auto op_desc = data->GetOpDesc();
  551. GE_IF_BOOL_EXEC(op_desc == nullptr,
  552. REPORT_INNER_ERROR("E19999", "op_desc in origin_data_nodes_ is nullptr, check invalid");
  553. GELOGE(PARAM_INVALID, "Op desc is nullptr.");
  554. return PARAM_INVALID);
  555. LabelStatusForData(data);
  556. if (!GetLocalOmgContext().dynamic_node_type.empty()) {
  557. LabelStatusForGetNextSink(data);
  558. }
  559. }
  560. map<string, vector<NodePtr>> frame_enters;
  561. InitStatus(frame_enters);
  562. bool changed = true;
  563. // If anyone of in node is kNodeInBatchBranch, it is also kNodeInBatchBranch
  564. while (changed) {
  565. changed = false;
  566. for (const auto &node : origin_all_nodes_) {
  567. auto iter = origin_nodes_status_.find(node.get());
  568. if (iter != origin_nodes_status_.end()) {
  569. continue;
  570. }
  571. for (auto &in_node : node->GetInDataNodes()) {
  572. if (origin_nodes_status_.find(in_node.get()) != origin_nodes_status_.end()) {
  573. if (origin_nodes_status_.find(node.get()) == origin_nodes_status_.end()) {
  574. origin_nodes_status_[node.get()] = kNodeInBatchBranch;
  575. ResetEnterStatus(frame_enters, node);
  576. changed = true;
  577. }
  578. break;
  579. }
  580. }
  581. }
  582. }
  583. return SUCCESS;
  584. }
  585. void MultiBatchGraphCopyer::InitStatus(map<string, vector<NodePtr>> &frame_enters) {
  586. for (const auto &node : origin_all_nodes_) {
  587. if (!IsEnterType(node->GetType())) {
  588. continue;
  589. }
  590. auto op_desc = node->GetOpDesc();
  591. if (op_desc == nullptr) {
  592. continue;
  593. }
  594. string frame_name;
  595. if (AttrUtils::GetStr(op_desc, ENTER_ATTR_FRAME_NAME, frame_name)) {
  596. frame_enters[frame_name].emplace_back(node);
  597. }
  598. }
  599. for (const auto &data : origin_data_nodes_) {
  600. auto data_shape = NodeUtils::GetOutputDesc(*data, kDataOutIndex).GetShape();
  601. if (!IsAllDimsPositive(data_shape.GetDims())) {
  602. origin_nodes_status_[data.get()] = kNodeInBatchBranch;
  603. }
  604. }
  605. }
  606. void MultiBatchGraphCopyer::ResetEnterStatus(map<string, vector<NodePtr>> &frame_enters, const NodePtr &node) {
  607. if (!IsEnterType(node->GetType())) {
  608. return;
  609. }
  610. for (const auto &frame_enter : frame_enters) {
  611. auto &enters = frame_enter.second;
  612. if (std::find(enters.begin(), enters.end(), node) != enters.end()) {
  613. for (const auto &enter : enters) {
  614. origin_nodes_status_[enter.get()] = kNodeInBatchBranch;
  615. }
  616. break;
  617. }
  618. }
  619. }
  620. Status MultiBatchGraphCopyer::LabelStatus() {
  621. if (LabelInBatchBranchStatus() != SUCCESS) {
  622. GELOGE(PARAM_INVALID, "Failed to label no in batch branch");
  623. return PARAM_INVALID;
  624. }
  625. for (const auto &node : origin_all_nodes_) {
  626. if (!(node->GetOpDesc()->GetSubgraphInstanceNames().empty())) {
  627. origin_nodes_status_[node.get()] = kNodeNotSupportNode;
  628. continue;
  629. }
  630. if (node->GetType() == NETOUTPUT) {
  631. origin_nodes_status_[node.get()] = kNodeOutBatchBranch;
  632. continue;
  633. }
  634. if (GetLocalOmgContext().dynamic_node_type.empty()) {
  635. if (IsDataLikeType(node->GetType())) {
  636. if (IsOnlyOutputToAipp(node)) {
  637. origin_nodes_status_[node.get()] = kNodeOutBatchBranch;
  638. } else {
  639. origin_nodes_status_[node.get()] = kNodeStartNode;
  640. }
  641. continue;
  642. }
  643. } else {
  644. if (IsDataLikeType(node->GetType())) {
  645. origin_nodes_status_[node.get()] = kNodeStartNode;
  646. continue;
  647. }
  648. if (IsGetNextType(node)) {
  649. origin_nodes_status_[node.get()] = kNodeStartNode;
  650. continue;
  651. }
  652. }
  653. if (origin_nodes_status_.find(node.get()) == origin_nodes_status_.end()) {
  654. origin_nodes_status_[node.get()] = kNodeOutBatchBranch;
  655. }
  656. }
  657. return SUCCESS;
  658. }
  659. Status MultiBatchGraphCopyer::CheckAndParseDynamicData(){
  660. size_t unknown_shape_count = 0;
  661. auto data_name_and_shape = GetLocalOmgContext().user_input_dims;
  662. GELOGD("raw data_name_and_shape size: %zu", data_name_and_shape.size());
  663. if (!getnext_sink_dynamic_dims_) {
  664. for (const auto &node : origin_all_nodes_) {
  665. auto data_desc = NodeUtils::GetOutputDesc(*node, kDataOutIndex);
  666. auto data_shape = data_desc.GetShape();
  667. auto data_format = data_desc.GetFormat() == Format::FORMAT_NCHW ? "NCHW" :
  668. data_desc.GetFormat() == Format::FORMAT_NHWC ? "NHWC" : "Others";
  669. auto data_name = node->GetName();
  670. auto branch_status = GetNodeStatus(node);
  671. if (branch_status != kNodeStartNode) {
  672. continue;
  673. }
  674. GELOGI("CheckAndParseDynamicData shape_dims is %s.", formats::JoinToString(data_shape.GetDims()).c_str());
  675. if (IsAllDimsPositive(data_shape.GetDims())) {
  676. continue;
  677. }
  678. std::vector<int64_t> data_shape_dims = data_shape.GetDims();
  679. ++unknown_shape_count;
  680. auto iter = find(data_name_order_.begin(), data_name_order_.end(), data_name);
  681. if (iter == data_name_order_.end()) {
  682. if (dynamic_type_ == DynamicType::kDynamicBatch) {
  683. auto ret = CheckDynamicBatchShape(data_shape_dims, data_name);
  684. GE_IF_BOOL_EXEC(ret == false, GELOGE(PARAM_INVALID, "Failed to check dynamic batch shape of %s.",
  685. data_name.c_str()); return PARAM_INVALID);
  686. } else if (dynamic_type_ == DynamicType::kDynamicImageSize) {
  687. auto ret = CheckDynamicImageSizeShape(data_shape_dims, data_name, data_format);
  688. GE_IF_BOOL_EXEC(ret == false, GELOGE(PARAM_INVALID, "Failed to check dynamic image size shape of %s.",
  689. data_name.c_str()); return PARAM_INVALID);
  690. } else if (dynamic_type_ == DynamicType::kDynamicDims) {
  691. ErrorManager::GetInstance().ATCReportErrMessage("E10001",
  692. {"parameter", "value" "reason"},
  693. {"--dynamic_dims", data_name,
  694. "all dynamic data must be set in --input_shape"});
  695. GELOGE(INTERNAL_ERROR, "data: %s shape:%s must be set int --input_shape",
  696. node->GetName().c_str(), data_shape.ToString().c_str());
  697. return INTERNAL_ERROR;
  698. }
  699. GELOGI("Data shape of %s is %s", data_name.c_str(), formats::JoinToString(data_shape_dims).c_str());
  700. data_name_and_shape.emplace_back(data_name, data_shape_dims);
  701. }
  702. }
  703. }
  704. auto ret = ParserDataToDynamicInfo(shapes_, data_name_and_shape, data_to_dynamic_info_);
  705. GE_CHK_STATUS_RET(ret, "Failed to parse data to dynamic info.");
  706. if (!getnext_sink_dynamic_dims_ && unknown_shape_count == 0) {
  707. ErrorManager::GetInstance().ATCReportErrMessage("E10040");
  708. GELOGE(PARAM_INVALID,
  709. "Need unknow shape data when user set --dynamic_batch_size, --dynamic_image_size or --dynamic_dims");
  710. return PARAM_INVALID;
  711. }
  712. return SUCCESS;
  713. }
  714. Status MultiBatchGraphCopyer::CreateNewNodes() {
  715. if (!getnext_sink_dynamic_dims_) {
  716. shape_data_ = InsertShapeDataNode();
  717. } else {
  718. shape_data_ = InsertGetDynamicDimsNode();
  719. }
  720. GE_IF_BOOL_EXEC(shape_data_ == nullptr, GELOGE(INTERNAL_ERROR, "Failed to create the shape node for multi batch");
  721. return INTERNAL_ERROR);
  722. GE_CHECK_NOTNULL(shape_data_->GetOpDesc());
  723. for (const auto &node : origin_all_nodes_) {
  724. GE_CHECK_NOTNULL(node->GetOpDesc());
  725. auto node_type = node->GetType();
  726. Status ret = INTERNAL_ERROR;
  727. auto branch_status = GetNodeStatus(node);
  728. GELOGD("Process node %s, status %d", node->GetName().c_str(), static_cast<int>(branch_status));
  729. switch (branch_status) {
  730. case kNodeStartNode:
  731. GELOGD("Name: %s, type: %s, status: kNodeStartNode.", node->GetName().c_str(), node->GetType().c_str());
  732. ret = InsertSwitchNAndUpdateMaxShape(node);
  733. break;
  734. case kNodeInBatchBranch:
  735. GELOGD("Name: %s, type: %s, status: kNodeInBatchBranch.", node->GetName().c_str(), node->GetType().c_str());
  736. ret = CopyNodeInBatchBranch(node);
  737. break;
  738. case kNodeOutBatchBranch:
  739. GELOGD("Name: %s, type: %s, status: kNodeOutBatchBranch.", node->GetName().c_str(), node->GetType().c_str());
  740. ret = InsertMergeForEdgeNode(node);
  741. if (ret == SUCCESS) {
  742. ret = LinkGetDynamicDimsToNetOutput(node);
  743. }
  744. break;
  745. case kNodeNotSupportNode:
  746. GELOGD("Name: %s, type: %s, status: kNodeNotSupportNode.", node->GetName().c_str(), node->GetType().c_str());
  747. break;
  748. default:
  749. GELOGE(INTERNAL_ERROR, "Unexpected status %d on node %s", static_cast<int>(branch_status),
  750. node->GetName().c_str());
  751. break;
  752. }
  753. if (ret != SUCCESS) {
  754. GELOGE(ret, "Failed to deal with node %s in multi-batch process", node->GetName().c_str());
  755. return ret;
  756. }
  757. }
  758. return SUCCESS;
  759. }
  760. NodePtr MultiBatchGraphCopyer::InsertMergeNode(const NodePtr &node, int index) {
  761. if (index < 0) {
  762. // the merge node must has data inputs, if origin connection is a control
  763. // edge, we use data edge instead
  764. index = 0;
  765. }
  766. auto &merge_nodes = nodes_to_merge_nodes_[node.get()];
  767. if (merge_nodes.empty()) {
  768. auto count = node->GetAllOutDataAnchorsSize();
  769. if (count == 0) {
  770. count = 1;
  771. }
  772. merge_nodes.resize(count, nullptr);
  773. }
  774. if (merge_nodes.at(index) != nullptr) {
  775. return merge_nodes[index];
  776. }
  777. auto merge_node_name = node->GetName() + "_ascend_mbatch_merge_" + std::to_string(index);
  778. auto merge_node = InsertMergeNodeToGraph(merge_node_name, shapes_.size(), node->GetOwnerComputeGraph());
  779. GE_IF_BOOL_EXEC(merge_node == nullptr, GELOGE(INTERNAL_ERROR, "Failed to create merge node for node %s, out index %d",
  780. node->GetName().c_str(), index);
  781. return nullptr);
  782. merge_nodes[index] = merge_node;
  783. GELOGI("Create merge node %s for node %s index %d", merge_node_name.c_str(), node->GetName().c_str(), index);
  784. return merge_node;
  785. }
  786. NodePtr MultiBatchGraphCopyer::FindSwitchnNodeForDataEdge(const OutDataAnchorPtr &data_out_anchor,
  787. const NodePtr &origin_node) {
  788. auto data_node = data_out_anchor->GetOwnerNode();
  789. GELOGD("Start find switchn node insert between %s and %s", data_node->GetName().c_str(),
  790. origin_node->GetName().c_str());
  791. NodePtr switchn = nullptr;
  792. if (!getnext_sink_dynamic_dims_ && data_nodes_to_switchn_.count(data_node.get()) > 0) {
  793. switchn = data_nodes_to_switchn_[data_node.get()];
  794. return switchn;
  795. }
  796. bool is_getnext_sink_data = false;
  797. for (size_t i = 0; i < getnext_nodes_to_switchn_.size(); ++i) {
  798. for (size_t j = 0; j < getnext_nodes_to_switchn_.at(i).size(); ++j) {
  799. if (getnext_nodes_to_switchn_.at(i).at(j).first == data_node.get()) {
  800. is_getnext_sink_data = true;
  801. break;
  802. }
  803. }
  804. }
  805. // get output_idx of origin_node(getnext)
  806. if (is_getnext_sink_data) {
  807. auto output_idx = data_out_anchor->GetIdx();
  808. size_t referenced_index = 0;
  809. GELOGI("The output idx %d has %zu referenced nums.", output_idx, data_out_anchor->GetPeerInDataAnchors().size());
  810. for (const auto &peer_in_anchor : data_out_anchor->GetPeerInDataAnchors()) {
  811. if (peer_in_anchor->GetOwnerNode()->GetOpDesc() == nullptr) {
  812. REPORT_INNER_ERROR("E19999", "peer op_desc of op:%s(%s)'s out_index:%d anchor exist nullptr, "
  813. "check invalid",
  814. data_node->GetName().c_str(), data_node->GetType().c_str(), output_idx);
  815. GELOGE(INTERNAL_ERROR, "Op desc should not be nullptr.");
  816. return nullptr;
  817. }
  818. if (getnext_nodes_to_switchn_.at(output_idx).empty()) {
  819. GELOGI("Output idx %d of %s is static output.", output_idx, data_node->GetName().c_str());
  820. return nullptr;
  821. }
  822. if (output_idx >= static_cast<int>(getnext_nodes_to_switchn_.size()) ||
  823. referenced_index >= getnext_nodes_to_switchn_.at(output_idx).size()) {
  824. REPORT_INNER_ERROR("E19999", "output_index:%d of op:%s(%s) > getnext_nodes_to_switchn_.size():%zu or "
  825. "referenced_index:%zu >= getnext_nodes_to_switchn_.at(output_idx).size():%zu, "
  826. "check invalid", output_idx,
  827. data_node->GetName().c_str(), data_node->GetType().c_str(), getnext_nodes_to_switchn_.size(),
  828. referenced_index, getnext_nodes_to_switchn_.at(output_idx).size());
  829. GELOGE(INTERNAL_ERROR, "Output idx is %d, referenced index is %zu", output_idx, referenced_index);
  830. return nullptr;
  831. }
  832. if (peer_in_anchor->GetOwnerNode()->GetOpDesc()->GetName() == origin_node->GetName()) {
  833. switchn = getnext_nodes_to_switchn_.at(output_idx).at(referenced_index).second;
  834. GELOGI("Name of switchn is %s.", switchn->GetName().c_str());
  835. return switchn;
  836. }
  837. referenced_index++;
  838. }
  839. }
  840. return switchn;
  841. }
  842. Status MultiBatchGraphCopyer::CopyInDataEdges(const NodePtr &origin_node, int batch_num, const NodePtr &copyed_node) {
  843. GELOGI("Start copy data edges for %s and %s.", origin_node->GetName().c_str(), copyed_node->GetName().c_str());
  844. for (auto &in_anchor : origin_node->GetAllInDataAnchors()) {
  845. auto origin_src_anchor = in_anchor->GetPeerOutAnchor();
  846. if (origin_src_anchor == nullptr) {
  847. GELOGD("The node %s does not have input on index %d", origin_node->GetName().c_str(), in_anchor->GetIdx());
  848. continue;
  849. }
  850. auto origin_src_node = origin_src_anchor->GetOwnerNode();
  851. auto dst_anchor = copyed_node->GetInDataAnchor(in_anchor->GetIdx());
  852. GE_CHECK_NOTNULL(dst_anchor);
  853. auto switchn = FindSwitchnNodeForDataEdge(origin_src_anchor, origin_node);
  854. if (switchn != nullptr) {
  855. auto ret = GraphUtils::AddEdge(switchn->GetOutDataAnchor(batch_num), dst_anchor);
  856. if (ret != GRAPH_SUCCESS) {
  857. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(out_index:%d) and op:%s(%s)(in_index:%d) failed",
  858. switchn->GetName().c_str(), switchn->GetType().c_str(),
  859. batch_num, copyed_node->GetName().c_str(), copyed_node->GetType().c_str(),
  860. in_anchor->GetIdx());
  861. GELOGE(INTERNAL_ERROR, "Failed to add data edge between %s(%d) to %s(%d), error-code %u",
  862. switchn->GetName().c_str(), batch_num, copyed_node->GetName().c_str(), in_anchor->GetIdx(),
  863. ret);
  864. return INTERNAL_ERROR;
  865. }
  866. GELOGD("Add data edge from %s(%d) to %s(%d)", switchn->GetName().c_str(), batch_num,
  867. copyed_node->GetName().c_str(), in_anchor->GetIdx());
  868. continue;
  869. }
  870. auto batch_branch_iter = nodes_to_batch_nodes_.find(origin_src_node.get());
  871. if (batch_branch_iter != nodes_to_batch_nodes_.end()) {
  872. auto src_batch_node = batch_branch_iter->second.at(batch_num);
  873. auto ret = GraphUtils::AddEdge(src_batch_node->GetOutDataAnchor(origin_src_anchor->GetIdx()), dst_anchor);
  874. if (ret != GRAPH_SUCCESS) {
  875. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(out_index:%d) and op:%s(%s)(in_index:%d) failed",
  876. src_batch_node->GetName().c_str(),
  877. src_batch_node->GetType().c_str(), origin_src_anchor->GetIdx(),
  878. copyed_node->GetName().c_str(), copyed_node->GetType().c_str(),
  879. in_anchor->GetIdx());
  880. GELOGE(INTERNAL_ERROR, "Failed to add data edge between %s(%d) to %s(%d), error-code %u",
  881. src_batch_node->GetName().c_str(), batch_num, copyed_node->GetName().c_str(), in_anchor->GetIdx(), ret);
  882. return INTERNAL_ERROR;
  883. }
  884. GELOGD("Add data edge from %s(%d) to %s(%d)", src_batch_node->GetName().c_str(), batch_num,
  885. copyed_node->GetName().c_str(), in_anchor->GetIdx());
  886. continue;
  887. }
  888. auto ret = GraphUtils::AddEdge(origin_src_anchor, dst_anchor);
  889. if (ret != GRAPH_SUCCESS) {
  890. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(out_index:%d) and op:%s(%s)(in_index:%d) failed",
  891. origin_src_node->GetName().c_str(),
  892. origin_src_node->GetType().c_str(), origin_src_anchor->GetIdx(),
  893. copyed_node->GetName().c_str(), copyed_node->GetType().c_str(),
  894. in_anchor->GetIdx());
  895. GELOGE(INTERNAL_ERROR, "Failed to add data edge between origin node %s(%d) to copyed %s(%d)",
  896. origin_src_node->GetName().c_str(), origin_src_anchor->GetIdx(), copyed_node->GetName().c_str(),
  897. dst_anchor->GetIdx());
  898. return INTERNAL_ERROR;
  899. }
  900. GELOGD("Add data edge between branch-out %s(%d) to branch-in %s(%d)", origin_src_node->GetName().c_str(),
  901. origin_src_anchor->GetIdx(), copyed_node->GetName().c_str(), dst_anchor->GetIdx());
  902. }
  903. return SUCCESS;
  904. }
  905. Status MultiBatchGraphCopyer::CopyInControlEdges(const NodePtr &node, int batch_num, const NodePtr &copyed_node) {
  906. GELOGI("Start copy control edge for %s and %s.", node->GetName().c_str(), copyed_node->GetName().c_str());
  907. for (auto &origin_src_node : node->GetInControlNodes()) {
  908. auto switchn_iter = data_nodes_to_switchn_.find(origin_src_node.get());
  909. if (switchn_iter != data_nodes_to_switchn_.end()) {
  910. // reconnect data node
  911. auto ret = GraphUtils::AddEdge(switchn_iter->second->GetOutControlAnchor(), copyed_node->GetInControlAnchor());
  912. if (ret != GRAPH_SUCCESS) {
  913. REPORT_CALL_ERROR("E19999", "Add ctrl edge between op:%s(%s) and op:%s(%s) failed",
  914. switchn_iter->second->GetName().c_str(), switchn_iter->second->GetType().c_str(),
  915. copyed_node->GetName().c_str(), copyed_node->GetType().c_str());
  916. GELOGE(INTERNAL_ERROR, "Failed to add control edge between %s to %s, error-code %u",
  917. switchn_iter->second->GetName().c_str(), copyed_node->GetName().c_str(), ret);
  918. return INTERNAL_ERROR;
  919. }
  920. GELOGD("Add control edge from %s to %s", switchn_iter->second->GetName().c_str(), copyed_node->GetName().c_str());
  921. continue;
  922. }
  923. auto batch_branch_iter = nodes_to_batch_nodes_.find(origin_src_node.get());
  924. if (batch_branch_iter != nodes_to_batch_nodes_.end()) {
  925. // reconnect node in batch branch
  926. auto src_batch_node = batch_branch_iter->second.at(batch_num);
  927. auto ret = GraphUtils::AddEdge(src_batch_node->GetOutControlAnchor(), copyed_node->GetInControlAnchor());
  928. if (ret != GRAPH_SUCCESS) {
  929. REPORT_CALL_ERROR("E19999", "Add ctrl edge between op:%s(%s) and op:%s(%s) failed",
  930. src_batch_node->GetName().c_str(), src_batch_node->GetType().c_str(),
  931. copyed_node->GetName().c_str(), copyed_node->GetType().c_str());
  932. GELOGE(INTERNAL_ERROR, "Failed to add data edge between %s to %s, error-code %u",
  933. src_batch_node->GetName().c_str(), copyed_node->GetName().c_str(), ret);
  934. return INTERNAL_ERROR;
  935. }
  936. GELOGD("Add control edge from %s to %s", src_batch_node->GetName().c_str(), copyed_node->GetName().c_str());
  937. continue;
  938. }
  939. auto ret = GraphUtils::AddEdge(origin_src_node->GetOutControlAnchor(), copyed_node->GetInControlAnchor());
  940. if (ret != GRAPH_SUCCESS) {
  941. REPORT_CALL_ERROR("E19999", "Add ctrl edge between op:%s(%s) and op:%s(%s) failed",
  942. origin_src_node->GetName().c_str(), origin_src_node->GetType().c_str(),
  943. copyed_node->GetName().c_str(), copyed_node->GetType().c_str());
  944. GELOGE(INTERNAL_ERROR, "Failed to add control edge from origin %s to copyed %s",
  945. origin_src_node->GetName().c_str(), copyed_node->GetName().c_str());
  946. return INTERNAL_ERROR;
  947. }
  948. GELOGD("Add control edge between branch-out %s to branch-in %s", origin_src_node->GetName().c_str(),
  949. copyed_node->GetName().c_str());
  950. }
  951. return SUCCESS;
  952. }
  953. NodePtr MultiBatchGraphCopyer::InsertShapeDataNode() {
  954. auto desc = MakeShared<OpDesc>();
  955. if (desc == nullptr) {
  956. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  957. GELOGE(OUT_OF_MEMORY, "Failed to create shape data node, out of memory");
  958. return nullptr;
  959. }
  960. string node_name = "ascend_mbatch_shape_data";
  961. // Only flush subgraph name
  962. if (graph_->GetParentGraph() != nullptr) {
  963. node_name = graph_->GetName() + "_" + node_name;
  964. }
  965. desc->SetName(node_name);
  966. desc->SetType(DATA);
  967. // input and output of DATA is gear_info
  968. GeTensorDesc tensor_desc(GeShape({static_cast<int64_t>(shapes_.at(0).size())}), FORMAT_ND, DT_INT64);
  969. auto ret = desc->AddInputDesc(tensor_desc);
  970. if (ret != GRAPH_SUCCESS) {
  971. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  972. desc->GetName().c_str(), desc->GetType().c_str());
  973. GELOGE(INTERNAL_ERROR, "Failed to add input desc for created data");
  974. return nullptr;
  975. }
  976. ret = desc->AddOutputDesc(tensor_desc);
  977. if (ret != GRAPH_SUCCESS) {
  978. REPORT_CALL_ERROR("E19999", "Add output desc into op:%s(%s) failed",
  979. desc->GetName().c_str(), desc->GetType().c_str());
  980. GELOGE(INTERNAL_ERROR, "Failed to add output desc for created data");
  981. return nullptr;
  982. }
  983. if (!AttrUtils::SetBool(desc, ATTR_INSERT_BY_MBATCH, true)) {
  984. REPORT_CALL_ERROR("E19999", "Set Attr:%s to node:%s(%s) failed",
  985. ATTR_INSERT_BY_MBATCH.c_str(), desc->GetName().c_str(), desc->GetType().c_str());
  986. GELOGE(INTERNAL_ERROR, "Failed to add attr for created data");
  987. return nullptr;
  988. }
  989. auto data_node = graph_->AddNode(desc);
  990. if (data_node == nullptr) {
  991. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  992. desc->GetName().c_str(), desc->GetType().c_str(), graph_->GetName().c_str());
  993. GELOGE(INTERNAL_ERROR, "Failed to add shape data node to graph");
  994. return nullptr;
  995. }
  996. ret = GraphUtils::AppendInputNode(graph_, data_node);
  997. if (ret != GRAPH_SUCCESS) {
  998. REPORT_CALL_ERROR("E19999", "Append input node:%s(%s) to graph:%s failed",
  999. data_node->GetName().c_str(), data_node->GetType().c_str(),
  1000. graph_->GetName().c_str());
  1001. GELOGE(INTERNAL_ERROR, "Failed to append data node %s as input to graph", data_node->GetName().c_str());
  1002. return nullptr;
  1003. }
  1004. return data_node;
  1005. }
  1006. NodePtr MultiBatchGraphCopyer::InsertGetDynamicDimsNode() {
  1007. GELOGD("Start insert getdynamicdims node to get shape info.");
  1008. auto desc = MakeShared<OpDesc>();
  1009. if (desc == nullptr) {
  1010. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  1011. GELOGE(OUT_OF_MEMORY, "Failed to create shape data node, out of memory");
  1012. return nullptr;
  1013. }
  1014. string node_name = "ascend_mbatch_get_dynamic_dims_node";
  1015. // Only flush subgraph name
  1016. if (graph_->GetParentGraph() != nullptr) {
  1017. node_name = graph_->GetName() + "_" + node_name;
  1018. }
  1019. desc->SetName(node_name);
  1020. desc->SetType(GETDYNAMICDIMS);
  1021. // input of GetDynamicDims is shape_of_each_data, output is gear_info
  1022. for (size_t i = 0; i < GetLocalOmgContext().user_input_dims.size(); ++i) {
  1023. size_t input_shape_dims = GetLocalOmgContext().user_input_dims.at(i).second.size();
  1024. if (input_shape_dims == 1 && GetLocalOmgContext().user_input_dims.at(i).second.at(0) == 0) {
  1025. GeTensorDesc tensor_desc;
  1026. tensor_desc.SetFormat(FORMAT_ND);
  1027. tensor_desc.SetDataType(DT_INT64);
  1028. auto ret = desc->AddInputDesc(tensor_desc);
  1029. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  1030. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  1031. desc->GetName().c_str(), desc->GetType().c_str());
  1032. GELOGE(INTERNAL_ERROR, "Failed to add input desc for created data");
  1033. return nullptr);
  1034. continue;
  1035. }
  1036. GeTensorDesc tensor_desc(GeShape({static_cast<int64_t>(input_shape_dims)}), FORMAT_ND, DT_INT64);
  1037. auto ret = desc->AddInputDesc(tensor_desc);
  1038. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  1039. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  1040. desc->GetName().c_str(), desc->GetType().c_str());
  1041. GELOGE(INTERNAL_ERROR, "Failed to add input desc for created data");
  1042. return nullptr);
  1043. }
  1044. GeTensorDesc tensor_desc(GeShape({static_cast<int64_t>(shapes_.at(0).size())}), FORMAT_ND, DT_INT64);
  1045. auto ret = desc->AddOutputDesc(tensor_desc);
  1046. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  1047. REPORT_CALL_ERROR("E19999", "Add output desc to op:%s(%s) failed",
  1048. desc->GetName().c_str(), desc->GetType().c_str());
  1049. GELOGE(INTERNAL_ERROR, "Failed to add output desc for created data");
  1050. return nullptr);
  1051. if (!AttrUtils::SetBool(desc, ATTR_INSERT_BY_MBATCH, true)) {
  1052. REPORT_CALL_ERROR("E19999", "Set Attr:%s to node:%s(%s) failed",
  1053. ATTR_INSERT_BY_MBATCH.c_str(), desc->GetName().c_str(), desc->GetType().c_str());
  1054. GELOGE(INTERNAL_ERROR, "Failed to add attr for created data");
  1055. return nullptr;
  1056. }
  1057. auto data_node = graph_->AddNode(desc);
  1058. if (data_node == nullptr) {
  1059. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  1060. desc->GetName().c_str(), desc->GetType().c_str(), graph_->GetName().c_str());
  1061. GELOGE(INTERNAL_ERROR, "Failed to add shape data node to graph");
  1062. return nullptr;
  1063. }
  1064. ret = GraphUtils::AppendInputNode(graph_, data_node);
  1065. if (ret != GRAPH_SUCCESS) {
  1066. REPORT_CALL_ERROR("E19999", "Append input node:%s(%s) to graph:%s failed",
  1067. data_node->GetName().c_str(), data_node->GetType().c_str(),
  1068. graph_->GetName().c_str());
  1069. GELOGE(INTERNAL_ERROR, "Failed to append data node %s as input to graph", data_node->GetName().c_str());
  1070. return nullptr;
  1071. }
  1072. return data_node;
  1073. }
  1074. Status MultiBatchGraphCopyer::CheckArguments() {
  1075. if (graph_ == nullptr) {
  1076. REPORT_INNER_ERROR("E19999", "graph_ is nullptr, check invalid");
  1077. GELOGE(PARAM_INVALID, "Failed to copy graph, the graph is null");
  1078. return PARAM_INVALID;
  1079. }
  1080. return CheckDynamicParams(shapes_);
  1081. }
  1082. Status MultiBatchGraphCopyer::CheckCopyResult(const std::vector<NodePtr> &start_nodes) {
  1083. for (auto &node : start_nodes) {
  1084. if (IsOnlyOutputToAipp(node)) {
  1085. continue;
  1086. }
  1087. auto dims = NodeUtils::GetOutputDesc(*node, kDataOutIndex).GetShape().GetDims();
  1088. if (!IsAllDimsPositive(dims)) {
  1089. REPORT_CALL_ERROR("E19999", "Failed to copy multi batch graph, the node %s still has unknown shape %s",
  1090. node->GetName().c_str(), formats::ShapeToString(dims).c_str());
  1091. GELOGE(INTERNAL_ERROR, "Failed to copy multi batch graph, the node %s still has unknown shape %s",
  1092. node->GetName().c_str(), formats::ShapeToString(dims).c_str());
  1093. return INTERNAL_ERROR;
  1094. }
  1095. }
  1096. return SUCCESS;
  1097. }
  1098. bool MultiBatchGraphCopyer::IsInBatchBranch(const NodePtr &node) {
  1099. if (!getnext_sink_dynamic_dims_) {
  1100. return (nodes_to_batch_nodes_.count(node.get()) > 0) || (data_nodes_to_switchn_.count(node.get()) > 0);
  1101. } else {
  1102. for (size_t i = 0; i < getnext_nodes_to_switchn_.size(); ++i) {
  1103. for (size_t j = 0; j < getnext_nodes_to_switchn_.at(i).size(); ++j) {
  1104. if (getnext_nodes_to_switchn_.at(i).at(j).first == node.get()) {
  1105. return true;
  1106. }
  1107. }
  1108. }
  1109. return nodes_to_batch_nodes_.count(node.get()) > 0;
  1110. }
  1111. }
  1112. Status MultiBatchGraphCopyer::LinkDataToMerge(const NodePtr &data, const NodePtr &merge, const NodePtr &switchn) {
  1113. // The caller should make sure that the there is a SwitchN node in the map
  1114. GELOGI("Link edge between data %s to merge %s throw switchn %s", data->GetName().c_str(), merge->GetName().c_str(),
  1115. switchn->GetName().c_str());
  1116. for (size_t i = 0; i < shapes_.size(); ++i) {
  1117. auto ret = GraphUtils::AddEdge(switchn->GetOutDataAnchor(i), merge->GetInDataAnchor(i));
  1118. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  1119. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%zu) and op:%s(%s)(index:%zu) failed",
  1120. switchn->GetName().c_str(), switchn->GetType().c_str(), i,
  1121. merge->GetName().c_str(), merge->GetType().c_str(), i);
  1122. GELOGE(INTERNAL_ERROR, "Failed to add edge between switchn %s(%zu) to merge %s(%zu), error-code %u",
  1123. switchn->GetName().c_str(), i, merge->GetName().c_str(), i, ret);
  1124. return INTERNAL_ERROR);
  1125. }
  1126. return SUCCESS;
  1127. }
  1128. Status MultiBatchGraphCopyer::LinkNodeToMerge(const NodePtr &node, int out_index, const NodePtr &merge) {
  1129. auto &copyed_nodes = nodes_to_batch_nodes_[node.get()];
  1130. if (copyed_nodes.size() != shapes_.size()) {
  1131. REPORT_INNER_ERROR("E19999", "Create merge node for node %s failed, "
  1132. "the copyed nodes for it count %zu different with shape %zu, check invalid",
  1133. node->GetName().c_str(), copyed_nodes.size(), shapes_.size());
  1134. GELOGE(INTERNAL_ERROR,
  1135. "Failed to create merge node for node %s, the copyed nodes for it count %zu different with shape %zu",
  1136. node->GetName().c_str(), copyed_nodes.size(), shapes_.size());
  1137. return INTERNAL_ERROR;
  1138. }
  1139. for (size_t i = 0; i < copyed_nodes.size(); ++i) {
  1140. auto src_node = copyed_nodes[i];
  1141. if (src_node->GetAllOutDataAnchorsSize() == 0) {
  1142. // if the node does not has any data output, we should create an const for it, like this:
  1143. // c d
  1144. // node ---> const ---> merge
  1145. auto const_name = src_node->GetName() + "_merge_const";
  1146. GELOGI("The node %s on the batch branch edge does not have any data output, create a const %s for it",
  1147. src_node->GetName().c_str(), const_name.c_str());
  1148. auto const_node = InsertConst(const_name, graph_);
  1149. GE_IF_BOOL_EXEC(const_node == nullptr,
  1150. GELOGE(OUT_OF_MEMORY, "Failed to create const for node %s to connect to a merge node",
  1151. src_node->GetName().c_str());
  1152. return OUT_OF_MEMORY);
  1153. auto ret = GraphUtils::AddEdge(src_node->GetOutControlAnchor(), const_node->GetInControlAnchor());
  1154. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  1155. REPORT_CALL_ERROR("E19999", "Add ctrl edge between op:%s(%s) and op:%s(%s) failed",
  1156. src_node->GetName().c_str(), src_node->GetType().c_str(),
  1157. const_node->GetName().c_str(), const_node->GetType().c_str());
  1158. GELOGE(INTERNAL_ERROR, "Failed to add control edge from %s to %s",
  1159. src_node->GetName().c_str(), const_node->GetName().c_str());
  1160. return INTERNAL_ERROR);
  1161. src_node = const_node;
  1162. }
  1163. auto ret = GraphUtils::AddEdge(src_node->GetOutDataAnchor(out_index), merge->GetInDataAnchor(i));
  1164. if (ret != GRAPH_SUCCESS) {
  1165. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%zu) failed",
  1166. src_node->GetName().c_str(), src_node->GetType().c_str(), out_index,
  1167. merge->GetName().c_str(), merge->GetType().c_str(), i);
  1168. GELOGE(INTERNAL_ERROR,
  1169. "Failed to add edge between copyed node %s(%d) to inserted merge node %s(%zu), error-code %u",
  1170. copyed_nodes[i]->GetName().c_str(), out_index, merge->GetName().c_str(), i, ret);
  1171. return INTERNAL_ERROR;
  1172. }
  1173. }
  1174. return SUCCESS;
  1175. }
  1176. Status MultiBatchGraphCopyer::InsertSwitchNAndUpdateMaxShape(const NodePtr &node) {
  1177. std::vector<std::pair<Node *, NodePtr>> dynamic_out_to_switchn;
  1178. if (!getnext_sink_dynamic_dims_) {
  1179. if (InsertSwitchNForData(node, kDataOutIndex, kDataOutIndex, dynamic_out_to_switchn) != SUCCESS) {
  1180. GELOGE(PARAM_INVALID, "Failed to insert switchn for %s.", node->GetName().c_str());
  1181. return PARAM_INVALID;
  1182. }
  1183. if (UpdateMaxShapeToData(node, kDataOutIndex) != SUCCESS) {
  1184. GELOGE(PARAM_INVALID, "Failed to update max shape of %s.", node->GetName().c_str());
  1185. return PARAM_INVALID;
  1186. }
  1187. } else {
  1188. if (!IsGetNextType(node)) {
  1189. GELOGI("No need to insert switchn and update max shape for %s when get sink dynamic.", node->GetName().c_str());
  1190. return SUCCESS;
  1191. }
  1192. for (size_t i = 0; i < getnext_sink_dynamic_out_mapping_.size(); ++i) {
  1193. dynamic_out_to_switchn.clear();
  1194. for (size_t j = 0; j < getnext_sink_dynamic_out_mapping_.at(i).second; ++j) {
  1195. GELOGI("The %zu data_index has %zu referenced nums.", getnext_sink_dynamic_out_mapping_.at(i).first,
  1196. getnext_sink_dynamic_out_mapping_.at(i).second);
  1197. if (InsertSwitchNForData(node, getnext_sink_dynamic_out_mapping_.at(i).first, j, dynamic_out_to_switchn) !=
  1198. SUCCESS) {
  1199. GELOGE(PARAM_INVALID, "Failed to insert switchn for %s of %zu out anchor when referenced index is %zu",
  1200. node->GetName().c_str(), getnext_sink_dynamic_out_mapping_.at(i).first, j);
  1201. return PARAM_INVALID;
  1202. }
  1203. }
  1204. getnext_nodes_to_switchn_.emplace_back(dynamic_out_to_switchn);
  1205. }
  1206. for (size_t i = 0; i < getnext_sink_dynamic_out_mapping_.size(); ++i) {
  1207. if(UpdateMaxShapeToData(node, i) != SUCCESS) {
  1208. GELOGE(PARAM_INVALID, "Failed to update %s max shape of %zu out anchor", node->GetName().c_str(), i);
  1209. return PARAM_INVALID;
  1210. }
  1211. }
  1212. }
  1213. return SUCCESS;
  1214. }
  1215. Status MultiBatchGraphCopyer::UpdateShapeOfShapeNode(const NodePtr &node, size_t out_anchor_index) {
  1216. auto data_shape = NodeUtils::GetOutputDesc(*node, out_anchor_index).GetShape();
  1217. size_t shape_index = out_anchor_index + (node->GetAllOutDataAnchors().size() / kDivisionConst);
  1218. GeTensorDesc output_desc = node->GetOpDesc()->GetOutputDesc(shape_index);
  1219. std::vector<int64_t> output_dims = {static_cast<int64_t>(data_shape.GetDims().size())};
  1220. GeShape output_shape(output_dims);
  1221. output_desc.SetShape(output_shape);
  1222. if (node->GetOpDesc()->UpdateOutputDesc(shape_index, output_desc) != SUCCESS) {
  1223. REPORT_CALL_ERROR("E19999", "Update output desc to op:%s(%s) failed, index:%zu",
  1224. node->GetName().c_str(), node->GetType().c_str(), shape_index);
  1225. GELOGE(FAILED, "Update output desc fail.");
  1226. return FAILED;
  1227. }
  1228. return SUCCESS;
  1229. }
  1230. Status MultiBatchGraphCopyer::UpdateMaxShapeToData(const NodePtr &node, size_t out_anchor_index) {
  1231. GELOGD("Start update max shape of %s, %zu output.", node->GetName().c_str(), out_anchor_index);
  1232. auto data_shape = NodeUtils::GetOutputDesc(*node, out_anchor_index).GetShape();
  1233. string data_name = node->GetName();
  1234. if (getnext_sink_dynamic_dims_) {
  1235. data_name.append("_").append(std::to_string(out_anchor_index));
  1236. }
  1237. GELOGD("Update max shape of %s, shape dims is %s.", data_name.c_str(),
  1238. formats::JoinToString(data_shape.GetDims()).c_str());
  1239. if (!getnext_sink_dynamic_dims_) {
  1240. if (IsAllDimsPositive(data_shape.GetDims())) {
  1241. GELOGD("No need to do anything for static data.");
  1242. return SUCCESS;
  1243. }
  1244. } else {
  1245. if (IsAllDimsPositive(data_shape.GetDims())) {
  1246. // need to update shape of Shape_node
  1247. GE_CHK_STATUS_RET(UpdateShapeOfShapeNode(node, out_anchor_index), "Failed to update shape of shape node");
  1248. return SUCCESS;
  1249. }
  1250. }
  1251. size_t max_shape_index = 0;
  1252. int64_t max_size = 0;
  1253. for (size_t i = 0; i < shapes_.size(); ++i) {
  1254. int64_t size = 1;
  1255. for (auto dim : data_to_dynamic_info_.at(data_name).at(i)) {
  1256. if (INT64_MAX / dim < size) {
  1257. REPORT_CALL_ERROR("E19999", "Op:%s(%s)'s shape:%s size will overflow after multi, check invalid",
  1258. node->GetName().c_str(), node->GetType().c_str(),
  1259. formats::ShapeToString(data_to_dynamic_info_[data_name].at(i)).c_str());
  1260. GELOGE(PARAM_INVALID, "The shape %s size overflow",
  1261. formats::ShapeToString(data_to_dynamic_info_[data_name].at(i)).c_str());
  1262. return PARAM_INVALID;
  1263. }
  1264. size *= dim;
  1265. }
  1266. if (size > max_size) {
  1267. max_size = size;
  1268. max_shape_index = i;
  1269. }
  1270. }
  1271. // must not be error, the calc result has been checked in function InsertSwitchNForData
  1272. (void)CalcShape(data_to_dynamic_info_.at(data_name).at(max_shape_index), data_shape);
  1273. auto ret = NodeUtils::UpdateOutputShape(*node, out_anchor_index, data_shape);
  1274. GE_CHK_GRAPH_STATUS_RET(ret, "Failed to update output shape for data %s", node->GetName().c_str());
  1275. // getnext_sink not has input
  1276. if (!getnext_sink_dynamic_dims_) {
  1277. ret = NodeUtils::UpdateInputShape(*node, kDataInIndex, data_shape);
  1278. GE_CHK_GRAPH_STATUS_RET(ret, "Failed to update input shape for data %s", node->GetName().c_str());
  1279. } else {
  1280. // need to update shape of Shape_node when getnext_sink_dynamic
  1281. GE_CHK_STATUS_RET(UpdateShapeOfShapeNode(node, out_anchor_index), "Failed to update shape of shape node");
  1282. }
  1283. GELOGI("Update the data %s input/output shape to the max %s", node->GetName().c_str(),
  1284. formats::ShapeToString(data_shape).c_str());
  1285. return SUCCESS;
  1286. }
  1287. Status MultiBatchGraphCopyer::InsertSwitchNForData(const NodePtr &node, const size_t &out_anchor_index,
  1288. const size_t &peer_in_anchor_index,
  1289. std::vector<std::pair<Node *, NodePtr>> &dynamic_out_to_switchn) {
  1290. auto data_shape = NodeUtils::GetOutputDesc(*node, out_anchor_index).GetShape();
  1291. string data_name = node->GetName();
  1292. if (getnext_sink_dynamic_dims_) {
  1293. data_name.append("_").append(std::to_string(out_anchor_index));
  1294. }
  1295. (void)AttrUtils::SetListInt(node->GetOpDesc(), ATTR_MBATCH_ORIGIN_INPUT_DIMS, data_shape.GetDims());
  1296. GELOGI("Insert switchn node of %s, shape dims is %s.", data_name.c_str(),
  1297. formats::JoinToString(data_shape.GetDims()).c_str());
  1298. if (IsAllDimsPositive(data_shape.GetDims())) {
  1299. GELOGI("The shape of data %s are positive(%s), skip the multi batch process", node->GetName().c_str(),
  1300. data_shape.ToString().c_str());
  1301. return SUCCESS;
  1302. }
  1303. auto switchn_desc = MakeShared<OpDesc>();
  1304. GE_IF_BOOL_EXEC(switchn_desc == nullptr,
  1305. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  1306. GELOGE(OUT_OF_MEMORY, "Failed to create switchn for data %s", node->GetName().c_str());
  1307. return OUT_OF_MEMORY);
  1308. string switchn_name = node->GetName() + "_ascend_mbatch_switchn";
  1309. if (getnext_sink_dynamic_dims_) {
  1310. switchn_name.append("_").append(std::to_string(out_anchor_index))
  1311. .append("_").append(std::to_string(peer_in_anchor_index));
  1312. }
  1313. GELOGI("name of switchn is %s.", switchn_name.c_str());
  1314. switchn_desc->SetName(switchn_name);
  1315. switchn_desc->SetType(SWITCHN);
  1316. GeTensorDesc tensor(NodeUtils::GetOutputDesc(*node, out_anchor_index));
  1317. GE_IF_BOOL_EXEC(switchn_desc->AddInputDesc("data", tensor) != GRAPH_SUCCESS,
  1318. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed, input desc name:%s",
  1319. switchn_desc->GetName().c_str(), switchn_desc->GetType().c_str(),
  1320. "data");
  1321. GELOGE(OUT_OF_MEMORY, "Failed to add input tensor desc for %s", switchn_desc->GetName().c_str());
  1322. return OUT_OF_MEMORY);
  1323. GeTensorDesc pred_tensor;
  1324. GE_IF_BOOL_EXEC(switchn_desc->AddInputDesc("pred_value", pred_tensor) != GRAPH_SUCCESS,
  1325. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed, input desc name:%s",
  1326. switchn_desc->GetName().c_str(), switchn_desc->GetType().c_str(),
  1327. "pred_value");
  1328. GELOGE(OUT_OF_MEMORY, "Failed to add input pred tensor desc for %s", switchn_desc->GetName().c_str());
  1329. return OUT_OF_MEMORY);
  1330. std::vector<std::string> input_dims_str;
  1331. for (size_t i = 0; i < shapes_.size(); ++i) {
  1332. GELOGI("Start clac shape for data %s, batch shape is %s.", data_name.c_str(),
  1333. formats::JoinToString(data_to_dynamic_info_.at(data_name).at(i)).c_str());
  1334. auto shape = data_shape;
  1335. auto ret = CalcShape(data_to_dynamic_info_.at(data_name).at(i), shape);
  1336. if (ret != SUCCESS) {
  1337. GELOGE(ret, "Failed to calculate the batched shape for data node %s, the shapes may not match",
  1338. node->GetName().c_str());
  1339. return ret;
  1340. }
  1341. tensor.SetShape(shape);
  1342. string input_str;
  1343. int64_t tensor_size = 0;
  1344. (void)TensorUtils::GetTensorSizeInBytes(tensor, tensor_size);
  1345. input_str = TypeUtils::FormatToSerialString(tensor.GetFormat()) + ":" +
  1346. TypeUtils::DataTypeToSerialString(tensor.GetDataType()) + ":" + node->GetName() + ":" +
  1347. std::to_string(tensor_size) + ":" + std::to_string(tensor.GetShape().GetDimNum()) + ":" +
  1348. formats::JoinToString(tensor.GetShape().GetDims());
  1349. input_dims_str.emplace_back(input_str);
  1350. if (!AttrUtils::SetListInt(tensor, ATTR_NAME_SWITCHN_PRED_VALUE, shapes_.at(i))) {
  1351. REPORT_CALL_ERROR("E19999", "Set Attr:%s to output tensor of node:%s(%s) failed, index:%zu",
  1352. ATTR_NAME_SWITCHN_PRED_VALUE.c_str(),
  1353. node->GetName().c_str(), node->GetType().c_str(), out_anchor_index);
  1354. GELOGE(INTERNAL_ERROR, "Failed to add attr value on output %zu tensor", i);
  1355. return INTERNAL_ERROR;
  1356. }
  1357. (void) AttrUtils::SetListInt(tensor, ATTR_NAME_COMBINED_DYNAMIC_DIMS, shape.GetDims());
  1358. if (switchn_desc->AddOutputDesc("output" + std::to_string(i), tensor) != GRAPH_SUCCESS) {
  1359. REPORT_CALL_ERROR("E19999", "Add output desc to op:%s(%s) failed, output desc name:%s",
  1360. switchn_desc->GetName().c_str(), switchn_desc->GetType().c_str(),
  1361. ("output" + std::to_string(i)).c_str());
  1362. GELOGE(GRAPH_FAILED, "Opdesc AddOutputDesc failed");
  1363. return GRAPH_FAILED;
  1364. }
  1365. GELOGD("The switchn %s output index %zu, shape %s", switchn_desc->GetName().c_str(), i, shape.ToString().c_str());
  1366. }
  1367. (void)AttrUtils::SetListStr(node->GetOpDesc(), "_all_origin_gears_inputs", input_dims_str);
  1368. if (!AttrUtils::SetListStr(switchn_desc, ATTR_USER_DESIGNEATE_SHAPE_ORDER, data_name_order_)) {
  1369. REPORT_CALL_ERROR("E19999", "Set Attr:%s to node:%s(%s) failed",
  1370. ATTR_USER_DESIGNEATE_SHAPE_ORDER.c_str(),
  1371. switchn_desc->GetName().c_str(), switchn_desc->GetType().c_str());
  1372. GELOGE(INTERNAL_ERROR, "Failed to add user designate shape order attr on switchn node %s",
  1373. switchn_desc->GetName().c_str());
  1374. return INTERNAL_ERROR;
  1375. }
  1376. if (!AttrUtils::SetBool(switchn_desc, ATTR_INSERT_BY_MBATCH, true)) {
  1377. REPORT_CALL_ERROR("E19999", "Set Attr:%s to node:%s(%s) failed",
  1378. ATTR_INSERT_BY_MBATCH.c_str(), switchn_desc->GetName().c_str(), switchn_desc->GetType().c_str());
  1379. GELOGE(INTERNAL_ERROR, "Failed to add insert attr on switchn node %s", switchn_desc->GetName().c_str());
  1380. return INTERNAL_ERROR;
  1381. }
  1382. if (!AttrUtils::SetStr(node->GetOpDesc(), kMbatchSwitchnName, switchn_desc->GetName())) {
  1383. REPORT_CALL_ERROR("E19999", "Set Attr:%s to node:%s(%s) failed",
  1384. kMbatchSwitchnName, node->GetName().c_str(), node->GetType().c_str());
  1385. GELOGE(INTERNAL_ERROR, "Failed to add switchn attr on data node %s", node->GetName().c_str());
  1386. return INTERNAL_ERROR;
  1387. }
  1388. if (StampDynamicType(switchn_desc) != SUCCESS) {
  1389. GELOGE(INTERNAL_ERROR, "Failed to add dynamic type attr on switchn node %s", switchn_desc->GetName().c_str());
  1390. return INTERNAL_ERROR;
  1391. }
  1392. auto switchn = graph_->AddNode(switchn_desc);
  1393. GE_IF_BOOL_EXEC(switchn == nullptr,
  1394. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  1395. switchn_desc->GetName().c_str(), switchn_desc->GetType().c_str(),
  1396. graph_->GetName().c_str());
  1397. GELOGE(OUT_OF_MEMORY, "Failed to create switchn %s from desc", switchn_desc->GetName().c_str());
  1398. return OUT_OF_MEMORY);
  1399. if (!getnext_sink_dynamic_dims_) {
  1400. data_nodes_to_switchn_[node.get()] = switchn;
  1401. } else {
  1402. dynamic_out_to_switchn.emplace_back(std::make_pair(node.get(), switchn));
  1403. GELOGD("Insert %s for %s.", switchn->GetName().c_str(), node->GetName().c_str());
  1404. }
  1405. return SUCCESS;
  1406. }
  1407. Status MultiBatchGraphCopyer::InsertMergeForEdgeNode(const NodePtr &node) {
  1408. for (auto &in_data_anchor : node->GetAllInDataAnchors()) {
  1409. auto src_out_anchor = in_data_anchor->GetPeerOutAnchor();
  1410. if (src_out_anchor == nullptr) {
  1411. GELOGD("The node %s does not has input at index %d", node->GetName().c_str(), in_data_anchor->GetIdx());
  1412. continue;
  1413. }
  1414. auto in_node = src_out_anchor->GetOwnerNode();
  1415. if (!IsInBatchBranch(in_node)) {
  1416. continue;
  1417. }
  1418. auto merge_node = InsertMergeNode(in_node, src_out_anchor->GetIdx());
  1419. if (merge_node == nullptr) {
  1420. return INTERNAL_ERROR;
  1421. }
  1422. }
  1423. for (auto &in_node : node->GetInControlNodes()) {
  1424. if (!IsInBatchBranch(in_node)) {
  1425. continue;
  1426. }
  1427. auto merge_node = InsertMergeNode(in_node, -1);
  1428. if (merge_node == nullptr) {
  1429. return INTERNAL_ERROR;
  1430. }
  1431. }
  1432. return SUCCESS;
  1433. }
  1434. Status MultiBatchGraphCopyer::LinkGetDynamicDimsToNetOutput(const NodePtr &node) {
  1435. if (node->GetType() == NETOUTPUT) {
  1436. if (!GetLocalOmgContext().dynamic_node_type.empty()) {
  1437. if (!AttrUtils::SetStr(node->GetOpDesc(), ATTR_ALL_GEARS_INFO, GetLocalOmgContext().dynamic_dims)) {
  1438. REPORT_CALL_ERROR("E19999", "Set Attr:%s to node:%s(%s) failed",
  1439. ATTR_ALL_GEARS_INFO.c_str(), node->GetName().c_str(), node->GetType().c_str());
  1440. GELOGE(INTERNAL_ERROR, "Failed to set all gears info attr on netoutput %s.", node->GetName().c_str());
  1441. return INTERNAL_ERROR;
  1442. }
  1443. }
  1444. if (getnext_sink_dynamic_dims_) {
  1445. size_t input_index = node->GetAllInDataAnchors().size();
  1446. if (NodeUtils::AppendInputAnchor(node, input_index + 1) != GRAPH_SUCCESS) {
  1447. REPORT_CALL_ERROR("E19999", "Append %zu input anchors to node:%s(%s) failed",
  1448. input_index + 1, node->GetName().c_str(), node->GetType().c_str());
  1449. GELOGE(INTERNAL_ERROR, "Append input anchor of %s of %zu failed.", node->GetName().c_str(), input_index);
  1450. return INTERNAL_ERROR;
  1451. }
  1452. auto ret =
  1453. ge::GraphUtils::AddEdge(shape_data_->GetOutDataAnchor(kDataOutIndex), node->GetInDataAnchor(input_index));
  1454. GE_IF_BOOL_EXEC(
  1455. ret != GRAPH_SUCCESS,
  1456. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%zu) failed",
  1457. shape_data_->GetName().c_str(), shape_data_->GetType().c_str(), kDataOutIndex,
  1458. node->GetName().c_str(), node->GetType().c_str(), input_index);
  1459. GELOGE(INTERNAL_ERROR, "Failed to link netoutput %s to getdynamicdims %s",
  1460. node->GetName().c_str(), shape_data_->GetName().c_str());
  1461. return INTERNAL_ERROR);
  1462. if (!AttrUtils::SetBool(node->GetOpDesc(), ATTR_GETNEXT_SINK_DYNMAIC, true)) {
  1463. REPORT_CALL_ERROR("E19999", "Set Attr:%s to node:%s(%s) failed",
  1464. ATTR_GETNEXT_SINK_DYNMAIC.c_str(), node->GetName().c_str(), node->GetType().c_str());
  1465. GELOGE(INTERNAL_ERROR, "Failed to set getnext sink dynamic attr on netoutput %s.", node->GetName().c_str());
  1466. return INTERNAL_ERROR;
  1467. }
  1468. }
  1469. }
  1470. return SUCCESS;
  1471. }
  1472. Status MultiBatchGraphCopyer::CopyNodeInBatchBranch(const NodePtr &node) {
  1473. auto &copyed_nodes = nodes_to_batch_nodes_[node.get()];
  1474. for (size_t i = 0; i < shapes_.size(); ++i) {
  1475. auto copyed_node = InsertCopyNode(node, i);
  1476. if (copyed_node == nullptr) {
  1477. GELOGE(INTERNAL_ERROR, "Failed to add node to graph when copy node %s", node->GetName().c_str());
  1478. return INTERNAL_ERROR;
  1479. }
  1480. copyed_nodes.emplace_back(copyed_node);
  1481. GELOGI("Copy node %s type %s for shape %s, new node name %s", node->GetName().c_str(), node->GetType().c_str(),
  1482. formats::JoinToString(shapes_.at(i)).c_str(), copyed_node->GetName().c_str());
  1483. }
  1484. return SUCCESS;
  1485. }
  1486. Status MultiBatchGraphCopyer::AddAttrForGetDynamicDims(const NodePtr &node) {
  1487. GELOGD("Add attr for :%s, type is %s:", shape_data_->GetName().c_str(), shape_data_->GetType().c_str());
  1488. size_t data_count = node->GetAllOutDataAnchors().size() / kDivisionConst;
  1489. if (!AttrUtils::SetInt(shape_data_->GetOpDesc(), ATTR_GETNEXT_SINK_DATA_COUNT, data_count)) {
  1490. REPORT_CALL_ERROR("E19999", "Set Attr:%s to node:%s(%s) failed",
  1491. ATTR_GETNEXT_SINK_DATA_COUNT.c_str(),
  1492. shape_data_->GetName().c_str(), shape_data_->GetType().c_str());
  1493. GELOGE(INTERNAL_ERROR, "set ATTR_GETNEXT_SINK_DATA_COUNT failed");
  1494. return INTERNAL_ERROR;
  1495. }
  1496. vector<int64_t> shape_info;
  1497. for (size_t i = 0; i < GetLocalOmgContext().user_input_dims.size(); ++i) {
  1498. if (GetLocalOmgContext().user_input_dims.at(i).second.size() == 1 &&
  1499. GetLocalOmgContext().user_input_dims.at(i).second.at(0) == 0) {
  1500. shape_info.emplace_back(0);
  1501. continue;
  1502. }
  1503. shape_info.emplace_back(GetLocalOmgContext().user_input_dims.at(i).second.size());
  1504. for (size_t j = 0; j < GetLocalOmgContext().user_input_dims.at(i).second.size(); ++j) {
  1505. shape_info.emplace_back(GetLocalOmgContext().user_input_dims.at(i).second.at(j));
  1506. }
  1507. }
  1508. if (!AttrUtils::SetListInt(shape_data_->GetOpDesc(), ATTR_GETNEXT_SINK_SHAPE_INFO, shape_info)) {
  1509. REPORT_CALL_ERROR("E19999", "Set Attr:%s to node:%s(%s) failed",
  1510. ATTR_GETNEXT_SINK_SHAPE_INFO.c_str(),
  1511. shape_data_->GetName().c_str(), shape_data_->GetType().c_str());
  1512. GELOGE(INTERNAL_ERROR, "set ATTR_GETNEXT_SINK_SHAPE_INFO failed");
  1513. return INTERNAL_ERROR;
  1514. }
  1515. return SUCCESS;
  1516. }
  1517. Status MultiBatchGraphCopyer::AddLinkForGetDynamicDims(const NodePtr &node) {
  1518. GELOGD("Start relink out anchor from shape node to getdynamicdims, and delete link between shape node and identity.");
  1519. size_t input_index = 0;
  1520. GELOGD("Out count of %s is %zu.", node->GetName().c_str(), node->GetAllOutDataAnchors().size());
  1521. size_t data_count = node->GetAllOutDataAnchors().size() / kDivisionConst;
  1522. for (size_t out_index = data_count; out_index < node->GetAllOutDataAnchors().size(); ++out_index, ++input_index) {
  1523. GELOGI("Start add %s of %zu out_anchor to %s of %zu in_anchor.", node->GetName().c_str(), out_index,
  1524. shape_data_->GetName().c_str(), input_index);
  1525. auto out_data_anchor = node->GetOutDataAnchor(out_index);
  1526. auto ret = GraphUtils::AddEdge(out_data_anchor, shape_data_->GetInDataAnchor(input_index));
  1527. GE_IF_BOOL_EXEC(
  1528. ret != GRAPH_SUCCESS,
  1529. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%zu) and op:%s(%s)(index:%zu) failed",
  1530. node->GetName().c_str(), node->GetType().c_str(), out_index,
  1531. shape_data_->GetName().c_str(), shape_data_->GetType().c_str(), input_index);
  1532. GELOGE(INTERNAL_ERROR, "Failed to link getnext %s to getdynamicdims %s",
  1533. node->GetName().c_str(), shape_data_->GetName().c_str());
  1534. return INTERNAL_ERROR);
  1535. }
  1536. return SUCCESS;
  1537. }
  1538. Status MultiBatchGraphCopyer::LinkEdges() {
  1539. Status ret;
  1540. for (const auto &node : origin_all_nodes_) {
  1541. GE_CHECK_NOTNULL(node->GetOpDesc());
  1542. if (!getnext_sink_dynamic_dims_) {
  1543. if (data_nodes_to_switchn_.count(node.get()) > 0) {
  1544. auto switchn = data_nodes_to_switchn_[node.get()];
  1545. GE_IF_BOOL_EXEC(switchn == nullptr,
  1546. REPORT_INNER_ERROR("E19999",
  1547. "swithn in data_nodes_to_switchn_ for op:%s(%s) is nullptr, check invalid",
  1548. node->GetName().c_str(), node->GetType().c_str());
  1549. GELOGE(PARAM_INVALID, "Switchn should not be nullptr for %s.", node->GetName().c_str());
  1550. return OUT_OF_MEMORY);
  1551. ret = LinkDataToSwitchN(node, switchn, kDataOutIndex);
  1552. GE_CHK_STATUS_RET(ret, "Link data to switchn failed.");
  1553. }
  1554. } else {
  1555. if (IsGetNextType(node)) {
  1556. GELOGD("Start add attr and link edge for %s.", node->GetName().c_str());
  1557. GE_CHK_STATUS_RET(AddAttrForGetDynamicDims(node), "Failed to add attr for %s.", node->GetName().c_str());
  1558. GE_CHK_STATUS_RET(AddLinkForGetDynamicDims(node), "Failed to add link for %s.", node->GetName().c_str());
  1559. }
  1560. for (size_t i = 0; i < getnext_nodes_to_switchn_.size(); ++i) {
  1561. for (size_t j = 0; j < getnext_nodes_to_switchn_.at(i).size(); ++j) {
  1562. if (getnext_nodes_to_switchn_.at(i).at(j).first == node.get()) {
  1563. auto switchn = getnext_nodes_to_switchn_.at(i).at(j).second;
  1564. GE_CHK_STATUS_RET(LinkDataToSwitchN(node, switchn, i), "Link %s to %s failed.", node->GetName().c_str(),
  1565. switchn->GetName().c_str());
  1566. }
  1567. }
  1568. }
  1569. }
  1570. if (nodes_to_merge_nodes_.count(node.get()) > 0) {
  1571. GE_CHK_STATUS_RET(LinkToMerge(node), "Link %s to merge failed.", node->GetName().c_str());
  1572. }
  1573. if (nodes_to_batch_nodes_.count(node.get()) > 0) {
  1574. ret = LinkToNodeInBranch(node);
  1575. } else {
  1576. ret = LinkToNodeOutBranch(node);
  1577. }
  1578. if (ret != SUCCESS) {
  1579. return ret;
  1580. }
  1581. }
  1582. return SUCCESS;
  1583. }
  1584. Status MultiBatchGraphCopyer::LinkDataToSwitchN(const NodePtr &data, const NodePtr &switchn, const int &out_index) {
  1585. auto ret =
  1586. GraphUtils::AddEdge(shape_data_->GetOutDataAnchor(kDataOutIndex), switchn->GetInDataAnchor(kSwitchNPredIndex));
  1587. GE_IF_BOOL_EXEC(
  1588. ret != GRAPH_SUCCESS,
  1589. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  1590. shape_data_->GetName().c_str(), shape_data_->GetType().c_str(), kDataOutIndex,
  1591. switchn->GetName().c_str(), switchn->GetType().c_str(), kSwitchNPredIndex);
  1592. GELOGE(INTERNAL_ERROR, "Failed to link shape data %s to switchn %s",
  1593. shape_data_->GetName().c_str(), switchn->GetName().c_str());
  1594. return INTERNAL_ERROR);
  1595. ret = GraphUtils::AddEdge(data->GetOutDataAnchor(out_index), switchn->GetInDataAnchor(kSwitchNDataIndex));
  1596. GE_IF_BOOL_EXEC(
  1597. ret != GRAPH_SUCCESS,
  1598. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  1599. data->GetName().c_str(), data->GetType().c_str(), out_index,
  1600. switchn->GetName().c_str(), switchn->GetType().c_str(), kSwitchNDataIndex);
  1601. GELOGE(INTERNAL_ERROR, "Failed to link data %s to switchn %s",
  1602. data->GetName().c_str(), switchn->GetName().c_str());
  1603. return INTERNAL_ERROR);
  1604. return SUCCESS;
  1605. }
  1606. Status MultiBatchGraphCopyer::LinkToMerge(const NodePtr &node) {
  1607. auto &merge_nodes = nodes_to_merge_nodes_[node.get()];
  1608. for (size_t i = 0; i < merge_nodes.size(); ++i) {
  1609. auto merge_node = merge_nodes[i];
  1610. if (merge_node == nullptr) {
  1611. continue;
  1612. }
  1613. if (nodes_to_batch_nodes_.count(node.get()) > 0) {
  1614. auto ret = LinkNodeToMerge(node, i, merge_node);
  1615. if (ret != SUCCESS) {
  1616. return ret;
  1617. }
  1618. continue;
  1619. }
  1620. if (!getnext_sink_dynamic_dims_) {
  1621. if (data_nodes_to_switchn_.count(node.get()) > 0) {
  1622. auto &switchn = data_nodes_to_switchn_[node.get()];
  1623. auto ret = LinkDataToMerge(node, merge_node, switchn);
  1624. if (ret != SUCCESS) {
  1625. return ret;
  1626. }
  1627. continue;
  1628. }
  1629. } else {
  1630. for (size_t j = 0; j < getnext_nodes_to_switchn_.size(); ++j) {
  1631. for (size_t k = 0; k < getnext_nodes_to_switchn_.at(j).size(); ++k) {
  1632. if (getnext_nodes_to_switchn_.at(j).at(k).first == node.get()) {
  1633. auto &switchn = getnext_nodes_to_switchn_.at(j).at(k).second;
  1634. auto ret = LinkDataToMerge(node, merge_node, switchn);
  1635. if (ret != SUCCESS) {
  1636. return ret;
  1637. }
  1638. }
  1639. }
  1640. }
  1641. continue;
  1642. }
  1643. REPORT_INNER_ERROR("E19999", "The merge node %s is created, index %zu, but can not find the src node, "
  1644. "check invalid", merge_node->GetName().c_str(), i);
  1645. GELOGE(INTERNAL_ERROR, "The merge node %s is created, index %zu, but can not find the src node",
  1646. merge_node->GetName().c_str(), i);
  1647. return INTERNAL_ERROR;
  1648. }
  1649. return SUCCESS;
  1650. }
  1651. Status MultiBatchGraphCopyer::LinkToNodeInBranch(const NodePtr &node) {
  1652. GELOGI("Start LinkToNodeInBranch for %s.", node->GetName().c_str());
  1653. auto &branch_nodes = nodes_to_batch_nodes_[node.get()];
  1654. for (size_t i = 0; i < branch_nodes.size(); ++i) {
  1655. auto ret = CopyInDataEdges(node, i, branch_nodes[i]);
  1656. if (ret != SUCCESS) {
  1657. return ret;
  1658. }
  1659. ret = CopyInControlEdges(node, i, branch_nodes[i]);
  1660. if (ret != SUCCESS) {
  1661. return ret;
  1662. }
  1663. }
  1664. return SUCCESS;
  1665. }
  1666. Status MultiBatchGraphCopyer::LinkToNodeOutBranch(const NodePtr &node) {
  1667. for (auto &in_data_anchor : node->GetAllInDataAnchors()) {
  1668. auto src_out_anchor = in_data_anchor->GetPeerOutAnchor();
  1669. if (src_out_anchor == nullptr) {
  1670. GELOGD("The node %s does not has input at index %d", node->GetName().c_str(), in_data_anchor->GetIdx());
  1671. continue;
  1672. }
  1673. auto in_node = src_out_anchor->GetOwnerNode();
  1674. if (!IsInBatchBranch(in_node)) {
  1675. continue;
  1676. }
  1677. auto iter = nodes_to_merge_nodes_.find(in_node.get());
  1678. if (iter == nodes_to_merge_nodes_.end()) {
  1679. REPORT_INNER_ERROR("E19999", "Failed to link data edge from %s(%s)(index:%d) to %s(%s)(index:%d), "
  1680. "cause no merge node found, check invalid",
  1681. in_node->GetName().c_str(), in_node->GetType().c_str(), src_out_anchor->GetIdx(),
  1682. node->GetName().c_str(), node->GetType().c_str(), in_data_anchor->GetIdx());
  1683. GELOGE(INTERNAL_ERROR, "Failed to link IO data edge from %s(%d) to %s(%d), no merge node found",
  1684. in_node->GetName().c_str(), src_out_anchor->GetIdx(), node->GetName().c_str(), in_data_anchor->GetIdx());
  1685. return INTERNAL_ERROR;
  1686. }
  1687. auto merge_node = iter->second[src_out_anchor->GetIdx()];
  1688. if (merge_node == nullptr) {
  1689. REPORT_INNER_ERROR("E19999", "Failed to link data edge from %s(%s)(index:%d) to %s(%s)(index:%d), "
  1690. "cause no merge node found, check invalid",
  1691. in_node->GetName().c_str(), in_node->GetType().c_str(), src_out_anchor->GetIdx(),
  1692. node->GetName().c_str(), node->GetType().c_str(), in_data_anchor->GetIdx());
  1693. GELOGE(INTERNAL_ERROR, "Failed to link IO data edge from %s(%d) to %s(%d), no merge node found",
  1694. in_node->GetName().c_str(), src_out_anchor->GetIdx(), node->GetName().c_str(), in_data_anchor->GetIdx());
  1695. return INTERNAL_ERROR;
  1696. }
  1697. auto ret = src_out_anchor->Unlink(in_data_anchor);
  1698. if (ret != GRAPH_SUCCESS) {
  1699. REPORT_INNER_ERROR("E19999", "Unlink edge from %s(%s)(index:%d) to %s(%s)(index:%d) failed",
  1700. in_node->GetName().c_str(), in_node->GetType().c_str(), src_out_anchor->GetIdx(),
  1701. node->GetName().c_str(), node->GetType().c_str(), in_data_anchor->GetIdx());
  1702. GELOGE(INTERNAL_ERROR, "Failed to unlink the control edge from %s(%d) to %s(%d)", in_node->GetName().c_str(),
  1703. src_out_anchor->GetIdx(), node->GetName().c_str(), in_data_anchor->GetIdx());
  1704. return INTERNAL_ERROR;
  1705. }
  1706. ret = GraphUtils::AddEdge(merge_node->GetOutDataAnchor(kMergeDataOutIndex), in_data_anchor);
  1707. if (ret != GRAPH_SUCCESS) {
  1708. REPORT_CALL_ERROR("E19999", "Add edge between op:%s(%s)(index:%d) and op:%s(%s)(index:%d) failed",
  1709. merge_node->GetName().c_str(), merge_node->GetType().c_str(), kMergeDataOutIndex,
  1710. node->GetName().c_str(), node->GetType().c_str(), in_data_anchor->GetIdx());
  1711. GELOGE(INTERNAL_ERROR, "Failed to add data edge from %s(%d) to %s(%d)", merge_node->GetName().c_str(),
  1712. src_out_anchor->GetIdx(), node->GetName().c_str(), in_data_anchor->GetIdx());
  1713. return INTERNAL_ERROR;
  1714. }
  1715. GELOGI("Link data edge from merge %s(from %s(%d)) to %s(%d)", merge_node->GetName().c_str(),
  1716. in_node->GetName().c_str(), src_out_anchor->GetIdx(), node->GetName().c_str(), in_data_anchor->GetIdx());
  1717. }
  1718. for (auto &in_node : node->GetInControlNodes()) {
  1719. if (!IsInBatchBranch(in_node)) {
  1720. continue;
  1721. }
  1722. auto iter = nodes_to_merge_nodes_.find(in_node.get());
  1723. if (iter == nodes_to_merge_nodes_.end()) {
  1724. REPORT_INNER_ERROR("E19999", "Failed to link IO control edge from %s(%s) to %s(%s), no merge node found,"
  1725. "check invalid",
  1726. in_node->GetName().c_str(), in_node->GetType().c_str(),
  1727. node->GetName().c_str(), node->GetType().c_str());
  1728. GELOGE(INTERNAL_ERROR, "Failed to link IO control edge from %s to %s, no merge node found",
  1729. in_node->GetName().c_str(), node->GetName().c_str());
  1730. return INTERNAL_ERROR;
  1731. }
  1732. auto merge_node = iter->second[0];
  1733. if (merge_node == nullptr) {
  1734. REPORT_INNER_ERROR("E19999",
  1735. "Failed to link IO control edge from %s(%s) to %s(%s), no merge node found, check invalid",
  1736. in_node->GetName().c_str(), in_node->GetType().c_str(),
  1737. node->GetName().c_str(), node->GetType().c_str());
  1738. GELOGE(INTERNAL_ERROR, "Failed to link IO control edge from %s to %s, no merge node found",
  1739. in_node->GetName().c_str(), node->GetName().c_str());
  1740. return INTERNAL_ERROR;
  1741. }
  1742. GE_IF_BOOL_EXEC(in_node->GetOutControlAnchor() == nullptr,
  1743. REPORT_INNER_ERROR("E19999", "Out control anchor of op:%s(%s) is nullptr, check invalid",
  1744. in_node->GetName().c_str(), in_node->GetType().c_str());
  1745. GELOGE(INTERNAL_ERROR, "Innode outputControlAnchor is null");
  1746. return INTERNAL_ERROR);
  1747. auto ret = in_node->GetOutControlAnchor()->Unlink(node->GetInControlAnchor());
  1748. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS,
  1749. REPORT_INNER_ERROR("E19999", "Unlink ctrl edge from %s(%s) to %s(%s) failed",
  1750. in_node->GetName().c_str(), in_node->GetType().c_str(),
  1751. node->GetName().c_str(), node->GetType().c_str());
  1752. GELOGE(INTERNAL_ERROR, "Failed to unlink the control edge from %s to %s",
  1753. in_node->GetName().c_str(), node->GetName().c_str());
  1754. return INTERNAL_ERROR);
  1755. ret = GraphUtils::AddEdge(merge_node->GetOutControlAnchor(), node->GetInControlAnchor());
  1756. GE_IF_BOOL_EXEC(
  1757. ret != GRAPH_SUCCESS,
  1758. REPORT_CALL_ERROR("E19999", "Add ctrl edge between op:%s(%s) and op:%s(%s) failed",
  1759. merge_node->GetName().c_str(), merge_node->GetType().c_str(),
  1760. node->GetName().c_str(), node->GetType().c_str());
  1761. GELOGE(INTERNAL_ERROR, "Failed to add control edge from %s to %s",
  1762. merge_node->GetName().c_str(), node->GetName().c_str());
  1763. return INTERNAL_ERROR);
  1764. GELOGI("Link control edge from merge %s(from %s) to %s", merge_node->GetName().c_str(), in_node->GetName().c_str(),
  1765. node->GetName().c_str());
  1766. }
  1767. return SUCCESS;
  1768. }
  1769. Status ProcessMultiBatch(ComputeGraphPtr &graph) {
  1770. const char *multi_batch_with_switchn = std::getenv("MULTI_BATCH_WITH_SWITCHN");
  1771. if (multi_batch_with_switchn == nullptr) {
  1772. PassManager pass_manager;
  1773. GE_CHK_STATUS_RET(pass_manager.AddPass("MultiBatchClonePass", new (std::nothrow) MultiBatchClonePass));
  1774. return pass_manager.Run(graph);
  1775. }
  1776. if (!GetLocalOmgContext().need_multi_batch) {
  1777. GELOGI("No need to process_multi for no_train graph.");
  1778. return SUCCESS;
  1779. }
  1780. std::vector<NodePtr> data_nodes;
  1781. std::vector<NodePtr> getnext_nosink_nodes;
  1782. std::vector<NodePtr> getnext_sink_nodes;
  1783. if (CheckSequenceOfOptions(graph, data_nodes, getnext_nosink_nodes, getnext_sink_nodes) != SUCCESS) {
  1784. GELOGE(PARAM_INVALID, "[Train_Dynamic] CheckSequenceOfOptions failed.");
  1785. return PARAM_INVALID;
  1786. }
  1787. if (UpdateNameOfInputShape(graph, data_nodes, getnext_nosink_nodes, getnext_sink_nodes) != SUCCESS) {
  1788. GELOGE(PARAM_INVALID, "[Train_Dynamic] UpdateNameForInputShapeOfOption failed.");
  1789. return PARAM_INVALID;
  1790. }
  1791. if (DeleteIdentityInsertByAdapter(graph) != SUCCESS) {
  1792. GELOGE(PARAM_INVALID, "DeleteIdentityInsertByAdapter failed.");
  1793. return PARAM_INVALID;
  1794. }
  1795. std::vector<std::vector<int64_t>> shapes;
  1796. if (!InitDynamicParams(shapes)) {
  1797. GELOGD("There is no multi-batch options, no need to process multi-batch copy");
  1798. return SUCCESS;
  1799. }
  1800. if (CheckNegativeCountOfOptions(shapes) != SUCCESS) {
  1801. GELOGE(PARAM_INVALID, "Input_shape and dynamic_dims should set correct params.");
  1802. return PARAM_INVALID;
  1803. }
  1804. DynamicType dynamic_type = DynamicType::kDynamicUnknown;
  1805. if (!GetLocalOmgContext().dynamic_batch_size.empty()) {
  1806. dynamic_type = DynamicType::kDynamicBatch;
  1807. } else if (!GetLocalOmgContext().dynamic_image_size.empty()) {
  1808. dynamic_type = DynamicType::kDynamicImageSize;
  1809. } else if (!GetLocalOmgContext().dynamic_dims.empty()) {
  1810. dynamic_type = DynamicType::kDynamicDims;
  1811. }
  1812. std::vector<std::pair<std::string, std::vector<int64_t>>> user_designate_shape;
  1813. user_designate_shape = GetLocalOmgContext().user_input_dims;
  1814. GELOGI("Begin to copy graph for multi-batch");
  1815. multibatch::MultiBatchGraphCopyer copyer(graph);
  1816. for (auto &shape : shapes) {
  1817. copyer.AddShape(shape);
  1818. }
  1819. copyer.SetDynamicType(dynamic_type);
  1820. copyer.SetUserDesignateShape(user_designate_shape);
  1821. return copyer.CopyGraph();
  1822. }
  1823. // +-----------+
  1824. // | Data | +-----------+ +-----------+ +-----------+
  1825. // +-----------+ | Data | ----> | SoftmaxV2 | ----> | NetOutput |
  1826. // \ /. +-----------+ +-----------+ +-----------+
  1827. // \ /.
  1828. // +-----------+ +-----------+ /. +-----------+ +-----------+ +-----------+
  1829. // | Data | ----> | Case | S--- | Data | ----> | SoftmaxV2 | ----> | NetOutput |
  1830. // +-----------+ +-----------+ \. +-----------+ +-----------+ +-----------+
  1831. // \ \.
  1832. // \ \. +-----------+ +-----------+ +-----------+
  1833. // +-----------+ | Data | ----> | SoftmaxV2 | ----> | NetOutput |
  1834. // | NetOutput | +-----------+ +-----------+ +-----------+
  1835. // +-----------+
  1836. // +-----------+ /
  1837. // | Data | --------------->/
  1838. // +-----------+
  1839. void GetDynamicShapeByGraph(const ComputeGraphPtr &graph, const NodePtr &node,
  1840. set<size_t> &dynamic_output_index, vector<string> &dynamic_output_dims) {
  1841. GELOGD("Try get dynamic shape info, Graph: %s, Node: %s", graph->GetName().c_str(), node->GetName().c_str());
  1842. const auto &func_desc = node->GetOpDesc();
  1843. if (!func_desc->HasAttr(ATTR_NAME_BATCH_NUM)) {
  1844. GELOGD("Graph: %s Not multi-batch, Node: %s", graph->GetName().c_str(), node->GetName().c_str());
  1845. return;
  1846. }
  1847. const auto &dynamic_branch_names = func_desc->GetSubgraphInstanceNames();
  1848. for (size_t i = 0; i < func_desc->GetOutputsSize(); ++i) {
  1849. for (size_t j = 0; j < dynamic_branch_names.size(); ++j) {
  1850. const auto &subgraph = graph->GetSubgraph(dynamic_branch_names[j]);
  1851. if (subgraph == nullptr) {
  1852. REPORT_INNER_ERROR("E19999", "Get subgraph:%s from graph:%s failed",
  1853. dynamic_branch_names[j].c_str(), graph->GetName().c_str());
  1854. GELOGE(GE_GRAPH_EMPTY_SUBGRAPH, "Subgraph not found, name: %s", dynamic_branch_names[j].c_str());
  1855. dynamic_output_dims.clear();
  1856. return;
  1857. }
  1858. const auto &out_node = subgraph->FindFirstNodeMatchType(NETOUTPUT);
  1859. if (out_node == nullptr) {
  1860. REPORT_INNER_ERROR("E19999", "No netoutput node exist in subgraph:%s, check invalid",
  1861. subgraph->GetName().c_str());
  1862. GELOGE(GE_GRAPH_GRAPH_NODE_NULL, "NetOutput not found, name: %s", dynamic_branch_names[j].c_str());
  1863. dynamic_output_dims.clear();
  1864. return;
  1865. }
  1866. GELOGI("Find the subgraph Output node %s and the index is %zu", out_node->GetName().c_str(), i);
  1867. const auto &out_desc = out_node->GetOpDesc();
  1868. if (out_desc == nullptr || out_desc->GetInputsSize() <= i) {
  1869. REPORT_INNER_ERROR("E19999",
  1870. "op_desc of node in subgraph:%s is nullptr or input desc size:%zu <= %zu, check invalid",
  1871. subgraph->GetName().c_str(), out_desc->GetInputsSize(), i);
  1872. GELOGE(GE_GRAPH_GRAPH_NODE_NULL, "Get Input desc failed, name: %s, index: %zu", out_node->GetName().c_str(), i);
  1873. dynamic_output_dims.clear();
  1874. return;
  1875. }
  1876. const auto &input_tensor = out_desc->GetInputDesc(i);
  1877. const auto &shape_msg = input_tensor.GetShape().ToString();
  1878. string output_shape = std::to_string(j) + "," + std::to_string(i) + "," + shape_msg;
  1879. GELOGI("The shape msg in dynamic batch is %s", output_shape.c_str());
  1880. dynamic_output_dims.emplace_back(output_shape);
  1881. uint32_t parent_index = 0;
  1882. (void)AttrUtils::GetInt(input_tensor, ATTR_NAME_PARENT_NODE_INDEX, parent_index);
  1883. dynamic_output_index.insert(parent_index);
  1884. }
  1885. }
  1886. }
  1887. // +-----------+ +-----------+ i = 0
  1888. // +----> | SoftmaxV2 | ----> |MemcpyAsync| ----> \.
  1889. // / +-----------+ +-----------+ \.
  1890. // / \.
  1891. // +-----------+ +-----------+ +-----------+ +-----------+ i = 1 +-----------+
  1892. // | Data | ----> | SwitchN | ----> | SoftmaxV2 | ----> |MemcpyAsync| ----> | Merge |
  1893. // +-----------+ +-----------+ +-----------+ +-----------+ +-----------+
  1894. // \ / \. j = 0
  1895. // \ +-----------+ +-----------+ i = 2 / \.
  1896. // +----> | SoftmaxV2 | ----> |MemcpyAsync| ----> / +-----------+
  1897. // +-----------+ +-----------+ | NetOutput |
  1898. // +-----------+
  1899. // +-----------+ /.
  1900. // | Data | --------------------------------------------------------------------------->/. j = 1
  1901. // +-----------+
  1902. void GetDynamicShapeByMerge(const ComputeGraphPtr &graph, const NodePtr &node,
  1903. set<size_t> &dynamic_output_index, vector<string> &dynamic_output_dims) {
  1904. GELOGD("Try get dynamic shape info, Graph: %s, Node: %s", graph->GetName().c_str(), node->GetName().c_str());
  1905. const auto &netoutput_desc = node->GetOpDesc();
  1906. const auto &inputnode_to_netoutput = node->GetInAllNodes();
  1907. GELOGI("Train_Dynamic Find the merge node size is %zu.", inputnode_to_netoutput.size());
  1908. for (size_t i = 0; i < inputnode_to_netoutput.size(); ++i) {
  1909. bool insert_by_mbatch = false;
  1910. (void)AttrUtils::GetBool(inputnode_to_netoutput.at(i)->GetOpDesc(), ATTR_INSERT_BY_MBATCH, insert_by_mbatch);
  1911. GELOGI("Train_Dynamic type is %s", inputnode_to_netoutput.at(i)->GetType().c_str());
  1912. if (inputnode_to_netoutput.at(i)->GetType() == MERGE && insert_by_mbatch) {
  1913. GELOGI("Find the merge node %s with mbatch attr and the index is %zu",
  1914. inputnode_to_netoutput.at(i)->GetName().c_str(), i);
  1915. dynamic_output_index.insert(i);
  1916. for (size_t j = 0; j < inputnode_to_netoutput.at(i)->GetInNodes().size(); ++j) {
  1917. auto input_desc = inputnode_to_netoutput.at(i)->GetOpDesc();
  1918. auto input_tensor_desc = input_desc->GetInputDesc(j);
  1919. auto shape_msg = input_tensor_desc.GetShape().ToString();
  1920. string output_shape = std::to_string(j) + "," + std::to_string(i) + "," + shape_msg;
  1921. GELOGI("The shape msg in dynamic batch is %s", output_shape.c_str());
  1922. dynamic_output_dims.emplace_back(output_shape);
  1923. }
  1924. }
  1925. }
  1926. }
  1927. // Connect NetOutput directly
  1928. void GetDirectOutputShape(const ComputeGraphPtr &graph, const NodePtr &node,
  1929. const set<size_t> &dynamic_output_index, vector<string> &dynamic_output_dims) {
  1930. if (!GetLocalOmgContext().dynamic_node_type.empty()) {
  1931. GELOGD("No need to get directly shape info of %s when train.", node->GetName().c_str());
  1932. return;
  1933. }
  1934. GELOGD("Try get directly shape info, Graph: %s, Node: %s", graph->GetName().c_str(), node->GetName().c_str());
  1935. const auto &netoutput_desc = node->GetOpDesc();
  1936. const auto &inputnode_to_netoutput = node->GetInAllNodes();
  1937. for (size_t i = 0; i < inputnode_to_netoutput.size(); ++i) {
  1938. if (dynamic_output_index.count(i) > 0) {
  1939. continue;
  1940. }
  1941. auto tensor_desc = netoutput_desc->GetInputDesc(i);
  1942. auto shape = tensor_desc.GetShape().ToString();
  1943. string static_output_shape = std::to_string(kStaticOutput) + "," + std::to_string(i) + "," + shape;
  1944. GELOGI("The static output shape msg is %s", static_output_shape.c_str());
  1945. dynamic_output_dims.emplace_back(static_output_shape);
  1946. }
  1947. }
  1948. Status GetDynamicOutputShape(ComputeGraphPtr &graph) {
  1949. GE_CHECK_NOTNULL(graph);
  1950. GELOGI("Start to get output dynamic batch shape message");
  1951. NodePtr net_output;
  1952. set<size_t> dynamic_output_index;
  1953. vector<string> dynamic_output_dims;
  1954. for (auto &node : graph->GetDirectNode()) {
  1955. if (node->GetType() == NETOUTPUT) {
  1956. net_output = node;
  1957. GetDynamicShapeByMerge(graph, node, dynamic_output_index, dynamic_output_dims);
  1958. } else if (node->GetType() == CASE) {
  1959. GetDynamicShapeByGraph(graph, node, dynamic_output_index, dynamic_output_dims);
  1960. }
  1961. }
  1962. if ((net_output != nullptr) && !dynamic_output_dims.empty()) {
  1963. GetDirectOutputShape(graph, net_output, dynamic_output_index, dynamic_output_dims);
  1964. if (!AttrUtils::SetListStr(net_output->GetOpDesc(), ATTR_NAME_DYNAMIC_OUTPUT_DIMS, dynamic_output_dims)) {
  1965. REPORT_CALL_ERROR("E19999", "Set Attr:%s to node:%s(%s) failed",
  1966. ATTR_NAME_DYNAMIC_OUTPUT_DIMS.c_str(),
  1967. net_output->GetName().c_str(), net_output->GetType().c_str());
  1968. GELOGE(FAILED, "Set dynamic output dims attr failed");
  1969. return FAILED;
  1970. }
  1971. }
  1972. return SUCCESS;
  1973. }
  1974. } // namespace multibatch
  1975. } // namespace ge

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