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
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
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
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
5 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
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
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
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
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
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  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. GE_CHECK_NOTNULL(end_op_desc);
  357. GE_IF_BOOL_EXEC(!AttrUtils::SetInt(end_op_desc, "peerIndex", graph_info_.num_of_pld_end_),
  358. GELOGW("SetInt peerIndex failed");)
  359. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(end_op_desc, "parentOpType", dst_node->GetType()),
  360. GELOGW("SetStr parentOpType failed");)
  361. GE_IF_BOOL_EXEC(!end_op_desc->SetExtAttr("parentNode", dst_node),
  362. GELOGW("SetEndExtAttr parentNode failed");)
  363. OpDescPtr dst_node_op_desc = dst_node->GetOpDesc();
  364. GE_CHECK_NOTNULL(dst_node_op_desc);
  365. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(end_op_desc, ATTR_NAME_END_REAR_NODE_ENGINE_NAME,
  366. dst_node_op_desc->GetOpEngineName()), GELOGW("SetStr rearNodeEngineName failed");)
  367. // replace input_desc of end with owner node's desc
  368. int output_index = ge::AnchorUtils::GetIdx(out_anchor);
  369. bool is_need_update_desc = (output_index >= 0) && (graph_info_.mode_ == kPartitioning);
  370. if (is_need_update_desc) {
  371. if (UpdateEndOpDesc(src_node, output_index, end_op_desc) != SUCCESS) {
  372. GELOGE(GRAPH_PARAM_INVALID, "[Update][EndOpDesc] failed, input index:%d, end_op_desc:%s",
  373. output_index, end_op_desc->GetName().c_str());
  374. return FAILED;
  375. }
  376. } else {
  377. GeTensorDesc input_desc;
  378. if (end_op_desc->AddInputDesc(input_desc) != SUCCESS) {
  379. REPORT_CALL_ERROR("E19999", "add input desc to op:%s failed, input index:%d",
  380. end_op_desc->GetName().c_str(), output_index);
  381. GELOGE(GRAPH_PARAM_INVALID, "[Add][InputDesc] to op:%s failed, input index %d",
  382. end_op_desc->GetName().c_str(), output_index);
  383. return FAILED;
  384. }
  385. }
  386. NodePtr new_end_node = end_graph->AddNode(end_op_desc);
  387. if (new_end_node == nullptr) {
  388. REPORT_CALL_ERROR("E19999", "add node:%s in graph:%s failed",
  389. end_op_desc->GetName().c_str(), end_graph->GetName().c_str());
  390. GELOGE(GRAPH_PARAM_INVALID, "[Add][Node] %s in graph:%s failed.",
  391. end_op_desc->GetName().c_str(), end_graph->GetName().c_str());
  392. return FAILED;
  393. }
  394. GE_IF_BOOL_EXEC(new_end_node->SetOwnerComputeGraph(end_graph) != GRAPH_SUCCESS,
  395. REPORT_CALL_ERROR("E19999", "SetOwnerComputeGraph %s for node:%s failed",
  396. end_graph->GetName().c_str(), new_end_node->GetName().c_str());
  397. GELOGE(GRAPH_PARAM_INVALID, "[Set][OwnerComputeGraph] %s for node:%s failed",
  398. end_graph->GetName().c_str(), new_end_node->GetName().c_str());
  399. return FAILED;)
  400. AnchorPtr end_dst_anchor = GetEndInAnchor(out_anchor, new_end_node);
  401. if (GraphUtils::AddEdge(out_anchor, end_dst_anchor) != GRAPH_SUCCESS) {
  402. REPORT_CALL_ERROR("E19999", "add edge from %s to %s failed", out_anchor->GetOwnerNode()->GetName().c_str(),
  403. end_dst_anchor->GetOwnerNode()->GetName().c_str());
  404. GELOGE(GE_GRAPH_ADD_PLC_END_FAILED, "[Add][Edge] from %s to %s failed",
  405. out_anchor->GetOwnerNode()->GetName().c_str(), end_dst_anchor->GetOwnerNode()->GetName().c_str());
  406. return FAILED;
  407. }
  408. /// For fe, op id has been set in AddNode,
  409. /// we can take op id of srcNode as the mark of parentId now
  410. const auto &src_node_opdesc = src_node->GetOpDesc();
  411. GE_CHECK_NOTNULL(src_node_opdesc);
  412. int64_t node_id = src_node_opdesc->GetId();
  413. const string pld_name = kPlaceHolderType + std::to_string(graph_info_.num_of_pld_end_);
  414. auto pld_op_desc = MakeShared<OpDesc>(pld_graph->GetName() + "_" + pld_name, PLACEHOLDER);
  415. GE_CHECK_NOTNULL(pld_op_desc);
  416. GE_IF_BOOL_EXEC(!AttrUtils::SetInt(pld_op_desc, "peerIndex", graph_info_.num_of_pld_end_),
  417. GELOGW("SetInt peerIndex failed");)
  418. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(pld_op_desc, "_peerNodeName", new_end_node->GetName()),
  419. GELOGW("SetStr _peerNodeName failed");)
  420. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(pld_op_desc, "parentOpType", src_node->GetType()),
  421. GELOGW("SetStr parentOpType failed");)
  422. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(pld_op_desc, "_parentNodeName", src_node->GetName()),
  423. GELOGW("SetStr parentOpName failed");)
  424. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(pld_op_desc, "parentId", end_graph->GetName() + ":" + std::to_string(node_id)),
  425. GELOGW("SetStr parentId failed");)
  426. GE_IF_BOOL_EXEC(!AttrUtils::SetInt(pld_op_desc, "anchorIndex", AnchorUtils::GetIdx(out_anchor)),
  427. GELOGW("SetInt anchorIndex failed");)
  428. GE_IF_BOOL_EXEC(!pld_op_desc->SetExtAttr("parentNode", src_node),
  429. GELOGW("SetPldExtAttr parentNode failed");)
  430. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(pld_op_desc, ATTR_NAME_PLD_FRONT_NODE_ENGINE_NAME,
  431. src_node_opdesc->GetOpEngineName()), GELOGW("SetStr frontNodeEngineName failed");)
  432. std::string l2_info_attr;
  433. if (AttrUtils::GetStr(src_node_opdesc, "_task_L2FusionInfo", l2_info_attr)) {
  434. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(pld_op_desc, "_task_L2FusionInfo", l2_info_attr),
  435. GELOGW("SetStr l2_info_attr failed");)
  436. }
  437. int64_t anchor_index_for_lxfusion;
  438. if (AttrUtils::GetInt(src_node_opdesc, "_data_anchor_index_for_lxfusion", anchor_index_for_lxfusion)) {
  439. GE_IF_BOOL_EXEC(!AttrUtils::SetInt(pld_op_desc, "_data_anchor_index_for_lxfusion", anchor_index_for_lxfusion),
  440. GELOGW("SetInt anchor_index_for_lxfusion failed");)
  441. }
  442. // do not care over flow
  443. graph_info_.num_of_pld_end_++;
  444. // replace output_desc of pld with input node's output desc
  445. int input_index = ge::AnchorUtils::GetIdx(peer_in_anchor);
  446. is_need_update_desc = (input_index >= 0) && (graph_info_.mode_ == kPartitioning);
  447. if (is_need_update_desc) {
  448. if (UpdatePldOpDesc(dst_node, input_index, pld_op_desc) != SUCCESS) {
  449. GELOGE(GRAPH_PARAM_INVALID, "[Update][PldOpDesc] failed, output index:%d, pld_op_desc:%s",
  450. input_index, pld_op_desc->GetName().c_str());
  451. return FAILED;
  452. }
  453. } else {
  454. GeTensorDesc output_desc;
  455. if (pld_op_desc->AddOutputDesc(output_desc) != SUCCESS) {
  456. REPORT_CALL_ERROR("E19999", "AddOutputDesc to op:%s failed, input index %d",
  457. pld_op_desc->GetName().c_str(), input_index);
  458. GELOGE(GRAPH_PARAM_INVALID, "[Add][OutputDesc] to op:%s failed, input index %d",
  459. pld_op_desc->GetName().c_str(), input_index);
  460. return FAILED;
  461. }
  462. }
  463. NodePtr new_pld_node = pld_graph->AddNode(pld_op_desc);
  464. if (new_pld_node == nullptr) {
  465. REPORT_CALL_ERROR("E19999", "AddNode %s in graph:%s failed.",
  466. pld_op_desc->GetName().c_str(), pld_graph->GetName().c_str());
  467. GELOGE(GRAPH_PARAM_INVALID, "[Add][Node] %s in graph:%s failed.",
  468. pld_op_desc->GetName().c_str(), pld_graph->GetName().c_str());
  469. return FAILED;
  470. }
  471. GE_IF_BOOL_EXEC(new_pld_node->SetOwnerComputeGraph(pld_graph) != GRAPH_SUCCESS,
  472. REPORT_CALL_ERROR("E19999", "SetOwnerComputeGraph for node:%s failed, graph:%s",
  473. new_pld_node->GetName().c_str(), pld_graph->GetName().c_str());
  474. GELOGE(GRAPH_PARAM_INVALID, "[Set][OwnerComputeGraph] for node:%s failed, graph:%s",
  475. new_pld_node->GetName().c_str(), pld_graph->GetName().c_str());
  476. return FAILED;)
  477. AnchorPtr pld_src_anchor = GetPldOutAnchor(new_pld_node, peer_in_anchor);
  478. // link placeHolder -> computeNode
  479. if (GraphUtils::AddEdge(pld_src_anchor, peer_in_anchor) != GRAPH_SUCCESS) {
  480. REPORT_CALL_ERROR("E19999", "AddEdge from %s to %s failed",
  481. pld_src_anchor->GetOwnerNode()->GetName().c_str(),
  482. peer_in_anchor->GetOwnerNode()->GetName().c_str());
  483. GELOGE(GE_GRAPH_ADD_PLC_END_FAILED, "[Add][Edge] from %s to %s failed",
  484. pld_src_anchor->GetOwnerNode()->GetName().c_str(), peer_in_anchor->GetOwnerNode()->GetName().c_str());
  485. return FAILED;
  486. }
  487. graph_info_.index_2_end_[graph_info_.num_of_pld_end_] = new_end_node;
  488. graph_info_.pld_2_end_[new_pld_node] = new_end_node;
  489. graph_info_.end_2_pld_[new_end_node] = new_pld_node;
  490. return SUCCESS;
  491. }
  492. Status ge::GraphPartitioner::LinkInput2EndRemoveOrginalLink(ge::NodePtr input_node, ge::ComputeGraphPtr src_graph,
  493. ge::ComputeGraphPtr dst_graph) {
  494. if ((input_node == nullptr) || (src_graph == nullptr) || (dst_graph == nullptr)) {
  495. REPORT_INNER_ERROR("E19999", "Param input_node or src_graph or dst_graph is nullptr, check invalid.");
  496. GELOGE(FAILED, "[Check][Param] parameter input_node or src_graph or dst_graph is nullptr.");
  497. return FAILED;
  498. }
  499. // get the original anchors and remove the original link
  500. for (const auto &out_data_anchor : input_node->GetAllOutAnchors()) {
  501. for (auto &peer_in_anchor : out_data_anchor->GetPeerAnchors()) {
  502. if (peer_in_anchor->GetOwnerNode()->GetType() != kEndType) {
  503. if (GraphUtils::RemoveEdge(out_data_anchor, peer_in_anchor) != GRAPH_SUCCESS) {
  504. REPORT_CALL_ERROR("E19999", "RemoveEdge between %s and %s failed.",
  505. out_data_anchor->GetOwnerNode()->GetName().c_str(),
  506. peer_in_anchor->GetOwnerNode()->GetName().c_str());
  507. GELOGE(FAILED, "[Remove][Edge] between %s and %s failed.",
  508. out_data_anchor->GetOwnerNode()->GetName().c_str(), peer_in_anchor->GetOwnerNode()->GetName().c_str());
  509. return FAILED;
  510. }
  511. // link input -> end
  512. auto ret = AddPlaceHolderEndInSrcDstGraph(out_data_anchor, peer_in_anchor, src_graph, dst_graph);
  513. if (ret != SUCCESS) {
  514. GELOGE(GE_GRAPH_ADD_PLC_END_FAILED, "[Call][AddPlaceHolderEndInSrcDstGraph] failed, ret:%d.", ret);
  515. return ret;
  516. }
  517. } else {
  518. auto end_node = peer_in_anchor->GetOwnerNode();
  519. if (GraphUtils::RemoveJustNode(src_graph, end_node) != GRAPH_SUCCESS) {
  520. REPORT_CALL_ERROR("E19999", "RemoveJustNode %s from graph:%s failed.",
  521. end_node->GetName().c_str(), src_graph->GetName().c_str());
  522. GELOGE(FAILED, "[Remove][JustNode] %s from graph:%s failed.",
  523. end_node->GetName().c_str(), src_graph->GetName().c_str());
  524. return FAILED;
  525. }
  526. if (end_node->SetOwnerComputeGraph(dst_graph) != GRAPH_SUCCESS) {
  527. REPORT_CALL_ERROR("E19999", "SetOwnerComputeGraph for node:%s failed, graph:%s.",
  528. end_node->GetName().c_str(), dst_graph->GetName().c_str());
  529. GELOGE(FAILED, "[Set][OwnerComputeGraph] to node:%s failed, graph:%s.",
  530. end_node->GetName().c_str(), dst_graph->GetName().c_str());
  531. return FAILED;
  532. }
  533. if (dst_graph->AddNode(end_node) == nullptr) {
  534. REPORT_CALL_ERROR("E19999", "AddNode %s in graph:%s failed.",
  535. end_node->GetName().c_str(), dst_graph->GetName().c_str());
  536. GELOGE(FAILED, "[Add][Node] %s in graph:%s failed.",
  537. end_node->GetName().c_str(), dst_graph->GetName().c_str());
  538. return FAILED;
  539. }
  540. }
  541. }
  542. }
  543. return SUCCESS;
  544. }
  545. Status ge::GraphPartitioner::PutInputNodesInSubGraph(const ge::ComputeGraphPtr &src_graph,
  546. const ge::ComputeGraphPtr &dst_graph) {
  547. if ((src_graph == nullptr) || (dst_graph == nullptr)) {
  548. REPORT_INNER_ERROR("E19999", "Param src_graph or dst_graph is nullptr, check invalid.");
  549. GELOGE(FAILED, "[Check][Param] parameter src_graph or dst_graph is nullptr.");
  550. return FAILED;
  551. }
  552. for (auto &input_node : src_graph->GetDirectNode()) {
  553. if (IsDataLike(input_node)) {
  554. if (input_node->SetOwnerComputeGraph(dst_graph) != GRAPH_SUCCESS) {
  555. REPORT_CALL_ERROR("E19999", "SetOwnerComputeGraph for node:%s failed, graph:%s.",
  556. input_node->GetName().c_str(), dst_graph->GetName().c_str());
  557. GELOGE(FAILED, "[Set][OwnerComputeGraph] for node:%s failed, graph:%s.",
  558. input_node->GetName().c_str(), dst_graph->GetName().c_str());
  559. return FAILED;
  560. }
  561. // remove input node from src_graph
  562. if (GraphUtils::RemoveJustNode(src_graph, input_node) != GRAPH_SUCCESS) {
  563. REPORT_CALL_ERROR("E19999", "RemoveJustNode %s from graph:%s failed.",
  564. input_node->GetName().c_str(), src_graph->GetName().c_str());
  565. GELOGE(FAILED, "[Remove][JustNode] %s from graph:%s failed.",
  566. input_node->GetName().c_str(), src_graph->GetName().c_str());
  567. return FAILED;
  568. }
  569. // add input node to dst_graph
  570. if (dst_graph->AddNode(input_node) == nullptr) {
  571. REPORT_CALL_ERROR("E19999", "AddNode %s in graph:%s failed.",
  572. input_node->GetName().c_str(), src_graph->GetName().c_str());
  573. GELOGE(FAILED, "[Add][Node] %s in graph:%s failed.",
  574. input_node->GetName().c_str(), src_graph->GetName().c_str());
  575. return FAILED;
  576. }
  577. if (LinkInput2EndRemoveOrginalLink(input_node, src_graph, dst_graph) != ge::SUCCESS) {
  578. GELOGE(FAILED, "[Call][LinkInput2EndRemoveOrginalLink] failed.");
  579. return FAILED;
  580. }
  581. }
  582. }
  583. return SUCCESS;
  584. }
  585. void ge::GraphPartitioner::AddNewGraphToPartition(ge::ComputeGraphPtr &input_graph, const std::string &engine_name) {
  586. if (input_graph == nullptr) {
  587. GELOGW("[GraphPartitioner]: input_graph is null, engine name is %s", engine_name.c_str());
  588. return;
  589. }
  590. graph_info_.partitions_[input_graph] = engine_name;
  591. }
  592. bool ge::GraphPartitioner::IsDataLike(ge::NodePtr node) {
  593. return (node->GetType() == CONSTANT) || (node->GetType() == DATA) || (node->GetType() == AIPPDATA) ||
  594. (node->GetType() == CONSTANTOP) || (node->GetType() == VARIABLE);
  595. }
  596. bool ge::GraphPartitioner::HasNoInput(ge::NodePtr node) {
  597. if (node == nullptr) {
  598. GELOGE(FAILED, "[Check][Param] node is nullptr.");
  599. return true;
  600. }
  601. return node->GetInNodes().empty();
  602. }
  603. Status ge::GraphPartitioner::Initialize(ge::ComputeGraphPtr compute_graph) {
  604. GELOGI("Initialize starts.");
  605. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  606. if (instance_ptr == nullptr || compute_graph == nullptr) {
  607. REPORT_INNER_ERROR("E19999", "compute_graph or instance_ptr of GELib is nullptr, check invalid.");
  608. GELOGE(GE_GRAPH_NOT_INIT, "[Check][Param] compute_graph or instance_ptr of GELib is nullptr.");
  609. return FAILED;
  610. }
  611. graph_info_.engine_placer_.SetComputeGraph(compute_graph);
  612. if (graph_info_.engine_placer_.Run() != SUCCESS) {
  613. GELOGE(FAILED, "[Call][Run] Engine placer run failed, graph:%s.", compute_graph->GetName().c_str());
  614. return FAILED;
  615. }
  616. const NodeEngineMap *node_engine_map = graph_info_.engine_placer_.GetNodeEngineMap();
  617. size_t temp_index = 0;
  618. // travese nodes by topo order one by one
  619. for (const auto &node : compute_graph->GetDirectNode()) {
  620. std::string temp_stream;
  621. // node opdesc has been checked before
  622. (void)AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, temp_stream);
  623. ClusterPtr new_cluster;
  624. // data like node without input should be handle specific
  625. if (HasNoInput(node) && IsDataLike(node)) {
  626. ClusterPtr cluster = MakeShared<Cluster>(temp_index, kEngineDefaultData, temp_stream);
  627. new_cluster = cluster;
  628. } else {
  629. if (node_engine_map->count(node) == 0) {
  630. REPORT_INNER_ERROR("E19999", "node:%s not find in node_engine_map", node->GetName().c_str());
  631. GELOGE(FAILED, "[Check][Param] node[%s] does not owner engine!", node->GetName().c_str());
  632. return FAILED;
  633. }
  634. ClusterPtr cluster = MakeShared<Cluster>(temp_index, node_engine_map->at(node), temp_stream);
  635. new_cluster = cluster;
  636. }
  637. if (new_cluster == nullptr) {
  638. REPORT_CALL_ERROR("E19999", "Allocate Cluster failed, index:%zu", temp_index);
  639. GELOGE(FAILED, "[Allocate][Cluster] failed, index:%zu", temp_index);
  640. return FAILED;
  641. }
  642. new_cluster->nodes_.push_back(node);
  643. if (!HasNoInput(node)) {
  644. auto node_id = node->GetOpDesc()->GetId();
  645. for (const auto &parent : node->GetInAllNodes()) {
  646. auto parent_id = parent->GetOpDesc()->GetId();
  647. if (parent_id < node_id) {
  648. auto iter = graph_info_.node_2_cluster_.find(parent);
  649. if (iter == graph_info_.node_2_cluster_.end()) {
  650. REPORT_INNER_ERROR("E19999", "node[%s]id[%ld]'s parent_node[%s]id[%ld] should make cluster in advance",
  651. node->GetOpDesc()->GetName().c_str(), node_id,
  652. parent->GetOpDesc()->GetName().c_str(), parent_id);
  653. GELOGE(FAILED, "[Check][Param] node[%s]id[%ld]'s parent_node[%s]id[%ld] should make cluster in advance",
  654. node->GetOpDesc()->GetName().c_str(), node_id, parent->GetOpDesc()->GetName().c_str(), parent_id);
  655. return FAILED;
  656. }
  657. new_cluster->in_clu_.insert(iter->second->index_);
  658. iter->second->out_clu_.insert(temp_index);
  659. }
  660. }
  661. }
  662. graph_info_.node_2_cluster_[node] = new_cluster;
  663. graph_info_.clusters_[temp_index] = new_cluster;
  664. GELOGD("Node name is %s, engine is %s, cluster index is %zu, stream label is %s", node->GetName().c_str(),
  665. new_cluster->engine_name_.c_str(), new_cluster->index_, new_cluster->stream_label_.c_str());
  666. temp_index++;
  667. }
  668. GELOGD("Initialize ends.");
  669. return SUCCESS;
  670. }
  671. Status ge::GraphPartitioner::AddPartitionsToGraphNode(vector<ge::SubGraphInfoPtr> &output_subgraphs,
  672. ge::ComputeGraphPtr compute_graph) {
  673. const std::string &input_subgraph_name = "inputNodesSubGraph";
  674. string session_graph_id;
  675. if (!AttrUtils::GetStr(*compute_graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id)) {
  676. GELOGW("Get graph session_graph_id attr failed.");
  677. return INTERNAL_ERROR;
  678. }
  679. // the output_subgraphs have topological order
  680. for (const auto &sub_graph : graph_info_.rank_2_partitions_) {
  681. if (graph_info_.partitions_.find(sub_graph) == graph_info_.partitions_.end()) {
  682. REPORT_INNER_ERROR("E19999", "partition is null, subgraph:%s", sub_graph->GetName().c_str());
  683. GELOGE(GE_GRAPH_EMPTY_PARTITION, "[Check][Param] partition is null, subgraph:%s", sub_graph->GetName().c_str());
  684. return FAILED;
  685. }
  686. auto &engine_name = graph_info_.partitions_.at(sub_graph);
  687. (void)AttrUtils::SetStr(sub_graph, ATTR_NAME_PARENT_GRAPH_NAME, compute_graph->GetName());
  688. GELOGD("set attr success. subgraph(%s) with parent graph(%s)", sub_graph->GetName().c_str(),
  689. compute_graph->GetName().c_str());
  690. GE_DUMP(sub_graph, sub_graph->GetName() + "_" + mode_2_str_[graph_info_.mode_]);
  691. if (!session_graph_id.empty()) {
  692. GE_IF_BOOL_EXEC(!AttrUtils::SetStr(sub_graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id),
  693. GELOGW("SetStr ATTR_NAME_SESSION_GRAPH_ID failed");)
  694. }
  695. // flush parent node of subgraph
  696. sub_graph->SetParentNode(compute_graph->GetParentNode());
  697. auto sgi = MakeShared<SubGraphInfo>();
  698. if (sgi == nullptr) {
  699. REPORT_CALL_ERROR("E19999", "allocate memory for SubGraphInfo failed.");
  700. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Allocate][Memory] for SubGraphInfo failed.");
  701. return FAILED;
  702. }
  703. // set engine name
  704. sgi->SetEngineName(engine_name);
  705. // set stream label
  706. string sub_graph_stream;
  707. if (AttrUtils::GetStr(sub_graph->GetDirectNode().at(0)->GetOpDesc(), ATTR_NAME_STREAM_LABEL, sub_graph_stream)) {
  708. sgi->SetStreamLabel(sub_graph_stream);
  709. }
  710. /// for now inputFlag is the same before and after partition. It should
  711. /// be changed according to the real partition
  712. std::vector<bool> sub_graph_input(graph_info_.input_size_, true);
  713. std::vector<bool> sub_graph_output(graph_info_.output_size_, true);
  714. sgi->SetSubGraph(sub_graph);
  715. sgi->SetOutputFlag(sub_graph_output);
  716. sgi->SetInputFlag(sub_graph_input);
  717. sgi->SetOutputContext(graph_info_.output_name_);
  718. AddEndPldInformationToSubGraphInfo(sgi);
  719. GELOGI("[GraphPartitioner]: subGraph engine name is %s, graph name is %s, stream label is %s", engine_name.c_str(),
  720. sub_graph->GetName().c_str(), sgi->GetStreamLabel().empty() ? "null" : sgi->GetStreamLabel().c_str());
  721. if (engine_name != input_subgraph_name) { // do not add Data subGraph into SubGraphInfo
  722. output_subgraphs.push_back(sgi);
  723. } else {
  724. graph_2_input_subgraph_[compute_graph] = sgi;
  725. }
  726. }
  727. return SUCCESS;
  728. }
  729. // check if two clusters can merge
  730. bool ge::GraphPartitioner::IsMergeable(size_t parent_cluster, size_t child_cluster, size_t upper_bound) {
  731. if ((graph_info_.clusters_[parent_cluster] == nullptr) || (graph_info_.clusters_[parent_cluster]->nodes_.empty()) ||
  732. (graph_info_.clusters_[child_cluster] == nullptr) || (graph_info_.clusters_[child_cluster]->nodes_.empty())) {
  733. return false;
  734. }
  735. // Check if parent_cluster,child_cluster has same engine or stream label
  736. if ((graph_info_.clusters_[parent_cluster]->engine_name_ != graph_info_.clusters_[child_cluster]->engine_name_) ||
  737. (graph_info_.clusters_[parent_cluster]->stream_label_ != graph_info_.clusters_[child_cluster]->stream_label_)) {
  738. GELOGD("Parent cluster %zu engine %s stream label %s, child cluster %zu engine %s stream label %s can not merge",
  739. parent_cluster, graph_info_.clusters_[parent_cluster]->engine_name_.c_str(),
  740. graph_info_.clusters_[parent_cluster]->stream_label_.c_str(), child_cluster,
  741. graph_info_.clusters_[child_cluster]->engine_name_.c_str(),
  742. graph_info_.clusters_[child_cluster]->stream_label_.c_str());
  743. return false;
  744. }
  745. // Check if parent_cluster,child_cluster is reachable
  746. RemoveEdge(parent_cluster, child_cluster);
  747. // Check if there is a path between parent and child, if return true, can not merge
  748. if (HasSecondPath(parent_cluster, child_cluster, upper_bound)) {
  749. GELOGD("Find second path from %zu to %zu, upper bound is %zu", parent_cluster, child_cluster, upper_bound);
  750. InsertEdge(parent_cluster, child_cluster);
  751. return false;
  752. }
  753. InsertEdge(parent_cluster, child_cluster);
  754. return true;
  755. }
  756. void ge::GraphPartitioner::MergeTwoClusters(size_t parent_cluster, size_t &child_cluster) {
  757. // check which index is bigger
  758. size_t big_cluster, small_cluster;
  759. size_t child_cluster_original = child_cluster;
  760. if (parent_cluster > child_cluster) {
  761. small_cluster = child_cluster;
  762. big_cluster = parent_cluster;
  763. } else {
  764. big_cluster = child_cluster;
  765. small_cluster = parent_cluster;
  766. // flush child_cluster, because it has been modified
  767. child_cluster = small_cluster;
  768. }
  769. // update node_2_cluster_ map
  770. for (auto &node : graph_info_.clusters_[big_cluster]->nodes_) {
  771. graph_info_.node_2_cluster_[node] = graph_info_.clusters_[small_cluster];
  772. }
  773. // merge nodes
  774. graph_info_.clusters_[small_cluster]->nodes_.splice(graph_info_.clusters_[small_cluster]->nodes_.end(),
  775. graph_info_.clusters_[big_cluster]->nodes_);
  776. // merge all input & output to small cluster
  777. graph_info_.clusters_[small_cluster]->in_clu_.insert(graph_info_.clusters_[big_cluster]->in_clu_.begin(),
  778. graph_info_.clusters_[big_cluster]->in_clu_.end());
  779. graph_info_.clusters_[small_cluster]->out_clu_.insert(graph_info_.clusters_[big_cluster]->out_clu_.begin(),
  780. graph_info_.clusters_[big_cluster]->out_clu_.end());
  781. // remove child_cluster's out parent_cluster's in between child_cluster and parent_cluster
  782. RemoveEdge(parent_cluster, child_cluster_original);
  783. // update in/out of the cluster with bigger index
  784. for (auto in_clu : graph_info_.clusters_[big_cluster]->in_clu_) {
  785. graph_info_.clusters_[in_clu]->out_clu_.insert(small_cluster);
  786. graph_info_.clusters_[in_clu]->out_clu_.erase(big_cluster);
  787. }
  788. for (auto out_clu : graph_info_.clusters_[big_cluster]->out_clu_) {
  789. graph_info_.clusters_[out_clu]->in_clu_.insert(small_cluster);
  790. graph_info_.clusters_[out_clu]->in_clu_.erase(big_cluster);
  791. }
  792. graph_info_.clusters_[big_cluster] = graph_info_.clusters_[small_cluster];
  793. }
  794. void ge::GraphPartitioner::RemoveEdge(size_t parent_cluster, size_t child_cluster) {
  795. graph_info_.clusters_[child_cluster]->in_clu_.erase(parent_cluster);
  796. graph_info_.clusters_[parent_cluster]->out_clu_.erase(child_cluster);
  797. }
  798. void ge::GraphPartitioner::InsertEdge(size_t from, size_t to) {
  799. if (from == to) {
  800. return;
  801. }
  802. if (!graph_info_.clusters_[from]->out_clu_.insert(to).second) {
  803. // edge has already exists
  804. return;
  805. }
  806. graph_info_.clusters_[to]->in_clu_.insert(from);
  807. }
  808. void ge::GraphPartitioner::MarkClusters() {
  809. GELOGI("MarkClusters starts. cluster size is %zu", graph_info_.clusters_.size());
  810. size_t cluster_size = graph_info_.clusters_.size();
  811. for (size_t child_cluster = 0; child_cluster < cluster_size; child_cluster++) {
  812. auto found_child_cluster = graph_info_.clusters_[child_cluster];
  813. if (found_child_cluster == nullptr) {
  814. GELOGW("can not found child_cluster is %zu", child_cluster);
  815. continue;
  816. }
  817. auto copy_parents_clusters = found_child_cluster->in_clu_;
  818. vector<size_t> ordered_cluster;
  819. for (const auto &parent_cluster : copy_parents_clusters) {
  820. ordered_cluster.emplace_back(parent_cluster);
  821. }
  822. // sort cluster according to it's output amount
  823. auto comp_func = [this](const size_t &parent_cluster1, const size_t &parent_cluster2) -> bool {
  824. return graph_info_.clusters_[parent_cluster1]->out_clu_.size() <
  825. graph_info_.clusters_[parent_cluster2]->out_clu_.size();
  826. };
  827. std::sort(ordered_cluster.begin(), ordered_cluster.end(), comp_func);
  828. auto child_merged = child_cluster;
  829. for (const auto &parent_cluster : ordered_cluster) {
  830. if (IsMergeable(parent_cluster, child_merged, child_cluster)) {
  831. MergeTwoClusters(parent_cluster, child_merged);
  832. GELOGD("Merging cluster %zu and %zu to %zu", parent_cluster, child_cluster, child_merged);
  833. }
  834. }
  835. }
  836. GELOGD("MarkClusters ends.");
  837. }
  838. Status ge::GraphPartitioner::SplitSubGraphs(ge::ComputeGraphPtr compute_graph) {
  839. GELOGD("SplitSubGraphs starts.");
  840. if (compute_graph == nullptr) {
  841. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid");
  842. GELOGE(FAILED, "[Check][Param] parameter ptr is null.");
  843. return FAILED;
  844. }
  845. // Create graphs for all clusters
  846. std::unordered_set<ClusterPtr> cluster_set;
  847. // add pld&end
  848. for (auto &node : compute_graph->GetDirectNode()) {
  849. GELOGD("Node name is %s.", node->GetName().c_str());
  850. auto child_cluster = graph_info_.node_2_cluster_[node];
  851. ge::ComputeGraphPtr corresponding_graph;
  852. // unordered_set's insert returns a pair, second of pair is bool
  853. if (!cluster_set.insert(child_cluster).second) {
  854. GELOGD("Old sub graph, child_cluster is %zu", child_cluster->index_);
  855. corresponding_graph = graph_info_.cluster_2_partition_.at(child_cluster);
  856. } else {
  857. std::string graph_name = "new_sub_graph" + std::to_string(graph_info_.partitions_.size());
  858. ComputeGraphPtr new_sub_graph = MakeShared<ge::ComputeGraph>(graph_name);
  859. if (new_sub_graph == nullptr) {
  860. REPORT_CALL_ERROR("E19999", "allocate memory for ge::ComputeGraph failed.");
  861. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Allocate][Memory] for ge::ComputeGraph failed.");
  862. return FAILED;
  863. }
  864. AddNewGraphToPartition(new_sub_graph, child_cluster->engine_name_);
  865. corresponding_graph = new_sub_graph;
  866. graph_info_.cluster_2_partition_[child_cluster] = corresponding_graph;
  867. GELOGD("New sub graph, name is %s", graph_name.c_str());
  868. }
  869. // build node to corresponding node map
  870. NodePtr corresponding_node = corresponding_graph->AddNode(node->GetOpDesc());
  871. if (corresponding_node == nullptr) {
  872. REPORT_CALL_ERROR("E19999", "add node:%s in graph:%s failed",
  873. node->GetName().c_str(), corresponding_graph->GetName().c_str());
  874. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Add][Node] %s in graph:%s failed.",
  875. node->GetName().c_str(), corresponding_graph->GetName().c_str());
  876. return FAILED;
  877. }
  878. graph_info_.corresponding_node_in_partitions_[node] = corresponding_node;
  879. GE_CHK_STATUS_RET(corresponding_node->SetOwnerComputeGraph(corresponding_graph))
  880. for (const auto &in_anchor : node->GetAllInAnchors()) {
  881. GELOGD("In anchor index is %d", AnchorUtils::GetIdx(in_anchor));
  882. for (auto &peer_out_anchor : in_anchor->GetPeerAnchors()) {
  883. GELOGD("Peer out anchor index is %d", AnchorUtils::GetIdx(peer_out_anchor));
  884. // Normally, all nodes have a copy in corresponding_node_in_partitions_, so function at can not be exception
  885. auto iter = graph_info_.corresponding_node_in_partitions_.find(peer_out_anchor->GetOwnerNode());
  886. if (iter == graph_info_.corresponding_node_in_partitions_.end()) {
  887. REPORT_INNER_ERROR("E19999", "node[%s]id[%ld]'s parent_node[%s]id[%ld]"
  888. "should make corresponding in advance",
  889. node->GetOpDesc()->GetName().c_str(), node->GetOpDesc()->GetId(),
  890. peer_out_anchor->GetOwnerNode()->GetOpDesc()->GetName().c_str(),
  891. peer_out_anchor->GetOwnerNode()->GetOpDesc()->GetId());
  892. GELOGE(GRAPH_FAILED, "[Check][Param] node[%s]id[%ld]'s parent_node[%s]id[%ld]"
  893. "should make corresponding in advance",
  894. node->GetOpDesc()->GetName().c_str(), node->GetOpDesc()->GetId(),
  895. peer_out_anchor->GetOwnerNode()->GetOpDesc()->GetName().c_str(),
  896. peer_out_anchor->GetOwnerNode()->GetOpDesc()->GetId());
  897. return GRAPH_FAILED;
  898. }
  899. auto parent_node = iter->second;
  900. GE_CHECK_NOTNULL(parent_node);
  901. GELOGD("Parent node name is %s", parent_node->GetName().c_str());
  902. // add edge
  903. auto src_anchor = parent_node->GetOutAnchor(AnchorUtils::GetIdx(peer_out_anchor));
  904. auto dst_anchor = corresponding_node->GetInAnchor(AnchorUtils::GetIdx(in_anchor));
  905. // if child and parent's cluster is not same, add plc and end
  906. auto parent_cluster = graph_info_.node_2_cluster_[peer_out_anchor->GetOwnerNode()];
  907. if (parent_cluster != child_cluster) {
  908. GELOGD("Parent cluster is %zu, child_cluster is %zu", parent_cluster->index_, child_cluster->index_);
  909. if (AddPlaceHolderEnd(peer_out_anchor, in_anchor) != ge::SUCCESS) {
  910. GELOGE(GE_GRAPH_ADD_PLC_END_FAILED,
  911. "[Call][AddPlaceHolderEnd] failed, out_anchor:%s index:%d, in_anchor:%s index:%d.",
  912. peer_out_anchor->GetOwnerNode()->GetName().c_str(), AnchorUtils::GetIdx(peer_out_anchor),
  913. in_anchor->GetOwnerNode()->GetName().c_str(), AnchorUtils::GetIdx(in_anchor));
  914. return FAILED;
  915. }
  916. } else { // parent and child in the same cluster, add edge
  917. GELOGD("AddEdge from parent cluster %zu to child %zu", parent_cluster->index_, child_cluster->index_);
  918. if (GraphUtils::AddEdge(src_anchor, dst_anchor) != GRAPH_SUCCESS) {
  919. REPORT_CALL_ERROR("E19999", "add edge from %s to %s failed",
  920. peer_out_anchor->GetOwnerNode()->GetName().c_str(),
  921. in_anchor->GetOwnerNode()->GetName().c_str());
  922. GELOGE(GRAPH_FAILED, "[Add][Edge] from %s to %s failed", peer_out_anchor->GetOwnerNode()->GetName().c_str(),
  923. in_anchor->GetOwnerNode()->GetName().c_str());
  924. return FAILED;
  925. }
  926. }
  927. }
  928. }
  929. }
  930. GELOGD("SplitSubGraphs ends.");
  931. return SUCCESS;
  932. }
  933. /// before calling this function, the direct path between src and dst are already removed.
  934. /// return true if a second path is found
  935. bool ge::GraphPartitioner::HasSecondPath(size_t src, size_t dst, size_t upper_bound) {
  936. if (graph_info_.clusters_.at(src)->out_clu_.empty() || graph_info_.clusters_.at(dst)->in_clu_.empty()) {
  937. return false;
  938. }
  939. /// Avoid recursion since stack space might be limited.
  940. /// We instead keep a stack of nodes to visit.
  941. std::vector<size_t> temp_stack;
  942. std::set<size_t> visited;
  943. temp_stack.push_back(src);
  944. while (!temp_stack.empty()) {
  945. size_t cluster = temp_stack.back();
  946. temp_stack.pop_back();
  947. ClusterPtr cur_cluster = graph_info_.clusters_[cluster];
  948. if (!visited.insert(cluster).second) {
  949. continue;
  950. }
  951. for (auto out : cur_cluster->out_clu_) {
  952. if (out == dst) {
  953. return true; // There is cycle
  954. }
  955. if (out < upper_bound) {
  956. temp_stack.push_back(out);
  957. }
  958. }
  959. }
  960. return false;
  961. }
  962. Status ge::GraphPartitioner::Partition(ge::ComputeGraphPtr compute_graph, Mode mode) {
  963. ClearAllPartitionData();
  964. auto real_ret = SUCCESS;
  965. auto ret = PartitionSubGraph(compute_graph, mode);
  966. if (ret != SUCCESS) {
  967. GELOGE(ret, "[Partition][SubGraph] Failed, ret:%d", ret);
  968. real_ret = ret;
  969. }
  970. GE_CHECK_NOTNULL(compute_graph);
  971. // partition sub graph
  972. for (const auto &sub_graph : compute_graph->GetAllSubgraphs()) {
  973. ret = PartitionSubGraph(sub_graph, mode);
  974. if (ret != SUCCESS) {
  975. GELOGE(ret, "[Partition][SubGraph] Failed, ret:%d", ret);
  976. real_ret = ret;
  977. }
  978. }
  979. if (real_ret != SUCCESS) {
  980. auto root_graph = ge::GraphUtils::FindRootGraph(compute_graph);
  981. GE_CHECK_NOTNULL(root_graph);
  982. (void)Analyzer::GetInstance()->SaveAnalyzerDataToFile(root_graph->GetSessionID(),
  983. root_graph->GetGraphID());
  984. }
  985. return real_ret;
  986. }
  987. Status ge::GraphPartitioner::PartitionSubGraph(ge::ComputeGraphPtr compute_graph, Mode mode) {
  988. if (compute_graph == nullptr) {
  989. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid.");
  990. GELOGE(GE_GRAPH_NULL_INPUT, "[Check][Param] compute_graph is nullptr.");
  991. return FAILED;
  992. }
  993. // clear graph_info
  994. graph_info_.ClearAllData(mode);
  995. graph_info_.output_name_ = compute_graph->GetOutput();
  996. graph_info_.output_size_ = compute_graph->GetOutputSize();
  997. graph_info_.input_size_ = compute_graph->GetInputSize();
  998. if (graph_info_.output_size_ == 0) {
  999. REPORT_INNER_ERROR("E19999", "the output size of graph:%s is 0, check invalid.",
  1000. compute_graph->GetName().c_str());
  1001. GELOGE(GE_GRAPH_NULL_INPUT, "[Check][Param] The output size:0 of graph:%s need to be greater than 0.",
  1002. compute_graph->GetName().c_str());
  1003. return FAILED;
  1004. }
  1005. GELOGI("Graph Partition starts, graph nodes size is %zu", compute_graph->GetDirectNodesSize());
  1006. Status ret = compute_graph->TopologicalSorting();
  1007. if (ret != SUCCESS) {
  1008. REPORT_CALL_ERROR("E19999", "TopologicalSorting for graph:%s failed",
  1009. compute_graph->GetName().c_str());
  1010. GELOGE(GE_GRAPH_TOPO_SORT_FAILED, "[Call][TopologicalSorting] for subGraph:%s failed",
  1011. compute_graph->GetName().c_str());
  1012. return FAILED;
  1013. }
  1014. GE_TIMESTAMP_START(PartitionSubGraphInitialize);
  1015. if (Initialize(compute_graph) != SUCCESS) {
  1016. GELOGE(GE_GRAPH_INIT_FAILED, "[Call][Initialize] for graph:%s failed", compute_graph->GetName().c_str());
  1017. return FAILED;
  1018. }
  1019. GE_TIMESTAMP_END(PartitionSubGraphInitialize, "GraphPartitioner::PartitionInitialize");
  1020. GE_TIMESTAMP_START(PartitionSubGraphMarkClusters);
  1021. MarkClusters();
  1022. GE_TIMESTAMP_END(PartitionSubGraphMarkClusters, "GraphPartitioner::PartitionMarkClusters");
  1023. GE_TIMESTAMP_START(PartitionSubGraphSplitSubGraphs);
  1024. if (SplitSubGraphs(compute_graph) != SUCCESS) {
  1025. GELOGE(FAILED, "[Split][SubGraphs] for graph:%s failed", compute_graph->GetName().c_str());
  1026. return FAILED;
  1027. }
  1028. GE_TIMESTAMP_END(PartitionSubGraphSplitSubGraphs, "GraphPartitioner::PartitionSplitSubGraphs");
  1029. GE_TIMESTAMP_START(PartitionSubGraphSortSubGraphs);
  1030. if (SortSubGraphs(compute_graph) != ge::SUCCESS) {
  1031. GELOGE(GE_GRAPH_TOPO_SORT_FAILED, "[Sort][SubGraphs] for graph:%s failed.",
  1032. compute_graph->GetName().c_str());
  1033. return ge::FAILED;
  1034. }
  1035. GE_TIMESTAMP_END(PartitionSubGraphSortSubGraphs, "GraphPartitioner::PartitionSortSubGraphs");
  1036. GE_TIMESTAMP_START(PartitionSubGraphAddPartitionsToGraphNode);
  1037. vector<ge::SubGraphInfoPtr> output_subgraphs;
  1038. if (AddPartitionsToGraphNode(output_subgraphs, compute_graph) != ge::SUCCESS) {
  1039. GELOGE(GE_GRAPH_EMPTY_PARTITION, "[Add][Partitions] To GraphNode failed, graph:%s.",
  1040. compute_graph->GetName().c_str());
  1041. return ge::FAILED;
  1042. }
  1043. GE_TIMESTAMP_END(PartitionSubGraphAddPartitionsToGraphNode, "GraphPartitioner::PartitionAddPartitionsToGraphNode");
  1044. GELOGI("Graph Partition ends. Adding partitions to SubGraphInfo, got %zu sub graphs", output_subgraphs.size());
  1045. graph_info_.mode_ = kMerging;
  1046. // do not care over flow
  1047. partition_times_++;
  1048. graph_2_graph_partition_info_[compute_graph] = graph_info_;
  1049. graph_2_subgraph_list_[compute_graph] = output_subgraphs;
  1050. return SUCCESS;
  1051. }
  1052. // all the inputs are the nodes and anchors in the original graph
  1053. Status ge::GraphPartitioner::AddPlaceHolderEnd(const AnchorPtr &out_anchor, const AnchorPtr &in_anchor) {
  1054. if ((out_anchor == nullptr) || (in_anchor == nullptr)) {
  1055. REPORT_INNER_ERROR("E19999", "Param out_anchor or in_anchor is nullptr, check invalid.");
  1056. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Check][Param] out_anchor or in_anchor is nullptr.");
  1057. return FAILED;
  1058. }
  1059. // nodes in original graph
  1060. const auto &src_node = out_anchor->GetOwnerNode();
  1061. const auto &dst_node = in_anchor->GetOwnerNode();
  1062. if ((src_node == nullptr) || (dst_node == nullptr)) {
  1063. REPORT_INNER_ERROR("E19999", "in_anchor'node or out_anchor'node is nullptr. check invalid.");
  1064. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Check][Param] src_node or dst_node is nullptr.");
  1065. return FAILED;
  1066. }
  1067. // All nodes have a copy in corresponding_node_in_partitions_, so function at can not be execption
  1068. auto src_anchor =
  1069. graph_info_.corresponding_node_in_partitions_.at(src_node)->GetOutAnchor(AnchorUtils::GetIdx(out_anchor));
  1070. auto dst_anchor =
  1071. graph_info_.corresponding_node_in_partitions_.at(dst_node)->GetInAnchor(AnchorUtils::GetIdx(in_anchor));
  1072. if ((src_anchor == nullptr) || (dst_anchor == nullptr)) {
  1073. REPORT_INNER_ERROR("E19999", "src_anchor(index:%d) or dst_anchor(index:%d) is nullptr.",
  1074. AnchorUtils::GetIdx(out_anchor), AnchorUtils::GetIdx(in_anchor));
  1075. GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Check][Param] src_anchor(index:%d) or dst_anchor(index:%d) is nullptr.",
  1076. AnchorUtils::GetIdx(out_anchor), AnchorUtils::GetIdx(in_anchor));
  1077. return FAILED;
  1078. }
  1079. // anchors in subGraph
  1080. const ComputeGraphPtr &src_subgraph = src_anchor->GetOwnerNode()->GetOwnerComputeGraph();
  1081. const ComputeGraphPtr &dst_subgraph = dst_anchor->GetOwnerNode()->GetOwnerComputeGraph();
  1082. // add end and pld node
  1083. auto ret = AddPlaceHolderEndInSrcDstGraph(src_anchor, dst_anchor, dst_subgraph, src_subgraph);
  1084. if (ret != SUCCESS) {
  1085. GELOGE(GE_GRAPH_ADD_PLC_END_FAILED, "[Call][AddPlaceHolderEndInSrcDstGraph] failed, ret:%d.", ret);
  1086. return ret;
  1087. }
  1088. return SUCCESS;
  1089. }
  1090. Status ge::GraphPartitioner::SortSubGraphs(const ge::ComputeGraphPtr &compute_graph) {
  1091. uint32_t rank = kRankOne; // rank 0 for data graph
  1092. ComputeGraphPtr new_input_nodes_sub_graph = MakeShared<ComputeGraph>("inputNodeGraph");
  1093. if ((new_input_nodes_sub_graph == nullptr) || (compute_graph == nullptr)) {
  1094. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr or Allocate Memory for ComputeGraph failed.");
  1095. GELOGE(FAILED, "[Check][Param] new_input_nodes_sub_graph or compute_graph is nullptr.");
  1096. return FAILED;
  1097. }
  1098. for (const auto &node : compute_graph->GetDirectNode()) {
  1099. // All nodes in original graph have a copy in corresponding_node_in_partitions_, so it can not be null
  1100. auto sub_graph = graph_info_.corresponding_node_in_partitions_.at(node)->GetOwnerComputeGraph();
  1101. if ((graph_info_.partitions_2_rank_.find(sub_graph) == graph_info_.partitions_2_rank_.end()) &&
  1102. (graph_info_.partitions_[sub_graph] != kEngineDefaultData)) {
  1103. graph_info_.partitions_2_rank_[sub_graph] = rank;
  1104. graph_info_.rank_2_partitions_.push_back(sub_graph);
  1105. rank++;
  1106. } else if (graph_info_.partitions_[sub_graph] == kEngineDefaultData) { // merge data graph
  1107. if (PutInputNodesInSubGraph(sub_graph, new_input_nodes_sub_graph) != SUCCESS) {
  1108. GELOGE(FAILED, "[Call][putInputNodesInSubGraph] failed.");
  1109. return FAILED;
  1110. }
  1111. auto to_be_del = graph_info_.partitions_.find(sub_graph);
  1112. graph_info_.partitions_.erase(to_be_del);
  1113. }
  1114. }
  1115. if (!new_input_nodes_sub_graph->GetDirectNode().empty()) {
  1116. graph_info_.rank_2_partitions_.insert(graph_info_.rank_2_partitions_.begin(), new_input_nodes_sub_graph);
  1117. graph_info_.partitions_2_rank_[new_input_nodes_sub_graph] = 0;
  1118. AddNewGraphToPartition(new_input_nodes_sub_graph, "inputNodesSubGraph");
  1119. }
  1120. // reinit rank
  1121. rank = kRankZero;
  1122. for (const auto &it : graph_info_.rank_2_partitions_) {
  1123. // rename subGraph based on rank
  1124. if (it != nullptr) {
  1125. // rename subGraph based on rank
  1126. string graph_name =
  1127. "partition" + std::to_string(partition_times_) + "_rank" + std::to_string(rank) + "_" + it->GetName();
  1128. it->SetName(graph_name);
  1129. }
  1130. rank++;
  1131. }
  1132. return SUCCESS;
  1133. }
  1134. AnchorPtr ge::GraphPartitioner::GetEndInAnchor(const AnchorPtr &src_anchor, const NodePtr &end_node) {
  1135. if ((src_anchor == nullptr) || (end_node == nullptr)) {
  1136. REPORT_INNER_ERROR("E19999", "Param src_anchor or end_node is nullptr, check invalid.");
  1137. GELOGE(FAILED, "[Check][Param] parameter src_anchor or end_node is nullptr.");
  1138. return nullptr;
  1139. }
  1140. AnchorPtr end_in_anchor;
  1141. if (Anchor::DynamicAnchorCast<OutDataAnchor>(src_anchor) != nullptr) {
  1142. end_in_anchor = end_node->GetInDataAnchor(0);
  1143. } else {
  1144. end_in_anchor = end_node->GetInControlAnchor();
  1145. }
  1146. return end_in_anchor;
  1147. }
  1148. AnchorPtr ge::GraphPartitioner::GetPldOutAnchor(const NodePtr &pld_node, const AnchorPtr &dst_anchor) {
  1149. if ((pld_node == nullptr) || (dst_anchor == nullptr)) {
  1150. REPORT_INNER_ERROR("E19999", "Param pld_node or dst_anchor is nullptr, check invalid.");
  1151. GELOGE(FAILED, "[Check][Param] parameter pld_node or dst_anchor is nullptr.");
  1152. return nullptr;
  1153. }
  1154. AnchorPtr pld_out_anchor;
  1155. if (Anchor::DynamicAnchorCast<InDataAnchor>(dst_anchor) != nullptr) {
  1156. pld_out_anchor = pld_node->GetOutDataAnchor(0);
  1157. } else {
  1158. pld_out_anchor = pld_node->GetOutControlAnchor();
  1159. }
  1160. return pld_out_anchor;
  1161. }
  1162. void ge::GraphPartitioner::AddEndPldInformationToSubGraphInfo(ge::SubGraphInfoPtr &subgraph_info) {
  1163. if (subgraph_info == nullptr) {
  1164. GELOGE(FAILED, "[Check][Param] parameter subgraph_info is nullptr.");
  1165. return;
  1166. }
  1167. auto subgraph = subgraph_info->GetSubGraph();
  1168. GE_CHECK_NOTNULL_JUST_RETURN(subgraph);
  1169. NodetoNodeMap end_map;
  1170. NodetoNodeMap pld_map;
  1171. for (const auto &node : subgraph->GetDirectNode()) {
  1172. if (node->GetType() == kEndType) {
  1173. end_map[node] = graph_info_.end_2_pld_.at(node);
  1174. }
  1175. if (node->GetType() == kPlaceHolderType) {
  1176. pld_map[node] = graph_info_.pld_2_end_.at(node);
  1177. }
  1178. }
  1179. subgraph_info->SetEnd2PldMap(end_map);
  1180. subgraph_info->SetPld2EndMap(pld_map);
  1181. }
  1182. const Graph2SubGraphInfoList &ge::GraphPartitioner::GetSubGraphMap() { return graph_2_subgraph_list_; }
  1183. void ge::GraphPartitioner::ClearAllPartitionData() {
  1184. graph_2_graph_partition_info_.clear();
  1185. graph_2_subgraph_list_.clear();
  1186. graph_2_input_subgraph_.clear();
  1187. GELOGD("Clear all partition data success.");
  1188. return;
  1189. }
  1190. } // namespace ge

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