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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. graphStatus AddOutputDesc(const GeTensorDesc &output_desc);
  74. graphStatus AddOutputDesc(const string &name, const GeTensorDesc &output_desc);
  75. graphStatus UpdateOutputDesc(uint32_t index, const GeTensorDesc &tensor_desc);
  76. graphStatus UpdateOutputDesc(const string &name, const GeTensorDesc &tensor_desc);
  77. GeTensorDesc GetOutputDesc(uint32_t index) const;
  78. GeTensorDesc GetOutputDesc(const string &name) const;
  79. GeTensorDescPtr MutableOutputDesc(uint32_t index) const;
  80. Vistor<GeTensorDesc> GetAllOutputsDesc() const;
  81. Vistor<GeTensorDescPtr> GetAllOutputsDescPtr() const;
  82. size_t GetOutputsSize() const;
  83. ConstGeTensorDescPtr GetOutputDescPtr(uint32_t index) const;
  84. ConstGeTensorDescPtr GetInputDescPtr(uint32_t index) const;
  85. graphStatus AddDynamicInputDesc(const string &name, const unsigned int num, bool isPushBack = true);
  86. graphStatus AddDynamicOutputDesc(const string &name, const unsigned int num, bool isPushBack = true);
  87. bool IsOptionalInput(const string &name) const;
  88. bool IsOptionalInput(uint32_t index) const;
  89. std::map<string, uint32_t> GetAllInputName();
  90. std::map<string, uint32_t> GetAllOutputName();
  91. bool UpdateInputName(std::map<string, uint32_t> inputNameIdx);
  92. bool UpdateOutputName(std::map<string, uint32_t> outputNameIdx);
  93. void AddInferFunc(const std::function<graphStatus(Operator &)> &func);
  94. std::function<graphStatus(Operator &)> GetInferFunc() const;
  95. graphStatus InferShapeAndType();
  96. void AddInferFormatFunc(const std::function<graphStatus(Operator &)> &func);
  97. std::function<graphStatus(Operator &)> GetInferFormatFunc() const;
  98. graphStatus DefaultInferFormat();
  99. std::function<graphStatus(Operator &)> GetVerifyFunc() const;
  100. void AddVerifierFunc(const std::function<graphStatus(Operator &)> &func);
  101. graphStatus CallInferFormatFunc(Operator &op);
  102. graphStatus OpVerify();
  103. graphStatus CommonVerify() const;
  104. using AttrHolder::AddRequiredAttr;
  105. using AttrHolder::DelAttr;
  106. using AttrHolder::GetAllAttrNames;
  107. using AttrHolder::GetAllAttrs;
  108. using AttrHolder::GetAttr;
  109. using AttrHolder::HasAttr;
  110. using AttrHolder::SetAttr;
  111. void SetId(int64_t id);
  112. int64_t GetId() const;
  113. void SetStreamId(int64_t stream_id);
  114. int64_t GetStreamId() const;
  115. void SetInputName(const vector<string> &input_name);
  116. vector<string> GetInputName() const;
  117. void SetSrcName(const vector<string> &src_name);
  118. vector<string> GetSrcName() const;
  119. void SetSrcIndex(const vector<int64_t> &src_index);
  120. vector<int64_t> GetSrcIndex() const;
  121. void SetInputOffset(const vector<int64_t> &input);
  122. vector<int64_t> GetInputOffset() const;
  123. void SetOutputOffset(const vector<int64_t> &input);
  124. vector<int64_t> GetOutputOffset() const;
  125. void SetDstName(const vector<string> &dst_name);
  126. vector<string> GetDstName() const;
  127. void SetDstIndex(const vector<int64_t> &dst_index);
  128. vector<int64_t> GetDstIndex() const;
  129. void SetWorkspace(const vector<int64_t> &workspace);
  130. vector<int64_t> GetWorkspace() const;
  131. void SetWorkspaceBytes(const vector<int64_t> &workspace_bytes);
  132. vector<int64_t> GetWorkspaceBytes() const;
  133. void SetIsInputConst(const vector<bool> &is_input_const);
  134. vector<bool> GetIsInputConst() const;
  135. string GetInputNameByIndex(uint32_t index) const;
  136. int GetInputIndexByName(const string &name) const;
  137. string GetOutputNameByIndex(uint32_t index) const;
  138. int GetOutputIndexByName(const string &name) const;
  139. graphStatus RestoreInputNameIdx(const string &name, const int &index);
  140. graphStatus RestoreOutputNameIdx(const string &name, const int &index);
  141. graphStatus CallInferFunc(Operator &op);
  142. void SetOpKernelLibName(const std::string &name);
  143. std::string GetOpKernelLibName() const;
  144. void SetOpEngineName(const std::string &name);
  145. std::string GetOpEngineName() const;
  146. protected:
  147. ProtoAttrMapHelper MutableAttrMap() override;
  148. ConstProtoAttrMapHelper GetAttrMap() const override;
  149. private:
  150. OpDesc(const ProtoMsgOwner &proto_msg_owner, ge::proto::OpDef *op_def);
  151. bool OpDescMembersAreEqual(const OpDesc &r_op_desc) const;
  152. bool OpDescAttrsAreEqual(const OpDesc &r_op_desc) const;
  153. bool OpDescGenTensorDescsAreEqual(const OpDesc &r_op_desc) const;
  154. GeIrProtoHelper<ge::proto::OpDef> op_def_;
  155. vector<GeTensorDescPtr> inputs_desc_{};
  156. map<string, uint32_t> input_name_idx_{};
  157. std::unordered_set<string> optional_input_names_{};
  158. vector<GeTensorDescPtr> outputs_desc_{};
  159. map<string, uint32_t> output_name_idx_{};
  160. std::function<graphStatus(Operator &)> infer_func_ = nullptr;
  161. std::function<graphStatus(Operator &)> infer_format_func_ = nullptr;
  162. std::function<graphStatus(Operator &)> verifier_func_ = nullptr;
  163. string op_kernel_lib_name_;
  164. string engine_name_;
  165. friend class OpDescUtils;
  166. friend class ModelSerializeImp;
  167. friend class AttrUtils;
  168. friend class GeAttrValueImp;
  169. friend class OnnxUtils;
  170. };
  171. } // namespace ge
  172. #endif // INC_GRAPH_OP_DESC_H_

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

Contributors (1)