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

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