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.

bcast.h 7.6 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
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 GE_GRAPH_COMMON_BCAST_H_
  17. #define GE_GRAPH_COMMON_BCAST_H_
  18. #include <stdint.h>
  19. #include <functional>
  20. #include <vector>
  21. #include "common/debug/log.h"
  22. #include "common/types.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "framework/common/ge_inner_error_codes.h"
  25. #include "graph/attr_value.h"
  26. #include "graph/ge_tensor.h"
  27. #include "graph/utils/tensor_adapter.h"
  28. namespace ge {
  29. static const size_t kMinDimNum = 2;
  30. class BCast {
  31. public:
  32. ///
  33. /// @ingroup domi_calibration
  34. /// @brief define kVecInt
  35. ///
  36. typedef std::vector<int64_t> kVecInt;
  37. ///
  38. /// @ingroup domi_calibration
  39. /// @brief constructor
  40. ///
  41. BCast() {}
  42. ///
  43. /// @ingroup domi_calibration
  44. /// @brief destructor
  45. ///
  46. ~BCast() {}
  47. ///
  48. /// @ingroup domi_calibration
  49. /// @brief Not optimize intermediate shapes
  50. /// @decrease dims, more efficient, set by user
  51. /// @param [in] x first Tensor dim
  52. /// @param [in] y second Tensor dim
  53. /// @return SUCCESS broadcast message successfully generated
  54. /// @return other broadcast message failed to generate
  55. ///
  56. ge::Status GenerateBcastInfo(const kVecInt &x, const kVecInt &y);
  57. ///
  58. /// @ingroup domi_calibration
  59. /// @brief get x_reshape
  60. ///
  61. const kVecInt &GetXReshape() const { return x_reshape_; }
  62. ///
  63. /// @ingroup domi_calibration
  64. /// @brief get x_bcast
  65. ///
  66. const kVecInt &GetXBcast() const { return x_bcast_; }
  67. ///
  68. /// @ingroup domi_calibration
  69. /// @brief get y_reshape
  70. ///
  71. const kVecInt &GetYReshape() const { return y_reshape_; }
  72. ///
  73. /// @ingroup domi_calibration
  74. /// @brief get y_bcast
  75. ///
  76. const kVecInt &GetYBcast() const { return y_bcast_; }
  77. ///
  78. /// @ingroup domi_calibration
  79. /// @brief get result_shape
  80. ///
  81. const kVecInt &GetResultShape() const { return result_; }
  82. ///
  83. /// @ingroup domi_calibration
  84. /// @brief get result_shape
  85. ///
  86. const kVecInt &GetOutputShape() const { return output_; }
  87. const kVecInt &GetGradXReduceIdx() const { return grad_x_reduce_idx_; }
  88. const kVecInt &GetGradYReduceIdx() const { return grad_y_reduce_idx_; }
  89. ///
  90. /// @ingroup domi_calibration
  91. /// @brief convert TensorDescriptor to kVecInt
  92. /// @param [in] shape Tensor descriptor
  93. /// @return kVecInt dim info
  94. ///
  95. static kVecInt TransShapeToDimVec(const GeTensorDesc &shape);
  96. void BCastIndexes(kVecInt &x_indexes, kVecInt &y_indexes);
  97. template <typename InT, typename OutT>
  98. Status BCastCompute(const std::vector<ConstGeTensorPtr> &input, std::vector<OutT> &v_output,
  99. const std::function<OutT(InT const &, InT const &)> &func) {
  100. Status ret;
  101. if (func == nullptr) {
  102. REPORT_INNER_ERROR("E19999", "Check param func nullptr");
  103. GELOGE(domi::PARAM_INVALID, "Param func is null");
  104. return domi::PARAM_INVALID;
  105. }
  106. // Min input num is 2
  107. if (input.size() < kMinDimNum) {
  108. REPORT_INNER_ERROR("E19999", "Param input.size():%zu < %zu, check invalid",
  109. input.size(), kMinDimNum);
  110. GELOGE(domi::PARAM_INVALID, "Input size is smaller than two.");
  111. return domi::PARAM_INVALID;
  112. }
  113. // Only broadcast shape
  114. ret =
  115. GenerateBcastInfo(TransShapeToDimVec(input[0]->GetTensorDesc()), TransShapeToDimVec(input[1]->GetTensorDesc()));
  116. if (ret != domi::SUCCESS) {
  117. GELOGE(ret, "Greater broadcasting failed.");
  118. return ret;
  119. }
  120. kVecInt x_indexes;
  121. kVecInt y_indexes;
  122. BCastIndexes(x_indexes, y_indexes);
  123. const void *x1_data = input[0]->GetData().data();
  124. const void *x2_data = input[1]->GetData().data();
  125. for (size_t i = 0; i < x_indexes.size(); i++) {
  126. int64_t x_index = x_indexes[i];
  127. int64_t y_index = y_indexes[i];
  128. auto value = func((*(reinterpret_cast<const InT *>(x1_data) + x_index)),
  129. (*(reinterpret_cast<const InT *>(x2_data) + y_index)));
  130. v_output.push_back(value);
  131. }
  132. return domi::SUCCESS;
  133. }
  134. template <typename InT, typename OutT>
  135. Status BCastComputeCheck(const std::vector<ConstGeTensorPtr> &input, std::vector<OutT> &v_output,
  136. const std::function<OutT(InT const &, InT const &, DataType &type, Status &)> &func) {
  137. if (func == nullptr) {
  138. REPORT_INNER_ERROR("E19999", "Check param func nullptr");
  139. GELOGE(PARAM_INVALID, "Param func is null");
  140. return PARAM_INVALID;
  141. }
  142. // Min input num is 2
  143. if (input.size() < kMinDimNum) {
  144. REPORT_INNER_ERROR("E19999", "Param input.size():%zu < %zu, check invalid",
  145. input.size(), kMinDimNum);
  146. GELOGE(PARAM_INVALID, "Input size is smaller than two.");
  147. return PARAM_INVALID;
  148. }
  149. // Only broadcast shape
  150. Status ret =
  151. GenerateBcastInfo(TransShapeToDimVec(input[0]->GetTensorDesc()), TransShapeToDimVec(input[1]->GetTensorDesc()));
  152. if (ret != SUCCESS) {
  153. GELOGE(ret, "Greater broadcasting failed.");
  154. return ret;
  155. }
  156. DataType data_type = input[0]->GetTensorDesc().GetDataType();
  157. kVecInt x_indexes;
  158. kVecInt y_indexes;
  159. BCastIndexes(x_indexes, y_indexes);
  160. const void *x1_data = input[0]->GetData().data();
  161. const void *x2_data = input[1]->GetData().data();
  162. for (size_t i = 0; i < x_indexes.size(); i++) {
  163. int64_t x_index = x_indexes[i];
  164. int64_t y_index = y_indexes[i];
  165. auto value = func((*(reinterpret_cast<const InT *>(x1_data) + x_index)),
  166. (*(reinterpret_cast<const InT *>(x2_data) + y_index)), data_type, ret);
  167. if (ret != SUCCESS) {
  168. REPORT_INNER_ERROR("E19999", "BCastComputeCheck func execute failed, datatype is %d.", data_type);
  169. GELOGE(ret, "BCastComputeCheck func execute failed, datatype is %d.", data_type);
  170. return ret;
  171. }
  172. v_output.push_back(value);
  173. }
  174. return SUCCESS;
  175. }
  176. private:
  177. ///
  178. /// @ingroup domi_calibration
  179. /// @brief reverse elements in kVecInt
  180. /// @param [in] shape dim info
  181. /// @return null
  182. ///
  183. static void Reverse(kVecInt &shape);
  184. ///
  185. /// @ingroup domi_calibration
  186. /// @brief two Tensor with different shape, set broadcast info
  187. /// @param [in] x first input Tensor dim info
  188. /// @param [in] y second input Tensor dim info
  189. /// @return null
  190. ///
  191. ge::Status SetShapeDifferentInfo(const kVecInt &x, const kVecInt &y);
  192. ///
  193. /// @ingroup domi_calibration
  194. /// @brief extend Tensor dim
  195. /// @param [in] x first input Tensor dim info
  196. /// @param [in] y second input Tensor dim info
  197. /// @return null
  198. ///
  199. void ExtendTensorDim(kVecInt &x, kVecInt &y);
  200. ///
  201. /// @ingroup domi_calibration
  202. /// @brief reverse all intermediate shape params
  203. /// @param [in] void
  204. /// @return null
  205. ///
  206. void ReverseAllIntermediateShapes();
  207. kVecInt x_reshape_;
  208. kVecInt x_bcast_;
  209. kVecInt y_reshape_;
  210. kVecInt y_bcast_;
  211. kVecInt result_;
  212. kVecInt output_;
  213. kVecInt grad_x_reduce_idx_;
  214. kVecInt grad_y_reduce_idx_;
  215. };
  216. } // namespace ge
  217. #endif // GE_GRAPH_COMMON_BCAST_H_

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