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

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