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

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

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