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 32 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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 <list>
  21. #include <map>
  22. #include <string>
  23. #include <unordered_map>
  24. #include <vector>
  25. #include "graph/anchor.h"
  26. #include "graph/compute_graph.h"
  27. #include "graph/graph.h"
  28. #include "graph/model.h"
  29. #include "graph/node.h"
  30. #include "graph/utils/anchor_utils.h"
  31. #define GE_DUMP(compute_graph, name) \
  32. do { \
  33. GraphUtils::DumpGEGraph(compute_graph, name); \
  34. GraphUtils::DumpGEGraphToOnnx(*compute_graph, name); \
  35. uint64_t i = 0; \
  36. for (const auto &sub_graph_func : compute_graph->GetAllSubgraphs()) { \
  37. auto sub_graph_func_name = std::string(name) + std::string("_sub_graph_") + std::to_string(i++); \
  38. GraphUtils::DumpGEGraph(sub_graph_func, sub_graph_func_name); \
  39. GraphUtils::DumpGEGraphToOnnx(*sub_graph_func, sub_graph_func_name); \
  40. } \
  41. } while (0)
  42. #define REFER_ATTR_VALUE(VT_ENUM, DataType, attr, ret) \
  43. do { \
  44. DataType ret; \
  45. attr.GetValue<DataType>(ret); \
  46. } while (0)
  47. #define PRINT_ATTR_VALUE_IF(value_type, VT_ENUM, DataType, attr, stream) \
  48. do { \
  49. if (value_type == VT_ENUM) { \
  50. REFER_ATTR_VALUE(VT_ENUM, DataType, attr, ret) \
  51. stream << ret; \
  52. } \
  53. } while (0)
  54. #define PRINT_LIST_ATTR_VALUE_IF(value_type, VT_ENUM, DataType, attr, stream) \
  55. do { \
  56. if (value_type == VT_ENUM) { \
  57. REFER_ATTR_VALUE(VT_ENUM, DataType, attr, ret) \
  58. stream << "["; \
  59. for (int i = 0; i < ret.size(); i++) { \
  60. stream << ret[i]; \
  61. if (i + 1 != ret.size()) stream << ", "; \
  62. } \
  63. stream << "]"; \
  64. } \
  65. } while (0)
  66. #define PRINT_ATTR_VALUE_ELIF(value_type, VT_ENUM, DataType, attr, stream) \
  67. else PRINT_ATTR_VALUE_IF(value_type, VT_ENUM, DataType, attr, stream)
  68. #define PRINT_LIST_ATTR_VALUE_ELIF(value_type, VT_ENUM, DataType, attr, stream) \
  69. else PRINT_LIST_ATTR_VALUE_IF(value_type, VT_ENUM, DataType, attr, stream)
  70. #define PRINT_SHAPE(i_o, n, idx, stream) \
  71. do { \
  72. auto op = n->GetOpDesc(); \
  73. GeTensorDesc td = i_o == "input" ? op->GetInputDesc(idx) : op->GetOutputDesc(idx); \
  74. auto shape = td.GetShape().GetDims(); \
  75. stream << "["; \
  76. for (int i = 0; i < shape.size(); i++) { \
  77. stream << shape[i]; \
  78. if (i + 1 < shape.size()) stream << ", "; \
  79. } \
  80. stream << "]"; \
  81. } while (0)
  82. #define PRINT_ATTR_FUNC(stream) \
  83. [&](GeAttrValue attr) { \
  84. auto type = attr.GetValueType(); \
  85. PRINT_ATTR_VALUE_IF(type, GeAttrValue::ValueType::VT_STRING, GeAttrValue::STR, attr, stream) \
  86. PRINT_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_FLOAT, GeAttrValue::FLOAT, attr, stream) \
  87. PRINT_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_BOOL, GeAttrValue::BOOL, attr, stream) \
  88. PRINT_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_INT, GeAttrValue::INT, attr, stream) \
  89. PRINT_LIST_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_LIST_STRING, GeAttrValue::LIST_STR, attr, stream) \
  90. PRINT_LIST_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_LIST_FLOAT, GeAttrValue::LIST_FLOAT, attr, stream) \
  91. PRINT_LIST_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_LIST_BOOL, GeAttrValue::LIST_BOOL, attr, stream) \
  92. PRINT_LIST_ATTR_VALUE_ELIF(type, GeAttrValue::ValueType::VT_LIST_INT, GeAttrValue::LIST_INT, attr, stream) \
  93. else if (type == GeAttrValue::ValueType::VT_TENSOR_DESC) stream << "TENSOR_DESC"; \
  94. else if (type == GeAttrValue::ValueType::VT_TENSOR) stream << "TENSOR"; \
  95. else if (type == GeAttrValue::ValueType::VT_BYTES) stream << "BYTES"; \
  96. else if (type == GeAttrValue::ValueType::VT_LIST_TENSOR_DESC) stream << "LIST_TENSOR_DESC"; \
  97. else if (type == GeAttrValue::ValueType::VT_LIST_TENSOR) stream << "LIST_TENSOR"; \
  98. else if (type == GeAttrValue::ValueType::VT_LIST_BYTES) stream << "LIST_BYTES"; \
  99. };
  100. namespace ge {
  101. enum IOType { kIn, kOut };
  102. struct NodeIndexIO {
  103. NodeIndexIO(ge::NodePtr node, uint32_t index, IOType io_type)
  104. : node_(std::move(node)), index_(index), io_type_(io_type) {
  105. if (node_ != nullptr) {
  106. value_ = node_->GetName() + (io_type_ == kOut ? "_out_" : "_in_") + std::to_string(index_);
  107. }
  108. }
  109. NodeIndexIO(ge::NodePtr node, int index, IOType io_type)
  110. : node_(std::move(node)), index_(static_cast<uint32_t>(index)), io_type_(io_type) {
  111. if (node_ != nullptr) {
  112. value_ = node_->GetName() + (io_type_ == kOut ? "_out_" : "_in_") + std::to_string(index_);
  113. }
  114. }
  115. ~NodeIndexIO() {}
  116. NodePtr node_ = nullptr;
  117. uint32_t index_ = 0;
  118. IOType io_type_ = kOut;
  119. std::string value_;
  120. const std::string &ToString() const { return value_; }
  121. };
  122. class GraphUtils {
  123. public:
  124. static ComputeGraphPtr GetComputeGraph(const Graph &graph);
  125. static Graph CreateGraphFromComputeGraph(const ComputeGraphPtr compute_graph);
  126. static graphStatus RecoverGraphOperators(const Graph &graph);
  127. static ComputeGraphPtr CreateGraphFromOperator(const string &name, const std::vector<Operator> &inputs);
  128. static graphStatus AddEdge(const OutDataAnchorPtr &src, const InDataAnchorPtr &dst);
  129. static graphStatus AddEdge(const OutDataAnchorPtr &src, const Format &src_format, const InDataAnchorPtr &dst,
  130. const Format &dst_format);
  131. static graphStatus AddEdge(const AnchorPtr &src, const AnchorPtr &dst);
  132. static graphStatus AddEdge(const OutControlAnchorPtr &src, const InControlAnchorPtr &dst);
  133. static graphStatus AddEdge(const OutDataAnchorPtr &src, const InControlAnchorPtr &dst);
  134. // check whether src is link to dst and then remove
  135. static graphStatus RemoveEdge(const OutDataAnchorPtr &src, const InDataAnchorPtr &dst);
  136. static graphStatus RemoveEdge(const AnchorPtr &src, const AnchorPtr &dst);
  137. static graphStatus RemoveEdge(const OutControlAnchorPtr &src, const InControlAnchorPtr &dst);
  138. static graphStatus RemoveEdge(const OutDataAnchorPtr &src, const InControlAnchorPtr &dst);
  139. static graphStatus ReplaceEdgeDst(const OutDataAnchorPtr &src, const InDataAnchorPtr &dst,
  140. const InDataAnchorPtr &new_dst);
  141. static graphStatus ReplaceEdgeDst(const OutControlAnchorPtr &src, const InControlAnchorPtr &dst,
  142. const InControlAnchorPtr &new_dst);
  143. static graphStatus InsertNodeBetweenDataAnchors(const OutDataAnchorPtr &src, const InDataAnchorPtr &dst,
  144. const NodePtr &new_node);
  145. static graphStatus RemoveSubgraphRecursively(const ComputeGraphPtr &compute_graph, const NodePtr &remove_node);
  146. static graphStatus RemoveNodeWithoutRelink(const ComputeGraphPtr &compute_graph, const NodePtr &node);
  147. static graphStatus InsertTransNode(ComputeGraphPtr compute_graph, const InDataAnchorPtr &in_data_anchor,
  148. const std::vector<OpDescPtr> &vec_op_desc);
  149. ///
  150. /// @brief Insert node: src->insert_node:input_index, insert_node:output_index->dst
  151. /// @param [in] src
  152. /// @param [in] dsts
  153. /// @param [in] insert_node
  154. /// @param [in] input_index
  155. /// @param [in] output_index
  156. /// @return graphStatus
  157. ///
  158. static graphStatus InsertNodeAfter(const OutDataAnchorPtr &src, const std::vector<InDataAnchorPtr> &dsts,
  159. const NodePtr &insert_node, uint32_t input_index = 0, uint32_t output_index = 0);
  160. static graphStatus RemoveJustNode(ComputeGraphPtr compute_graph, const NodePtr &node);
  161. static graphStatus RemoveJustNode(ComputeGraph &compute_graph, const NodePtr &node);
  162. static void RecordOriginalNames(std::vector<ge::NodePtr> original_nodes, const ge::NodePtr &node);
  163. static void RecordOriginalNames(std::vector<std::string> names_tmp, const ge::NodePtr &node);
  164. static bool MatchDumpStr(const std::string &suffix);
  165. static void DumpGEGraph(const ge::ComputeGraphPtr &graph, const std::string &suffix, bool is_always_dump = false,
  166. const std::string &user_graph_name = "");
  167. static void DumpGEGrph(const ge::ComputeGraphPtr &graph, const std::string &path, const std::string &suffix);
  168. static bool LoadGEGraph(const char *file, ge::ComputeGraph &compute_graph);
  169. static bool LoadGEGraph(const char *file, ge::ComputeGraphPtr &compute_graph);
  170. static void BreakConnect(const std::map<OperatorImplPtr, NodePtr> &all_nodes_infos);
  171. static void DumpGEGraphToOnnx(const ge::ComputeGraph &compute_graph, const std::string &suffix);
  172. static void DumpGrphToOnnx(const ge::ComputeGraph &compute_graph, const std::string &path, const std::string &suffix);
  173. static bool LoadGEGraphFromOnnx(const char *file, ge::ComputeGraph &compute_graph);
  174. static bool ReadProtoFromTextFile(const char *file, google::protobuf::Message *message);
  175. static void WriteProtoToTextFile(const google::protobuf::Message &proto, const char *real_path);
  176. static graphStatus AppendInputNode(const ComputeGraphPtr &graph, const NodePtr &node);
  177. ///
  178. /// Isolating `node`, relinking data links from the in-anchor peer nodes to
  179. /// the out-anchor peer nodes according to `io_map`, relinking control links
  180. /// to ensure that input nodes of `node` are before out nodes
  181. ///
  182. /// Link the `io_map[i]` input anchor peer node to `i` output anchor peer
  183. /// nodes, then unlink all links connecting with `node`. If `io_map[i]` < 0,
  184. /// unlink all links from `i` output anchor without any relinking.
  185. ///
  186. /// @param node
  187. /// @param io_map
  188. /// @return
  189. ///
  190. static graphStatus IsolateNode(const NodePtr &node, const std::initializer_list<int> &io_map);
  191. static graphStatus IsolateNode(const NodePtr &node, const std::vector<int> &io_map);
  192. ///
  193. /// Isolate `node` which must be one input one output, equivalent to
  194. /// `IsolateNode(node, {0})`
  195. /// @param node
  196. /// @return
  197. ///
  198. static graphStatus IsolateNodeOneIO(const NodePtr &node);
  199. ///
  200. /// The data anchors replacing behavior is the same with
  201. /// `ReplaceNodeDataAnchors`. In addition, replace all `old_node` control
  202. /// anchors with `new_node`'s.
  203. /// @param new_node
  204. /// @param old_node
  205. /// @param inputs_map
  206. /// @param outputs_map
  207. /// @return
  208. ///
  209. static graphStatus ReplaceNodeAnchors(const NodePtr &new_node, const NodePtr &old_node,
  210. std::initializer_list<int> inputs_map, std::initializer_list<int> outputs_map);
  211. static graphStatus ReplaceNodeAnchors(const NodePtr &new_node, const NodePtr &old_node,
  212. const std::vector<int> &inputs_map, const std::vector<int> &outputs_map);
  213. ///
  214. /// Replace `old_node` data anchors with `new_node`'s according to `inputs_map` and `outputs_map`.
  215. /// Replace the `i` in/out data anchor on `old_node` with
  216. /// `inputs_map[i]`/`outputs_map[i]` data anchor on `new_node`.
  217. /// If `inputs_map[i]`/`outputs_map[i]` < 0 or the index not contained in
  218. /// `inputs_map[i]`/`outputs_map[i]`, the `i` data anchor will remain
  219. /// on `old_node`.
  220. /// @param new_node
  221. /// @param old_node
  222. /// @param inputs_map
  223. /// @param outputs_map
  224. /// @return
  225. ///
  226. static graphStatus ReplaceNodeDataAnchors(const NodePtr &new_node, const NodePtr &old_node,
  227. std::initializer_list<int> inputs_map,
  228. std::initializer_list<int> outputs_map);
  229. static graphStatus ReplaceNodeDataAnchors(const NodePtr &new_node, const NodePtr &old_node,
  230. const std::vector<int> &inputs_map, const std::vector<int> &outputs_map);
  231. ///
  232. /// Copy all in-control edges from `src_node` to `dst_node`
  233. /// @param src_node
  234. /// @param dst_node
  235. /// @return
  236. ///
  237. static graphStatus CopyInCtrlEdges(const NodePtr &src_node, NodePtr &dst_node);
  238. static graphStatus MoveInCtrlEdges(const NodePtr &src_node, NodePtr &dst_node);
  239. ///
  240. /// Copy all out-control edges from `src_node` to `dst_node`
  241. /// @param src_node
  242. /// @param dst_node
  243. /// @return success: GRAPH_SUCESS
  244. ///
  245. static graphStatus CopyOutCtrlEdges(const NodePtr &src_node, NodePtr &dst_node);
  246. ///
  247. /// Move all out-control edges from `src_node` to `dst_node`
  248. /// @param src_node
  249. /// @param dst_node
  250. /// @return success: GRAPH_SUCESS
  251. ///
  252. static graphStatus MoveOutCtrlEdges(NodePtr &src_node, NodePtr &dst_node);
  253. ///
  254. /// Copy all in-data edges from `src_node` to `dst_node`
  255. /// @param src_node
  256. /// @param dst_node
  257. /// @return
  258. ///
  259. static graphStatus CopyInDataEdges(const NodePtr &src_node, NodePtr &dst_node);
  260. static ComputeGraphPtr FindRootGraph(ComputeGraphPtr graph);
  261. ///
  262. /// Make a copy of ComputeGraph.
  263. /// @param graph: original graph.
  264. /// @param prefix: node name prefix of new graph.
  265. /// @return ComputeGraphPtr
  266. ///
  267. static ComputeGraphPtr CloneGraph(const ComputeGraphPtr &graph, const string &prefix,
  268. std::vector<NodePtr> &input_nodes, std::vector<NodePtr> &output_nodes);
  269. ///
  270. /// Copy tensor attribute to new node.
  271. /// @param [in] dst_desc: cloned node.
  272. /// @param [in] src_node: original node.
  273. /// @return success: GRAPH_SUCESS
  274. ///
  275. static graphStatus CopyTensorAttrs(const OpDescPtr &dst_desc, const NodePtr &src_node);
  276. static graphStatus TopologicalSortingByName(const ge::ComputeGraphPtr &compute_graph, vector<NodePtr> &node_vec);
  277. ///
  278. /// Get reference-mapping of all data_anchors in graph
  279. /// @param [in] graph
  280. /// @param [out] symbol_to_anchors
  281. /// @param [out] anchor_to_symbol
  282. /// @return success: GRAPH_SUCESS
  283. ///
  284. static graphStatus GetRefMapping(const ComputeGraphPtr &graph,
  285. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  286. std::map<std::string, std::string> &anchor_to_symbol);
  287. ///
  288. /// Determine if the graph is a UNKNOWN_SHAPE graph based on whether the graph and all subgraphs
  289. /// of the graph have UNKNOWN_SHAPE operators or not.
  290. /// Note: This function will only look 'down' from the graph, not 'up'. For example, the following
  291. /// scenario (K for known shape, U for unknown shape), ROOT graph is UNKNOWN_SHAPE while SUB graph is KNOWN_SHAPE
  292. /// ROOT graph: A -----> B -----> C
  293. /// K subgraph U
  294. /// |
  295. /// V
  296. /// SUB graph: D --> E --> F
  297. /// K K K
  298. /// @param [in] graph
  299. /// @return bool
  300. ///
  301. static bool IsUnknownShapeGraph(const ComputeGraphPtr &graph);
  302. static NodePtr FindNodeFromAllNodes(ComputeGraphPtr &graph, const std::string &name);
  303. private:
  304. ///
  305. /// Get reference-mapping for in_data_anchors of node
  306. /// @param [in] node
  307. /// @param [out] symbol_to_anchors
  308. /// @param [out] anchor_to_symbol
  309. /// @return success: GRAPH_SUCESS
  310. ///
  311. static graphStatus HandleInAnchorMapping(const NodePtr &node,
  312. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  313. std::map<std::string, std::string> &anchor_to_symbol);
  314. ///
  315. /// Get reference-mapping for out_data_anchors of node
  316. /// @param [in] node
  317. /// @param [out] symbol_to_anchors
  318. /// @param [out] anchor_to_symbol
  319. /// @return success: GRAPH_SUCESS
  320. ///
  321. static graphStatus HandleOutAnchorMapping(const NodePtr &node,
  322. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  323. std::map<std::string, std::string> &anchor_to_symbol);
  324. ///
  325. /// Handle input of subgraph
  326. /// @param [in] node
  327. /// @param [out] symbol_to_anchors
  328. /// @param [out] anchor_to_symbol
  329. /// @return success: GRAPH_SUCESS
  330. ///
  331. static graphStatus HandleSubgraphInput(const NodePtr &node,
  332. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  333. std::map<std::string, std::string> &anchor_to_symbol);
  334. ///
  335. /// Handle input of Merge op
  336. /// @param [in] node
  337. /// @param [out] symbol_to_anchors
  338. /// @param [out] anchor_to_symbol
  339. /// @return success: GRAPH_SUCESS
  340. ///
  341. static graphStatus HandleMergeInput(const NodePtr &node,
  342. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  343. std::map<std::string, std::string> &anchor_to_symbol);
  344. ///
  345. /// Handle output of subgraph
  346. /// @param [in] node
  347. /// @param [out] symbol_to_anchors
  348. /// @param [out] anchor_to_symbol
  349. /// @return success: GRAPH_SUCESS
  350. ///
  351. static graphStatus HandleSubgraphOutput(const NodePtr &node,
  352. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  353. std::map<std::string, std::string> &anchor_to_symbol);
  354. ///
  355. /// Relink all edges for cloned ComputeGraph.
  356. /// @param [in] node: original node.
  357. /// @param [in] prefix: node name prefix of new node.
  358. /// @param [in] all_nodes: all nodes in new graph.
  359. /// @return success: GRAPH_SUCESS
  360. ///
  361. static graphStatus RelinkGraphEdges(const NodePtr &node, const string &prefix,
  362. const std::unordered_map<string, NodePtr> &all_nodes);
  363. ///
  364. /// Union ref-mapping
  365. /// @param [in] exist_node_info1
  366. /// @param [in] exist_node_info2
  367. /// @param [out] symbol_to_anchors
  368. /// @param [out] anchor_to_symbol
  369. /// @param [out] symbol
  370. /// @return success: GRAPH_SUCESS
  371. ///
  372. static graphStatus UnionSymbolMapping(const NodeIndexIO &exist_node_info1, const NodeIndexIO &exist_node_info2,
  373. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  374. std::map<std::string, std::string> &anchor_to_symbol, std::string &symbol);
  375. ///
  376. /// Update symbol mapping with a new reference pair
  377. /// @param [in] cur_node_info
  378. /// @param [in] exist_node_info
  379. /// @param [out] symbol_to_anchors
  380. /// @param [out] anchor_to_symbol
  381. /// @return success: GRAPH_SUCESS
  382. ///
  383. static graphStatus UpdateRefMapping(const NodeIndexIO &cur_node_info, const NodeIndexIO &exist_node_info,
  384. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  385. std::map<std::string, std::string> &anchor_to_symbol);
  386. ///
  387. /// Check if out_data_anchor is reference of input
  388. /// @param [in] out_data_anchor
  389. /// @param [out] reuse_in_index
  390. /// @return bool
  391. ///
  392. static bool IsRefFromInput(const OutDataAnchorPtr &out_data_anchor, int32_t &reuse_in_index);
  393. };
  394. class ComputeGraphBuilder {
  395. public:
  396. ComputeGraphBuilder() : owner_graph_(nullptr) {}
  397. ComputeGraphBuilder(const ComputeGraphBuilder &) = delete;
  398. ComputeGraphBuilder &operator=(const ComputeGraphBuilder &) = delete;
  399. ComputeGraphBuilder(const ComputeGraphBuilder &&) = delete;
  400. ComputeGraphBuilder &operator=(const ComputeGraphBuilder &&) = delete;
  401. ~ComputeGraphBuilder() = default;
  402. ///
  403. /// @brief Add node to graph
  404. /// @param [in] op_desc
  405. /// @return ComputeGraphBuilder
  406. ///
  407. virtual ComputeGraphBuilder &AddNode(const OpDescPtr &op_desc);
  408. ///
  409. /// @brief Add data-link among nodes in graph
  410. /// @param [in] src_name
  411. /// @param [in] out_anchor_ind
  412. /// @param [in] dst_name
  413. /// @param [in] in_anchor_ind
  414. /// @return ComputeGraphBuilder
  415. ///
  416. virtual ComputeGraphBuilder &AddDataLink(const std::string &src_name, uint32_t out_anchor_ind,
  417. const std::string &dst_name, uint32_t in_anchor_ind);
  418. ///
  419. /// @brief Add ctrl-link among nodes in graph
  420. /// @param [in] src_name
  421. /// @param [in] dst_name
  422. /// @return ComputeGraphBuilder
  423. ///
  424. virtual ComputeGraphBuilder &AddControlLink(const std::string &src_name, const std::string &dst_name);
  425. ///
  426. /// @brief Build graph
  427. /// @param [out] error_code
  428. /// @param [out] error_msg
  429. /// @return ComputeGraphPtr
  430. ///
  431. virtual ComputeGraphPtr Build(graphStatus &error_code, std::string &error_msg) = 0;
  432. /// @brief Get node with name
  433. /// @param [in] name
  434. /// @return NodePtr
  435. ///
  436. NodePtr GetNode(const std::string &name);
  437. /// @brief Get all nodes
  438. /// @return std::vector<NodePtr>
  439. ///
  440. std::vector<NodePtr> GetAllNodes();
  441. protected:
  442. ///
  443. /// @brief Build nodes
  444. /// @param [out] error_code
  445. /// @param [out] error_msg
  446. /// @return void
  447. ///
  448. void BuildNodes(graphStatus &error_code, std::string &error_msg);
  449. ///
  450. /// @brief Build data-links
  451. /// @param [out] error_code
  452. /// @param [out] error_msg
  453. /// @return void
  454. ///
  455. void BuildDataLinks(graphStatus &error_code, std::string &error_msg);
  456. ///
  457. /// @brief Build ctrl-links
  458. /// @param [out] error_code
  459. /// @param [out] error_msg
  460. /// @return void
  461. ///
  462. void BuildCtrlLinks(graphStatus &error_code, std::string &error_msg);
  463. ComputeGraphPtr owner_graph_;
  464. // node_name -> node
  465. std::map<std::string, NodePtr> node_names_;
  466. std::vector<OpDescPtr> nodes_;
  467. // <src_node_name, out_anchor_ind> -> <dst_node_name, in_anchor_ind>
  468. std::vector<std::pair<std::pair<std::string, uint32_t>, std::pair<std::string, uint32_t>>> data_links_;
  469. // src_node_name -> dst_node_name
  470. std::vector<std::pair<std::string, std::string>> ctrl_links_;
  471. };
  472. class CompleteGraphBuilder : public ComputeGraphBuilder {
  473. public:
  474. explicit CompleteGraphBuilder(std::string name, bool retval_flag = true)
  475. : name_(std::move(name)), parent_node_(nullptr), retval_flag_(retval_flag) {}
  476. CompleteGraphBuilder(const CompleteGraphBuilder &) = delete;
  477. CompleteGraphBuilder &operator=(const CompleteGraphBuilder &) = delete;
  478. CompleteGraphBuilder(const CompleteGraphBuilder &&) = delete;
  479. CompleteGraphBuilder &operator=(const CompleteGraphBuilder &&) = delete;
  480. ~CompleteGraphBuilder() = default;
  481. ///
  482. /// @brief Add node to graph
  483. /// @param [in] op_desc
  484. /// @return CompleteGraphBuilder
  485. ///
  486. CompleteGraphBuilder &AddNode(const OpDescPtr &op_desc) override;
  487. ///
  488. /// @brief Add data-link among nodes in graph
  489. /// @param [in] src_name
  490. /// @param [in] out_anchor_ind
  491. /// @param [in] dst_name
  492. /// @param [in] in_anchor_ind
  493. /// @return CompleteGraphBuilder
  494. ///
  495. CompleteGraphBuilder &AddDataLink(const std::string &src_name, uint32_t out_anchor_ind, const std::string &dst_name,
  496. uint32_t in_anchor_ind) override;
  497. ///
  498. /// @brief Add ctrl-link among nodes in graph
  499. /// @param [in] src_name
  500. /// @param [in] dst_name
  501. /// @return CompleteGraphBuilder
  502. ///
  503. CompleteGraphBuilder &AddControlLink(const std::string &src_name, const std::string &dst_name) override;
  504. ///
  505. /// @brief Set index_th input anchor for graph
  506. /// @param [in] index
  507. /// @param [in] node_names
  508. /// @param [in] anchor_inds
  509. /// @return CompleteGraphBuilder
  510. ///
  511. CompleteGraphBuilder &SetInput(uint32_t index, const std::vector<std::string> &node_names,
  512. const std::vector<uint32_t> &anchor_inds);
  513. ///
  514. /// @brief Set index_th input of graph as useless
  515. /// @param [in] index
  516. /// @return CompleteGraphBuilder
  517. ///
  518. CompleteGraphBuilder &SetUselessInput(uint32_t index);
  519. ///
  520. /// @brief Add output anchor for graph
  521. /// @param [in] owner_node_name
  522. /// @param [in] anchor_ind
  523. /// @return CompleteGraphBuilder
  524. ///
  525. CompleteGraphBuilder &AddOutput(const std::string &owner_node_name, uint32_t anchor_ind);
  526. ///
  527. /// @brief Add target for graph
  528. /// @param [in] target_name
  529. /// @return CompleteGraphBuilder
  530. ///
  531. CompleteGraphBuilder &AddTarget(const std::string &target_name);
  532. ///
  533. /// @brief Set parent-node of graph
  534. /// @param [in] parent_node
  535. /// @return CompleteGraphBuilder
  536. ///
  537. CompleteGraphBuilder &SetParentNode(const NodePtr &parent_node);
  538. ///
  539. /// @brief Set mapping-relation of parent-node in_anchor_ind & Data-node
  540. /// @param [in] input_mapping: index_of_graph_input -> in_anchor_index_of_parent_node
  541. /// @return CompleteGraphBuilder
  542. ///
  543. CompleteGraphBuilder &SetInputMapping(const std::map<uint32_t, uint32_t> &input_mapping);
  544. ///
  545. /// @brief Set mapping-relation of parent-node out_anchor_ind & NetOutput-node out_anchor_ind
  546. /// @param [in] output_mapping: index_of_graph_output -> out_anchor_index_of_parent_node
  547. /// @return CompleteGraphBuilder
  548. ///
  549. CompleteGraphBuilder &SetOutputMapping(const std::map<uint32_t, uint32_t> &output_mapping);
  550. ///
  551. /// @brief Build graph
  552. /// @param [out] error_code
  553. /// @param [out] error_msg
  554. /// @return ComputeGraphPtr
  555. ///
  556. ComputeGraphPtr Build(graphStatus &error_code, std::string &error_msg) override;
  557. private:
  558. ///
  559. /// @brief Add data nodes
  560. /// @param [out] error_code
  561. /// @param [out] error_msg
  562. /// @return void
  563. ///
  564. void AddDataNodes(graphStatus &error_code, std::string &error_msg);
  565. ///
  566. /// @brief Add data node
  567. /// @param [in] index
  568. /// @param [out] error_code
  569. /// @param [out] error_msg
  570. /// @return void
  571. ///
  572. NodePtr AddDataNode(uint32_t index, graphStatus &error_code, std::string &error_msg);
  573. ///
  574. /// @brief Add RetVal nodes
  575. /// @param [out] error_code
  576. /// @param [out] error_msg
  577. /// @return void
  578. ///
  579. void AddRetValNodes(graphStatus &error_code, std::string &error_msg);
  580. ///
  581. /// @brief Build target-nodes for graph
  582. /// @param [out] error_code
  583. /// @param [out] error_msg
  584. /// @return void
  585. ///
  586. void BuildGraphTargets(graphStatus &error_code, std::string &error_msg);
  587. ///
  588. /// @brief Add NetOutput node
  589. /// @param [out] error_code
  590. /// @param [out] error_msg
  591. /// @return void
  592. ///
  593. void AddNetOutputNode(graphStatus &error_code, std::string &error_msg);
  594. ///
  595. /// @brief Build NetOutput nodes with data & ctrl edges
  596. /// @param [in] net_output_desc
  597. /// @param [in] peer_out_anchors
  598. /// @param [out] error_code
  599. /// @param [out] error_msg
  600. /// @return void
  601. ///
  602. void BuildNetOutputNodeWithLink(const OpDescPtr &net_output_desc,
  603. const std::vector<OutDataAnchorPtr> &peer_out_anchors, graphStatus &error_code,
  604. std::string &error_msg);
  605. ///
  606. /// @brief process after build
  607. /// @param [out] error_code
  608. /// @param [out] error_msg
  609. /// @return void
  610. ///
  611. void PostProcess(graphStatus &error_code, std::string &error_msg);
  612. std::string name_;
  613. NodePtr parent_node_;
  614. bool retval_flag_;
  615. std::map<uint32_t, std::pair<std::vector<std::string>, std::vector<uint32_t>>> graph_inputs_;
  616. std::vector<std::pair<std::string, uint32_t>> graph_outputs_;
  617. std::vector<std::string> graph_targets_;
  618. // index_of_graph_input -> in_anchor_index_of_parent_node
  619. std::map<uint32_t, uint32_t> input_mapping_;
  620. // index_of_graph_output -> out_anchor_index_of_parent_node
  621. std::map<uint32_t, uint32_t> output_mapping_;
  622. };
  623. class PartialGraphBuilder : public ComputeGraphBuilder {
  624. public:
  625. PartialGraphBuilder() = default;
  626. PartialGraphBuilder(const PartialGraphBuilder &) = delete;
  627. PartialGraphBuilder &operator=(const PartialGraphBuilder &) = delete;
  628. PartialGraphBuilder(const PartialGraphBuilder &&) = delete;
  629. PartialGraphBuilder &operator=(const PartialGraphBuilder &&) = delete;
  630. ~PartialGraphBuilder() = default;
  631. ///
  632. /// @brief Add node to graph
  633. /// @param [in] op_desc
  634. /// @return PartialGraphBuilder
  635. ///
  636. PartialGraphBuilder &AddNode(const OpDescPtr &op_desc) override;
  637. ///
  638. /// @brief Add data-link among nodes in graph
  639. /// @param [in] src_name
  640. /// @param [in] out_anchor_ind
  641. /// @param [in] dst_name
  642. /// @param [in] in_anchor_ind
  643. /// @return PartialGraphBuilder
  644. ///
  645. PartialGraphBuilder &AddDataLink(const std::string &src_name, uint32_t out_anchor_ind, const std::string &dst_name,
  646. uint32_t in_anchor_ind) override;
  647. ///
  648. /// @brief Add ctrl-link among nodes in graph
  649. /// @param [in] src_name
  650. /// @param [in] dst_name
  651. /// @return PartialGraphBuilder
  652. ///
  653. PartialGraphBuilder &AddControlLink(const std::string &src_name, const std::string &dst_name) override;
  654. ///
  655. /// @brief Set owner graph
  656. /// @param [in] graph
  657. /// @return PartialGraphBuilder
  658. ///
  659. PartialGraphBuilder &SetOwnerGraph(const ComputeGraphPtr &graph);
  660. ///
  661. /// @brief Add exist node
  662. /// @param [in] node
  663. /// @return PartialGraphBuilder
  664. ///
  665. PartialGraphBuilder &AddExistNode(const NodePtr &node);
  666. ///
  667. /// @brief Build multi nodes with links
  668. /// @param [out] error_code
  669. /// @param [out] error_msg
  670. /// @return ComputeGraphPtr
  671. ///
  672. ComputeGraphPtr Build(graphStatus &error_code, std::string &error_msg) override;
  673. private:
  674. ///
  675. /// @brief Build exist nodes
  676. /// @param [out] error_code
  677. /// @param [out] error_msg
  678. /// @return void
  679. ///
  680. void BuildExistNodes(graphStatus &error_code, std::string &error_msg);
  681. std::vector<NodePtr> exist_nodes_;
  682. };
  683. } // namespace ge
  684. #endif // INC_GRAPH_UTILS_GRAPH_UTILS_H_

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