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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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, graph is [%s]", graph->GetName().c_str());
  29. if (graph == nullptr) {
  30. REPORT_INNER_ERROR("E19999", "Param graph is nullptr, check invalid");
  31. GELOGE(GE_GRAPH_ISNULL, "input compute graph is NULL.");
  32. return GE_GRAPH_ISNULL;
  33. }
  34. std::vector<NodePtr> out_nodes;
  35. std::unordered_set<NodePtr> nodes;
  36. for (NodePtr &node_ptr : graph->GetDirectNode()) {
  37. GE_CHECK_NOTNULL(node_ptr->GetOpDesc());
  38. nodes.insert(node_ptr);
  39. if (node_ptr->GetOpDesc()->GetType() == NETOUTPUT) {
  40. out_nodes.push_back(node_ptr);
  41. }
  42. }
  43. if (out_nodes.empty()) {
  44. GELOGW("graph [%s] does not contain NETOUTPUT type node,no return value. Do nothing!", graph->GetName().c_str());
  45. return ge::SUCCESS;
  46. }
  47. std::unordered_set<NodePtr> nodes_seen;
  48. for (NodePtr &node_ptr : out_nodes) {
  49. std::deque<NodePtr> queue;
  50. queue.push_back(node_ptr);
  51. nodes_seen.insert(node_ptr);
  52. while (!queue.empty()) {
  53. NodePtr node = queue.front();
  54. GE_CHECK_NOTNULL(node->GetOpDesc());
  55. queue.pop_front();
  56. for (auto &in_node : node->GetInAllNodes()) {
  57. if (nodes_seen.insert(in_node).second) {
  58. queue.push_back(in_node);
  59. }
  60. }
  61. }
  62. }
  63. for (auto &node_ptr : nodes) {
  64. if (nodes_seen.count(node_ptr) != 0) {
  65. continue;
  66. }
  67. if (node_ptr->GetOpDesc()->GetType() == DATA || node_ptr->GetOpDesc()->GetType() == AIPPDATA) {
  68. Status status = ge::GraphUtils::AddEdge(node_ptr->GetOutControlAnchor(), out_nodes[0]->GetInControlAnchor());
  69. if (status != ge::SUCCESS) {
  70. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  71. node_ptr->GetName().c_str(), node_ptr->GetType().c_str(),
  72. out_nodes[0]->GetName().c_str(), out_nodes[0]->GetType().c_str());
  73. GELOGE(INTERNAL_ERROR, "[PrunePass] add control edge fail between DATA node[%s] and NETOUTPUT node[%s]!",
  74. node_ptr->GetOpDesc()->GetName().c_str(), out_nodes[0]->GetOpDesc()->GetName().c_str());
  75. return INTERNAL_ERROR;
  76. }
  77. GELOGI("[PrunePass] add extra control edge between DATA node[%s] and NETOUTPUT node[%s]!",
  78. node_ptr->GetOpDesc()->GetName().c_str(), out_nodes[0]->GetOpDesc()->GetName().c_str());
  79. continue;
  80. }
  81. // Remove subgraphs on the node before remove it in graph.
  82. (void)NodeUtils::RemoveSubgraphsOnNode(node_ptr);
  83. /// Common function:[RemoveNode] will delete not only input node but its constant input node also will be deleted
  84. (void)graph->RemoveNode(node_ptr);
  85. GELOGI("[PrunePass] remove graph node [%s]!", node_ptr->GetOpDesc()->GetName().c_str());
  86. }
  87. return ge::SUCCESS;
  88. }
  89. } // namespace ge

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