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.

reduce_prod_kernel.cc 12 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 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
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * Copyright 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. #include "host_kernels/reduce_prod_kernel.h"
  17. #include <memory>
  18. #include <set>
  19. #include "common/math/math_util.h"
  20. #include "framework/common/op/ge_op_utils.h"
  21. #include "framework/common/types.h"
  22. #include "framework/common/debug/ge_log.h"
  23. #include "framework/common/ge_inner_error_codes.h"
  24. #include "host_kernels/kernel_utils.h"
  25. #include "graph/utils/type_utils.h"
  26. #include "inc/kernel_factory.h"
  27. namespace ge {
  28. namespace {
  29. const size_t kReduceProdDataIndex = 0;
  30. const size_t kReduceProdAxisIndex = 1;
  31. const size_t kReduceProdMaxAxisRank = 1;
  32. const size_t kReduceProdInputOnlyData = 1;
  33. const size_t kReduceProdInputSize = 2;
  34. const std::set<DataType> kReduceProdSupportedType = {DT_INT32};
  35. } // namespace
  36. Status ReduceProdKernel::ReduceProdCheck(const ge::OpDescPtr &op_desc_ptr,
  37. const std::vector<ge::ConstGeTensorPtr> &input) const {
  38. if (op_desc_ptr == nullptr) {
  39. GELOGW("Input opdesc is nullptr.");
  40. return PARAM_INVALID;
  41. }
  42. if (input.size() != kReduceProdInputSize) {
  43. if (input.size() == kReduceProdInputOnlyData) {
  44. // Input only data, which means calculate product for all elements in data_tensor.
  45. GELOGI("ReduceProd node input size is 1, which does not have param axis, node name %s",
  46. op_desc_ptr->GetName().c_str());
  47. return NOT_CHANGED;
  48. }
  49. GELOGW("Unexpected ReduceProd node, node input size: %zu, node name: %s", input.size(),
  50. op_desc_ptr->GetName().c_str());
  51. return PARAM_INVALID;
  52. }
  53. ConstGeTensorPtr data_tensor = input.at(kReduceProdDataIndex);
  54. ConstGeTensorPtr axis_tensor = input.at(kReduceProdAxisIndex);
  55. GE_CHECK_NOTNULL(data_tensor);
  56. GE_CHECK_NOTNULL(axis_tensor);
  57. if (axis_tensor->GetTensorDesc().GetShape().GetDimNum() > kReduceProdMaxAxisRank) {
  58. GELOGW("Axis must be at most rank 1, node: %s", op_desc_ptr->GetName().c_str());
  59. return PARAM_INVALID;
  60. }
  61. DataType data_type = data_tensor->GetTensorDesc().GetDataType();
  62. if (kReduceProdSupportedType.find(data_type) == kReduceProdSupportedType.end()) {
  63. GELOGW("ReduceProdKernel data type %s not support, node name: %s",
  64. TypeUtils::DataTypeToSerialString(data_type).c_str(), op_desc_ptr->GetName().c_str());
  65. return PARAM_INVALID;
  66. }
  67. return SUCCESS;
  68. }
  69. Status ReduceProdKernel::AxisCal(const std::vector<ge::ConstGeTensorPtr> &input) {
  70. ConstGeTensorPtr data_tensor = input.at(kReduceProdDataIndex);
  71. ConstGeTensorPtr axis_tensor = input.at(kReduceProdAxisIndex);
  72. // support: compute for the first element of axis.
  73. vector<int64_t> data_dims = data_tensor->GetTensorDesc().GetShape().GetDims();
  74. size_t data_dim_size = data_dims.size();
  75. int32_t *axis = const_cast<int32_t *>(reinterpret_cast<const int32_t *>(axis_tensor->GetData().GetData()));
  76. GE_CHECK_NOTNULL(axis);
  77. if (static_cast<size_t>(*axis) >= data_dim_size) {
  78. GELOGW("axis is out of rank of data_dims, axis is %d.", *axis);
  79. return PARAM_INVALID;
  80. }
  81. axis_dim_ = data_dims[static_cast<size_t>(*axis)];
  82. head_dim_ = 1;
  83. end_dim_ = 1;
  84. bool axis_appear = false;
  85. for (size_t i = 0; i < data_dim_size; i++) {
  86. if (i == static_cast<size_t>(*axis)) {
  87. axis_appear = true;
  88. continue;
  89. }
  90. // data_dims is the vector of dims, element in data_dims isn't negative.
  91. if (axis_appear) {
  92. if (data_dims[i] != 0 && end_dim_ > (INT64_MAX / data_dims[i])) {
  93. GELOGW("Product is overflow. multiplier 1: %ld. multiplier 2: %ld.", end_dim_, data_dims[i]);
  94. return INTERNAL_ERROR;
  95. }
  96. end_dim_ *= data_dims[i];
  97. } else {
  98. if (data_dims[i] != 0 && head_dim_ > (INT64_MAX / data_dims[i])) {
  99. GELOGW("Product is overflow. multiplier 1: %ld. multiplier 2: %ld.", head_dim_, data_dims[i]);
  100. return INTERNAL_ERROR;
  101. }
  102. head_dim_ *= data_dims[i];
  103. }
  104. }
  105. return SUCCESS;
  106. }
  107. Status ReduceProdKernel::DataCal(const std::vector<ge::ConstGeTensorPtr> &input, ge::GeTensorPtr output_ptr) {
  108. ConstGeTensorPtr data_tensor = input.at(kReduceProdDataIndex);
  109. DataType data_dtype = data_tensor->GetTensorDesc().GetDataType();
  110. if (data_dtype == DT_INT32) {
  111. int32_t *input_data = const_cast<int32_t *>(reinterpret_cast<const int32_t *>(data_tensor->GetData().GetData()));
  112. GE_CHECK_NOTNULL(input_data);
  113. size_t data_num = data_tensor->GetData().size() / sizeof(int32_t);
  114. unique_ptr<int32_t[]> buf(new (std::nothrow) int32_t[data_num]());
  115. if (buf == nullptr) {
  116. GELOGW("new buf failed");
  117. return INTERNAL_ERROR;
  118. }
  119. int32_t tmp_x = 1;
  120. int32_t tmp_y = 1;
  121. for (int64_t i = 0; i < head_dim_; ++i) {
  122. for (int64_t j = 0; j < end_dim_; ++j) {
  123. // all index for input_data is less than size of input_data
  124. tmp_x = input_data[static_cast<size_t>(i * end_dim_ * axis_dim_ + j)];
  125. for (int64_t k = 1; k < axis_dim_; ++k) {
  126. tmp_y = input_data[static_cast<size_t>(i * end_dim_ * axis_dim_ + j + k * end_dim_)];
  127. if (ge::CheckInt32MulOverflow(tmp_x, tmp_y) != SUCCESS) {
  128. GELOGW("Product is overflow. multiplier 1: %d. multiplier 2: %d.", tmp_x, tmp_y);
  129. return INTERNAL_ERROR;
  130. }
  131. tmp_x *= tmp_y;
  132. }
  133. buf[static_cast<size_t>(i * end_dim_ + j)] = tmp_x;
  134. }
  135. }
  136. GE_IF_BOOL_EXEC(output_ptr->SetData(reinterpret_cast<uint8_t *>(buf.get()),
  137. static_cast<size_t>(head_dim_ * end_dim_ * sizeof(int32_t))) != GRAPH_SUCCESS,
  138. GELOGW("set data failed");
  139. return INTERNAL_ERROR);
  140. }
  141. return SUCCESS;
  142. }
  143. void ReduceProdKernel::ShapeCal(const ge::OpDescPtr &op_desc_ptr, const std::vector<ge::ConstGeTensorPtr> &input,
  144. ge::GeTensorPtr output_ptr) {
  145. ConstGeTensorPtr data_tensor = input.at(kReduceProdDataIndex);
  146. ConstGeTensorPtr axis_tensor = input.at(kReduceProdAxisIndex);
  147. vector<int64_t> data_dims = data_tensor->GetTensorDesc().GetShape().GetDims();
  148. int32_t data_dim_size = static_cast<int32_t>(data_dims.size());
  149. const uint8_t *axis_data = axis_tensor->GetData().GetData();
  150. GE_CHECK_NOTNULL_EXEC(axis_data, return);
  151. int32_t axis = *(const_cast<int32_t *>(reinterpret_cast<const int32_t *>(axis_data)));
  152. bool keep_dims = false;
  153. if (!AttrUtils::GetBool(op_desc_ptr, "keep_dims", keep_dims)) {
  154. GELOGI("Get the attr keep_dims was failed.");
  155. }
  156. if (keep_dims) {
  157. for (int32_t i = 0; i < data_dim_size; i++) {
  158. if (i == axis) {
  159. data_dims[i] = 1;
  160. }
  161. }
  162. } else {
  163. vector<int64_t> tmp_dims;
  164. for (int32_t i = 0; i < data_dim_size; i++) {
  165. if (i != axis) {
  166. tmp_dims.push_back(data_dims[i]);
  167. }
  168. }
  169. data_dims.clear();
  170. data_dims = tmp_dims;
  171. }
  172. output_ptr->MutableTensorDesc().SetShape(GeShape(data_dims));
  173. }
  174. Status ReduceProdKernel::ComputeNoAxis(const ge::OpDescPtr &op_desc_ptr, const std::vector<ConstGeTensorPtr> &input,
  175. ge::GeTensorPtr output_ptr) {
  176. ConstGeTensorPtr data_tensor = input.at(kReduceProdDataIndex);
  177. GE_CHECK_NOTNULL(data_tensor);
  178. if (data_tensor->GetData().size() == 0) {
  179. GELOGW("ReduceProdKernel data size of inputs is 0, node node: %s", op_desc_ptr->GetName().c_str());
  180. return PARAM_INVALID;
  181. }
  182. DataType data_type = data_tensor->GetTensorDesc().GetDataType();
  183. if (kReduceProdSupportedType.find(data_type) == kReduceProdSupportedType.end()) {
  184. GELOGW("ReduceProdKernel data type %s not support, node name: %s",
  185. TypeUtils::DataTypeToSerialString(data_type).c_str(), op_desc_ptr->GetName().c_str());
  186. return PARAM_INVALID;
  187. }
  188. if (data_type == DT_INT32) {
  189. int32_t *input_data = const_cast<int32_t *>(reinterpret_cast<const int32_t *>(data_tensor->GetData().GetData()));
  190. GE_CHECK_NOTNULL(input_data);
  191. size_t data_num = data_tensor->GetData().size() / sizeof(int32_t);
  192. unique_ptr<int32_t[]> buf(new (std::nothrow) int32_t[data_num]());
  193. if (buf == nullptr) {
  194. GELOGW("new buf failed");
  195. return INTERNAL_ERROR;
  196. }
  197. int32_t tmp_x = input_data[0];
  198. int32_t tmp_y = 1;
  199. for (size_t k = 1; k < data_num; ++k) {
  200. tmp_y = input_data[k];
  201. if (ge::CheckInt32MulOverflow(tmp_x, tmp_y) != SUCCESS) {
  202. GELOGW("Product is overflow. multiplier 1: %d. multiplier 2: %d.", tmp_x, tmp_y);
  203. return INTERNAL_ERROR;
  204. }
  205. tmp_x *= tmp_y;
  206. }
  207. buf[0] = tmp_x;
  208. GE_IF_BOOL_EXEC(output_ptr->SetData(reinterpret_cast<uint8_t *>(buf.get()), sizeof(int32_t)) != GRAPH_SUCCESS,
  209. GELOGW("set data failed");
  210. return INTERNAL_ERROR);
  211. output_ptr->MutableTensorDesc().SetDataType(data_type);
  212. output_ptr->MutableTensorDesc().SetShape(GeShape());
  213. }
  214. return SUCCESS;
  215. }
  216. Status ReduceProdKernel::Compute(const ge::OpDescPtr op_desc_ptr, const std::vector<ge::ConstGeTensorPtr> &input,
  217. std::vector<ge::GeTensorPtr> &v_output) {
  218. GELOGI("ReduceProdKernel in.");
  219. Status ret = ReduceProdCheck(op_desc_ptr, input);
  220. if (ret != SUCCESS && ret != NOT_CHANGED) {
  221. GELOGW("ReduceProdKernel input is invalid, failed to fold node.");
  222. return NOT_CHANGED;
  223. }
  224. // Index 0 can always gets a GeTensorDesc object from any OpDescPtr.
  225. auto output_tensor_desc = op_desc_ptr->GetOutputDesc(0);
  226. GeTensorPtr output_ptr = MakeShared<GeTensor>(output_tensor_desc);
  227. if (output_ptr == nullptr) {
  228. GELOGW("make_shared ge::GeTensor failed, node name %s.", op_desc_ptr->GetName().c_str());
  229. return NOT_CHANGED;
  230. }
  231. if (ret == NOT_CHANGED) {
  232. // compute output tensor when no param axis
  233. ret = ComputeNoAxis(op_desc_ptr, input, output_ptr);
  234. if (ret != SUCCESS) {
  235. return NOT_CHANGED;
  236. }
  237. } else if (input.at(kReduceProdAxisIndex)->GetData().size() == 0) {
  238. // axis tensor value is [], means no process for input
  239. output_ptr->MutableTensorDesc().SetShape(input.at(kReduceProdDataIndex)->GetTensorDesc().GetShape());
  240. output_ptr->MutableTensorDesc().SetDataType(input.at(kReduceProdDataIndex)->GetTensorDesc().GetDataType());
  241. if (output_ptr->SetData(input.at(kReduceProdDataIndex)->GetData()) != GRAPH_SUCCESS) {
  242. GELOGW("Compute: SetData failed");
  243. }
  244. } else {
  245. // calculate axis to reduce
  246. ret = AxisCal(input);
  247. if (ret != SUCCESS) {
  248. return NOT_CHANGED;
  249. }
  250. // calculate and set shape
  251. ShapeCal(op_desc_ptr, input, output_ptr);
  252. // set data type
  253. output_ptr->MutableTensorDesc().SetDataType(input.at(kReduceProdDataIndex)->GetTensorDesc().GetDataType());
  254. // data size == 0 means input tensor has zero in shape, and tensor value is [].
  255. if (input.at(kReduceProdDataIndex)->GetData().size() != 0) {
  256. // calculate data and data type
  257. ret = DataCal(input, output_ptr);
  258. if (ret != SUCCESS) {
  259. return NOT_CHANGED;
  260. }
  261. }
  262. }
  263. // print output tensor information, and will be deleted
  264. GELOGD("ReduceProd op %s output tensor data size is %zu", op_desc_ptr->GetName().c_str(),
  265. output_ptr->GetData().size());
  266. vector<int64_t> data_dims = output_ptr->GetTensorDesc().GetShape().GetDims();
  267. GELOGD("ReduceProd op %s output tensor dim size is %zu", op_desc_ptr->GetName().c_str(), data_dims.size());
  268. v_output.emplace_back(output_ptr);
  269. GELOGI("ReduceProdKernel success.");
  270. return SUCCESS;
  271. }
  272. REGISTER_KERNEL(REDUCEPROD, ReduceProdKernel);
  273. } // namespace ge

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