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

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