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.

ge_op_utils.h 13 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_FRAMEWORK_COMMON_OP_GE_OP_UTILS_H_
  17. #define INC_FRAMEWORK_COMMON_OP_GE_OP_UTILS_H_
  18. #include <cce/dnn.h>
  19. #include <memory>
  20. #include <vector>
  21. #include "common/op/attr_value_util.h"
  22. #include "common/types.h"
  23. #include "common/util.h"
  24. #include "graph/attr_value.h"
  25. #include "graph/ge_tensor.h"
  26. #include "graph/node.h"
  27. #include "graph/op_desc.h"
  28. #include "proto/insert_op.pb.h"
  29. namespace ge {
  30. using namespace cce;
  31. using domi::Status;
  32. // Add Sub Mul
  33. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t ADD_INPUT_NUM;
  34. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t SUB_INPUT_NUM;
  35. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t MUL_INPUT_NUM;
  36. // Permute
  37. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const int32_t PERMUTE_ORDER_NUM;
  38. // Ssd PriroBox
  39. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const double SSD_PRIORBOX_ASPECT_RATIO_VALUE;
  40. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t STRIDEDSLICE_INPUT_NUM;
  41. // Switch
  42. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t SWITCH_INPUT_NUM;
  43. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t SWITCH_OUTPUT_NUM;
  44. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t SWITCH_FALSE_OUTPUT;
  45. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t SWITCH_TRUE_OUTPUT;
  46. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t SWITCH_DATA_INPUT;
  47. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t SWITCH_PRED_INPUT;
  48. // FunctionOp
  49. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t IF_COND_INPUT;
  50. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t FOR_START_INPUT;
  51. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t FOR_LIMIT_INPUT;
  52. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t FOR_DELTA_INPUT;
  53. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY extern const uint32_t FOR_DATA_INPUT;
  54. class OpUtils {
  55. public:
  56. ///
  57. /// @ingroup domi_ome
  58. /// @brief Check whether check_value is in [min_enum_value, max_enum_value]
  59. /// @return true Within
  60. /// @return false out of range
  61. //
  62. static inline bool CheckEnumValid(int32_t check_value, int32_t min_enum_value, int32_t max_enum_value) {
  63. return check_value < min_enum_value ? false : (check_value >= max_enum_value ? false : true);
  64. }
  65. ///
  66. /// @ingroup domi_omg
  67. /// @brief Convert the dimension of array according to different format
  68. /// @param [in] src_format src_shape format
  69. /// @param [in] src Dimension array to be converted
  70. /// @param [in] dst_format Target format after conversion
  71. /// @param [out] dst Dimension array after conversion
  72. /// @return SUCCESS success
  73. /// @return FAILED fail
  74. ///
  75. static bool ConvertDim(ccTensorFormat_t src_format, const std::vector<int64_t> &src, ccTensorFormat_t dst_format,
  76. std::vector<int64_t> &dst);
  77. ///
  78. /// @ingroup domi_omg
  79. /// @brief Determine whether to manually calculate the tensor size based on the values of format and dim
  80. /// @param [in] format, Format information of the tensor
  81. /// @param [in] real_dim_cnt, Tensor dim
  82. /// @return true Manually calculate the size based on dim and datatype
  83. /// @return false skip
  84. ///
  85. static bool IsComputDimsSize(const int32_t format, const uint32_t real_dim_cnt);
  86. ///
  87. /// @ingroup domi_ome
  88. /// @brief Initialize the tensor description, which is used for input and output.
  89. /// @param [in] model_tensor Tensor information defined by the offline model
  90. /// @param [out] cc_tensor Tensor definition used by CC
  91. /// @return SUCCESS success
  92. /// @return FAILED fail
  93. ///
  94. static Status InitTensorDescriptor(const ge::GeTensorDesc &model_tensor, ccTensorDescriptor_t &cc_tensor);
  95. ///
  96. /// @ingroup domi_ome
  97. /// @brief Initialize the tensor description, which is used for input and output.
  98. /// @param [in] model_tensor Tensor information defined by the offline model
  99. /// @param [in] dst_data_type data_type of the target cc_tensor
  100. /// @param [out] cc_tensor Tensor definition used by CC
  101. /// @return SUCCESS success
  102. /// @return FAILED fail
  103. ///
  104. static Status InitTensorDescriptor(const ge::GeTensorDesc &model_tensor, int32_t dst_data_type,
  105. ccTensorDescriptor_t &cc_tensor);
  106. ///
  107. /// @ingroup domi_ome
  108. /// @brief Initialize the tensor description for bias.
  109. /// @param [in] model_tensor Tensor information defined by the offline model
  110. /// @param [out] cc_tensor Tensor definition used by CC
  111. /// @return SUCCESS success
  112. /// @return FAILED fail
  113. ///
  114. ///
  115. static Status InitTensorDescriptor(const ge::GeTensor &model_tensor, ccTensorDescriptor_t &cc_tensor);
  116. ///
  117. /// @ingroup domi_ome
  118. /// @brief Initialize the tensor description for bias.
  119. /// @param [in] model_tensor Tensor information defined by the offline model
  120. /// @param [in] dst_data_type data_type of the target cc_tensor
  121. /// @param [out] cc_tensor Tensor definition used by CC
  122. /// @return SUCCESS success
  123. /// @return FAILED fail
  124. ///
  125. static Status InitTensorDescriptor(const ge::GeTensor &model_tensor, int32_t dst_data_type,
  126. ccTensorDescriptor_t &cc_tensor);
  127. static Status InitTensorDescriptor(int32_t format, int32_t data_type, const std::vector<int64_t> &dim,
  128. ccTensorDescriptor_t &cc_tensor, uint32_t real_dim_cnt = 4);
  129. ///
  130. /// @ingroup domi_ome
  131. /// @brief Destroys a tensor
  132. /// @param [inout] cc_tensor Tensor definition used by CC
  133. ///
  134. static void DestroyTensorDescriptor(ccTensorDescriptor_t &cc_tensor) noexcept;
  135. ///
  136. /// @ingroup domi_ome
  137. /// @brief Destroys a tensor
  138. /// @param [inout] cc_filter cc_filter Definition of the filter used by CC
  139. ///
  140. static void DestroyFilterDescriptor(ccFilterDescriptor_t &cc_filter);
  141. ///
  142. /// @ingroup domi_ome
  143. /// @brief Initializing Filter Description
  144. /// @param [in] model_filter Filter information defined in the offline model
  145. /// @param [out] cc_filter Definition of the filter used by CC
  146. /// @return SUCCESS success
  147. /// @return FAILED fail
  148. ///
  149. static Status InitFilterDescriptor(const ge::GeTensor &model_filter, ccFilterDescriptor_t &cc_filter);
  150. ///
  151. /// @brief Extract AIPP parameters from AttrDefMap and splice them
  152. /// @param [in] aipp_attr attr of operator
  153. /// @param [out] aipp_params aipp parameters
  154. /// @return enum of tagCCAippInputFormat
  155. ///
  156. static Status ConvertAippParams(const GeAttrValue::NamedAttrs &aipp_attr, domi::AippOpParams *aipp_params);
  157. static Status TransferDim(const std::vector<int64_t> &dim, std::vector<int64_t> &dim_vector);
  158. template <typename T>
  159. static void SliceData(const std::vector<char *> &input, int64_t chunk_size, std::vector<char *> &output,
  160. int64_t begin, int64_t out_dim, int64_t stride);
  161. template <typename T>
  162. static Status SetDataByDataType(size_t out_size, const std::vector<char *> &chunk_input,
  163. const std::vector<char *> &chunk_output, GeTensor *output);
  164. template <typename T>
  165. static Status SetOutputSliceDataByDataType(void *data, int64_t data_size, const std::vector<int64_t> &input_dims,
  166. const std::vector<int64_t> &begin, const std::vector<int64_t> &output_dims,
  167. ge::GeTensor *output, const std::vector<int64_t> &stride);
  168. static Status SetOutputSliceData(void *data, int64_t data_size, int32_t data_type, std::vector<int64_t> &input_dims,
  169. std::vector<int64_t> &begin, std::vector<int64_t> &output_dims, ge::GeTensor *output,
  170. std::vector<int64_t> &stride);
  171. ///
  172. /// @ingroup domi_omg
  173. /// @brief Convert the convolutional weight data from [h, w, c, k] to [k, c, h, w]
  174. /// @param [in] input Weight data in HWCK format
  175. /// @param [in] H value of H dimension
  176. /// @param [in] W value of W dimension
  177. /// @param [in] C value of C dimension
  178. /// @param [in] K value of K dimension
  179. /// @param [out] output Data pointer after conversion. The format is KCHW.
  180. ///
  181. static void TransDataHWCK2KCHW(const void *input, int64_t H, int64_t W, int64_t C, int64_t K, void **output);
  182. ///
  183. /// @ingroup domi_omg
  184. /// @brief Converts the convolutional weight data from [k, c, h, w] to [h, w, c, k].
  185. /// @param [in] input Weight data in HWCK format
  186. /// @param [in] K value of K dimension
  187. /// @param [in] C value of C dimension
  188. /// @param [in] H value of H dimension
  189. /// @param [in] W value of W dimension
  190. /// @param [out] output Data pointer after conversion. The format is HWCK
  191. ///
  192. static void TransDataKCHW2HWCK(const void *input, int64_t K, int64_t C, int64_t H, int64_t W, void *output);
  193. ///
  194. /// @ingroup domi_omg
  195. /// @brief Initialize the input and output description of the data node which is applied to filter weight in the
  196. /// training network
  197. /// @param [in] model_tensor input and output tensor information
  198. /// @param [out] cc_tensor Tensor in CCE format after conversion
  199. ///
  200. static Status InitFilterTensorDescriptor(const ge::GeTensorDesc &model_tensor, ccFilterDescriptor_t &cc_tensor);
  201. static void SetTensorDescriptorAllOffsetQuantizeInfo(const GeTensorDesc &tensor, ccTensorDescriptor_t cc_tensor);
  202. static vector<ConstGeTensorPtr> GetWeights(const ge::Node &node);
  203. static vector<ConstGeTensorPtr> GetWeights(ge::ConstNodePtr node);
  204. static vector<GeTensorPtr> MutableWeights(const ge::Node &node);
  205. static vector<GeTensorPtr> MutableWeights(const ge::NodePtr node);
  206. static Status SetWeights(ge::Node &node, const vector<ge::GeTensorPtr> &weights);
  207. static Status SetWeights(ge::NodePtr node, const vector<ge::GeTensorPtr> &weights);
  208. static Status GetShapeDataFromConstTensor(const ConstGeTensorPtr &tensor, DataType type, std::vector<int64_t> &dims);
  209. private:
  210. friend class CceTensorDescriptor;
  211. static uint32_t GetRealDimCnt(const GeTensorDesc &tensor_desc);
  212. };
  213. class CceTensorDescriptor;
  214. using CceTensorDescriptorPtr = std::shared_ptr<CceTensorDescriptor>;
  215. class CceTensorDescriptor {
  216. public:
  217. explicit CceTensorDescriptor(ccTensorDescriptor_t cc_tensor);
  218. CceTensorDescriptor(const CceTensorDescriptor &) = delete;
  219. CceTensorDescriptor &operator=(const CceTensorDescriptor &) = delete;
  220. ~CceTensorDescriptor();
  221. ccTensorDescriptor_t GetPtr() { return cc_tensor_; }
  222. ///
  223. /// @brief Initializes the tensor based on shape information.
  224. /// @param[in] format data permutation format
  225. /// @param[in] data_type Data Type
  226. /// @param[in] dim dim information
  227. /// @return return code
  228. ///
  229. Status InitTensor(int32_t format, int32_t data_type, const std::vector<int64_t> &dims);
  230. Status InitTensor(int32_t format, int32_t data_type, const ge::GeShape &shape);
  231. ///
  232. /// @brief get format of tensor
  233. /// @param[out] format format of the tensor
  234. /// @return return code
  235. ///
  236. Status GetFormat(ccTensorFormat_t *format);
  237. ///
  238. /// @brief Obtains the size of the tensor.
  239. /// @param[out] size size of Tensor
  240. /// @return return code
  241. ///
  242. Status GetTensorSizeInBytes(uint32_t *size);
  243. ///
  244. /// @brief transform tensor between 4d(NCHW) and 5d(NC1HWC0)
  245. /// @param [in] xDesc descriptor of input tensor
  246. /// @param [in] x point to input data in host memory
  247. /// @param [in] dataTypeTransmode mode of data type transform
  248. /// @param [in] yDesc descriptor of output tensor
  249. /// @param [in|out] y point to output data in host memory
  250. /// @param [in] ySizeInBytes size of outputData
  251. /// @return return code
  252. ///
  253. static Status TransTensor(const ccTensorDescriptor_t xDesc, const void *x, const CceTensorDescriptorPtr &yDesc,
  254. void *y, uint32_t ySizeInBytes);
  255. ///
  256. /// @brief CceTensorDescriptor Static Constructor
  257. /// @return CceTensorDescriptor smart pointer
  258. ///
  259. static CceTensorDescriptorPtr Create();
  260. ccTensorDescriptor_t cc_tensor_ = nullptr;
  261. };
  262. } // namespace ge
  263. #endif // INC_FRAMEWORK_COMMON_OP_GE_OP_UTILS_H_

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