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.

node.h 6.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_NODE_H_
  17. #define INC_GRAPH_NODE_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <utility>
  22. #include <vector>
  23. #include <unordered_set>
  24. #include "graph/ge_attr_value.h"
  25. #include "utils/attr_utils.h"
  26. #include "graph/op_desc.h"
  27. #include "graph/range_vistor.h"
  28. namespace ge {
  29. class ComputeGraph;
  30. using ComputeGraphPtr = std::shared_ptr<ComputeGraph>;
  31. class Node;
  32. using NodePtr = std::shared_ptr<Node>;
  33. using ConstNodePtr = std::shared_ptr<const Node>;
  34. using NodeRef = std::weak_ptr<Node>;
  35. class Anchor;
  36. using AnchorPtr = std::shared_ptr<Anchor>;
  37. class InDataAnchor;
  38. using InDataAnchorPtr = std::shared_ptr<InDataAnchor>;
  39. class OutDataAnchor;
  40. using OutDataAnchorPtr = std::shared_ptr<OutDataAnchor>;
  41. class ControlAnchor;
  42. using ControlAnchorPtr = std::shared_ptr<ControlAnchor>;
  43. class InControlAnchor;
  44. using InControlAnchorPtr = std::shared_ptr<InControlAnchor>;
  45. class OutControlAnchor;
  46. using OutControlAnchorPtr = std::shared_ptr<OutControlAnchor>;
  47. using OpDescPtr = std::shared_ptr<OpDesc>;
  48. using ConstNode = const Node;
  49. typedef std::vector<std::multimap<std::string, ge::AnchorPtr>> kFusionDataFlowVec_t;
  50. // Node is a component of ComputeGraph
  51. class Node : public std::enable_shared_from_this<Node> {
  52. friend class ComputeGraph;
  53. friend class ModelSerializeImp;
  54. public:
  55. template <class T>
  56. using Vistor = RangeVistor<T, std::shared_ptr<ConstNode>>;
  57. ~Node();
  58. Node(const Node &) = delete;
  59. Node &operator=(const Node &) = delete;
  60. bool operator==(const Node &r_node) const;
  61. protected:
  62. Node() = default;
  63. Node(const OpDescPtr &op, const ComputeGraphPtr &ownerGraph);
  64. public:
  65. graphStatus Init();
  66. std::string GetName() const;
  67. std::string GetType() const;
  68. ComputeGraphPtr GetOwnerComputeGraph() const;
  69. graphStatus SetOwnerComputeGraph(const ComputeGraphPtr &graph);
  70. graphStatus SetAnyOwnerComputeGraph(const ComputeGraphPtr &graph);
  71. Vistor<InDataAnchorPtr> GetAllInDataAnchors() const;
  72. Vistor<OutDataAnchorPtr> GetAllOutDataAnchors() const;
  73. uint32_t GetAllInDataAnchorsSize() const;
  74. uint32_t GetAllOutDataAnchorsSize() const;
  75. Vistor<AnchorPtr> GetAllOutAnchors() const;
  76. Vistor<AnchorPtr> GetAllInAnchors() const;
  77. InDataAnchorPtr GetInDataAnchor(int idx) const;
  78. OutDataAnchorPtr GetOutDataAnchor(int idx) const;
  79. InControlAnchorPtr GetInControlAnchor() const;
  80. OutControlAnchorPtr GetOutControlAnchor() const;
  81. Vistor<NodePtr> GetInNodes() const;
  82. Vistor<NodePtr> GetOutNodes() const;
  83. AnchorPtr GetInAnchor(int idx) const;
  84. AnchorPtr GetOutAnchor(int idx) const;
  85. bool IsAllInNodesSeen(std::unordered_set<Node *> &nodes_seen) const;
  86. // All in Data nodes
  87. Vistor<NodePtr> GetInDataNodes() const;
  88. // All in Control nodes
  89. Vistor<NodePtr> GetInControlNodes() const;
  90. // GetInAllNodes = InDataNodes + InControlNodes
  91. Vistor<NodePtr> GetInAllNodes() const;
  92. // All out Data nodes
  93. Vistor<NodePtr> GetOutDataNodes() const;
  94. uint32_t GetOutDataNodesSize() const;
  95. // All out Control nodes
  96. Vistor<NodePtr> GetOutControlNodes() const;
  97. // GetOutAllNodes = OutDataNodes + InControlNodes
  98. Vistor<NodePtr> GetOutAllNodes() const;
  99. // Get all in data nodes and its out-anchor
  100. Vistor<std::pair<NodePtr, OutDataAnchorPtr>> GetInDataNodesAndAnchors() const;
  101. // Get all out data nodes and its in-anchor
  102. Vistor<std::pair<NodePtr, InDataAnchorPtr>> GetOutDataNodesAndAnchors() const;
  103. graphStatus InferShapeAndType() const;
  104. graphStatus Verify() const;
  105. graphStatus InferOriginFormat() const;
  106. OpDescPtr GetOpDesc() const;
  107. graphStatus UpdateOpDesc(const OpDescPtr &op);
  108. graphStatus AddLinkFrom(const NodePtr &input_node);
  109. graphStatus AddLinkFrom(const uint32_t &index, NodePtr input_node);
  110. graphStatus AddLinkFrom(const string &name, NodePtr input_node);
  111. graphStatus AddLinkFromForParse(const NodePtr &input_node);
  112. void AddSendEventId(uint32_t event_id) { send_event_id_list_.push_back(event_id); }
  113. void AddRecvEventId(uint32_t event_id) { recv_event_id_list_.push_back(event_id); }
  114. const std::vector<uint32_t> &GetSendEventIdList() const { return send_event_id_list_; }
  115. const std::vector<uint32_t> &GetRecvEventIdList() const { return recv_event_id_list_; }
  116. void GetFusionInputFlowList(kFusionDataFlowVec_t &fusion_input_list) {
  117. fusion_input_list = fusion_input_dataflow_list_;
  118. }
  119. void GetFusionOutputFlowList(kFusionDataFlowVec_t &fusion_output_list) {
  120. fusion_output_list = fusion_output_dataflow_list_;
  121. }
  122. void SetFusionInputFlowList(kFusionDataFlowVec_t &fusion_input_list) {
  123. fusion_input_dataflow_list_ = fusion_input_list;
  124. }
  125. void SetFusionOutputFlowList(kFusionDataFlowVec_t &fusion_output_list) {
  126. fusion_output_dataflow_list_ = fusion_output_list;
  127. }
  128. bool GetHostNode() const { return host_node_; }
  129. void SetHostNode(bool is_host) { host_node_ = is_host; }
  130. void SetOrigNode(const NodePtr &orignode) { orig_node_ = orignode; }
  131. NodePtr GetOrigNode() { return orig_node_; }
  132. private:
  133. bool NodeMembersAreEqual(const Node &r_node) const;
  134. bool NodeAttrsAreEqual(const Node &r_node) const;
  135. bool NodeInConnectsAreEqual(const Node &r_node) const;
  136. bool NodeOutConnectsAreEqual(const Node &r_node) const;
  137. bool NodeAnchorIsEqual(const AnchorPtr &l_anchor, const AnchorPtr &r_anchor, size_t i) const;
  138. OpDescPtr op_;
  139. std::weak_ptr<ComputeGraph> owner_graph_;
  140. vector<InDataAnchorPtr> in_data_anchors_;
  141. vector<OutDataAnchorPtr> out_data_anchors_;
  142. InControlAnchorPtr in_control_anchor_;
  143. OutControlAnchorPtr out_control_anchor_;
  144. map<string, GeAttrValue> attrs_;
  145. bool has_init_{false};
  146. bool host_node_{false};
  147. bool anchor_status_updated_{false};
  148. std::vector<uint32_t> send_event_id_list_;
  149. std::vector<uint32_t> recv_event_id_list_;
  150. kFusionDataFlowVec_t fusion_input_dataflow_list_;
  151. kFusionDataFlowVec_t fusion_output_dataflow_list_;
  152. NodePtr orig_node_;
  153. friend class NodeUtils;
  154. friend class OnnxUtils;
  155. friend class TuningUtils;
  156. };
  157. } // namespace ge
  158. #endif // INC_GRAPH_NODE_H_

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