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.

graph_partition.cc 64 kB

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
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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236
  1. /**
  2. * Copyright 2019-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/partition/graph_partition.h"
  17. #include <algorithm>
  18. #include <memory>
  19. #include <string>
  20. #include <unordered_set>
  21. #include <vector>
  22. #include "analyzer/analyzer.h"
  23. #include "common/ge/ge_util.h"
  24. #include "common/op/ge_op_utils.h"
  25. #include "framework/common/types.h"
  26. #include "graph/debug/ge_attr_define.h"
  27. #include "graph/manager/graph_manager_utils.h"
  28. #include "graph/common/ge_call_wrapper.h"
  29. #include "graph/utils/graph_utils.h"
  30. #include "graph/utils/op_desc_utils.h"
  31. #include "graph/utils/type_utils.h"
  32. #include "init/gelib.h"
  33. #include "opskernel_manager/ops_kernel_manager.h"
  34. namespace {
  35. const char *const kEngineDefaultData = "ENGINE_DEFAULT_DATA";
  36. const char *const kEndType = "End";
  37. const char *const kPlaceHolderType = "PlaceHolder";
  38. const int kOneGraph = 1; // only one graph
  39. const int kRankOne = 1; // order of graph list is 0,1,2,3..., 1 means second order
  40. const int kRankZero = 0; // order of graph list is 0,1,2,3..., 0 means first order
  41. } // namespace
  42. namespace ge {
  43. Status ge::GraphPartitioner::CheckIfEnd2PldEmpty(ge::ComputeGraphPtr &output_merged_compute_graph) {
  44. // only one condition:no data node, one engine, there is only one graph + input graph
  45. if (graph_info_.partitions_.size() == kOneGraph) {
  46. auto partition = (*graph_info_.partitions_.begin());
  47. if (partition.first == nullptr) {
  48. REPORT_INNER_ERROR("E19999", "partition.first is nullptr, check invalid, engine name is %s",
  49. partition.second.c_str());
  50. GELOGE(GE_GRAPH_EMPTY_PARTITION, "[Check][Param] partition.first is null, engine name is %s",
  51. partition.second.c_str());
  52. return FAILED;
  53. }
  54. output_merged_compute_graph = partition.first;
  55. } else { // if placeholder to end map is empty, it should be an exception condition
  56. REPORT_INNER_ERROR("E19999", "partitions size:%zu is not 1, check invalid.", graph_info_.partitions_.size());
  57. GELOGE(GE_GRAPH_EMPTY_PARTITION,
  58. "[Check][Param] placeholder to end map is empty, partitions size:%zu is not 1.",
  59. graph_info_.partitions_.size());
  60. return FAILED;
  61. }
  62. return SUCCESS;
  63. }
  64. Status ge::GraphPartitioner::MergeAllSubGraph(ge::ComputeGraphPtr &output_merged_compute_graph,
  65. const std::vector<SubGraphInfoPtr> &sub_graph_list) {
  66. for (size_t rank = 0; rank < graph_info_.rank_2_partitions_.size(); rank++) {
  67. string temp_stream;
  68. // sub_graph_list index is one ahead of rank_2_partitions_list index
  69. if (rank > 0) {
  70. temp_stream = sub_graph_list[rank - 1]->GetStreamLabel();
  71. }
  72. for (const auto &node : graph_info_.rank_2_partitions_[rank]->GetDirectNode()) {
  73. if (node == nullptr) {
  74. continue;
  75. }
  76. if ((node->GetType() == kEndType) || (node->GetType() == kPlaceHolderType)) {
  77. continue;
  78. }
  79. if (!temp_stream.empty() && !AttrUtils::HasAttr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL)) {
  80. (void)AttrUtils::SetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, temp_stream);
  81. }
  82. if (node->SetOwnerComputeGraph(output_merged_compute_graph) != GRAPH_SUCCESS) {
  83. REPORT_CALL_ERROR("E19999", "SetOwnerComputeGraph for node:%s failed.", node->GetName().c_str());
  84. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Set][OwnerComputeGraph] failed, node %s", node->GetName().c_str());
  85. return FAILED;
  86. }
  87. (void)output_merged_compute_graph->AddNode(node);
  88. }
  89. }
  90. // get session graph id from subgraph
  91. SetMergedGraphId(output_merged_compute_graph);
  92. return SUCCESS;
  93. }
  94. void ge::GraphPartitioner::SetMergedGraphId(ge::ComputeGraphPtr &output_merged_compute_graph) {
  95. string session_graph_id;
  96. // get session graph id from subgraph
  97. if (graph_info_.rank_2_partitions_.empty() ||
  98. !AttrUtils::GetStr(*(graph_info_.rank_2_partitions_[0]), ATTR_NAME_SESSION_GRAPH_ID, session_graph_id)) {
  99. GELOGW("Get graph session_graph_id attr failed.");
  100. }
  101. // set session graph id into merged subgraph
  102. if (!session_graph_id.empty()) {
  103. GELOGI("Set session graph id %s in merged compute graph", session_graph_id.c_str());
  104. // private function, promise output_merged_compute_graph not null
  105. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(*output_merged_compute_graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id),
  106. GELOGW("SetStr ATTR_NAME_SESSION_GRAPH_ID failed");)
  107. }
  108. }
  109. Status ge::GraphPartitioner::RemoveNodeAndEdgeBetweenEndPld(ge::ComputeGraphPtr &output_merged_compute_graph,
  110. const std::vector<SubGraphInfoPtr> &sub_graph_list) {
  111. if ((output_merged_compute_graph == nullptr) ||
  112. (MergeAllSubGraph(output_merged_compute_graph, sub_graph_list) != SUCCESS)) {
  113. REPORT_INNER_ERROR("E19999", "output_merged_compute_graph is nullptr or Call MergeAllSubGraph failed.");
  114. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Merge][AllSubGraph] failed.");
  115. return FAILED;
  116. }
  117. for (const auto &it : graph_info_.index_2_end_) {
  118. auto &end = it.second;
  119. auto &pld = graph_info_.end_2_pld_[it.second];
  120. if ((end != nullptr) && (pld != nullptr) && (end->GetInDataAnchor(0) != nullptr) &&
  121. (pld->GetOutDataAnchor(0) != nullptr)) {
  122. AnchorPtr end_in_anchor = (end->GetInDataAnchor(0)->GetFirstPeerAnchor() == nullptr)
  123. ? Anchor::DynamicAnchorCast<Anchor>(end->GetInControlAnchor())
  124. : Anchor::DynamicAnchorCast<Anchor>(end->GetInDataAnchor(0));
  125. AnchorPtr pld_out_anchor = (pld->GetOutDataAnchor(0)->GetFirstPeerAnchor() == nullptr)
  126. ? Anchor::DynamicAnchorCast<Anchor>(pld->GetOutControlAnchor())
  127. : Anchor::DynamicAnchorCast<Anchor>(pld->GetOutDataAnchor(0));
  128. auto src_anchor = end_in_anchor->GetFirstPeerAnchor(); // src_anchor should be only 1
  129. if (GraphUtils::RemoveEdge(src_anchor, end_in_anchor) != GRAPH_SUCCESS) {
  130. REPORT_CALL_ERROR("E19999", "RemoveEdge between %s and %s failed",
  131. src_anchor->GetOwnerNode()->GetName().c_str(),
  132. end_in_anchor->GetOwnerNode()->GetName().c_str());
  133. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Remove][Edge] between %s and %s failed. node_name:%s, graph_name:%s",
  134. src_anchor->GetOwnerNode()->GetName().c_str(), end_in_anchor->GetOwnerNode()->GetName().c_str(),
  135. end->GetName().c_str(), end->GetOwnerComputeGraph()->GetName().c_str());
  136. return FAILED;
  137. }
  138. GE_CHECK_NOTNULL(pld_out_anchor);
  139. for (const auto &peer_in_anchor : pld_out_anchor->GetPeerAnchors()) {
  140. if (GraphUtils::RemoveEdge(pld_out_anchor, peer_in_anchor) != GRAPH_SUCCESS) {
  141. REPORT_CALL_ERROR("E19999", "RemoveEdge between %s and %s failed",
  142. pld_out_anchor->GetOwnerNode()->GetName().c_str(),
  143. peer_in_anchor->GetOwnerNode()->GetName().c_str());
  144. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Remove][Edge] between %s and %s failed. node_name:%s, graph_name:%s",
  145. pld_out_anchor->GetOwnerNode()->GetName().c_str(), peer_in_anchor->GetOwnerNode()->GetName().c_str(),
  146. pld->GetName().c_str(), pld->GetOwnerComputeGraph()->GetName().c_str());
  147. return FAILED;
  148. }
  149. if (GraphUtils::AddEdge(src_anchor, peer_in_anchor) != GRAPH_SUCCESS) {
  150. REPORT_CALL_ERROR("E19999", "AddEdge from %s to %s failed.",
  151. src_anchor->GetOwnerNode()->GetName().c_str(),
  152. peer_in_anchor->GetOwnerNode()->GetName().c_str());
  153. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Add][Edge] from %s to %s failed.",
  154. src_anchor->GetOwnerNode()->GetName().c_str(), peer_in_anchor->GetOwnerNode()->GetName().c_str());
  155. return FAILED;
  156. }
  157. }
  158. } else {
  159. GELOGW("End or pld is nullptr or in data anchor of end is nullptr or out data anchor of pld is nullptr");
  160. }
  161. }
  162. return SUCCESS;
  163. }
  164. Status ge::GraphPartitioner::MergeAfterSubGraphOptimization(ge::ComputeGraphPtr &output_merged_compute_graph,
  165. const ge::ComputeGraphPtr &original_compute_graph) {
  166. Status real_ret = SUCCESS;
  167. auto ret = MergeSubGraph(output_merged_compute_graph, original_compute_graph);
  168. if (ret != SUCCESS) {
  169. // even though failed, ensure all op do finish check support
  170. real_ret = FAILED;
  171. GELOGE(ret, "[Merge][SubGraph] Failed, ret:%d", ret);
  172. }
  173. GE_CHECK_NOTNULL(original_compute_graph);
  174. // partition sub graph
  175. for (const auto &sub_graph : original_compute_graph->GetAllSubgraphs()) {
  176. ComputeGraphPtr merged_sub_graph = nullptr;
  177. ret = MergeSubGraph(merged_sub_graph, sub_graph);
  178. if (ret != SUCCESS) {
  179. real_ret = FAILED;
  180. GELOGE(ret, "[Merge][SubGraph] Failed, ret:%d", ret);
  181. continue;
  182. }
  183. // add sub graph
  184. output_merged_compute_graph->SetName(original_compute_graph->GetName());
  185. merged_sub_graph->SetName(sub_graph->GetName());
  186. merged_sub_graph->SetInputSize(sub_graph->GetInputSize());
  187. merged_sub_graph->SetOutputSize(sub_graph->GetOutputSize());
  188. auto parent_node = sub_graph->GetParentNode();
  189. GE_IF_BOOL_EXEC(parent_node == nullptr,
  190. REPORT_INNER_ERROR("E19999", "Parent node of graph:%s is nullptr.",
  191. sub_graph->GetName().c_str());
  192. GELOGE(FAILED, "[Check][Param] Parent node is null, graph name is %s",
  193. sub_graph->GetName().c_str());
  194. return FAILED;)
  195. auto original_graph = parent_node->GetOwnerComputeGraph();
  196. GE_IF_BOOL_EXEC(graph_2_graph_partition_info_.find(original_graph) == graph_2_graph_partition_info_.end(),
  197. REPORT_INNER_ERROR("E19999", "graph:%s not find in graph_2_graph_partition_info_, check invalid.",
  198. original_graph->GetName().c_str());
  199. GELOGE(FAILED, "[Check][Param] Find graph info failed, graph name is %s",
  200. original_graph->GetName().c_str());
  201. return FAILED;)
  202. auto graph_info = graph_2_graph_partition_info_[original_graph];
  203. GE_IF_BOOL_EXEC(graph_info.corresponding_node_in_partitions_.count(parent_node) == 0,
  204. REPORT_INNER_ERROR("E19999", "node:%s not find in corresponding_node_in_partitions_, "
  205. "check invalid", parent_node->GetName().c_str());
  206. GELOGE(FAILED, "[Check][Param] Find corresponding node failed, parent node name is %s",
  207. parent_node->GetName().c_str());
  208. return FAILED;)
  209. auto corresponding_node = graph_info.corresponding_node_in_partitions_[parent_node];
  210. GE_IF_BOOL_EXEC(corresponding_node == nullptr,
  211. REPORT_INNER_ERROR("E19999", "Get null node in corresponding_node_in_partitions_, "
  212. "first node name is %s", parent_node->GetName().c_str());
  213. GELOGE(FAILED, "[Check][Param] Get null node in corresponding_node_in_partitions_, "
  214. "first node name is %s", parent_node->GetName().c_str());
  215. return FAILED;);
  216. merged_sub_graph->SetParentNode(corresponding_node);
  217. auto subgraph_parent_graph = corresponding_node->GetOwnerComputeGraph();
  218. merged_sub_graph->SetParentGraph(subgraph_parent_graph);
  219. ret = output_merged_compute_graph->AddSubgraph(sub_graph->GetName(), merged_sub_graph);
  220. GE_IF_BOOL_EXEC(ret != GRAPH_SUCCESS, return ret;)
  221. }
  222. ClearAllPartitionData();
  223. if (real_ret != SUCCESS) {
  224. auto root_graph = ge::GraphUtils::FindRootGraph(original_compute_graph);
  225. GE_CHECK_NOTNULL(root_graph);
  226. (void)Analyzer::GetInstance()->SaveAnalyzerDataToFile(root_graph->GetSessionID(), root_graph->GetGraphID());
  227. }
  228. return real_ret;
  229. }
  230. Status ge::GraphPartitioner::MergeSubGraph(ge::ComputeGraphPtr &output_merged_compute_graph,
  231. const ge::ComputeGraphPtr &original_compute_graph) {
  232. if (original_compute_graph == nullptr) {
  233. REPORT_INNER_ERROR("E19999", "Param original_compute_graph is nullptr, check invalid.");
  234. GELOGE(GE_GRAPH_NULL_INPUT, "[Check][Param] original_compute_graph is nullptr.");
  235. return FAILED;
  236. }
  237. if ((graph_2_graph_partition_info_.find(original_compute_graph) == graph_2_graph_partition_info_.end()) ||
  238. (graph_2_subgraph_list_.find(original_compute_graph) == graph_2_subgraph_list_.end())) {
  239. REPORT_INNER_ERROR("E19999", "original_compute_graph:%s is not find in graph_2_graph_partition_info_.",
  240. original_compute_graph->GetName().c_str());
  241. GELOGE(GE_GRAPH_NULL_INPUT,
  242. "[Check][Param] original_compute_graph:%s is not find in graph_2_graph_partition_info_.",
  243. original_compute_graph->GetName().c_str());
  244. return FAILED;
  245. }
  246. GraphPartitionInfo &subgraph_info = graph_2_graph_partition_info_[original_compute_graph];
  247. const auto &sub_graph_list = graph_2_subgraph_list_[original_compute_graph];
  248. graph_info_ = subgraph_info;
  249. if (graph_info_.mode_ != kMerging) {
  250. REPORT_INNER_ERROR("E19999", "Cannot call merging in partition mode, as mode != %d", kMerging);
  251. GELOGE(GE_GRAPH_UNSUPPORTED, "[Check][Param] Cannot call merging in partition mode, as mode != %d", kMerging);
  252. return FAILED;
  253. }
  254. GELOGD("Graph merge starts.");
  255. // check input param
  256. for (const auto &it : sub_graph_list) {
  257. if (it == nullptr) {
  258. REPORT_INNER_ERROR("E19999", "sub_graph is nullptr, check invalid.");
  259. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Check][Param] merging sub-graphs failed, sub-graph is nullptr");
  260. return FAILED;
  261. }
  262. }
  263. bool is_map_empty = graph_info_.end_2_pld_.empty() || graph_info_.pld_2_end_.empty();
  264. if (is_map_empty) {
  265. if (CheckIfEnd2PldEmpty(output_merged_compute_graph) != SUCCESS) {
  266. return FAILED;
  267. }
  268. }
  269. ComputeGraphPtr new_sub_graph = MakeShared<ComputeGraph>(original_compute_graph->GetName());
  270. GE_CHECK_NOTNULL(new_sub_graph);
  271. output_merged_compute_graph = new_sub_graph;
  272. GE_TIMESTAMP_START(MergeSubGraphRemoveNode);
  273. if (RemoveNodeAndEdgeBetweenEndPld(output_merged_compute_graph, sub_graph_list) != ge::SUCCESS) {
  274. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Call][RemoveNodeAndEdgeBetweenEndPld] failed, graph:%s",
  275. output_merged_compute_graph->GetName().c_str());
  276. return FAILED;
  277. }
  278. GE_TIMESTAMP_END(MergeSubGraphRemoveNode, "GraphPartitioner::MergeGraphRemoveNodeAndEdge");
  279. GE_TIMESTAMP_START(MergeSubGraphTopologicalSorting);
  280. Status ret = output_merged_compute_graph->TopologicalSorting();
  281. if (ret != SUCCESS) {
  282. GELOGE(GE_GRAPH_TOPO_SORT_FAILED, "[Call][TopologicalSorting] for output_merged_compute_graph:%s failed",
  283. output_merged_compute_graph->GetName().c_str());
  284. return FAILED;
  285. }
  286. GE_TIMESTAMP_END(MergeSubGraphTopologicalSorting, "GraphPartitioner::MergeGraphTopologicalSorting");
  287. // flush all nodes' engine of merged graph
  288. GE_TIMESTAMP_START(MergeSubGraphEnginePlacerRun);
  289. graph_info_.engine_placer_.SetComputeGraph(output_merged_compute_graph);
  290. if (graph_info_.engine_placer_.Run() != SUCCESS) {
  291. GELOGE(GE_GRAPH_INIT_FAILED, "[Call][Run] engine_placer run failed, graph:%s",
  292. output_merged_compute_graph->GetName().c_str());
  293. return FAILED;
  294. }
  295. GE_TIMESTAMP_END(MergeSubGraphEnginePlacerRun, "GraphPartitioner::MergeGraphEnginePlacerRun");
  296. GELOGD("Graph merge ends.");
  297. return SUCCESS;
  298. }
  299. Status ge::GraphPartitioner::UpdatePldOpDesc(const NodePtr &dst_node, int input_index, OpDescPtr &pld_op_desc) {
  300. if ((dst_node == nullptr) || (pld_op_desc == nullptr) || (dst_node->GetOpDesc() == nullptr)) {
  301. REPORT_INNER_ERROR("E19999", "Param dst_node or pld_op_desc or op of dst_node is nullptr, check invalid");
  302. GELOGE(FAILED, "[Check][Param] parameter ptr is null.");
  303. return FAILED;
  304. }
  305. const auto &input_desc = dst_node->GetOpDesc()->GetInputDesc(static_cast<uint32_t>(input_index));
  306. GE_IF_BOOL_EXEC(pld_op_desc->AddOutputDesc(input_desc) != GRAPH_SUCCESS,
  307. REPORT_CALL_ERROR("E19999", "AddOutputDesc to op:%s failed", pld_op_desc->GetName().c_str());
  308. GELOGE(FAILED, "[Add][OutputDesc] to op:%s failed", pld_op_desc->GetName().c_str());
  309. return FAILED;)
  310. if (pld_op_desc->MutableOutputDesc(0) != nullptr) {
  311. ge::TensorUtils::SetRealDimCnt(*(pld_op_desc->MutableOutputDesc(0).get()),
  312. static_cast<uint32_t>(input_desc.GetShape().GetDims().size()));
  313. } else {
  314. REPORT_INNER_ERROR("E19999", "output(0) of op:%s is nullptr, check invalid", pld_op_desc->GetName().c_str());
  315. GELOGE(GE_GRAPH_ADD_PLC_END_FAILED, "[Check][Param] output(0) of op:%s is nullptr.",
  316. pld_op_desc->GetName().c_str());
  317. return FAILED;
  318. }
  319. return SUCCESS;
  320. }
  321. Status ge::GraphPartitioner::UpdateEndOpDesc(const NodePtr &src_node, int output_index, OpDescPtr &end_op_desc) {
  322. if ((src_node == nullptr) || (end_op_desc == nullptr) || (src_node->GetOpDesc() == nullptr)) {
  323. REPORT_INNER_ERROR("E19999", "Param src_node or end_op_desc or op of src_node is nullptr, check invalid.");
  324. GELOGE(FAILED, "[Check][Param] parameter ptr is null.");
  325. return FAILED;
  326. }
  327. const auto &output_desc = src_node->GetOpDesc()->GetOutputDesc(static_cast<uint32_t>(output_index));
  328. GE_IF_BOOL_EXEC(end_op_desc->AddInputDesc(output_desc) != GRAPH_SUCCESS,
  329. REPORT_CALL_ERROR("E19999", "AddInputDesc to op:%s failed", end_op_desc->GetName().c_str());
  330. GELOGE(FAILED, "[Add][InputDesc] to op:%s failed", end_op_desc->GetName().c_str());
  331. return FAILED;)
  332. if (end_op_desc->MutableInputDesc(0) != nullptr) {
  333. ge::TensorUtils::SetRealDimCnt(*(end_op_desc->MutableInputDesc(0).get()),
  334. static_cast<uint32_t>(output_desc.GetShape().GetDims().size()));
  335. } else {
  336. REPORT_INNER_ERROR("E19999", "input(0) of op:%s is nullptr, check invalid.", end_op_desc->GetName().c_str());
  337. GELOGE(GE_GRAPH_ADD_PLC_END_FAILED, "[Check][Param] input(0) of op:%s is nullptr.",
  338. end_op_desc->GetName().c_str());
  339. return FAILED;
  340. }
  341. return SUCCESS;
  342. }
  343. graphStatus ge::GraphPartitioner::AddPlaceHolderEndInSrcDstGraph(const AnchorPtr &out_anchor,
  344. const AnchorPtr &peer_in_anchor,
  345. const ge::ComputeGraphPtr &pld_graph,
  346. const ge::ComputeGraphPtr &end_graph) {
  347. GE_CHECK_NOTNULL(peer_in_anchor);
  348. GE_CHECK_NOTNULL(pld_graph);
  349. GE_CHECK_NOTNULL(out_anchor);
  350. GE_CHECK_NOTNULL(end_graph);
  351. const auto &src_node = out_anchor->GetOwnerNode();
  352. const auto &dst_node = peer_in_anchor->GetOwnerNode();
  353. // link input -> end
  354. string end_name = kEndType + std::to_string(graph_info_.num_of_pld_end_);
  355. auto end_op_desc = MakeShared<OpDesc>(end_graph->GetName() + "_" + end_name, END);
  356. if (end_op_desc == nullptr) {
  357. REPORT_CALL_ERROR("E19999", "New Memory for OpDesc failed.");
  358. GELOGE(GRAPH_PARAM_INVALID, "[New][Memory] for OpDesc failed, pld_op_desc is nullptr.");
  359. return FAILED;
  360. }
  361. GE_IF_BOOL_EXEC(!AttrUtils::SetInt(end_op_desc, "peerIndex", graph_info_.num_of_pld_end_),
  362. GELOGW("SetInt peerIndex failed");)
  363. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(end_op_desc, "parentOpType", dst_node->GetType()),
  364. GELOGW("SetStr parentOpType failed");)
  365. GE_IF_BOOL_EXEC(!end_op_desc->SetExtAttr("parentNode", dst_node),
  366. GELOGW("SetEndExtAttr parentNode failed");)
  367. OpDescPtr dst_node_op_desc = dst_node->GetOpDesc();
  368. GE_CHECK_NOTNULL(dst_node_op_desc);
  369. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(end_op_desc, ATTR_NAME_END_REAR_NODE_ENGINE_NAME,
  370. dst_node_op_desc->GetOpEngineName()), GELOGW("SetStr rearNodeEngineName failed");)
  371. // replace input_desc of end with owner node's desc
  372. int output_index = ge::AnchorUtils::GetIdx(out_anchor);
  373. bool is_need_update_desc = (output_index >= 0) && (graph_info_.mode_ == kPartitioning);
  374. if (is_need_update_desc) {
  375. if (UpdateEndOpDesc(src_node, output_index, end_op_desc) != SUCCESS) {
  376. GELOGE(GRAPH_PARAM_INVALID, "[Update][EndOpDesc] failed, input index:%d, end_op_desc:%s",
  377. output_index, end_op_desc->GetName().c_str());
  378. return FAILED;
  379. }
  380. } else {
  381. GeTensorDesc input_desc;
  382. if (end_op_desc->AddInputDesc(input_desc) != SUCCESS) {
  383. REPORT_CALL_ERROR("E19999", "add input desc to op:%s failed, input index:%d",
  384. end_op_desc->GetName().c_str(), output_index);
  385. GELOGE(GRAPH_PARAM_INVALID, "[Add][InputDesc] to op:%s failed, input index %d",
  386. end_op_desc->GetName().c_str(), output_index);
  387. return FAILED;
  388. }
  389. }
  390. NodePtr new_end_node = end_graph->AddNode(end_op_desc);
  391. if (new_end_node == nullptr) {
  392. REPORT_CALL_ERROR("E19999", "add node:%s in graph:%s failed",
  393. end_op_desc->GetName().c_str(), end_graph->GetName().c_str());
  394. GELOGE(GRAPH_PARAM_INVALID, "[Add][Node] %s in graph:%s failed.",
  395. end_op_desc->GetName().c_str(), end_graph->GetName().c_str());
  396. return FAILED;
  397. }
  398. GE_IF_BOOL_EXEC(new_end_node->SetOwnerComputeGraph(end_graph) != GRAPH_SUCCESS,
  399. REPORT_CALL_ERROR("E19999", "SetOwnerComputeGraph %s for node:%s failed",
  400. end_graph->GetName().c_str(), new_end_node->GetName().c_str());
  401. GELOGE(GRAPH_PARAM_INVALID, "[Set][OwnerComputeGraph] %s for node:%s failed",
  402. end_graph->GetName().c_str(), new_end_node->GetName().c_str());
  403. return FAILED;)
  404. AnchorPtr end_dst_anchor = GetEndInAnchor(out_anchor, new_end_node);
  405. if (GraphUtils::AddEdge(out_anchor, end_dst_anchor) != GRAPH_SUCCESS) {
  406. REPORT_CALL_ERROR("E19999", "add edge from %s to %s failed", out_anchor->GetOwnerNode()->GetName().c_str(),
  407. end_dst_anchor->GetOwnerNode()->GetName().c_str());
  408. GELOGE(GE_GRAPH_ADD_PLC_END_FAILED, "[Add][Edge] from %s to %s failed",
  409. out_anchor->GetOwnerNode()->GetName().c_str(), end_dst_anchor->GetOwnerNode()->GetName().c_str());
  410. return FAILED;
  411. }
  412. /// For fe, op id has been set in AddNode,
  413. /// we can take op id of srcNode as the mark of parentId now
  414. const auto &src_node_opdesc = src_node->GetOpDesc();
  415. GE_CHECK_NOTNULL(src_node_opdesc);
  416. int64_t node_id = src_node_opdesc->GetId();
  417. const string pld_name = kPlaceHolderType + std::to_string(graph_info_.num_of_pld_end_);
  418. auto pld_op_desc = MakeShared<OpDesc>(pld_graph->GetName() + "_" + pld_name, PLACEHOLDER);
  419. if (pld_op_desc == nullptr) {
  420. REPORT_CALL_ERROR("E19999", "New Memory for OpDesc failed.");
  421. GELOGE(GRAPH_PARAM_INVALID, "[New][Memory] for OpDesc failed.");
  422. return FAILED;
  423. }
  424. GE_IF_BOOL_EXEC(!AttrUtils::SetInt(pld_op_desc, "peerIndex", graph_info_.num_of_pld_end_),
  425. GELOGW("SetInt peerIndex failed");)
  426. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(pld_op_desc, "_peerNodeName", new_end_node->GetName()),
  427. GELOGW("SetStr _peerNodeName failed");)
  428. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(pld_op_desc, "parentOpType", src_node->GetType()),
  429. GELOGW("SetStr parentOpType failed");)
  430. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(pld_op_desc, "_parentNodeName", src_node->GetName()),
  431. GELOGW("SetStr parentOpName failed");)
  432. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(pld_op_desc, "parentId", end_graph->GetName() + ":" + std::to_string(node_id)),
  433. GELOGW("SetStr parentId failed");)
  434. GE_IF_BOOL_EXEC(!AttrUtils::SetInt(pld_op_desc, "anchorIndex", AnchorUtils::GetIdx(out_anchor)),
  435. GELOGW("SetInt anchorIndex failed");)
  436. GE_IF_BOOL_EXEC(!pld_op_desc->SetExtAttr("parentNode", src_node),
  437. GELOGW("SetPldExtAttr parentNode failed");)
  438. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(pld_op_desc, ATTR_NAME_PLD_FRONT_NODE_ENGINE_NAME,
  439. src_node_opdesc->GetOpEngineName()), GELOGW("SetStr frontNodeEngineName failed");)
  440. std::string l2_info_attr;
  441. if (AttrUtils::GetStr(src_node_opdesc, "_task_L2FusionInfo", l2_info_attr)) {
  442. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(pld_op_desc, "_task_L2FusionInfo", l2_info_attr),
  443. GELOGW("SetStr l2_info_attr failed");)
  444. }
  445. int64_t anchor_index_for_lxfusion;
  446. if (AttrUtils::GetInt(src_node_opdesc, "_data_anchor_index_for_lxfusion", anchor_index_for_lxfusion)) {
  447. GE_IF_BOOL_EXEC(!AttrUtils::SetInt(pld_op_desc, "_data_anchor_index_for_lxfusion", anchor_index_for_lxfusion),
  448. GELOGW("SetInt anchor_index_for_lxfusion failed");)
  449. }
  450. // do not care over flow
  451. graph_info_.num_of_pld_end_++;
  452. // replace output_desc of pld with input node's output desc
  453. int input_index = ge::AnchorUtils::GetIdx(peer_in_anchor);
  454. is_need_update_desc = (input_index >= 0) && (graph_info_.mode_ == kPartitioning);
  455. if (is_need_update_desc) {
  456. if (UpdatePldOpDesc(dst_node, input_index, pld_op_desc) != SUCCESS) {
  457. GELOGE(GRAPH_PARAM_INVALID, "[Update][PldOpDesc] failed, output index:%d, pld_op_desc:%s",
  458. input_index, pld_op_desc->GetName().c_str());
  459. return FAILED;
  460. }
  461. } else {
  462. GeTensorDesc output_desc;
  463. if (pld_op_desc->AddOutputDesc(output_desc) != SUCCESS) {
  464. REPORT_CALL_ERROR("E19999", "AddOutputDesc to op:%s failed, input index %d",
  465. pld_op_desc->GetName().c_str(), input_index);
  466. GELOGE(GRAPH_PARAM_INVALID, "[Add][OutputDesc] to op:%s failed, input index %d",
  467. pld_op_desc->GetName().c_str(), input_index);
  468. return FAILED;
  469. }
  470. }
  471. NodePtr new_pld_node = pld_graph->AddNode(pld_op_desc);
  472. if (new_pld_node == nullptr) {
  473. REPORT_CALL_ERROR("E19999", "AddNode %s in graph:%s failed.",
  474. pld_op_desc->GetName().c_str(), pld_graph->GetName().c_str());
  475. GELOGE(GRAPH_PARAM_INVALID, "[Add][Node] %s in graph:%s failed.",
  476. pld_op_desc->GetName().c_str(), pld_graph->GetName().c_str());
  477. return FAILED;
  478. }
  479. GE_IF_BOOL_EXEC(new_pld_node->SetOwnerComputeGraph(pld_graph) != GRAPH_SUCCESS,
  480. REPORT_CALL_ERROR("E19999", "SetOwnerComputeGraph for node:%s failed, graph:%s",
  481. new_pld_node->GetName().c_str(), pld_graph->GetName().c_str());
  482. GELOGE(GRAPH_PARAM_INVALID, "[Set][OwnerComputeGraph] for node:%s failed, graph:%s",
  483. new_pld_node->GetName().c_str(), pld_graph->GetName().c_str());
  484. return FAILED;)
  485. AnchorPtr pld_src_anchor = GetPldOutAnchor(new_pld_node, peer_in_anchor);
  486. // link placeHolder -> computeNode
  487. if (GraphUtils::AddEdge(pld_src_anchor, peer_in_anchor) != GRAPH_SUCCESS) {
  488. REPORT_CALL_ERROR("E19999", "AddEdge from %s to %s failed",
  489. pld_src_anchor->GetOwnerNode()->GetName().c_str(),
  490. peer_in_anchor->GetOwnerNode()->GetName().c_str());
  491. GELOGE(GE_GRAPH_ADD_PLC_END_FAILED, "[Add][Edge] from %s to %s failed",
  492. pld_src_anchor->GetOwnerNode()->GetName().c_str(), peer_in_anchor->GetOwnerNode()->GetName().c_str());
  493. return FAILED;
  494. }
  495. graph_info_.index_2_end_[graph_info_.num_of_pld_end_] = new_end_node;
  496. graph_info_.pld_2_end_[new_pld_node] = new_end_node;
  497. graph_info_.end_2_pld_[new_end_node] = new_pld_node;
  498. return SUCCESS;
  499. }
  500. Status ge::GraphPartitioner::LinkInput2EndRemoveOrginalLink(ge::NodePtr input_node, ge::ComputeGraphPtr src_graph,
  501. ge::ComputeGraphPtr dst_graph) {
  502. if ((input_node == nullptr) || (src_graph == nullptr) || (dst_graph == nullptr)) {
  503. REPORT_INNER_ERROR("E19999", "Param input_node or src_graph or dst_graph is nullptr, check invalid.");
  504. GELOGE(FAILED, "[Check][Param] parameter input_node or src_graph or dst_graph is nullptr.");
  505. return FAILED;
  506. }
  507. // get the original anchors and remove the original link
  508. for (const auto &out_data_anchor : input_node->GetAllOutAnchors()) {
  509. for (auto &peer_in_anchor : out_data_anchor->GetPeerAnchors()) {
  510. if (peer_in_anchor->GetOwnerNode()->GetType() != kEndType) {
  511. if (GraphUtils::RemoveEdge(out_data_anchor, peer_in_anchor) != GRAPH_SUCCESS) {
  512. REPORT_CALL_ERROR("E19999", "RemoveEdge between %s and %s failed.",
  513. out_data_anchor->GetOwnerNode()->GetName().c_str(),
  514. peer_in_anchor->GetOwnerNode()->GetName().c_str());
  515. GELOGE(FAILED, "[Remove][Edge] between %s and %s failed.",
  516. out_data_anchor->GetOwnerNode()->GetName().c_str(), peer_in_anchor->GetOwnerNode()->GetName().c_str());
  517. return FAILED;
  518. }
  519. // link input -> end
  520. auto ret = AddPlaceHolderEndInSrcDstGraph(out_data_anchor, peer_in_anchor, src_graph, dst_graph);
  521. if (ret != SUCCESS) {
  522. GELOGE(GE_GRAPH_ADD_PLC_END_FAILED, "[Call][AddPlaceHolderEndInSrcDstGraph] failed, ret:%d.", ret);
  523. return ret;
  524. }
  525. } else {
  526. auto end_node = peer_in_anchor->GetOwnerNode();
  527. if (GraphUtils::RemoveJustNode(src_graph, end_node) != GRAPH_SUCCESS) {
  528. REPORT_CALL_ERROR("E19999", "RemoveJustNode %s from graph:%s failed.",
  529. end_node->GetName().c_str(), src_graph->GetName().c_str());
  530. GELOGE(FAILED, "[Remove][JustNode] %s from graph:%s failed.",
  531. end_node->GetName().c_str(), src_graph->GetName().c_str());
  532. return FAILED;
  533. }
  534. if (end_node->SetOwnerComputeGraph(dst_graph) != GRAPH_SUCCESS) {
  535. REPORT_CALL_ERROR("E19999", "SetOwnerComputeGraph for node:%s failed, graph:%s.",
  536. end_node->GetName().c_str(), dst_graph->GetName().c_str());
  537. GELOGE(FAILED, "[Set][OwnerComputeGraph] to node:%s failed, graph:%s.",
  538. end_node->GetName().c_str(), dst_graph->GetName().c_str());
  539. return FAILED;
  540. }
  541. if (dst_graph->AddNode(end_node) == nullptr) {
  542. REPORT_CALL_ERROR("E19999", "AddNode %s in graph:%s failed.",
  543. end_node->GetName().c_str(), dst_graph->GetName().c_str());
  544. GELOGE(FAILED, "[Add][Node] %s in graph:%s failed.",
  545. end_node->GetName().c_str(), dst_graph->GetName().c_str());
  546. return FAILED;
  547. }
  548. }
  549. }
  550. }
  551. return SUCCESS;
  552. }
  553. Status ge::GraphPartitioner::PutInputNodesInSubGraph(const ge::ComputeGraphPtr &src_graph,
  554. const ge::ComputeGraphPtr &dst_graph) {
  555. if ((src_graph == nullptr) || (dst_graph == nullptr)) {
  556. REPORT_INNER_ERROR("E19999", "Param src_graph or dst_graph is nullptr, check invalid.");
  557. GELOGE(FAILED, "[Check][Param] parameter src_graph or dst_graph is nullptr.");
  558. return FAILED;
  559. }
  560. for (auto &input_node : src_graph->GetDirectNode()) {
  561. if (IsDataLike(input_node)) {
  562. if (input_node->SetOwnerComputeGraph(dst_graph) != GRAPH_SUCCESS) {
  563. REPORT_CALL_ERROR("E19999", "SetOwnerComputeGraph for node:%s failed, graph:%s.",
  564. input_node->GetName().c_str(), dst_graph->GetName().c_str());
  565. GELOGE(FAILED, "[Set][OwnerComputeGraph] for node:%s failed, graph:%s.",
  566. input_node->GetName().c_str(), dst_graph->GetName().c_str());
  567. return FAILED;
  568. }
  569. // remove input node from src_graph
  570. if (GraphUtils::RemoveJustNode(src_graph, input_node) != GRAPH_SUCCESS) {
  571. REPORT_CALL_ERROR("E19999", "RemoveJustNode %s from graph:%s failed.",
  572. input_node->GetName().c_str(), src_graph->GetName().c_str());
  573. GELOGE(FAILED, "[Remove][JustNode] %s from graph:%s failed.",
  574. input_node->GetName().c_str(), src_graph->GetName().c_str());
  575. return FAILED;
  576. }
  577. // add input node to dst_graph
  578. if (dst_graph->AddNode(input_node) == nullptr) {
  579. REPORT_CALL_ERROR("E19999", "AddNode %s in graph:%s failed.",
  580. input_node->GetName().c_str(), src_graph->GetName().c_str());
  581. GELOGE(FAILED, "[Add][Node] %s in graph:%s failed.",
  582. input_node->GetName().c_str(), src_graph->GetName().c_str());
  583. return FAILED;
  584. }
  585. if (LinkInput2EndRemoveOrginalLink(input_node, src_graph, dst_graph) != ge::SUCCESS) {
  586. GELOGE(FAILED, "[Call][LinkInput2EndRemoveOrginalLink] failed.");
  587. return FAILED;
  588. }
  589. }
  590. }
  591. return SUCCESS;
  592. }
  593. void ge::GraphPartitioner::AddNewGraphToPartition(ge::ComputeGraphPtr &input_graph, const std::string &engine_name) {
  594. if (input_graph == nullptr) {
  595. GELOGW("[GraphPartitioner]: input_graph is null, engine name is %s", engine_name.c_str());
  596. return;
  597. }
  598. graph_info_.partitions_[input_graph] = engine_name;
  599. }
  600. bool ge::GraphPartitioner::IsDataLike(ge::NodePtr node) {
  601. return (node->GetType() == CONSTANT) || (node->GetType() == DATA) || (node->GetType() == AIPPDATA) ||
  602. (node->GetType() == CONSTANTOP) || (node->GetType() == VARIABLE);
  603. }
  604. bool ge::GraphPartitioner::HasNoInput(ge::NodePtr node) {
  605. if (node == nullptr) {
  606. GELOGE(FAILED, "[Check][Param] node is nullptr.");
  607. return true;
  608. }
  609. return node->GetInNodes().empty();
  610. }
  611. Status ge::GraphPartitioner::Initialize(ge::ComputeGraphPtr compute_graph) {
  612. GELOGI("Initialize starts.");
  613. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  614. if (instance_ptr == nullptr || compute_graph == nullptr) {
  615. REPORT_INNER_ERROR("E19999", "compute_graph or instance_ptr of GELib is nullptr, check invalid.");
  616. GELOGE(GE_GRAPH_NOT_INIT, "[Check][Param] compute_graph or instance_ptr of GELib is nullptr.");
  617. return FAILED;
  618. }
  619. graph_info_.engine_placer_.SetComputeGraph(compute_graph);
  620. if (graph_info_.engine_placer_.Run() != SUCCESS) {
  621. GELOGE(FAILED, "[Call][Run] Engine placer run failed, graph:%s.", compute_graph->GetName().c_str());
  622. return FAILED;
  623. }
  624. const NodeEngineMap *node_engine_map = graph_info_.engine_placer_.GetNodeEngineMap();
  625. size_t temp_index = 0;
  626. // travese nodes by topo order one by one
  627. for (const auto &node : compute_graph->GetDirectNode()) {
  628. std::string temp_stream;
  629. // node opdesc has been checked before
  630. (void)AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, temp_stream);
  631. ClusterPtr new_cluster;
  632. // data like node without input should be handle specific
  633. if (HasNoInput(node) && IsDataLike(node)) {
  634. ClusterPtr cluster = MakeShared<Cluster>(temp_index, kEngineDefaultData, temp_stream);
  635. new_cluster = cluster;
  636. } else {
  637. if (node_engine_map->count(node) == 0) {
  638. REPORT_INNER_ERROR("E19999", "node:%s not find in node_engine_map", node->GetName().c_str());
  639. GELOGE(FAILED, "[Check][Param] node[%s] does not owner engine!", node->GetName().c_str());
  640. return FAILED;
  641. }
  642. ClusterPtr cluster = MakeShared<Cluster>(temp_index, node_engine_map->at(node), temp_stream);
  643. new_cluster = cluster;
  644. }
  645. if (new_cluster == nullptr) {
  646. REPORT_CALL_ERROR("E19999", "Allocate Cluster failed, index:%zu", temp_index);
  647. GELOGE(FAILED, "[Allocate][Cluster] failed, index:%zu", temp_index);
  648. return FAILED;
  649. }
  650. new_cluster->nodes_.push_back(node);
  651. if (!HasNoInput(node)) {
  652. auto node_id = node->GetOpDesc()->GetId();
  653. for (const auto &parent : node->GetInAllNodes()) {
  654. auto parent_id = parent->GetOpDesc()->GetId();
  655. if (parent_id < node_id) {
  656. auto iter = graph_info_.node_2_cluster_.find(parent);
  657. if (iter == graph_info_.node_2_cluster_.end()) {
  658. REPORT_INNER_ERROR("E19999", "node[%s]id[%ld]'s parent_node[%s]id[%ld] should make cluster in advance",
  659. node->GetOpDesc()->GetName().c_str(), node_id,
  660. parent->GetOpDesc()->GetName().c_str(), parent_id);
  661. GELOGE(FAILED, "[Check][Param] node[%s]id[%ld]'s parent_node[%s]id[%ld] should make cluster in advance",
  662. node->GetOpDesc()->GetName().c_str(), node_id, parent->GetOpDesc()->GetName().c_str(), parent_id);
  663. return FAILED;
  664. }
  665. new_cluster->in_clu_.insert(iter->second->index_);
  666. iter->second->out_clu_.insert(temp_index);
  667. }
  668. }
  669. }
  670. graph_info_.node_2_cluster_[node] = new_cluster;
  671. graph_info_.clusters_[temp_index] = new_cluster;
  672. GELOGD("Node name is %s, engine is %s, cluster index is %zu, stream label is %s", node->GetName().c_str(),
  673. new_cluster->engine_name_.c_str(), new_cluster->index_, new_cluster->stream_label_.c_str());
  674. temp_index++;
  675. }
  676. GELOGD("Initialize ends.");
  677. return SUCCESS;
  678. }
  679. Status ge::GraphPartitioner::AddPartitionsToGraphNode(vector<ge::SubGraphInfoPtr> &output_subgraphs,
  680. ge::ComputeGraphPtr compute_graph) {
  681. const std::string &input_subgraph_name = "inputNodesSubGraph";
  682. string session_graph_id;
  683. if (!AttrUtils::GetStr(*compute_graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id)) {
  684. GELOGW("Get graph session_graph_id attr failed.");
  685. return INTERNAL_ERROR;
  686. }
  687. // the output_subgraphs have topological order
  688. for (const auto &sub_graph : graph_info_.rank_2_partitions_) {
  689. if (graph_info_.partitions_.find(sub_graph) == graph_info_.partitions_.end()) {
  690. REPORT_INNER_ERROR("E19999", "partition is null, subgraph:%s", sub_graph->GetName().c_str());
  691. GELOGE(GE_GRAPH_EMPTY_PARTITION, "[Check][Param] partition is null, subgraph:%s", sub_graph->GetName().c_str());
  692. return FAILED;
  693. }
  694. auto &engine_name = graph_info_.partitions_.at(sub_graph);
  695. (void)AttrUtils::SetStr(sub_graph, ATTR_NAME_PARENT_GRAPH_NAME, compute_graph->GetName());
  696. GELOGD("set attr success. subgraph(%s) with parent graph(%s)", sub_graph->GetName().c_str(),
  697. compute_graph->GetName().c_str());
  698. GE_DUMP(sub_graph, sub_graph->GetName() + "_" + mode_2_str_[graph_info_.mode_]);
  699. if (!session_graph_id.empty()) {
  700. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(sub_graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id),
  701. GELOGW("SetStr ATTR_NAME_SESSION_GRAPH_ID failed");)
  702. }
  703. // flush parent node of subgraph
  704. sub_graph->SetParentNode(compute_graph->GetParentNode());
  705. auto sgi = MakeShared<SubGraphInfo>();
  706. if (sgi == nullptr) {
  707. REPORT_CALL_ERROR("E19999", "allocate memory for SubGraphInfo failed.");
  708. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Allocate][Memory] for SubGraphInfo failed.");
  709. return FAILED;
  710. }
  711. // set engine name
  712. sgi->SetEngineName(engine_name);
  713. // set stream label
  714. string sub_graph_stream;
  715. if (AttrUtils::GetStr(sub_graph->GetDirectNode().at(0)->GetOpDesc(), ATTR_NAME_STREAM_LABEL, sub_graph_stream)) {
  716. sgi->SetStreamLabel(sub_graph_stream);
  717. }
  718. /// for now inputFlag is the same before and after partition. It should
  719. /// be changed according to the real partition
  720. std::vector<bool> sub_graph_input(graph_info_.input_size_, true);
  721. std::vector<bool> sub_graph_output(graph_info_.output_size_, true);
  722. sgi->SetSubGraph(sub_graph);
  723. sgi->SetOutputFlag(sub_graph_output);
  724. sgi->SetInputFlag(sub_graph_input);
  725. sgi->SetOutputContext(graph_info_.output_name_);
  726. AddEndPldInformationToSubGraphInfo(sgi);
  727. GELOGI("[GraphPartitioner]: subGraph engine name is %s, graph name is %s, stream label is %s", engine_name.c_str(),
  728. sub_graph->GetName().c_str(), sgi->GetStreamLabel().empty() ? "null" : sgi->GetStreamLabel().c_str());
  729. if (engine_name != input_subgraph_name) { // do not add Data subGraph into SubGraphInfo
  730. output_subgraphs.push_back(sgi);
  731. } else {
  732. graph_2_input_subgraph_[compute_graph] = sgi;
  733. }
  734. }
  735. return SUCCESS;
  736. }
  737. // check if two clusters can merge
  738. bool ge::GraphPartitioner::IsMergeable(size_t parent_cluster, size_t child_cluster, size_t upper_bound) {
  739. if ((graph_info_.clusters_[parent_cluster] == nullptr) || (graph_info_.clusters_[parent_cluster]->nodes_.empty()) ||
  740. (graph_info_.clusters_[child_cluster] == nullptr) || (graph_info_.clusters_[child_cluster]->nodes_.empty())) {
  741. return false;
  742. }
  743. // Check if parent_cluster,child_cluster has same engine or stream label
  744. if ((graph_info_.clusters_[parent_cluster]->engine_name_ != graph_info_.clusters_[child_cluster]->engine_name_) ||
  745. (graph_info_.clusters_[parent_cluster]->stream_label_ != graph_info_.clusters_[child_cluster]->stream_label_)) {
  746. GELOGD("Parent cluster %zu engine %s stream label %s, child cluster %zu engine %s stream label %s can not merge",
  747. parent_cluster, graph_info_.clusters_[parent_cluster]->engine_name_.c_str(),
  748. graph_info_.clusters_[parent_cluster]->stream_label_.c_str(), child_cluster,
  749. graph_info_.clusters_[child_cluster]->engine_name_.c_str(),
  750. graph_info_.clusters_[child_cluster]->stream_label_.c_str());
  751. return false;
  752. }
  753. // Check if parent_cluster,child_cluster is reachable
  754. RemoveEdge(parent_cluster, child_cluster);
  755. // Check if there is a path between parent and child, if return true, can not merge
  756. if (HasSecondPath(parent_cluster, child_cluster, upper_bound)) {
  757. GELOGD("Find second path from %zu to %zu, upper bound is %zu", parent_cluster, child_cluster, upper_bound);
  758. InsertEdge(parent_cluster, child_cluster);
  759. return false;
  760. }
  761. InsertEdge(parent_cluster, child_cluster);
  762. return true;
  763. }
  764. void ge::GraphPartitioner::MergeTwoClusters(size_t parent_cluster, size_t &child_cluster) {
  765. // check which index is bigger
  766. size_t big_cluster, small_cluster;
  767. size_t child_cluster_original = child_cluster;
  768. if (parent_cluster > child_cluster) {
  769. small_cluster = child_cluster;
  770. big_cluster = parent_cluster;
  771. } else {
  772. big_cluster = child_cluster;
  773. small_cluster = parent_cluster;
  774. // flush child_cluster, because it has been modified
  775. child_cluster = small_cluster;
  776. }
  777. // update node_2_cluster_ map
  778. for (auto &node : graph_info_.clusters_[big_cluster]->nodes_) {
  779. graph_info_.node_2_cluster_[node] = graph_info_.clusters_[small_cluster];
  780. }
  781. // merge nodes
  782. graph_info_.clusters_[small_cluster]->nodes_.splice(graph_info_.clusters_[small_cluster]->nodes_.end(),
  783. graph_info_.clusters_[big_cluster]->nodes_);
  784. // merge all input & output to small cluster
  785. graph_info_.clusters_[small_cluster]->in_clu_.insert(graph_info_.clusters_[big_cluster]->in_clu_.begin(),
  786. graph_info_.clusters_[big_cluster]->in_clu_.end());
  787. graph_info_.clusters_[small_cluster]->out_clu_.insert(graph_info_.clusters_[big_cluster]->out_clu_.begin(),
  788. graph_info_.clusters_[big_cluster]->out_clu_.end());
  789. // remove child_cluster's out parent_cluster's in between child_cluster and parent_cluster
  790. RemoveEdge(parent_cluster, child_cluster_original);
  791. // update in/out of the cluster with bigger index
  792. for (auto in_clu : graph_info_.clusters_[big_cluster]->in_clu_) {
  793. graph_info_.clusters_[in_clu]->out_clu_.insert(small_cluster);
  794. graph_info_.clusters_[in_clu]->out_clu_.erase(big_cluster);
  795. }
  796. for (auto out_clu : graph_info_.clusters_[big_cluster]->out_clu_) {
  797. graph_info_.clusters_[out_clu]->in_clu_.insert(small_cluster);
  798. graph_info_.clusters_[out_clu]->in_clu_.erase(big_cluster);
  799. }
  800. graph_info_.clusters_[big_cluster] = graph_info_.clusters_[small_cluster];
  801. }
  802. void ge::GraphPartitioner::RemoveEdge(size_t parent_cluster, size_t child_cluster) {
  803. graph_info_.clusters_[child_cluster]->in_clu_.erase(parent_cluster);
  804. graph_info_.clusters_[parent_cluster]->out_clu_.erase(child_cluster);
  805. }
  806. void ge::GraphPartitioner::InsertEdge(size_t from, size_t to) {
  807. if (from == to) {
  808. return;
  809. }
  810. if (!graph_info_.clusters_[from]->out_clu_.insert(to).second) {
  811. // edge has already exists
  812. return;
  813. }
  814. graph_info_.clusters_[to]->in_clu_.insert(from);
  815. }
  816. void ge::GraphPartitioner::MarkClusters() {
  817. GELOGI("MarkClusters starts. cluster size is %zu", graph_info_.clusters_.size());
  818. size_t cluster_size = graph_info_.clusters_.size();
  819. for (size_t child_cluster = 0; child_cluster < cluster_size; child_cluster++) {
  820. auto found_child_cluster = graph_info_.clusters_[child_cluster];
  821. if (found_child_cluster == nullptr) {
  822. GELOGW("can not found child_cluster is %zu", child_cluster);
  823. continue;
  824. }
  825. auto copy_parents_clusters = found_child_cluster->in_clu_;
  826. vector<size_t> ordered_cluster;
  827. for (const auto &parent_cluster : copy_parents_clusters) {
  828. ordered_cluster.emplace_back(parent_cluster);
  829. }
  830. // sort cluster according to it's output amount
  831. auto comp_func = [this](const size_t &parent_cluster1, const size_t &parent_cluster2) -> bool {
  832. return graph_info_.clusters_[parent_cluster1]->out_clu_.size() <
  833. graph_info_.clusters_[parent_cluster2]->out_clu_.size();
  834. };
  835. std::sort(ordered_cluster.begin(), ordered_cluster.end(), comp_func);
  836. auto child_merged = child_cluster;
  837. for (const auto &parent_cluster : ordered_cluster) {
  838. if (IsMergeable(parent_cluster, child_merged, child_cluster)) {
  839. MergeTwoClusters(parent_cluster, child_merged);
  840. GELOGD("Merging cluster %zu and %zu to %zu", parent_cluster, child_cluster, child_merged);
  841. }
  842. }
  843. }
  844. GELOGD("MarkClusters ends.");
  845. }
  846. Status ge::GraphPartitioner::SplitSubGraphs(ge::ComputeGraphPtr compute_graph) {
  847. GELOGD("SplitSubGraphs starts.");
  848. if (compute_graph == nullptr) {
  849. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid");
  850. GELOGE(FAILED, "[Check][Param] parameter ptr is null.");
  851. return FAILED;
  852. }
  853. // Create graphs for all clusters
  854. std::unordered_set<ClusterPtr> cluster_set;
  855. // add pld&end
  856. for (auto &node : compute_graph->GetDirectNode()) {
  857. GELOGD("Node name is %s.", node->GetName().c_str());
  858. auto child_cluster = graph_info_.node_2_cluster_[node];
  859. ge::ComputeGraphPtr corresponding_graph;
  860. // unordered_set's insert returns a pair, second of pair is bool
  861. if (!cluster_set.insert(child_cluster).second) {
  862. GELOGD("Old sub graph, child_cluster is %zu", child_cluster->index_);
  863. corresponding_graph = graph_info_.cluster_2_partition_.at(child_cluster);
  864. } else {
  865. std::string graph_name = "new_sub_graph" + std::to_string(graph_info_.partitions_.size());
  866. ComputeGraphPtr new_sub_graph = MakeShared<ge::ComputeGraph>(graph_name);
  867. if (new_sub_graph == nullptr) {
  868. REPORT_CALL_ERROR("E19999", "allocate memory for ge::ComputeGraph failed.");
  869. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Allocate][Memory] for ge::ComputeGraph failed.");
  870. return FAILED;
  871. }
  872. AddNewGraphToPartition(new_sub_graph, child_cluster->engine_name_);
  873. corresponding_graph = new_sub_graph;
  874. graph_info_.cluster_2_partition_[child_cluster] = corresponding_graph;
  875. GELOGD("New sub graph, name is %s", graph_name.c_str());
  876. }
  877. // build node to corresponding node map
  878. NodePtr corresponding_node = corresponding_graph->AddNode(node->GetOpDesc());
  879. if (corresponding_node == nullptr) {
  880. REPORT_CALL_ERROR("E19999", "add node:%s in graph:%s failed",
  881. node->GetName().c_str(), corresponding_graph->GetName().c_str());
  882. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Add][Node] %s in graph:%s failed.",
  883. node->GetName().c_str(), corresponding_graph->GetName().c_str());
  884. return FAILED;
  885. }
  886. graph_info_.corresponding_node_in_partitions_[node] = corresponding_node;
  887. GE_CHK_STATUS_RET(corresponding_node->SetOwnerComputeGraph(corresponding_graph))
  888. for (const auto &in_anchor : node->GetAllInAnchors()) {
  889. GELOGD("In anchor index is %d", AnchorUtils::GetIdx(in_anchor));
  890. for (auto &peer_out_anchor : in_anchor->GetPeerAnchors()) {
  891. GELOGD("Peer out anchor index is %d", AnchorUtils::GetIdx(peer_out_anchor));
  892. // Normally, all nodes have a copy in corresponding_node_in_partitions_, so function at can not be exception
  893. auto iter = graph_info_.corresponding_node_in_partitions_.find(peer_out_anchor->GetOwnerNode());
  894. if (iter == graph_info_.corresponding_node_in_partitions_.end()) {
  895. REPORT_INNER_ERROR("E19999", "node[%s]id[%ld]'s parent_node[%s]id[%ld]"
  896. "should make corresponding in advance",
  897. node->GetOpDesc()->GetName().c_str(), node->GetOpDesc()->GetId(),
  898. peer_out_anchor->GetOwnerNode()->GetOpDesc()->GetName().c_str(),
  899. peer_out_anchor->GetOwnerNode()->GetOpDesc()->GetId());
  900. GELOGE(GRAPH_FAILED, "[Check][Param] node[%s]id[%ld]'s parent_node[%s]id[%ld]"
  901. "should make corresponding in advance",
  902. node->GetOpDesc()->GetName().c_str(), node->GetOpDesc()->GetId(),
  903. peer_out_anchor->GetOwnerNode()->GetOpDesc()->GetName().c_str(),
  904. peer_out_anchor->GetOwnerNode()->GetOpDesc()->GetId());
  905. return GRAPH_FAILED;
  906. }
  907. auto parent_node = iter->second;
  908. GE_CHECK_NOTNULL(parent_node);
  909. GELOGD("Parent node name is %s", parent_node->GetName().c_str());
  910. // add edge
  911. auto src_anchor = parent_node->GetOutAnchor(AnchorUtils::GetIdx(peer_out_anchor));
  912. auto dst_anchor = corresponding_node->GetInAnchor(AnchorUtils::GetIdx(in_anchor));
  913. // if child and parent's cluster is not same, add plc and end
  914. auto parent_cluster = graph_info_.node_2_cluster_[peer_out_anchor->GetOwnerNode()];
  915. if (parent_cluster != child_cluster) {
  916. GELOGD("Parent cluster is %zu, child_cluster is %zu", parent_cluster->index_, child_cluster->index_);
  917. if (AddPlaceHolderEnd(peer_out_anchor, in_anchor) != ge::SUCCESS) {
  918. GELOGE(GE_GRAPH_ADD_PLC_END_FAILED,
  919. "[Call][AddPlaceHolderEnd] failed, out_anchor:%s index:%d, in_anchor:%s index:%d.",
  920. peer_out_anchor->GetOwnerNode()->GetName().c_str(), AnchorUtils::GetIdx(peer_out_anchor),
  921. in_anchor->GetOwnerNode()->GetName().c_str(), AnchorUtils::GetIdx(in_anchor));
  922. return FAILED;
  923. }
  924. } else { // parent and child in the same cluster, add edge
  925. GELOGD("AddEdge from parent cluster %zu to child %zu", parent_cluster->index_, child_cluster->index_);
  926. if (GraphUtils::AddEdge(src_anchor, dst_anchor) != GRAPH_SUCCESS) {
  927. REPORT_CALL_ERROR("E19999", "add edge from %s to %s failed",
  928. peer_out_anchor->GetOwnerNode()->GetName().c_str(),
  929. in_anchor->GetOwnerNode()->GetName().c_str());
  930. GELOGE(GRAPH_FAILED, "[Add][Edge] from %s to %s failed", peer_out_anchor->GetOwnerNode()->GetName().c_str(),
  931. in_anchor->GetOwnerNode()->GetName().c_str());
  932. return FAILED;
  933. }
  934. }
  935. }
  936. }
  937. }
  938. GELOGD("SplitSubGraphs ends.");
  939. return SUCCESS;
  940. }
  941. /// before calling this function, the direct path between src and dst are already removed.
  942. /// return true if a second path is found
  943. bool ge::GraphPartitioner::HasSecondPath(size_t src, size_t dst, size_t upper_bound) {
  944. if (graph_info_.clusters_.at(src)->out_clu_.empty() || graph_info_.clusters_.at(dst)->in_clu_.empty()) {
  945. return false;
  946. }
  947. /// Avoid recursion since stack space might be limited.
  948. /// We instead keep a stack of nodes to visit.
  949. std::vector<size_t> temp_stack;
  950. std::set<size_t> visited;
  951. temp_stack.push_back(src);
  952. while (!temp_stack.empty()) {
  953. size_t cluster = temp_stack.back();
  954. temp_stack.pop_back();
  955. ClusterPtr cur_cluster = graph_info_.clusters_[cluster];
  956. if (!visited.insert(cluster).second) {
  957. continue;
  958. }
  959. for (auto out : cur_cluster->out_clu_) {
  960. if (out == dst) {
  961. return true; // There is cycle
  962. }
  963. if (out < upper_bound) {
  964. temp_stack.push_back(out);
  965. }
  966. }
  967. }
  968. return false;
  969. }
  970. Status ge::GraphPartitioner::Partition(ge::ComputeGraphPtr compute_graph, Mode mode) {
  971. ClearAllPartitionData();
  972. auto real_ret = SUCCESS;
  973. auto ret = PartitionSubGraph(compute_graph, mode);
  974. if (ret != SUCCESS) {
  975. GELOGE(ret, "[Partition][SubGraph] Failed, ret:%d", ret);
  976. real_ret = ret;
  977. }
  978. GE_CHECK_NOTNULL(compute_graph);
  979. // partition sub graph
  980. for (const auto &sub_graph : compute_graph->GetAllSubgraphs()) {
  981. ret = PartitionSubGraph(sub_graph, mode);
  982. if (ret != SUCCESS) {
  983. GELOGE(ret, "[Partition][SubGraph] Failed, ret:%d", ret);
  984. real_ret = ret;
  985. }
  986. }
  987. if (real_ret != SUCCESS) {
  988. auto root_graph = ge::GraphUtils::FindRootGraph(compute_graph);
  989. GE_CHECK_NOTNULL(root_graph);
  990. (void)Analyzer::GetInstance()->SaveAnalyzerDataToFile(root_graph->GetSessionID(),
  991. root_graph->GetGraphID());
  992. }
  993. return real_ret;
  994. }
  995. Status ge::GraphPartitioner::PartitionSubGraph(ge::ComputeGraphPtr compute_graph, Mode mode) {
  996. if (compute_graph == nullptr) {
  997. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid.");
  998. GELOGE(GE_GRAPH_NULL_INPUT, "[Check][Param] compute_graph is nullptr.");
  999. return FAILED;
  1000. }
  1001. // clear graph_info
  1002. graph_info_.ClearAllData(mode);
  1003. graph_info_.output_name_ = compute_graph->GetOutput();
  1004. graph_info_.output_size_ = compute_graph->GetOutputSize();
  1005. graph_info_.input_size_ = compute_graph->GetInputSize();
  1006. if (graph_info_.output_size_ == 0) {
  1007. REPORT_INNER_ERROR("E19999", "the output size of graph:%s is 0, check invalid.",
  1008. compute_graph->GetName().c_str());
  1009. GELOGE(GE_GRAPH_NULL_INPUT, "[Check][Param] The output size:0 of graph:%s need to be greater than 0.",
  1010. compute_graph->GetName().c_str());
  1011. return FAILED;
  1012. }
  1013. GELOGI("Graph Partition starts, graph nodes size is %zu", compute_graph->GetDirectNodesSize());
  1014. Status ret = compute_graph->TopologicalSorting();
  1015. if (ret != SUCCESS) {
  1016. REPORT_CALL_ERROR("E19999", "TopologicalSorting for graph:%s failed",
  1017. compute_graph->GetName().c_str());
  1018. GELOGE(GE_GRAPH_TOPO_SORT_FAILED, "[Call][TopologicalSorting] for subGraph:%s failed",
  1019. compute_graph->GetName().c_str());
  1020. return FAILED;
  1021. }
  1022. GE_TIMESTAMP_START(PartitionSubGraphInitialize);
  1023. if (Initialize(compute_graph) != SUCCESS) {
  1024. GELOGE(GE_GRAPH_INIT_FAILED, "[Call][Initialize] for graph:%s failed", compute_graph->GetName().c_str());
  1025. return FAILED;
  1026. }
  1027. GE_TIMESTAMP_END(PartitionSubGraphInitialize, "GraphPartitioner::PartitionInitialize");
  1028. GE_TIMESTAMP_START(PartitionSubGraphMarkClusters);
  1029. MarkClusters();
  1030. GE_TIMESTAMP_END(PartitionSubGraphMarkClusters, "GraphPartitioner::PartitionMarkClusters");
  1031. GE_TIMESTAMP_START(PartitionSubGraphSplitSubGraphs);
  1032. if (SplitSubGraphs(compute_graph) != SUCCESS) {
  1033. GELOGE(FAILED, "[Split][SubGraphs] for graph:%s failed", compute_graph->GetName().c_str());
  1034. return FAILED;
  1035. }
  1036. GE_TIMESTAMP_END(PartitionSubGraphSplitSubGraphs, "GraphPartitioner::PartitionSplitSubGraphs");
  1037. GE_TIMESTAMP_START(PartitionSubGraphSortSubGraphs);
  1038. if (SortSubGraphs(compute_graph) != ge::SUCCESS) {
  1039. GELOGE(GE_GRAPH_TOPO_SORT_FAILED, "[Sort][SubGraphs] for graph:%s failed.",
  1040. compute_graph->GetName().c_str());
  1041. return ge::FAILED;
  1042. }
  1043. GE_TIMESTAMP_END(PartitionSubGraphSortSubGraphs, "GraphPartitioner::PartitionSortSubGraphs");
  1044. GE_TIMESTAMP_START(PartitionSubGraphAddPartitionsToGraphNode);
  1045. vector<ge::SubGraphInfoPtr> output_subgraphs;
  1046. if (AddPartitionsToGraphNode(output_subgraphs, compute_graph) != ge::SUCCESS) {
  1047. GELOGE(GE_GRAPH_EMPTY_PARTITION, "[Add][Partitions] To GraphNode failed, graph:%s.",
  1048. compute_graph->GetName().c_str());
  1049. return ge::FAILED;
  1050. }
  1051. GE_TIMESTAMP_END(PartitionSubGraphAddPartitionsToGraphNode, "GraphPartitioner::PartitionAddPartitionsToGraphNode");
  1052. GELOGI("Graph Partition ends. Adding partitions to SubGraphInfo, got %zu sub graphs", output_subgraphs.size());
  1053. graph_info_.mode_ = kMerging;
  1054. // do not care over flow
  1055. partition_times_++;
  1056. graph_2_graph_partition_info_[compute_graph] = graph_info_;
  1057. graph_2_subgraph_list_[compute_graph] = output_subgraphs;
  1058. return SUCCESS;
  1059. }
  1060. // all the inputs are the nodes and anchors in the original graph
  1061. Status ge::GraphPartitioner::AddPlaceHolderEnd(const AnchorPtr &out_anchor, const AnchorPtr &in_anchor) {
  1062. if ((out_anchor == nullptr) || (in_anchor == nullptr)) {
  1063. REPORT_INNER_ERROR("E19999", "Param out_anchor or in_anchor is nullptr, check invalid.");
  1064. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Check][Param] out_anchor or in_anchor is nullptr.");
  1065. return FAILED;
  1066. }
  1067. // nodes in original graph
  1068. const auto &src_node = out_anchor->GetOwnerNode();
  1069. const auto &dst_node = in_anchor->GetOwnerNode();
  1070. if ((src_node == nullptr) || (dst_node == nullptr)) {
  1071. REPORT_INNER_ERROR("E19999", "in_anchor'node or out_anchor'node is nullptr. check invalid.");
  1072. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Check][Param] src_node or dst_node is nullptr.");
  1073. return FAILED;
  1074. }
  1075. // All nodes have a copy in corresponding_node_in_partitions_, so function at can not be execption
  1076. auto src_anchor =
  1077. graph_info_.corresponding_node_in_partitions_.at(src_node)->GetOutAnchor(AnchorUtils::GetIdx(out_anchor));
  1078. auto dst_anchor =
  1079. graph_info_.corresponding_node_in_partitions_.at(dst_node)->GetInAnchor(AnchorUtils::GetIdx(in_anchor));
  1080. if ((src_anchor == nullptr) || (dst_anchor == nullptr)) {
  1081. REPORT_INNER_ERROR("E19999", "src_anchor(index:%d) or dst_anchor(index:%d) is nullptr.",
  1082. AnchorUtils::GetIdx(out_anchor), AnchorUtils::GetIdx(in_anchor));
  1083. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Check][Param] src_anchor(index:%d) or dst_anchor(index:%d) is nullptr.",
  1084. AnchorUtils::GetIdx(out_anchor), AnchorUtils::GetIdx(in_anchor));
  1085. return FAILED;
  1086. }
  1087. // anchors in subGraph
  1088. const ComputeGraphPtr &src_subgraph = src_anchor->GetOwnerNode()->GetOwnerComputeGraph();
  1089. const ComputeGraphPtr &dst_subgraph = dst_anchor->GetOwnerNode()->GetOwnerComputeGraph();
  1090. // add end and pld node
  1091. auto ret = AddPlaceHolderEndInSrcDstGraph(src_anchor, dst_anchor, dst_subgraph, src_subgraph);
  1092. if (ret != SUCCESS) {
  1093. GELOGE(GE_GRAPH_ADD_PLC_END_FAILED, "[Call][AddPlaceHolderEndInSrcDstGraph] failed, ret:%d.", ret);
  1094. return ret;
  1095. }
  1096. return SUCCESS;
  1097. }
  1098. Status ge::GraphPartitioner::SortSubGraphs(const ge::ComputeGraphPtr &compute_graph) {
  1099. uint32_t rank = kRankOne; // rank 0 for data graph
  1100. ComputeGraphPtr new_input_nodes_sub_graph = MakeShared<ComputeGraph>("inputNodeGraph");
  1101. if ((new_input_nodes_sub_graph == nullptr) || (compute_graph == nullptr)) {
  1102. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr or Allocate Memory for ComputeGraph failed.");
  1103. GELOGE(FAILED, "[Check][Param] new_input_nodes_sub_graph or compute_graph is nullptr.");
  1104. return FAILED;
  1105. }
  1106. for (const auto &node : compute_graph->GetDirectNode()) {
  1107. // All nodes in original graph have a copy in corresponding_node_in_partitions_, so it can not be null
  1108. auto sub_graph = graph_info_.corresponding_node_in_partitions_.at(node)->GetOwnerComputeGraph();
  1109. if ((graph_info_.partitions_2_rank_.find(sub_graph) == graph_info_.partitions_2_rank_.end()) &&
  1110. (graph_info_.partitions_[sub_graph] != kEngineDefaultData)) {
  1111. graph_info_.partitions_2_rank_[sub_graph] = rank;
  1112. graph_info_.rank_2_partitions_.push_back(sub_graph);
  1113. rank++;
  1114. } else if (graph_info_.partitions_[sub_graph] == kEngineDefaultData) { // merge data graph
  1115. if (PutInputNodesInSubGraph(sub_graph, new_input_nodes_sub_graph) != SUCCESS) {
  1116. GELOGE(FAILED, "[Call][putInputNodesInSubGraph] failed.");
  1117. return FAILED;
  1118. }
  1119. auto to_be_del = graph_info_.partitions_.find(sub_graph);
  1120. graph_info_.partitions_.erase(to_be_del);
  1121. }
  1122. }
  1123. if (!new_input_nodes_sub_graph->GetDirectNode().empty()) {
  1124. graph_info_.rank_2_partitions_.insert(graph_info_.rank_2_partitions_.begin(), new_input_nodes_sub_graph);
  1125. graph_info_.partitions_2_rank_[new_input_nodes_sub_graph] = 0;
  1126. AddNewGraphToPartition(new_input_nodes_sub_graph, "inputNodesSubGraph");
  1127. }
  1128. // reinit rank
  1129. rank = kRankZero;
  1130. for (const auto &it : graph_info_.rank_2_partitions_) {
  1131. // rename subGraph based on rank
  1132. if (it != nullptr) {
  1133. // rename subGraph based on rank
  1134. string graph_name =
  1135. "partition" + std::to_string(partition_times_) + "_rank" + std::to_string(rank) + "_" + it->GetName();
  1136. it->SetName(graph_name);
  1137. }
  1138. rank++;
  1139. }
  1140. return SUCCESS;
  1141. }
  1142. AnchorPtr ge::GraphPartitioner::GetEndInAnchor(const AnchorPtr &src_anchor, const NodePtr &end_node) {
  1143. if ((src_anchor == nullptr) || (end_node == nullptr)) {
  1144. REPORT_INNER_ERROR("E19999", "Param src_anchor or end_node is nullptr, check invalid.");
  1145. GELOGE(FAILED, "[Check][Param] parameter src_anchor or end_node is nullptr.");
  1146. return nullptr;
  1147. }
  1148. AnchorPtr end_in_anchor;
  1149. if (Anchor::DynamicAnchorCast<OutDataAnchor>(src_anchor) != nullptr) {
  1150. end_in_anchor = end_node->GetInDataAnchor(0);
  1151. } else {
  1152. end_in_anchor = end_node->GetInControlAnchor();
  1153. }
  1154. return end_in_anchor;
  1155. }
  1156. AnchorPtr ge::GraphPartitioner::GetPldOutAnchor(const NodePtr &pld_node, const AnchorPtr &dst_anchor) {
  1157. if ((pld_node == nullptr) || (dst_anchor == nullptr)) {
  1158. REPORT_INNER_ERROR("E19999", "Param pld_node or dst_anchor is nullptr, check invalid.");
  1159. GELOGE(FAILED, "[Check][Param] parameter pld_node or dst_anchor is nullptr.");
  1160. return nullptr;
  1161. }
  1162. AnchorPtr pld_out_anchor;
  1163. if (Anchor::DynamicAnchorCast<InDataAnchor>(dst_anchor) != nullptr) {
  1164. pld_out_anchor = pld_node->GetOutDataAnchor(0);
  1165. } else {
  1166. pld_out_anchor = pld_node->GetOutControlAnchor();
  1167. }
  1168. return pld_out_anchor;
  1169. }
  1170. void ge::GraphPartitioner::AddEndPldInformationToSubGraphInfo(ge::SubGraphInfoPtr &subgraph_info) {
  1171. if (subgraph_info == nullptr) {
  1172. GELOGE(FAILED, "[Check][Param] parameter subgraph_info is nullptr.");
  1173. return;
  1174. }
  1175. auto subgraph = subgraph_info->GetSubGraph();
  1176. GE_CHECK_NOTNULL_JUST_RETURN(subgraph);
  1177. NodetoNodeMap end_map;
  1178. NodetoNodeMap pld_map;
  1179. for (const auto &node : subgraph->GetDirectNode()) {
  1180. if (node->GetType() == kEndType) {
  1181. end_map[node] = graph_info_.end_2_pld_.at(node);
  1182. }
  1183. if (node->GetType() == kPlaceHolderType) {
  1184. pld_map[node] = graph_info_.pld_2_end_.at(node);
  1185. }
  1186. }
  1187. subgraph_info->SetEnd2PldMap(end_map);
  1188. subgraph_info->SetPld2EndMap(pld_map);
  1189. }
  1190. const Graph2SubGraphInfoList &ge::GraphPartitioner::GetSubGraphMap() { return graph_2_subgraph_list_; }
  1191. void ge::GraphPartitioner::ClearAllPartitionData() {
  1192. graph_2_graph_partition_info_.clear();
  1193. graph_2_subgraph_list_.clear();
  1194. graph_2_input_subgraph_.clear();
  1195. GELOGD("Clear all partition data success.");
  1196. return;
  1197. }
  1198. } // namespace ge

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