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

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