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.

dynamic_shape_partition.cc 40 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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/dynamic_shape_partition.h"
  17. #include <algorithm>
  18. #include <iostream>
  19. #include <memory>
  20. #include <queue>
  21. #include <sstream>
  22. #include <string>
  23. #include <unordered_set>
  24. #include <vector>
  25. #include "common/ge/ge_util.h"
  26. #include "framework/common/debug/ge_log.h"
  27. #include "framework/common/debug/log.h"
  28. #include "framework/common/types.h"
  29. #include "graph/debug/ge_attr_define.h"
  30. #include "graph/utils/graph_utils.h"
  31. #include "graph/utils/op_desc_utils.h"
  32. #include "graph/common/omg_util.h"
  33. #define REQUIRE(cond, ...) \
  34. do { \
  35. if (!(cond)) { \
  36. GELOGE(FAILED, "[Dynamic shape partition]" __VA_ARGS__); \
  37. return FAILED; \
  38. } \
  39. } while (0)
  40. #define REQUIRE_NOT_NULL(cond, ...) REQUIRE(((cond) != nullptr), __VA_ARGS__)
  41. #define REQUIRE_SUCCESS(cond, ...) REQUIRE(((cond) == SUCCESS), __VA_ARGS__)
  42. #define REQUIRE_GRAPH_SUCCESS(cond, ...) REQUIRE(((cond) == GRAPH_SUCCESS), __VA_ARGS__)
  43. namespace ge {
  44. namespace {
  45. const std::set<std::string> kControlFlowOps{
  46. STREAMACTIVE, STREAMSWITCH, STREAMMERGE, ENTER, REFENTER, LOOPCOND, NEXTITERATION, REFNEXTITERATION, EXIT, REFEXIT
  47. };
  48. }
  49. using Cluster = DynamicShapePartitioner::Cluster;
  50. using ClusterPtr = std::shared_ptr<Cluster>;
  51. static bool IsSingleOpScene(const ComputeGraphPtr &root_graph) {
  52. for (const auto &node : root_graph->GetAllNodes()) {
  53. GE_CHECK_NOTNULL(node->GetOpDesc());
  54. // not do partition in single op scene.
  55. bool is_singleop = false;
  56. (void)AttrUtils::GetBool(node->GetOpDesc(), ATTR_SINGLE_OP_SCENE, is_singleop);
  57. if (is_singleop) {
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. Status DynamicShapePartitioner::Partition() {
  64. REQUIRE_NOT_NULL(root_graph_, "Graph is nullptr.");
  65. if (IsSingleOpScene(root_graph_)) {
  66. GELOGD("Skip dynamic shape partition as in single op scene.");
  67. REQUIRE(AttrUtils::SetBool(*root_graph_, ATTR_NAME_DYNAMIC_SHAPE_PARTITIONED, false),
  68. "Failed set dynamic shape partitioned flag on root graph.");
  69. return SUCCESS;
  70. }
  71. GELOGD("Start dynamic shape partition graph %s.", root_graph_->GetName().c_str());
  72. REQUIRE_SUCCESS(MarkUnknownShapeNodes(), "Failed mark unknown shape nodes, root grah name:%s.",
  73. root_graph_->GetName().c_str());
  74. if (unknown_shape_nodes_.empty()) {
  75. GELOGD("Skip dynamic shape partition of graph %s as all nodes are known shape.", root_graph_->GetName().c_str());
  76. REQUIRE(AttrUtils::SetBool(*root_graph_, ATTR_NAME_DYNAMIC_SHAPE_PARTITIONED, false),
  77. "Failed set dynamic shape partitioned flag on root graph %s.", root_graph_->GetName().c_str());
  78. return SUCCESS;
  79. }
  80. REQUIRE(AttrUtils::SetBool(*root_graph_, ATTR_NAME_DYNAMIC_SHAPE_PARTITIONED, true),
  81. "Failed set dynamic shape partitioned flag on root graph %s.", root_graph_->GetName().c_str());
  82. REQUIRE_SUCCESS(CtrlEdgeTransfer(), "Failed do ctrl edge transfer!");
  83. DumpGraph("_Before_DSP");
  84. auto status = PartitionImpl();
  85. GELOGD("%s.", DebugString().c_str());
  86. if (status != SUCCESS) {
  87. GELOGE(status, "Failed dynamic shape partition graph: %s, status:\n %s", root_graph_->GetName().c_str(),
  88. DebugString().c_str());
  89. }
  90. DumpGraph("_After_DSP");
  91. GELOGD("Finish dynamic shape partition graph %s.", root_graph_->GetName().c_str());
  92. ClearResource();
  93. return status;
  94. }
  95. Status DynamicShapePartitioner::CtrlEdgeTransfer() {
  96. GELOGD("Do ctrl edge transfer start!");
  97. GE_CHECK_NOTNULL(root_graph_);
  98. bool is_dynamic_shape = false;
  99. (void)AttrUtils::GetBool(root_graph_, ATTR_NAME_DYNAMIC_SHAPE_PARTITIONED, is_dynamic_shape);
  100. if (!is_dynamic_shape) {
  101. return SUCCESS;
  102. }
  103. for (auto &subgraph : root_graph_->GetAllSubgraphs()) {
  104. for (ge::NodePtr &n : subgraph->GetDirectNode()) {
  105. auto op_desc = n->GetOpDesc();
  106. if (op_desc == nullptr) {
  107. continue;
  108. }
  109. auto op_type = op_desc->GetType();
  110. if (op_type == CONSTANT || op_type == CONSTANTOP) {
  111. if (n->GetInAllNodes().empty()) {
  112. GELOGD("[CtrlEdgeTransferPass] node [%s] in nodes is empty", n->GetName().c_str());
  113. continue;
  114. }
  115. GELOGD("start to tranfer ctrl edge for const node [%s]", n->GetName().c_str());
  116. for (auto &in_control_node : n->GetInControlNodes()) {
  117. GE_CHECK_NOTNULL(in_control_node);
  118. GE_CHK_STATUS_RET(ge::GraphUtils::RemoveEdge(in_control_node->GetOutControlAnchor(),
  119. n->GetInControlAnchor()), "remove edge failed");
  120. for (auto &out_node : n->GetOutNodes()) {
  121. if (out_node == nullptr) {
  122. continue;
  123. }
  124. GE_CHK_STATUS_RET(ge::GraphUtils::AddEdge(in_control_node->GetOutControlAnchor(),
  125. out_node->GetInControlAnchor()), "add edge failed.");
  126. }
  127. }
  128. }
  129. }
  130. }
  131. GELOGD("Do ctrl edge transfer end!");
  132. return SUCCESS;
  133. }
  134. Status DynamicShapePartitioner::PartitionImpl() {
  135. REQUIRE_SUCCESS(root_graph_->TopologicalSorting(), "Graph topological sort failed.");
  136. REQUIRE_SUCCESS(InitClusters(), "Failed init cluster nodes.");
  137. REQUIRE_SUCCESS(MergeClusters(), "Failed merge clusters.");
  138. PruneUniqueClusters();
  139. REQUIRE_SUCCESS(BuildPartitionFrame(), "Failed build cluster partition frame.");
  140. REQUIRE_SUCCESS(CombinePartitionFrame(), "Failed combine cluster partition frame.");
  141. REQUIRE_SUCCESS(BuildPartitionSubgraph(), "Failed build cluster partition subgraph.");
  142. return SUCCESS;
  143. }
  144. void DynamicShapePartitioner::PruneUniqueClusters() {
  145. for (auto &node : root_graph_->GetDirectNode()) {
  146. auto cluster = node_2_cluster_[node];
  147. if (unique_clusters_.count(cluster) != 0) {
  148. continue;
  149. }
  150. if (unique_clusters_.insert(cluster).second) {
  151. sorted_unique_clusters_.emplace_back(cluster);
  152. }
  153. }
  154. auto comp_func = [](std::shared_ptr<Cluster> clu_a, std::shared_ptr<Cluster> clu_b) -> bool {
  155. return clu_a->Id() < clu_b->Id();
  156. };
  157. std::sort(sorted_unique_clusters_.begin(), sorted_unique_clusters_.end(), comp_func);
  158. }
  159. Status DynamicShapePartitioner::BuildPartitionFrame() {
  160. for (const auto &cluster : sorted_unique_clusters_) {
  161. REQUIRE_SUCCESS(cluster->BuildFrame(), "Failed build frame of cluster[%lu].", cluster->Id());
  162. }
  163. return SUCCESS;
  164. }
  165. Status DynamicShapePartitioner::CombinePartitionFrame() {
  166. for (const auto &cluster : sorted_unique_clusters_) {
  167. REQUIRE_SUCCESS(cluster->CombinePartitionFrame(), "Failed combine frame of cluster[%lu].", cluster->Id());
  168. }
  169. return SUCCESS;
  170. }
  171. Status DynamicShapePartitioner::BuildPartitionSubgraph() {
  172. for (const auto &cluster : sorted_unique_clusters_) {
  173. REQUIRE_SUCCESS(cluster->BuildPartitionSubgraph(), "Failed build subgraph of cluster[%lu].", cluster->Id());
  174. }
  175. return SUCCESS;
  176. }
  177. std::string DynamicShapePartitioner::DebugString() const {
  178. size_t unknown = 0;
  179. size_t known = 0;
  180. size_t data = 0;
  181. size_t netoutput = 0;
  182. size_t is_inputnode = 0;
  183. size_t stage = 0;
  184. std::stringstream ss;
  185. ss << "All unknown shape nodes:" << std::endl;
  186. for (const auto &node : unknown_shape_nodes_) {
  187. ss << " [" << node->GetName() << "](" << node->GetType() << ")" << std::endl;
  188. }
  189. for (const auto &cluster : unique_clusters_) {
  190. if (cluster->IsUnknownShape()) {
  191. unknown++;
  192. } else if (cluster->IsKnownShape()) {
  193. known++;
  194. } else if (cluster->IsData()) {
  195. data++;
  196. } else if (cluster->IsNetOutput()) {
  197. netoutput++;
  198. } else if (cluster->IsInputNode()) {
  199. is_inputnode++;
  200. } else if (cluster->IsIndependent()) {
  201. stage++;
  202. }
  203. }
  204. ss << "All clusters:" << unique_clusters_.size() << ", data:" << data << ", known:" << known
  205. << ", unknown:" << unknown << ", netoutput:" << netoutput << ", is_inputnode:" << is_inputnode
  206. << ", stage:" << stage << std::endl;
  207. for (const auto &cluster : unique_clusters_) {
  208. ss << " " << cluster->DebugString() << std::endl;
  209. }
  210. return ss.str();
  211. }
  212. void DynamicShapePartitioner::DumpGraph(const std::string &suffix) {
  213. GraphUtils::DumpGEGraphToOnnx(*root_graph_, root_graph_->GetName() + suffix);
  214. for (const auto &sub_graph : root_graph_->GetAllSubgraphs()) {
  215. GraphUtils::DumpGEGraphToOnnx(*sub_graph, sub_graph->GetName() + suffix);
  216. }
  217. }
  218. void DynamicShapePartitioner::ClearResource() {
  219. for (const auto &cluster : unique_clusters_) {
  220. cluster->Clear();
  221. }
  222. node_2_cluster_.clear();
  223. ordered_cluster_.clear();
  224. unique_clusters_.clear();
  225. sorted_unique_clusters_.clear();
  226. unknown_shape_nodes_.clear();
  227. root_graph_.reset();
  228. }
  229. Status DynamicShapePartitioner::MarkUnknownShapeNodes() {
  230. for (auto &node : root_graph_->GetDirectNode()) {
  231. REQUIRE_SUCCESS(CollectSpreadUnknownShapeNodes(node), "Failed collect spread unknown shape nodes %s.",
  232. node->GetName().c_str());
  233. }
  234. return SUCCESS;
  235. }
  236. Status DynamicShapePartitioner::InitClusters() {
  237. auto graph = root_graph_;
  238. size_t rank = 0;
  239. for (const auto &node : graph->GetDirectNode()) {
  240. Cluster::Type type = Cluster::DATA;
  241. bool is_input = ((node->GetType() == CONSTANT) || (node->GetType() == CONSTANTOP)) && node->GetInNodes().empty();
  242. REQUIRE_NOT_NULL(node->GetOpDesc(), "op_desc is null");
  243. if (node->GetType() == DATA) {
  244. type = Cluster::DATA;
  245. } else if (is_input) {
  246. type = Cluster::INPUT_NODE;
  247. } else if (node->GetType() == NETOUTPUT) {
  248. type = Cluster::NETOUTPUT;
  249. } else if ((node->GetType() == PARTITIONEDCALL) && (node->GetOpDesc()->HasAttr(ATTR_STAGE_LEVEL))) {
  250. type = Cluster::STAGE;
  251. } else if (unknown_shape_nodes_.count(node) > 0) {
  252. type = Cluster::UNKNOWN_SHAPE;
  253. } else {
  254. type = Cluster::KNOWN_SHAPE;
  255. }
  256. auto cluster = MakeShared<Cluster>(rank++, type, node, this);
  257. REQUIRE_NOT_NULL(cluster, "Failed new memory for cluster.");
  258. node_2_cluster_[node] = cluster;
  259. if (cluster->IsUnknownShape() && !cluster->IsControlFlow()) {
  260. ordered_cluster_.push_back(cluster);
  261. }
  262. // Already sorted topologically, so access to the parent cluster is safe
  263. for (const auto &parent : node->GetInAllNodes()) {
  264. cluster->AddInput(node_2_cluster_[parent]);
  265. }
  266. }
  267. for (const auto &node : graph->GetDirectNode()) {
  268. GELOGD("Make cluster for node %s : %s.", node->GetName().c_str(), node_2_cluster_[node]->DebugString().c_str());
  269. }
  270. return SUCCESS;
  271. }
  272. Status DynamicShapePartitioner::TopologicalSortClusters() {
  273. ordered_cluster_.clear();
  274. // BFS topological sort clusters for known shape cluster
  275. std::queue<ClusterPtr> ready_clusters;
  276. std::unordered_map<ClusterPtr, size_t> cluster_pending_count;
  277. std::unordered_set<ClusterPtr> seen_clusters;
  278. for (auto &node : root_graph_->GetDirectNode()) {
  279. auto &cluster = node_2_cluster_[node];
  280. if (seen_clusters.count(cluster) != 0) {
  281. continue;
  282. }
  283. seen_clusters.insert(cluster);
  284. auto pending_count = cluster->Inputs().size();
  285. if (pending_count == 0) {
  286. ready_clusters.push(cluster);
  287. } else {
  288. cluster_pending_count[cluster] = pending_count;
  289. }
  290. }
  291. size_t rank = 0;
  292. while (!ready_clusters.empty()) {
  293. auto cluster = ready_clusters.front();
  294. ready_clusters.pop();
  295. cluster->UpdateRank(rank++);
  296. if (cluster->IsKnownShape() || cluster->IsInputNode()) {
  297. ordered_cluster_.push_back(cluster);
  298. }
  299. for (const auto &out_cluster : cluster->Outputs()) {
  300. if (cluster_pending_count[out_cluster] > 0 && --cluster_pending_count[out_cluster] == 0) {
  301. ready_clusters.push(out_cluster);
  302. }
  303. }
  304. }
  305. if (rank != seen_clusters.size()) {
  306. return FAILED;
  307. }
  308. return SUCCESS;
  309. }
  310. namespace {
  311. static std::string ToString(const std::vector<ClusterPtr> &clusters) {
  312. if (clusters.empty()) {
  313. return "()";
  314. }
  315. std::stringstream ss;
  316. ss << "(";
  317. auto iter = clusters.begin();
  318. for (size_t i = 0; i < clusters.size() - 1; i++) {
  319. ss << (*iter)->Id() << ",";
  320. iter++;
  321. }
  322. ss << (*iter)->Id() << ").";
  323. return ss.str();
  324. }
  325. }
  326. void DynamicShapePartitioner::MergeClustersUnknownShape() {
  327. // Merge unknown shape clusters
  328. for (const auto &cluster : ordered_cluster_) {
  329. if (cluster->IsIndependent() || cluster->IsControlFlow()) {
  330. continue;
  331. }
  332. for (const auto &in_cluster : cluster->Inputs()) {
  333. if (!in_cluster->IsUnknownShape()) {
  334. continue;
  335. }
  336. auto merged_clusters = cluster->MergeAllPathFrom(in_cluster);
  337. GELOGD("Merge all path cluster from %lu to %lu %s.", in_cluster->Id(), cluster->Id(),
  338. ToString(merged_clusters).c_str());
  339. for (const auto &merged_cluster : merged_clusters) {
  340. for (const auto &node : merged_cluster->Nodes()) {
  341. node_2_cluster_[node] = cluster;
  342. }
  343. }
  344. }
  345. }
  346. }
  347. void DynamicShapePartitioner::MergeClustersKnownShape() {
  348. // Merge known shape clusters
  349. for (const auto &cluster : ordered_cluster_) {
  350. if (cluster->IsIndependent()) {
  351. continue;
  352. }
  353. if (cluster->IsRefVariable() && cluster->Inputs().size() == 1) {
  354. auto in_cluster = *(cluster->Inputs().begin());
  355. in_cluster->Merge(cluster);
  356. node_2_cluster_[*(cluster->Nodes().begin())] = in_cluster;
  357. continue;
  358. }
  359. for (const auto &in_cluster : cluster->Inputs()) {
  360. if (!in_cluster->IsKnownShape()) {
  361. continue;
  362. }
  363. if (cluster->TryMerge(in_cluster)) {
  364. GELOGD("Success merge known shape cluster from %lu to %lu.", in_cluster->Id(), cluster->Id());
  365. for (const auto &node : in_cluster->Nodes()) {
  366. node_2_cluster_[node] = cluster;
  367. }
  368. }
  369. }
  370. }
  371. }
  372. void DynamicShapePartitioner::MergeClustersInputData() {
  373. // Merge input clusters
  374. std::shared_ptr<Cluster> cluster_pre = nullptr;
  375. for (const auto &cluster : ordered_cluster_) {
  376. if (!cluster->IsInputNode()) {
  377. continue;
  378. }
  379. if (cluster_pre != nullptr) {
  380. cluster_pre->Merge(cluster);
  381. } else {
  382. cluster_pre = cluster;
  383. }
  384. GELOGD("Success merge input node cluster from %lu to %lu.", cluster->Id(), cluster->Id());
  385. for (const auto &node : cluster->Nodes()) {
  386. node_2_cluster_[node] = cluster_pre;
  387. }
  388. }
  389. }
  390. Status DynamicShapePartitioner::MergeClusters() {
  391. MergeClustersUnknownShape();
  392. REQUIRE_SUCCESS(TopologicalSortClusters(), "Failed topological sort clusters after merge unknown shape clusters.");
  393. MergeClustersKnownShape();
  394. MergeClustersInputData();
  395. return SUCCESS;
  396. }
  397. bool DynamicShapePartitioner::JudgeUnknowShapeWithAttr(const OpDescPtr &opdesc) {
  398. bool is_forced_unknown = false;
  399. if (AttrUtils::GetBool(opdesc, ATTR_NAME_IS_UNKNOWN_SHAPE, is_forced_unknown) && is_forced_unknown) {
  400. GELOGD("Collect node %s as unknown as it was marked unknown forcibly.", opdesc->GetName().c_str());
  401. return true;
  402. }
  403. bool forced_unknown = false;
  404. if (AttrUtils::GetBool(opdesc, ATTR_NAME_FORCE_UNKNOWN_SHAPE, forced_unknown) && forced_unknown) {
  405. GELOGD("Collect node %s as unknown as it was marked force unknown node forcibly.", opdesc->GetName().c_str());
  406. return true;
  407. }
  408. return false;
  409. }
  410. Status DynamicShapePartitioner::CollectSpreadUnknownShapeNodes(NodePtr node) {
  411. if (unknown_shape_nodes_.count(node) > 0) {
  412. return SUCCESS;
  413. }
  414. auto opdesc = node->GetOpDesc();
  415. REQUIRE_NOT_NULL(opdesc, "Opdesc is nullptr.");
  416. // One can set 'ATTR_NAME_IS_UNKNOWN_SHAPE=true' on node so as to forcing the node flow into the unknown subgraph,
  417. // ignore the actual shape.
  418. if (JudgeUnknowShapeWithAttr(opdesc)) {
  419. unknown_shape_nodes_.insert(node);
  420. return SUCCESS;
  421. }
  422. size_t anchor_index = 0;
  423. bool is_unknown = false;
  424. for (auto &out_tensor : opdesc->GetAllOutputsDesc()) {
  425. if (IsUnknownShapeTensor(out_tensor)) {
  426. GELOGD("Collect node %s as unknown as output %lu is unknown.", node->GetName().c_str(), anchor_index);
  427. is_unknown = true;
  428. auto anchor = node->GetOutDataAnchor(static_cast<int>(anchor_index));
  429. for (const auto peer_anchor : anchor->GetPeerInDataAnchors()) {
  430. if (peer_anchor != nullptr) {
  431. GELOGD("Collect node %s as has unknown input from %s:%lu.", peer_anchor->GetOwnerNode()->GetName().c_str(),
  432. node->GetName().c_str(), anchor_index);
  433. unknown_shape_nodes_.insert(peer_anchor->GetOwnerNode());
  434. }
  435. }
  436. }
  437. anchor_index++;
  438. }
  439. anchor_index = 0;
  440. for (auto &in_tensor : opdesc->GetAllInputsDesc()) {
  441. if (IsUnknownShapeTensor(in_tensor)) {
  442. GELOGD("Collect node %s as unknown as input %lu is unknown.", node->GetName().c_str(), anchor_index);
  443. is_unknown = true;
  444. auto anchor = node->GetInDataAnchor(static_cast<int>(anchor_index));
  445. const auto peer_anchor = anchor->GetPeerOutAnchor();
  446. if (peer_anchor != nullptr) {
  447. GELOGD("Collect node %s as has unknown output to %s:%lu.", peer_anchor->GetOwnerNode()->GetName().c_str(),
  448. node->GetName().c_str(), anchor_index);
  449. unknown_shape_nodes_.insert(peer_anchor->GetOwnerNode());
  450. }
  451. }
  452. anchor_index++;
  453. }
  454. if (is_unknown) {
  455. unknown_shape_nodes_.insert(node);
  456. } else {
  457. auto graph = root_graph_;
  458. for (const auto &subgraph_name : opdesc->GetSubgraphInstanceNames()) {
  459. auto subgraph = graph->GetSubgraph(subgraph_name);
  460. REQUIRE_NOT_NULL(subgraph, "Failed get subgraph %s of node %s on root graph.", subgraph_name.c_str(),
  461. node->GetName().c_str());
  462. bool is_graph_unknow = false;
  463. REQUIRE_SUCCESS(IsUnknownShapeGraph(subgraph, is_graph_unknow), "Failed check subgraph %s shape of node %s.",
  464. subgraph_name.c_str(), node->GetName().c_str());
  465. if (is_graph_unknow) {
  466. GELOGD("Collect node %s as its subgraph %s is unknown.", node->GetName().c_str(), subgraph->GetName().c_str());
  467. unknown_shape_nodes_.insert(node);
  468. break;
  469. }
  470. }
  471. }
  472. return SUCCESS;
  473. }
  474. Status DynamicShapePartitioner::IsUnknownShapeNode(NodePtr node, bool &is_unknown) {
  475. auto opdesc = node->GetOpDesc();
  476. auto graph = root_graph_;
  477. for (auto &out_tensor : opdesc->GetAllOutputsDesc()) {
  478. if (IsUnknownShapeTensor(out_tensor)) {
  479. GELOGD("Mark node %s unknown as unknown output.", node->GetName().c_str());
  480. is_unknown = true;
  481. return SUCCESS;
  482. }
  483. }
  484. for (auto &in_tensor : opdesc->GetAllInputsDesc()) {
  485. if (IsUnknownShapeTensor(in_tensor)) {
  486. GELOGD("Mark node %s unknown as unknown intput.", node->GetName().c_str());
  487. is_unknown = true;
  488. return SUCCESS;
  489. }
  490. }
  491. for (auto &subgraph_name : opdesc->GetSubgraphInstanceNames()) {
  492. auto subgraph = graph->GetSubgraph(subgraph_name);
  493. REQUIRE_NOT_NULL(subgraph, "Failed get subgraph %s of node %s on root graph.", subgraph_name.c_str(),
  494. node->GetName().c_str());
  495. REQUIRE_SUCCESS(IsUnknownShapeGraph(subgraph, is_unknown), "Failed check subgraph %s shape of node %s.",
  496. subgraph_name.c_str(), node->GetName().c_str());
  497. if (is_unknown) {
  498. GELOGD("Mark node %s unknown as unknown subgraph.", node->GetName().c_str());
  499. return SUCCESS;
  500. }
  501. }
  502. is_unknown = false;
  503. return SUCCESS;
  504. }
  505. Status DynamicShapePartitioner::IsUnknownShapeGraph(ComputeGraphPtr graph, bool &is_unknown) {
  506. for (auto &node : graph->GetDirectNode()) {
  507. REQUIRE_SUCCESS(IsUnknownShapeNode(node, is_unknown), "Failed check node %s shape on graph %s.",
  508. node->GetName().c_str(), graph->GetName().c_str());
  509. if (is_unknown) {
  510. GELOGD("Mark graph %s unknown as contains unknown node %s.", graph->GetName().c_str(), node->GetName().c_str());
  511. return SUCCESS;
  512. }
  513. }
  514. return SUCCESS;
  515. }
  516. std::string Cluster::DebugString() const {
  517. std::stringstream ss;
  518. switch (type_) {
  519. case DATA:
  520. ss << "DATA";
  521. break;
  522. case INPUT_NODE:
  523. ss << "INPUT_NODE";
  524. break;
  525. case NETOUTPUT:
  526. ss << "NETOUTPUT";
  527. break;
  528. case UNKNOWN_SHAPE:
  529. ss << "UNKNOW";
  530. break;
  531. case KNOWN_SHAPE:
  532. ss << "KNOW";
  533. break;
  534. default:
  535. break;
  536. }
  537. ss << "[" << id_ << "](size:" << nodes_.size() << ")";
  538. ss << "(" << min_ << "," << max_ << ")(";
  539. for (const auto &cluster : in_clusters_) {
  540. ss << cluster->id_ << ",";
  541. }
  542. ss << ")->(";
  543. for (const auto &cluster : out_clusters_) {
  544. ss << cluster->id_ << ",";
  545. }
  546. ss << ")|";
  547. for (const auto &node : nodes_) {
  548. ss << (node->GetName() + "|");
  549. }
  550. return ss.str();
  551. }
  552. size_t Cluster::Id() const { return id_; }
  553. void Cluster::UpdateRank(size_t rank) {
  554. max_ = rank;
  555. min_ = rank;
  556. };
  557. bool Cluster::IsData() const { return type_ == DATA; };
  558. bool Cluster::IsKnownShape() const { return type_ == KNOWN_SHAPE; };
  559. bool Cluster::IsUnknownShape() const { return type_ == UNKNOWN_SHAPE; };
  560. bool Cluster::IsIndependent() const { return type_ == STAGE; };
  561. bool Cluster::IsNetOutput() const { return type_ == NETOUTPUT; };
  562. bool Cluster::IsInputNode() const { return type_ == INPUT_NODE; };
  563. bool Cluster::IsRefVariable() const {
  564. if ((nodes_.size() == 1) && ((nodes_[0]->GetType() == VARIABLE) || (nodes_[0]->GetType() == VARIABLEV2))) {
  565. std::string ref_variable_name;
  566. return (AttrUtils::GetStr(nodes_[0]->GetOpDesc(), REF_VAR_SRC_VAR_NAME, ref_variable_name) &&
  567. !ref_variable_name.empty());
  568. }
  569. return false;
  570. }
  571. bool Cluster::IsControlFlow() const {
  572. const auto &op_desc = nodes_[0]->GetOpDesc();
  573. bool is_ctrl_flow = kControlFlowOps.count(op_desc->GetType()) > 0 && op_desc->HasAttr(ATTR_NAME_FORCE_UNKNOWN_SHAPE);
  574. GELOGD("[%s] %s rts control flow Op ", op_desc->GetName().c_str(), is_ctrl_flow ? "Is" : "Not");
  575. return is_ctrl_flow;
  576. }
  577. void Cluster::AddInput(ClusterPtr in) {
  578. if (std::find(in_clusters_.begin(), in_clusters_.end(), in) != in_clusters_.end()) return;
  579. in_clusters_.insert(in_clusters_.end(), in);
  580. if (std::find(in->out_clusters_.begin(), in->out_clusters_.end(), shared_from_this()) != in->out_clusters_.end())
  581. return;
  582. in->out_clusters_.insert(in->out_clusters_.end(), shared_from_this());
  583. };
  584. void Cluster::RemoveInput(ClusterPtr in) {
  585. in_clusters_.erase(std::remove(in_clusters_.begin(), in_clusters_.end(), in), in_clusters_.end());
  586. in->out_clusters_.erase(std::remove(in->out_clusters_.begin(), in->out_clusters_.end(), shared_from_this()),
  587. in->out_clusters_.end());
  588. };
  589. void Cluster::AddOutput(ClusterPtr out) {
  590. if (std::find(out_clusters_.begin(), out_clusters_.end(), out) != out_clusters_.end()) return;
  591. out_clusters_.insert(out_clusters_.end(), out);
  592. if (std::find(out->in_clusters_.begin(), out->in_clusters_.end(), shared_from_this()) != out->in_clusters_.end())
  593. return;
  594. out->in_clusters_.insert(out->in_clusters_.end(), shared_from_this());
  595. };
  596. void Cluster::RemoveOutput(ClusterPtr out) {
  597. out_clusters_.erase(std::remove(out_clusters_.begin(), out_clusters_.end(), out), out_clusters_.end());
  598. out->in_clusters_.erase(std::remove(out->in_clusters_.begin(), out->in_clusters_.end(), shared_from_this()),
  599. out->in_clusters_.end());
  600. };
  601. void Cluster::Merge(ClusterPtr other) {
  602. if (other->IsIndependent()) {
  603. return;
  604. }
  605. nodes_.insert(nodes_.end(), other->nodes_.begin(), other->nodes_.end());
  606. other->in_clusters_.erase(std::remove(other->in_clusters_.begin(), other->in_clusters_.end(), shared_from_this()),
  607. other->in_clusters_.end());
  608. other->out_clusters_.erase(std::remove(other->out_clusters_.begin(), other->out_clusters_.end(), shared_from_this()),
  609. other->out_clusters_.end());
  610. in_clusters_.erase(std::remove(in_clusters_.begin(), in_clusters_.end(), other), in_clusters_.end());
  611. out_clusters_.erase(std::remove(out_clusters_.begin(), out_clusters_.end(), other), out_clusters_.end());
  612. auto in_clusters = other->in_clusters_;
  613. for (const auto &cluster : in_clusters) {
  614. cluster->RemoveOutput(other);
  615. cluster->AddOutput(shared_from_this());
  616. }
  617. auto out_clusters = other->out_clusters_;
  618. for (const auto &cluster : out_clusters) {
  619. cluster->RemoveInput(other);
  620. cluster->AddInput(shared_from_this());
  621. }
  622. if (other->max_ > max_) {
  623. max_ = other->max_;
  624. }
  625. if (other->min_ < min_) {
  626. min_ = other->min_;
  627. }
  628. };
  629. bool Cluster::TryMerge(ClusterPtr other) {
  630. std::queue<ClusterPtr> forward_reached;
  631. forward_reached.push(other);
  632. while (!forward_reached.empty()) {
  633. auto current_cluster = forward_reached.front();
  634. forward_reached.pop();
  635. for (const auto &cluster : current_cluster->out_clusters_) {
  636. if (cluster->max_ == max_ && current_cluster != other) {
  637. return false;
  638. } else if (cluster->min_ < max_) {
  639. forward_reached.push(cluster);
  640. }
  641. }
  642. }
  643. Merge(other);
  644. return true;
  645. };
  646. std::vector<ClusterPtr> Cluster::MergeAllPathFrom(ClusterPtr other) {
  647. std::queue<ClusterPtr> forward_reached_queue;
  648. std::queue<ClusterPtr> backward_reached_queue;
  649. std::unordered_set<ClusterPtr> forward_reached_clusters;
  650. std::unordered_set<ClusterPtr> backward_reached_clusters;
  651. std::vector<ClusterPtr> path_clusters;
  652. if (other->IsIndependent()) {
  653. return path_clusters;
  654. }
  655. if (std::find(other->out_clusters_.begin(), other->out_clusters_.end(), shared_from_this()) ==
  656. other->out_clusters_.end()) {
  657. return path_clusters;
  658. }
  659. path_clusters.push_back(other);
  660. forward_reached_queue.push(other);
  661. backward_reached_queue.push(shared_from_this());
  662. while (!forward_reached_queue.empty()) {
  663. auto current_cluster = forward_reached_queue.front();
  664. forward_reached_queue.pop();
  665. for (const auto &cluster : current_cluster->out_clusters_) {
  666. if (cluster->min_ < max_ && cluster->max_ != max_ && forward_reached_clusters.count(cluster) == 0) {
  667. forward_reached_clusters.insert(cluster);
  668. forward_reached_queue.push(cluster);
  669. }
  670. }
  671. }
  672. while (!backward_reached_queue.empty()) {
  673. auto current_cluster = backward_reached_queue.front();
  674. backward_reached_queue.pop();
  675. for (const auto &cluster : current_cluster->in_clusters_) {
  676. if (cluster->max_ > other->min_ && cluster->max_ != other->max_ &&
  677. backward_reached_clusters.count(cluster) == 0) {
  678. backward_reached_clusters.insert(cluster);
  679. backward_reached_queue.push(cluster);
  680. if (forward_reached_clusters.count(cluster) != 0) {
  681. path_clusters.push_back(cluster);
  682. }
  683. }
  684. }
  685. }
  686. for (const auto &cluster : path_clusters) {
  687. Merge(cluster);
  688. }
  689. return path_clusters;
  690. }
  691. std::vector<ClusterPtr> Cluster::Inputs() const { return in_clusters_; };
  692. std::vector<ClusterPtr> Cluster::Outputs() const { return out_clusters_; };
  693. std::vector<NodePtr> Cluster::Nodes() const { return nodes_; };
  694. void Cluster::AddFrameInput(InDataAnchorPtr anchor) {
  695. if (anchor != nullptr && anchor->GetPeerOutAnchor() != nullptr) {
  696. inputs_index_[anchor] = inputs_.size();
  697. inputs_.push_back(anchor);
  698. }
  699. }
  700. void Cluster::AddFrameOutput(OutDataAnchorPtr anchor) {
  701. if (anchor != nullptr) {
  702. outputs_index_[anchor] = outputs_.size();
  703. outputs_.push_back(anchor);
  704. }
  705. }
  706. InDataAnchorPtr Cluster::GetFrameInDataAnchor(InDataAnchorPtr anchor) {
  707. return partition_node_->GetInDataAnchor(static_cast<int>(inputs_index_[anchor]));
  708. }
  709. OutDataAnchorPtr Cluster::GetFrameOutDataAnchor(OutDataAnchorPtr anchor) {
  710. return partition_node_->GetOutDataAnchor(static_cast<int>(outputs_index_[anchor]));
  711. }
  712. InControlAnchorPtr Cluster::GetFrameInControlAnchor() { return partition_node_->GetInControlAnchor(); };
  713. OutControlAnchorPtr Cluster::GetFrameOutControlAnchor() { return partition_node_->GetOutControlAnchor(); };
  714. Status Cluster::BuildFrame() {
  715. if ((IsUnknownShape() || IsKnownShape() || IsInputNode()) && !IsControlFlow()) {
  716. return BuildPartitionFrame();
  717. } else {
  718. auto node = nodes_.front();
  719. auto in_control_anchor = node->GetInControlAnchor();
  720. if (in_control_anchor != nullptr) {
  721. for (const auto &peer_out_control_anchor : in_control_anchor->GetPeerOutControlAnchors()) {
  722. auto src_cluster = partitioner_->node_2_cluster_[peer_out_control_anchor->GetOwnerNode()];
  723. if (src_cluster->id_ != id_) {
  724. REQUIRE_GRAPH_SUCCESS(
  725. GraphUtils::RemoveEdge(peer_out_control_anchor, in_control_anchor),
  726. "Failed remove edge from node %s index %d to node %s index %d.",
  727. peer_out_control_anchor->GetOwnerNode()->GetName().c_str(), AnchorUtils::GetIdx(peer_out_control_anchor),
  728. in_control_anchor->GetOwnerNode()->GetName().c_str(), AnchorUtils::GetIdx(in_control_anchor));
  729. control_inputs_.insert(src_cluster);
  730. src_cluster->control_outputs_.insert(peer_out_control_anchor);
  731. }
  732. }
  733. }
  734. if (IsData() || IsIndependent()) {
  735. for (const auto &anchor : node->GetAllOutDataAnchors()) {
  736. AddFrameOutput(anchor);
  737. }
  738. } else {
  739. for (const auto &anchor : node->GetAllInDataAnchors()) {
  740. AddFrameInput(anchor);
  741. }
  742. }
  743. partition_node_ = node;
  744. }
  745. return SUCCESS;
  746. }
  747. Status Cluster::BuildPartitionFrame() {
  748. auto graph = partitioner_->root_graph_;
  749. bool is_unknown_shape = IsUnknownShape();
  750. bool is_input = IsInputNode();
  751. string known_name = (is_unknown_shape ? "_unknow" : "_know");
  752. string sub_graph_name_patten = (is_input ? "_input" : known_name);
  753. std::string sub_graph_name = graph->GetName() + "_sub_" + std::to_string(unique_id_) + sub_graph_name_patten;
  754. subgraph_ = MakeShared<ComputeGraph>(sub_graph_name);
  755. REQUIRE_NOT_NULL(subgraph_, "Failed new memory for subgraph.");
  756. auto partition_op = MakeShared<OpDesc>("PartitionedCall_" + std::to_string(unique_id_++), "PartitionedCall");
  757. REQUIRE_NOT_NULL(partition_op, "Failed new memory for partition op.");
  758. REQUIRE(AttrUtils::SetBool(partition_op, ATTR_NAME_IS_UNKNOWN_SHAPE, is_unknown_shape),
  759. "Failed set _is_unknown_shape flag on partitioned op %s.", partition_op->GetName().c_str());
  760. REQUIRE_GRAPH_SUCCESS(partition_op->AddSubgraphName(subgraph_->GetName()), "Failed add subgraph name.");
  761. REQUIRE_GRAPH_SUCCESS(partition_op->SetSubgraphInstanceName(0, subgraph_->GetName()),
  762. "Failed set subgraph instance name.");
  763. for (auto &node : nodes_) {
  764. REQUIRE_NOT_NULL(subgraph_->AddNode(node), "Failed add node to subgraph.");
  765. REQUIRE(AttrUtils::SetBool(node->GetOpDesc(), ATTR_NAME_IS_UNKNOWN_SHAPE, is_unknown_shape),
  766. "Failed set shape flag.");
  767. REQUIRE_GRAPH_SUCCESS(GraphUtils::RemoveJustNode(graph, node), "Failed remove root graph node.");
  768. REQUIRE_GRAPH_SUCCESS(node->SetOwnerComputeGraph(subgraph_), "Failed set owner graph.");
  769. for (const auto &anchor : node->GetAllInDataAnchors()) {
  770. auto peer_out_anchor = anchor->GetPeerOutAnchor();
  771. if (peer_out_anchor == nullptr) {
  772. continue; // Skip overhang input.
  773. }
  774. auto src_cluster = partitioner_->node_2_cluster_[peer_out_anchor->GetOwnerNode()];
  775. if (src_cluster->id_ != id_) {
  776. AddFrameInput(anchor);
  777. REQUIRE_GRAPH_SUCCESS(partition_op->AddInputDesc(node->GetOpDesc()->GetInputDesc(anchor->GetIdx())),
  778. "Failed add input desc.");
  779. }
  780. }
  781. auto in_control_anchor = node->GetInControlAnchor();
  782. if (in_control_anchor != nullptr) {
  783. for (const auto &peer_out_control_anchor : in_control_anchor->GetPeerOutControlAnchors()) {
  784. if (peer_out_control_anchor == nullptr) {
  785. continue;
  786. }
  787. auto src_cluster = partitioner_->node_2_cluster_[peer_out_control_anchor->GetOwnerNode()];
  788. if (src_cluster->id_ != id_) {
  789. REQUIRE_GRAPH_SUCCESS(
  790. GraphUtils::RemoveEdge(peer_out_control_anchor, in_control_anchor),
  791. "Failed remove edge from %s:%d to %s:%d.", peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  792. peer_out_control_anchor->GetIdx(), node->GetName().c_str(), in_control_anchor->GetIdx());
  793. control_inputs_.insert(src_cluster);
  794. src_cluster->control_outputs_.insert(peer_out_control_anchor);
  795. }
  796. }
  797. }
  798. for (const auto &anchor : node->GetAllOutDataAnchors()) {
  799. auto peer_in_anchors = anchor->GetPeerInDataAnchors();
  800. for (const auto &peer_in_anchor : peer_in_anchors) {
  801. auto src_cluster = partitioner_->node_2_cluster_[peer_in_anchor->GetOwnerNode()];
  802. if (src_cluster->id_ != id_) {
  803. AddFrameOutput(anchor);
  804. REQUIRE_GRAPH_SUCCESS(partition_op->AddOutputDesc(node->GetOpDesc()->GetOutputDesc(anchor->GetIdx())),
  805. "Failed add output desc.");
  806. break;
  807. }
  808. }
  809. }
  810. }
  811. partition_node_ = graph->AddNode(partition_op);
  812. REQUIRE_NOT_NULL(partition_node_, "Failed add partition node.");
  813. REQUIRE_GRAPH_SUCCESS(partition_node_->SetOwnerComputeGraph(graph), "Failed set owner graph.");
  814. subgraph_->SetParentNode(partition_node_);
  815. subgraph_->SetParentGraph(graph);
  816. REQUIRE_GRAPH_SUCCESS(graph->AddSubgraph(subgraph_), "Failed add subgraph to root graph.");
  817. std::string session_graph_id;
  818. REQUIRE(AttrUtils::GetStr(*graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id),
  819. "Failed get ATTR_NAME_SESSION_GRAPH_ID on root graph.");
  820. REQUIRE(AttrUtils::SetStr(*subgraph_, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id),
  821. "Failed set ATTR_NAME_SESSION_GRAPH_ID on subgraph.");
  822. return SUCCESS;
  823. }
  824. Status Cluster::CombinePartitionFrame() {
  825. for (const auto &anchor : inputs_) {
  826. auto peer_out_anchor = anchor->GetPeerOutAnchor();
  827. auto src_cluster = partitioner_->node_2_cluster_[peer_out_anchor->GetOwnerNode()];
  828. auto src_anchor = src_cluster->GetFrameOutDataAnchor(peer_out_anchor);
  829. auto dst_anchor = GetFrameInDataAnchor(anchor);
  830. REQUIRE_GRAPH_SUCCESS(GraphUtils::RemoveEdge(peer_out_anchor, anchor), "Failed remove edge from %s:%d to %s:%d.",
  831. peer_out_anchor->GetOwnerNode()->GetName().c_str(), peer_out_anchor->GetIdx(),
  832. anchor->GetOwnerNode()->GetName().c_str(), anchor->GetIdx());
  833. REQUIRE_GRAPH_SUCCESS(GraphUtils::AddEdge(src_anchor, dst_anchor), "Failed add edge from %s:%d to %s:%d.",
  834. src_anchor->GetOwnerNode()->GetName().c_str(), src_anchor->GetIdx(),
  835. dst_anchor->GetOwnerNode()->GetName().c_str(), dst_anchor->GetIdx());
  836. }
  837. for (const auto &src_cluster : control_inputs_) {
  838. auto src_anchor = src_cluster->GetFrameOutControlAnchor();
  839. auto dst_anchor = GetFrameInControlAnchor();
  840. REQUIRE_GRAPH_SUCCESS(GraphUtils::AddEdge(src_anchor, dst_anchor), "Failed add edge from %s:%d to %s:%d.",
  841. src_anchor->GetOwnerNode()->GetName().c_str(), src_anchor->GetIdx(),
  842. dst_anchor->GetOwnerNode()->GetName().c_str(), dst_anchor->GetIdx());
  843. }
  844. return SUCCESS;
  845. }
  846. Status Cluster::BuildPartitionSubgraph() {
  847. if (IsData() || IsNetOutput() || IsIndependent() || IsControlFlow()) {
  848. return SUCCESS;
  849. }
  850. int64_t parent_node_index = 0;
  851. for (auto anchor : inputs_) {
  852. auto data_op =
  853. MakeShared<OpDesc>(subgraph_->GetName() + std::string("Data_") + std::to_string(parent_node_index), ge::DATA);
  854. REQUIRE_NOT_NULL(data_op, "Failed new memory for data op.");
  855. auto input_desc = anchor->GetOwnerNode()->GetOpDesc()->GetInputDesc(anchor->GetIdx());
  856. REQUIRE_GRAPH_SUCCESS(data_op->AddInputDesc(input_desc), "Failed add input desc.");
  857. REQUIRE_GRAPH_SUCCESS(data_op->AddOutputDesc(input_desc), "Failed add output desc.");
  858. REQUIRE(AttrUtils::SetInt(data_op, ATTR_NAME_PARENT_NODE_INDEX, parent_node_index),
  859. "Failed set parent_node_index on subgraph data node.");
  860. bool is_unknown_shape = IsUnknownShape();
  861. REQUIRE(AttrUtils::SetBool(data_op, ATTR_NAME_IS_UNKNOWN_SHAPE, is_unknown_shape),
  862. "Failed set _is_unknown_shape flag on data op %s.", data_op->GetName().c_str());
  863. auto data_node = subgraph_->AddNode(data_op);
  864. REQUIRE_NOT_NULL(data_node, "Failed add data node to subgraph.");
  865. REQUIRE_GRAPH_SUCCESS(data_node->SetOwnerComputeGraph(subgraph_), "Failed set owner graph of data node.");
  866. REQUIRE_GRAPH_SUCCESS(GraphUtils::AddEdge(data_node->GetOutDataAnchor(0), anchor),
  867. "Faile add data input edge to %s:%d", anchor->GetOwnerNode()->GetName().c_str(),
  868. anchor->GetIdx());
  869. parent_node_index++;
  870. }
  871. if (outputs_.empty() && control_outputs_.empty()) {
  872. return SUCCESS;
  873. }
  874. auto net_output_op = MakeShared<OpDesc>(subgraph_->GetName() + "_" + NODE_NAME_NET_OUTPUT, ge::NETOUTPUT);
  875. REQUIRE_NOT_NULL(net_output_op, "Failed new memory for netoutput op.");
  876. bool is_unknown_shape = IsUnknownShape();
  877. REQUIRE(AttrUtils::SetBool(net_output_op, ATTR_NAME_IS_UNKNOWN_SHAPE, is_unknown_shape),
  878. "Failed set _is_unknown_shape flag on net_output_op %s.", net_output_op->GetName().c_str());
  879. for (size_t i = 0; i < outputs_.size(); ++i) {
  880. GeTensorDesc input_desc;
  881. REQUIRE_GRAPH_SUCCESS(net_output_op->AddInputDesc(input_desc), "Failed add input desc.");
  882. }
  883. auto net_output_node = subgraph_->AddNode(net_output_op);
  884. REQUIRE_NOT_NULL(net_output_node, "Failed add netoutput node to subgraph.");
  885. REQUIRE_GRAPH_SUCCESS(net_output_node->SetOwnerComputeGraph(subgraph_), "Failed set owner graph of netoutput node.");
  886. parent_node_index = 0;
  887. for (const auto &anchor : outputs_) {
  888. auto output_desc = anchor->GetOwnerNode()->GetOpDesc()->GetOutputDesc(static_cast<uint32_t>(anchor->GetIdx()));
  889. REQUIRE(AttrUtils::SetInt(output_desc, ATTR_NAME_PARENT_NODE_INDEX, parent_node_index),
  890. "Failed set parent_node_index on subgraph netoutput's input.");
  891. REQUIRE_GRAPH_SUCCESS(net_output_op->UpdateInputDesc(parent_node_index, output_desc),
  892. "Failed update input desc of netoutput node.");
  893. REQUIRE_GRAPH_SUCCESS(GraphUtils::AddEdge(anchor, net_output_node->GetInDataAnchor(parent_node_index)),
  894. "Faile add edge from %s:%d to netoutput node.", anchor->GetOwnerNode()->GetName().c_str(),
  895. anchor->GetIdx());
  896. parent_node_index++;
  897. }
  898. for (const auto &anchor : control_outputs_) {
  899. REQUIRE_GRAPH_SUCCESS(GraphUtils::AddEdge(anchor, net_output_node->GetInControlAnchor()),
  900. "Faile add control edge from %s:%d to netoutput node.",
  901. anchor->GetOwnerNode()->GetName().c_str(), anchor->GetIdx());
  902. }
  903. return SUCCESS;
  904. }
  905. void Cluster::Clear() {
  906. in_clusters_.clear();
  907. out_clusters_.clear();
  908. nodes_.clear();
  909. partitioner_ = nullptr;
  910. inputs_index_.clear();
  911. outputs_index_.clear();
  912. inputs_.clear();
  913. outputs_.clear();
  914. control_inputs_.clear();
  915. control_outputs_.clear();
  916. partition_node_.reset();
  917. subgraph_.reset();
  918. unique_id_ = 0;
  919. }
  920. thread_local size_t Cluster::unique_id_ = 0;
  921. } // namespace ge

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