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

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