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.4 kB

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

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