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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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. const 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 InsertNodeAfter(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. ///
  249. /// Copy all in-data edges from `src_node` to `dst_node`
  250. /// @param src_node
  251. /// @param dst_node
  252. /// @return
  253. ///
  254. static graphStatus CopyInDataEdges(const NodePtr &src_node, NodePtr &dst_node);
  255. static ComputeGraphPtr FindRootGraph(ComputeGraphPtr graph);
  256. static graphStatus TopologicalSortingByName(const ge::ComputeGraphPtr &compute_graph, vector<NodePtr> &node_vec);
  257. ///
  258. /// Get reference-mapping of all data_anchors in graph
  259. /// @param [in] graph
  260. /// @param [out] symbol_to_anchors
  261. /// @param [out] anchor_to_symbol
  262. /// @return success: GRAPH_SUCESS
  263. ///
  264. static graphStatus GetRefMapping(const ComputeGraphPtr &graph,
  265. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  266. std::map<std::string, std::string> &anchor_to_symbol);
  267. ///
  268. /// Determine if the graph is a UNKNOWN_SHAPE graph based on whether the graph and all subgraphs
  269. /// of the graph have UNKNOWN_SHAPE operators or not.
  270. /// Note: This function will only look 'down' from the graph, not 'up'. For example, the following
  271. /// scenario (K for known shape, U for unknown shape), ROOT graph is UNKNOWN_SHAPE while SUB graph is KNOWN_SHAPE
  272. /// ROOT graph: A -----> B -----> C
  273. /// K subgraph U
  274. /// |
  275. /// V
  276. /// SUB graph: D --> E --> F
  277. /// K K K
  278. /// @param [in] graph
  279. /// @return bool
  280. ///
  281. static bool IsUnknownShapeGraph(const ComputeGraphPtr &graph);
  282. static NodePtr FindNodeFromAllNodes(ComputeGraphPtr &graph, const std::string &name);
  283. private:
  284. ///
  285. /// Get reference-mapping for in_data_anchors of node
  286. /// @param [in] node
  287. /// @param [out] symbol_to_anchors
  288. /// @param [out] anchor_to_symbol
  289. /// @return success: GRAPH_SUCESS
  290. ///
  291. static graphStatus HandleInAnchorMapping(const NodePtr &node,
  292. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  293. std::map<std::string, std::string> &anchor_to_symbol);
  294. ///
  295. /// Get reference-mapping for out_data_anchors of node
  296. /// @param [in] node
  297. /// @param [out] symbol_to_anchors
  298. /// @param [out] anchor_to_symbol
  299. /// @return success: GRAPH_SUCESS
  300. ///
  301. static graphStatus HandleOutAnchorMapping(const NodePtr &node,
  302. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  303. std::map<std::string, std::string> &anchor_to_symbol);
  304. ///
  305. /// Handle input of subgraph
  306. /// @param [in] node
  307. /// @param [out] symbol_to_anchors
  308. /// @param [out] anchor_to_symbol
  309. /// @return success: GRAPH_SUCESS
  310. ///
  311. static graphStatus HandleSubgraphInput(const NodePtr &node,
  312. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  313. std::map<std::string, std::string> &anchor_to_symbol);
  314. ///
  315. /// Handle input of Merge op
  316. /// @param [in] node
  317. /// @param [out] symbol_to_anchors
  318. /// @param [out] anchor_to_symbol
  319. /// @return success: GRAPH_SUCESS
  320. ///
  321. static graphStatus HandleMergeInput(const NodePtr &node,
  322. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  323. std::map<std::string, std::string> &anchor_to_symbol);
  324. ///
  325. /// Handle output of subgraph
  326. /// @param [in] node
  327. /// @param [out] symbol_to_anchors
  328. /// @param [out] anchor_to_symbol
  329. /// @return success: GRAPH_SUCESS
  330. ///
  331. static graphStatus HandleSubgraphOutput(const NodePtr &node,
  332. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  333. std::map<std::string, std::string> &anchor_to_symbol);
  334. ///
  335. /// Union ref-mapping
  336. /// @param [in] exist_node_info1
  337. /// @param [in] exist_node_info2
  338. /// @param [out] symbol_to_anchors
  339. /// @param [out] anchor_to_symbol
  340. /// @param [out] symbol
  341. /// @return success: GRAPH_SUCESS
  342. ///
  343. static graphStatus UnionSymbolMapping(const NodeIndexIO &exist_node_info1, const NodeIndexIO &exist_node_info2,
  344. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  345. std::map<std::string, std::string> &anchor_to_symbol, std::string &symbol);
  346. ///
  347. /// Update symbol mapping with a new reference pair
  348. /// @param [in] cur_node_info
  349. /// @param [in] exist_node_info
  350. /// @param [out] symbol_to_anchors
  351. /// @param [out] anchor_to_symbol
  352. /// @return success: GRAPH_SUCESS
  353. ///
  354. static graphStatus UpdateRefMapping(const NodeIndexIO &cur_node_info, const NodeIndexIO &exist_node_info,
  355. std::map<std::string, std::list<NodeIndexIO>> &symbol_to_anchors,
  356. std::map<std::string, std::string> &anchor_to_symbol);
  357. ///
  358. /// Check if out_data_anchor is reference of input
  359. /// @param [in] out_data_anchor
  360. /// @param [out] reuse_in_index
  361. /// @return bool
  362. ///
  363. static bool IsRefFromInput(const OutDataAnchorPtr &out_data_anchor, int32_t &reuse_in_index);
  364. };
  365. class ComputeGraphBuilder {
  366. public:
  367. ComputeGraphBuilder() : owner_graph_(nullptr) {}
  368. ComputeGraphBuilder(const ComputeGraphBuilder &) = delete;
  369. ComputeGraphBuilder &operator=(const ComputeGraphBuilder &) = delete;
  370. ComputeGraphBuilder(const ComputeGraphBuilder &&) = delete;
  371. ComputeGraphBuilder &operator=(const ComputeGraphBuilder &&) = delete;
  372. ~ComputeGraphBuilder() = default;
  373. ///
  374. /// @brief Add node to graph
  375. /// @param [in] op_desc
  376. /// @return ComputeGraphBuilder
  377. ///
  378. virtual ComputeGraphBuilder &AddNode(const OpDescPtr &op_desc);
  379. ///
  380. /// @brief Add data-link among nodes in graph
  381. /// @param [in] src_name
  382. /// @param [in] out_anchor_ind
  383. /// @param [in] dst_name
  384. /// @param [in] in_anchor_ind
  385. /// @return ComputeGraphBuilder
  386. ///
  387. virtual ComputeGraphBuilder &AddDataLink(const std::string &src_name, uint32_t out_anchor_ind,
  388. const std::string &dst_name, uint32_t in_anchor_ind);
  389. ///
  390. /// @brief Add ctrl-link among nodes in graph
  391. /// @param [in] src_name
  392. /// @param [in] dst_name
  393. /// @return ComputeGraphBuilder
  394. ///
  395. virtual ComputeGraphBuilder &AddControlLink(const std::string &src_name, const std::string &dst_name);
  396. ///
  397. /// @brief Build graph
  398. /// @param [out] error_code
  399. /// @param [out] error_msg
  400. /// @return ComputeGraphPtr
  401. ///
  402. virtual ComputeGraphPtr Build(graphStatus &error_code, std::string &error_msg) = 0;
  403. /// @brief Get node with name
  404. /// @param [in] name
  405. /// @return NodePtr
  406. ///
  407. NodePtr GetNode(const std::string &name);
  408. /// @brief Get all nodes
  409. /// @return std::vector<NodePtr>
  410. ///
  411. std::vector<NodePtr> GetAllNodes();
  412. protected:
  413. ///
  414. /// @brief Build nodes
  415. /// @param [out] error_code
  416. /// @param [out] error_msg
  417. /// @return void
  418. ///
  419. void BuildNodes(graphStatus &error_code, std::string &error_msg);
  420. ///
  421. /// @brief Build data-links
  422. /// @param [out] error_code
  423. /// @param [out] error_msg
  424. /// @return void
  425. ///
  426. void BuildDataLinks(graphStatus &error_code, std::string &error_msg);
  427. ///
  428. /// @brief Build ctrl-links
  429. /// @param [out] error_code
  430. /// @param [out] error_msg
  431. /// @return void
  432. ///
  433. void BuildCtrlLinks(graphStatus &error_code, std::string &error_msg);
  434. ComputeGraphPtr owner_graph_;
  435. // node_name -> node
  436. std::map<std::string, NodePtr> node_names_;
  437. std::vector<OpDescPtr> nodes_;
  438. // <src_node_name, out_anchor_ind> -> <dst_node_name, in_anchor_ind>
  439. std::vector<std::pair<std::pair<std::string, uint32_t>, std::pair<std::string, uint32_t>>> data_links_;
  440. // src_node_name -> dst_node_name
  441. std::vector<std::pair<std::string, std::string>> ctrl_links_;
  442. };
  443. class CompleteGraphBuilder : public ComputeGraphBuilder {
  444. public:
  445. explicit CompleteGraphBuilder(std::string name) : name_(std::move(name)), parent_node_(nullptr) {}
  446. CompleteGraphBuilder(const CompleteGraphBuilder &) = delete;
  447. CompleteGraphBuilder &operator=(const CompleteGraphBuilder &) = delete;
  448. CompleteGraphBuilder(const CompleteGraphBuilder &&) = delete;
  449. CompleteGraphBuilder &operator=(const CompleteGraphBuilder &&) = delete;
  450. ~CompleteGraphBuilder() = default;
  451. ///
  452. /// @brief Add node to graph
  453. /// @param [in] op_desc
  454. /// @return CompleteGraphBuilder
  455. ///
  456. CompleteGraphBuilder &AddNode(const OpDescPtr &op_desc) override;
  457. ///
  458. /// @brief Add data-link among nodes in graph
  459. /// @param [in] src_name
  460. /// @param [in] out_anchor_ind
  461. /// @param [in] dst_name
  462. /// @param [in] in_anchor_ind
  463. /// @return CompleteGraphBuilder
  464. ///
  465. CompleteGraphBuilder &AddDataLink(const std::string &src_name, uint32_t out_anchor_ind, const std::string &dst_name,
  466. uint32_t in_anchor_ind) override;
  467. ///
  468. /// @brief Add ctrl-link among nodes in graph
  469. /// @param [in] src_name
  470. /// @param [in] dst_name
  471. /// @return CompleteGraphBuilder
  472. ///
  473. CompleteGraphBuilder &AddControlLink(const std::string &src_name, const std::string &dst_name) override;
  474. ///
  475. /// @brief Set index_th input anchor for graph
  476. /// @param [in] index
  477. /// @param [in] node_names
  478. /// @param [in] anchor_inds
  479. /// @return CompleteGraphBuilder
  480. ///
  481. CompleteGraphBuilder &SetInput(uint32_t index, const std::vector<std::string> &node_names,
  482. const std::vector<uint32_t> &anchor_inds);
  483. ///
  484. /// @brief Set index_th input of graph as useless
  485. /// @param [in] index
  486. /// @return CompleteGraphBuilder
  487. ///
  488. CompleteGraphBuilder &SetUselessInput(uint32_t index);
  489. ///
  490. /// @brief Add output anchor for graph
  491. /// @param [in] owner_node_name
  492. /// @param [in] anchor_ind
  493. /// @return CompleteGraphBuilder
  494. ///
  495. CompleteGraphBuilder &AddOutput(const std::string &owner_node_name, uint32_t anchor_ind);
  496. ///
  497. /// @brief Add target for graph
  498. /// @param [in] target_name
  499. /// @return CompleteGraphBuilder
  500. ///
  501. CompleteGraphBuilder &AddTarget(const std::string &target_name);
  502. ///
  503. /// @brief Set parent-node of graph
  504. /// @param [in] parent_node
  505. /// @return CompleteGraphBuilder
  506. ///
  507. CompleteGraphBuilder &SetParentNode(const NodePtr &parent_node);
  508. ///
  509. /// @brief Set mapping-relation of parent-node in_anchor_ind & Data-node
  510. /// @param [in] input_mapping: index_of_graph_input -> in_anchor_index_of_parent_node
  511. /// @return CompleteGraphBuilder
  512. ///
  513. CompleteGraphBuilder &SetInputMapping(const std::map<uint32_t, uint32_t> &input_mapping);
  514. ///
  515. /// @brief Set mapping-relation of parent-node out_anchor_ind & NetOutput-node out_anchor_ind
  516. /// @param [in] output_mapping: index_of_graph_output -> out_anchor_index_of_parent_node
  517. /// @return CompleteGraphBuilder
  518. ///
  519. CompleteGraphBuilder &SetOutputMapping(const std::map<uint32_t, uint32_t> &output_mapping);
  520. ///
  521. /// @brief Build graph
  522. /// @param [out] error_code
  523. /// @param [out] error_msg
  524. /// @return ComputeGraphPtr
  525. ///
  526. ComputeGraphPtr Build(graphStatus &error_code, std::string &error_msg) override;
  527. private:
  528. ///
  529. /// @brief Add data nodes
  530. /// @param [out] error_code
  531. /// @param [out] error_msg
  532. /// @return void
  533. ///
  534. void AddDataNodes(graphStatus &error_code, std::string &error_msg);
  535. ///
  536. /// @brief Add data node
  537. /// @param [in] index
  538. /// @param [out] error_code
  539. /// @param [out] error_msg
  540. /// @return void
  541. ///
  542. NodePtr AddDataNode(uint32_t index, graphStatus &error_code, std::string &error_msg);
  543. ///
  544. /// @brief Add RetVal nodes
  545. /// @param [out] error_code
  546. /// @param [out] error_msg
  547. /// @return void
  548. ///
  549. void AddRetValNodes(graphStatus &error_code, std::string &error_msg);
  550. ///
  551. /// @brief Build target-nodes for graph
  552. /// @param [out] error_code
  553. /// @param [out] error_msg
  554. /// @return void
  555. ///
  556. void BuildGraphTargets(graphStatus &error_code, std::string &error_msg);
  557. std::string name_;
  558. NodePtr parent_node_;
  559. std::map<uint32_t, std::pair<std::vector<std::string>, std::vector<uint32_t>>> graph_inputs_;
  560. std::vector<std::pair<std::string, uint32_t>> graph_outputs_;
  561. std::vector<std::string> graph_targets_;
  562. // index_of_graph_input -> in_anchor_index_of_parent_node
  563. std::map<uint32_t, uint32_t> input_mapping_;
  564. // index_of_graph_output -> out_anchor_index_of_parent_node
  565. std::map<uint32_t, uint32_t> output_mapping_;
  566. };
  567. class PartialGraphBuilder : public ComputeGraphBuilder {
  568. public:
  569. PartialGraphBuilder() = default;
  570. PartialGraphBuilder(const PartialGraphBuilder &) = delete;
  571. PartialGraphBuilder &operator=(const PartialGraphBuilder &) = delete;
  572. PartialGraphBuilder(const PartialGraphBuilder &&) = delete;
  573. PartialGraphBuilder &operator=(const PartialGraphBuilder &&) = delete;
  574. ~PartialGraphBuilder() = default;
  575. ///
  576. /// @brief Add node to graph
  577. /// @param [in] op_desc
  578. /// @return PartialGraphBuilder
  579. ///
  580. PartialGraphBuilder &AddNode(const OpDescPtr &op_desc) override;
  581. ///
  582. /// @brief Add data-link among nodes in graph
  583. /// @param [in] src_name
  584. /// @param [in] out_anchor_ind
  585. /// @param [in] dst_name
  586. /// @param [in] in_anchor_ind
  587. /// @return PartialGraphBuilder
  588. ///
  589. PartialGraphBuilder &AddDataLink(const std::string &src_name, uint32_t out_anchor_ind, const std::string &dst_name,
  590. uint32_t in_anchor_ind) override;
  591. ///
  592. /// @brief Add ctrl-link among nodes in graph
  593. /// @param [in] src_name
  594. /// @param [in] dst_name
  595. /// @return PartialGraphBuilder
  596. ///
  597. PartialGraphBuilder &AddControlLink(const std::string &src_name, const std::string &dst_name) override;
  598. ///
  599. /// @brief Set owner graph
  600. /// @param [in] graph
  601. /// @return PartialGraphBuilder
  602. ///
  603. PartialGraphBuilder &SetOwnerGraph(const ComputeGraphPtr &graph);
  604. ///
  605. /// @brief Add exist node
  606. /// @param [in] node
  607. /// @return PartialGraphBuilder
  608. ///
  609. PartialGraphBuilder &AddExistNode(const NodePtr &node);
  610. ///
  611. /// @brief Build multi nodes with links
  612. /// @param [out] error_code
  613. /// @param [out] error_msg
  614. /// @return ComputeGraphPtr
  615. ///
  616. ComputeGraphPtr Build(graphStatus &error_code, std::string &error_msg) override;
  617. private:
  618. ///
  619. /// @brief Build exist nodes
  620. /// @param [out] error_code
  621. /// @param [out] error_msg
  622. /// @return void
  623. ///
  624. void BuildExistNodes(graphStatus &error_code, std::string &error_msg);
  625. std::vector<NodePtr> exist_nodes_;
  626. };
  627. } // namespace ge
  628. #endif // INC_GRAPH_UTILS_GRAPH_UTILS_H_

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