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.

base_pass.h 4.9 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #ifndef GE_GRAPH_PASSES_BASE_PASS_H_
  17. #define GE_GRAPH_PASSES_BASE_PASS_H_
  18. #include <set>
  19. #include <string>
  20. #include <unordered_set>
  21. #include <utility>
  22. #include <vector>
  23. #include "framework/common/ge_inner_error_codes.h"
  24. #include "framework/common/types.h"
  25. #include "graph/compute_graph.h"
  26. #include "graph/utils/op_desc_utils.h"
  27. namespace ge {
  28. enum NodePassOption {
  29. // if there is a sub graph on the node, the pass on the node will do:
  30. // Pass(node) -> pass all sub graphs on the node -> Pass(node)
  31. // when pass the node for the second time, the kOptimizeAfterSubGraph will be set as a flag key
  32. kOptimizeAfterSubGraph,
  33. // add new options before kOptionEnd
  34. kOptionEnd
  35. };
  36. class BaseNodePass {
  37. public:
  38. ///
  39. /// Optimize on one node. the function can add nodes to the graph, change
  40. /// connections between nodes while optimizing or remove nodes from the graph.
  41. /// @param node
  42. /// @return
  43. ///
  44. virtual Status Run(NodePtr &node) = 0;
  45. virtual ~BaseNodePass() = default;
  46. std::unordered_set<NodePtr> GetNodesNeedRePass() { return nodes_need_re_pass_; }
  47. std::unordered_set<NodePtr> GetNodesNeedRePassImmediately() { return nodes_need_re_pass_immediately_; }
  48. std::unordered_set<NodePtr> GetNodesDeleted() { return nodes_deleted_; }
  49. void SetOption(NodePassOption option, const std::string &value) { options_[option] = value; }
  50. void ClearOptions() { options_.clear(); }
  51. void init() {
  52. nodes_need_re_pass_.clear();
  53. nodes_deleted_.clear();
  54. nodes_need_re_pass_immediately_.clear();
  55. }
  56. protected:
  57. Status IsolateAndDeleteNode(NodePtr &node, const std::vector<int> &io_map);
  58. Status IsolateAndDeleteNode(NodePtr &node, const std::initializer_list<int> &io_map) {
  59. return IsolateAndDeleteNode(node, std::vector<int>(io_map));
  60. }
  61. ///
  62. /// Add a node to be optimized again. If you add a new node to the graph, or
  63. /// change a node connections, and you want to make sure the node will be
  64. /// optimized by other passes, call this function.
  65. /// @param node
  66. ///
  67. void AddRePassNode(NodePtr &node) { nodes_need_re_pass_.insert(node); }
  68. ///
  69. /// Add a node to be optimized immediately again. If you add a new node to the graph, or
  70. /// change a node connections, and you want to make sure the node will be
  71. /// optimized by other passes, call this function.
  72. /// @param node
  73. ///
  74. void AddImmediateRePassNode(NodePtr &node) { nodes_need_re_pass_immediately_.insert(node); }
  75. ///
  76. /// Add a node and it's input/output data nodes to be optimized again.
  77. /// @param node
  78. ///
  79. void AddRePassNodesWithInOut(NodePtr &node) {
  80. AddRePassNode(node);
  81. auto out_nodes = node->GetOutNodes();
  82. for (auto &out_node : out_nodes) {
  83. AddRePassNode(out_node);
  84. }
  85. auto in_nodes = node->GetInNodes();
  86. for (auto &in_node : in_nodes) {
  87. AddRePassNode(in_node);
  88. }
  89. }
  90. ///
  91. /// If you deleted a node from the graph, especially current node. The remain
  92. /// iterate passes will continue process on the deleted node(if it can be
  93. /// reached by edge connections) till the last one. Obviously it is a waste of
  94. /// time. You can add the deleted nodes by calling this function, to stop the
  95. /// next iterations.
  96. /// @param node
  97. ///
  98. void AddNodeDeleted(const NodePtr &node) { nodes_deleted_.insert(node); }
  99. bool OptionExists(NodePassOption option) { return options_.count(option) > 0; }
  100. private:
  101. std::unordered_set<NodePtr> nodes_need_re_pass_;
  102. std::unordered_set<NodePtr> nodes_need_re_pass_immediately_;
  103. std::unordered_set<NodePtr> nodes_deleted_;
  104. std::map<NodePassOption, std::string> options_;
  105. };
  106. using NamesToPass = std::vector<std::pair<std::string, BaseNodePass *>>;
  107. class GEPass {
  108. public:
  109. explicit GEPass(ComputeGraphPtr &graph) : graph_(graph), root_graph_(graph), depth_(1) {}
  110. virtual ~GEPass() = default;
  111. Status Run(const NamesToPass &names_to_passes);
  112. private:
  113. GEPass(ComputeGraphPtr &graph, ComputeGraphPtr &root_graph, int depth)
  114. : graph_(graph), root_graph_(root_graph), depth_(depth) {}
  115. Status RunPassesOneGraph(const NamesToPass &names_to_passes);
  116. Status RunPassesOnSubGraph(const NodePtr &node, const NamesToPass &names_to_passes, bool &has_sub_graph);
  117. ComputeGraphPtr graph_;
  118. ComputeGraphPtr root_graph_;
  119. int depth_;
  120. };
  121. } // namespace ge
  122. #endif // GE_GRAPH_PASSES_BASE_PASS_H_

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