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

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
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
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. Status OpUtils::ConvertAippParams(const GeAttrValue::NAMED_ATTRS &aipp_attr, domi::AippOpParams *aipp_params) {
  97. GE_CHECK_NOTNULL(aipp_params);
  98. AIPP_CONVERT_FORMAT_EX(aipp_mode, domi::AippOpParams::AippMode, int32_t, GeAttrValue::INT);
  99. AIPP_CONVERT_INT(related_input_rank);
  100. if (aipp_params->aipp_mode() == domi::AippOpParams::dynamic) {
  101. AIPP_CONVERT_INT(max_src_image_size);
  102. AIPP_CONVERT_BOOL(support_rotation);
  103. } else {
  104. AIPP_CONVERT_FORMAT_EX(input_format, domi::AippOpParams::InputFormat, int32_t, GeAttrValue::INT);
  105. AIPP_CONVERT_BOOL(csc_switch);
  106. AIPP_CONVERT_BOOL(crop);
  107. AIPP_CONVERT_INT(load_start_pos_w);
  108. AIPP_CONVERT_INT(load_start_pos_h);
  109. AIPP_CONVERT_INT(crop_size_w);
  110. AIPP_CONVERT_INT(crop_size_h);
  111. AIPP_CONVERT_BOOL(resize);
  112. AIPP_CONVERT_INT(resize_output_w);
  113. AIPP_CONVERT_INT(resize_output_h);
  114. AIPP_CONVERT_BOOL(padding);
  115. AIPP_CONVERT_INT(left_padding_size);
  116. AIPP_CONVERT_INT(right_padding_size);
  117. AIPP_CONVERT_INT(top_padding_size);
  118. AIPP_CONVERT_INT(bottom_padding_size);
  119. AIPP_CONVERT_INT(src_image_size_w);
  120. AIPP_CONVERT_INT(src_image_size_h);
  121. AIPP_CONVERT_FLOAT(cpadding_value);
  122. AIPP_CONVERT_BOOL(rbuv_swap_switch);
  123. AIPP_CONVERT_BOOL(ax_swap_switch);
  124. AIPP_CONVERT_BOOL(single_line_mode);
  125. AIPP_CONVERT_INT(mean_chn_0);
  126. AIPP_CONVERT_INT(mean_chn_1);
  127. AIPP_CONVERT_INT(mean_chn_2);
  128. AIPP_CONVERT_FLOAT(min_chn_0);
  129. AIPP_CONVERT_FLOAT(min_chn_1);
  130. AIPP_CONVERT_FLOAT(min_chn_2);
  131. AIPP_CONVERT_LIST_FLOAT(var_reci_chn_0, true);
  132. AIPP_CONVERT_LIST_FLOAT(var_reci_chn_1, true);
  133. AIPP_CONVERT_LIST_FLOAT(var_reci_chn_2, true);
  134. AIPP_CONVERT_LIST_FLOAT(var_reci_chn_3, true);
  135. const bool csc_switch = aipp_params->csc_switch();
  136. AIPP_CONVERT_LIST_INT(matrix_r0c0, csc_switch);
  137. AIPP_CONVERT_LIST_INT(matrix_r0c1, csc_switch);
  138. AIPP_CONVERT_LIST_INT(matrix_r0c2, csc_switch);
  139. AIPP_CONVERT_LIST_INT(matrix_r1c0, csc_switch);
  140. AIPP_CONVERT_LIST_INT(matrix_r1c1, csc_switch);
  141. AIPP_CONVERT_LIST_INT(matrix_r1c2, csc_switch);
  142. AIPP_CONVERT_LIST_INT(matrix_r2c0, csc_switch);
  143. AIPP_CONVERT_LIST_INT(matrix_r2c1, csc_switch);
  144. AIPP_CONVERT_LIST_INT(matrix_r2c2, csc_switch);
  145. AIPP_CONVERT_LIST_INT(output_bias_0, csc_switch);
  146. AIPP_CONVERT_LIST_INT(output_bias_1, csc_switch);
  147. AIPP_CONVERT_LIST_INT(output_bias_2, csc_switch);
  148. AIPP_CONVERT_LIST_INT(input_bias_0, csc_switch);
  149. AIPP_CONVERT_LIST_INT(input_bias_1, csc_switch);
  150. AIPP_CONVERT_LIST_INT(input_bias_2, csc_switch);
  151. }
  152. return SUCCESS;
  153. }
  154. Status OpUtils::TransferDim(const std::vector<int64_t> &dim, std::vector<int64_t> &dim_vector) {
  155. size_t input_shape_size = dim.size();
  156. std::list<uint32_t> new_dim_list;
  157. for (auto dim_temp : dim) {
  158. new_dim_list.push_back(dim_temp);
  159. }
  160. if (input_shape_size > DIM_DEFAULT_SIZE) {
  161. dim_vector = dim;
  162. GELOGI("Dim_vector size is %zu, do not to transfer dim", input_shape_size);
  163. return SUCCESS;
  164. }
  165. switch (input_shape_size) {
  166. case kDimSizeZero: {
  167. new_dim_list.push_back(1);
  168. new_dim_list.push_back(1);
  169. new_dim_list.push_back(1);
  170. new_dim_list.push_back(1);
  171. break;
  172. }
  173. case kDimSizeOne: {
  174. new_dim_list.push_front(1);
  175. new_dim_list.push_back(1);
  176. new_dim_list.push_back(1);
  177. break;
  178. }
  179. case kDimSizeTwo: {
  180. new_dim_list.push_front(1);
  181. new_dim_list.push_back(1);
  182. break;
  183. }
  184. case kDimSizeThree: {
  185. new_dim_list.push_front(1);
  186. break;
  187. }
  188. default:
  189. GELOGI("Invalid input_shape_size.");
  190. break;
  191. }
  192. dim_vector.clear();
  193. for (auto dims : new_dim_list) {
  194. dim_vector.push_back(dims);
  195. }
  196. return SUCCESS;
  197. }
  198. template <typename T>
  199. void OpUtils::SliceData(const std::vector<char *> &input, int64_t chunk_size, std::vector<char *> &output,
  200. int64_t begin, int64_t out_dim, int64_t stride) {
  201. char *slice = nullptr;
  202. // chunk_size * (begin + (out_dim-1)*stride) always less than chunk_size * dim_i, no need to check.
  203. for (size_t j = 0; j < input.size(); j++) {
  204. slice = input[j] + sizeof(T) * begin * chunk_size;
  205. for (int64_t i = 0; i < out_dim; i++) {
  206. output.push_back(slice + sizeof(T) * i * chunk_size * stride);
  207. }
  208. }
  209. }
  210. template <typename T>
  211. Status OpUtils::SetDataByDataType(size_t out_size, const std::vector<char *> &chunk_input,
  212. const std::vector<char *> &chunk_output, GeTensor *output) {
  213. unique_ptr<T[]> output_data(new (std::nothrow) T[out_size]());
  214. if (output_data == nullptr) {
  215. GELOGE(MEMALLOC_FAILED, "[Malloc][Data]New buf failed");
  216. REPORT_CALL_ERROR("E19999", "New buf failed");
  217. return INTERNAL_ERROR;
  218. }
  219. if (!chunk_input.empty()) {
  220. for (size_t j = 0; j < out_size; j++) {
  221. T *value = reinterpret_cast<T *>(chunk_input[j]);
  222. output_data[j] = value[0];
  223. }
  224. } else {
  225. for (size_t j = 0; j < out_size; j++) {
  226. T *value = reinterpret_cast<T *>(chunk_output[j]);
  227. output_data[j] = value[0];
  228. }
  229. }
  230. // output_data != nullptr and out_size > 0, SetData always return success, no need to check value
  231. (void)output->SetData(reinterpret_cast<uint8_t *>(output_data.get()), out_size * sizeof(T));
  232. return SUCCESS;
  233. }
  234. template <typename T>
  235. Status OpUtils::SetOutputSliceDataByDataType(void *data, int64_t data_size, const std::vector<int64_t> &input_dims,
  236. const std::vector<int64_t> &begin, const std::vector<int64_t> &output_dims,
  237. GeTensor *output, const std::vector<int64_t> &stride) {
  238. std::vector<char *> chunk_input;
  239. std::vector<char *> chunk_output;
  240. chunk_input.push_back(reinterpret_cast<char *>(data));
  241. int64_t chunk_size = data_size;
  242. size_t dim_size = input_dims.size();
  243. for (size_t i = 0; i < dim_size; i++) {
  244. int64_t begin_i = begin[i];
  245. int64_t size_i = output_dims[i];
  246. int64_t dim_i = input_dims[i];
  247. int64_t stride_i = stride[i];
  248. if (dim_i == 0) {
  249. GELOGE(PARAM_INVALID, "[Check][Param]Invalid, Dim_i of size tensor is 0");
  250. REPORT_INNER_ERROR("E19999", "Dim_i of size tensor is 0, invalid");
  251. return PARAM_INVALID;
  252. }
  253. chunk_size = chunk_size / dim_i;
  254. if (i % kSliceDataNum == 0) {
  255. SliceData<T>(chunk_input, chunk_size, chunk_output, begin_i, size_i, stride_i);
  256. chunk_input.clear();
  257. } else {
  258. SliceData<T>(chunk_output, chunk_size, chunk_input, begin_i, size_i, stride_i);
  259. chunk_output.clear();
  260. }
  261. }
  262. size_t out_size = chunk_input.size() + chunk_output.size();
  263. GE_CHK_BOOL_RET_STATUS(out_size > 0, FAILED, "Out_size <= 0");
  264. Status ret = SetDataByDataType<T>(out_size, chunk_input, chunk_output, output);
  265. return ret;
  266. }
  267. Status OpUtils::SetOutputSliceData(void *data, int64_t data_size, int32_t data_type, std::vector<int64_t> &input_dims,
  268. std::vector<int64_t> &begin, std::vector<int64_t> &output_dims, GeTensor *output,
  269. std::vector<int64_t> &stride) {
  270. if (data == nullptr || output == nullptr) {
  271. GELOGE(PARAM_INVALID, "[Check][Param]Input param is nullptr");
  272. REPORT_INNER_ERROR("E19999", "Input param is nullptr");
  273. return PARAM_INVALID;
  274. }
  275. Status ret;
  276. switch (data_type) {
  277. case DT_INT32:
  278. ret = SetOutputSliceDataByDataType<int32_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  279. break;
  280. case DT_FLOAT:
  281. ret = SetOutputSliceDataByDataType<float>(data, data_size, input_dims, begin, output_dims, output, stride);
  282. break;
  283. case DT_DOUBLE:
  284. ret = SetOutputSliceDataByDataType<double>(data, data_size, input_dims, begin, output_dims, output, stride);
  285. break;
  286. case DT_FLOAT16:
  287. ret = SetOutputSliceDataByDataType<fp16_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  288. break;
  289. case DT_UINT8:
  290. ret = SetOutputSliceDataByDataType<uint8_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  291. break;
  292. case DT_INT8:
  293. ret = SetOutputSliceDataByDataType<int8_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  294. break;
  295. case DT_UINT16:
  296. ret = SetOutputSliceDataByDataType<uint16_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  297. break;
  298. case DT_INT16:
  299. ret = SetOutputSliceDataByDataType<int16_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  300. break;
  301. case DT_UINT32:
  302. ret = SetOutputSliceDataByDataType<uint32_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  303. break;
  304. case DT_UINT64:
  305. ret = SetOutputSliceDataByDataType<uint64_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  306. break;
  307. case DT_INT64:
  308. ret = SetOutputSliceDataByDataType<int64_t>(data, data_size, input_dims, begin, output_dims, output, stride);
  309. break;
  310. default:
  311. GELOGW("Unsupported data type: %s", TypeUtils::DataTypeToSerialString(static_cast<DataType>(data_type)).c_str());
  312. return PARAM_INVALID;
  313. }
  314. return ret;
  315. }
  316. void OpUtils::TransDataHWCK2KCHW(const void *input, int64_t h, int64_t w, int64_t c, int64_t k, void **output) {
  317. if (input == nullptr) {
  318. return;
  319. }
  320. if (output == nullptr) {
  321. return;
  322. }
  323. const char *w_data = (const char *)input;
  324. int64_t count = h * w * c * k;
  325. GE_IF_BOOL_EXEC(count <= 0, GELOGW("Count value must be greater than 0, but count = %ld", count); return);
  326. float *buf = new (std::nothrow) float[count]();
  327. GE_RT_VOID_CHECK_NOTNULL(buf);
  328. float *src_buff = nullptr;
  329. float *dst_buff = nullptr;
  330. for (int h_i = 0; h_i < h; ++h_i) {
  331. for (int w_i = 0; w_i < w; ++w_i) {
  332. for (int c_i = 0; c_i < c; ++c_i) {
  333. for (int k_i = 0; k_i < k; ++k_i) {
  334. src_buff = reinterpret_cast<float *>(const_cast<char *>(w_data)) +
  335. ((h_i * w * c * k) + (w_i * c * k) + (c_i * k) + (k_i));
  336. dst_buff = buf + ((k_i * c * h * w) + (c_i * h * w) + (h_i * w) + (w_i));
  337. *dst_buff = *src_buff;
  338. }
  339. }
  340. }
  341. }
  342. *output = buf;
  343. }
  344. void OpUtils::TransDataKCHW2HWCK(const void *input, int64_t k, int64_t c, int64_t h, int64_t w, void *output) {
  345. if ((input == nullptr) || (output == nullptr)) {
  346. GELOGD("%s[%d]: input param is nullptr.", __FILE__, __LINE__);
  347. return;
  348. }
  349. const char *w_data = (const char *)input;
  350. float *buf = reinterpret_cast<float *>(output);
  351. float *src_buff = nullptr;
  352. float *dst_buff = nullptr;
  353. for (int k_i = 0; k_i < k; ++k_i) {
  354. for (int c_i = 0; c_i < c; ++c_i) {
  355. for (int h_i = 0; h_i < h; ++h_i) {
  356. for (int w_i = 0; w_i < w; ++w_i) {
  357. src_buff = reinterpret_cast<float *>(const_cast<char *>(w_data)) +
  358. ((k_i * c * h * w) + (c_i * h * w) + (h_i * w) + (w_i));
  359. dst_buff = buf + ((h_i * w * c * k) + (w_i * c * k) + (c_i * k) + (k_i));
  360. *dst_buff = *src_buff;
  361. }
  362. }
  363. }
  364. }
  365. }
  366. vector<ConstGeTensorPtr> OpUtils::GetWeights(const ge::Node &node) { return OpDescUtils::GetWeights(node); }
  367. vector<ConstGeTensorPtr> OpUtils::GetWeights(ge::ConstNodePtr node) { return OpDescUtils::GetWeights(node); }
  368. vector<GeTensorPtr> OpUtils::MutableWeights(const ge::Node &node) { return OpDescUtils::MutableWeights(node); }
  369. vector<GeTensorPtr> OpUtils::MutableWeights(const ge::NodePtr node) { return OpDescUtils::MutableWeights(node); }
  370. Status OpUtils::SetWeights(ge::Node &node, const vector<ge::GeTensorPtr> &weights) {
  371. return OpDescUtils::SetWeights(node, weights);
  372. }
  373. Status OpUtils::SetWeights(ge::NodePtr node, const vector<ge::GeTensorPtr> &weights) {
  374. return OpDescUtils::SetWeights(node, weights);
  375. }
  376. // The caller guarantees that the input sensor is constant
  377. Status OpUtils::GetShapeDataFromConstTensor(const ConstGeTensorPtr &tensor, DataType type, std::vector<int64_t> &dims) {
  378. if (tensor == nullptr) {
  379. GELOGE(PARAM_INVALID, "[Check][Param]Input tensor is nullptr");
  380. REPORT_INNER_ERROR("E19999", "Input tensor is nullptr");
  381. return PARAM_INVALID;
  382. }
  383. // If the tensor data is a vector, the shape dimension must be 1
  384. if (tensor->GetTensorDesc().GetShape().GetDims().size() > 1) {
  385. GELOGE(PARAM_INVALID, "[Check][Param]The dimension of the input tensor shape cannot be more than 1, it is %zu",
  386. tensor->GetTensorDesc().GetShape().GetDims().size());
  387. REPORT_CALL_ERROR("E19999", "The dimension of the input tensor shape %zu invalid, more than 1",
  388. tensor->GetTensorDesc().GetShape().GetDims().size());
  389. return PARAM_INVALID;
  390. }
  391. if (type == DT_INT32) {
  392. int32_t *shape_data = const_cast<int32_t *>(reinterpret_cast<const int32_t *>(tensor->GetData().GetData()));
  393. GE_CHECK_NOTNULL(shape_data);
  394. size_t dims_num = tensor->GetData().size() / sizeof(int32_t);
  395. for (size_t i = 0; i < dims_num; i++) {
  396. dims.push_back(static_cast<int64_t>(shape_data[i]));
  397. }
  398. } else if (type == DT_INT64) {
  399. int64_t *shape_data = const_cast<int64_t *>(reinterpret_cast<const int64_t *>(tensor->GetData().GetData()));
  400. GE_CHECK_NOTNULL(shape_data);
  401. size_t dims_num = tensor->GetData().size() / sizeof(int64_t);
  402. for (size_t i = 0; i < dims_num; i++) {
  403. dims.push_back(shape_data[i]);
  404. }
  405. } else {
  406. GELOGE(PARAM_INVALID, "[Check][DataType]Invalid, type only can be DT_INT32 or DT_INT64, type is %s",
  407. TypeUtils::DataTypeToSerialString(type).c_str());
  408. REPORT_INNER_ERROR("E19999", "Data type %s check invalid, only can be DT_INT32 or DT_INT64",
  409. TypeUtils::DataTypeToSerialString(type).c_str());
  410. return PARAM_INVALID;
  411. }
  412. return SUCCESS;
  413. }
  414. uint32_t OpUtils::GetRealDimCnt(const GeTensorDesc &tensor_desc) {
  415. uint32_t real_dim_cnt = 0;
  416. domi::Status ret = TensorUtils::GetRealDimCnt(tensor_desc, real_dim_cnt);
  417. return (ret == domi::SUCCESS) ? real_dim_cnt : 0;
  418. }
  419. } // namespace ge

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