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

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