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

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