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

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