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

dynamic_shape_partition.cc 40 kB

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

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