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.

op_desc.h 10 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_OP_DESC_H_
  17. #define INC_GRAPH_OP_DESC_H_
  18. #include <functional>
  19. #include <map>
  20. #include <memory>
  21. #include <string>
  22. #include <unordered_set>
  23. #include <vector>
  24. #include "detail/attributes_holder.h"
  25. #include "graph/range_vistor.h"
  26. #define DYNAMIN_INPUT_NAME(name, index) (((name)) + std::to_string((index)))
  27. #define DYNAMIN_OUTPUT_NAME(name, index) (((name)) + std::to_string((index)))
  28. namespace ge {
  29. using std::map;
  30. using std::pair;
  31. using std::shared_ptr;
  32. using std::string;
  33. using std::vector;
  34. class Operator;
  35. class GeTensorDesc;
  36. using GeTensorDescPtr = shared_ptr<GeTensorDesc>;
  37. using ConstGeTensorDescPtr = shared_ptr<const GeTensorDesc>;
  38. class OpDesc;
  39. using OpDescPtr = shared_ptr<OpDesc>;
  40. using ConstOpDescPtr = shared_ptr<const OpDesc>;
  41. class GeAttrValue;
  42. using ConstOpDesc = const OpDesc;
  43. enum SubgraphType { kStatic, kDynamic, kSubgraphTypeEnd };
  44. class OpDesc : public std::enable_shared_from_this<OpDesc>, public AttrHolder {
  45. public:
  46. template <class T>
  47. using Vistor = RangeVistor<T, shared_ptr<ConstOpDesc>>;
  48. friend class GraphBuilderImpl;
  49. friend class OperatorImpl;
  50. OpDesc(const string &name, const string &type);
  51. OpDesc();
  52. ~OpDesc();
  53. bool operator==(const OpDesc &r_op_desc) const;
  54. string GetName() const;
  55. void SetName(const string &name);
  56. string GetType() const;
  57. void SetType(const string &type);
  58. graphStatus AddInputDesc(const GeTensorDesc &input_desc);
  59. graphStatus AddInputDesc(const string &name, const GeTensorDesc &input_desc);
  60. graphStatus AddInputDesc(uint32_t index, const ge::GeTensorDesc &input_desc);
  61. graphStatus AddInputDescForward(const string &name, const unsigned int num);
  62. graphStatus AddInputDescMiddle(const string &name, const unsigned int num, size_t index);
  63. graphStatus AddOutputDescForward(const string &name, const unsigned int num);
  64. graphStatus AddOptionalInputDesc(const string &name, const GeTensorDesc &input_desc);
  65. graphStatus UpdateInputDesc(uint32_t index, const GeTensorDesc &tensor_desc);
  66. graphStatus UpdateInputDesc(const string &name, const GeTensorDesc &tensor_desc);
  67. bool InputIsSet(const string &name) const;
  68. GeTensorDesc GetInputDesc(uint32_t index) const;
  69. GeTensorDesc GetInputDesc(const string &name) const;
  70. Vistor<string> GetAllInputNames() const;
  71. GeTensorDescPtr MutableInputDesc(uint32_t index) const;
  72. GeTensorDescPtr MutableInputDesc(const string &name) const;
  73. Vistor<GeTensorDesc> GetAllInputsDesc() const;
  74. Vistor<GeTensorDescPtr> GetAllInputsDescPtr() const;
  75. size_t GetInputsSize() const;
  76. size_t GetAllInputsSize() const;
  77. graphStatus AddOutputDesc(const GeTensorDesc &output_desc);
  78. graphStatus AddOutputDesc(const string &name, const GeTensorDesc &output_desc);
  79. graphStatus UpdateOutputDesc(uint32_t index, const GeTensorDesc &tensor_desc);
  80. graphStatus UpdateOutputDesc(const string &name, const GeTensorDesc &tensor_desc);
  81. GeTensorDesc GetOutputDesc(uint32_t index) const;
  82. GeTensorDesc GetOutputDesc(const string &name) const;
  83. GeTensorDescPtr MutableOutputDesc(uint32_t index) const;
  84. GeTensorDescPtr MutableOutputDesc(const string &name) const;
  85. uint32_t GetAllOutputsDescSize() const;
  86. Vistor<GeTensorDesc> GetAllOutputsDesc() const;
  87. Vistor<GeTensorDescPtr> GetAllOutputsDescPtr() const;
  88. size_t GetOutputsSize() const;
  89. ConstGeTensorDescPtr GetOutputDescPtr(uint32_t index) const;
  90. ConstGeTensorDescPtr GetInputDescPtr(uint32_t index) const;
  91. ConstGeTensorDescPtr GetInputDescPtrDfault(uint32_t index) const;
  92. ConstGeTensorDescPtr GetInputDescPtr(const string &name) const;
  93. graphStatus AddDynamicInputDesc(const string &name, const unsigned int num, bool isPushBack = true);
  94. graphStatus AddDynamicInputDescByIndex(const string &name, const unsigned int num, size_t index);
  95. graphStatus AddDynamicOutputDesc(const string &name, const unsigned int num, bool isPushBack = true);
  96. bool IsOptionalInput(const string &name) const;
  97. bool IsOptionalInput(uint32_t index) const;
  98. std::map<string, uint32_t> GetAllInputName() const;
  99. void SetAllInputName(const std::map<string, uint32_t> &input_name_idx);
  100. std::vector<string> GetAllOptionalInputName() const;
  101. std::map<string, uint32_t> GetAllOutputName();
  102. bool UpdateInputName(std::map<string, uint32_t> inputNameIdx);
  103. bool UpdateOutputName(std::map<string, uint32_t> outputNameIdx);
  104. void AddInferFunc(const std::function<graphStatus(Operator &)> &func);
  105. std::function<graphStatus(Operator &)> GetInferFunc() const;
  106. graphStatus InferShapeAndType();
  107. void AddInferFormatFunc(const std::function<graphStatus(Operator &)> &func);
  108. std::function<graphStatus(Operator &)> GetInferFormatFunc() const;
  109. graphStatus DefaultInferFormat();
  110. std::function<graphStatus(Operator &)> GetVerifyFunc() const;
  111. void AddVerifierFunc(const std::function<graphStatus(Operator &)> &func);
  112. graphStatus CallInferFormatFunc(Operator &op);
  113. graphStatus OpVerify();
  114. graphStatus CommonVerify() const;
  115. using AttrHolder::AddRequiredAttr;
  116. using AttrHolder::DelAttr;
  117. using AttrHolder::GetAllAttrNames;
  118. using AttrHolder::GetAllAttrs;
  119. using AttrHolder::GetAttr;
  120. using AttrHolder::HasAttr;
  121. using AttrHolder::SetAttr;
  122. void SetId(int64_t id);
  123. int64_t GetId() const;
  124. void SetStreamId(int64_t stream_id);
  125. int64_t GetStreamId() const;
  126. void SetInputName(const vector<string> &input_name);
  127. vector<string> GetInputName() const;
  128. void SetSrcName(const vector<string> &src_name);
  129. vector<string> GetSrcName() const;
  130. void SetSrcIndex(const vector<int64_t> &src_index);
  131. vector<int64_t> GetSrcIndex() const;
  132. void SetInputOffset(const vector<int64_t> &input);
  133. vector<int64_t> GetInputOffset() const;
  134. void SetOutputOffset(const vector<int64_t> &input);
  135. vector<int64_t> GetOutputOffset() const;
  136. void SetDstName(const vector<string> &dst_name);
  137. vector<string> GetDstName() const;
  138. void SetDstIndex(const vector<int64_t> &dst_index);
  139. vector<int64_t> GetDstIndex() const;
  140. void SetWorkspace(const vector<int64_t> &workspace);
  141. vector<int64_t> GetWorkspace() const;
  142. void SetWorkspaceBytes(const vector<int64_t> &workspace_bytes);
  143. vector<int64_t> GetWorkspaceBytes() const;
  144. void SetIsInputConst(const vector<bool> &is_input_const);
  145. vector<bool> GetIsInputConst() const;
  146. void SetOpInferDepends(const vector<string> &depend_names);
  147. vector<string> GetOpInferDepends() const;
  148. string GetInputNameByIndex(uint32_t index) const;
  149. int GetInputIndexByName(const string &name) const;
  150. string GetOutputNameByIndex(uint32_t index) const;
  151. int GetOutputIndexByName(const string &name) const;
  152. graphStatus RestoreInputNameIdx(const string &name, const int &index);
  153. graphStatus RestoreOutputNameIdx(const string &name, const int &index);
  154. graphStatus CallInferFunc(Operator &op);
  155. void SetOpKernelLibName(const std::string &name);
  156. std::string GetOpKernelLibName() const;
  157. void SetOpEngineName(const std::string &name);
  158. std::string GetOpEngineName() const;
  159. void RegisterSubgraphIrName(const std::string &name, SubgraphType type);
  160. const std::map<std::string, SubgraphType> &GetSubgraphIrNames() const;
  161. SubgraphType GetSubgraphTypeByIrName(const std::string &name) const;
  162. graphStatus AddSubgraphName(const std::string &name);
  163. const std::map<std::string, uint32_t> &GetSubgraphNameIndexes() const;
  164. std::string GetSubgraphInstanceName(uint32_t index) const;
  165. const std::vector<std::string> &GetSubgraphInstanceNames() const;
  166. /// Does not provide functions `AddSubgraphInstance` or `AppendSubgraphInstance`,
  167. /// because this kind of functions will only append a new subgraph instance name
  168. /// at the tail of `subgraph_instance_names_` and ignore the synchronous change of `subgraph_names_to_index_`.
  169. /// If we want to append a new subgraph instance name, the function `AddSubgraphName` should be called first.
  170. /// \param index
  171. /// \param name
  172. /// \return
  173. graphStatus SetSubgraphInstanceName(uint32_t index, const std::string &name);
  174. void RemoveSubgraphInstanceName(const std::string &name);
  175. graphStatus GetSubgraphNameByInstanceName(const std::string &instance_name, std::string &subgraph_name) const;
  176. protected:
  177. ProtoAttrMapHelper MutableAttrMap() override;
  178. ConstProtoAttrMapHelper GetAttrMap() const override;
  179. private:
  180. OpDesc(const ProtoMsgOwner &proto_msg_owner, ge::proto::OpDef *op_def);
  181. bool OpDescMembersAreEqual(const OpDesc &r_op_desc) const;
  182. bool OpDescAttrsAreEqual(const OpDesc &r_op_desc) const;
  183. bool OpDescGenTensorDescsAreEqual(const OpDesc &r_op_desc) const;
  184. GeIrProtoHelper<ge::proto::OpDef> op_def_;
  185. std::vector<std::string> subgraph_instance_names_;
  186. // subgraph names to index, for a `if` operator:
  187. // then_branch: 0
  188. // else_branch: 1
  189. // or for a `case` node:
  190. // branches0: 0
  191. // branches1: 1
  192. // branches2: 2
  193. std::map<std::string, uint32_t> subgraph_names_to_index_;
  194. // subgraph ir names to type, for a `if` operator:
  195. // then_branch: static
  196. // else_branch: static
  197. // or for a `case` op:
  198. // branches: dynamic
  199. std::map<std::string, SubgraphType> subgraph_ir_names_to_type_;
  200. vector<GeTensorDescPtr> inputs_desc_{};
  201. vector<GeTensorDescPtr> outputs_desc_{};
  202. map<string, uint32_t> output_name_idx_{};
  203. std::function<graphStatus(Operator &)> infer_func_ = nullptr;
  204. std::function<graphStatus(Operator &)> infer_format_func_ = nullptr;
  205. std::function<graphStatus(Operator &)> verifier_func_ = nullptr;
  206. string op_kernel_lib_name_;
  207. string engine_name_;
  208. friend class OpDescUtils;
  209. friend class ModelSerializeImp;
  210. friend class AttrUtils;
  211. friend class GeAttrValueImp;
  212. friend class OnnxUtils;
  213. };
  214. } // namespace ge
  215. #endif // INC_GRAPH_OP_DESC_H_

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