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.4 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. Vistor<InDataAnchorPtr> GetAllInDataAnchors() const;
  71. Vistor<OutDataAnchorPtr> GetAllOutDataAnchors() const;
  72. uint32_t GetAllInDataAnchorsSize() const;
  73. uint32_t GetAllOutDataAnchorsSize() const;
  74. Vistor<AnchorPtr> GetAllOutAnchors() const;
  75. Vistor<AnchorPtr> GetAllInAnchors() const;
  76. InDataAnchorPtr GetInDataAnchor(int idx) const;
  77. OutDataAnchorPtr GetOutDataAnchor(int idx) const;
  78. InControlAnchorPtr GetInControlAnchor() const;
  79. OutControlAnchorPtr GetOutControlAnchor() const;
  80. Vistor<NodePtr> GetInNodes() const;
  81. Vistor<NodePtr> GetOutNodes() const;
  82. AnchorPtr GetInAnchor(int idx) const;
  83. AnchorPtr GetOutAnchor(int idx) const;
  84. bool IsAllInNodesSeen(std::unordered_set<Node *> &nodes_seen) const;
  85. // All in Data nodes
  86. Vistor<NodePtr> GetInDataNodes() const;
  87. // All in Control nodes
  88. Vistor<NodePtr> GetInControlNodes() const;
  89. // GetInAllNodes = InDataNodes + InControlNodes
  90. Vistor<NodePtr> GetInAllNodes() const;
  91. // All out Data nodes
  92. Vistor<NodePtr> GetOutDataNodes() const;
  93. uint32_t GetOutDataNodesSize() const;
  94. // All out Control nodes
  95. Vistor<NodePtr> GetOutControlNodes() const;
  96. // GetOutAllNodes = OutDataNodes + InControlNodes
  97. Vistor<NodePtr> GetOutAllNodes() const;
  98. // Get all in data nodes and its out-anchor
  99. Vistor<std::pair<NodePtr, OutDataAnchorPtr>> GetInDataNodesAndAnchors() const;
  100. // Get all out data nodes and its in-anchor
  101. Vistor<std::pair<NodePtr, InDataAnchorPtr>> GetOutDataNodesAndAnchors() const;
  102. graphStatus InferShapeAndType() const;
  103. graphStatus Verify() const;
  104. graphStatus InferOriginFormat() const;
  105. OpDescPtr GetOpDesc() const;
  106. graphStatus UpdateOpDesc(const OpDescPtr &op);
  107. graphStatus AddLinkFrom(const NodePtr &input_node);
  108. graphStatus AddLinkFrom(const uint32_t &index, NodePtr input_node);
  109. graphStatus AddLinkFrom(const string &name, NodePtr input_node);
  110. graphStatus AddLinkFromForParse(const NodePtr &input_node);
  111. void AddSendEventId(uint32_t event_id) { send_event_id_list_.push_back(event_id); }
  112. void AddRecvEventId(uint32_t event_id) { recv_event_id_list_.push_back(event_id); }
  113. const std::vector<uint32_t> &GetSendEventIdList() const { return send_event_id_list_; }
  114. const std::vector<uint32_t> &GetRecvEventIdList() const { return recv_event_id_list_; }
  115. void GetFusionInputFlowList(kFusionDataFlowVec_t &fusion_input_list) {
  116. fusion_input_list = fusion_input_dataflow_list_;
  117. }
  118. void GetFusionOutputFlowList(kFusionDataFlowVec_t &fusion_output_list) {
  119. fusion_output_list = fusion_output_dataflow_list_;
  120. }
  121. void SetFusionInputFlowList(kFusionDataFlowVec_t &fusion_input_list) {
  122. fusion_input_dataflow_list_ = fusion_input_list;
  123. }
  124. void SetFusionOutputFlowList(kFusionDataFlowVec_t &fusion_output_list) {
  125. fusion_output_dataflow_list_ = fusion_output_list;
  126. }
  127. void SetOrigNode(const NodePtr &orignode) { orig_node_ = orignode; }
  128. NodePtr GetOrigNode(void) { return orig_node_; }
  129. private:
  130. bool NodeMembersAreEqual(const Node &r_node) const;
  131. bool NodeAttrsAreEqual(const Node &r_node) const;
  132. bool NodeInConnectsAreEqual(const Node &r_node) const;
  133. bool NodeOutConnectsAreEqual(const Node &r_node) const;
  134. bool NodeAnchorIsEqual(const AnchorPtr &l_anchor, const AnchorPtr &r_anchor, size_t i) const;
  135. OpDescPtr op_;
  136. std::weak_ptr<ComputeGraph> owner_graph_;
  137. vector<InDataAnchorPtr> in_data_anchors_;
  138. vector<OutDataAnchorPtr> out_data_anchors_;
  139. InControlAnchorPtr in_control_anchor_;
  140. OutControlAnchorPtr out_control_anchor_;
  141. map<string, GeAttrValue> attrs_;
  142. bool has_init_{false};
  143. bool anchor_status_updated_{false};
  144. std::vector<uint32_t> send_event_id_list_;
  145. std::vector<uint32_t> recv_event_id_list_;
  146. kFusionDataFlowVec_t fusion_input_dataflow_list_;
  147. kFusionDataFlowVec_t fusion_output_dataflow_list_;
  148. NodePtr orig_node_;
  149. friend class NodeUtils;
  150. friend class OnnxUtils;
  151. };
  152. } // namespace ge
  153. #endif // INC_GRAPH_NODE_H_

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

Contributors (1)