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

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

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