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.1 kB

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

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