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

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