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 8.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. class OpDesc : public std::enable_shared_from_this<OpDesc>, public AttrHolder {
  44. public:
  45. template <class T>
  46. using Vistor = RangeVistor<T, shared_ptr<ConstOpDesc>>;
  47. friend class GraphBuilderImpl;
  48. friend class OperatorImpl;
  49. OpDesc(const string &name, const string &type);
  50. OpDesc();
  51. ~OpDesc();
  52. bool operator==(const OpDesc &r_op_desc) const;
  53. string GetName() const;
  54. void SetName(const string &name);
  55. string GetType() const;
  56. void SetType(const string &type);
  57. graphStatus AddInputDesc(const GeTensorDesc &input_desc);
  58. graphStatus AddInputDesc(const string &name, const GeTensorDesc &input_desc);
  59. graphStatus AddInputDesc(uint32_t index, const ge::GeTensorDesc &input_desc);
  60. graphStatus AddInputDescForward(const string &name, const unsigned int num);
  61. graphStatus AddOutputDescForward(const string &name, const unsigned int num);
  62. graphStatus AddOptionalInputDesc(const string &name, const GeTensorDesc &input_desc);
  63. graphStatus UpdateInputDesc(uint32_t index, const GeTensorDesc &tensor_desc);
  64. graphStatus UpdateInputDesc(const string &name, const GeTensorDesc &tensor_desc);
  65. bool InputIsSet(const string &name) const;
  66. GeTensorDesc GetInputDesc(uint32_t index) const;
  67. GeTensorDesc GetInputDesc(const string &name) const;
  68. Vistor<string> GetAllInputNames() const;
  69. GeTensorDescPtr MutableInputDesc(uint32_t index) const;
  70. Vistor<GeTensorDesc> GetAllInputsDesc() const;
  71. Vistor<GeTensorDescPtr> GetAllInputsDescPtr() const;
  72. size_t GetInputsSize() const;
  73. size_t GetAllInputsSize() const;
  74. graphStatus AddOutputDesc(const GeTensorDesc &output_desc);
  75. graphStatus AddOutputDesc(const string &name, const GeTensorDesc &output_desc);
  76. graphStatus UpdateOutputDesc(uint32_t index, const GeTensorDesc &tensor_desc);
  77. graphStatus UpdateOutputDesc(const string &name, const GeTensorDesc &tensor_desc);
  78. GeTensorDesc GetOutputDesc(uint32_t index) const;
  79. GeTensorDesc GetOutputDesc(const string &name) const;
  80. GeTensorDescPtr MutableOutputDesc(uint32_t index) const;
  81. uint32_t GetAllOutputsDescSize() const;
  82. Vistor<GeTensorDesc> GetAllOutputsDesc() const;
  83. Vistor<GeTensorDescPtr> GetAllOutputsDescPtr() const;
  84. size_t GetOutputsSize() const;
  85. ConstGeTensorDescPtr GetOutputDescPtr(uint32_t index) const;
  86. ConstGeTensorDescPtr GetInputDescPtr(uint32_t index) const;
  87. ConstGeTensorDescPtr GetInputDescPtrDfault(uint32_t index) const;
  88. ConstGeTensorDescPtr GetInputDescPtr(const string &name) const;
  89. graphStatus AddDynamicInputDesc(const string &name, const unsigned int num, bool isPushBack = true);
  90. graphStatus AddDynamicOutputDesc(const string &name, const unsigned int num, bool isPushBack = true);
  91. bool IsOptionalInput(const string &name) const;
  92. bool IsOptionalInput(uint32_t index) const;
  93. std::map<string, uint32_t> GetAllInputName() const;
  94. void SetAllInputName(const std::map<string, uint32_t> &input_name_idx);
  95. std::vector<string> GetAllOptionalInputName() const;
  96. std::map<string, uint32_t> GetAllOutputName();
  97. bool UpdateInputName(std::map<string, uint32_t> inputNameIdx);
  98. bool UpdateOutputName(std::map<string, uint32_t> outputNameIdx);
  99. void AddInferFunc(const std::function<graphStatus(Operator &)> &func);
  100. std::function<graphStatus(Operator &)> GetInferFunc() const;
  101. graphStatus InferShapeAndType();
  102. void AddInferFormatFunc(const std::function<graphStatus(Operator &)> &func);
  103. std::function<graphStatus(Operator &)> GetInferFormatFunc() const;
  104. graphStatus DefaultInferFormat();
  105. std::function<graphStatus(Operator &)> GetVerifyFunc() const;
  106. void AddVerifierFunc(const std::function<graphStatus(Operator &)> &func);
  107. graphStatus CallInferFormatFunc(Operator &op);
  108. graphStatus OpVerify();
  109. graphStatus CommonVerify() const;
  110. using AttrHolder::AddRequiredAttr;
  111. using AttrHolder::DelAttr;
  112. using AttrHolder::GetAllAttrNames;
  113. using AttrHolder::GetAllAttrs;
  114. using AttrHolder::GetAttr;
  115. using AttrHolder::HasAttr;
  116. using AttrHolder::SetAttr;
  117. void SetId(int64_t id);
  118. int64_t GetId() const;
  119. void SetStreamId(int64_t stream_id);
  120. int64_t GetStreamId() const;
  121. void SetInputName(const vector<string> &input_name);
  122. vector<string> GetInputName() const;
  123. void SetSrcName(const vector<string> &src_name);
  124. vector<string> GetSrcName() const;
  125. void SetSrcIndex(const vector<int64_t> &src_index);
  126. vector<int64_t> GetSrcIndex() const;
  127. void SetInputOffset(const vector<int64_t> &input);
  128. vector<int64_t> GetInputOffset() const;
  129. void SetOutputOffset(const vector<int64_t> &input);
  130. vector<int64_t> GetOutputOffset() const;
  131. void SetDstName(const vector<string> &dst_name);
  132. vector<string> GetDstName() const;
  133. void SetDstIndex(const vector<int64_t> &dst_index);
  134. vector<int64_t> GetDstIndex() const;
  135. void SetWorkspace(const vector<int64_t> &workspace);
  136. vector<int64_t> GetWorkspace() const;
  137. void SetWorkspaceBytes(const vector<int64_t> &workspace_bytes);
  138. vector<int64_t> GetWorkspaceBytes() const;
  139. void SetIsInputConst(const vector<bool> &is_input_const);
  140. vector<bool> GetIsInputConst() const;
  141. string GetInputNameByIndex(uint32_t index) const;
  142. int GetInputIndexByName(const string &name) const;
  143. string GetOutputNameByIndex(uint32_t index) const;
  144. int GetOutputIndexByName(const string &name) const;
  145. graphStatus RestoreInputNameIdx(const string &name, const int &index);
  146. graphStatus RestoreOutputNameIdx(const string &name, const int &index);
  147. graphStatus CallInferFunc(Operator &op);
  148. void SetOpKernelLibName(const std::string &name);
  149. std::string GetOpKernelLibName() const;
  150. void SetOpEngineName(const std::string &name);
  151. std::string GetOpEngineName() const;
  152. graphStatus AddSubgraphName(const std::string &name);
  153. const std::map<std::string, uint32_t> &GetSubgraphNameIndexes() const;
  154. std::string GetSubgraphInstanceName(uint32_t index) const;
  155. const std::vector<std::string> &GetSubgraphInstanceNames() const;
  156. void AddSubgraphInstanceName(std::string name);
  157. void RemoveSubgraphInstanceName(const std::string &name);
  158. protected:
  159. ProtoAttrMapHelper MutableAttrMap() override;
  160. ConstProtoAttrMapHelper GetAttrMap() const override;
  161. private:
  162. OpDesc(const ProtoMsgOwner &proto_msg_owner, ge::proto::OpDef *op_def);
  163. bool OpDescMembersAreEqual(const OpDesc &r_op_desc) const;
  164. bool OpDescAttrsAreEqual(const OpDesc &r_op_desc) const;
  165. bool OpDescGenTensorDescsAreEqual(const OpDesc &r_op_desc) const;
  166. GeIrProtoHelper<ge::proto::OpDef> op_def_;
  167. std::vector<std::string> subgraph_instance_names_;
  168. std::map<std::string, uint32_t> subgraph_names_to_index_;
  169. vector<GeTensorDescPtr> inputs_desc_{};
  170. vector<GeTensorDescPtr> outputs_desc_{};
  171. map<string, uint32_t> output_name_idx_{};
  172. std::function<graphStatus(Operator &)> infer_func_ = nullptr;
  173. std::function<graphStatus(Operator &)> infer_format_func_ = nullptr;
  174. std::function<graphStatus(Operator &)> verifier_func_ = nullptr;
  175. string op_kernel_lib_name_;
  176. string engine_name_;
  177. friend class OpDescUtils;
  178. friend class ModelSerializeImp;
  179. friend class AttrUtils;
  180. friend class GeAttrValueImp;
  181. friend class OnnxUtils;
  182. };
  183. } // namespace ge
  184. #endif // INC_GRAPH_OP_DESC_H_

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