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

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