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 8.8 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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. // todo comments
  38. public:
  39. ///
  40. /// Optimize on one node. the function can add nodes to the graph, change
  41. /// connections between nodes while optimizing or remove nodes from the graph.
  42. /// @param node
  43. /// @return
  44. ///
  45. virtual Status Run(NodePtr &node) = 0;
  46. virtual ~BaseNodePass() = default;
  47. const std::vector<NodePtr> &GetNodesNeedRePass() { return nodes_need_re_pass_; }
  48. const std::unordered_set<NodePtr> &GetNodesNeedRePassImmediately() { return nodes_need_re_pass_immediately_; }
  49. const std::unordered_set<NodePtr> &GetNodesDeleted() { return nodes_deleted_; }
  50. const std::unordered_set<NodePtr> &GetNodesSuspend() { return nodes_suspend_; }
  51. const std::unordered_set<NodePtr> &GetNodesResume() { return nodes_resume_; }
  52. virtual Status OnSuspendNodesLeaked() { return SUCCESS; }
  53. void SetOption(NodePassOption option, const std::string &value) { options_[option] = value; }
  54. void ClearOptions() { options_.clear(); }
  55. void init() {
  56. nodes_need_re_pass_.clear();
  57. nodes_need_re_pass_immediately_.clear();
  58. nodes_deleted_.clear();
  59. nodes_suspend_.clear();
  60. nodes_resume_.clear();
  61. }
  62. virtual void OnStartPassGraph(const ComputeGraphPtr &graph) {
  63. current_graph_name_ = graph->GetName();
  64. }
  65. protected:
  66. const string &GetCurrentGraphName() const {
  67. return current_graph_name_;
  68. }
  69. Status IsolateAndDeleteNode(NodePtr &node, const std::vector<int> &io_map, bool is_repass_io_immediately = false);
  70. Status IsolateAndDeleteNode(NodePtr &node, const std::initializer_list<int> &io_map, bool is_repass_io_immediately = false) {
  71. return IsolateAndDeleteNode(node, std::vector<int>(io_map), is_repass_io_immediately);
  72. }
  73. ///
  74. /// Add a node to be optimized again. If you add a new node to the graph, or
  75. /// change a node connections, and you want to make sure the node will be
  76. /// optimized by other passes, call this function.
  77. /// @param node
  78. ///
  79. void AddRePassNode(const NodePtr &node) { nodes_need_re_pass_.emplace_back(node); }
  80. ///
  81. /// Add a node to be optimized immediately again. If you add a new node to the graph, or
  82. /// change a node connections, and you want to make sure the node will be
  83. /// optimized by other passes, call this function.
  84. /// @param node
  85. ///
  86. void AddImmediateRePassNode(const NodePtr &node) { nodes_need_re_pass_immediately_.insert(node); }
  87. ///
  88. /// Add a node and it's input/output data nodes to be optimized again.
  89. /// @param node
  90. ///
  91. void AddRePassNodesWithInOut(const NodePtr &node) {
  92. auto in_nodes = node->GetInNodes();
  93. for (auto &in_node : in_nodes) {
  94. AddRePassNode(in_node);
  95. }
  96. AddRePassNode(node);
  97. auto out_nodes = node->GetOutNodes();
  98. for (auto &out_node : out_nodes) {
  99. AddRePassNode(out_node);
  100. }
  101. }
  102. ///
  103. /// Add a node and it's input/output data nodes to be optimized immediately again.
  104. /// @param node
  105. ///
  106. void AddImmediateRePassNodesWithInOut(const NodePtr &node) {
  107. auto in_nodes = node->GetInNodes();
  108. for (auto &in_node : in_nodes) {
  109. AddImmediateRePassNode(in_node);
  110. }
  111. AddImmediateRePassNode(node);
  112. auto out_nodes = node->GetOutNodes();
  113. for (auto &out_node : out_nodes) {
  114. AddImmediateRePassNode(out_node);
  115. }
  116. }
  117. ///
  118. /// If you deleted a node from the graph, especially current node. The remain
  119. /// iterate passes will continue process on the deleted node(if it can be
  120. /// reached by edge connections) till the last one. Obviously it is a waste of
  121. /// time. You can add the deleted nodes by calling this function, to stop the
  122. /// next iterations.
  123. /// @param node
  124. ///
  125. void AddNodeDeleted(const NodePtr &node) { nodes_deleted_.insert(node); }
  126. ///
  127. /// If you postpone a node from the graph, especially following node. The remain
  128. /// iterate passes will stop process on the postpone node(if it can be
  129. /// reached by edge connections) till the last one. Obviously it is a waste of
  130. /// time. You can add the postpone nodes by calling this function, to stop the
  131. /// next iterations.
  132. /// @param node
  133. ///
  134. void AddNodeSuspend(const NodePtr &node) { nodes_suspend_.insert(node); }
  135. void AddNodeResume(const NodePtr &node) { nodes_resume_.insert(node); }
  136. bool OptionExists(NodePassOption option) { return options_.count(option) > 0; }
  137. private:
  138. std::vector<NodePtr> nodes_need_re_pass_;
  139. std::unordered_set<NodePtr> nodes_need_re_pass_immediately_;
  140. std::unordered_set<NodePtr> nodes_deleted_;
  141. std::unordered_set<NodePtr> nodes_suspend_;
  142. std::unordered_set<NodePtr> nodes_resume_;
  143. std::map<NodePassOption, std::string> options_;
  144. std::string current_graph_name_;
  145. };
  146. using NamesToPass = std::vector<std::pair<std::string, BaseNodePass *>>;
  147. class GEPass {
  148. public:
  149. explicit GEPass(ComputeGraphPtr &graph) : graph_(graph), root_graph_(graph), depth_(1) {}
  150. virtual ~GEPass() = default;
  151. Status Run(const NamesToPass &names_to_passes);
  152. /*
  153. * todo
  154. * OneGraph: nodes_deleted, nodes_seen, nodes_passed, nodes_suspended
  155. * RePass: nodes_re_pass
  156. * GraphOneTime: nodes_last
  157. * NodeOneTime: nodes_re_pass_immediately, nodes_resume
  158. */
  159. struct GraphLevelState {
  160. std::unordered_set<NodePtr> nodes_deleted;
  161. std::unordered_set<Node *> nodes_seen;
  162. std::unordered_set<NodePtr> nodes_passed;
  163. std::unordered_set<NodePtr> nodes_suspend;
  164. std::unordered_set<NodePtr> nodes_last;
  165. std::deque<NodePtr> nodes;
  166. int re_pass_times;
  167. void AddNodeToQueueFront(NodePtr node) {
  168. nodes_seen.insert(node.get());
  169. nodes.emplace_front(std::move(node));
  170. }
  171. void AddNodeToQueue(NodePtr node) {
  172. nodes_seen.insert(node.get());
  173. nodes.emplace_back(std::move(node));
  174. }
  175. void AddNodeToQueueIfNotSeen(NodePtr node) {
  176. if (nodes_seen.insert(node.get()).second) {
  177. nodes.emplace_back(std::move(node));
  178. }
  179. }
  180. NodePtr PopFront() {
  181. NodePtr node = nodes.front();
  182. nodes.pop_front();
  183. return node;
  184. }
  185. };
  186. struct RepassLevelState {
  187. std::vector<NodePtr> nodes_re_pass;
  188. std::unordered_set<NodePtr> nodes_re_pass_set;
  189. bool AddNodeToRepass(NodePtr node) {
  190. if (!nodes_re_pass_set.insert(node).second) {
  191. return false;
  192. }
  193. nodes_re_pass.emplace_back(node);
  194. return true;
  195. }
  196. void EraseNodeFromRepass(NodePtr node) {
  197. nodes_re_pass_set.erase(node);
  198. }
  199. void ClearRepass() {
  200. nodes_re_pass_set.clear();
  201. nodes_re_pass.clear();
  202. }
  203. };
  204. struct GraphOneTimeLevelState {
  205. std::unordered_set<NodePtr> nodes_last;
  206. };
  207. private:
  208. GEPass(ComputeGraphPtr &graph, ComputeGraphPtr &root_graph, int depth)
  209. : graph_(graph), root_graph_(root_graph), depth_(depth) {}
  210. Status RunPassesNodeOnce(NodePtr &node, const NamesToPass &names_to_passes,
  211. GraphLevelState &g_state, RepassLevelState &rp_state);
  212. Status RunPassesGraphRepass(const NamesToPass &names_to_passes, GraphLevelState &g_state);
  213. Status RunPassesOneGraph(const NamesToPass &names_to_passes);
  214. Status RunPassesOnSubGraph(const NodePtr &node, const NamesToPass &names_to_passes, bool &has_sub_graph);
  215. Status RunPassesOnNode(NodePtr &node, const NamesToPass &names_to_passes, GraphLevelState &g_state,
  216. RepassLevelState &rp_state);
  217. Status HandleLeakedSuspendNodes(const NamesToPass &names_to_passes, GraphLevelState &g_state);
  218. ComputeGraphPtr graph_;
  219. ComputeGraphPtr root_graph_;
  220. int depth_;
  221. };
  222. } // namespace ge
  223. #endif // GE_GRAPH_PASSES_BASE_PASS_H_

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