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

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