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.

anchor.h 7.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_ANCHOR_H_
  17. #define INC_GRAPH_ANCHOR_H_
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include "graph/ge_error_codes.h"
  22. #include "graph/range_vistor.h"
  23. #include "graph/types.h"
  24. namespace ge {
  25. enum AnchorStatus { ANCHOR_SUSPEND = 0, ANCHOR_CONST = 1, ANCHOR_DATA = 2, ANCHOR_RESERVED = 3 };
  26. using std::string;
  27. using std::vector;
  28. class Node;
  29. using NodePtr = std::shared_ptr<Node>;
  30. class Edge;
  31. using EdgePtr = std::shared_ptr<Edge>;
  32. class Anchor;
  33. using AnchorPtr = std::shared_ptr<Anchor>;
  34. class DataAnchor;
  35. using DataAnchorPtr = std::shared_ptr<DataAnchor>;
  36. class InDataAnchor;
  37. using InDataAnchorPtr = std::shared_ptr<InDataAnchor>;
  38. class OutDataAnchor;
  39. using OutDataAnchorPtr = std::shared_ptr<OutDataAnchor>;
  40. class ControlAnchor;
  41. using ControlAnchorPtr = std::shared_ptr<ControlAnchor>;
  42. class InControlAnchor;
  43. using InControlAnchorPtr = std::shared_ptr<InControlAnchor>;
  44. class OutControlAnchor;
  45. using OutControlAnchorPtr = std::shared_ptr<OutControlAnchor>;
  46. using ConstAnchor = const Anchor;
  47. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Anchor : public std::enable_shared_from_this<Anchor> {
  48. friend class AnchorUtils;
  49. public:
  50. using TYPE = const char *;
  51. template <class T>
  52. using Vistor = RangeVistor<T, std::shared_ptr<ConstAnchor>>;
  53. Anchor(const NodePtr &ownerNode, int idx);
  54. virtual ~Anchor() = default;
  55. protected:
  56. // Whether the two anchors are equal
  57. virtual bool Equal(AnchorPtr anchor) const = 0;
  58. virtual bool IsTypeOf(TYPE type) const;
  59. public:
  60. // Get all peer anchors connected to current anchor
  61. Vistor<AnchorPtr> GetPeerAnchors() const;
  62. // Get the first peer anchor
  63. AnchorPtr GetFirstPeerAnchor() const;
  64. // Get the node which is the owner of the anchor
  65. NodePtr GetOwnerNode() const;
  66. // Remove all links with the anchor
  67. void UnlinkAll() noexcept;
  68. // Remove link with the given anchor
  69. graphStatus Unlink(const AnchorPtr &peer);
  70. // Replace the peeranchor with the new peeranchor
  71. graphStatus ReplacePeer(const AnchorPtr &oldPeer, const AnchorPtr &firstPeer, const AnchorPtr &secondPeer);
  72. // Judge if the anchor is linked with the given anchor
  73. bool IsLinkedWith(const AnchorPtr &peer);
  74. // Get the anchor index of the node
  75. int GetIdx() const;
  76. // Set the anchor index of the node
  77. void SetIdx(int index);
  78. protected:
  79. // All peer anchors connected to current anchor
  80. vector<std::weak_ptr<Anchor>> peer_anchors_;
  81. // The owner nodes of the anchor
  82. std::weak_ptr<Node> owner_node_;
  83. // The index of current anchor
  84. int idx_;
  85. template <class T>
  86. static Anchor::TYPE TypeOf() {
  87. static_assert(std::is_base_of<Anchor, T>::value, "T must be a Anchor!");
  88. return __PRETTY_FUNCTION__;
  89. }
  90. public:
  91. template <class T>
  92. static std::shared_ptr<T> DynamicAnchorCast(AnchorPtr anchorPtr) {
  93. static_assert(std::is_base_of<Anchor, T>::value, "T must be a Anchor!");
  94. if (anchorPtr == nullptr || !anchorPtr->IsTypeOf<T>()) {
  95. return nullptr;
  96. }
  97. return std::static_pointer_cast<T>(anchorPtr);
  98. }
  99. template <typename T>
  100. bool IsTypeOf() {
  101. return IsTypeOf(TypeOf<T>());
  102. }
  103. };
  104. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY DataAnchor : public Anchor {
  105. friend class AnchorUtils;
  106. public:
  107. explicit DataAnchor(const NodePtr &ownerNode, int idx);
  108. virtual ~DataAnchor() = default;
  109. protected:
  110. bool IsTypeOf(TYPE type) const override;
  111. private:
  112. Format format_{FORMAT_ND};
  113. AnchorStatus status_{ANCHOR_SUSPEND};
  114. };
  115. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY InDataAnchor : public DataAnchor {
  116. friend class OutDataAnchor;
  117. friend class OutControlAnchor;
  118. public:
  119. explicit InDataAnchor(const NodePtr &ownerNode, int idx);
  120. virtual ~InDataAnchor() = default;
  121. // Get source out data anchor
  122. OutDataAnchorPtr GetPeerOutAnchor() const;
  123. // Build connection from OutDataAnchor to InDataAnchor
  124. graphStatus LinkFrom(const OutDataAnchorPtr &src);
  125. protected:
  126. bool Equal(AnchorPtr anchor) const override;
  127. bool IsTypeOf(TYPE type) const override;
  128. };
  129. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY OutDataAnchor : public DataAnchor {
  130. friend class InDataAnchor;
  131. friend class AnchorUtils;
  132. public:
  133. template <class T>
  134. using Vistor = RangeVistor<T, std::shared_ptr<ConstAnchor>>;
  135. explicit OutDataAnchor(const NodePtr &ownerNode, int idx);
  136. virtual ~OutDataAnchor() = default;
  137. // Get dst in data anchor(one or more)
  138. Vistor<InDataAnchorPtr> GetPeerInDataAnchors() const;
  139. uint32_t GetPeerInDataNodesSize() const;
  140. // Get dst in control anchor(one or more)
  141. Vistor<InControlAnchorPtr> GetPeerInControlAnchors() const;
  142. // Build connection from OutDataAnchor to InDataAnchor
  143. graphStatus LinkTo(const InDataAnchorPtr &dest);
  144. // Build connection from OutDataAnchor to InControlAnchor
  145. graphStatus LinkTo(const InControlAnchorPtr &dest);
  146. protected:
  147. bool Equal(AnchorPtr anchor) const override;
  148. bool IsTypeOf(TYPE type) const override;
  149. };
  150. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY ControlAnchor : public Anchor {
  151. public:
  152. explicit ControlAnchor(const NodePtr &ownerNode);
  153. explicit ControlAnchor(const NodePtr &ownerNode, int idx);
  154. virtual ~ControlAnchor() = default;
  155. protected:
  156. bool IsTypeOf(TYPE type) const override;
  157. };
  158. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY InControlAnchor : public ControlAnchor {
  159. friend class OutControlAnchor;
  160. friend class OutDataAnchor;
  161. public:
  162. explicit InControlAnchor(const NodePtr &ownerNode);
  163. explicit InControlAnchor(const NodePtr &ownerNode, int idx);
  164. virtual ~InControlAnchor() = default;
  165. // Get source out control anchors
  166. Vistor<OutControlAnchorPtr> GetPeerOutControlAnchors() const;
  167. bool IsPeerOutAnchorsEmpty() const { return peer_anchors_.empty(); }
  168. // Get source out data anchors
  169. Vistor<OutDataAnchorPtr> GetPeerOutDataAnchors() const;
  170. // Build connection from OutControlAnchor to InControlAnchor
  171. graphStatus LinkFrom(const OutControlAnchorPtr &src);
  172. protected:
  173. bool Equal(AnchorPtr anchor) const override;
  174. bool IsTypeOf(TYPE type) const override;
  175. };
  176. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY OutControlAnchor : public ControlAnchor {
  177. friend class InControlAnchor;
  178. public:
  179. template <class T>
  180. using Vistor = RangeVistor<T, std::shared_ptr<ConstAnchor>>;
  181. explicit OutControlAnchor(const NodePtr &ownerNode);
  182. explicit OutControlAnchor(const NodePtr &ownerNode, int idx);
  183. virtual ~OutControlAnchor() = default;
  184. // Get dst in control anchor(one or more)
  185. Vistor<InControlAnchorPtr> GetPeerInControlAnchors() const;
  186. // Get dst data anchor in control anchor(one or more)
  187. Vistor<InDataAnchorPtr> GetPeerInDataAnchors() const;
  188. // Build connection from OutControlAnchor to InControlAnchor
  189. graphStatus LinkTo(const InControlAnchorPtr &dest);
  190. // Build connection from OutDataAnchor to InDataAnchor
  191. graphStatus LinkTo(const InDataAnchorPtr &dest);
  192. protected:
  193. bool Equal(AnchorPtr anchor) const override;
  194. bool IsTypeOf(TYPE type) const override;
  195. };
  196. } // namespace ge
  197. #endif // INC_GRAPH_ANCHOR_H_

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

Contributors (1)