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

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