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

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