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

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

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