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

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

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