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 82 kB

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

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