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

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