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 38 kB

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

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