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.

for_pass.h 6.0 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_FOR_PASS_H
  17. #define GE_GRAPH_PASSES_FOR_PASS_H
  18. #include "graph/passes/base_pass.h"
  19. struct ForInfo {
  20. ForInfo() : for_node(nullptr), start(nullptr), limit(nullptr), delta(nullptr), for_body(nullptr) {}
  21. ge::NodePtr for_node;
  22. ge::OutDataAnchorPtr start;
  23. ge::OutDataAnchorPtr limit;
  24. ge::OutDataAnchorPtr delta;
  25. std::string body_name;
  26. ge::ComputeGraphPtr for_body;
  27. std::vector<ge::OutDataAnchorPtr> data_inputs;
  28. std::vector<std::vector<ge::InDataAnchorPtr>> data_outputs;
  29. std::vector<ge::OutControlAnchorPtr> ctrl_inputs;
  30. std::vector<ge::InControlAnchorPtr> ctrl_outputs;
  31. };
  32. struct WhileInfo {
  33. WhileInfo()
  34. : while_node(nullptr), sub_graph_node(nullptr), i(nullptr), abs_delta(nullptr), range(nullptr),
  35. start(nullptr), delta(nullptr), for_body(nullptr), while_cond(nullptr), while_body(nullptr) {}
  36. ge::NodePtr while_node;
  37. ge::NodePtr sub_graph_node;
  38. ge::OutDataAnchorPtr i;
  39. ge::OutDataAnchorPtr abs_delta;
  40. ge::OutDataAnchorPtr range;
  41. ge::OutDataAnchorPtr start;
  42. ge::OutDataAnchorPtr delta;
  43. std::string for_body_name;
  44. ge::ComputeGraphPtr for_body;
  45. ge::ComputeGraphPtr while_cond;
  46. ge::ComputeGraphPtr while_body;
  47. std::vector<ge::OutDataAnchorPtr> data_inputs;
  48. std::vector<std::vector<ge::InDataAnchorPtr>> data_outputs;
  49. std::vector<ge::OutControlAnchorPtr> ctrl_inputs;
  50. std::vector<ge::InControlAnchorPtr> ctrl_outputs;
  51. };
  52. namespace ge {
  53. class ForPass : public BaseNodePass {
  54. public:
  55. Status Run(NodePtr &node) override;
  56. private:
  57. ///
  58. /// @brief Build for_info
  59. /// @param [in] root_graph
  60. /// @param [in] node
  61. /// @param [out] for_info
  62. /// @return Status
  63. ///
  64. static Status BuildForInfo(const ComputeGraphPtr &root_graph, const NodePtr &node, ForInfo &for_info);
  65. ///
  66. /// @brief Transfer while_info from for_info
  67. /// @param [in] graph
  68. /// @param [in] for_info
  69. /// @param [out] while_info
  70. /// @return Status
  71. ///
  72. Status TranWhileInfo(const ComputeGraphPtr &graph, const ForInfo &for_info, WhileInfo &while_info);
  73. ///
  74. /// @brief Build cond_graph for while_node
  75. /// @param [in&out] while_info
  76. /// @return ComputeGraphPtr
  77. ///
  78. static ComputeGraphPtr BuildCondGraph(WhileInfo &while_info);
  79. ///
  80. /// @brief Build body_graph for while_node
  81. /// @param [in&out] while_info
  82. /// @return ComputeGraphPtr
  83. ///
  84. static ComputeGraphPtr BuildBodyGraph(WhileInfo &while_info);
  85. ///
  86. /// @brief Update InputMapping for for-body-graph
  87. /// @param [in] while_info
  88. /// @return Status
  89. ///
  90. static Status UpdateForBodyInputMapping(const WhileInfo &while_info);
  91. ///
  92. /// @brief Find input with index for For node
  93. /// @param [in] node
  94. /// @param [in] index
  95. /// @return OutDataAnchorPtr
  96. ///
  97. static OutDataAnchorPtr FindInputWithIndex(const NodePtr &node, uint32_t index);
  98. ///
  99. /// @brief Find inputs / outputs for for node
  100. /// @param [in] node
  101. /// @param [out] data_inputs
  102. /// @param [out] data_outputs
  103. /// @param [out] ctrl_inputs
  104. /// @param [out] ctrl_outputs
  105. /// @return Status
  106. ///
  107. static Status FindInputsAndOutputs(const NodePtr &node, std::vector<OutDataAnchorPtr> &data_inputs,
  108. std::vector<std::vector<ge::InDataAnchorPtr>> &data_outputs,
  109. std::vector<ge::OutControlAnchorPtr> &ctrl_inputs,
  110. std::vector<ge::InControlAnchorPtr> &ctrl_outputs);
  111. ///
  112. /// @brief Create const op_desc
  113. /// @param [in] name
  114. /// @param [in] value
  115. /// @return OpDescPtr
  116. ///
  117. static OpDescPtr CreateConstDesc(const std::string &name, int32_t value);
  118. ///
  119. /// @brief Create loop input
  120. /// @param [in] graph
  121. /// @param [in] for_info
  122. /// @param [out] range_input
  123. /// @param [out] abs_delta_input
  124. /// @return Status
  125. ///
  126. Status CreateLoopInput(const ComputeGraphPtr &graph, const ForInfo &for_info, OutDataAnchorPtr &range_input,
  127. OutDataAnchorPtr &abs_delta_input);
  128. ///
  129. /// @brief Create op_desc
  130. /// @param [in] name
  131. /// @param [in] type
  132. /// @param [in] io_equal_flag
  133. /// @return OpDescPtr
  134. ///
  135. static OpDescPtr CreateOpDesc(const std::string &name, const std::string &type, bool io_equal_flag);
  136. ///
  137. /// @brief Build while-info
  138. /// @param [in] for_info
  139. /// @param [in] i_input
  140. /// @param [in] range_input
  141. /// @param [in] abs_delta_input
  142. /// @param [out] while_info
  143. /// @return void
  144. ///
  145. static void BuildWhileInfo(const ForInfo &for_info, const OutDataAnchorPtr &i_input,
  146. const OutDataAnchorPtr &range_input, const OutDataAnchorPtr &abs_delta_input,
  147. WhileInfo &while_info);
  148. ///
  149. /// @brief Insert while_node
  150. /// @param [in] graph
  151. /// @param [in] name
  152. /// @param [in] while_info
  153. /// @return Status
  154. ///
  155. Status InsertWhileNode(const ComputeGraphPtr &graph, const std::string &name, WhileInfo &while_info);
  156. ///
  157. /// @brief Build while link-edge
  158. /// @param [in] while_info
  159. /// @return Status
  160. ///
  161. static Status BuildWhileLink(const WhileInfo &while_info);
  162. ///
  163. /// @brief Create op_desc for subgraph node
  164. /// @param [in] name
  165. /// @param [in] input_num
  166. /// @param [in] output_num
  167. /// @return OpDescPtr
  168. ///
  169. static OpDescPtr CreateSubgraphOpDesc(const std::string &name, uint32_t input_num, uint32_t output_num);
  170. };
  171. } // namespace ge
  172. #endif //GE_GRAPH_PASSES_FOR_PASS_H

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