Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "graph/partition/engine_partitioner.h"
12 :
13 : #include <algorithm>
14 : #include <memory>
15 : #include <string>
16 : #include <unordered_set>
17 : #include <vector>
18 : #include <stack>
19 : #include "analyzer/analyzer.h"
20 : #include "common/plugin/ge_make_unique_util.h"
21 : #include "framework/common/op/ge_op_utils.h"
22 : #include "common/compile_profiling/ge_trace_wrapper.h"
23 : #include "graph/ascend_string.h"
24 : #include "graph/custom_op_factory.h"
25 : #include "graph/utils/graph_utils.h"
26 : #include "graph/utils/op_desc_utils.h"
27 : #include "graph/utils/type_utils.h"
28 : #include "graph/utils/op_type_utils.h"
29 : #include "graph/utils/attr_utils.h"
30 : #include "graph/build/stream/stream_utils.h"
31 : #include "common/checker.h"
32 : #include "graph/ge_context.h"
33 : #include "api/gelib/gelib.h"
34 : #include "graph/attribute_group/attr_group_shape_env.h"
35 : #include "graph_metadef/common/ge_common/util.h"
36 : #include "common/ge_common/ge_types.h"
37 :
38 : namespace ge {
39 : namespace {
40 : const char_t *const kEngineDefaultData = "ENGINE_DEFAULT_DATA";
41 : const char_t *const kEndType = "End";
42 : const char_t *const kPlaceHolderType = "PlaceHolder";
43 : const char_t *const kPeerIndex = "peerIndex";
44 : const char_t *const kParentOpType = "parentOpType";
45 : const char_t *const kParentNode = "parentNode";
46 : const char_t *const kPeerNodeName = "_peerNodeName";
47 : const char_t *const kParentNodeName = "_parentNodeName";
48 : const char_t *const kParentId = "parentId";
49 : const char_t *const kAnchorIndex = "anchorIndex";
50 : const char_t *const kTaskL2FusionInfo = "_task_L2FusionInfo";
51 : const char_t *const kDataAnchorIndexForLxfusion = "_data_anchor_index_for_lxfusion";
52 : const char_t *const kEnableCvParallel = "_enable_cv_parallel";
53 : const char_t *const kVectorEngineName = "VectorEngine";
54 : const char_t *const kHostCpuEngineName = "DNN_VM_HOST_CPU";
55 : const std::string kStableRdfsSort = "3";
56 : const int32_t kOneGraph = 1; // only one graph
57 : const int32_t kRankOne = 1; // order of graph list is 0,1,2,3..., 1 means second order
58 : const int32_t kRankZero = 0; // order of graph list is 0,1,2,3..., 0 means first order
59 : const int64_t kOverflowDefaultValue = -1;
60 :
61 : bool IsCustomOpExecOnHostCpu(const OpDescPtr &op_desc) {
62 : if ((op_desc == nullptr) || (op_desc->GetOpEngineName() != kEngineNameCustom) ||
63 : (op_desc->GetOpKernelLibName() != kCustomOpKernelLibName) ||
64 : !CustomOpFactory::IsExistOp(AscendString(op_desc->GetTypePtr()), OpBackend::kHostCPU)) {
65 : return false;
66 : }
67 : std::string lowering_func;
68 : return AttrUtils::GetStr(op_desc, kAttrLowingFunc, lowering_func) && (lowering_func == kHostCpuCustomOpLowerFunc);
69 : }
70 :
71 : struct DeviceIndex {
72 : std::string engine_type;
73 : std::vector<int32_t> indices;
74 : std::string DebugString() const {
75 : return engine_type + ToString(indices);
76 : };
77 : bool operator==(const DeviceIndex &rhs) const {
78 : return engine_type == rhs.engine_type && indices == rhs.indices;
79 : };
80 : bool operator!=(const DeviceIndex &rhs) const {
81 : return !(*this == rhs);
82 : };
83 : bool operator<(const DeviceIndex &rhs) const {
84 : if (engine_type < rhs.engine_type) {
85 : return true;
86 : }
87 : if (rhs.engine_type < engine_type) {
88 : return false;
89 : }
90 : return indices < rhs.indices;
91 : };
92 : };
93 :
94 : std::string GenClusterEngineName(const NodePtr &node, EnginePartitioner::Mode mode, const NodeEngineMap &engine_map) {
95 : auto engine_name = engine_map.at(node);
96 : // 流分配时,device自定义算子需要跟aicore算子一条流
97 : if ((mode == EnginePartitioner::Mode::kSecondPartitioning) && (engine_name == kEngineNameCustom) &&
98 : !IsCustomOpExecOnHostCpu(node->GetOpDesc())) {
99 : // 临时改动,后续需要从用户的注册引擎信息里面获取此处的自定义算子挂靠的引擎名字
100 : engine_name = kEngineNameAiCore;
101 : }
102 : return engine_name;
103 : }
104 :
105 : bool IsLinkedInGraph(const NodePtr &src_node, const NodePtr &dst_node) {
106 : if ((src_node == nullptr) || (dst_node == nullptr)) {
107 : return false;
108 : }
109 : const auto src_node_id = src_node->GetOpDesc()->GetId();
110 : const auto dst_node_id = dst_node->GetOpDesc()->GetId();
111 : if (src_node_id > dst_node_id) {
112 : return false;
113 : }
114 :
115 : std::stack<NodePtr> node_stack;
116 : std::unordered_set<NodePtr> visited; // 记录已访问节点,避免循环
117 :
118 : node_stack.push(src_node);
119 : visited.insert(src_node);
120 :
121 : while (!node_stack.empty()) {
122 : NodePtr current_node = node_stack.top();
123 : node_stack.pop();
124 :
125 : for (const auto &node : current_node->GetOutAllNodes()) {
126 : if (node == dst_node) {
127 : return true;
128 : }
129 : if (node->GetOpDesc()->GetId() > dst_node_id) {
130 : continue;
131 : }
132 : if (visited.find(node) == visited.end()) {
133 : visited.insert(node);
134 : node_stack.push(node);
135 : }
136 : }
137 : }
138 :
139 : return false;
140 : }
141 :
142 : // 根据topo序找到aiv前后相邻的aic节点,如果aiv和其中至少1个aic在图上没有通路则可以并发
143 : void MarkCvParallelAivNodes(const ComputeGraphPtr &graph) {
144 : if (!StreamUtils::EnableCvParallel(graph)) {
145 : return;
146 : }
147 : std::map<NodePtr, std::pair<NodePtr, NodePtr>> aiv_to_adjacent_aic_nodes;
148 : NodePtr pre_aic = nullptr;
149 : std::vector<decltype(aiv_to_adjacent_aic_nodes.begin())> aiv_iters;
150 : for (auto node : graph->GetDirectNode()) {
151 : if (StreamUtils::IsAivNode(node)) {
152 : auto insert_ret = aiv_to_adjacent_aic_nodes.insert({node, {pre_aic, nullptr}});
153 : aiv_iters.emplace_back(insert_ret.first);
154 : }
155 : if (StreamUtils::IsAicNode(node)) {
156 : for (auto iter : aiv_iters) {
157 : if ((iter != aiv_to_adjacent_aic_nodes.end()) && (iter->second.second == nullptr)) {
158 : iter->second.second = node;
159 : }
160 : }
161 : aiv_iters.clear();
162 : pre_aic = node;
163 : }
164 : }
165 : for (auto aiv_iter : aiv_to_adjacent_aic_nodes) {
166 : auto aiv_node = aiv_iter.first;
167 : auto pre_aic_node = aiv_iter.second.first;
168 : auto after_aic_node = aiv_iter.second.second;
169 : bool is_pre_aic_link_to_aiv = IsLinkedInGraph(pre_aic_node, aiv_node);
170 : if (is_pre_aic_link_to_aiv && (after_aic_node == nullptr)) {
171 : continue;
172 : }
173 : if (!is_pre_aic_link_to_aiv || !IsLinkedInGraph(aiv_node, after_aic_node)) {
174 : AttrUtils::SetBool(aiv_node->GetOpDesc(), kEnableCvParallel, true);
175 : GELOGD("node %s set cv parallel", aiv_node->GetNamePtr());
176 : }
177 : GELOGD("pre aic %s, current aiv %s, after aic %s", (pre_aic_node == nullptr) ? nullptr : pre_aic_node->GetNamePtr(),
178 : aiv_node->GetNamePtr(), (after_aic_node == nullptr) ? nullptr : after_aic_node->GetNamePtr());
179 : }
180 : }
181 : } // namespace
182 : Status ge::EnginePartitioner::CheckValidIfEnd2PldEmpty(const GraphPartitionInfo &graph_info,
183 : ge::ComputeGraphPtr &output_merged_compute_graph) const {
184 : // only one condition:no data node, one engine, there is only one graph + input graph
185 : if (graph_info.partitions_.size() == kOneGraph) {
186 : const auto &partition = (*graph_info.partitions_.begin());
187 : if (partition.first == nullptr) {
188 : REPORT_INNER_ERR_MSG("E19999", "partition.first is nullptr, check invalid, engine name is %s",
189 : partition.second.c_str());
190 : GELOGE(GE_GRAPH_EMPTY_PARTITION, "[Check][Param] partition.first is null, engine name is %s",
191 : partition.second.c_str());
192 : return FAILED;
193 : }
194 : output_merged_compute_graph = partition.first;
195 : } else { // if placeholder to end map is empty, it should be an exception condition
196 : REPORT_INNER_ERR_MSG("E19999", "partitions size:%zu is not 1, check invalid.", graph_info.partitions_.size());
197 : GELOGE(GE_GRAPH_EMPTY_PARTITION, "[Check][Param] placeholder to end map is empty, partitions size:%zu is not 1.",
198 : graph_info.partitions_.size());
199 : return FAILED;
200 : }
201 : return SUCCESS;
202 : }
203 :
204 : Status ge::EnginePartitioner::MergeAllSubGraph(ge::ComputeGraphPtr &output_merged_compute_graph,
205 : const std::vector<SubGraphInfoPtr> &sub_graph_list,
206 : const GraphPartitionInfo &graph_info) const {
207 : for (size_t rank = 0UL; rank < graph_info.rank_2_partitions_.size(); rank++) {
208 : std::string temp_stream;
209 : // sub_graph_list index is one ahead of rank_2_partitions_list index
210 : if (rank > 0UL) {
211 : temp_stream = sub_graph_list[rank - 1UL]->GetStreamLabel();
212 : }
213 : for (const auto &node : graph_info.rank_2_partitions_[rank]->GetDirectNode()) {
214 : if (node == nullptr) {
215 : continue;
216 : }
217 : if ((node->GetType() == kEndType) || (node->GetType() == kPlaceHolderType)) {
218 : continue;
219 : }
220 : if ((!temp_stream.empty()) && (!AttrUtils::HasAttr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL))) {
221 : (void)AttrUtils::SetStr(node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, temp_stream);
222 : }
223 : GE_ASSERT_GRAPH_SUCCESS(node->SetOwnerComputeGraph(output_merged_compute_graph),
224 : "[Set][OwnerComputeGraph] failed, node %s", node->GetName().c_str());
225 : (void)output_merged_compute_graph->AddNode(node);
226 : }
227 : }
228 : // get session graph id from subgraph
229 : GE_ASSERT_SUCCESS(SetMergedGraphId(output_merged_compute_graph, graph_info),
230 : "[Call][SetMergedGraphId] failed, graph:%s", output_merged_compute_graph->GetName().c_str());
231 : return SUCCESS;
232 : }
233 :
234 : Status ge::EnginePartitioner::SetMergedGraphId(const ge::ComputeGraphPtr &output_merged_compute_graph,
235 : const GraphPartitionInfo &graph_info) const {
236 : std::string session_graph_id;
237 : // get session graph id from subgraph
238 : if (graph_info.rank_2_partitions_.empty() ||
239 : !AttrUtils::GetStr(*(graph_info.rank_2_partitions_[0U]), ATTR_NAME_SESSION_GRAPH_ID, session_graph_id)) {
240 : GELOGW("Get graph session_graph_id attr failed.");
241 : }
242 : // set session graph id into merged subgraph
243 : if (!session_graph_id.empty()) {
244 : GELOGI("Set session graph id %s in merged compute graph", session_graph_id.c_str());
245 : // private function, promise output_merged_compute_graph not null
246 : GE_ASSERT_TRUE(AttrUtils::SetStr(*output_merged_compute_graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id),
247 : "SetStr ATTR_NAME_SESSION_GRAPH_ID[%s] failed of graph:%s.", session_graph_id.c_str(),
248 : output_merged_compute_graph->GetName().c_str());
249 : }
250 : return SUCCESS;
251 : }
252 :
253 : Status ge::EnginePartitioner::RemoveNodeAndEdgeBetweenEndPld(ge::ComputeGraphPtr &output_merged_compute_graph,
254 : const std::vector<SubGraphInfoPtr> &sub_graph_list,
255 : const GraphPartitionInfo &graph_info) {
256 : GE_ASSERT_NOTNULL(output_merged_compute_graph, "[Check][Input] failed, output_merged_compute_graph is null.");
257 : GE_ASSERT_SUCCESS(MergeAllSubGraph(output_merged_compute_graph, sub_graph_list, graph_info),
258 : "[Merge][AllSubGraph] failed.");
259 : for (const auto &it : graph_info.index_2_end_) {
260 : const auto &end = it.second;
261 : const auto &pld = graph_info.end_2_pld_.at(it.second);
262 : if ((end != nullptr) && (pld != nullptr) && (end->GetInDataAnchor(0) != nullptr) &&
263 : (pld->GetOutDataAnchor(0) != nullptr)) {
264 : AnchorPtr end_in_anchor = (end->GetInDataAnchor(0)->GetFirstPeerAnchor() == nullptr)
265 : ? Anchor::DynamicAnchorCast<Anchor>(end->GetInControlAnchor())
266 : : Anchor::DynamicAnchorCast<Anchor>(end->GetInDataAnchor(0));
267 : AnchorPtr pld_out_anchor = (pld->GetOutDataAnchor(0)->GetFirstPeerAnchor() == nullptr)
268 : ? Anchor::DynamicAnchorCast<Anchor>(pld->GetOutControlAnchor())
269 : : Anchor::DynamicAnchorCast<Anchor>(pld->GetOutDataAnchor(0));
270 : GE_CHECK_NOTNULL(end_in_anchor);
271 : auto src_anchor = end_in_anchor->GetFirstPeerAnchor(); // src_anchor should be only 1
272 : GE_CHECK_NOTNULL(src_anchor);
273 : GE_ASSERT_GRAPH_SUCCESS(GraphUtils::RemoveEdge(src_anchor, end_in_anchor),
274 : "[Remove][Edge] between %s and %s failed. node_name:%s, graph_name:%s",
275 : src_anchor->GetOwnerNode()->GetName().c_str(),
276 : end_in_anchor->GetOwnerNode()->GetName().c_str(), end->GetName().c_str(),
277 : end->GetOwnerComputeGraph()->GetName().c_str());
278 : GE_CHECK_NOTNULL(pld_out_anchor);
279 : for (const auto &peer_in_anchor : pld_out_anchor->GetPeerAnchors()) {
280 : GE_ASSERT_GRAPH_SUCCESS(GraphUtils::RemoveEdge(pld_out_anchor, peer_in_anchor),
281 : "[Remove][Edge] between %s and %s failed. node_name:%s, graph_name:%s",
282 : pld_out_anchor->GetOwnerNode()->GetName().c_str(),
283 : peer_in_anchor->GetOwnerNode()->GetName().c_str(), pld->GetName().c_str(),
284 : pld->GetOwnerComputeGraph()->GetName().c_str());
285 : GE_ASSERT_GRAPH_SUCCESS(GraphUtils::AddEdge(src_anchor, peer_in_anchor), "[Add][Edge] from %s to %s failed.",
286 : src_anchor->GetOwnerNode()->GetName().c_str(),
287 : peer_in_anchor->GetOwnerNode()->GetName().c_str());
288 : }
289 : NodeUtils::UnlinkAll(*pld);
290 : NodeUtils::UnlinkAll(*end);
291 : } else {
292 : GELOGW("End or pld is nullptr or in data anchor of end is nullptr or out data anchor of pld is nullptr");
293 : }
294 : }
295 : return SUCCESS;
296 : }
297 :
298 : Status ge::EnginePartitioner::MergeOverflowAttr(const ge::ComputeGraphPtr &sub_graph,
299 : ge::ComputeGraphPtr &root_graph) const {
300 : GE_CHECK_NOTNULL(sub_graph);
301 : GE_CHECK_NOTNULL(root_graph);
302 : if (AttrUtils::HasAttr(sub_graph, GLOBALWORKSPACE_TYPE) && !AttrUtils::HasAttr(root_graph, GLOBALWORKSPACE_TYPE)) {
303 : (void)AttrUtils::SetInt(root_graph, GLOBALWORKSPACE_TYPE, global_workspace_type_);
304 : (void)AttrUtils::SetInt(root_graph, "globalworkspace_size", global_workspace_size_);
305 : }
306 : return SUCCESS;
307 : }
308 :
309 : Status ge::EnginePartitioner::MergeAfterSubGraphOptimization(ge::ComputeGraphPtr &output_merged_compute_graph,
310 : const ge::ComputeGraphPtr &original_compute_graph,
311 : EnginePartitioner::Mode mode) {
312 : current_mode_ = mode;
313 : // Assign engine for each node in the graph
314 : DNNEngineManager::GetInstance().InitPerformanceStatistic();
315 : (void)mode;
316 : Status real_ret = SUCCESS;
317 : auto ret = MergeSubGraph(output_merged_compute_graph, original_compute_graph);
318 : if (ret != SUCCESS) {
319 : // even though failed, ensure all op do finish check support
320 : real_ret = FAILED;
321 : GELOGE(ret, "[Merge][SubGraph] Failed, ret:%d", ret);
322 : }
323 : GE_CHECK_NOTNULL(original_compute_graph);
324 : output_merged_compute_graph->SetName(original_compute_graph->GetName());
325 : // merge sub graph
326 : for (const auto &sub_graph : original_compute_graph->GetAllSubgraphs()) {
327 : GE_CHECK_NOTNULL(sub_graph);
328 : bool no_need_merge = false;
329 : (void)ge::AttrUtils::GetBool(sub_graph, ATTR_NAME_NO_NEED_MERGE, no_need_merge);
330 : if (no_need_merge) {
331 : GELOGI("sub graph %s no need merge, skip it", sub_graph->GetName().c_str());
332 : continue;
333 : }
334 : ComputeGraphPtr merged_sub_graph = nullptr;
335 : ret = MergeSubGraph(merged_sub_graph, sub_graph);
336 : if (ret != SUCCESS) {
337 : real_ret = FAILED;
338 : GELOGE(ret, "[Merge][SubGraph] Failed, ret:%d", ret);
339 : continue;
340 : }
341 : // this means subgraph added in optimize subgraph and without partitions, so just add to root graph
342 : if (merged_sub_graph == sub_graph) {
343 : GELOGI("Just add subgraph %s (parent node is %s) to root graph %s.", sub_graph->GetName().c_str(),
344 : sub_graph->GetParentNode()->GetName().c_str(), output_merged_compute_graph->GetName().c_str());
345 : sub_graph->SetParentGraph(sub_graph->GetParentNode()->GetOwnerComputeGraph());
346 : GE_ASSERT_GRAPH_SUCCESS(output_merged_compute_graph->AddSubgraph(sub_graph->GetName(), merged_sub_graph),
347 : "[Call][AddSubgraph] failed, subgraph:%s, merged subgraph:%s",
348 : sub_graph->GetName().c_str(), merged_sub_graph->GetName().c_str());
349 : continue;
350 : }
351 : // add sub graph
352 : merged_sub_graph->SetName(sub_graph->GetName());
353 : merged_sub_graph->SetInputSize(sub_graph->GetInputSize());
354 : merged_sub_graph->SetOutputSize(sub_graph->GetOutputSize());
355 : const auto &parent_node = sub_graph->GetParentNode();
356 : GE_ASSERT_NOTNULL(parent_node, "[Check][Param] Parent node is null, graph name is %s",
357 : sub_graph->GetName().c_str());
358 : const auto &original_graph = parent_node->GetOwnerComputeGraph();
359 : GE_ASSERT_TRUE(graph_2_graph_partition_info_.find(original_graph) != graph_2_graph_partition_info_.end(),
360 : "[Check][Param] Find graph info failed, graph name is %s", original_graph->GetName().c_str());
361 : auto &graph_info = graph_2_graph_partition_info_[original_graph];
362 : GE_ASSERT_TRUE(graph_info.corresponding_node_in_partitions_.count(parent_node->GetName()) != 0U,
363 : "[Check][Param] Find corresponding node failed, parent node name is %s",
364 : parent_node->GetName().c_str());
365 : const auto &corresponding_node = graph_info.corresponding_node_in_partitions_[parent_node->GetName()];
366 : GE_ASSERT_NOTNULL(corresponding_node,
367 : "[Check][Param] Get null node in corresponding_node_in_partitions_, parent node name is %s",
368 : parent_node->GetName().c_str());
369 : merged_sub_graph->SetParentNode(corresponding_node);
370 : merged_sub_graph->SetParentGraph(corresponding_node->GetOwnerComputeGraph());
371 :
372 : // merge overflow detection attr to root_graph
373 : if (MergeOverflowAttr(merged_sub_graph, output_merged_compute_graph) != SUCCESS) {
374 : return FAILED;
375 : }
376 : GE_ASSERT_GRAPH_SUCCESS(output_merged_compute_graph->AddSubgraph(sub_graph->GetName(), merged_sub_graph),
377 : "[Call][AddSubgraph] failed, subgraph:%s, merged subgraph:%s.",
378 : sub_graph->GetName().c_str(), merged_sub_graph->GetName().c_str());
379 : }
380 : DNNEngineManager::GetInstance().LogCheckSupportCost();
381 : ClearAllPartitionData();
382 : if (real_ret != SUCCESS) {
383 : auto root_graph = ge::GraphUtils::FindRootGraph(original_compute_graph);
384 : GE_CHECK_NOTNULL(root_graph);
385 : (void)Analyzer::GetInstance()->SaveAnalyzerDataToFile(root_graph->GetSessionID(), root_graph->GetGraphID());
386 : }
387 : return real_ret;
388 : }
389 :
390 : Status ge::EnginePartitioner::FindOverflowAttr(const ge::ComputeGraphPtr &sub_graph,
391 : ge::ComputeGraphPtr &original_graph) {
392 : GE_CHECK_NOTNULL(sub_graph);
393 : GE_CHECK_NOTNULL(original_graph);
394 : if (AttrUtils::HasAttr(original_graph, GLOBALWORKSPACE_TYPE)) {
395 : return SUCCESS;
396 : }
397 : for (const auto &node : sub_graph->GetDirectNode()) {
398 : (void)AttrUtils::GetInt(node->GetOpDesc(), GLOBALWORKSPACE_TYPE, global_workspace_type_);
399 : (void)AttrUtils::GetInt(node->GetOpDesc(), "globalworkspace_size", global_workspace_size_);
400 :
401 : if ((global_workspace_type_ == kOverflowDefaultValue) || (global_workspace_size_ == kOverflowDefaultValue)) {
402 : continue;
403 : }
404 :
405 : (void)AttrUtils::SetInt(original_graph, GLOBALWORKSPACE_TYPE, global_workspace_type_);
406 : (void)AttrUtils::SetInt(original_graph, "globalworkspace_size", global_workspace_size_);
407 : break;
408 : }
409 : return SUCCESS;
410 : }
411 :
412 : Status ge::EnginePartitioner::MergeSubGraph(ge::ComputeGraphPtr &output_merged_compute_graph,
413 : const ge::ComputeGraphPtr &original_compute_graph) {
414 : GE_ASSERT_NOTNULL(original_compute_graph, "[Check][Param] original_compute_graph is nullptr.");
415 : if ((graph_2_graph_partition_info_.find(original_compute_graph) == graph_2_graph_partition_info_.end()) ||
416 : (graph_2_subgraph_list_.find(original_compute_graph) == graph_2_subgraph_list_.end())) {
417 : GELOGW("[GraphPartition]: compute_graph has not found, just return original.");
418 : output_merged_compute_graph = original_compute_graph;
419 : return SUCCESS;
420 : }
421 : GraphPartitionInfo &graph_info = graph_2_graph_partition_info_[original_compute_graph];
422 : const auto &sub_graph_list = graph_2_subgraph_list_[original_compute_graph];
423 :
424 : GE_ASSERT_TRUE(graph_info.mode_ == Mode::kMerging,
425 : "[Check][Param] Cannot call merging in partition mode, as mode != %d", Mode::kMerging);
426 : GELOGD("Graph merge starts.");
427 : ComputeGraphPtr new_sub_graph = MakeShared<ComputeGraph>(original_compute_graph->GetName());
428 : GE_CHECK_NOTNULL(new_sub_graph);
429 : // check input param
430 : for (const auto &it : sub_graph_list) {
431 : GE_ASSERT_NOTNULL(it, "[Check][Param] merging sub-graphs failed, sub-graph is nullptr");
432 : // Get overflow detection attr
433 : if (FindOverflowAttr(it->GetSubGraph(), new_sub_graph) != SUCCESS) {
434 : return FAILED;
435 : }
436 : }
437 : bool is_map_empty = graph_info.end_2_pld_.empty() || graph_info.pld_2_end_.empty();
438 : if (is_map_empty) {
439 : if (CheckValidIfEnd2PldEmpty(graph_info, output_merged_compute_graph) != SUCCESS) {
440 : return FAILED;
441 : }
442 : }
443 : output_merged_compute_graph = new_sub_graph;
444 : GE_TRACE_START(MergeSubGraphRemoveNode);
445 : GE_ASSERT_GRAPH_SUCCESS(RemoveNodeAndEdgeBetweenEndPld(output_merged_compute_graph, sub_graph_list, graph_info),
446 : "[Call][RemoveNodeAndEdgeBetweenEndPld] failed, graph:%s",
447 : output_merged_compute_graph->GetName().c_str());
448 : GE_COMPILE_TRACE_TIMESTAMP_END(MergeSubGraphRemoveNode, "EnginePartitioner::MergeGraphRemoveNodeAndEdge");
449 : // flush all nodes' engine of merged graph
450 : GE_TRACE_START(MergeSubGraphEnginePlacerRun);
451 : engine_placer_.SetComputeGraph(output_merged_compute_graph);
452 : GE_CHK_STATUS_RET(engine_placer_.Run(), "[Call][Run] engine_placer run failed, graph:%s",
453 : output_merged_compute_graph->GetName().c_str());
454 : GE_CHK_STATUS_RET(InheritOriginalAttr(original_compute_graph, output_merged_compute_graph),
455 : "[Inherit][OriginalAttr] failed, graph:%s", output_merged_compute_graph->GetName().c_str());
456 : GE_COMPILE_TRACE_TIMESTAMP_END(MergeSubGraphEnginePlacerRun, "EnginePartitioner::MergeGraphEnginePlacerRun");
457 : return UpdateCorrespondNodeInPartitions(output_merged_compute_graph, graph_info);
458 : }
459 :
460 : Status EnginePartitioner::InheritOriginalAttr(const ComputeGraphPtr &original_compute_graph,
461 : ComputeGraphPtr &output_merged_compute_graph) const {
462 : if (original_compute_graph->GetGraphUnknownFlag()) {
463 : output_merged_compute_graph->SetGraphUnknownFlag(true);
464 : for (const auto &node : output_merged_compute_graph->GetDirectNode()) {
465 : ge::AttrUtils::SetBool(node->GetOpDesc(), "OwnerGraphIsUnknown", true);
466 : GELOGD("Set OwnerGraphIsUnknow attr to node[%s], graph [%s]", node->GetName().c_str(),
467 : output_merged_compute_graph->GetName().c_str());
468 : }
469 : }
470 : const std::map<string, GeAttrValue> &original_attrs = AttrUtils::GetAllAttrs(original_compute_graph);
471 : for (auto const &attr_iter : original_attrs) {
472 : if (output_merged_compute_graph->TrySetAttr(attr_iter.first, attr_iter.second) != GRAPH_SUCCESS) {
473 : GELOGW("Set inherit original attr[%s] failed, Please Check.", attr_iter.first.c_str());
474 : }
475 : }
476 : auto *device_mapping = original_compute_graph->GetExtAttr<std::map<DeviceIndex, std::vector<int32_t>>>(
477 : ge::ATTR_NAME_DEVICE_INDEX_TO_LOGIC_DEVICE_ID);
478 : if (device_mapping != nullptr) {
479 : GE_ASSERT_TRUE(
480 : output_merged_compute_graph->SetExtAttr(ge::ATTR_NAME_DEVICE_INDEX_TO_LOGIC_DEVICE_ID, *device_mapping));
481 : }
482 :
483 : // AttrStore里面属性组没有被拷贝,并且没有提供CopyAllAttrStore方法,暂时先手动拷贝必须的
484 : auto origin_shape_env_attr = original_compute_graph->GetAttrsGroup<ShapeEnvAttr>();
485 : if (origin_shape_env_attr != nullptr) {
486 : auto shape_env_attr = output_merged_compute_graph->GetOrCreateAttrsGroup<ShapeEnvAttr>();
487 : GE_ASSERT_NOTNULL(shape_env_attr);
488 : *shape_env_attr = *origin_shape_env_attr;
489 : }
490 : return SUCCESS;
491 : }
492 :
493 : graphStatus ge::EnginePartitioner::UpdatePldOpDesc(const NodePtr &dst_node, int32_t input_index,
494 : const OpDescPtr &pld_op_desc) const {
495 : GE_ASSERT_NOTNULL(dst_node, "[Check][Param] parameter dst_node is null.");
496 : GE_ASSERT_NOTNULL(pld_op_desc, "[Check][Param] parameter pld_op_desc is null.");
497 : GE_ASSERT_NOTNULL(dst_node->GetOpDesc(), "[Check][Param] parameter dst_node opdesc is null.");
498 : const auto &input_desc = dst_node->GetOpDesc()->GetInputDesc(static_cast<uint32_t>(input_index));
499 : GE_ASSERT_GRAPH_SUCCESS(pld_op_desc->AddOutputDesc(input_desc), "[Add][OutputDesc] to op:%s failed",
500 : pld_op_desc->GetName().c_str());
501 : const auto &pld_op = pld_op_desc->MutableOutputDesc(0);
502 : GE_ASSERT_NOTNULL(pld_op, "[Check][Param] output(0) of op:%s is nullptr.", pld_op_desc->GetName().c_str());
503 : ge::TensorUtils::SetRealDimCnt(*(pld_op_desc->MutableOutputDesc(0).get()),
504 : static_cast<uint32_t>(input_desc.GetShape().GetDims().size()));
505 : return GRAPH_SUCCESS;
506 : }
507 :
508 : graphStatus ge::EnginePartitioner::UpdateEndOpDesc(const NodePtr &src_node, int32_t output_index,
509 : const OpDescPtr &end_op_desc) const {
510 : GE_ASSERT_NOTNULL(src_node, "[Check][Param] src_node is null.");
511 : GE_ASSERT_NOTNULL(src_node->GetOpDesc(), "[Check][Param] src_op_desc is null.");
512 : GE_ASSERT_NOTNULL(end_op_desc, "[Check][Param] end_op_desc is null.");
513 : const auto &output_desc = src_node->GetOpDesc()->GetOutputDesc(static_cast<uint32_t>(output_index));
514 : GE_ASSERT_GRAPH_SUCCESS(end_op_desc->AddInputDesc(output_desc), "[Add][InputDesc] to op:%s failed",
515 : end_op_desc->GetName().c_str());
516 : const auto &end_op_input_tensor0 = end_op_desc->MutableInputDesc(0);
517 : GE_ASSERT_NOTNULL(end_op_input_tensor0, "[Check][Param] input(0) of op:%s is nullptr.",
518 : end_op_desc->GetName().c_str());
519 : ge::TensorUtils::SetRealDimCnt(*(end_op_desc->MutableInputDesc(0).get()),
520 : static_cast<uint32_t>(output_desc.GetShape().GetDims().size()));
521 : return GRAPH_SUCCESS;
522 : }
523 :
524 : graphStatus ge::EnginePartitioner::MakeEndOpNode(const AnchorPtr &out_anchor, const ge::ComputeGraphPtr &end_graph,
525 : NodePtr &new_end_node) {
526 : std::string end_name = kEndType + std::to_string(graph_info_.num_of_pld_end_);
527 : auto end_op_desc = MakeShared<OpDesc>(end_graph->GetName() + "_" + end_name, END);
528 : GE_CHECK_NOTNULL(end_op_desc);
529 : // replace input_desc of end with owner node's desc
530 : int32_t output_index = ge::AnchorUtils::GetIdx(out_anchor);
531 : bool is_need_update_desc = (output_index >= 0) && ((graph_info_.mode_ == Mode::kAtomicEnginePartitioning) ||
532 : (graph_info_.mode_ == Mode::kCompositeEnginePartitioning));
533 : if (is_need_update_desc) {
534 : GE_ASSERT_GRAPH_SUCCESS(UpdateEndOpDesc(out_anchor->GetOwnerNode(), output_index, end_op_desc),
535 : "[Update][EndOpDesc] failed, input index:%d, end_op_desc:%s", output_index,
536 : end_op_desc->GetName().c_str());
537 : } else {
538 : GeTensorDesc input_desc;
539 : GE_ASSERT_GRAPH_SUCCESS(end_op_desc->AddInputDesc(input_desc), "[Add][InputDesc] to op:%s failed, input index %d",
540 : end_op_desc->GetName().c_str(), output_index);
541 : }
542 : new_end_node = end_graph->AddNode(end_op_desc);
543 : return GRAPH_SUCCESS;
544 : }
545 :
546 : graphStatus ge::EnginePartitioner::MakePldOpNode(const AnchorPtr &peer_in_anchor, const NodePtr &src_node,
547 : const ge::ComputeGraphPtr &pld_graph, NodePtr &new_pld_node) {
548 : /// For fe, op id has been set in AddNode,
549 : /// we can take op id of srcNode as the mark of parentId now
550 : const auto &src_node_op_desc = src_node->GetOpDesc();
551 : GE_CHECK_NOTNULL(src_node_op_desc);
552 : const std::string pld_name = kPlaceHolderType + std::to_string(graph_info_.num_of_pld_end_);
553 : auto pld_op_desc = MakeShared<OpDesc>(pld_graph->GetName() + "_" + pld_name, PLACEHOLDER);
554 : GE_CHECK_NOTNULL(pld_op_desc);
555 : // replace output_desc of pld with input node's output desc
556 : int32_t input_index = ge::AnchorUtils::GetIdx(peer_in_anchor);
557 : bool is_need_update_desc = (input_index >= 0) && ((graph_info_.mode_ == Mode::kAtomicEnginePartitioning) ||
558 : (graph_info_.mode_ == Mode::kCompositeEnginePartitioning));
559 : if (is_need_update_desc) {
560 : GE_ASSERT_GRAPH_SUCCESS(UpdatePldOpDesc(peer_in_anchor->GetOwnerNode(), input_index, pld_op_desc),
561 : "[Update][PldOpDesc] failed, output index:%d, pld_op_desc:%s", input_index,
562 : pld_op_desc->GetName().c_str());
563 : } else {
564 : GeTensorDesc output_desc;
565 : GE_ASSERT_GRAPH_SUCCESS(pld_op_desc->AddOutputDesc(output_desc),
566 : "[Add][OutputDesc] to op:%s failed, input index %d", pld_op_desc->GetName().c_str(),
567 : input_index);
568 : }
569 : new_pld_node = pld_graph->AddNode(pld_op_desc);
570 : return GRAPH_SUCCESS;
571 : }
572 :
573 : graphStatus ge::EnginePartitioner::SetPldOpAttr(const NodePtr &src_node, const NodePtr &new_end_node,
574 : const ge::ComputeGraphPtr &end_graph, const AnchorPtr &out_anchor,
575 : const OpDescPtr &pld_op_desc) const {
576 : int64_t node_id = src_node->GetOpDesc()->GetId();
577 : auto src_node_op_desc = src_node->GetOpDesc();
578 : GE_ASSERT_TRUE(AttrUtils::SetInt(pld_op_desc, kPeerIndex, graph_info_.num_of_pld_end_),
579 : "SetInt peerIndex failed of op:%s.", pld_op_desc->GetName().c_str());
580 : GE_ASSERT_TRUE(AttrUtils::SetStr(pld_op_desc, kParentOpType, src_node->GetType()),
581 : "SetStr parentOpType failed of op:%s.", pld_op_desc->GetName().c_str());
582 : GE_ASSERT_TRUE(AttrUtils::SetStr(pld_op_desc, kParentNodeName, src_node->GetName()),
583 : "SetStr parentOpName failed of op:%s.", pld_op_desc->GetName().c_str());
584 : GE_ASSERT_TRUE(pld_op_desc->SetExtAttr(kParentNode, src_node), "SetPldExtAttr parentNode failed of op:%s.",
585 : pld_op_desc->GetName().c_str());
586 : GE_ASSERT_TRUE(
587 : AttrUtils::SetStr(pld_op_desc, ATTR_NAME_PLD_FRONT_NODE_ENGINE_NAME, src_node_op_desc->GetOpEngineName()),
588 : "SetStr frontNodeEngineName failed of op:%s.", pld_op_desc->GetName().c_str());
589 : std::string l2_info_attr;
590 : if (AttrUtils::GetStr(src_node_op_desc, kTaskL2FusionInfo, l2_info_attr)) {
591 : GE_ASSERT_TRUE(AttrUtils::SetStr(pld_op_desc, kTaskL2FusionInfo, l2_info_attr),
592 : "SetStr l2_info_attr failed of op:%s.", src_node_op_desc->GetName().c_str());
593 : }
594 : int64_t anchor_index_for_lxfusion;
595 : if (AttrUtils::GetInt(src_node_op_desc, kDataAnchorIndexForLxfusion, anchor_index_for_lxfusion)) {
596 : GE_ASSERT_TRUE(AttrUtils::SetInt(pld_op_desc, kDataAnchorIndexForLxfusion, anchor_index_for_lxfusion),
597 : "SetInt anchor_index_for_lxfusion failed");
598 : }
599 : GE_ASSERT_TRUE(AttrUtils::SetStr(pld_op_desc, kParentId, end_graph->GetName() + ":" + std::to_string(node_id)),
600 : "SetStr parentId failed of op:%s.", pld_op_desc->GetName().c_str());
601 : GE_ASSERT_TRUE(AttrUtils::SetInt(pld_op_desc, kAnchorIndex, AnchorUtils::GetIdx(out_anchor)),
602 : "SetInt anchorIndex failed of op:%s.", pld_op_desc->GetName().c_str());
603 : GE_ASSERT_TRUE(AttrUtils::SetStr(pld_op_desc, kPeerNodeName, new_end_node->GetName()),
604 : "SetStr _peerNodeName failed of op:%s.", pld_op_desc->GetName().c_str());
605 : return GRAPH_SUCCESS;
606 : }
607 :
608 : graphStatus ge::EnginePartitioner::SetEndOpAttr(const NodePtr &dst_node, const OpDescPtr &end_op_desc) const {
609 : GE_ASSERT_TRUE(AttrUtils::SetInt(end_op_desc, kPeerIndex, graph_info_.num_of_pld_end_),
610 : "SetInt peerIndex failed of op:%s.", end_op_desc->GetName().c_str());
611 : GE_ASSERT_TRUE(AttrUtils::SetStr(end_op_desc, kParentOpType, dst_node->GetType()),
612 : "SetStr parentOpType failed of op:%s", end_op_desc->GetName().c_str());
613 : GE_ASSERT_TRUE(end_op_desc->SetExtAttr(kParentNode, dst_node), "SetEndExtAttr parentNode failed of op:%s",
614 : dst_node->GetName().c_str());
615 : OpDescPtr dst_node_op_desc = dst_node->GetOpDesc();
616 : GE_CHECK_NOTNULL(dst_node_op_desc);
617 : GE_ASSERT_TRUE(
618 : AttrUtils::SetStr(end_op_desc, ATTR_NAME_END_REAR_NODE_ENGINE_NAME, dst_node_op_desc->GetOpEngineName()),
619 : "SetStr rearNodeEngineName failed of op:%s", end_op_desc->GetName().c_str());
620 : return GRAPH_SUCCESS;
621 : }
622 :
623 : graphStatus ge::EnginePartitioner::AddPlaceHolderEndInSrcDstGraph(const AnchorPtr &out_anchor,
624 : const AnchorPtr &peer_in_anchor,
625 : const ge::ComputeGraphPtr &pld_graph,
626 : const ge::ComputeGraphPtr &end_graph) {
627 : const auto &src_node = out_anchor->GetOwnerNode();
628 : const auto &dst_node = peer_in_anchor->GetOwnerNode();
629 : // link input -> end
630 : NodePtr new_end_node = nullptr;
631 : GE_ASSERT_GRAPH_SUCCESS(MakeEndOpNode(out_anchor, end_graph, new_end_node),
632 : "[Make][EndOpNode] failed, pld_graph[%s], end_graph[%s], src_node[%s], dst_node[%s]",
633 : pld_graph->GetName().c_str(), end_graph->GetName().c_str(), src_node->GetName().c_str(),
634 : dst_node->GetName().c_str());
635 : GE_ASSERT_NOTNULL(new_end_node, "[Add][Node] in graph:%s failed.", end_graph->GetName().c_str());
636 : GE_ASSERT_GRAPH_SUCCESS(SetEndOpAttr(dst_node, new_end_node->GetOpDesc()), "[Set][EndOpAttr] failed, op name[%s].",
637 : new_end_node->GetName().c_str());
638 : GE_ASSERT_GRAPH_SUCCESS(new_end_node->SetOwnerComputeGraph(end_graph),
639 : "[Set][OwnerComputeAttrUtilsGraph] %s for node:%s failed", end_graph->GetName().c_str(),
640 : new_end_node->GetName().c_str());
641 : AnchorPtr end_dst_anchor = GetEndInAnchor(out_anchor, new_end_node);
642 : GE_ASSERT_NOTNULL(end_dst_anchor);
643 : GE_ASSERT_GRAPH_SUCCESS(GraphUtils::AddEdge(out_anchor, end_dst_anchor), "[Add][Edge] from %s to %s failed",
644 : out_anchor->GetOwnerNode()->GetName().c_str(),
645 : end_dst_anchor->GetOwnerNode()->GetName().c_str());
646 : NodePtr new_pld_node = nullptr;
647 : GE_ASSERT_GRAPH_SUCCESS(MakePldOpNode(peer_in_anchor, src_node, pld_graph, new_pld_node),
648 : "[Make][PldOpNode] failed, pld_graph[%s], end_graph[%s], src_node[%s], dst_node[%s]",
649 : pld_graph->GetName().c_str(), end_graph->GetName().c_str(), src_node->GetName().c_str(),
650 : dst_node->GetName().c_str());
651 : GE_ASSERT_NOTNULL(new_pld_node, "[Add][Node] in graph:%s failed.", pld_graph->GetName().c_str());
652 : GE_ASSERT_GRAPH_SUCCESS(SetPldOpAttr(src_node, new_end_node, end_graph, out_anchor, new_pld_node->GetOpDesc()),
653 : "[Set][PldOpAttr] failed, op name[%s].", new_pld_node->GetName().c_str());
654 : GE_ASSERT_GRAPH_SUCCESS(new_pld_node->SetOwnerComputeGraph(pld_graph),
655 : "[Set][OwnerComputeGraph] for node:%s failed, graph:%s", new_pld_node->GetName().c_str(),
656 : pld_graph->GetName().c_str());
657 : AnchorPtr pld_src_anchor = GetPldOutAnchor(new_pld_node, peer_in_anchor);
658 : // link placeHolder -> computeNode
659 : GE_CHECK_NOTNULL(pld_src_anchor);
660 : GE_ASSERT_GRAPH_SUCCESS(GraphUtils::AddEdge(pld_src_anchor, peer_in_anchor), "[Add][Edge] from %s to %s failed",
661 : pld_src_anchor->GetOwnerNode()->GetName().c_str(),
662 : peer_in_anchor->GetOwnerNode()->GetName().c_str());
663 : // do not care over flow
664 : graph_info_.num_of_pld_end_++;
665 : graph_info_.index_2_end_[graph_info_.num_of_pld_end_] = new_end_node;
666 : graph_info_.pld_2_end_[new_pld_node] = new_end_node;
667 : graph_info_.end_2_pld_[new_end_node] = new_pld_node;
668 : return SUCCESS;
669 : }
670 :
671 : Status ge::EnginePartitioner::LinkInput2EndRemoveOrginalLink(const ge::NodePtr &input_node,
672 : const ge::ComputeGraphPtr &src_graph,
673 : const ge::ComputeGraphPtr &dst_graph) {
674 : if ((input_node == nullptr) || (src_graph == nullptr) || (dst_graph == nullptr)) {
675 : REPORT_INNER_ERR_MSG("E19999", "Param input_node or src_graph or dst_graph is nullptr, check invalid.");
676 : GELOGE(FAILED, "[Check][Param] parameter input_node or src_graph or dst_graph is nullptr.");
677 : return FAILED;
678 : }
679 : // get the original anchors and remove the original link
680 : for (const auto &out_data_anchor : input_node->GetAllOutAnchors()) {
681 : for (auto &peer_in_anchor : out_data_anchor->GetPeerAnchors()) {
682 : if (peer_in_anchor->GetOwnerNode()->GetType() != kEndType) {
683 : if (GraphUtils::RemoveEdge(out_data_anchor, peer_in_anchor) != GRAPH_SUCCESS) {
684 : REPORT_INNER_ERR_MSG("E19999", "RemoveEdge between %s and %s failed.",
685 : out_data_anchor->GetOwnerNode()->GetName().c_str(),
686 : peer_in_anchor->GetOwnerNode()->GetName().c_str());
687 : GELOGE(FAILED, "[Remove][Edge] between %s and %s failed.", out_data_anchor->GetOwnerNode()->GetName().c_str(),
688 : peer_in_anchor->GetOwnerNode()->GetName().c_str());
689 : return FAILED;
690 : }
691 : // link input -> end
692 : auto ret = AddPlaceHolderEndInSrcDstGraph(out_data_anchor, peer_in_anchor, src_graph, dst_graph);
693 : if (ret != SUCCESS) {
694 : GELOGE(GE_GRAPH_ADD_PLC_END_FAILED, "[Call][AddPlaceHolderEndInSrcDstGraph] failed, ret:%d.", ret);
695 : return ret;
696 : }
697 : } else {
698 : auto end_node = peer_in_anchor->GetOwnerNode();
699 : if (GraphUtils::RemoveJustNode(src_graph, end_node) != GRAPH_SUCCESS) {
700 : REPORT_INNER_ERR_MSG("E19999", "RemoveJustNode %s from graph:%s failed.", end_node->GetName().c_str(),
701 : src_graph->GetName().c_str());
702 : GELOGE(FAILED, "[Remove][JustNode] %s from graph:%s failed.", end_node->GetName().c_str(),
703 : src_graph->GetName().c_str());
704 : return FAILED;
705 : }
706 : if (end_node->SetOwnerComputeGraph(dst_graph) != GRAPH_SUCCESS) {
707 : REPORT_INNER_ERR_MSG("E19999", "SetOwnerComputeGraph for node:%s failed, graph:%s.",
708 : end_node->GetName().c_str(), dst_graph->GetName().c_str());
709 : GELOGE(FAILED, "[Set][OwnerComputeGraph] to node:%s failed, graph:%s.", end_node->GetName().c_str(),
710 : dst_graph->GetName().c_str());
711 : return FAILED;
712 : }
713 : if (dst_graph->AddNode(end_node) == nullptr) {
714 : REPORT_INNER_ERR_MSG("E19999", "AddNode %s in graph:%s failed.", end_node->GetName().c_str(),
715 : dst_graph->GetName().c_str());
716 : GELOGE(FAILED, "[Add][Node] %s in graph:%s failed.", end_node->GetName().c_str(),
717 : dst_graph->GetName().c_str());
718 : return FAILED;
719 : }
720 : }
721 : }
722 : }
723 : return SUCCESS;
724 : }
725 :
726 : Status ge::EnginePartitioner::PutInputNodesInSubGraph(const ge::ComputeGraphPtr &src_graph,
727 : const ge::ComputeGraphPtr &dst_graph) {
728 : GE_CHECK_NOTNULL(src_graph);
729 : GE_CHECK_NOTNULL(dst_graph);
730 : for (const auto &input_node : src_graph->GetDirectNode()) {
731 : if (IsDataLike(input_node)) {
732 : if (input_node->SetOwnerComputeGraph(dst_graph) != GRAPH_SUCCESS) {
733 : REPORT_INNER_ERR_MSG("E19999", "SetOwnerComputeGraph for node:%s failed, graph:%s.",
734 : input_node->GetName().c_str(), dst_graph->GetName().c_str());
735 : GELOGE(FAILED, "[Set][OwnerComputeGraph] for node:%s failed, graph:%s.", input_node->GetName().c_str(),
736 : dst_graph->GetName().c_str());
737 : return FAILED;
738 : }
739 : // remove input node from src_graph
740 : if (GraphUtils::RemoveJustNode(src_graph, input_node) != GRAPH_SUCCESS) {
741 : REPORT_INNER_ERR_MSG("E19999", "RemoveJustNode %s from graph:%s failed.", input_node->GetName().c_str(),
742 : src_graph->GetName().c_str());
743 : GELOGE(FAILED, "[Remove][JustNode] %s from graph:%s failed.", input_node->GetName().c_str(),
744 : src_graph->GetName().c_str());
745 : return FAILED;
746 : }
747 : // add input node to dst_graph
748 : if (dst_graph->AddNode(input_node) == nullptr) {
749 : REPORT_INNER_ERR_MSG("E19999", "AddNode %s in graph:%s failed.", input_node->GetName().c_str(),
750 : src_graph->GetName().c_str());
751 : GELOGE(FAILED, "[Add][Node] %s in graph:%s failed.", input_node->GetName().c_str(),
752 : src_graph->GetName().c_str());
753 : return FAILED;
754 : }
755 : if (LinkInput2EndRemoveOrginalLink(input_node, src_graph, dst_graph) != ge::SUCCESS) {
756 : GELOGE(FAILED, "[Call][LinkInput2EndRemoveOrginalLink] failed.");
757 : return FAILED;
758 : }
759 : }
760 : }
761 : return SUCCESS;
762 : }
763 :
764 : void ge::EnginePartitioner::AddNewGraphToPartition(const ge::ComputeGraphPtr &input_graph,
765 : const std::string &engine_name) {
766 : if (input_graph == nullptr) {
767 : GELOGW("[EnginePartitioner]: input_graph is null, engine name is %s", engine_name.c_str());
768 : return;
769 : }
770 : graph_info_.partitions_[input_graph] = engine_name;
771 : }
772 :
773 : bool ge::EnginePartitioner::IsDataLike(ge::NodePtr node) const {
774 : const auto &node_type = node->GetType();
775 : return (node_type == CONSTANT) || OpTypeUtils::IsDataNode(node_type) || (node_type == CONSTANTOP) ||
776 : OpTypeUtils::IsVarLikeNode(node_type);
777 : }
778 :
779 : bool ge::EnginePartitioner::HasNoInput(const ge::NodePtr &node) const {
780 : if (node == nullptr) {
781 : GELOGE(FAILED, "[Check][Param] node is nullptr.");
782 : return true;
783 : }
784 : return node->GetInNodesSize() == 0UL;
785 : }
786 :
787 : Status ge::EnginePartitioner::InitializeInputClusters(const NodePtr &node, const ClusterPtr &cluster, size_t index) {
788 : auto node_id = node->GetOpDesc()->GetId();
789 : for (const auto &parent : node->GetInAllNodes()) {
790 : GE_CHECK_NOTNULL(parent->GetOpDesc());
791 : auto parent_id = parent->GetOpDesc()->GetId();
792 : if (parent_id < node_id) {
793 : const auto iter = graph_info_.node_2_cluster_.find(parent);
794 : GE_CHK_BOOL_RET_STATUS(iter != graph_info_.node_2_cluster_.cend(), FAILED,
795 : "[Check][Param] node[%s]id[%ld]'s parent_node[%s]id[%ld] should make cluster in advance",
796 : node->GetOpDesc()->GetName().c_str(), node_id, parent->GetOpDesc()->GetName().c_str(),
797 : parent_id);
798 : cluster->in_clu_.insert(iter->second->index_);
799 : iter->second->out_clu_.insert(index);
800 : }
801 : }
802 : return SUCCESS;
803 : }
804 :
805 : Status ge::EnginePartitioner::Initialize(const ge::ComputeGraphPtr &compute_graph, Mode mode) {
806 : GELOGI("Initialize starts, Engine partition mode: %d.", static_cast<int32_t>(graph_info_.mode_));
807 : const auto &node_engine_map = GetNodeEngineMap();
808 : size_t temp_index = 0;
809 : std::map<NodePtr, OpInfo> nodes_to_op_infos;
810 : MarkCvParallelAivNodes(compute_graph);
811 : for (const auto &node : compute_graph->GetDirectNode()) {
812 : std::string temp_stream;
813 : const auto op_desc = node->GetOpDesc();
814 : GE_CHECK_NOTNULL(op_desc);
815 : // node opdesc has been checked before
816 : (void)AttrUtils::GetStr(op_desc, ATTR_NAME_STREAM_LABEL, temp_stream);
817 : std::string temp_user_stream;
818 : (void)AttrUtils::GetStr(op_desc, public_attr::USER_STREAM_LABEL, temp_user_stream);
819 : ClusterPtr new_cluster;
820 : // data like node without input should be handle specific
821 : if (HasNoInput(node) && IsDataLike(node)) {
822 : // data类节点不应该打上用户流标签,后续逻辑流分配会报错
823 : if (!temp_user_stream.empty()) {
824 : (void)AttrUtils::SetStr(op_desc, public_attr::USER_STREAM_LABEL, "");
825 : temp_user_stream.clear();
826 : }
827 :
828 : ClusterPtr cluster = MakeShared<Cluster>(temp_index, kEngineDefaultData, temp_stream, temp_user_stream);
829 : new_cluster = cluster;
830 : } else {
831 : if (node_engine_map.count(node) == 0) {
832 : bool is_check_support_success = false;
833 : std::set<std::string> exclude_engines;
834 : DNNEngineManager::GetExcludeEngines(exclude_engines);
835 : OpInfo op_info;
836 : GE_CHK_STATUS_RET(engine_placer_.SelectEngine(node, exclude_engines, is_check_support_success, op_info),
837 : "[Check][Param] node[%s] does not owner engine!", node->GetName().c_str());
838 : nodes_to_op_infos.emplace(node, op_info);
839 : }
840 : GE_ASSERT_TRUE(node_engine_map.count(node) > 0, "Failed to find node:%s(%s) in node engine map, mode:%d",
841 : node->GetName().c_str(), node->GetType().c_str(), static_cast<int32_t>(graph_info_.mode_));
842 : std::string engine_name = GenClusterEngineName(node, mode, node_engine_map);
843 : ClusterPtr cluster = MakeShared<Cluster>(temp_index, engine_name, temp_stream, temp_user_stream);
844 : new_cluster = cluster;
845 : }
846 : GE_ASSERT_NOTNULL(new_cluster, "[Allocate][Cluster] failed, index:%zu", temp_index);
847 : new_cluster->nodes_.push_back(node);
848 : if (AttrUtils::HasAttr(op_desc, kEnableCvParallel)) {
849 : new_cluster->engine_name_ = kVectorEngineName;
850 : }
851 :
852 : if (!HasNoInput(node)) {
853 : GE_CHK_STATUS_RET(InitializeInputClusters(node, new_cluster, temp_index),
854 : "Failed to init input clusters of cluster:%zu", temp_index);
855 : }
856 : graph_info_.node_2_cluster_[node] = new_cluster;
857 : graph_info_.clusters_[temp_index] = new_cluster;
858 : GELOGD("Node name is %s, engine is %s, cluster index is %zu, stream label is %s", node->GetName().c_str(),
859 : new_cluster->engine_name_.c_str(), new_cluster->index_, new_cluster->stream_label_.c_str());
860 : temp_index++;
861 : }
862 : DNNEngineManager::UpdateOpDescsWithOpInfos(nodes_to_op_infos);
863 : GELOGD("Initialize ends.");
864 : return SUCCESS;
865 : }
866 :
867 : Status ge::EnginePartitioner::AddPartitionsToGraphNode(std::vector<ge::SubGraphInfoPtr> &output_subgraphs,
868 : ge::ComputeGraphPtr compute_graph) {
869 : const std::string &input_subgraph_name = "inputNodesSubGraph";
870 : std::string session_graph_id;
871 : if (!AttrUtils::GetStr(*compute_graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id)) {
872 : GELOGW("Get graph session_graph_id attr failed.");
873 : return INTERNAL_ERROR;
874 : }
875 : // the output_subgraphs have topological order
876 : for (const auto &sub_graph : graph_info_.rank_2_partitions_) {
877 : if (graph_info_.partitions_.find(sub_graph) == graph_info_.partitions_.end()) {
878 : REPORT_INNER_ERR_MSG("E19999", "partition is null, subgraph:%s", sub_graph->GetName().c_str());
879 : GELOGE(GE_GRAPH_EMPTY_PARTITION, "[Check][Param] partition is null, subgraph:%s", sub_graph->GetName().c_str());
880 : return FAILED;
881 : }
882 : auto &engine_name = graph_info_.partitions_.at(sub_graph);
883 : (void)AttrUtils::SetStr(sub_graph, ATTR_NAME_PARENT_GRAPH_NAME, compute_graph->GetName());
884 : (void)sub_graph->SetExtAttr("part_src_graph", compute_graph);
885 : GELOGD("set attr success. subgraph(%s) with parent graph(%s)", sub_graph->GetName().c_str(),
886 : compute_graph->GetName().c_str());
887 : GE_DUMP(sub_graph, sub_graph->GetName() + "_" + mode_2_str_[graph_info_.mode_]);
888 : if (!session_graph_id.empty()) {
889 : GE_ASSERT_TRUE(AttrUtils::SetStr(sub_graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id),
890 : "SetStr ATTR_NAME_SESSION_GRAPH_ID[%s] failed of subgraph:%s", session_graph_id.c_str(),
891 : sub_graph->GetName().c_str());
892 : }
893 : // flush parent node of subgraph
894 : if (compute_graph->GetParentNode() == nullptr) {
895 : GE_ASSERT_TRUE(AttrUtils::SetBool(sub_graph, ATTR_NAME_IS_ROOT_GRAPH, true),
896 : "Set attr ATTR_NAME_IS_ROOT_GRAPH[%s] failed of subgraph:%s", session_graph_id.c_str(),
897 : sub_graph->GetName().c_str());
898 : } else {
899 : sub_graph->SetParentNode(compute_graph->GetParentNode());
900 : }
901 :
902 : sub_graph->SetGraphUnknownFlag(compute_graph->GetGraphUnknownFlag());
903 : auto sgi = MakeShared<SubGraphInfo>();
904 : if (SetMemberForSubGraphInfo(sgi, sub_graph, engine_name) != SUCCESS) {
905 : REPORT_INNER_ERR_MSG("E19999", "set members for subgraph:%s info failed", sub_graph->GetName().c_str());
906 : GELOGE(FAILED, "set members for subgraph:%s info failed", sub_graph->GetName().c_str());
907 : return FAILED;
908 : }
909 :
910 : AddEndPldInformationToSubGraphInfo(sgi);
911 : GELOGD("[EnginePartitioner]: subGraph engine name is %s, graph name is %s, stream label[%s], user stream label[%s]",
912 : engine_name.c_str(), sub_graph->GetName().c_str(),
913 : sgi->GetStreamLabel().empty() ? "null" : sgi->GetStreamLabel().c_str(),
914 : sgi->GetUserStreamLabel().empty() ? "null" : sgi->GetUserStreamLabel().c_str());
915 : if (engine_name != input_subgraph_name) { // do not add Data subGraph into SubGraphInfo
916 : output_subgraphs.push_back(sgi);
917 : } else {
918 : graph_2_input_subgraph_[compute_graph] = sgi;
919 : }
920 : }
921 : return SUCCESS;
922 : }
923 :
924 : Status ge::EnginePartitioner::SetMemberForSubGraphInfo(ge::SubGraphInfoPtr &sgi, const ComputeGraphPtr &sub_graph,
925 : const std::string &engine_name) {
926 : // The caller guarantee the sub_graph parm is not null
927 : if (sgi == nullptr) {
928 : REPORT_INNER_ERR_MSG("E19999", "allocate memory for SubGraphInfo failed.");
929 : GELOGE(GE_GRAPH_PARAM_NULLPTR, "[Allocate][Memory] for SubGraphInfo failed.");
930 : return FAILED;
931 : }
932 : // set engine name
933 : sgi->SetEngineName(engine_name);
934 : // set stream label
935 : std::string sub_graph_stream;
936 : GE_ASSERT_TRUE(sub_graph->GetDirectNodesSize() != 0, "[Check][Param]graph:%s has no node",
937 : sub_graph->GetName().c_str());
938 : if (AttrUtils::GetStr(sub_graph->GetDirectNodePtr().at(0)->GetOpDesc(), ATTR_NAME_STREAM_LABEL, sub_graph_stream)) {
939 : sgi->SetStreamLabel(sub_graph_stream);
940 : }
941 : std::string sub_graph_user_stream;
942 : if (AttrUtils::GetStr(sub_graph->GetDirectNodePtr().at(0)->GetOpDesc(), public_attr::USER_STREAM_LABEL,
943 : sub_graph_user_stream)) {
944 : sgi->SetUserStreamLabel(sub_graph_user_stream);
945 : }
946 : /// for now inputFlag is the same before and after partition. It should
947 : /// be changed according to the real partition
948 : std::vector<bool> sub_graph_input(graph_info_.input_size_, true);
949 : std::vector<bool> sub_graph_output(graph_info_.output_size_, true);
950 : sgi->SetSubGraph(sub_graph);
951 : sgi->SetOutputFlag(sub_graph_output);
952 : sgi->SetInputFlag(sub_graph_input);
953 : sgi->SetOutputContext(graph_info_.output_name_);
954 : return SUCCESS;
955 : }
956 :
957 : // check if two clusters can merge
958 : bool ge::EnginePartitioner::IsMergeable(size_t parent_cluster, size_t child_cluster, size_t upper_bound) {
959 : if ((graph_info_.clusters_[parent_cluster] == nullptr) || (graph_info_.clusters_[parent_cluster]->nodes_.empty()) ||
960 : (graph_info_.clusters_[child_cluster] == nullptr) || (graph_info_.clusters_[child_cluster]->nodes_.empty())) {
961 : return false;
962 : }
963 : if ((current_mode_ == Mode::kSecondPartitioning) &&
964 : ((!graph_info_.clusters_[parent_cluster]->user_stream_label_.empty()) &&
965 : (graph_info_.clusters_[parent_cluster]->user_stream_label_ ==
966 : graph_info_.clusters_[child_cluster]->user_stream_label_))) {
967 : GELOGD(
968 : "Parent cluster[%zu] engine[%s] stream label[%s] user stream label[%s], child cluster[%zu] engine[%s] stream "
969 : "label[%s] user stream label[%s] should merge",
970 : parent_cluster, graph_info_.clusters_[parent_cluster]->engine_name_.c_str(),
971 : graph_info_.clusters_[parent_cluster]->stream_label_.c_str(),
972 : graph_info_.clusters_[parent_cluster]->user_stream_label_.c_str(), child_cluster,
973 : graph_info_.clusters_[child_cluster]->engine_name_.c_str(),
974 : graph_info_.clusters_[child_cluster]->stream_label_.c_str(),
975 : graph_info_.clusters_[child_cluster]->user_stream_label_.c_str());
976 : return true;
977 : }
978 : // Check if parent_cluster,child_cluster has same engine or stream label
979 : if ((graph_info_.clusters_[parent_cluster]->engine_name_ != graph_info_.clusters_[child_cluster]->engine_name_) ||
980 : (graph_info_.clusters_[parent_cluster]->stream_label_ != graph_info_.clusters_[child_cluster]->stream_label_)) {
981 : GELOGD(
982 : "Parent cluster[%zu] engine[%s] stream label[%s] user stream label[%s], child cluster[%zu] engine[%s] stream "
983 : "label[%s] user stream label[%s] cannot merge",
984 : parent_cluster, graph_info_.clusters_[parent_cluster]->engine_name_.c_str(),
985 : graph_info_.clusters_[parent_cluster]->stream_label_.c_str(),
986 : graph_info_.clusters_[parent_cluster]->user_stream_label_.c_str(), child_cluster,
987 : graph_info_.clusters_[child_cluster]->engine_name_.c_str(),
988 : graph_info_.clusters_[child_cluster]->stream_label_.c_str(),
989 : graph_info_.clusters_[child_cluster]->user_stream_label_.c_str());
990 : return false;
991 : }
992 : // Check if parent_cluster,child_cluster is reachable
993 : RemoveEdge(parent_cluster, child_cluster);
994 : // Check if there is a path between parent and child, if return true, cannot merge
995 : if (HasSecondPath(parent_cluster, child_cluster, upper_bound)) {
996 : GELOGD("Find second path from %zu to %zu, upper bound is %zu", parent_cluster, child_cluster, upper_bound);
997 : InsertEdge(parent_cluster, child_cluster);
998 : return false;
999 : }
1000 : InsertEdge(parent_cluster, child_cluster);
1001 : return true;
1002 : }
1003 :
1004 : void ge::EnginePartitioner::MergeTwoClusters(size_t parent_cluster, size_t &child_cluster) {
1005 : // check which index is bigger
1006 : size_t big_cluster, small_cluster;
1007 : size_t child_cluster_original = child_cluster;
1008 : if (parent_cluster > child_cluster) {
1009 : small_cluster = child_cluster;
1010 : big_cluster = parent_cluster;
1011 : } else {
1012 : big_cluster = child_cluster;
1013 : small_cluster = parent_cluster;
1014 : // flush child_cluster, because it has been modified
1015 : child_cluster = small_cluster;
1016 : }
1017 :
1018 : // update node_2_cluster_ map
1019 : for (const auto &node : graph_info_.clusters_[big_cluster]->nodes_) {
1020 : graph_info_.node_2_cluster_[node] = graph_info_.clusters_[small_cluster];
1021 : }
1022 : // merge nodes
1023 : graph_info_.clusters_[small_cluster]->nodes_.splice(graph_info_.clusters_[small_cluster]->nodes_.cend(),
1024 : graph_info_.clusters_[big_cluster]->nodes_);
1025 : // remove child_cluster's out parent_cluster's in between child_cluster and parent_cluster
1026 : // this should be called before `merge all input & output to small cluster`
1027 : RemoveEdge(parent_cluster, child_cluster_original);
1028 : // merge all input & output to small cluster
1029 : graph_info_.clusters_[small_cluster]->in_clu_.insert(graph_info_.clusters_[big_cluster]->in_clu_.cbegin(),
1030 : graph_info_.clusters_[big_cluster]->in_clu_.cend());
1031 : graph_info_.clusters_[small_cluster]->out_clu_.insert(graph_info_.clusters_[big_cluster]->out_clu_.cbegin(),
1032 : graph_info_.clusters_[big_cluster]->out_clu_.cend());
1033 : // update in/out of the cluster with bigger index
1034 : for (auto in_clu : graph_info_.clusters_[big_cluster]->in_clu_) {
1035 : GE_CHECK_NOTNULL_JUST_RETURN(graph_info_.clusters_[in_clu]);
1036 : graph_info_.clusters_[in_clu]->out_clu_.insert(small_cluster);
1037 : graph_info_.clusters_[in_clu]->out_clu_.erase(big_cluster);
1038 : }
1039 : for (auto out_clu : graph_info_.clusters_[big_cluster]->out_clu_) {
1040 : GE_CHECK_NOTNULL_JUST_RETURN(graph_info_.clusters_[out_clu]);
1041 : graph_info_.clusters_[out_clu]->in_clu_.insert(small_cluster);
1042 : graph_info_.clusters_[out_clu]->in_clu_.erase(big_cluster);
1043 : }
1044 : graph_info_.clusters_[big_cluster] = graph_info_.clusters_[small_cluster];
1045 : }
1046 :
1047 : void ge::EnginePartitioner::RemoveEdge(size_t parent_cluster, size_t child_cluster) {
1048 : graph_info_.clusters_[child_cluster]->in_clu_.erase(parent_cluster);
1049 : graph_info_.clusters_[parent_cluster]->out_clu_.erase(child_cluster);
1050 : }
1051 :
1052 : void ge::EnginePartitioner::InsertEdge(size_t from, size_t to) {
1053 : if (from == to) {
1054 : return;
1055 : }
1056 : if (!graph_info_.clusters_[from]->out_clu_.insert(to).second) {
1057 : // edge has already exists
1058 : return;
1059 : }
1060 : graph_info_.clusters_[to]->in_clu_.insert(from);
1061 : }
1062 :
1063 : Status ge::EnginePartitioner::MarkClustersWithConsistantId() {
1064 : GELOGI("MarkClustersWithConsistantId starts. cluster size is %zu", graph_info_.clusters_.size());
1065 : size_t cluster_size = graph_info_.clusters_.size();
1066 : std::vector<size_t> cluster_id;
1067 : for (size_t i = 0UL; i < cluster_size; i++) {
1068 : auto cluster = graph_info_.clusters_[i];
1069 : GE_ASSERT_NOTNULL(cluster);
1070 : if (cluster->engine_name_ != kEngineDefaultData) {
1071 : cluster_id.emplace_back(i);
1072 : }
1073 : }
1074 : if (cluster_id.empty()) {
1075 : return SUCCESS;
1076 : }
1077 : for (size_t i = cluster_id.size() - 1UL; i > 0UL; i--) {
1078 : auto cur_cluster_id = cluster_id[i];
1079 : auto next_cluster_id = cluster_id[i - 1UL];
1080 : auto merged_id = cur_cluster_id;
1081 : if (IsMergeable(next_cluster_id, merged_id, merged_id)) {
1082 : MergeTwoClusters(next_cluster_id, merged_id);
1083 : GELOGD("Merging cluster %zu and %zu to %zu", cur_cluster_id, next_cluster_id, merged_id);
1084 : }
1085 : }
1086 : GELOGD("MarkClustersWithConsistantId ends.");
1087 : return SUCCESS;
1088 : }
1089 :
1090 : void ge::EnginePartitioner::MarkClusters() {
1091 : GELOGI("MarkClusters starts. cluster size is %zu", graph_info_.clusters_.size());
1092 : size_t cluster_size = graph_info_.clusters_.size();
1093 : for (size_t child_cluster = 0; child_cluster < cluster_size; child_cluster++) {
1094 : auto found_child_cluster = graph_info_.clusters_[child_cluster];
1095 : if (found_child_cluster == nullptr) {
1096 : GELOGW("cannot found child_cluster is %zu", child_cluster);
1097 : continue;
1098 : }
1099 : auto copy_parents_clusters = found_child_cluster->in_clu_;
1100 : std::vector<size_t> ordered_cluster;
1101 : for (const auto &parent_cluster : copy_parents_clusters) {
1102 : ordered_cluster.emplace_back(parent_cluster);
1103 : }
1104 : // sort cluster according to it's output amount
1105 : auto comp_func = [this](const size_t &parent_cluster1, const size_t &parent_cluster2) -> bool {
1106 : return graph_info_.clusters_[parent_cluster1]->out_clu_.size() <
1107 : graph_info_.clusters_[parent_cluster2]->out_clu_.size();
1108 : };
1109 : std::sort(ordered_cluster.begin(), ordered_cluster.end(), comp_func);
1110 : auto child_merged = child_cluster;
1111 : for (const auto &parent_cluster : ordered_cluster) {
1112 : if (IsMergeable(parent_cluster, child_merged, child_cluster)) {
1113 : MergeTwoClusters(parent_cluster, child_merged);
1114 : GELOGD("Merging cluster %zu and %zu to %zu", parent_cluster, child_cluster, child_merged);
1115 : }
1116 : }
1117 : }
1118 : GELOGD("MarkClusters ends.");
1119 : }
1120 :
1121 : Status ge::EnginePartitioner::SplitNodeInputs(const NodePtr &node, const NodePtr &corresponding_node,
1122 : const ClusterPtr &child_cluster) {
1123 : for (const auto &in_anchor : node->GetAllInAnchors()) {
1124 : GELOGD("In anchor index is %d", AnchorUtils::GetIdx(in_anchor));
1125 : for (const auto &peer_out_anchor : in_anchor->GetPeerAnchors()) {
1126 : GE_CHECK_NOTNULL(peer_out_anchor->GetOwnerNode()->GetOpDesc());
1127 : GELOGD("Peer out anchor index is %d", AnchorUtils::GetIdx(peer_out_anchor));
1128 : // Normally, all nodes have a copy in corresponding_node_in_partitions_, so function at cannot be exception
1129 : const auto iter = graph_info_.corresponding_node_in_partitions_.find(peer_out_anchor->GetOwnerNode()->GetName());
1130 : GE_CHK_BOOL_RET_STATUS(iter != graph_info_.corresponding_node_in_partitions_.cend(), FAILED,
1131 : "[Check][Param] node[%s]id[%ld]'s parent_node[%s]id[%ld]"
1132 : "should make corresponding in advance",
1133 : node->GetOpDesc()->GetName().c_str(), node->GetOpDesc()->GetId(),
1134 : peer_out_anchor->GetOwnerNode()->GetOpDesc()->GetName().c_str(),
1135 : peer_out_anchor->GetOwnerNode()->GetOpDesc()->GetId());
1136 : const auto &parent_node = iter->second;
1137 : GE_CHECK_NOTNULL(parent_node);
1138 : GELOGD("Parent node name is %s", parent_node->GetName().c_str());
1139 : // add edge
1140 : const auto &src_anchor = parent_node->GetOutAnchor(AnchorUtils::GetIdx(peer_out_anchor));
1141 : const auto &dst_anchor = corresponding_node->GetInAnchor(AnchorUtils::GetIdx(in_anchor));
1142 : // if child and parent's cluster is not same, add plc and end
1143 : const auto &parent_cluster = graph_info_.node_2_cluster_[peer_out_anchor->GetOwnerNode()];
1144 : GE_CHECK_NOTNULL(parent_cluster);
1145 : if (parent_cluster != child_cluster) {
1146 : GELOGD("Parent cluster is %zu, child_cluster is %zu", parent_cluster->index_, child_cluster->index_);
1147 : GE_CHK_STATUS_RET(AddPlaceHolderEnd(peer_out_anchor, in_anchor),
1148 : "[AddPlaceHolderEnd] failed, out_anchor:%s index:%d, in_anchor:%s index:%d.",
1149 : peer_out_anchor->GetOwnerNode()->GetName().c_str(), AnchorUtils::GetIdx(peer_out_anchor),
1150 : in_anchor->GetOwnerNode()->GetName().c_str(), AnchorUtils::GetIdx(in_anchor));
1151 : } else { // parent and child in the same cluster, add edge
1152 : GELOGD("AddEdge from parent cluster %zu to child %zu", parent_cluster->index_, child_cluster->index_);
1153 : GE_CHK_STATUS_RET(GraphUtils::AddEdge(src_anchor, dst_anchor), "Add edge from %s to %s failed",
1154 : peer_out_anchor->GetOwnerNode()->GetName().c_str(),
1155 : in_anchor->GetOwnerNode()->GetName().c_str());
1156 : }
1157 : }
1158 : }
1159 : return SUCCESS;
1160 : }
1161 :
1162 : Status ge::EnginePartitioner::SplitSubGraphs(const ge::ComputeGraphPtr &compute_graph) {
1163 : GELOGD("SplitSubGraphs starts.");
1164 : // Create graphs for all clusters
1165 : std::unordered_set<ClusterPtr> cluster_set;
1166 : // add pld&end
1167 : for (const auto &node : compute_graph->GetDirectNode()) {
1168 : GE_CHECK_NOTNULL(node->GetOpDesc());
1169 : GELOGD("Node name is %s.", node->GetName().c_str());
1170 : const auto &child_cluster = graph_info_.node_2_cluster_[node];
1171 : ge::ComputeGraphPtr corresponding_graph;
1172 : // unordered_set's insert returns a pair, second of pair is bool
1173 : if (!cluster_set.insert(child_cluster).second) {
1174 : GELOGD("Old sub graph, child_cluster is %zu", child_cluster->index_);
1175 : corresponding_graph = graph_info_.cluster_2_partition_.at(child_cluster);
1176 : } else {
1177 : std::string graph_name = "new_sub_graph" + std::to_string(graph_info_.partitions_.size());
1178 : ComputeGraphPtr new_sub_graph = MakeShared<ge::ComputeGraph>(graph_name);
1179 : GE_ASSERT_NOTNULL(new_sub_graph, "[Allocate][Memory] for ge::ComputeGraph failed.");
1180 : AddNewGraphToPartition(new_sub_graph, child_cluster->engine_name_);
1181 : corresponding_graph = new_sub_graph;
1182 : graph_info_.cluster_2_partition_[child_cluster] = corresponding_graph;
1183 : GELOGD("New sub graph, name is %s", graph_name.c_str());
1184 : }
1185 : // build node to corresponding node map
1186 : NodePtr corresponding_node = corresponding_graph->AddNode(node->GetOpDesc());
1187 : GE_CHECK_NOTNULL(corresponding_node);
1188 : if (!graph_info_.corresponding_node_in_partitions_.insert({node->GetName(), corresponding_node}).second) {
1189 : REPORT_INNER_ERR_MSG("E19999", "Node name %s already existed in graph %s", node->GetName().c_str(),
1190 : compute_graph->GetName().c_str());
1191 : GELOGE(FAILED, "Node name %s already existed in graph %s", node->GetName().c_str(),
1192 : compute_graph->GetName().c_str());
1193 : return FAILED;
1194 : }
1195 : GE_CHK_STATUS_RET(corresponding_node->SetOwnerComputeGraph(corresponding_graph));
1196 : GE_CHK_STATUS_RET(SplitNodeInputs(node, corresponding_node, child_cluster),
1197 : "[Split][NodeInputs] failed, node[%s], child_cluster[%zu]", node->GetName().c_str(),
1198 : child_cluster->index_);
1199 : }
1200 : GELOGD("SplitSubGraphs ends.");
1201 : return SUCCESS;
1202 : }
1203 :
1204 : /// before calling this function, the direct path between src and dst are already removed.
1205 : /// return true if a second path is found
1206 157 : bool ge::EnginePartitioner::HasSecondPath(size_t src, size_t dst, size_t upper_bound) const {
1207 : bool has_second = false;
1208 : if (graph_info_.clusters_.at(src)->out_clu_.empty() || graph_info_.clusters_.at(dst)->in_clu_.empty()) {
1209 : return has_second;
1210 : }
1211 : /// Avoid recursion since stack space might be limited.
1212 : /// We instead keep a stack of nodes to visit.
1213 : std::vector<size_t> temp_stack;
1214 : std::vector<size_t> second_path_ids;
1215 : std::set<size_t> visited;
1216 : temp_stack.push_back(src);
1217 : while (!temp_stack.empty()) {
1218 : if (has_second) {
1219 : break;
1220 : }
1221 : size_t cluster = temp_stack.back();
1222 : second_path_ids.emplace_back(cluster);
1223 : temp_stack.pop_back();
1224 3 : ClusterPtr cur_cluster = graph_info_.clusters_.at(cluster);
1225 : if (!visited.insert(cluster).second) {
1226 : continue;
1227 : }
1228 : for (auto out : cur_cluster->out_clu_) {
1229 : if (out == dst) {
1230 : has_second = true; // There is cycle
1231 : second_path_ids.emplace_back(out);
1232 : break;
1233 : }
1234 : if (out < upper_bound) {
1235 : temp_stack.push_back(out);
1236 : }
1237 : }
1238 : }
1239 : if (has_second) {
1240 : std::stringstream path;
1241 : std::for_each(second_path_ids.begin(), second_path_ids.end(), [&path](const size_t &id) { path << id << "->"; });
1242 : GELOGD("Second path is [%s]", path.str().c_str());
1243 : }
1244 : return has_second;
1245 : }
1246 :
1247 : Status ge::EnginePartitioner::Partition(const ge::ComputeGraphPtr &compute_graph, Mode mode) {
1248 : current_mode_ = mode;
1249 : GE_CHECK_NOTNULL(compute_graph);
1250 : GE_CHK_STATUS_RET(compute_graph->TopologicalSorting(), "TopologicalSorting for graph:%s failed",
1251 : compute_graph->GetName().c_str());
1252 : if (mode == EnginePartitioner::Mode::kCompositeEnginePartitioning) {
1253 : GE_CHK_STATUS_RET(engine_placer_.AssignCompositeEngine(),
1254 : "[Partition][SubGraph] Assign composite engine for graph %s failed",
1255 : compute_graph->GetName().c_str());
1256 : }
1257 : ge::GetContext().GetOption(ge::OPTION_TOPOSORTING_MODE, topo_sorting_mode_);
1258 : ClearAllPartitionData();
1259 : GELOGD("%s start part with mode %d", compute_graph->GetName().c_str(), mode);
1260 : auto real_ret = SUCCESS;
1261 : auto ret = PartitionSubGraph(compute_graph, mode);
1262 : if (ret != SUCCESS) {
1263 : GELOGE(ret, "[Partition][SubGraph] Failed, ret:%d", ret);
1264 : real_ret = ret;
1265 : }
1266 : GE_CHECK_NOTNULL(compute_graph);
1267 : // partition sub graph
1268 : for (const auto &sub_graph : compute_graph->GetAllSubgraphs()) {
1269 : GE_CHECK_NOTNULL(sub_graph);
1270 : GELOGD("%s start part for its subgraph %s with mode %d", compute_graph->GetName().c_str(),
1271 : sub_graph->GetName().c_str(), mode);
1272 : bool no_need_partition_and_merge = false;
1273 : bool no_need_partition = false;
1274 : (void)ge::AttrUtils::GetBool(sub_graph, ATTR_NAME_NO_NEED_PARTITION, no_need_partition);
1275 : (void)ge::AttrUtils::GetBool(sub_graph, ATTR_NAME_NO_NEED_PARTITION_AND_MERGE, no_need_partition_and_merge);
1276 : if (no_need_partition_and_merge || no_need_partition) {
1277 : GELOGI("sub graph %s no need partition, skip it", sub_graph->GetName().c_str());
1278 : continue;
1279 : }
1280 : ret = PartitionSubGraph(sub_graph, mode);
1281 : if (ret != SUCCESS) {
1282 : GELOGE(ret, "[Partition][SubGraph] Failed, ret:%d", ret);
1283 : real_ret = ret;
1284 : }
1285 : }
1286 : if (real_ret != SUCCESS) {
1287 : auto root_graph = ge::GraphUtils::FindRootGraph(compute_graph);
1288 : GE_CHECK_NOTNULL(root_graph);
1289 : (void)Analyzer::GetInstance()->SaveAnalyzerDataToFile(root_graph->GetSessionID(), root_graph->GetGraphID());
1290 : }
1291 : return real_ret;
1292 : }
1293 :
1294 : Status ge::EnginePartitioner::PartitionSubGraph(const ge::ComputeGraphPtr &compute_graph, Mode mode) {
1295 : GE_CHECK_NOTNULL(compute_graph);
1296 : // clear graph_info
1297 : GraphPartitionInfo graph_info(mode);
1298 : graph_info_ = std::move(graph_info);
1299 : graph_info_.output_name_ = compute_graph->GetOutput();
1300 : graph_info_.output_size_ = compute_graph->GetOutputSize();
1301 : graph_info_.input_size_ = compute_graph->GetInputSize();
1302 : GELOGI("Graph Partition starts, graph nodes size is %zu", compute_graph->GetDirectNodesSize());
1303 : GE_TRACE_START(PartitionSubGraphInitialize);
1304 : GE_CHK_STATUS_RET(Initialize(compute_graph, mode), "[Initialize] for graph:%s failed",
1305 : compute_graph->GetName().c_str());
1306 : GE_COMPILE_TRACE_TIMESTAMP_END(PartitionSubGraphInitialize, "EnginePartitioner::PartitionInitialize");
1307 : GE_TRACE_START(PartitionSubGraphMarkClusters);
1308 : if (topo_sorting_mode_ == kStableRdfsSort) {
1309 : GE_ASSERT_SUCCESS(MarkClustersWithConsistantId());
1310 : } else {
1311 : MarkClusters();
1312 : }
1313 : GE_COMPILE_TRACE_TIMESTAMP_END(PartitionSubGraphMarkClusters, "EnginePartitioner::PartitionMarkClusters");
1314 : GE_TRACE_START(PartitionSubGraphSplitSubGraphs);
1315 : if (SplitSubGraphs(compute_graph) != SUCCESS) {
1316 : GELOGE(FAILED, "[Split][SubGraphs] for graph:%s failed", compute_graph->GetName().c_str());
1317 : return FAILED;
1318 : }
1319 : GE_COMPILE_TRACE_TIMESTAMP_END(PartitionSubGraphSplitSubGraphs, "EnginePartitioner::PartitionSplitSubGraphs");
1320 : GE_TRACE_START(PartitionSubGraphSortSubGraphs);
1321 : if (SortSubGraphs(compute_graph) != ge::SUCCESS) {
1322 : GELOGE(GE_GRAPH_TOPO_SORT_FAILED, "[Sort][SubGraphs] for graph:%s failed.", compute_graph->GetName().c_str());
1323 : return ge::FAILED;
1324 : }
1325 : GE_COMPILE_TRACE_TIMESTAMP_END(PartitionSubGraphSortSubGraphs, "EnginePartitioner::PartitionSortSubGraphs");
1326 : GE_TRACE_START(PartitionSubGraphAddPartitionsToGraphNode);
1327 : std::vector<ge::SubGraphInfoPtr> output_subgraphs;
1328 : if (AddPartitionsToGraphNode(output_subgraphs, compute_graph) != ge::SUCCESS) {
1329 : GELOGE(GE_GRAPH_EMPTY_PARTITION, "[Add][Partitions] To GraphNode failed, graph:%s.",
1330 : compute_graph->GetName().c_str());
1331 : return ge::FAILED;
1332 : }
1333 : GE_COMPILE_TRACE_TIMESTAMP_END(PartitionSubGraphAddPartitionsToGraphNode,
1334 : "EnginePartitioner::PartitionAddPartitionsToGraphNode");
1335 : GELOGI("Graph Partition ends. Adding partitions to SubGraphInfo, got %zu sub graphs", output_subgraphs.size());
1336 : partition_times_++; // do not care over flow
1337 : graph_2_graph_partition_info_[compute_graph] = std::move(graph_info_);
1338 : graph_2_graph_partition_info_[compute_graph].mode_ = Mode::kMerging;
1339 : graph_2_subgraph_list_[compute_graph] = std::move(output_subgraphs);
1340 : return SUCCESS;
1341 : }
1342 :
1343 : // all the inputs are the nodes and anchors in the original graph
1344 : Status ge::EnginePartitioner::AddPlaceHolderEnd(const AnchorPtr &out_anchor, const AnchorPtr &in_anchor) {
1345 : GE_CHECK_NOTNULL(out_anchor);
1346 : GE_CHECK_NOTNULL(in_anchor);
1347 : // nodes in original graph
1348 : const auto &src_node = out_anchor->GetOwnerNode();
1349 : const auto &dst_node = in_anchor->GetOwnerNode();
1350 : GE_CHECK_NOTNULL(src_node);
1351 : GE_CHECK_NOTNULL(dst_node);
1352 : // All nodes have a copy in corresponding_node_in_partitions_, so function at cannot be exception
1353 : const auto &node_in_partitions = graph_info_.corresponding_node_in_partitions_;
1354 : const auto &src_anchor = node_in_partitions.at(src_node->GetName())->GetOutAnchor(AnchorUtils::GetIdx(out_anchor));
1355 : const auto &dst_anchor = node_in_partitions.at(dst_node->GetName())->GetInAnchor(AnchorUtils::GetIdx(in_anchor));
1356 : GE_CHECK_NOTNULL(src_anchor, "src_anchor(index:%d) is nullptr", AnchorUtils::GetIdx(out_anchor));
1357 : GE_CHECK_NOTNULL(dst_anchor, "dst_anchor(index:%d) is nullptr", AnchorUtils::GetIdx(in_anchor));
1358 : // anchors in subGraph
1359 : const ComputeGraphPtr &src_subgraph = src_anchor->GetOwnerNode()->GetOwnerComputeGraph();
1360 : const ComputeGraphPtr &dst_subgraph = dst_anchor->GetOwnerNode()->GetOwnerComputeGraph();
1361 : GE_CHECK_NOTNULL(src_subgraph);
1362 : GE_CHECK_NOTNULL(dst_subgraph);
1363 : // add end and pld node
1364 : auto ret = AddPlaceHolderEndInSrcDstGraph(src_anchor, dst_anchor, dst_subgraph, src_subgraph);
1365 : if (ret != SUCCESS) {
1366 : GELOGE(GE_GRAPH_ADD_PLC_END_FAILED, "[Call][AddPlaceHolderEndInSrcDstGraph] failed, ret:%d.", ret);
1367 : return ret;
1368 : }
1369 : return SUCCESS;
1370 : }
1371 :
1372 : Status ge::EnginePartitioner::SortSubGraphs(const ge::ComputeGraphPtr &compute_graph) {
1373 : uint32_t rank = kRankOne; // rank 0 for data graph
1374 : ComputeGraphPtr new_input_nodes_sub_graph = MakeShared<ComputeGraph>("inputNodeGraph");
1375 : GE_CHECK_NOTNULL(new_input_nodes_sub_graph);
1376 : GE_CHECK_NOTNULL(compute_graph);
1377 : for (const auto &node : compute_graph->GetDirectNode()) {
1378 : // All nodes in original graph have a copy in corresponding_node_in_partitions_, so it cannot be null
1379 : auto sub_graph = graph_info_.corresponding_node_in_partitions_.at(node->GetName())->GetOwnerComputeGraph();
1380 : if ((graph_info_.partitions_2_rank_.find(sub_graph) == graph_info_.partitions_2_rank_.end()) &&
1381 : (graph_info_.partitions_[sub_graph] != kEngineDefaultData)) {
1382 : graph_info_.partitions_2_rank_[sub_graph] = rank;
1383 : graph_info_.rank_2_partitions_.push_back(sub_graph);
1384 : rank++;
1385 : } else if (graph_info_.partitions_[sub_graph] == kEngineDefaultData) { // merge data graph
1386 : if (PutInputNodesInSubGraph(sub_graph, new_input_nodes_sub_graph) != SUCCESS) {
1387 : GELOGE(FAILED, "[Call][putInputNodesInSubGraph] failed.");
1388 : return FAILED;
1389 : }
1390 : graph_info_.partitions_.erase(graph_info_.partitions_.find(sub_graph));
1391 : }
1392 : }
1393 : if (!new_input_nodes_sub_graph->GetDirectNode().empty()) {
1394 : graph_info_.rank_2_partitions_.insert(graph_info_.rank_2_partitions_.cbegin(), new_input_nodes_sub_graph);
1395 : graph_info_.partitions_2_rank_[new_input_nodes_sub_graph] = 0;
1396 : AddNewGraphToPartition(new_input_nodes_sub_graph, "inputNodesSubGraph");
1397 : }
1398 : // reinit rank
1399 : rank = kRankZero;
1400 : for (const auto &it : graph_info_.rank_2_partitions_) {
1401 : // rename subGraph based on rank
1402 : if (it != nullptr) {
1403 : // rename subGraph based on rank
1404 : std::string graph_name =
1405 : "partition" + std::to_string(partition_times_) + "_rank" + std::to_string(rank) + "_" + it->GetName();
1406 : it->SetName(graph_name);
1407 : }
1408 : rank++;
1409 : }
1410 : return SUCCESS;
1411 : }
1412 :
1413 : AnchorPtr ge::EnginePartitioner::GetEndInAnchor(const AnchorPtr &src_anchor, const NodePtr &end_node) const {
1414 : if ((src_anchor == nullptr) || (end_node == nullptr)) {
1415 : REPORT_INNER_ERR_MSG("E19999", "Param src_anchor or end_node is nullptr, check invalid.");
1416 : GELOGE(FAILED, "[Check][Param] parameter src_anchor or end_node is nullptr.");
1417 : return nullptr;
1418 : }
1419 : AnchorPtr end_in_anchor;
1420 : if (Anchor::DynamicAnchorCast<OutDataAnchor>(src_anchor) != nullptr) {
1421 : end_in_anchor = end_node->GetInDataAnchor(0);
1422 : } else {
1423 : end_in_anchor = end_node->GetInControlAnchor();
1424 : }
1425 : return end_in_anchor;
1426 : }
1427 :
1428 : AnchorPtr ge::EnginePartitioner::GetPldOutAnchor(const NodePtr &pld_node, const AnchorPtr &dst_anchor) const {
1429 : if ((pld_node == nullptr) || (dst_anchor == nullptr)) {
1430 : REPORT_INNER_ERR_MSG("E19999", "Param pld_node or dst_anchor is nullptr, check invalid.");
1431 : GELOGE(FAILED, "[Check][Param] parameter pld_node or dst_anchor is nullptr.");
1432 : return nullptr;
1433 : }
1434 : AnchorPtr pld_out_anchor;
1435 : if (Anchor::DynamicAnchorCast<InDataAnchor>(dst_anchor) != nullptr) {
1436 : pld_out_anchor = pld_node->GetOutDataAnchor(0);
1437 : } else {
1438 : pld_out_anchor = pld_node->GetOutControlAnchor();
1439 : }
1440 : return pld_out_anchor;
1441 : }
1442 :
1443 : void ge::EnginePartitioner::AddEndPldInformationToSubGraphInfo(ge::SubGraphInfoPtr &subgraph_info) {
1444 : if (subgraph_info == nullptr) {
1445 : GELOGE(FAILED, "[Check][Param] parameter subgraph_info is nullptr.");
1446 : return;
1447 : }
1448 : auto subgraph = subgraph_info->GetSubGraph();
1449 : GE_CHECK_NOTNULL_JUST_RETURN(subgraph);
1450 : NodetoNodeMap end_map;
1451 : NodetoNodeMap pld_map;
1452 : for (const auto &node : subgraph->GetDirectNode()) {
1453 : if (strcmp(node->GetTypePtr(), kEndType) == 0) {
1454 : end_map[node] = graph_info_.end_2_pld_.at(node);
1455 : }
1456 : if (strcmp(node->GetTypePtr(), kPlaceHolderType) == 0) {
1457 : pld_map[node] = graph_info_.pld_2_end_.at(node);
1458 : }
1459 : }
1460 : subgraph_info->SetEnd2PldMap(end_map);
1461 : subgraph_info->SetPld2EndMap(pld_map);
1462 : }
1463 :
1464 : const Graph2SubGraphInfoList &ge::EnginePartitioner::GetSubGraphMap() {
1465 : return graph_2_subgraph_list_;
1466 : }
1467 :
1468 : void ge::EnginePartitioner::ClearAllPartitionData() {
1469 : graph_2_graph_partition_info_.clear();
1470 : graph_2_subgraph_list_.clear();
1471 : graph_2_input_subgraph_.clear();
1472 : GELOGD("Clear all partition data success.");
1473 : }
1474 :
1475 : const NodeEngineMap &EnginePartitioner::GetNodeEngineMap() const {
1476 : return engine_placer_.GetNodeEngineMap(graph_info_.mode_ == Mode::kCompositeEnginePartitioning);
1477 : }
1478 :
1479 : Status EnginePartitioner::UpdateCorrespondNodeInPartitions(const ComputeGraphPtr &compute_graph,
1480 : GraphPartitionInfo &graph_info) const {
1481 : GELOGI("Graph partition info: %s", graph_info.output_name_.c_str());
1482 : for (const auto &node_after_optimize : compute_graph->GetDirectNode()) {
1483 : auto iter = graph_info.corresponding_node_in_partitions_.find(node_after_optimize->GetName());
1484 : if (iter != graph_info.corresponding_node_in_partitions_.end()) {
1485 : GELOGD("Update correspond node in partitions[%s]", node_after_optimize->GetName().c_str());
1486 : iter->second = node_after_optimize;
1487 : }
1488 : }
1489 : GELOGD("Graph partition update ends.");
1490 : return SUCCESS;
1491 : }
1492 : } // namespace ge
|