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.

graph_utils.h 13 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 INC_GRAPH_UTILS_GRAPH_UTILS_H_
  17. #define INC_GRAPH_UTILS_GRAPH_UTILS_H_
  18. #include <fstream>
  19. #include <iostream>
  20. #include <map>
  21. #include <string>
  22. #include <vector>
  23. #include "graph/anchor.h"
  24. #include "graph/node.h"
  25. #include "graph/compute_graph.h"
  26. #include "graph/utils/anchor_utils.h"
  27. #include "graph/graph.h"
  28. #include "graph/model.h"
  29. #define REFER_ATTR_VALUE(VT_ENUM, DataType, attr, ret) \
  30. do { \
  31. DataType ret; \
  32. attr.GetValue<DataType>(ret); \
  33. } while (0)
  34. #define PRINT_ATTR_VALUE_IF(value_type, VT_ENUM, DataType, attr, stream) \
  35. do { \
  36. if (value_type == VT_ENUM) { \
  37. REFER_ATTR_VALUE(VT_ENUM, DataType, attr, ret) \
  38. stream << ret; \
  39. } \
  40. } while (0)
  41. #define PRINT_LIST_ATTR_VALUE_IF(value_type, VT_ENUM, DataType, attr, stream) \
  42. do { \
  43. if (value_type == VT_ENUM) { \
  44. REFER_ATTR_VALUE(VT_ENUM, DataType, attr, ret) \
  45. stream << "["; \
  46. for (int i = 0; i < ret.size(); i++) { \
  47. stream << ret[i]; \
  48. if (i + 1 != ret.size()) stream << ", "; \
  49. } \
  50. stream << "]"; \
  51. } \
  52. } while (0)
  53. #define PRINT_ATTR_VALUE_ELIF(value_type, VT_ENUM, DataType, attr, stream) \
  54. else PRINT_ATTR_VALUE_IF(value_type, VT_ENUM, DataType, attr, stream)
  55. #define PRINT_LIST_ATTR_VALUE_ELIF(value_type, VT_ENUM, DataType, attr, stream) \
  56. else PRINT_LIST_ATTR_VALUE_IF(value_type, VT_ENUM, DataType, attr, stream)
  57. #define PRINT_SHAPE(i_o, n, idx, stream) \
  58. do { \
  59. auto op = n->GetOpDesc(); \
  60. GeTensorDesc td = i_o == "input" ? op->GetInputDesc(idx) : op->GetOutputDesc(idx); \
  61. auto shape = td.GetShape().GetDims(); \
  62. stream << "["; \
  63. for (int i = 0; i < shape.size(); i++) { \
  64. stream << shape[i]; \
  65. if (i + 1 < shape.size()) stream << ", "; \
  66. } \
  67. stream << "]"; \
  68. } while (0)
  69. #define PRINT_ATTR_FUNC(stream) \
  70. [&](GeAttrValue attr) { \
  71. auto type = attr.GetValueType(); \
  72. PRINT_ATTR_VALUE_IF(type, GeAttrValue::ValueType::VT_STRING, GeAttrValue::STR, attr, stream) \
  73. PRINT_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_FLOAT, GeAttrValue::FLOAT, attr, stream) \
  74. PRINT_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_BOOL, GeAttrValue::BOOL, attr, stream) \
  75. PRINT_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_INT, GeAttrValue::INT, attr, stream) \
  76. PRINT_LIST_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_LIST_STRING, GeAttrValue::LIST_STR, attr, stream) \
  77. PRINT_LIST_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_LIST_FLOAT, GeAttrValue::LIST_FLOAT, attr, stream) \
  78. PRINT_LIST_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_LIST_BOOL, GeAttrValue::LIST_BOOL, attr, stream) \
  79. PRINT_LIST_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_LIST_INT, GeAttrValue::LIST_INT, attr, stream) \
  80. else if (type == GeAttrValue::ValueType::VT_TENSOR_DESC) stream << "TENSOR_DESC"; \
  81. else if (type == GeAttrValue::ValueType::VT_TENSOR) stream << "TENSOR"; \
  82. else if (type == GeAttrValue::ValueType::VT_BYTES) stream << "BYTES"; \
  83. else if (type == GeAttrValue::ValueType::VT_LIST_TENSOR_DESC) stream << "LIST_TENSOR_DESC"; \
  84. else if (type == GeAttrValue::ValueType::VT_LIST_TENSOR) stream << "LIST_TENSOR"; \
  85. else if (type == GeAttrValue::ValueType::VT_LIST_BYTES) stream << "LIST_BYTES"; \
  86. };
  87. namespace ge {
  88. class GraphUtils {
  89. public:
  90. static ComputeGraphPtr GetComputeGraph(const Graph &graph);
  91. static Graph CreateGraphFromComputeGraph(const ComputeGraphPtr compute_graph);
  92. static ComputeGraphPtr CreateGraphFromOperator(const string &name, const std::vector<Operator> &inputs);
  93. static graphStatus AddEdge(const OutDataAnchorPtr &src, const InDataAnchorPtr &dst);
  94. static graphStatus AddEdge(const OutDataAnchorPtr &src, const Format &src_format, const InDataAnchorPtr &dst,
  95. const Format &dst_format);
  96. static graphStatus AddEdge(const AnchorPtr &src, const AnchorPtr &dst);
  97. static graphStatus AddEdge(const OutControlAnchorPtr &src, const InControlAnchorPtr &dst);
  98. static graphStatus AddEdge(const OutDataAnchorPtr &src, const InControlAnchorPtr &dst);
  99. // check whether src is link to dst and then remove
  100. static graphStatus RemoveEdge(const OutDataAnchorPtr &src, const InDataAnchorPtr &dst);
  101. static graphStatus RemoveEdge(const AnchorPtr &src, const AnchorPtr &dst);
  102. static graphStatus RemoveEdge(const OutControlAnchorPtr &src, const InControlAnchorPtr &dst);
  103. static graphStatus RemoveEdge(const OutDataAnchorPtr &src, const InControlAnchorPtr &dst);
  104. static graphStatus ReplaceEdgeDst(const OutDataAnchorPtr &src, const InDataAnchorPtr &dst,
  105. const InDataAnchorPtr &new_dst);
  106. static graphStatus ReplaceEdgeDst(const OutControlAnchorPtr &src, const InControlAnchorPtr &dst,
  107. const InControlAnchorPtr &new_dst);
  108. static graphStatus InsertNodeBetweenDataAnchors(const OutDataAnchorPtr &src, const InDataAnchorPtr &dst,
  109. const NodePtr &new_node);
  110. static graphStatus RemoveNodeWithoutRelink(const ComputeGraphPtr &compute_graph, const NodePtr &node);
  111. static graphStatus InsertTransNode(ComputeGraphPtr compute_graph, const InDataAnchorPtr &in_data_anchor,
  112. const std::vector<OpDescPtr> &vec_op_desc);
  113. static graphStatus RemoveJustNode(ComputeGraphPtr compute_graph, const NodePtr &node);
  114. static graphStatus RemoveJustNode(ComputeGraph &compute_graph, const NodePtr &node);
  115. static void RecordOriginalNames(std::vector<ge::NodePtr> original_nodes, const ge::NodePtr &node);
  116. static void RecordOriginalNames(std::vector<std::string> names_tmp, const ge::NodePtr &node);
  117. static bool CheckIsTrainGraph(const ge::ComputeGraphPtr &compute_graph);
  118. static bool MatchDumpStr(const std::string &suffix);
  119. static void DumpGEGraph(const ge::ComputeGraphPtr &graph, const std::string &suffix, bool is_always_dump = false);
  120. static bool LoadGEGraph(const char *file, ge::ComputeGraph &compute_graph);
  121. static bool CheckGlobalStepNode(const ge::NodePtr &node);
  122. static void BreakConnect(const std::map<OperatorImplPtr, NodePtr> &all_nodes_infos);
  123. static void DumpGEGraphToOnnx(const ge::ComputeGraph &compute_graph, const std::string &suffix);
  124. static bool LoadGEGraphFromOnnx(const char *file, ge::ComputeGraph &compute_graph);
  125. static bool ReadProtoFromTextFile(const char *file, google::protobuf::Message *message);
  126. static void WriteProtoToTextFile(const google::protobuf::Message &proto, const char *real_path);
  127. static graphStatus AppendInputNode(const ComputeGraphPtr &graph, const NodePtr &node);
  128. ///
  129. /// Isolating `node`, relinking data links from the in-anchor peer nodes to
  130. /// the out-anchor peer nodes according to `io_map`, relinking control links
  131. /// to ensure that input nodes of `node` are before out nodes
  132. ///
  133. /// Link the `io_map[i]` input anchor peer node to `i` output anchor peer
  134. /// nodes, then unlink all links connecting with `node`. If `io_map[i]` < 0,
  135. /// unlink all links from `i` output anchor without any relinking.
  136. ///
  137. /// @param node
  138. /// @param io_map
  139. /// @return
  140. ///
  141. static graphStatus IsolateNode(const NodePtr &node, const std::initializer_list<int> &io_map);
  142. static graphStatus IsolateNode(const NodePtr &node, const std::vector<int> &io_map);
  143. ///
  144. /// Isolate `node` which must be one input one output, equivalent to
  145. /// `IsolateNode(node, {0})`
  146. /// @param node
  147. /// @return
  148. ///
  149. static graphStatus IsolateNodeOneIO(const NodePtr &node);
  150. ///
  151. /// The data anchors replacing behavior is the same with
  152. /// `ReplaceNodeDataAnchors`. In addition, replace all `old_node` control
  153. /// anchors with `new_node`'s.
  154. /// @param new_node
  155. /// @param old_node
  156. /// @param inputs_map
  157. /// @param outputs_map
  158. /// @return
  159. ///
  160. static graphStatus ReplaceNodeAnchors(const NodePtr &new_node, const NodePtr &old_node,
  161. std::initializer_list<int> inputs_map, std::initializer_list<int> outputs_map);
  162. static graphStatus ReplaceNodeAnchors(const NodePtr &new_node, const NodePtr &old_node,
  163. const std::vector<int> &inputs_map, const std::vector<int> &outputs_map);
  164. ///
  165. /// Replace `old_node` data anchors with `new_node`'s according to `inputs_map` and `outputs_map`.
  166. /// Replace the `i` in/out data anchor on `old_node` with
  167. /// `inputs_map[i]`/`outputs_map[i]` data anchor on `new_node`.
  168. /// If `inputs_map[i]`/`outputs_map[i]` < 0 or the index not contained in
  169. /// `inputs_map[i]`/`outputs_map[i]`, the `i` data anchor will remain
  170. /// on `old_node`.
  171. /// @param new_node
  172. /// @param old_node
  173. /// @param inputs_map
  174. /// @param outputs_map
  175. /// @return
  176. ///
  177. static graphStatus ReplaceNodeDataAnchors(const NodePtr &new_node, const NodePtr &old_node,
  178. std::initializer_list<int> inputs_map,
  179. std::initializer_list<int> outputs_map);
  180. static graphStatus ReplaceNodeDataAnchors(const NodePtr &new_node, const NodePtr &old_node,
  181. const std::vector<int> &inputs_map, const std::vector<int> &outputs_map);
  182. ///
  183. /// Copy all in-control edges from `src_node` to `dst_node`
  184. /// @param src_node
  185. /// @param dst_node
  186. /// @return
  187. ///
  188. static graphStatus CopyInCtrlEdges(const NodePtr &src_node, NodePtr &dst_node);
  189. static graphStatus MoveInCtrlEdges(const NodePtr &src_node, NodePtr &dst_node);
  190. ///
  191. /// Copy all out-control edges from `src_node` to `dst_node`
  192. /// @param src_node
  193. /// @param dst_node
  194. /// @return success: GRAPH_SUCESS
  195. ///
  196. static graphStatus CopyOutCtrlEdges(const NodePtr &src_node, NodePtr &dst_node);
  197. ///
  198. /// Move all out-control edges from `src_node` to `dst_node`
  199. /// @param src_node
  200. /// @param dst_node
  201. /// @return success: GRAPH_SUCESS
  202. ///
  203. static graphStatus MoveOutCtrlEdges(NodePtr &src_node, NodePtr &dst_node);
  204. };
  205. } // namespace ge
  206. #endif // INC_GRAPH_UTILS_GRAPH_UTILS_H_

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

Contributors (1)