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

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