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.4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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> GetNodesDeleted() { return nodes_deleted_; }
  48. void SetOption(NodePassOption option, const std::string &value) { options_[option] = value; }
  49. void ClearOptions() { options_.clear(); }
  50. void init() {
  51. nodes_need_re_pass_.clear();
  52. nodes_deleted_.clear();
  53. }
  54. protected:
  55. Status IsolateAndDeleteNode(NodePtr &node, const std::vector<int> &io_map);
  56. Status IsolateAndDeleteNode(NodePtr &node, const std::initializer_list<int> &io_map) {
  57. return IsolateAndDeleteNode(node, std::vector<int>(io_map));
  58. }
  59. ///
  60. /// Add a node to be optimized again. If you add a new node to the graph, or
  61. /// change a node connections, and you want to make sure the node will be
  62. /// optimized by other passes, call this function.
  63. /// @param node
  64. ///
  65. void AddRePassNode(NodePtr &node) { nodes_need_re_pass_.insert(node); }
  66. ///
  67. /// Add a node and it's input/output data nodes to be optimized again.
  68. /// @param node
  69. ///
  70. void AddRePassNodesWithInOut(NodePtr &node) {
  71. AddRePassNode(node);
  72. auto out_nodes = node->GetOutNodes();
  73. for (auto &out_node : out_nodes) {
  74. AddRePassNode(out_node);
  75. }
  76. auto in_nodes = node->GetInNodes();
  77. for (auto &in_node : in_nodes) {
  78. AddRePassNode(in_node);
  79. }
  80. }
  81. ///
  82. /// If you deleted a node from the graph, especially current node. The remain
  83. /// iterate passes will continue process on the deleted node(if it can be
  84. /// reached by edge connections) till the last one. Obviously it is a waste of
  85. /// time. You can add the deleted nodes by calling this function, to stop the
  86. /// next iterations.
  87. /// @param node
  88. ///
  89. void AddNodeDeleted(const NodePtr &node) { nodes_deleted_.insert(node); }
  90. bool OptionExists(NodePassOption option) { return options_.count(option) > 0; }
  91. private:
  92. std::unordered_set<NodePtr> nodes_need_re_pass_;
  93. std::unordered_set<NodePtr> nodes_deleted_;
  94. std::map<NodePassOption, std::string> options_;
  95. };
  96. using NamesToPass = std::vector<std::pair<std::string, BaseNodePass *>>;
  97. class GEPass {
  98. public:
  99. explicit GEPass(ComputeGraphPtr &graph) : graph_(graph), root_graph_(graph), depth_(1) {}
  100. virtual ~GEPass() = default;
  101. Status Run(const NamesToPass &names_to_passes);
  102. private:
  103. GEPass(ComputeGraphPtr &graph, ComputeGraphPtr &root_graph, int depth)
  104. : graph_(graph), root_graph_(root_graph), depth_(depth) {}
  105. Status RunPassesOneGraph(const NamesToPass &names_to_passes);
  106. Status RunPassesOnSubGraph(const NodePtr &node, const NamesToPass &names_to_passes, bool &has_sub_graph);
  107. ComputeGraphPtr graph_;
  108. ComputeGraphPtr root_graph_;
  109. int depth_;
  110. };
  111. } // namespace ge
  112. #endif // GE_GRAPH_PASSES_BASE_PASS_H_

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