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

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