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 12 kB

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

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

Contributors (1)