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

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