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

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