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

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

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