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 29 kB

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

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