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.

single_op_parser.cc 24 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 "single_op_parser.h"
  17. #include <vector>
  18. #include <algorithm>
  19. #include <fstream>
  20. #include <sstream>
  21. #include <nlohmann/json.hpp>
  22. #include "framework/common/debug/ge_log.h"
  23. #include "common/util/error_manager/error_manager.h"
  24. #include "common/ge_inner_error_codes.h"
  25. #include "framework/common/util.h"
  26. #include "graph/utils/tensor_utils.h"
  27. #include "graph/utils/type_utils.h"
  28. #include "graph/utils/op_desc_utils.h"
  29. #include "graph/operator_factory_impl.h"
  30. using Json = nlohmann::json;
  31. using std::string;
  32. using std::vector;
  33. using std::map;
  34. namespace ge {
  35. namespace {
  36. constexpr char const *kKeyOp = "op";
  37. constexpr char const *kKeyInputDesc = "input_desc";
  38. constexpr char const *kKeyOutputDesc = "output_desc";
  39. constexpr char const *kKeyAttr = "attr";
  40. constexpr char const *kKeyName = "name";
  41. constexpr char const *kKeyType = "type";
  42. constexpr char const *kKeyShape = "shape";
  43. constexpr char const *kKeyOriginShape = "origin_shape";
  44. constexpr char const *kKeyShapeRange = "shape_range";
  45. constexpr char const *kKeyValue = "value";
  46. constexpr char const *kKeyFormat = "format";
  47. constexpr char const *kKeyOriginFormat = "origin_format";
  48. constexpr char const *kFileSuffix = ".om";
  49. constexpr char const *kKeyDynamicInput = "dynamic_input";
  50. constexpr char const *kKeyDynamicOutput = "dynamic_output";
  51. constexpr int kDumpJsonIndent = 2;
  52. constexpr int kShapeRangePairSize = 2;
  53. constexpr int kShapeRangeLow = 0;
  54. constexpr int kShapeRangeHigh = 1;
  55. constexpr int kMaxFileNameLen = 128;
  56. map<string, GeAttrValue::ValueType> kAttrTypeDict = {
  57. {"bool", GeAttrValue::VT_BOOL},
  58. {"int", GeAttrValue::VT_INT},
  59. {"float", GeAttrValue::VT_FLOAT},
  60. {"string", GeAttrValue::VT_STRING},
  61. {"list_bool", GeAttrValue::VT_LIST_BOOL},
  62. {"list_int", GeAttrValue::VT_LIST_INT},
  63. {"list_float", GeAttrValue::VT_LIST_FLOAT},
  64. {"list_string", GeAttrValue::VT_LIST_STRING},
  65. {"list_list_int", GeAttrValue::VT_LIST_LIST_INT},
  66. {"data_type", GeAttrValue::VT_DATA_TYPE},
  67. };
  68. map<string, DataType> kDataTypeDict = {
  69. {"bool", DT_BOOL},
  70. {"int8", DT_INT8},
  71. {"uint8", DT_UINT8},
  72. {"int16", DT_INT16},
  73. {"uint16", DT_UINT16},
  74. {"int32", DT_INT32},
  75. {"uint32", DT_UINT32},
  76. {"int64", DT_INT64},
  77. {"uint64", DT_UINT64},
  78. {"float16", DT_FLOAT16},
  79. {"half", DT_FLOAT16},
  80. {"fp16", DT_FLOAT16},
  81. {"float", DT_FLOAT},
  82. {"float32", DT_FLOAT},
  83. {"double", DT_DOUBLE},
  84. };
  85. map<string, Format> kFormatDict = {
  86. {"nchw", FORMAT_NCHW},
  87. {"nhwc", FORMAT_NHWC},
  88. {"nd", FORMAT_ND},
  89. {"nc1hwc0", FORMAT_NC1HWC0},
  90. {"fractal_z", FORMAT_FRACTAL_Z},
  91. {"nc1c0hwpad", FORMAT_NC1C0HWPAD},
  92. {"nhwc1c0", FORMAT_NHWC1C0},
  93. {"fsr_nchw", FORMAT_FSR_NCHW},
  94. {"fractal_deconv", FORMAT_FRACTAL_DECONV},
  95. {"c1hwnc0", FORMAT_C1HWNC0},
  96. {"fractal_deconv_transpose", FORMAT_FRACTAL_DECONV_TRANSPOSE},
  97. {"fractal_deconv_sp_stride_trans", FORMAT_FRACTAL_DECONV_SP_STRIDE_TRANS},
  98. {"nc1hwc0_c04", FORMAT_NC1HWC0_C04},
  99. {"fractal_z_c04", FORMAT_FRACTAL_Z_C04},
  100. {"chwn", FORMAT_CHWN},
  101. {"deconv_sp_stride8_trans", FORMAT_FRACTAL_DECONV_SP_STRIDE8_TRANS},
  102. {"nc1khkwhwc0", FORMAT_NC1KHKWHWC0},
  103. {"bn_weight", FORMAT_BN_WEIGHT},
  104. {"filter_hwck", FORMAT_FILTER_HWCK},
  105. {"hwcn", FORMAT_HWCN},
  106. {"lookup_lookups", FORMAT_HASHTABLE_LOOKUP_LOOKUPS},
  107. {"lookup_keys", FORMAT_HASHTABLE_LOOKUP_KEYS},
  108. {"lookup_value", FORMAT_HASHTABLE_LOOKUP_VALUE},
  109. {"lookup_output", FORMAT_HASHTABLE_LOOKUP_OUTPUT},
  110. {"lookup_hits", FORMAT_HASHTABLE_LOOKUP_HITS},
  111. {"md", FORMAT_MD},
  112. {"c1hwncoc0", FORMAT_C1HWNCoC0},
  113. {"fractal_nz", FORMAT_FRACTAL_NZ},
  114. {"ndhwc", FORMAT_NDHWC},
  115. {"ncdhw", FORMAT_NCDHW},
  116. {"dhwcn", FORMAT_DHWCN},
  117. {"dhwnc", FORMAT_DHWNC},
  118. {"ndc1hwc0", FORMAT_NDC1HWC0},
  119. {"fractal_z_3d", FORMAT_FRACTAL_Z_3D},
  120. {"fractal_z_3d_transpose", FORMAT_FRACTAL_Z_3D_TRANSPOSE},
  121. {"cn", FORMAT_CN},
  122. {"nc", FORMAT_NC},
  123. {"fractal_zn_lstm", FORMAT_FRACTAL_ZN_LSTM},
  124. {"fractal_z_g", FORMAT_FRACTAL_Z_G}
  125. };
  126. std::string GenerateFileName(const SingleOpDesc &single_op_desc, int index) {
  127. std::stringstream file_name_ss;
  128. file_name_ss << index;
  129. file_name_ss << "_" << single_op_desc.op;
  130. for (auto &desc : single_op_desc.input_desc) {
  131. file_name_ss << "_" << desc.type << "_" << desc.format;
  132. for (auto dim : desc.dims) {
  133. file_name_ss << "_" << dim;
  134. }
  135. }
  136. for (auto &desc : single_op_desc.output_desc) {
  137. file_name_ss << "_" << desc.type << "_" << desc.format;
  138. for (auto dim : desc.dims) {
  139. file_name_ss << "_" << dim;
  140. }
  141. }
  142. std::string file_name = file_name_ss.str();
  143. if (file_name.length() > kMaxFileNameLen) {
  144. GELOGI("Trim file name for it is too long, origin file name = %s", file_name.c_str());
  145. file_name = file_name.substr(0, kMaxFileNameLen);
  146. }
  147. file_name += kFileSuffix;
  148. return file_name;
  149. }
  150. } // namespace
  151. template<typename T>
  152. void SetAttrValue(const Json &j, SingleOpAttr &attr) {
  153. attr.value.SetValue<T>(j.at(kKeyValue).get<T>());
  154. }
  155. template<typename T>
  156. T GetValue(const map<string, T> &dict, string &key, T default_val) {
  157. transform(key.begin(), key.end(), key.begin(), ::tolower);
  158. auto it = dict.find(key);
  159. if (it == dict.end()) {
  160. return default_val;
  161. }
  162. return it->second;
  163. }
  164. void from_json(const Json &j, SingleOpTensorDesc &desc) {
  165. bool is_tensor_valid = true;
  166. desc.dims = j.at(kKeyShape).get<vector<int64_t>>();
  167. auto it = j.find(kKeyShapeRange);
  168. if (it != j.end()) {
  169. desc.dim_ranges = j.at(kKeyShapeRange).get<vector<std::vector<int64_t>>>();
  170. }
  171. it = j.find(kKeyOriginShape);
  172. if (it != j.end()) {
  173. desc.ori_dims = j.at(kKeyOriginShape).get<vector<int64_t>>();
  174. }
  175. string format_str = j.at(kKeyFormat).get<string>();
  176. string type_str = j.at(kKeyType).get<string>();
  177. desc.format = GetValue(kFormatDict, format_str, FORMAT_RESERVED);
  178. desc.type = GetValue(kDataTypeDict, type_str, DT_UNDEFINED);
  179. is_tensor_valid = is_tensor_valid && ge::TypeUtils::IsFormatValid(format_str);
  180. is_tensor_valid = is_tensor_valid && ge::TypeUtils::IsDataTypeValid(type_str);
  181. it = j.find(kKeyOriginFormat);
  182. if (it != j.end()) {
  183. string origin_format_str = j.at(kKeyOriginFormat).get<string>();
  184. is_tensor_valid = is_tensor_valid && ge::TypeUtils::IsFormatValid(origin_format_str);
  185. desc.ori_format = GetValue(kFormatDict, origin_format_str, FORMAT_RESERVED);
  186. }
  187. auto tensor_name = j.find(kKeyName);
  188. if (tensor_name != j.end()) {
  189. desc.name = tensor_name->get<string>();
  190. }
  191. auto dynamic_input_name = j.find(kKeyDynamicInput);
  192. if (dynamic_input_name != j.end()) {
  193. desc.dynamic_input_name = dynamic_input_name->get<string>();
  194. }
  195. if (!is_tensor_valid) {
  196. desc.SetValidFlag(is_tensor_valid);
  197. }
  198. }
  199. void from_json(const Json &j, SingleOpAttr &attr) {
  200. attr.name = j.at(kKeyName).get<string>();
  201. attr.type = j.at(kKeyType).get<string>();
  202. auto it = kAttrTypeDict.find(attr.type);
  203. if (it == kAttrTypeDict.end()) {
  204. GELOGE(UNSUPPORTED, "Parse attr[%s] failed. Unsupported type: %s", attr.name.c_str(), attr.type.c_str());
  205. return;
  206. }
  207. switch (it->second) {
  208. case GeAttrValue::VT_BOOL:
  209. SetAttrValue<bool>(j, attr);
  210. break;
  211. case GeAttrValue::VT_INT:
  212. SetAttrValue<int64_t>(j, attr);
  213. break;
  214. case GeAttrValue::VT_FLOAT:
  215. SetAttrValue<float>(j, attr);
  216. break;
  217. case GeAttrValue::VT_STRING:
  218. SetAttrValue<string>(j, attr);
  219. break;
  220. case GeAttrValue::VT_LIST_BOOL:
  221. SetAttrValue<vector<bool>>(j, attr);
  222. break;
  223. case GeAttrValue::VT_LIST_INT:
  224. SetAttrValue<vector<int64_t>>(j, attr);
  225. break;
  226. case GeAttrValue::VT_LIST_FLOAT:
  227. SetAttrValue<vector<float>>(j, attr);
  228. break;
  229. case GeAttrValue::VT_LIST_STRING:
  230. SetAttrValue<vector<string>>(j, attr);
  231. break;
  232. case GeAttrValue::VT_LIST_LIST_INT:
  233. SetAttrValue<vector<vector<int64_t>>>(j, attr);
  234. break;
  235. case GeAttrValue::VT_DATA_TYPE:
  236. SetAttrValue<DataType>(j, attr);
  237. break;
  238. default:
  239. GELOGE(UNSUPPORTED, "Parse attr[%s] failed. Unsupported type: %s", attr.name.c_str(), attr.type.c_str());
  240. break;
  241. }
  242. }
  243. void from_json(const Json &j, SingleOpDesc &desc) {
  244. desc.op = j.at(kKeyOp).get<string>();
  245. auto input_desc = j.find(kKeyInputDesc);
  246. if (input_desc != j.end()) {
  247. desc.input_desc = input_desc->get<vector<SingleOpTensorDesc>>();
  248. }
  249. auto output_desc = j.find(kKeyOutputDesc);
  250. if (output_desc != j.end()) {
  251. desc.output_desc = output_desc->get<vector<SingleOpTensorDesc>>();
  252. }
  253. auto attr_field = j.find(kKeyAttr);
  254. if (attr_field != j.end()) {
  255. desc.attrs = attr_field->get<vector<SingleOpAttr>>();
  256. }
  257. }
  258. Status SingleOpParser::ReadJsonFile(const std::string &file, Json &json_obj) {
  259. std::string real_path = RealPath(file.c_str());
  260. if (real_path.empty()) {
  261. ErrorManager::GetInstance().ATCReportErrMessage("E10023", {"value"}, {file});
  262. GELOGE(FAILED, "Input parameter[--singleop]'s value[%s] is not a valid path.", file.c_str());
  263. return INTERNAL_ERROR;
  264. }
  265. std::ifstream ifs(real_path);
  266. if (!ifs.is_open()) {
  267. ErrorManager::GetInstance().ATCReportErrMessage("E10024", {"value"}, {file});
  268. GELOGE(FAILED, "Open file[%s] provided in input parameter[--singleop] failed.", file.c_str());
  269. return FAILED;
  270. }
  271. try {
  272. ifs >> json_obj;
  273. } catch (const std::exception &e) {
  274. ErrorManager::GetInstance().ATCReportErrMessage("E10025", {"realpath", "errmsg"}, {real_path, e.what()});
  275. GELOGE(PARAM_INVALID, "Parse file[%s] provided in input parameter[--singleop] failed, exception = %s.",
  276. real_path.c_str(), e.what());
  277. return PARAM_INVALID;
  278. }
  279. ifs.close();
  280. return SUCCESS;
  281. }
  282. bool SingleOpParser::Validate(const SingleOpDesc &op_desc) {
  283. if (op_desc.op.empty()) {
  284. ErrorManager::GetInstance().ATCReportErrMessage("E10026");
  285. GELOGE(PARAM_INVALID, "Op name is empty");
  286. return false;
  287. }
  288. int index = 0;
  289. for (auto &tensor_desc : op_desc.input_desc) {
  290. if (!tensor_desc.GetValidFlag()) {
  291. ErrorManager::GetInstance().ATCReportErrMessage("E10027", {"input", "type", "index"},
  292. {"intput", "datatype or format", std::to_string(index)});
  293. GELOGE(PARAM_INVALID, "Input's dataType or format is invalid when the index is %d", index);
  294. return false;
  295. }
  296. if ((tensor_desc.type == DT_UNDEFINED && tensor_desc.format != FORMAT_RESERVED) ||
  297. (tensor_desc.type != DT_UNDEFINED && tensor_desc.format == FORMAT_RESERVED)){
  298. ErrorManager::GetInstance().ATCReportErrMessage("E10027", {"input", "type", "index"},
  299. {"intput", "datatype or format", std::to_string(index)});
  300. GELOGE(PARAM_INVALID, "Input's dataType or format is invalid when the index is %d", index);
  301. return false;
  302. }
  303. ++index;
  304. }
  305. index = 0;
  306. for (auto &tensor_desc : op_desc.output_desc) {
  307. if (!tensor_desc.GetValidFlag()) {
  308. ErrorManager::GetInstance().ATCReportErrMessage("E10027", {"input", "type", "index"},
  309. {"output", "datatype", std::to_string(index)});
  310. GELOGE(PARAM_INVALID, "Output's dataType is invalid when the index is %d", index);
  311. return false;
  312. }
  313. if (tensor_desc.type == DT_UNDEFINED) {
  314. ErrorManager::GetInstance().ATCReportErrMessage("E10027", {"input", "type", "index"},
  315. {"output", "datatype", std::to_string(index)});
  316. GELOGE(PARAM_INVALID, "Output's dataType is invalid when the index is %d", index);
  317. return false;
  318. }
  319. if (tensor_desc.format == FORMAT_RESERVED) {
  320. ErrorManager::GetInstance().ATCReportErrMessage("E10027", {"input", "type", "index"},
  321. {"output", "format", std::to_string(index)});
  322. GELOGE(PARAM_INVALID, "Output's format is invalid when the index is %d", index);
  323. return false;
  324. }
  325. ++index;
  326. }
  327. for (auto &attr : op_desc.attrs) {
  328. if (attr.name.empty()) {
  329. ErrorManager::GetInstance().ATCReportErrMessage("E10029");
  330. GELOGE(PARAM_INVALID, "attr name is empty");
  331. return false;
  332. }
  333. if (attr.value.IsEmpty()) {
  334. ErrorManager::GetInstance().ATCReportErrMessage("E10030", {"attrname"}, {attr.name});
  335. GELOGE(PARAM_INVALID, "Parse attr \"%s\" failed. ", attr.name.c_str());
  336. return false;
  337. }
  338. }
  339. return true;
  340. }
  341. std::unique_ptr<OpDesc> SingleOpParser::CreateOpDesc(const string &op_type) {
  342. return std::unique_ptr<OpDesc>(new(std::nothrow) OpDesc(op_type, op_type));
  343. }
  344. Status SingleOpParser::UpdateDynamicTensorName(std::vector<SingleOpTensorDesc> &desc) {
  345. std::map<std::string, int> dynamic_name_map;
  346. for (auto &tensor : desc) {
  347. if (tensor.dynamic_input_name.empty()) {
  348. continue;
  349. }
  350. if (dynamic_name_map.find(tensor.dynamic_input_name) == dynamic_name_map.end()) {
  351. dynamic_name_map[tensor.dynamic_input_name] = 0;
  352. } else {
  353. dynamic_name_map[tensor.dynamic_input_name]++;
  354. }
  355. tensor.name = tensor.dynamic_input_name + std::to_string(dynamic_name_map[tensor.dynamic_input_name]);
  356. }
  357. GELOGD("Update dynamic tensor name success!");
  358. return SUCCESS;
  359. }
  360. Status SingleOpParser::ConvertToBuildParam(int index,
  361. const SingleOpDesc &single_op_desc,
  362. SingleOpBuildParam &build_param) {
  363. auto op_desc = CreateOpDesc(single_op_desc.op);
  364. GE_CHECK_NOTNULL(op_desc);
  365. for (auto &desc : single_op_desc.input_desc) {
  366. GeTensorDesc ge_tensor_desc(GeShape(desc.dims),
  367. desc.format,
  368. desc.type);
  369. auto ori_format_to_set = desc.ori_format != FORMAT_RESERVED ? desc.ori_format : desc.format;
  370. auto ori_dims = !desc.ori_dims.empty() ? desc.ori_dims : desc.dims;
  371. ge_tensor_desc.SetOriginFormat(ori_format_to_set);
  372. ge_tensor_desc.SetOriginShape(GeShape(ori_dims));
  373. GE_CHK_STATUS_RET_NOLOG(SetShapeRange(op_desc->GetName(), desc, ge_tensor_desc));
  374. TensorUtils::SetRealDimCnt(ge_tensor_desc, ori_dims.size());
  375. TensorUtils::SetInputTensor(ge_tensor_desc, true);
  376. TensorUtils::SetOutputTensor(ge_tensor_desc, false);
  377. if (desc.name.empty()) {
  378. op_desc->AddInputDesc(ge_tensor_desc);
  379. } else {
  380. op_desc->AddInputDesc(desc.name, ge_tensor_desc);
  381. }
  382. build_param.inputs.emplace_back(ge_tensor_desc);
  383. }
  384. for (auto &desc : single_op_desc.output_desc) {
  385. GeTensorDesc ge_tensor_desc(GeShape(desc.dims),
  386. desc.format,
  387. desc.type);
  388. auto ori_format_to_set = desc.ori_format != FORMAT_RESERVED ? desc.ori_format : desc.format;
  389. auto ori_dims = !desc.ori_dims.empty() ? desc.ori_dims : desc.dims;
  390. ge_tensor_desc.SetOriginFormat(ori_format_to_set);
  391. ge_tensor_desc.SetOriginShape(GeShape(ori_dims));
  392. GE_CHK_STATUS_RET_NOLOG(SetShapeRange(op_desc->GetName(), desc, ge_tensor_desc));
  393. TensorUtils::SetRealDimCnt(ge_tensor_desc, ori_dims.size());
  394. TensorUtils::SetInputTensor(ge_tensor_desc, false);
  395. TensorUtils::SetOutputTensor(ge_tensor_desc, true);
  396. if (desc.name.empty()) {
  397. op_desc->AddOutputDesc(ge_tensor_desc);
  398. } else {
  399. op_desc->AddOutputDesc(desc.name, ge_tensor_desc);
  400. }
  401. build_param.outputs.emplace_back(ge_tensor_desc);
  402. }
  403. for (const auto &attr : single_op_desc.attrs) {
  404. op_desc->SetAttr(attr.name, attr.value);
  405. }
  406. if (VerifyOpInputOutputSizeByIr(*op_desc) != SUCCESS) {
  407. GELOGE(PARAM_INVALID, "Verify op [%s] input or output size failed.", op_desc->GetType().c_str());
  408. return PARAM_INVALID;
  409. }
  410. build_param.file_name = GenerateFileName(single_op_desc, index);
  411. build_param.op_desc.reset(op_desc.release());
  412. return SUCCESS;
  413. }
  414. Status SingleOpParser::VerifyOpInputOutputSizeByIr(const OpDesc &current_op_desc) {
  415. ge::Operator operator_ir = ge::OperatorFactory::CreateOperator("tmp_operator", current_op_desc.GetType());
  416. if (!operator_ir.IsEmpty()) {
  417. auto opdesc_ir = ge::OpDescUtils::GetOpDescFromOperator(operator_ir);
  418. GE_CHECK_NOTNULL(opdesc_ir);
  419. size_t current_opdesc_inputs_num = current_op_desc.GetInputsSize();
  420. size_t ir_opdesc_inputs_num = opdesc_ir->GetInputsSize();
  421. if (current_opdesc_inputs_num < ir_opdesc_inputs_num) {
  422. string reason = "is smaller than the ir needed input size " + std::to_string(ir_opdesc_inputs_num);
  423. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  424. {current_op_desc.GetName(), "input size " + std::to_string(current_opdesc_inputs_num), reason});
  425. GELOGE(PARAM_INVALID, "This op [%s] input size %zu is smaller than the ir needed input size %zu",
  426. current_op_desc.GetName().c_str(), current_opdesc_inputs_num, ir_opdesc_inputs_num);
  427. return PARAM_INVALID;
  428. }
  429. size_t current_opdesc_outputs_num = current_op_desc.GetOutputsSize();
  430. size_t ir_opdesc_outputs_num = opdesc_ir->GetOutputsSize();
  431. if (current_opdesc_outputs_num < ir_opdesc_outputs_num) {
  432. string reason = "is smaller than the ir needed output size " + std::to_string(ir_opdesc_outputs_num);
  433. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  434. {current_op_desc.GetName(), "output size " + std::to_string(current_opdesc_outputs_num), reason});
  435. GELOGE(PARAM_INVALID, "This op [%s] output size %zu is smaller than the ir needed output size %zu",
  436. current_op_desc.GetName().c_str(), current_opdesc_outputs_num, ir_opdesc_outputs_num);
  437. return PARAM_INVALID;
  438. }
  439. }
  440. return SUCCESS;
  441. }
  442. Status SingleOpParser::SetShapeRange(const std::string &op_name,
  443. const SingleOpTensorDesc &tensor_desc,
  444. GeTensorDesc &ge_tensor_desc) {
  445. auto num_shape_ranges = tensor_desc.dim_ranges.size();
  446. GELOGD("Number of shape ranges = %zu", num_shape_ranges);
  447. auto it = std::find(tensor_desc.dims.begin(), tensor_desc.dims.end(), ge::UNKNOWN_DIM_NUM);
  448. if (it != tensor_desc.dims.end()) {
  449. if (tensor_desc.dims != ge::UNKNOWN_RANK) {
  450. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  451. {op_name,
  452. "shape",
  453. "has unknown rank but dim size is not one"});
  454. GELOGE(PARAM_INVALID, "Invalid tensor shape: [%s]", ge_tensor_desc.MutableShape().ToString().c_str());
  455. return PARAM_INVALID;
  456. }
  457. if (!tensor_desc.dim_ranges.empty()) {
  458. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  459. {op_name,
  460. "shape range",
  461. "is not needed while the rank the shape is unknown"});
  462. GELOGE(PARAM_INVALID, "Shape range is not needed while the rank the shape is unknown");
  463. return PARAM_INVALID;
  464. }
  465. GELOGD("Shape is unknown rank, do not set shape range");
  466. return SUCCESS;
  467. }
  468. std::vector<std::pair<int64_t, int64_t>> shape_range;
  469. size_t range_index = 0;
  470. for (auto dim : tensor_desc.dims) {
  471. if (dim >= 0) {
  472. shape_range.emplace_back(dim, dim);
  473. GELOGD("Adding shape range: [%ld, %ld]", dim, dim);
  474. } else {
  475. GELOGD("To get shape range by index = %zu", range_index);
  476. if (range_index >= num_shape_ranges) {
  477. string reason = "is smaller than the unknown dim size " + std::to_string(++range_index);
  478. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  479. {op_name,
  480. "shape range size " + std::to_string(num_shape_ranges),
  481. reason});
  482. GELOGE(PARAM_INVALID, "The number of shape_range mismatches that of unknown dims.");
  483. return PARAM_INVALID;
  484. }
  485. auto &range = tensor_desc.dim_ranges[range_index];
  486. if (range.size() != kShapeRangePairSize) {
  487. string reason = "has " + std::to_string(range.size()) + " item(s)";
  488. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  489. {op_name,
  490. "shape range " + std::to_string(range_index),
  491. reason});
  492. GELOGE(PARAM_INVALID, "Invalid shape range entry. index = %zu, size = %zu", range_index, range.size());
  493. return PARAM_INVALID;
  494. }
  495. shape_range.emplace_back(range[kShapeRangeLow], range[kShapeRangeHigh]);
  496. GELOGD("Adding shape range: [%ld, %ld]", range[kShapeRangeLow], range[kShapeRangeHigh]);
  497. ++range_index;
  498. }
  499. }
  500. if (num_shape_ranges != range_index) {
  501. string reason = "is greater than the unknown dim size " + std::to_string(range_index);
  502. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  503. {op_name,
  504. "shape range size " + std::to_string(num_shape_ranges),
  505. reason});
  506. GELOGE(PARAM_INVALID,
  507. "The number of shape_range(%zu) mismatches that of unknown dims(%zu).",
  508. num_shape_ranges,
  509. range_index);
  510. return PARAM_INVALID;
  511. }
  512. if (range_index > 0) {
  513. ge_tensor_desc.SetShapeRange(shape_range);
  514. }
  515. return SUCCESS;
  516. }
  517. Status SingleOpParser::ParseSingleOpList(const std::string &file, std::vector<SingleOpBuildParam> &op_list) {
  518. int index = 0;
  519. try {
  520. Json single_op_list_json;
  521. auto ret = ReadJsonFile(file, single_op_list_json);
  522. if (ret != SUCCESS) {
  523. return ret;
  524. }
  525. for (const Json &single_op_json : single_op_list_json) {
  526. SingleOpDesc single_op_desc;
  527. GELOGI("Parsing op[%d], jsonStr = %s", index, single_op_json.dump(kDumpJsonIndent).c_str());
  528. single_op_desc = single_op_json;
  529. if (UpdateDynamicTensorName(single_op_desc.input_desc) != SUCCESS) {
  530. GELOGE(FAILED, "Update dynamic tensor name failed!");
  531. return FAILED;
  532. }
  533. if (!Validate(single_op_desc)) {
  534. GELOGE(PARAM_INVALID, "Validate the index[%d] of op failed when read json file[%s].", index, file.c_str());
  535. return PARAM_INVALID;
  536. }
  537. SingleOpBuildParam param;
  538. ret = ConvertToBuildParam(index, single_op_desc, param);
  539. if (ret != SUCCESS) {
  540. return ret;
  541. }
  542. op_list.emplace_back(param);
  543. GELOGI("Parse the index[%d] of op success", index);
  544. index += 1;
  545. }
  546. } catch (const nlohmann::json::exception &e) {
  547. ErrorManager::GetInstance().ATCReportErrMessage("E10032", {"index", "jsonfile", "exception"},
  548. {std::to_string(index), file, e.what()});
  549. GELOGE(PARAM_INVALID, "Parse the index[%d] of op failed when read json file[%s], exception %s",
  550. index, file.c_str(), e.what());
  551. return PARAM_INVALID;
  552. }
  553. return SUCCESS;
  554. }
  555. } // namespace ge

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