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.cc 19 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 "framework/common/op/ge_op_utils.h"
  17. #include <list>
  18. #include "common/fp16_t.h"
  19. #include "common/ge/ge_util.h"
  20. #include "external/graph/types.h"
  21. #include "framework/common/debug/ge_log.h"
  22. #include "framework/common/debug/log.h"
  23. #include "framework/common/fmk_error_codes.h"
  24. #include "framework/common/ge_inner_error_codes.h"
  25. #include "framework/common/op/attr_value_util.h"
  26. #include "framework/common/util.h"
  27. #include "framework/common/types.h"
  28. #include "graph/anchor.h"
  29. #include "graph/debug/ge_attr_define.h"
  30. #include "graph/utils/op_desc_utils.h"
  31. #include "graph/utils/tensor_utils.h"
  32. #include "graph/utils/type_utils.h"
  33. #include "mmpa/mmpa_api.h"
  34. using std::vector;
  35. namespace ge {
  36. // General constant
  37. const int32_t kDimSizeZero = 0;
  38. const int32_t kDimSizeOne = 1;
  39. const int32_t kDimSizeTwo = 2;
  40. const int32_t kDimSizeThree = 3;
  41. const uint32_t kSliceDataNum = 2;
  42. // Add Sub Mul
  43. const uint32_t ADD_INPUT_NUM = 2;
  44. const uint32_t MUL_INPUT_NUM = 2;
  45. // Permute
  46. const int32_t PERMUTE_ORDER_NUM = 4;
  47. // Ssd PriroBox
  48. const double SSD_PRIORBOX_ASPECT_RATIO_VALUE = 1.0;
  49. // Switch
  50. const uint32_t SWITCH_INPUT_NUM = 2;
  51. const uint32_t SWITCH_OUTPUT_NUM = 2;
  52. const uint32_t SWITCH_FALSE_OUTPUT = 0;
  53. const uint32_t SWITCH_TRUE_OUTPUT = 1;
  54. const uint32_t SWITCH_DATA_INPUT = 0;
  55. const uint32_t SWITCH_PRED_INPUT = 1;
  56. // Merge
  57. const uint32_t MERGE_DATA_OUTPUT = 0;
  58. const uint32_t MERGE_INDEX_OUTPUT = 1;
  59. // FunctionOp
  60. const uint32_t IF_COND_INPUT = 0;
  61. const uint32_t FOR_START_INPUT = 0;
  62. const uint32_t FOR_LIMIT_INPUT = 1;
  63. const uint32_t FOR_DELTA_INPUT = 2;
  64. const uint32_t FOR_DATA_INPUT = 3;
  65. const int NORMAL_TENSOR_SIZE = 4;
  66. // Get the value of key from attr
  67. #define AIPP_GET_ATTR_VALUE(KEY, ATTR_TYPE) \
  68. if (aipp_attr.GetItem(#KEY).GetValue<ATTR_TYPE>(KEY) != SUCCESS) { \
  69. GELOGI("Attr %s will take default value.", #KEY); \
  70. break; \
  71. }
  72. // Converting aippparams and attrdefmap
  73. #define AIPP_CONVERT_FORMAT_EX(KEY, ORG_TYPE, SAVE_TYPE, ATTR_TYPE) \
  74. do { \
  75. SAVE_TYPE KEY = static_cast<SAVE_TYPE>(0); \
  76. AIPP_GET_ATTR_VALUE(KEY, ATTR_TYPE) \
  77. aipp_params->set_##KEY(ORG_TYPE(KEY)); \
  78. } while (0)
  79. // Converting aippparams and attrdefmap
  80. #define AIPP_CONVERT_FORMAT(KEY, KEY_TYPE, ATTR_TYPE) AIPP_CONVERT_FORMAT_EX(KEY, KEY_TYPE, KEY_TYPE, ATTR_TYPE)
  81. #define AIPP_CONVERT_INT(KEY) AIPP_CONVERT_FORMAT(KEY, int64_t, GeAttrValue::INT)
  82. #define AIPP_CONVERT_BOOL(KEY) AIPP_CONVERT_FORMAT(KEY, bool, GeAttrValue::BOOL)
  83. #define AIPP_CONVERT_FLOAT(KEY) AIPP_CONVERT_FORMAT(KEY, float, GeAttrValue::FLOAT)
  84. // Transform aippparams (with repeated decoration) and attrdefmap
  85. #define AIPP_CONVERT_LIST_FORMAT(KEY, KEY_TYPE, REQUIRED, ATTR_TYPE) \
  86. do { \
  87. if (REQUIRED) { \
  88. KEY_TYPE KEY; \
  89. AIPP_GET_ATTR_VALUE(KEY, ATTR_TYPE) \
  90. aipp_params->add_##KEY(KEY); \
  91. } \
  92. } while (0)
  93. #define AIPP_CONVERT_LIST_INT(KEY, REQUIRED) AIPP_CONVERT_LIST_FORMAT(KEY, int64_t, REQUIRED, GeAttrValue::INT)
  94. #define AIPP_CONVERT_LIST_BOOL(KEY, REQUIRED) AIPP_CONVERT_LIST_FORMAT(KEY, bool, REQUIRED, GeAttrValue::BOOL)
  95. #define AIPP_CONVERT_LIST_FLOAT(KEY, REQUIRED) AIPP_CONVERT_LIST_FORMAT(KEY, float, REQUIRED, GeAttrValue::FLOAT)
  96. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status
  97. OpUtils::ConvertAippParams(const GeAttrValue::NAMED_ATTRS &aipp_attr, domi::AippOpParams *aipp_params) {
  98. GE_CHECK_NOTNULL(aipp_params);
  99. AIPP_CONVERT_FORMAT_EX(aipp_mode, domi::AippOpParams::AippMode, int32_t, GeAttrValue::INT);
  100. AIPP_CONVERT_INT(related_input_rank);
  101. if (aipp_params->aipp_mode() == domi::AippOpParams::dynamic) {
  102. AIPP_CONVERT_INT(max_src_image_size);
  103. AIPP_CONVERT_BOOL(support_rotation);
  104. } else {
  105. AIPP_CONVERT_FORMAT_EX(input_format, domi::AippOpParams::InputFormat, int32_t, GeAttrValue::INT);
  106. AIPP_CONVERT_BOOL(csc_switch);
  107. AIPP_CONVERT_BOOL(crop);
  108. AIPP_CONVERT_INT(load_start_pos_w);
  109. AIPP_CONVERT_INT(load_start_pos_h);
  110. AIPP_CONVERT_INT(crop_size_w);
  111. AIPP_CONVERT_INT(crop_size_h);
  112. AIPP_CONVERT_BOOL(resize);
  113. AIPP_CONVERT_INT(resize_output_w);
  114. AIPP_CONVERT_INT(resize_output_h);
  115. AIPP_CONVERT_BOOL(padding);
  116. AIPP_CONVERT_INT(left_padding_size);
  117. AIPP_CONVERT_INT(right_padding_size);
  118. AIPP_CONVERT_INT(top_padding_size);
  119. AIPP_CONVERT_INT(bottom_padding_size);
  120. AIPP_CONVERT_INT(src_image_size_w);
  121. AIPP_CONVERT_INT(src_image_size_h);
  122. AIPP_CONVERT_FLOAT(cpadding_value);
  123. AIPP_CONVERT_BOOL(rbuv_swap_switch);
  124. AIPP_CONVERT_BOOL(ax_swap_switch);
  125. AIPP_CONVERT_BOOL(single_line_mode);
  126. AIPP_CONVERT_INT(mean_chn_0);
  127. AIPP_CONVERT_INT(mean_chn_1);
  128. AIPP_CONVERT_INT(mean_chn_2);
  129. AIPP_CONVERT_FLOAT(min_chn_0);
  130. AIPP_CONVERT_FLOAT(min_chn_1);
  131. AIPP_CONVERT_FLOAT(min_chn_2);
  132. AIPP_CONVERT_LIST_FLOAT(var_reci_chn_0, true);
  133. AIPP_CONVERT_LIST_FLOAT(var_reci_chn_1, true);
  134. AIPP_CONVERT_LIST_FLOAT(var_reci_chn_2, true);
  135. AIPP_CONVERT_LIST_FLOAT(var_reci_chn_3, true);
  136. const bool csc_switch = aipp_params->csc_switch();
  137. AIPP_CONVERT_LIST_INT(matrix_r0c0, csc_switch);
  138. AIPP_CONVERT_LIST_INT(matrix_r0c1, csc_switch);
  139. AIPP_CONVERT_LIST_INT(matrix_r0c2, csc_switch);
  140. AIPP_CONVERT_LIST_INT(matrix_r1c0, csc_switch);
  141. AIPP_CONVERT_LIST_INT(matrix_r1c1, csc_switch);
  142. AIPP_CONVERT_LIST_INT(matrix_r1c2, csc_switch);
  143. AIPP_CONVERT_LIST_INT(matrix_r2c0, csc_switch);
  144. AIPP_CONVERT_LIST_INT(matrix_r2c1, csc_switch);
  145. AIPP_CONVERT_LIST_INT(matrix_r2c2, csc_switch);
  146. AIPP_CONVERT_LIST_INT(output_bias_0, csc_switch);
  147. AIPP_CONVERT_LIST_INT(output_bias_1, csc_switch);
  148. AIPP_CONVERT_LIST_INT(output_bias_2, csc_switch);
  149. AIPP_CONVERT_LIST_INT(input_bias_0, csc_switch);
  150. AIPP_CONVERT_LIST_INT(input_bias_1, csc_switch);
  151. AIPP_CONVERT_LIST_INT(input_bias_2, csc_switch);
  152. }
  153. return SUCCESS;
  154. }
  155. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status OpUtils::TransferDim(const std::vector<int64_t> &dim,
  156. std::vector<int64_t> &dim_vector) {
  157. size_t input_shape_size = dim.size();
  158. std::list<uint32_t> new_dim_list;
  159. for (auto dim_temp : dim) {
  160. new_dim_list.push_back(dim_temp);
  161. }
  162. if (input_shape_size > DIM_DEFAULT_SIZE) {
  163. dim_vector = dim;
  164. GELOGI("Dim_vector size is %zu, do not to transfer dim", input_shape_size);
  165. return SUCCESS;
  166. }
  167. switch (input_shape_size) {
  168. case kDimSizeZero: {
  169. new_dim_list.push_back(1);
  170. new_dim_list.push_back(1);
  171. new_dim_list.push_back(1);
  172. new_dim_list.push_back(1);
  173. break;
  174. }
  175. case kDimSizeOne: {
  176. new_dim_list.push_front(1);
  177. new_dim_list.push_back(1);
  178. new_dim_list.push_back(1);
  179. break;
  180. }
  181. case kDimSizeTwo: {
  182. new_dim_list.push_front(1);
  183. new_dim_list.push_back(1);
  184. break;
  185. }
  186. case kDimSizeThree: {
  187. new_dim_list.push_front(1);
  188. break;
  189. }
  190. default:
  191. GELOGI("Invalid input_shape_size.");
  192. break;
  193. }
  194. dim_vector.clear();
  195. for (auto dims : new_dim_list) {
  196. dim_vector.push_back(dims);
  197. }
  198. return SUCCESS;
  199. }
  200. template <typename T>
  201. void OpUtils::SliceData(const std::vector<char *> &input, int64_t chunk_size, std::vector<char *> &output,
  202. int64_t begin, int64_t out_dim, int64_t stride) {
  203. char *slice = nullptr;
  204. // chunk_size * (begin + (out_dim-1)*stride) always less than chunk_size * dim_i, no need to check.
  205. for (size_t j = 0; j < input.size(); j++) {
  206. slice = input[j] + sizeof(T) * begin * chunk_size;
  207. for (int64_t i = 0; i < out_dim; i++) {
  208. output.push_back(slice + sizeof(T) * i * chunk_size * stride);
  209. }
  210. }
  211. }
  212. template <typename T>
  213. Status OpUtils::SetDataByDataType(size_t out_size, const std::vector<char *> &chunk_input,
  214. const std::vector<char *> &chunk_output, GeTensor *output) {
  215. unique_ptr<T[]> output_data(new (std::nothrow) T[out_size]());
  216. if (output_data == nullptr) {
  217. GELOGE(MEMALLOC_FAILED, "[Malloc][Data]New buf failed");
  218. REPORT_CALL_ERROR("E19999", "New buf failed");
  219. return INTERNAL_ERROR;
  220. }
  221. if (!chunk_input.empty()) {
  222. for (size_t j = 0; j < out_size; j++) {
  223. T *value = reinterpret_cast<T *>(chunk_input[j]);
  224. output_data[j] = value[0];
  225. }
  226. } else {
  227. for (size_t j = 0; j < out_size; j++) {
  228. T *value = reinterpret_cast<T *>(chunk_output[j]);
  229. output_data[j] = value[0];
  230. }
  231. }
  232. // output_data != nullptr and out_size > 0, SetData always return success, no need to check value
  233. (void)output->SetData(reinterpret_cast<uint8_t *>(output_data.get()), out_size * sizeof(T));
  234. return SUCCESS;
  235. }
  236. template <typename T>
  237. Status OpUtils::SetOutputSliceDataByDataType(void *data, int64_t data_size, const std::vector<int64_t> &input_dims,
  238. const std::vector<int64_t> &begin, const std::vector<int64_t> &output_dims,
  239. GeTensor *output, const std::vector<int64_t> &stride) {
  240. std::vector<char *> chunk_input;
  241. std::vector<char *> chunk_output;
  242. chunk_input.push_back(reinterpret_cast<char *>(data));
  243. int64_t chunk_size = data_size;
  244. size_t dim_size = input_dims.size();
  245. for (size_t i = 0; i < dim_size; i++) {
  246. int64_t begin_i = begin[i];
  247. int64_t size_i = output_dims[i];
  248. int64_t dim_i = input_dims[i];
  249. int64_t stride_i = stride[i];
  250. if (dim_i == 0) {
  251. GELOGE(PARAM_INVALID, "[Check][Param]Invalid, Dim_i of size tensor is 0");
  252. REPORT_INNER_ERROR("E19999", "Dim_i of size tensor is 0, invalid");
  253. return PARAM_INVALID;
  254. }
  255. chunk_size = chunk_size / dim_i;
  256. if (i % kSliceDataNum == 0) {
  257. SliceData<T>(chunk_input, chunk_size, chunk_output, begin_i, size_i, stride_i);
  258. chunk_input.clear();
  259. } else {
  260. SliceData<T>(chunk_output, chunk_size, chunk_input, begin_i, size_i, stride_i);
  261. chunk_output.clear();
  262. }
  263. }
  264. size_t out_size = chunk_input.size() + chunk_output.size();
  265. GE_CHK_BOOL_RET_STATUS(out_size > 0, FAILED, "Out_size <= 0");
  266. Status ret = SetDataByDataType<T>(out_size, chunk_input, chunk_output, output);
  267. return ret;
  268. }
  269. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status OpUtils::SetOutputSliceData(
  270. void *data, int64_t data_size, int32_t data_type, std::vector<int64_t> &input_dims, std::vector<int64_t> &begin,
  271. std::vector<int64_t> &output_dims, GeTensor *output, std::vector<int64_t> &stride) {
  272. if (data == nullptr || output == nullptr) {
  273. GELOGE(PARAM_INVALID, "[Check][Param]Input param is nullptr");
  274. REPORT_INNER_ERROR("E19999", "Input param is nullptr");
  275. return PARAM_INVALID;
  276. }
  277. Status ret;
  278. switch (data_type) {
  279. case DT_INT32:
  280. ret = SetOutputSliceDataByDataType<int32_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  281. break;
  282. case DT_FLOAT:
  283. ret = SetOutputSliceDataByDataType<float>(data, data_size, input_dims, begin, output_dims, output, stride);
  284. break;
  285. case DT_DOUBLE:
  286. ret = SetOutputSliceDataByDataType<double>(data, data_size, input_dims, begin, output_dims, output, stride);
  287. break;
  288. case DT_FLOAT16:
  289. ret = SetOutputSliceDataByDataType<fp16_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  290. break;
  291. case DT_UINT8:
  292. ret = SetOutputSliceDataByDataType<uint8_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  293. break;
  294. case DT_INT8:
  295. ret = SetOutputSliceDataByDataType<int8_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  296. break;
  297. case DT_UINT16:
  298. ret = SetOutputSliceDataByDataType<uint16_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  299. break;
  300. case DT_INT16:
  301. ret = SetOutputSliceDataByDataType<int16_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  302. break;
  303. case DT_UINT32:
  304. ret = SetOutputSliceDataByDataType<uint32_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  305. break;
  306. case DT_UINT64:
  307. ret = SetOutputSliceDataByDataType<uint64_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  308. break;
  309. case DT_INT64:
  310. ret = SetOutputSliceDataByDataType<int64_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  311. break;
  312. default:
  313. GELOGW("Unsupported data type: %s", TypeUtils::DataTypeToSerialString(static_cast<DataType>(data_type)).c_str());
  314. return PARAM_INVALID;
  315. }
  316. return ret;
  317. }
  318. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void OpUtils::TransDataHWCK2KCHW(const void *input, int64_t h,
  319. int64_t w, int64_t c, int64_t k,
  320. void **output) {
  321. if (input == nullptr) {
  322. return;
  323. }
  324. if (output == nullptr) {
  325. return;
  326. }
  327. const char *w_data = (const char *)input;
  328. int64_t count = h * w * c * k;
  329. GE_IF_BOOL_EXEC(count <= 0, GELOGW("Count value must be greater than 0, but count = %ld", count); return);
  330. float *buf = new (std::nothrow) float[count]();
  331. GE_RT_VOID_CHECK_NOTNULL(buf);
  332. float *src_buff = nullptr;
  333. float *dst_buff = nullptr;
  334. for (int h_i = 0; h_i < h; ++h_i) {
  335. for (int w_i = 0; w_i < w; ++w_i) {
  336. for (int c_i = 0; c_i < c; ++c_i) {
  337. for (int k_i = 0; k_i < k; ++k_i) {
  338. src_buff = reinterpret_cast<float *>(const_cast<char *>(w_data)) +
  339. ((h_i * w * c * k) + (w_i * c * k) + (c_i * k) + (k_i));
  340. dst_buff = buf + ((k_i * c * h * w) + (c_i * h * w) + (h_i * w) + (w_i));
  341. *dst_buff = *src_buff;
  342. }
  343. }
  344. }
  345. }
  346. *output = buf;
  347. }
  348. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY void OpUtils::TransDataKCHW2HWCK(const void *input, int64_t k,
  349. int64_t c, int64_t h, int64_t w,
  350. void *output) {
  351. if ((input == nullptr) || (output == nullptr)) {
  352. GELOGD("%s[%d]: input param is nullptr.", __FILE__, __LINE__);
  353. return;
  354. }
  355. const char *w_data = (const char *)input;
  356. float *buf = reinterpret_cast<float *>(output);
  357. float *src_buff = nullptr;
  358. float *dst_buff = nullptr;
  359. for (int k_i = 0; k_i < k; ++k_i) {
  360. for (int c_i = 0; c_i < c; ++c_i) {
  361. for (int h_i = 0; h_i < h; ++h_i) {
  362. for (int w_i = 0; w_i < w; ++w_i) {
  363. src_buff = reinterpret_cast<float *>(const_cast<char *>(w_data)) +
  364. ((k_i * c * h * w) + (c_i * h * w) + (h_i * w) + (w_i));
  365. dst_buff = buf + ((h_i * w * c * k) + (w_i * c * k) + (c_i * k) + (k_i));
  366. *dst_buff = *src_buff;
  367. }
  368. }
  369. }
  370. }
  371. }
  372. vector<ConstGeTensorPtr> OpUtils::GetWeights(const ge::Node &node) { return OpDescUtils::GetWeights(node); }
  373. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY vector<ConstGeTensorPtr> OpUtils::GetWeights(ge::ConstNodePtr node) {
  374. return OpDescUtils::GetWeights(node);
  375. }
  376. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY vector<GeTensorPtr> OpUtils::MutableWeights(const ge::Node &node) {
  377. return OpDescUtils::MutableWeights(node);
  378. }
  379. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY vector<GeTensorPtr> OpUtils::MutableWeights(const ge::NodePtr node) {
  380. return OpDescUtils::MutableWeights(node);
  381. }
  382. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status OpUtils::SetWeights(ge::Node &node,
  383. const vector<ge::GeTensorPtr> &weights) {
  384. return OpDescUtils::SetWeights(node, weights);
  385. }
  386. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status OpUtils::SetWeights(ge::NodePtr node,
  387. const vector<ge::GeTensorPtr> &weights) {
  388. return OpDescUtils::SetWeights(node, weights);
  389. }
  390. // The caller guarantees that the input sensor is constant
  391. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status
  392. OpUtils::GetShapeDataFromConstTensor(const ConstGeTensorPtr &tensor, DataType type, std::vector<int64_t> &dims) {
  393. if (tensor == nullptr) {
  394. GELOGE(PARAM_INVALID, "[Check][Param]Input tensor is nullptr");
  395. REPORT_INNER_ERROR("E19999","Input tensor is nullptr");
  396. return PARAM_INVALID;
  397. }
  398. // If the tensor data is a vector, the shape dimension must be 1
  399. if (tensor->GetTensorDesc().GetShape().GetDims().size() > 1) {
  400. GELOGE(PARAM_INVALID, "[Check][Param]The dimension of the input tensor shape "
  401. "cannot be more than 1, it is %zu",
  402. tensor->GetTensorDesc().GetShape().GetDims().size());
  403. REPORT_CALL_ERROR("E19999", "The dimension of the input tensor shape %zu invalid, "
  404. "more than 1", tensor->GetTensorDesc().GetShape().GetDims().size());
  405. return PARAM_INVALID;
  406. }
  407. if (type == DT_INT32) {
  408. int32_t *shape_data = const_cast<int32_t *>(reinterpret_cast<const int32_t *>(tensor->GetData().GetData()));
  409. GE_CHECK_NOTNULL(shape_data);
  410. size_t dims_num = tensor->GetData().size() / sizeof(int32_t);
  411. for (size_t i = 0; i < dims_num; i++) {
  412. dims.push_back(static_cast<int64_t>(shape_data[i]));
  413. }
  414. } else if (type == DT_INT64) {
  415. int64_t *shape_data = const_cast<int64_t *>(reinterpret_cast<const int64_t *>(tensor->GetData().GetData()));
  416. GE_CHECK_NOTNULL(shape_data);
  417. size_t dims_num = tensor->GetData().size() / sizeof(int64_t);
  418. for (size_t i = 0; i < dims_num; i++) {
  419. dims.push_back(shape_data[i]);
  420. }
  421. } else {
  422. GELOGE(PARAM_INVALID, "[Check][DataType]Invalid, type only can be DT_INT32 or DT_INT64, "
  423. "type is %s", TypeUtils::DataTypeToSerialString(type).c_str());
  424. REPORT_INNER_ERROR("E19999", "Data type %s check invalid, only can be DT_INT32 or DT_INT64",
  425. TypeUtils::DataTypeToSerialString(type).c_str());
  426. return PARAM_INVALID;
  427. }
  428. return SUCCESS;
  429. }
  430. uint32_t OpUtils::GetRealDimCnt(const GeTensorDesc &tensor_desc) {
  431. uint32_t real_dim_cnt = 0;
  432. domi::Status ret = TensorUtils::GetRealDimCnt(tensor_desc, real_dim_cnt);
  433. return (ret == domi::SUCCESS) ? real_dim_cnt : 0;
  434. }
  435. } // namespace ge

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