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

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

Contributors (1)