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

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
  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 <memory>
  19. #include <vector>
  20. #include "framework/common/op/attr_value_util.h"
  21. #include "register/register_types.h"
  22. #include "register/register_error_codes.h"
  23. #include "framework/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 domi::Status;
  31. // Add Sub Mul
  32. GE_FUNC_VISIBILITY extern const uint32_t ADD_INPUT_NUM;
  33. GE_FUNC_VISIBILITY extern const uint32_t SUB_INPUT_NUM;
  34. GE_FUNC_VISIBILITY extern const uint32_t MUL_INPUT_NUM;
  35. // Permute
  36. GE_FUNC_VISIBILITY extern const int32_t PERMUTE_ORDER_NUM;
  37. // Ssd PriroBox
  38. GE_FUNC_VISIBILITY extern const double SSD_PRIORBOX_ASPECT_RATIO_VALUE;
  39. GE_FUNC_VISIBILITY extern const uint32_t STRIDEDSLICE_INPUT_NUM;
  40. // Switch
  41. GE_FUNC_VISIBILITY extern const uint32_t SWITCH_INPUT_NUM;
  42. GE_FUNC_VISIBILITY extern const uint32_t SWITCH_OUTPUT_NUM;
  43. GE_FUNC_VISIBILITY extern const uint32_t SWITCH_FALSE_OUTPUT;
  44. GE_FUNC_VISIBILITY extern const uint32_t SWITCH_TRUE_OUTPUT;
  45. GE_FUNC_VISIBILITY extern const uint32_t SWITCH_DATA_INPUT;
  46. GE_FUNC_VISIBILITY extern const uint32_t SWITCH_PRED_INPUT;
  47. // Merge
  48. GE_FUNC_VISIBILITY extern const uint32_t MERGE_DATA_OUTPUT;
  49. GE_FUNC_VISIBILITY extern const uint32_t MERGE_INDEX_OUTPUT;
  50. // FunctionOp
  51. GE_FUNC_VISIBILITY extern const uint32_t IF_COND_INPUT;
  52. GE_FUNC_VISIBILITY extern const uint32_t FOR_START_INPUT;
  53. GE_FUNC_VISIBILITY extern const uint32_t FOR_LIMIT_INPUT;
  54. GE_FUNC_VISIBILITY extern const uint32_t FOR_DELTA_INPUT;
  55. GE_FUNC_VISIBILITY extern const uint32_t FOR_DATA_INPUT;
  56. GE_FUNC_VISIBILITY extern const int NORMAL_TENSOR_SIZE;
  57. class GE_FUNC_VISIBILITY OpUtils {
  58. public:
  59. ///
  60. /// @ingroup domi_ome
  61. /// @brief Check whether check_value is in [min_enum_value, max_enum_value]
  62. /// @return true Within
  63. /// @return false out of range
  64. //
  65. static inline bool CheckEnumValid(int32_t check_value, int32_t min_enum_value, int32_t max_enum_value) {
  66. return check_value < min_enum_value ? false : (check_value >= max_enum_value ? false : true);
  67. }
  68. ///
  69. /// @ingroup domi_omg
  70. /// @brief Determine whether to manually calculate the tensor size based on the values of format and dim
  71. /// @param [in] format, Format information of the tensor
  72. /// @param [in] real_dim_cnt, Tensor dim
  73. /// @return true Manually calculate the size based on dim and datatype
  74. /// @return false skip
  75. ///
  76. static bool IsComputDimsSize(const int32_t format, const uint32_t real_dim_cnt);
  77. ///
  78. /// @brief Extract AIPP parameters from AttrDefMap and splice them
  79. /// @param [in] aipp_attr attr of operator
  80. /// @param [out] aipp_params aipp parameters
  81. /// @return enum of tagCCAippInputFormat
  82. ///
  83. static Status ConvertAippParams(const NamedAttrs &aipp_attr, domi::AippOpParams *aipp_params);
  84. static Status TransferDim(const std::vector<int64_t> &dim, std::vector<int64_t> &dim_vector);
  85. template <typename T>
  86. static void SliceData(const std::vector<char *> &input, int64_t chunk_size, std::vector<char *> &output,
  87. int64_t begin, int64_t out_dim, int64_t stride);
  88. template <typename T>
  89. static Status SetDataByDataType(size_t out_size, const std::vector<char *> &chunk_input,
  90. const std::vector<char *> &chunk_output, GeTensor *output);
  91. template <typename T>
  92. static Status SetOutputSliceDataByDataType(void *data, int64_t data_size, const std::vector<int64_t> &input_dims,
  93. const std::vector<int64_t> &begin, const std::vector<int64_t> &output_dims,
  94. ge::GeTensor *output, const std::vector<int64_t> &stride);
  95. static Status SetOutputSliceData(void *data, int64_t data_size, int32_t data_type, std::vector<int64_t> &input_dims,
  96. std::vector<int64_t> &begin, std::vector<int64_t> &output_dims, ge::GeTensor *output,
  97. std::vector<int64_t> &stride);
  98. ///
  99. /// @ingroup domi_omg
  100. /// @brief Convert the convolutional weight data from [h, w, c, k] to [k, c, h, w]
  101. /// @param [in] input Weight data in HWCK format
  102. /// @param [in] H value of H dimension
  103. /// @param [in] W value of W dimension
  104. /// @param [in] C value of C dimension
  105. /// @param [in] K value of K dimension
  106. /// @param [out] output Data pointer after conversion. The format is KCHW.
  107. ///
  108. static void TransDataHWCK2KCHW(const void *input, int64_t H, int64_t W, int64_t C, int64_t K, void **output);
  109. ///
  110. /// @ingroup domi_omg
  111. /// @brief Converts the convolutional weight data from [k, c, h, w] to [h, w, c, k].
  112. /// @param [in] input Weight data in HWCK format
  113. /// @param [in] K value of K dimension
  114. /// @param [in] C value of C dimension
  115. /// @param [in] H value of H dimension
  116. /// @param [in] W value of W dimension
  117. /// @param [out] output Data pointer after conversion. The format is HWCK
  118. ///
  119. static void TransDataKCHW2HWCK(const void *input, int64_t K, int64_t C, int64_t H, int64_t W, void *output);
  120. static std::vector<ConstGeTensorPtr> GetWeights(const ge::Node &node);
  121. static std::vector<ConstGeTensorPtr> GetWeights(ge::ConstNodePtr node);
  122. static std::vector<GeTensorPtr> MutableWeights(const ge::Node &node);
  123. static std::vector<GeTensorPtr> MutableWeights(const ge::NodePtr node);
  124. static Status SetWeights(ge::Node &node, const std::vector<ge::GeTensorPtr> &weights);
  125. static Status SetWeights(ge::NodePtr node, const std::vector<ge::GeTensorPtr> &weights);
  126. static Status GetShapeDataFromConstTensor(const ConstGeTensorPtr &tensor, DataType type, std::vector<int64_t> &dims);
  127. private:
  128. static uint32_t GetRealDimCnt(const GeTensorDesc &tensor_desc);
  129. };
  130. } // namespace ge
  131. #endif // INC_FRAMEWORK_COMMON_OP_GE_OP_UTILS_H_

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