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 31 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  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 <list>
  24. #include <unordered_map>
  25. #include "graph/anchor.h"
  26. #include "graph/node.h"
  27. #include "graph/compute_graph.h"
  28. #include "graph/utils/anchor_utils.h"
  29. #include "graph/graph.h"
  30. #include "graph/model.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 bool LoadGEGraph(const char *file, ge::ComputeGraph &compute_graph);
  168. static bool LoadGEGraph(const char *file, ge::ComputeGraphPtr &compute_graph);
  169. static void BreakConnect(const std::map<OperatorImplPtr, NodePtr> &all_nodes_infos);
  170. static void DumpGEGraphToOnnx(const ge::ComputeGraph &compute_graph, const std::string &suffix);
  171. static bool LoadGEGraphFromOnnx(const char *file, ge::ComputeGraph &compute_graph);
  172. static bool ReadProtoFromTextFile(const char *file, google::protobuf::Message *message);
  173. static void WriteProtoToTextFile(const google::protobuf::Message &proto, const char *real_path);
  174. static graphStatus AppendInputNode(const ComputeGraphPtr &graph, const NodePtr &node);
  175. ///
  176. /// Isolating `node`, relinking data links from the in-anchor peer nodes to
  177. /// the out-anchor peer nodes according to `io_map`, relinking control links
  178. /// to ensure that input nodes of `node` are before out nodes
  179. ///
  180. /// Link the `io_map[i]` input anchor peer node to `i` output anchor peer
  181. /// nodes, then unlink all links connecting with `node`. If `io_map[i]` < 0,
  182. /// unlink all links from `i` output anchor without any relinking.
  183. ///
  184. /// @param node
  185. /// @param io_map
  186. /// @return
  187. ///
  188. static graphStatus IsolateNode(const NodePtr &node, const std::initializer_list<int> &io_map);
  189. static graphStatus IsolateNode(const NodePtr &node, const std::vector<int> &io_map);
  190. ///
  191. /// Isolate `node` which must be one input one output, equivalent to
  192. /// `IsolateNode(node, {0})`
  193. /// @param node
  194. /// @return
  195. ///
  196. static graphStatus IsolateNodeOneIO(const NodePtr &node);
  197. ///
  198. /// The data anchors replacing behavior is the same with
  199. /// `ReplaceNodeDataAnchors`. In addition, replace all `old_node` control
  200. /// anchors with `new_node`'s.
  201. /// @param new_node
  202. /// @param old_node
  203. /// @param inputs_map
  204. /// @param outputs_map
  205. /// @return
  206. ///
  207. static graphStatus ReplaceNodeAnchors(const NodePtr &new_node, const NodePtr &old_node,
  208. std::initializer_list<int> inputs_map, std::initializer_list<int> outputs_map);
  209. static graphStatus ReplaceNodeAnchors(const NodePtr &new_node, const NodePtr &old_node,
  210. const std::vector<int> &inputs_map, const std::vector<int> &outputs_map);
  211. ///
  212. /// Replace `old_node` data anchors with `new_node`'s according to `inputs_map` and `outputs_map`.
  213. /// Replace the `i` in/out data anchor on `old_node` with
  214. /// `inputs_map[i]`/`outputs_map[i]` data anchor on `new_node`.
  215. /// If `inputs_map[i]`/`outputs_map[i]` < 0 or the index not contained in
  216. /// `inputs_map[i]`/`outputs_map[i]`, the `i` data anchor will remain
  217. /// on `old_node`.
  218. /// @param new_node
  219. /// @param old_node
  220. /// @param inputs_map
  221. /// @param outputs_map
  222. /// @return
  223. ///
  224. static graphStatus ReplaceNodeDataAnchors(const NodePtr &new_node, const NodePtr &old_node,
  225. std::initializer_list<int> inputs_map,
  226. std::initializer_list<int> outputs_map);
  227. static graphStatus ReplaceNodeDataAnchors(const NodePtr &new_node, const NodePtr &old_node,
  228. const std::vector<int> &inputs_map, const std::vector<int> &outputs_map);
  229. ///
  230. /// Copy all in-control edges from `src_node` to `dst_node`
  231. /// @param src_node
  232. /// @param dst_node
  233. /// @return
  234. ///
  235. static graphStatus CopyInCtrlEdges(const NodePtr &src_node, NodePtr &dst_node);
  236. static graphStatus MoveInCtrlEdges(const NodePtr &src_node, NodePtr &dst_node);
  237. ///
  238. /// Copy all out-control edges from `src_node` to `dst_node`
  239. /// @param src_node
  240. /// @param dst_node
  241. /// @return success: GRAPH_SUCESS
  242. ///
  243. static graphStatus CopyOutCtrlEdges(const NodePtr &src_node, NodePtr &dst_node);
  244. ///
  245. /// Move all out-control edges from `src_node` to `dst_node`
  246. /// @param src_node
  247. /// @param dst_node
  248. /// @return success: GRAPH_SUCESS
  249. ///
  250. static graphStatus MoveOutCtrlEdges(NodePtr &src_node, NodePtr &dst_node);
  251. ///
  252. /// Copy all in-data edges from `src_node` to `dst_node`
  253. /// @param src_node
  254. /// @param dst_node
  255. /// @return
  256. ///
  257. static graphStatus CopyInDataEdges(const NodePtr &src_node, NodePtr &dst_node);
  258. static ComputeGraphPtr FindRootGraph(ComputeGraphPtr graph);
  259. ///
  260. /// Make a copy of ComputeGraph.
  261. /// @param graph: original graph.
  262. /// @param prefix: node name prefix of new graph.
  263. /// @return ComputeGraphPtr
  264. ///
  265. static ComputeGraphPtr CloneGraph(const ComputeGraphPtr &graph, const string &prefix,
  266. std::vector<NodePtr> &input_nodes, std::vector<NodePtr> &output_nodes);
  267. ///
  268. /// Copy tensor attribute to new node.
  269. /// @param [in] dst_desc: cloned node.
  270. /// @param [in] src_node: original node.
  271. /// @return success: GRAPH_SUCESS
  272. ///
  273. static graphStatus CopyTensorAttrs(const OpDescPtr &dst_desc, const NodePtr &src_node);
  274. static graphStatus TopologicalSortingByName(const ge::ComputeGraphPtr &compute_graph, vector<NodePtr> &node_vec);
  275. ///
  276. /// Get reference-mapping of all data_anchors in graph
  277. /// @param [in] graph
  278. /// @param [out] symbol_to_anchors
  279. /// @param [out] anchor_to_symbol
  280. /// @return success: GRAPH_SUCESS
  281. ///
  282. static graphStatus GetRefMapping(const ComputeGraphPtr &graph,
  283. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  284. std::map<std::string, std::string> &anchor_to_symbol);
  285. ///
  286. /// Determine if the graph is a UNKNOWN_SHAPE graph based on whether the graph and all subgraphs
  287. /// of the graph have UNKNOWN_SHAPE operators or not.
  288. /// Note: This function will only look 'down' from the graph, not 'up'. For example, the following
  289. /// scenario (K for known shape, U for unknown shape), ROOT graph is UNKNOWN_SHAPE while SUB graph is KNOWN_SHAPE
  290. /// ROOT graph: A -----> B -----> C
  291. /// K subgraph U
  292. /// |
  293. /// V
  294. /// SUB graph: D --> E --> F
  295. /// K K K
  296. /// @param [in] graph
  297. /// @return bool
  298. ///
  299. static bool IsUnknownShapeGraph(const ComputeGraphPtr &graph);
  300. static NodePtr FindNodeFromAllNodes(ComputeGraphPtr &graph, const std::string &name);
  301. private:
  302. ///
  303. /// Get reference-mapping for in_data_anchors of node
  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 HandleInAnchorMapping(const NodePtr &node,
  310. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  311. std::map<std::string, std::string> &anchor_to_symbol);
  312. ///
  313. /// Get reference-mapping for out_data_anchors of node
  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 HandleOutAnchorMapping(const NodePtr &node,
  320. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  321. std::map<std::string, std::string> &anchor_to_symbol);
  322. ///
  323. /// Handle input of subgraph
  324. /// @param [in] node
  325. /// @param [out] symbol_to_anchors
  326. /// @param [out] anchor_to_symbol
  327. /// @return success: GRAPH_SUCESS
  328. ///
  329. static graphStatus HandleSubgraphInput(const NodePtr &node,
  330. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  331. std::map<std::string, std::string> &anchor_to_symbol);
  332. ///
  333. /// Handle input of Merge op
  334. /// @param [in] node
  335. /// @param [out] symbol_to_anchors
  336. /// @param [out] anchor_to_symbol
  337. /// @return success: GRAPH_SUCESS
  338. ///
  339. static graphStatus HandleMergeInput(const NodePtr &node,
  340. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  341. std::map<std::string, std::string> &anchor_to_symbol);
  342. ///
  343. /// Handle output of subgraph
  344. /// @param [in] node
  345. /// @param [out] symbol_to_anchors
  346. /// @param [out] anchor_to_symbol
  347. /// @return success: GRAPH_SUCESS
  348. ///
  349. static graphStatus HandleSubgraphOutput(const NodePtr &node,
  350. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  351. std::map<std::string, std::string> &anchor_to_symbol);
  352. ///
  353. /// Relink all edges for cloned ComputeGraph.
  354. /// @param [in] node: original node.
  355. /// @param [in] prefix: node name prefix of new node.
  356. /// @param [in] all_nodes: all nodes in new graph.
  357. /// @return success: GRAPH_SUCESS
  358. ///
  359. static graphStatus RelinkGraphEdges(const NodePtr &node, const string &prefix,
  360. const std::unordered_map<string, NodePtr> &all_nodes);
  361. ///
  362. /// Union ref-mapping
  363. /// @param [in] exist_node_info1
  364. /// @param [in] exist_node_info2
  365. /// @param [out] symbol_to_anchors
  366. /// @param [out] anchor_to_symbol
  367. /// @param [out] symbol
  368. /// @return success: GRAPH_SUCESS
  369. ///
  370. static graphStatus UnionSymbolMapping(const NodeIndexIO &exist_node_info1, const NodeIndexIO &exist_node_info2,
  371. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  372. std::map<std::string, std::string> &anchor_to_symbol, std::string &symbol);
  373. ///
  374. /// Update symbol mapping with a new reference pair
  375. /// @param [in] cur_node_info
  376. /// @param [in] exist_node_info
  377. /// @param [out] symbol_to_anchors
  378. /// @param [out] anchor_to_symbol
  379. /// @return success: GRAPH_SUCESS
  380. ///
  381. static graphStatus UpdateRefMapping(const NodeIndexIO &cur_node_info, const NodeIndexIO &exist_node_info,
  382. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  383. std::map<std::string, std::string> &anchor_to_symbol);
  384. ///
  385. /// Check if out_data_anchor is reference of input
  386. /// @param [in] out_data_anchor
  387. /// @param [out] reuse_in_index
  388. /// @return bool
  389. ///
  390. static bool IsRefFromInput(const OutDataAnchorPtr &out_data_anchor, int32_t &reuse_in_index);
  391. };
  392. class ComputeGraphBuilder {
  393. public:
  394. ComputeGraphBuilder() : owner_graph_(nullptr) {}
  395. ComputeGraphBuilder(const ComputeGraphBuilder &) = delete;
  396. ComputeGraphBuilder &operator=(const ComputeGraphBuilder &) = delete;
  397. ComputeGraphBuilder(const ComputeGraphBuilder &&) = delete;
  398. ComputeGraphBuilder &operator=(const ComputeGraphBuilder &&) = delete;
  399. ~ComputeGraphBuilder() = default;
  400. ///
  401. /// @brief Add node to graph
  402. /// @param [in] op_desc
  403. /// @return ComputeGraphBuilder
  404. ///
  405. virtual ComputeGraphBuilder &AddNode(const OpDescPtr &op_desc);
  406. ///
  407. /// @brief Add data-link among nodes in graph
  408. /// @param [in] src_name
  409. /// @param [in] out_anchor_ind
  410. /// @param [in] dst_name
  411. /// @param [in] in_anchor_ind
  412. /// @return ComputeGraphBuilder
  413. ///
  414. virtual ComputeGraphBuilder &AddDataLink(const std::string &src_name, uint32_t out_anchor_ind,
  415. const std::string &dst_name, uint32_t in_anchor_ind);
  416. ///
  417. /// @brief Add ctrl-link among nodes in graph
  418. /// @param [in] src_name
  419. /// @param [in] dst_name
  420. /// @return ComputeGraphBuilder
  421. ///
  422. virtual ComputeGraphBuilder &AddControlLink(const std::string &src_name, const std::string &dst_name);
  423. ///
  424. /// @brief Build graph
  425. /// @param [out] error_code
  426. /// @param [out] error_msg
  427. /// @return ComputeGraphPtr
  428. ///
  429. virtual ComputeGraphPtr Build(graphStatus &error_code, std::string &error_msg) = 0;
  430. /// @brief Get node with name
  431. /// @param [in] name
  432. /// @return NodePtr
  433. ///
  434. NodePtr GetNode(const std::string &name);
  435. /// @brief Get all nodes
  436. /// @return std::vector<NodePtr>
  437. ///
  438. std::vector<NodePtr> GetAllNodes();
  439. protected:
  440. ///
  441. /// @brief Build nodes
  442. /// @param [out] error_code
  443. /// @param [out] error_msg
  444. /// @return void
  445. ///
  446. void BuildNodes(graphStatus &error_code, std::string &error_msg);
  447. ///
  448. /// @brief Build data-links
  449. /// @param [out] error_code
  450. /// @param [out] error_msg
  451. /// @return void
  452. ///
  453. void BuildDataLinks(graphStatus &error_code, std::string &error_msg);
  454. ///
  455. /// @brief Build ctrl-links
  456. /// @param [out] error_code
  457. /// @param [out] error_msg
  458. /// @return void
  459. ///
  460. void BuildCtrlLinks(graphStatus &error_code, std::string &error_msg);
  461. ComputeGraphPtr owner_graph_;
  462. // node_name -> node
  463. std::map<std::string, NodePtr> node_names_;
  464. std::vector<OpDescPtr> nodes_;
  465. // <src_node_name, out_anchor_ind> -> <dst_node_name, in_anchor_ind>
  466. std::vector<std::pair<std::pair<std::string, uint32_t>, std::pair<std::string, uint32_t>>> data_links_;
  467. // src_node_name -> dst_node_name
  468. std::vector<std::pair<std::string, std::string>> ctrl_links_;
  469. };
  470. class CompleteGraphBuilder : public ComputeGraphBuilder {
  471. public:
  472. explicit CompleteGraphBuilder(std::string name) : name_(std::move(name)), parent_node_(nullptr) {}
  473. CompleteGraphBuilder(const CompleteGraphBuilder &) = delete;
  474. CompleteGraphBuilder &operator=(const CompleteGraphBuilder &) = delete;
  475. CompleteGraphBuilder(const CompleteGraphBuilder &&) = delete;
  476. CompleteGraphBuilder &operator=(const CompleteGraphBuilder &&) = delete;
  477. ~CompleteGraphBuilder() = default;
  478. ///
  479. /// @brief Add node to graph
  480. /// @param [in] op_desc
  481. /// @return CompleteGraphBuilder
  482. ///
  483. CompleteGraphBuilder &AddNode(const OpDescPtr &op_desc) override;
  484. ///
  485. /// @brief Add data-link among nodes in graph
  486. /// @param [in] src_name
  487. /// @param [in] out_anchor_ind
  488. /// @param [in] dst_name
  489. /// @param [in] in_anchor_ind
  490. /// @return CompleteGraphBuilder
  491. ///
  492. CompleteGraphBuilder &AddDataLink(const std::string &src_name, uint32_t out_anchor_ind, const std::string &dst_name,
  493. uint32_t in_anchor_ind) override;
  494. ///
  495. /// @brief Add ctrl-link among nodes in graph
  496. /// @param [in] src_name
  497. /// @param [in] dst_name
  498. /// @return CompleteGraphBuilder
  499. ///
  500. CompleteGraphBuilder &AddControlLink(const std::string &src_name, const std::string &dst_name) override;
  501. ///
  502. /// @brief Set index_th input anchor for graph
  503. /// @param [in] index
  504. /// @param [in] node_names
  505. /// @param [in] anchor_inds
  506. /// @return CompleteGraphBuilder
  507. ///
  508. CompleteGraphBuilder &SetInput(uint32_t index, const std::vector<std::string> &node_names,
  509. const std::vector<uint32_t> &anchor_inds);
  510. ///
  511. /// @brief Set index_th input of graph as useless
  512. /// @param [in] index
  513. /// @return CompleteGraphBuilder
  514. ///
  515. CompleteGraphBuilder &SetUselessInput(uint32_t index);
  516. ///
  517. /// @brief Add output anchor for graph
  518. /// @param [in] owner_node_name
  519. /// @param [in] anchor_ind
  520. /// @return CompleteGraphBuilder
  521. ///
  522. CompleteGraphBuilder &AddOutput(const std::string &owner_node_name, uint32_t anchor_ind);
  523. ///
  524. /// @brief Add target for graph
  525. /// @param [in] target_name
  526. /// @return CompleteGraphBuilder
  527. ///
  528. CompleteGraphBuilder &AddTarget(const std::string &target_name);
  529. ///
  530. /// @brief Set parent-node of graph
  531. /// @param [in] parent_node
  532. /// @return CompleteGraphBuilder
  533. ///
  534. CompleteGraphBuilder &SetParentNode(const NodePtr &parent_node);
  535. ///
  536. /// @brief Set mapping-relation of parent-node in_anchor_ind & Data-node
  537. /// @param [in] input_mapping: index_of_graph_input -> in_anchor_index_of_parent_node
  538. /// @return CompleteGraphBuilder
  539. ///
  540. CompleteGraphBuilder &SetInputMapping(const std::map<uint32_t, uint32_t> &input_mapping);
  541. ///
  542. /// @brief Set mapping-relation of parent-node out_anchor_ind & NetOutput-node out_anchor_ind
  543. /// @param [in] output_mapping: index_of_graph_output -> out_anchor_index_of_parent_node
  544. /// @return CompleteGraphBuilder
  545. ///
  546. CompleteGraphBuilder &SetOutputMapping(const std::map<uint32_t, uint32_t> &output_mapping);
  547. ///
  548. /// @brief Build graph
  549. /// @param [out] error_code
  550. /// @param [out] error_msg
  551. /// @return ComputeGraphPtr
  552. ///
  553. ComputeGraphPtr Build(graphStatus &error_code, std::string &error_msg) override;
  554. private:
  555. ///
  556. /// @brief Add data nodes
  557. /// @param [out] error_code
  558. /// @param [out] error_msg
  559. /// @return void
  560. ///
  561. void AddDataNodes(graphStatus &error_code, std::string &error_msg);
  562. ///
  563. /// @brief Add data node
  564. /// @param [in] index
  565. /// @param [out] error_code
  566. /// @param [out] error_msg
  567. /// @return void
  568. ///
  569. NodePtr AddDataNode(uint32_t index, graphStatus &error_code, std::string &error_msg);
  570. ///
  571. /// @brief Add RetVal nodes
  572. /// @param [out] error_code
  573. /// @param [out] error_msg
  574. /// @return void
  575. ///
  576. void AddRetValNodes(graphStatus &error_code, std::string &error_msg);
  577. ///
  578. /// @brief Build target-nodes for graph
  579. /// @param [out] error_code
  580. /// @param [out] error_msg
  581. /// @return void
  582. ///
  583. void BuildGraphTargets(graphStatus &error_code, std::string &error_msg);
  584. std::string name_;
  585. NodePtr parent_node_;
  586. std::map<uint32_t, std::pair<std::vector<std::string>, std::vector<uint32_t>>> graph_inputs_;
  587. std::vector<std::pair<std::string, uint32_t>> graph_outputs_;
  588. std::vector<std::string> graph_targets_;
  589. // index_of_graph_input -> in_anchor_index_of_parent_node
  590. std::map<uint32_t, uint32_t> input_mapping_;
  591. // index_of_graph_output -> out_anchor_index_of_parent_node
  592. std::map<uint32_t, uint32_t> output_mapping_;
  593. };
  594. class PartialGraphBuilder : public ComputeGraphBuilder {
  595. public:
  596. PartialGraphBuilder() = default;
  597. PartialGraphBuilder(const PartialGraphBuilder &) = delete;
  598. PartialGraphBuilder &operator=(const PartialGraphBuilder &) = delete;
  599. PartialGraphBuilder(const PartialGraphBuilder &&) = delete;
  600. PartialGraphBuilder &operator=(const PartialGraphBuilder &&) = delete;
  601. ~PartialGraphBuilder() = default;
  602. ///
  603. /// @brief Add node to graph
  604. /// @param [in] op_desc
  605. /// @return PartialGraphBuilder
  606. ///
  607. PartialGraphBuilder &AddNode(const OpDescPtr &op_desc) override;
  608. ///
  609. /// @brief Add data-link among nodes in graph
  610. /// @param [in] src_name
  611. /// @param [in] out_anchor_ind
  612. /// @param [in] dst_name
  613. /// @param [in] in_anchor_ind
  614. /// @return PartialGraphBuilder
  615. ///
  616. PartialGraphBuilder &AddDataLink(const std::string &src_name, uint32_t out_anchor_ind, const std::string &dst_name,
  617. uint32_t in_anchor_ind) override;
  618. ///
  619. /// @brief Add ctrl-link among nodes in graph
  620. /// @param [in] src_name
  621. /// @param [in] dst_name
  622. /// @return PartialGraphBuilder
  623. ///
  624. PartialGraphBuilder &AddControlLink(const std::string &src_name, const std::string &dst_name) override;
  625. ///
  626. /// @brief Set owner graph
  627. /// @param [in] graph
  628. /// @return PartialGraphBuilder
  629. ///
  630. PartialGraphBuilder &SetOwnerGraph(const ComputeGraphPtr &graph);
  631. ///
  632. /// @brief Add exist node
  633. /// @param [in] node
  634. /// @return PartialGraphBuilder
  635. ///
  636. PartialGraphBuilder &AddExistNode(const NodePtr &node);
  637. ///
  638. /// @brief Build multi nodes with links
  639. /// @param [out] error_code
  640. /// @param [out] error_msg
  641. /// @return ComputeGraphPtr
  642. ///
  643. ComputeGraphPtr Build(graphStatus &error_code, std::string &error_msg) override;
  644. private:
  645. ///
  646. /// @brief Build exist nodes
  647. /// @param [out] error_code
  648. /// @param [out] error_msg
  649. /// @return void
  650. ///
  651. void BuildExistNodes(graphStatus &error_code, std::string &error_msg);
  652. std::vector<NodePtr> exist_nodes_;
  653. };
  654. } // namespace ge
  655. #endif // INC_GRAPH_UTILS_GRAPH_UTILS_H_

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