You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dynamic_shape_partition.cc 40 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  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. if (!cluster->IsAdjoinNodes(in_cluster)) {
  360. continue;
  361. }
  362. auto merged_clusters = cluster->MergeAllPathFrom(in_cluster);
  363. GELOGD("Merge all path cluster from %lu to %lu %s.", in_cluster->Id(), cluster->Id(),
  364. ToString(merged_clusters).c_str());
  365. for (const auto &merged_cluster : merged_clusters) {
  366. for (const auto &node : merged_cluster->Nodes()) {
  367. node_2_cluster_[node] = cluster;
  368. }
  369. }
  370. }
  371. }
  372. }
  373. void DynamicShapePartitioner::MergeClustersKnownShape() {
  374. // Merge known shape clusters
  375. for (const auto &cluster : ordered_cluster_) {
  376. if (cluster->IsIndependent()) {
  377. continue;
  378. }
  379. if (cluster->IsRefVariable() && cluster->Inputs().size() == 1) {
  380. auto in_cluster = *(cluster->Inputs().begin());
  381. in_cluster->Merge(cluster);
  382. node_2_cluster_[*(cluster->Nodes().begin())] = in_cluster;
  383. continue;
  384. }
  385. for (const auto &in_cluster : cluster->Inputs()) {
  386. if (!in_cluster->IsKnownShape()) {
  387. continue;
  388. }
  389. if (cluster->TryMerge(in_cluster)) {
  390. GELOGD("Success merge known shape cluster from %lu to %lu.", in_cluster->Id(), cluster->Id());
  391. for (const auto &node : in_cluster->Nodes()) {
  392. node_2_cluster_[node] = cluster;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. void DynamicShapePartitioner::MergeClustersInputData() {
  399. // Merge input clusters
  400. std::shared_ptr<Cluster> cluster_pre = nullptr;
  401. for (const auto &cluster : ordered_cluster_) {
  402. if (!cluster->IsInputNode()) {
  403. continue;
  404. }
  405. if (cluster_pre != nullptr) {
  406. cluster_pre->Merge(cluster);
  407. } else {
  408. cluster_pre = cluster;
  409. }
  410. GELOGD("Success merge input node cluster from %lu to %lu.", cluster->Id(), cluster->Id());
  411. for (const auto &node : cluster->Nodes()) {
  412. node_2_cluster_[node] = cluster_pre;
  413. }
  414. }
  415. }
  416. Status DynamicShapePartitioner::MergeClusters() {
  417. MergeClustersControlFlow();
  418. MergeClustersUnknownShape();
  419. REQUIRE_SUCCESS(TopologicalSortClusters(), "Failed topological sort clusters after merge unknown shape clusters.");
  420. MergeClustersKnownShape();
  421. MergeClustersInputData();
  422. return SUCCESS;
  423. }
  424. bool DynamicShapePartitioner::JudgeUnknowShapeWithAttr(const OpDescPtr &opdesc) {
  425. bool is_forced_unknown = false;
  426. if (AttrUtils::GetBool(opdesc, ATTR_NAME_IS_UNKNOWN_SHAPE, is_forced_unknown) && is_forced_unknown) {
  427. GELOGD("Collect node %s as unknown as it was marked unknown forcibly.", opdesc->GetName().c_str());
  428. return true;
  429. }
  430. bool forced_unknown = false;
  431. if (AttrUtils::GetBool(opdesc, ATTR_NAME_FORCE_UNKNOWN_SHAPE, forced_unknown) && forced_unknown) {
  432. GELOGD("Collect node %s as unknown as it was marked force unknown node forcibly.", opdesc->GetName().c_str());
  433. return true;
  434. }
  435. return false;
  436. }
  437. Status DynamicShapePartitioner::CollectSpreadUnknownShapeNodes(NodePtr node) {
  438. if (unknown_shape_nodes_.count(node) > 0) {
  439. return SUCCESS;
  440. }
  441. auto opdesc = node->GetOpDesc();
  442. REQUIRE_NOT_NULL(opdesc, "Opdesc is nullptr.");
  443. // One can set 'ATTR_NAME_IS_UNKNOWN_SHAPE=true' on node so as to forcing the node flow into the unknown subgraph,
  444. // ignore the actual shape.
  445. if (JudgeUnknowShapeWithAttr(opdesc)) {
  446. unknown_shape_nodes_.insert(node);
  447. return SUCCESS;
  448. }
  449. size_t anchor_index = 0;
  450. bool is_unknown = false;
  451. for (auto &out_tensor : opdesc->GetAllOutputsDesc()) {
  452. if (IsUnknownShapeTensor(out_tensor)) {
  453. GELOGD("Collect node %s as unknown as output %lu is unknown.", node->GetName().c_str(), anchor_index);
  454. is_unknown = true;
  455. auto anchor = node->GetOutDataAnchor(static_cast<int>(anchor_index));
  456. for (const auto peer_anchor : anchor->GetPeerInDataAnchors()) {
  457. if (peer_anchor != nullptr) {
  458. GELOGD("Collect node %s as has unknown input from %s:%lu.", peer_anchor->GetOwnerNode()->GetName().c_str(),
  459. node->GetName().c_str(), anchor_index);
  460. unknown_shape_nodes_.insert(peer_anchor->GetOwnerNode());
  461. }
  462. }
  463. }
  464. anchor_index++;
  465. }
  466. anchor_index = 0;
  467. for (auto &in_tensor : opdesc->GetAllInputsDesc()) {
  468. if (IsUnknownShapeTensor(in_tensor)) {
  469. GELOGD("Collect node %s as unknown as input %lu is unknown.", node->GetName().c_str(), anchor_index);
  470. is_unknown = true;
  471. auto anchor = node->GetInDataAnchor(static_cast<int>(anchor_index));
  472. const auto peer_anchor = anchor->GetPeerOutAnchor();
  473. if (peer_anchor != nullptr) {
  474. GELOGD("Collect node %s as has unknown output to %s:%lu.", peer_anchor->GetOwnerNode()->GetName().c_str(),
  475. node->GetName().c_str(), anchor_index);
  476. unknown_shape_nodes_.insert(peer_anchor->GetOwnerNode());
  477. }
  478. }
  479. anchor_index++;
  480. }
  481. if (is_unknown) {
  482. unknown_shape_nodes_.insert(node);
  483. } else {
  484. auto graph = root_graph_;
  485. for (const 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. bool is_graph_unknow = false;
  490. REQUIRE_SUCCESS(IsUnknownShapeGraph(subgraph, is_graph_unknow), "Failed check subgraph %s shape of node %s.",
  491. subgraph_name.c_str(), node->GetName().c_str());
  492. if (is_graph_unknow) {
  493. GELOGD("Collect node %s as its subgraph %s is unknown.", node->GetName().c_str(), subgraph->GetName().c_str());
  494. unknown_shape_nodes_.insert(node);
  495. break;
  496. }
  497. }
  498. }
  499. return SUCCESS;
  500. }
  501. Status DynamicShapePartitioner::IsUnknownShapeNode(NodePtr node, bool &is_unknown) {
  502. auto opdesc = node->GetOpDesc();
  503. auto graph = root_graph_;
  504. for (auto &out_tensor : opdesc->GetAllOutputsDesc()) {
  505. if (IsUnknownShapeTensor(out_tensor)) {
  506. GELOGD("Mark node %s unknown as unknown output.", node->GetName().c_str());
  507. is_unknown = true;
  508. return SUCCESS;
  509. }
  510. }
  511. for (auto &in_tensor : opdesc->GetAllInputsDesc()) {
  512. if (IsUnknownShapeTensor(in_tensor)) {
  513. GELOGD("Mark node %s unknown as unknown intput.", node->GetName().c_str());
  514. is_unknown = true;
  515. return SUCCESS;
  516. }
  517. }
  518. for (auto &subgraph_name : opdesc->GetSubgraphInstanceNames()) {
  519. auto subgraph = graph->GetSubgraph(subgraph_name);
  520. REQUIRE_NOT_NULL(subgraph, "Failed get subgraph %s of node %s on root graph.", subgraph_name.c_str(),
  521. node->GetName().c_str());
  522. REQUIRE_SUCCESS(IsUnknownShapeGraph(subgraph, is_unknown), "Failed check subgraph %s shape of node %s.",
  523. subgraph_name.c_str(), node->GetName().c_str());
  524. if (is_unknown) {
  525. GELOGD("Mark node %s unknown as unknown subgraph.", node->GetName().c_str());
  526. return SUCCESS;
  527. }
  528. }
  529. is_unknown = false;
  530. return SUCCESS;
  531. }
  532. Status DynamicShapePartitioner::IsUnknownShapeGraph(ComputeGraphPtr graph, bool &is_unknown) {
  533. for (auto &node : graph->GetDirectNode()) {
  534. REQUIRE_SUCCESS(IsUnknownShapeNode(node, is_unknown), "Failed check node %s shape on graph %s.",
  535. node->GetName().c_str(), graph->GetName().c_str());
  536. if (is_unknown) {
  537. GELOGD("Mark graph %s unknown as contains unknown node %s.", graph->GetName().c_str(), node->GetName().c_str());
  538. return SUCCESS;
  539. }
  540. }
  541. return SUCCESS;
  542. }
  543. std::string Cluster::DebugString() const {
  544. std::stringstream ss;
  545. switch (type_) {
  546. case DATA:
  547. ss << "DATA";
  548. break;
  549. case INPUT_NODE:
  550. ss << "INPUT_NODE";
  551. break;
  552. case NETOUTPUT:
  553. ss << "NETOUTPUT";
  554. break;
  555. case UNKNOWN_SHAPE:
  556. ss << "UNKNOW";
  557. break;
  558. case KNOWN_SHAPE:
  559. ss << "KNOW";
  560. break;
  561. default:
  562. break;
  563. }
  564. ss << "[" << id_ << "](size:" << nodes_.size() << ")";
  565. ss << "(" << min_ << "," << max_ << ")(";
  566. for (const auto &cluster : in_clusters_) {
  567. ss << cluster->id_ << ",";
  568. }
  569. ss << ")->(";
  570. for (const auto &cluster : out_clusters_) {
  571. ss << cluster->id_ << ",";
  572. }
  573. ss << ")|";
  574. for (const auto &node : nodes_) {
  575. ss << (node->GetName() + "|");
  576. }
  577. return ss.str();
  578. }
  579. size_t Cluster::Id() const { return id_; }
  580. void Cluster::UpdateRank(size_t rank) {
  581. max_ = rank;
  582. min_ = rank;
  583. };
  584. bool Cluster::IsData() const { return type_ == DATA; };
  585. bool Cluster::IsKnownShape() const { return type_ == KNOWN_SHAPE; };
  586. bool Cluster::IsUnknownShape() const { return type_ == UNKNOWN_SHAPE; };
  587. bool Cluster::IsIndependent() const { return type_ == STAGE; };
  588. bool Cluster::IsNetOutput() const { return type_ == NETOUTPUT; };
  589. bool Cluster::IsInputNode() const { return type_ == INPUT_NODE; };
  590. bool Cluster::IsRefVariable() const {
  591. if ((nodes_.size() == 1) && ((nodes_[0]->GetType() == VARIABLE) || (nodes_[0]->GetType() == VARIABLEV2))) {
  592. std::string ref_variable_name;
  593. return (AttrUtils::GetStr(nodes_[0]->GetOpDesc(), REF_VAR_SRC_VAR_NAME, ref_variable_name) &&
  594. !ref_variable_name.empty());
  595. }
  596. return false;
  597. }
  598. void Cluster::AddInput(ClusterPtr in) {
  599. if (std::find(in_clusters_.begin(), in_clusters_.end(), in) != in_clusters_.end()) return;
  600. in_clusters_.insert(in_clusters_.end(), in);
  601. if (std::find(in->out_clusters_.begin(), in->out_clusters_.end(), shared_from_this()) != in->out_clusters_.end())
  602. return;
  603. in->out_clusters_.insert(in->out_clusters_.end(), shared_from_this());
  604. };
  605. void Cluster::RemoveInput(ClusterPtr in) {
  606. in_clusters_.erase(std::remove(in_clusters_.begin(), in_clusters_.end(), in), in_clusters_.end());
  607. in->out_clusters_.erase(std::remove(in->out_clusters_.begin(), in->out_clusters_.end(), shared_from_this()),
  608. in->out_clusters_.end());
  609. };
  610. void Cluster::AddOutput(ClusterPtr out) {
  611. if (std::find(out_clusters_.begin(), out_clusters_.end(), out) != out_clusters_.end()) return;
  612. out_clusters_.insert(out_clusters_.end(), out);
  613. if (std::find(out->in_clusters_.begin(), out->in_clusters_.end(), shared_from_this()) != out->in_clusters_.end())
  614. return;
  615. out->in_clusters_.insert(out->in_clusters_.end(), shared_from_this());
  616. };
  617. void Cluster::RemoveOutput(ClusterPtr out) {
  618. out_clusters_.erase(std::remove(out_clusters_.begin(), out_clusters_.end(), out), out_clusters_.end());
  619. out->in_clusters_.erase(std::remove(out->in_clusters_.begin(), out->in_clusters_.end(), shared_from_this()),
  620. out->in_clusters_.end());
  621. };
  622. void Cluster::Merge(ClusterPtr other) {
  623. if (other->IsIndependent()) {
  624. return;
  625. }
  626. nodes_.insert(nodes_.end(), other->nodes_.begin(), other->nodes_.end());
  627. other->in_clusters_.erase(std::remove(other->in_clusters_.begin(), other->in_clusters_.end(), shared_from_this()),
  628. other->in_clusters_.end());
  629. other->out_clusters_.erase(std::remove(other->out_clusters_.begin(), other->out_clusters_.end(), shared_from_this()),
  630. other->out_clusters_.end());
  631. in_clusters_.erase(std::remove(in_clusters_.begin(), in_clusters_.end(), other), in_clusters_.end());
  632. out_clusters_.erase(std::remove(out_clusters_.begin(), out_clusters_.end(), other), out_clusters_.end());
  633. auto in_clusters = other->in_clusters_;
  634. for (const auto &cluster : in_clusters) {
  635. cluster->RemoveOutput(other);
  636. cluster->AddOutput(shared_from_this());
  637. }
  638. auto out_clusters = other->out_clusters_;
  639. for (const auto &cluster : out_clusters) {
  640. cluster->RemoveInput(other);
  641. cluster->AddInput(shared_from_this());
  642. }
  643. if (other->max_ > max_) {
  644. max_ = other->max_;
  645. }
  646. if (other->min_ < min_) {
  647. min_ = other->min_;
  648. }
  649. };
  650. bool Cluster::TryMerge(ClusterPtr other) {
  651. std::queue<ClusterPtr> forward_reached;
  652. forward_reached.push(other);
  653. while (!forward_reached.empty()) {
  654. auto current_cluster = forward_reached.front();
  655. forward_reached.pop();
  656. for (const auto &cluster : current_cluster->out_clusters_) {
  657. if (cluster->max_ == max_ && current_cluster != other) {
  658. return false;
  659. } else if (cluster->min_ < max_) {
  660. forward_reached.push(cluster);
  661. }
  662. }
  663. }
  664. Merge(other);
  665. return true;
  666. };
  667. std::vector<ClusterPtr> Cluster::MergeAllPathFrom(ClusterPtr other) {
  668. std::queue<ClusterPtr> forward_reached_queue;
  669. std::queue<ClusterPtr> backward_reached_queue;
  670. std::unordered_set<ClusterPtr> forward_reached_clusters;
  671. std::unordered_set<ClusterPtr> backward_reached_clusters;
  672. std::vector<ClusterPtr> path_clusters;
  673. if (other->IsIndependent()) {
  674. return path_clusters;
  675. }
  676. path_clusters.push_back(other);
  677. forward_reached_queue.push(other);
  678. backward_reached_queue.push(shared_from_this());
  679. while (!forward_reached_queue.empty()) {
  680. auto current_cluster = forward_reached_queue.front();
  681. forward_reached_queue.pop();
  682. for (const auto &cluster : current_cluster->out_clusters_) {
  683. if (cluster->min_ < max_ && cluster->max_ != max_ && forward_reached_clusters.count(cluster) == 0) {
  684. forward_reached_clusters.insert(cluster);
  685. forward_reached_queue.push(cluster);
  686. }
  687. }
  688. }
  689. while (!backward_reached_queue.empty()) {
  690. auto current_cluster = backward_reached_queue.front();
  691. backward_reached_queue.pop();
  692. for (const auto &cluster : current_cluster->in_clusters_) {
  693. if (cluster->max_ > other->min_ && cluster->max_ != other->max_ &&
  694. backward_reached_clusters.count(cluster) == 0) {
  695. backward_reached_clusters.insert(cluster);
  696. backward_reached_queue.push(cluster);
  697. if (forward_reached_clusters.count(cluster) != 0) {
  698. path_clusters.push_back(cluster);
  699. }
  700. }
  701. }
  702. }
  703. for (const auto &cluster : path_clusters) {
  704. Merge(cluster);
  705. }
  706. return path_clusters;
  707. }
  708. std::vector<ClusterPtr> Cluster::Inputs() const { return in_clusters_; };
  709. std::vector<ClusterPtr> Cluster::Outputs() const { return out_clusters_; };
  710. std::vector<NodePtr> Cluster::Nodes() const { return nodes_; };
  711. void Cluster::AddFrameInput(InDataAnchorPtr anchor) {
  712. if (anchor != nullptr && anchor->GetPeerOutAnchor() != nullptr) {
  713. inputs_index_[anchor] = inputs_.size();
  714. inputs_.push_back(anchor);
  715. }
  716. }
  717. void Cluster::AddFrameOutput(OutDataAnchorPtr anchor) {
  718. if (anchor != nullptr) {
  719. outputs_index_[anchor] = outputs_.size();
  720. outputs_.push_back(anchor);
  721. }
  722. }
  723. InDataAnchorPtr Cluster::GetFrameInDataAnchor(InDataAnchorPtr anchor) {
  724. return partition_node_->GetInDataAnchor(static_cast<int>(inputs_index_[anchor]));
  725. }
  726. OutDataAnchorPtr Cluster::GetFrameOutDataAnchor(OutDataAnchorPtr anchor) {
  727. return partition_node_->GetOutDataAnchor(static_cast<int>(outputs_index_[anchor]));
  728. }
  729. InControlAnchorPtr Cluster::GetFrameInControlAnchor() { return partition_node_->GetInControlAnchor(); };
  730. OutControlAnchorPtr Cluster::GetFrameOutControlAnchor() { return partition_node_->GetOutControlAnchor(); };
  731. Status Cluster::BuildFrame() {
  732. if (IsUnknownShape() || IsKnownShape() || IsInputNode()) {
  733. return BuildPartitionFrame();
  734. } else {
  735. auto node = nodes_.front();
  736. auto in_control_anchor = node->GetInControlAnchor();
  737. if (in_control_anchor != nullptr) {
  738. for (const auto &peer_out_control_anchor : in_control_anchor->GetPeerOutControlAnchors()) {
  739. auto src_cluster = partitioner_->node_2_cluster_[peer_out_control_anchor->GetOwnerNode()];
  740. if (src_cluster->id_ != id_) {
  741. REQUIRE_GRAPH_SUCCESS(
  742. GraphUtils::RemoveEdge(peer_out_control_anchor, in_control_anchor),
  743. "Failed remove edge from node %s index %d to node %s index %d.",
  744. peer_out_control_anchor->GetOwnerNode()->GetName().c_str(), AnchorUtils::GetIdx(peer_out_control_anchor),
  745. in_control_anchor->GetOwnerNode()->GetName().c_str(), AnchorUtils::GetIdx(in_control_anchor));
  746. control_inputs_.insert(src_cluster);
  747. src_cluster->control_outputs_.insert(peer_out_control_anchor);
  748. }
  749. }
  750. }
  751. if (IsData() || IsIndependent()) {
  752. for (const auto &anchor : node->GetAllOutDataAnchors()) {
  753. AddFrameOutput(anchor);
  754. }
  755. } else {
  756. for (const auto &anchor : node->GetAllInDataAnchors()) {
  757. AddFrameInput(anchor);
  758. }
  759. }
  760. partition_node_ = node;
  761. }
  762. return SUCCESS;
  763. }
  764. Status Cluster::BuildPartitionFrame() {
  765. auto graph = partitioner_->root_graph_;
  766. bool is_unknown_shape = IsUnknownShape();
  767. bool is_input = IsInputNode();
  768. string known_name = (is_unknown_shape ? "_unknow" : "_know");
  769. string sub_graph_name_patten = (is_input ? "_input" : known_name);
  770. std::string sub_graph_name = graph->GetName() + "_sub_" + std::to_string(unique_id_) + sub_graph_name_patten;
  771. subgraph_ = MakeShared<ComputeGraph>(sub_graph_name);
  772. REQUIRE_NOT_NULL(subgraph_, "Failed new memory for subgraph.");
  773. auto partition_op = MakeShared<OpDesc>("PartitionedCall_" + std::to_string(unique_id_++), "PartitionedCall");
  774. REQUIRE_NOT_NULL(partition_op, "Failed new memory for partition op.");
  775. REQUIRE(AttrUtils::SetBool(partition_op, ATTR_NAME_IS_UNKNOWN_SHAPE, is_unknown_shape),
  776. "Failed set _is_unknown_shape flag on partitioned op %s.", partition_op->GetName().c_str());
  777. REQUIRE_GRAPH_SUCCESS(partition_op->AddSubgraphName(subgraph_->GetName()), "Failed add subgraph name.");
  778. REQUIRE_GRAPH_SUCCESS(partition_op->SetSubgraphInstanceName(0, subgraph_->GetName()),
  779. "Failed set subgraph instance name.");
  780. for (auto &node : nodes_) {
  781. REQUIRE_NOT_NULL(subgraph_->AddNode(node), "Failed add node to subgraph.");
  782. REQUIRE(AttrUtils::SetBool(node->GetOpDesc(), ATTR_NAME_IS_UNKNOWN_SHAPE, is_unknown_shape),
  783. "Failed set shape flag.");
  784. REQUIRE_GRAPH_SUCCESS(GraphUtils::RemoveJustNode(graph, node), "Failed remove root graph node.");
  785. REQUIRE_GRAPH_SUCCESS(node->SetOwnerComputeGraph(subgraph_), "Failed set owner graph.");
  786. for (const auto &anchor : node->GetAllInDataAnchors()) {
  787. auto peer_out_anchor = anchor->GetPeerOutAnchor();
  788. if (peer_out_anchor == nullptr) {
  789. continue; // Skip overhang input.
  790. }
  791. auto src_cluster = partitioner_->node_2_cluster_[peer_out_anchor->GetOwnerNode()];
  792. if (src_cluster->id_ != id_) {
  793. AddFrameInput(anchor);
  794. REQUIRE_GRAPH_SUCCESS(partition_op->AddInputDesc(node->GetOpDesc()->GetInputDesc(anchor->GetIdx())),
  795. "Failed add input desc.");
  796. }
  797. }
  798. auto in_control_anchor = node->GetInControlAnchor();
  799. if (in_control_anchor != nullptr) {
  800. for (const auto &peer_out_control_anchor : in_control_anchor->GetPeerOutControlAnchors()) {
  801. if (peer_out_control_anchor == nullptr) {
  802. continue;
  803. }
  804. auto src_cluster = partitioner_->node_2_cluster_[peer_out_control_anchor->GetOwnerNode()];
  805. if (src_cluster->id_ != id_) {
  806. REQUIRE_GRAPH_SUCCESS(
  807. GraphUtils::RemoveEdge(peer_out_control_anchor, in_control_anchor),
  808. "Failed remove edge from %s:%d to %s:%d.", peer_out_control_anchor->GetOwnerNode()->GetName().c_str(),
  809. peer_out_control_anchor->GetIdx(), node->GetName().c_str(), in_control_anchor->GetIdx());
  810. control_inputs_.insert(src_cluster);
  811. src_cluster->control_outputs_.insert(peer_out_control_anchor);
  812. }
  813. }
  814. }
  815. for (const auto &anchor : node->GetAllOutDataAnchors()) {
  816. auto peer_in_anchors = anchor->GetPeerInDataAnchors();
  817. for (const auto &peer_in_anchor : peer_in_anchors) {
  818. auto src_cluster = partitioner_->node_2_cluster_[peer_in_anchor->GetOwnerNode()];
  819. if (src_cluster->id_ != id_) {
  820. AddFrameOutput(anchor);
  821. REQUIRE_GRAPH_SUCCESS(partition_op->AddOutputDesc(node->GetOpDesc()->GetOutputDesc(anchor->GetIdx())),
  822. "Failed add output desc.");
  823. break;
  824. }
  825. }
  826. }
  827. }
  828. partition_node_ = graph->AddNode(partition_op);
  829. REQUIRE_NOT_NULL(partition_node_, "Failed add partition node.");
  830. REQUIRE_GRAPH_SUCCESS(partition_node_->SetOwnerComputeGraph(graph), "Failed set owner graph.");
  831. subgraph_->SetParentNode(partition_node_);
  832. subgraph_->SetParentGraph(graph);
  833. REQUIRE_GRAPH_SUCCESS(graph->AddSubgraph(subgraph_), "Failed add subgraph to root graph.");
  834. std::string session_graph_id;
  835. REQUIRE(AttrUtils::GetStr(*graph, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id),
  836. "Failed get ATTR_NAME_SESSION_GRAPH_ID on root graph.");
  837. REQUIRE(AttrUtils::SetStr(*subgraph_, ATTR_NAME_SESSION_GRAPH_ID, session_graph_id),
  838. "Failed set ATTR_NAME_SESSION_GRAPH_ID on subgraph.");
  839. return SUCCESS;
  840. }
  841. Status Cluster::CombinePartitionFrame() {
  842. for (const auto &anchor : inputs_) {
  843. auto peer_out_anchor = anchor->GetPeerOutAnchor();
  844. auto src_cluster = partitioner_->node_2_cluster_[peer_out_anchor->GetOwnerNode()];
  845. auto src_anchor = src_cluster->GetFrameOutDataAnchor(peer_out_anchor);
  846. auto dst_anchor = GetFrameInDataAnchor(anchor);
  847. REQUIRE_GRAPH_SUCCESS(GraphUtils::RemoveEdge(peer_out_anchor, anchor), "Failed remove edge from %s:%d to %s:%d.",
  848. peer_out_anchor->GetOwnerNode()->GetName().c_str(), peer_out_anchor->GetIdx(),
  849. anchor->GetOwnerNode()->GetName().c_str(), anchor->GetIdx());
  850. REQUIRE_GRAPH_SUCCESS(GraphUtils::AddEdge(src_anchor, dst_anchor), "Failed add edge from %s:%d to %s:%d.",
  851. src_anchor->GetOwnerNode()->GetName().c_str(), src_anchor->GetIdx(),
  852. dst_anchor->GetOwnerNode()->GetName().c_str(), dst_anchor->GetIdx());
  853. }
  854. for (const auto &src_cluster : control_inputs_) {
  855. auto src_anchor = src_cluster->GetFrameOutControlAnchor();
  856. auto dst_anchor = GetFrameInControlAnchor();
  857. REQUIRE_GRAPH_SUCCESS(GraphUtils::AddEdge(src_anchor, dst_anchor), "Failed add edge from %s:%d to %s:%d.",
  858. src_anchor->GetOwnerNode()->GetName().c_str(), src_anchor->GetIdx(),
  859. dst_anchor->GetOwnerNode()->GetName().c_str(), dst_anchor->GetIdx());
  860. }
  861. return SUCCESS;
  862. }
  863. Status Cluster::BuildPartitionSubgraph() {
  864. if (IsData() || IsNetOutput() || IsIndependent()) {
  865. return SUCCESS;
  866. }
  867. int64_t parent_node_index = 0;
  868. for (auto anchor : inputs_) {
  869. auto data_op =
  870. MakeShared<OpDesc>(subgraph_->GetName() + std::string("Data_") + std::to_string(parent_node_index), ge::DATA);
  871. REQUIRE_NOT_NULL(data_op, "Failed new memory for data op.");
  872. auto input_desc = anchor->GetOwnerNode()->GetOpDesc()->GetInputDesc(anchor->GetIdx());
  873. REQUIRE_GRAPH_SUCCESS(data_op->AddInputDesc(input_desc), "Failed add input desc.");
  874. REQUIRE_GRAPH_SUCCESS(data_op->AddOutputDesc(input_desc), "Failed add output desc.");
  875. REQUIRE(AttrUtils::SetInt(data_op, ATTR_NAME_PARENT_NODE_INDEX, parent_node_index),
  876. "Failed set parent_node_index on subgraph data node.");
  877. bool is_unknown_shape = IsUnknownShape();
  878. REQUIRE(AttrUtils::SetBool(data_op, ATTR_NAME_IS_UNKNOWN_SHAPE, is_unknown_shape),
  879. "Failed set _is_unknown_shape flag on data op %s.", data_op->GetName().c_str());
  880. auto data_node = subgraph_->AddNode(data_op);
  881. REQUIRE_NOT_NULL(data_node, "Failed add data node to subgraph.");
  882. REQUIRE_GRAPH_SUCCESS(data_node->SetOwnerComputeGraph(subgraph_), "Failed set owner graph of data node.");
  883. REQUIRE_GRAPH_SUCCESS(GraphUtils::AddEdge(data_node->GetOutDataAnchor(0), anchor),
  884. "Faile add data input edge to %s:%d", anchor->GetOwnerNode()->GetName().c_str(),
  885. anchor->GetIdx());
  886. parent_node_index++;
  887. }
  888. if (outputs_.empty() && control_outputs_.empty()) {
  889. return SUCCESS;
  890. }
  891. auto net_output_op = MakeShared<OpDesc>(subgraph_->GetName() + "_" + NODE_NAME_NET_OUTPUT, ge::NETOUTPUT);
  892. REQUIRE_NOT_NULL(net_output_op, "Failed new memory for netoutput op.");
  893. bool is_unknown_shape = IsUnknownShape();
  894. REQUIRE(AttrUtils::SetBool(net_output_op, ATTR_NAME_IS_UNKNOWN_SHAPE, is_unknown_shape),
  895. "Failed set _is_unknown_shape flag on net_output_op %s.", net_output_op->GetName().c_str());
  896. for (size_t i = 0; i < outputs_.size(); ++i) {
  897. GeTensorDesc input_desc;
  898. REQUIRE_GRAPH_SUCCESS(net_output_op->AddInputDesc(input_desc), "Failed add input desc.");
  899. }
  900. auto net_output_node = subgraph_->AddNode(net_output_op);
  901. REQUIRE_NOT_NULL(net_output_node, "Failed add netoutput node to subgraph.");
  902. REQUIRE_GRAPH_SUCCESS(net_output_node->SetOwnerComputeGraph(subgraph_), "Failed set owner graph of netoutput node.");
  903. parent_node_index = 0;
  904. for (const auto &anchor : outputs_) {
  905. auto output_desc = anchor->GetOwnerNode()->GetOpDesc()->GetOutputDesc(static_cast<uint32_t>(anchor->GetIdx()));
  906. REQUIRE(AttrUtils::SetInt(output_desc, ATTR_NAME_PARENT_NODE_INDEX, parent_node_index),
  907. "Failed set parent_node_index on subgraph netoutput's input.");
  908. REQUIRE_GRAPH_SUCCESS(net_output_op->UpdateInputDesc(parent_node_index, output_desc),
  909. "Failed update input desc of netoutput node.");
  910. REQUIRE_GRAPH_SUCCESS(GraphUtils::AddEdge(anchor, net_output_node->GetInDataAnchor(parent_node_index)),
  911. "Faile add edge from %s:%d to netoutput node.", anchor->GetOwnerNode()->GetName().c_str(),
  912. anchor->GetIdx());
  913. parent_node_index++;
  914. }
  915. for (const auto &anchor : control_outputs_) {
  916. REQUIRE_GRAPH_SUCCESS(GraphUtils::AddEdge(anchor, net_output_node->GetInControlAnchor()),
  917. "Faile add control edge from %s:%d to netoutput node.",
  918. anchor->GetOwnerNode()->GetName().c_str(), anchor->GetIdx());
  919. }
  920. return SUCCESS;
  921. }
  922. void Cluster::Clear() {
  923. in_clusters_.clear();
  924. out_clusters_.clear();
  925. nodes_.clear();
  926. partitioner_ = nullptr;
  927. inputs_index_.clear();
  928. outputs_index_.clear();
  929. inputs_.clear();
  930. outputs_.clear();
  931. control_inputs_.clear();
  932. control_outputs_.clear();
  933. partition_node_.reset();
  934. subgraph_.reset();
  935. unique_id_ = 0;
  936. }
  937. thread_local size_t Cluster::unique_id_ = 0;
  938. } // namespace ge

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