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

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