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.

prune_pass.cc 3.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * Copyright 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/passes/prune_pass.h"
  17. #include <deque>
  18. #include <set>
  19. #include <unordered_set>
  20. #include <vector>
  21. #include "common/debug/log.h"
  22. #include "common/types.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "framework/common/ge_inner_error_codes.h"
  25. #include "graph/utils/node_utils.h"
  26. namespace ge {
  27. Status PrunePass::Run(ge::ComputeGraphPtr graph) {
  28. GELOGD("PrunePass Start");
  29. if (graph == nullptr) {
  30. GELOGE(GE_GRAPH_ISNULL, "input compute graph is NULL.");
  31. return GE_GRAPH_ISNULL;
  32. }
  33. std::vector<NodePtr> out_nodes;
  34. std::unordered_set<NodePtr> nodes;
  35. for (NodePtr &node_ptr : graph->GetDirectNode()) {
  36. GE_CHECK_NOTNULL(node_ptr->GetOpDesc());
  37. nodes.insert(node_ptr);
  38. if (node_ptr->GetOpDesc()->GetType() == NETOUTPUT) {
  39. out_nodes.push_back(node_ptr);
  40. }
  41. }
  42. if (out_nodes.empty()) {
  43. GELOGW("graph [%s] does not contain NETOUTPUT type node,no return value. Do nothing!", graph->GetName().c_str());
  44. return ge::SUCCESS;
  45. }
  46. std::unordered_set<NodePtr> nodes_seen;
  47. for (NodePtr &node_ptr : out_nodes) {
  48. std::deque<NodePtr> queue;
  49. queue.push_back(node_ptr);
  50. nodes_seen.insert(node_ptr);
  51. while (!queue.empty()) {
  52. NodePtr node = queue.front();
  53. GE_CHECK_NOTNULL(node->GetOpDesc());
  54. queue.pop_front();
  55. for (auto &in_node : node->GetInAllNodes()) {
  56. if (nodes_seen.insert(in_node).second) {
  57. queue.push_back(in_node);
  58. }
  59. }
  60. }
  61. }
  62. for (auto &node_ptr : nodes) {
  63. if (nodes_seen.count(node_ptr) != 0) {
  64. continue;
  65. }
  66. if (node_ptr->GetOpDesc()->GetType() == DATA || node_ptr->GetOpDesc()->GetType() == AIPPDATA) {
  67. Status status = ge::GraphUtils::AddEdge(node_ptr->GetOutControlAnchor(), out_nodes[0]->GetInControlAnchor());
  68. if (status != ge::SUCCESS) {
  69. GELOGE(INTERNAL_ERROR, "[PrunePass] add control edge fail between DATA node[%s] and NETOUTPUT node[%s]!",
  70. node_ptr->GetOpDesc()->GetName().c_str(), out_nodes[0]->GetOpDesc()->GetName().c_str());
  71. return INTERNAL_ERROR;
  72. }
  73. GELOGI("[PrunePass] add extra control edge between DATA node[%s] and NETOUTPUT node[%s]!",
  74. node_ptr->GetOpDesc()->GetName().c_str(), out_nodes[0]->GetOpDesc()->GetName().c_str());
  75. continue;
  76. }
  77. // Remove subgraphs on the node before remove it in graph.
  78. (void)NodeUtils::RemoveSubgraphsOnNode(node_ptr);
  79. /// Common function:[RemoveNode] will delete not only input node but its constant input node also will be deleted
  80. (void)graph->RemoveNode(node_ptr);
  81. GELOGI("[PrunePass] remove graph node [%s]!", node_ptr->GetOpDesc()->GetName().c_str());
  82. }
  83. return ge::SUCCESS;
  84. }
  85. } // namespace ge

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