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.

operator.h 9.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_EXTERNAL_GRAPH_OPERATOR_H_
  17. #define INC_EXTERNAL_GRAPH_OPERATOR_H_
  18. #include <functional>
  19. #include <map>
  20. #include <memory>
  21. #include <string>
  22. #include <vector>
  23. #include "external/graph/ge_error_codes.h"
  24. #include "external/graph//inference_context.h"
  25. #include "external/graph//tensor.h"
  26. #include "external/graph//usr_types.h"
  27. #ifndef USER_GE_LOGI
  28. #define USER_GE_LOGI(...)
  29. #endif // USER_GE_LOGI
  30. #ifndef USER_GE_LOGW
  31. #define USER_GE_LOGW(...)
  32. #endif // USER_GE_LOGW
  33. #ifndef USER_GE_LOGE
  34. #define USER_GE_LOGE(...)
  35. #endif // USER_GE_LOGE
  36. #define DYNAMIC_OUTPUT_TD_NUM(name) ("__dynamic_output_" + name + "_cnt")
  37. #define DYNAMIC_INPUT_TD_NUM(name) ("__dynamic_input_" + name + "_cnt")
  38. namespace ge {
  39. class OperatorImpl;
  40. class AttrValue;
  41. using OperatorImplPtr = std::shared_ptr<OperatorImpl>;
  42. class OpIO;
  43. using OutHandler = std::shared_ptr<OpIO>;
  44. using InHandler = std::shared_ptr<OpIO>;
  45. using std::function;
  46. using std::shared_ptr;
  47. using std::string;
  48. class GE_FUNC_DEV_VISIBILITY GE_FUNC_HOST_VISIBILITY Operator {
  49. public:
  50. friend class OperatorImpl;
  51. friend class GraphBuilderImpl;
  52. using OpInt = int64_t;
  53. using OpFloat = float;
  54. using OpString = string;
  55. using OpBool = bool;
  56. using OpTensor = Tensor;
  57. using OpType = ge::DataType;
  58. using OpListInt = std::vector<int64_t>;
  59. using OpListFloat = std::vector<float>;
  60. using OpListString = std::vector<string>;
  61. using OpListBool = std::vector<bool>;
  62. using OpListTensor = std::vector<Tensor>;
  63. using OpBytes = std::vector<uint8_t>;
  64. using OpListListInt = std::vector<std::vector<int64_t>>;
  65. using OpListType = std::vector<ge::DataType>;
  66. Operator() {}
  67. explicit Operator(const string &type);
  68. Operator(const string &name, const string &type);
  69. virtual ~Operator() = default;
  70. bool IsEmpty() const;
  71. string GetName() const;
  72. string GetOpType() const;
  73. // Only has one output index = 0
  74. Operator &SetInput(const string &dst_name, const Operator &src_oprt);
  75. Operator &SetInput(const string &dst_name, const Operator &src_oprt, const string &name);
  76. Operator &AddControlInput(const Operator &src_oprt);
  77. graphStatus GetInputConstData(const string &dst_name, Tensor &data) const;
  78. TensorDesc GetInputDesc(const string &name) const;
  79. TensorDesc GetInputDesc(uint32_t index) const;
  80. int GetDynamicOutputNum(const string &name) const;
  81. int GetDynamicInputNum(const string &name) const;
  82. graphStatus TryGetInputDesc(const string &name, TensorDesc &tensor_desc) const;
  83. graphStatus UpdateInputDesc(const string &name, const TensorDesc &tensor_desc);
  84. TensorDesc GetOutputDesc(const string &name) const;
  85. TensorDesc GetOutputDesc(uint32_t index) const;
  86. graphStatus UpdateOutputDesc(const string &name, const TensorDesc &tensor_desc);
  87. TensorDesc GetDynamicInputDesc(const string &name, uint32_t index) const;
  88. graphStatus UpdateDynamicInputDesc(const string &name, uint32_t index, const TensorDesc &tensor_desc);
  89. TensorDesc GetDynamicOutputDesc(const string &name, uint32_t index) const;
  90. graphStatus UpdateDynamicOutputDesc(const string &name, uint32_t index, const TensorDesc &tensor_desc);
  91. graphStatus InferShapeAndType();
  92. void SetInferenceContext(const InferenceContextPtr &inference_context);
  93. InferenceContextPtr GetInferenceContext() const;
  94. graphStatus VerifyAllAttr(bool disable_common_verifier = false);
  95. size_t GetInputsSize() const;
  96. size_t GetOutputsSize() const;
  97. const std::map<std::string, std::string> GetAllAttrNamesAndTypes() const;
  98. Operator &SetAttr(const string &name, int64_t attr_value);
  99. Operator &SetAttr(const string &name, int32_t attr_value);
  100. Operator &SetAttr(const string &name, uint32_t attr_value);
  101. graphStatus GetAttr(const string &name, int64_t &attr_value) const;
  102. graphStatus GetAttr(const string &name, int32_t &attr_value) const;
  103. graphStatus GetAttr(const string &name, uint32_t &attr_value) const;
  104. Operator &SetAttr(const string &name, const std::vector<int64_t> &attr_value);
  105. Operator &SetAttr(const string &name, const std::vector<int32_t> &attr_value);
  106. Operator &SetAttr(const string &name, const std::vector<uint32_t> &attr_value);
  107. Operator &SetAttr(const string &name, std::initializer_list<int64_t> &&attr_value);
  108. graphStatus GetAttr(const string &name, std::vector<int64_t> &attr_value) const;
  109. graphStatus GetAttr(const string &name, std::vector<int32_t> &attr_value) const;
  110. graphStatus GetAttr(const string &name, std::vector<uint32_t> &attr_value) const;
  111. Operator &SetAttr(const string &name, float attr_value);
  112. graphStatus GetAttr(const string &name, float &attr_value) const;
  113. Operator &SetAttr(const string &name, const std::vector<float> &attr_value);
  114. graphStatus GetAttr(const string &name, std::vector<float> &attr_value) const;
  115. Operator &SetAttr(const string &name, AttrValue &&attr_value);
  116. graphStatus GetAttr(const string &name, AttrValue &attr_value) const;
  117. Operator &SetAttr(const string &name, const string &attr_value);
  118. graphStatus GetAttr(const string &name, string &attr_value) const;
  119. Operator &SetAttr(const string &name, const std::vector<string> &attr_value);
  120. graphStatus GetAttr(const string &name, std::vector<string> &attr_value) const;
  121. Operator &SetAttr(const string &name, bool attr_value);
  122. graphStatus GetAttr(const string &name, bool &attr_value) const;
  123. Operator &SetAttr(const string &name, const std::vector<bool> &attr_value);
  124. graphStatus GetAttr(const string &name, std::vector<bool> &attr_value) const;
  125. Operator &SetAttr(const string &name, const Tensor &attr_value);
  126. graphStatus GetAttr(const string &name, Tensor &attr_value) const;
  127. Operator &SetAttr(const string &name, const std::vector<Tensor> &attr_value);
  128. graphStatus GetAttr(const string &name, std::vector<Tensor> &attr_value) const;
  129. // Bytes type
  130. Operator &SetAttr(const string &name, const OpBytes &attr_value);
  131. // Bytes type
  132. graphStatus GetAttr(const string &name, OpBytes &attr_value) const;
  133. Operator &SetAttr(const string &name, const UsrQuantizeFactorParams &attr_value);
  134. graphStatus GetAttr(const string &name, UsrQuantizeFactorParams &attr_value) const;
  135. Operator &SetAttr(const string &name, const std::vector<std::vector<int64_t>> &attr_value);
  136. graphStatus GetAttr(const string &name, std::vector<std::vector<int64_t>> &attr_value) const;
  137. Operator &SetAttr(const string &name, const std::vector<ge::DataType> &attr_value);
  138. graphStatus GetAttr(const string &name, std::vector<ge::DataType> &attr_value) const;
  139. Operator &SetAttr(const string &name, const ge::DataType &attr_value);
  140. graphStatus GetAttr(const string &name, ge::DataType &attr_value) const;
  141. void BreakConnect() const;
  142. protected:
  143. void AttrRegister(const string &name, float attr_value);
  144. void AttrRegister(const string &name, const std::vector<float> &attr_value);
  145. void AttrRegister(const string &name, int64_t attr_value);
  146. void AttrRegister(const string &name, const std::vector<int64_t> &attr_value);
  147. void AttrRegister(const string &name, const string &attr_value);
  148. void AttrRegister(const string &name, const std::vector<string> &attr_value);
  149. void AttrRegister(const string &name, bool attr_value);
  150. void AttrRegister(const string &name, const std::vector<bool> &attr_value);
  151. void AttrRegister(const string &name, const Tensor &attr_value);
  152. void AttrRegister(const string &name, const std::vector<Tensor> &attr_value);
  153. void AttrRegister(const string &name, const OpBytes &attr_value);
  154. void AttrRegister(const string &name, const std::vector<std::vector<int64_t>> &attr_value);
  155. void AttrRegister(const string &name, const std::vector<ge::DataType> &attr_value);
  156. void AttrRegister(const string &name, const ge::DataType &attr_value);
  157. explicit Operator(OperatorImplPtr &&op_impl);
  158. void InputRegister(const string &name);
  159. void OptionalInputRegister(const string &name);
  160. void InferFuncRegister(const std::function<graphStatus(Operator &)> &func);
  161. void VerifierFuncRegister(const std::function<graphStatus(Operator &)> &func);
  162. void InferFormatFuncRegister(const std::function<graphStatus(Operator &)> &func);
  163. void OutputRegister(const string &name);
  164. void DynamicInputRegister(const string &name, const unsigned int num, bool is_push_back = true);
  165. void DynamicOutputRegister(const string &name, const unsigned int num, bool is_push_back = true);
  166. void RequiredAttrRegister(const string &name);
  167. graphStatus VerifyAll();
  168. // Only has one output index = 0
  169. Operator &SetInput(const string &dst_name, uint32_t dst_index,
  170. const Operator &src_oprt);
  171. Operator &SetInput(const string &dst_name, uint32_t dst_index, const Operator &src_oprt,
  172. const string &name);
  173. private:
  174. Operator &SetInput(const string &dst_name, const OutHandler &out_handler);
  175. OutHandler GetOutput(const string &name) const;
  176. OperatorImplPtr GetOperatorImplPtr() const;
  177. OperatorImplPtr operator_impl_{nullptr};
  178. graphStatus GetInputConstDataOut(const string &dst_name, Tensor &data) const;
  179. };
  180. } // namespace ge
  181. #endif // INC_EXTERNAL_GRAPH_OPERATOR_H_

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

Contributors (1)