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

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