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 6.2 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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. const std::unordered_set<NodePtr> &GetNodesNeedRePass() { return nodes_need_re_pass_; }
  47. const std::unordered_set<NodePtr> &GetNodesNeedRePassImmediately() { return nodes_need_re_pass_immediately_; }
  48. const std::unordered_set<NodePtr> &GetNodesDeleted() { return nodes_deleted_; }
  49. const std::unordered_set<NodePtr> &GetNodesSuspend() { return nodes_suspend_; }
  50. const std::unordered_set<NodePtr> &GetNodesResume() { return nodes_resume_; }
  51. void SetOption(NodePassOption option, const std::string &value) { options_[option] = value; }
  52. void ClearOptions() { options_.clear(); }
  53. void init() {
  54. nodes_need_re_pass_.clear();
  55. nodes_deleted_.clear();
  56. nodes_need_re_pass_immediately_.clear();
  57. nodes_suspend_.clear();
  58. nodes_resume_.clear();
  59. }
  60. protected:
  61. Status IsolateAndDeleteNode(NodePtr &node, const std::vector<int> &io_map);
  62. Status IsolateAndDeleteNode(NodePtr &node, const std::initializer_list<int> &io_map) {
  63. return IsolateAndDeleteNode(node, std::vector<int>(io_map));
  64. }
  65. ///
  66. /// Add a node to be optimized again. If you add a new node to the graph, or
  67. /// change a node connections, and you want to make sure the node will be
  68. /// optimized by other passes, call this function.
  69. /// @param node
  70. ///
  71. void AddRePassNode(const NodePtr &node) { nodes_need_re_pass_.insert(node); }
  72. ///
  73. /// Add a node to be optimized immediately again. If you add a new node to the graph, or
  74. /// change a node connections, and you want to make sure the node will be
  75. /// optimized by other passes, call this function.
  76. /// @param node
  77. ///
  78. void AddImmediateRePassNode(const NodePtr &node) { nodes_need_re_pass_immediately_.insert(node); }
  79. ///
  80. /// Add a node and it's input/output data nodes to be optimized again.
  81. /// @param node
  82. ///
  83. void AddRePassNodesWithInOut(const NodePtr &node) {
  84. AddRePassNode(node);
  85. auto out_nodes = node->GetOutNodes();
  86. for (auto &out_node : out_nodes) {
  87. AddRePassNode(out_node);
  88. }
  89. auto in_nodes = node->GetInNodes();
  90. for (auto &in_node : in_nodes) {
  91. AddRePassNode(in_node);
  92. }
  93. }
  94. ///
  95. /// If you deleted a node from the graph, especially current node. The remain
  96. /// iterate passes will continue process on the deleted node(if it can be
  97. /// reached by edge connections) till the last one. Obviously it is a waste of
  98. /// time. You can add the deleted nodes by calling this function, to stop the
  99. /// next iterations.
  100. /// @param node
  101. ///
  102. void AddNodeDeleted(const NodePtr &node) { nodes_deleted_.insert(node); }
  103. ///
  104. /// If you suspend a node from the graph, especially following node. The remain
  105. /// iterate passes will stop process on the suspend node(if it can be
  106. /// reached by edge connections) till the last one. Obviously it is a waste of
  107. /// time. You can add the suspend nodes by calling this function, to stop the
  108. /// next iterations.
  109. /// @param node
  110. ///
  111. void AddNodeSuspend(const NodePtr &node) { nodes_suspend_.insert(node); }
  112. ///
  113. /// If you resume a node from the graph, especially following node. The remain
  114. /// iterate passes will continue process on the resume node(if it can be
  115. /// reached by edge connections) till the last one.
  116. /// You can add the resume nodes by calling this function, to resume the
  117. /// next iterations.
  118. /// @param node
  119. ///
  120. void AddNodeResume(const NodePtr &node) { nodes_resume_.insert(node); }
  121. bool OptionExists(NodePassOption option) { return options_.count(option) > 0; }
  122. private:
  123. std::unordered_set<NodePtr> nodes_need_re_pass_;
  124. std::unordered_set<NodePtr> nodes_need_re_pass_immediately_;
  125. std::unordered_set<NodePtr> nodes_deleted_;
  126. std::unordered_set<NodePtr> nodes_suspend_;
  127. std::unordered_set<NodePtr> nodes_resume_;
  128. std::map<NodePassOption, std::string> options_;
  129. };
  130. using NamesToPass = std::vector<std::pair<std::string, BaseNodePass *>>;
  131. class GEPass {
  132. public:
  133. explicit GEPass(ComputeGraphPtr &graph) : graph_(graph), root_graph_(graph), depth_(1) {}
  134. virtual ~GEPass() = default;
  135. Status Run(const NamesToPass &names_to_passes);
  136. private:
  137. GEPass(ComputeGraphPtr &graph, ComputeGraphPtr &root_graph, int depth)
  138. : graph_(graph), root_graph_(root_graph), depth_(depth) {}
  139. Status RunPassesOneGraph(const NamesToPass &names_to_passes);
  140. Status RunPassesOnSubGraph(const NodePtr &node, const NamesToPass &names_to_passes, bool &has_sub_graph);
  141. ComputeGraphPtr graph_;
  142. ComputeGraphPtr root_graph_;
  143. int depth_;
  144. };
  145. } // namespace ge
  146. #endif // GE_GRAPH_PASSES_BASE_PASS_H_

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