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 26 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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 char const *kKeyCompileFlag = "compile_flag";
  52. constexpr int kDumpJsonIndent = 2;
  53. constexpr int kShapeRangePairSize = 2;
  54. constexpr int kShapeRangeLow = 0;
  55. constexpr int kShapeRangeHigh = 1;
  56. constexpr int kMaxFileNameLen = 128;
  57. map<string, GeAttrValue::ValueType> kAttrTypeDict = {
  58. {"bool", GeAttrValue::VT_BOOL},
  59. {"int", GeAttrValue::VT_INT},
  60. {"float", GeAttrValue::VT_FLOAT},
  61. {"string", GeAttrValue::VT_STRING},
  62. {"list_bool", GeAttrValue::VT_LIST_BOOL},
  63. {"list_int", GeAttrValue::VT_LIST_INT},
  64. {"list_float", GeAttrValue::VT_LIST_FLOAT},
  65. {"list_string", GeAttrValue::VT_LIST_STRING},
  66. {"list_list_int", GeAttrValue::VT_LIST_LIST_INT},
  67. {"data_type", GeAttrValue::VT_DATA_TYPE},
  68. };
  69. map<string, DataType> kDataTypeDict = {
  70. {"bool", DT_BOOL},
  71. {"int8", DT_INT8},
  72. {"uint8", DT_UINT8},
  73. {"int16", DT_INT16},
  74. {"uint16", DT_UINT16},
  75. {"int32", DT_INT32},
  76. {"uint32", DT_UINT32},
  77. {"int64", DT_INT64},
  78. {"uint64", DT_UINT64},
  79. {"float16", DT_FLOAT16},
  80. {"half", DT_FLOAT16},
  81. {"fp16", DT_FLOAT16},
  82. {"float", DT_FLOAT},
  83. {"float32", DT_FLOAT},
  84. {"double", DT_DOUBLE},
  85. {"complex64", DT_COMPLEX64}
  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. bool AttrValueIsString(const Json &j, const string &key) {
  154. try {
  155. string tmp_str = j.at(key).get<string>();
  156. return true;
  157. } catch (Json::type_error &e) {
  158. return false;
  159. }
  160. }
  161. template<typename T>
  162. T GetValue(const map<string, T> &dict, string &key, T default_val) {
  163. transform(key.begin(), key.end(), key.begin(), ::tolower);
  164. auto it = dict.find(key);
  165. if (it == dict.end()) {
  166. return default_val;
  167. }
  168. return it->second;
  169. }
  170. template<typename T>
  171. void SetAttrValue(const Json &j, SingleOpAttr &attr) {
  172. // when attr type is "data_type", we support two kinds of attr value.
  173. // 1. value: "DT_FLOAT", "DT_INT32", "DT_INT8" ...
  174. // 2. value: 1, 3 ...
  175. if (j.at(kKeyType).get<string>() == "data_type" && AttrValueIsString(j, kKeyValue)) {
  176. string type_str = j.at(kKeyValue).get<string>();
  177. DataType dtype = TypeUtils::SerialStringToDataType(type_str);
  178. attr.value.SetValue<DataType>(dtype);
  179. return;
  180. }
  181. attr.value.SetValue<T>(j.at(kKeyValue).get<T>());
  182. }
  183. void from_json(const Json &j, SingleOpTensorDesc &desc) {
  184. bool is_tensor_valid = true;
  185. desc.dims = j.at(kKeyShape).get<vector<int64_t>>();
  186. auto it = j.find(kKeyShapeRange);
  187. if (it != j.end()) {
  188. desc.dim_ranges = j.at(kKeyShapeRange).get<vector<std::vector<int64_t>>>();
  189. }
  190. it = j.find(kKeyOriginShape);
  191. if (it != j.end()) {
  192. desc.ori_dims = j.at(kKeyOriginShape).get<vector<int64_t>>();
  193. }
  194. string format_str = j.at(kKeyFormat).get<string>();
  195. string type_str = j.at(kKeyType).get<string>();
  196. desc.format = GetValue(kFormatDict, format_str, FORMAT_RESERVED);
  197. desc.type = GetValue(kDataTypeDict, type_str, DT_UNDEFINED);
  198. is_tensor_valid = is_tensor_valid && ge::TypeUtils::IsFormatValid(format_str);
  199. is_tensor_valid = is_tensor_valid && ge::TypeUtils::IsDataTypeValid(type_str);
  200. it = j.find(kKeyOriginFormat);
  201. if (it != j.end()) {
  202. string origin_format_str = j.at(kKeyOriginFormat).get<string>();
  203. is_tensor_valid = is_tensor_valid && ge::TypeUtils::IsFormatValid(origin_format_str);
  204. desc.ori_format = GetValue(kFormatDict, origin_format_str, FORMAT_RESERVED);
  205. }
  206. auto tensor_name = j.find(kKeyName);
  207. if (tensor_name != j.end()) {
  208. desc.name = tensor_name->get<string>();
  209. }
  210. auto dynamic_input_name = j.find(kKeyDynamicInput);
  211. if (dynamic_input_name != j.end()) {
  212. desc.dynamic_input_name = dynamic_input_name->get<string>();
  213. }
  214. if (!is_tensor_valid) {
  215. desc.SetValidFlag(is_tensor_valid);
  216. }
  217. }
  218. void from_json(const Json &j, SingleOpAttr &attr) {
  219. attr.name = j.at(kKeyName).get<string>();
  220. attr.type = j.at(kKeyType).get<string>();
  221. auto it = kAttrTypeDict.find(attr.type);
  222. if (it == kAttrTypeDict.end()) {
  223. GELOGE(UNSUPPORTED, "[Find][JsonAttr] name=%s, type=%s failed for Unsupported type.",
  224. attr.name.c_str(), attr.type.c_str());
  225. REPORT_INNER_ERROR("E19999", "Find jsonattr name=%s, type=%s failed for Unsupported type.",
  226. attr.name.c_str(), attr.type.c_str());
  227. return;
  228. }
  229. switch (it->second) {
  230. case GeAttrValue::VT_BOOL:
  231. SetAttrValue<bool>(j, attr);
  232. break;
  233. case GeAttrValue::VT_INT:
  234. SetAttrValue<int64_t>(j, attr);
  235. break;
  236. case GeAttrValue::VT_FLOAT:
  237. SetAttrValue<float>(j, attr);
  238. break;
  239. case GeAttrValue::VT_STRING:
  240. SetAttrValue<string>(j, attr);
  241. break;
  242. case GeAttrValue::VT_LIST_BOOL:
  243. SetAttrValue<vector<bool>>(j, attr);
  244. break;
  245. case GeAttrValue::VT_LIST_INT:
  246. SetAttrValue<vector<int64_t>>(j, attr);
  247. break;
  248. case GeAttrValue::VT_LIST_FLOAT:
  249. SetAttrValue<vector<float>>(j, attr);
  250. break;
  251. case GeAttrValue::VT_LIST_STRING:
  252. SetAttrValue<vector<string>>(j, attr);
  253. break;
  254. case GeAttrValue::VT_LIST_LIST_INT:
  255. SetAttrValue<vector<vector<int64_t>>>(j, attr);
  256. break;
  257. case GeAttrValue::VT_DATA_TYPE:
  258. SetAttrValue<DataType>(j, attr);
  259. break;
  260. default:
  261. GELOGE(UNSUPPORTED, "[Find][JsonAttr] name=%s, type=%s failed for Unsupported type.",
  262. attr.name.c_str(), attr.type.c_str());
  263. REPORT_INNER_ERROR("E19999", "Find jsonattr name=%s, type=%s failed for Unsupported type.",
  264. attr.name.c_str(), attr.type.c_str());
  265. break;
  266. }
  267. }
  268. void from_json(const Json &j, SingleOpDesc &desc) {
  269. auto op = j.find(kKeyOp);
  270. if (op != j.end()) {
  271. desc.op = j.at(kKeyOp).get<string>();
  272. }
  273. auto input_desc = j.find(kKeyInputDesc);
  274. if (input_desc != j.end()) {
  275. desc.input_desc = input_desc->get<vector<SingleOpTensorDesc>>();
  276. }
  277. auto output_desc = j.find(kKeyOutputDesc);
  278. if (output_desc != j.end()) {
  279. desc.output_desc = output_desc->get<vector<SingleOpTensorDesc>>();
  280. }
  281. auto attr_field = j.find(kKeyAttr);
  282. if (attr_field != j.end()) {
  283. desc.attrs = attr_field->get<vector<SingleOpAttr>>();
  284. }
  285. auto compile_flag = j.find(kKeyCompileFlag);
  286. if (compile_flag != j.end()) {
  287. desc.compile_flag = compile_flag->get<int32_t>();
  288. }
  289. }
  290. Status SingleOpParser::ReadJsonFile(const std::string &file, Json &json_obj) {
  291. std::string real_path = RealPath(file.c_str());
  292. if (real_path.empty()) {
  293. ErrorManager::GetInstance().ATCReportErrMessage("E10023", {"value"}, {file});
  294. GELOGE(FAILED, "[Read][JsonFile]Input parameter[--singleop]'s value[%s] is not a valid path.", file.c_str());
  295. return INTERNAL_ERROR;
  296. }
  297. std::ifstream ifs(real_path);
  298. if (!ifs.is_open()) {
  299. ErrorManager::GetInstance().ATCReportErrMessage("E10024", {"value"}, {file});
  300. GELOGE(FAILED, "[Open][JsonFile] failed for file[%s] provided in input parameter[--singleop].", file.c_str());
  301. return FAILED;
  302. }
  303. try {
  304. ifs >> json_obj;
  305. } catch (const std::exception &e) {
  306. ErrorManager::GetInstance().ATCReportErrMessage("E10025", {"realpath", "errmsg"}, {real_path, e.what()});
  307. GELOGE(PARAM_INVALID,
  308. "[Parse][JsonFile] fail for file[%s] provided in input parameter[--singleop], exception = %s.",
  309. real_path.c_str(), e.what());
  310. return PARAM_INVALID;
  311. }
  312. ifs.close();
  313. return SUCCESS;
  314. }
  315. bool SingleOpParser::Validate(const SingleOpDesc &op_desc) {
  316. if (op_desc.op.empty()) {
  317. ErrorManager::GetInstance().ATCReportErrMessage("E10026");
  318. GELOGE(PARAM_INVALID, "[Check][Param] fail for name of input SingleOpDesc is empty.");
  319. return false;
  320. }
  321. int index = 0;
  322. for (auto &tensor_desc : op_desc.input_desc) {
  323. if (!tensor_desc.GetValidFlag()) {
  324. ErrorManager::GetInstance().ATCReportErrMessage("E10027", {"op_name", "input", "type", "index"},
  325. {op_desc.op, "input", "tensor", std::to_string(index)});
  326. GELOGE(PARAM_INVALID,
  327. "[Check][Param] fail for Input's dataType or format is invalid when the index is %d", index);
  328. return false;
  329. }
  330. if ((tensor_desc.type == DT_UNDEFINED && tensor_desc.format != FORMAT_RESERVED) ||
  331. (tensor_desc.type != DT_UNDEFINED && tensor_desc.format == FORMAT_RESERVED)){
  332. ErrorManager::GetInstance().ATCReportErrMessage("E10027", {"op_name", "input", "type", "index"},
  333. {op_desc.op, "input", "datatype or format", std::to_string(index)});
  334. GELOGE(PARAM_INVALID, "[Check][Param]Input's dataType or format is invalid when the index is %d", index);
  335. return false;
  336. }
  337. ++index;
  338. }
  339. index = 0;
  340. for (auto &tensor_desc : op_desc.output_desc) {
  341. if (!tensor_desc.GetValidFlag()) {
  342. ErrorManager::GetInstance().ATCReportErrMessage("E10027", {"op_name", "input", "type", "index"},
  343. {op_desc.op, "output", "tensor", std::to_string(index)});
  344. GELOGE(PARAM_INVALID, "[Check][Param]fail for Output's dataType is invalid when the index is %d", index);
  345. return false;
  346. }
  347. if (tensor_desc.type == DT_UNDEFINED) {
  348. ErrorManager::GetInstance().ATCReportErrMessage("E10027", {"op_name", "input", "type", "index"},
  349. {op_desc.op, "output", "datatype", std::to_string(index)});
  350. GELOGE(PARAM_INVALID, "[Check][Param]Output's dataType is invalid when the index is %d", index);
  351. return false;
  352. }
  353. if (tensor_desc.format == FORMAT_RESERVED) {
  354. ErrorManager::GetInstance().ATCReportErrMessage("E10027", {"op_name", "input", "type", "index"},
  355. {op_desc.op, "output", "format", std::to_string(index)});
  356. GELOGE(PARAM_INVALID, "[Check][Param]Output's format is invalid when the index is %d", index);
  357. return false;
  358. }
  359. ++index;
  360. }
  361. for (auto &attr : op_desc.attrs) {
  362. if (attr.name.empty()) {
  363. ErrorManager::GetInstance().ATCReportErrMessage("E10029", {"op_name"}, {op_desc.op});
  364. GELOGE(PARAM_INVALID, "[Parse][Attr]attr name is empty");
  365. return false;
  366. }
  367. if (attr.value.IsEmpty()) {
  368. ErrorManager::GetInstance().ATCReportErrMessage("E10030", {"op_name", "attrname"}, {op_desc.op, attr.name});
  369. GELOGE(PARAM_INVALID, "[Parse][Attr] fail for vale of attr name:\"%s\" is empty. ", attr.name.c_str());
  370. return false;
  371. }
  372. }
  373. return true;
  374. }
  375. std::unique_ptr<OpDesc> SingleOpParser::CreateOpDesc(const string &op_type) {
  376. return std::unique_ptr<OpDesc>(new(std::nothrow) OpDesc(op_type, op_type));
  377. }
  378. Status SingleOpParser::UpdateDynamicTensorName(std::vector<SingleOpTensorDesc> &desc) {
  379. std::map<std::string, int> dynamic_name_map;
  380. for (auto &tensor : desc) {
  381. if (tensor.dynamic_input_name.empty()) {
  382. continue;
  383. }
  384. if (dynamic_name_map.find(tensor.dynamic_input_name) == dynamic_name_map.end()) {
  385. dynamic_name_map[tensor.dynamic_input_name] = 0;
  386. } else {
  387. dynamic_name_map[tensor.dynamic_input_name]++;
  388. }
  389. tensor.name = tensor.dynamic_input_name + std::to_string(dynamic_name_map[tensor.dynamic_input_name]);
  390. }
  391. GELOGD("Update dynamic tensor name success!");
  392. return SUCCESS;
  393. }
  394. Status SingleOpParser::ConvertToBuildParam(int index,
  395. const SingleOpDesc &single_op_desc,
  396. SingleOpBuildParam &build_param) {
  397. auto op_desc = CreateOpDesc(single_op_desc.op);
  398. GE_CHECK_NOTNULL(op_desc);
  399. for (auto &desc : single_op_desc.input_desc) {
  400. GeTensorDesc ge_tensor_desc(GeShape(desc.dims),
  401. desc.format,
  402. desc.type);
  403. auto ori_format_to_set = desc.ori_format != FORMAT_RESERVED ? desc.ori_format : desc.format;
  404. auto ori_dims = !desc.ori_dims.empty() ? desc.ori_dims : desc.dims;
  405. ge_tensor_desc.SetOriginFormat(ori_format_to_set);
  406. ge_tensor_desc.SetOriginShape(GeShape(ori_dims));
  407. GE_CHK_STATUS_RET_NOLOG(SetShapeRange(op_desc->GetName(), desc, ge_tensor_desc));
  408. TensorUtils::SetRealDimCnt(ge_tensor_desc, ori_dims.size());
  409. TensorUtils::SetInputTensor(ge_tensor_desc, true);
  410. TensorUtils::SetOutputTensor(ge_tensor_desc, false);
  411. if (desc.name.empty()) {
  412. op_desc->AddInputDesc(ge_tensor_desc);
  413. } else {
  414. op_desc->AddInputDesc(desc.name, ge_tensor_desc);
  415. }
  416. build_param.inputs.emplace_back(ge_tensor_desc);
  417. }
  418. for (auto &desc : single_op_desc.output_desc) {
  419. GeTensorDesc ge_tensor_desc(GeShape(desc.dims),
  420. desc.format,
  421. desc.type);
  422. auto ori_format_to_set = desc.ori_format != FORMAT_RESERVED ? desc.ori_format : desc.format;
  423. auto ori_dims = !desc.ori_dims.empty() ? desc.ori_dims : desc.dims;
  424. ge_tensor_desc.SetOriginFormat(ori_format_to_set);
  425. ge_tensor_desc.SetOriginShape(GeShape(ori_dims));
  426. GE_CHK_STATUS_RET_NOLOG(SetShapeRange(op_desc->GetName(), desc, ge_tensor_desc));
  427. TensorUtils::SetRealDimCnt(ge_tensor_desc, ori_dims.size());
  428. TensorUtils::SetInputTensor(ge_tensor_desc, false);
  429. TensorUtils::SetOutputTensor(ge_tensor_desc, true);
  430. if (desc.name.empty()) {
  431. op_desc->AddOutputDesc(ge_tensor_desc);
  432. } else {
  433. op_desc->AddOutputDesc(desc.name, ge_tensor_desc);
  434. }
  435. build_param.outputs.emplace_back(ge_tensor_desc);
  436. }
  437. for (const auto &attr : single_op_desc.attrs) {
  438. op_desc->SetAttr(attr.name, attr.value);
  439. }
  440. if (VerifyOpInputOutputSizeByIr(*op_desc) != SUCCESS) {
  441. GELOGE(PARAM_INVALID, "[Verify][OpInputOutputSize] fail for input op [%s] invalid.", op_desc->GetType().c_str());
  442. return PARAM_INVALID;
  443. }
  444. build_param.file_name = GenerateFileName(single_op_desc, index);
  445. build_param.op_desc.reset(op_desc.release());
  446. return SUCCESS;
  447. }
  448. Status SingleOpParser::VerifyOpInputOutputSizeByIr(const OpDesc &current_op_desc) {
  449. ge::Operator operator_ir = ge::OperatorFactory::CreateOperator("tmp_operator", current_op_desc.GetType());
  450. if (!operator_ir.IsEmpty()) {
  451. auto opdesc_ir = ge::OpDescUtils::GetOpDescFromOperator(operator_ir);
  452. GE_CHECK_NOTNULL(opdesc_ir);
  453. size_t current_opdesc_inputs_num = current_op_desc.GetInputsSize();
  454. size_t ir_opdesc_inputs_num = opdesc_ir->GetInputsSize();
  455. if (current_opdesc_inputs_num < ir_opdesc_inputs_num) {
  456. string reason = "is smaller than the ir needed input size " + std::to_string(ir_opdesc_inputs_num);
  457. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  458. {current_op_desc.GetName(), "input size " + std::to_string(current_opdesc_inputs_num), reason});
  459. GELOGE(PARAM_INVALID,
  460. "[Verify][OpInputOutputSize]This op:%s input size %zu is smaller than the ir needed input size %zu",
  461. current_op_desc.GetName().c_str(), current_opdesc_inputs_num, ir_opdesc_inputs_num);
  462. return PARAM_INVALID;
  463. }
  464. size_t current_opdesc_outputs_num = current_op_desc.GetOutputsSize();
  465. size_t ir_opdesc_outputs_num = opdesc_ir->GetOutputsSize();
  466. if (current_opdesc_outputs_num < ir_opdesc_outputs_num) {
  467. string reason = "is smaller than the ir needed output size " + std::to_string(ir_opdesc_outputs_num);
  468. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  469. {current_op_desc.GetName(), "output size " + std::to_string(current_opdesc_outputs_num), reason});
  470. GELOGE(PARAM_INVALID,
  471. "[Verify][OpInputOutputSize]This op:%s output size %zu is smaller than the ir needed output size %zu",
  472. current_op_desc.GetName().c_str(), current_opdesc_outputs_num, ir_opdesc_outputs_num);
  473. return PARAM_INVALID;
  474. }
  475. }
  476. return SUCCESS;
  477. }
  478. Status SingleOpParser::SetShapeRange(const std::string &op_name,
  479. const SingleOpTensorDesc &tensor_desc,
  480. GeTensorDesc &ge_tensor_desc) {
  481. auto num_shape_ranges = tensor_desc.dim_ranges.size();
  482. GELOGD("Number of shape ranges = %zu", num_shape_ranges);
  483. auto it = std::find(tensor_desc.dims.begin(), tensor_desc.dims.end(), ge::UNKNOWN_DIM_NUM);
  484. if (it != tensor_desc.dims.end()) {
  485. if (tensor_desc.dims != ge::UNKNOWN_RANK) {
  486. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  487. {op_name,
  488. "shape",
  489. "has unknown rank but dim size is not one"});
  490. GELOGE(PARAM_INVALID, "[Set][ShapeRange]Invalid tensor shape:%s.",
  491. ge_tensor_desc.MutableShape().ToString().c_str());
  492. return PARAM_INVALID;
  493. }
  494. if (!tensor_desc.dim_ranges.empty()) {
  495. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  496. {op_name,
  497. "shape range",
  498. "is not needed while the rank the shape is unknown"});
  499. GELOGE(PARAM_INVALID, "[Set][ShapeRange]Shape range is not needed while the rank the shape is unknown.");
  500. return PARAM_INVALID;
  501. }
  502. GELOGD("Shape is unknown rank, do not set shape range");
  503. return SUCCESS;
  504. }
  505. std::vector<std::pair<int64_t, int64_t>> shape_range;
  506. size_t range_index = 0;
  507. for (auto dim : tensor_desc.dims) {
  508. if (dim >= 0) {
  509. shape_range.emplace_back(dim, dim);
  510. GELOGD("Adding shape range: [%ld, %ld]", dim, dim);
  511. } else {
  512. GELOGD("To get shape range by index = %zu", range_index);
  513. if (range_index >= num_shape_ranges) {
  514. string reason = "is smaller than the unknown dim size " + std::to_string(++range_index);
  515. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  516. {op_name,
  517. "shape range size " + std::to_string(num_shape_ranges),
  518. reason});
  519. GELOGE(PARAM_INVALID, "[Set][ShapeRange]The number of shape_range mismatches that of unknown dims.");
  520. return PARAM_INVALID;
  521. }
  522. auto &range = tensor_desc.dim_ranges[range_index];
  523. if (range.size() != kShapeRangePairSize) {
  524. string reason = "has " + std::to_string(range.size()) + " item(s)";
  525. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  526. {op_name,
  527. "shape range " + std::to_string(range_index),
  528. reason});
  529. GELOGE(PARAM_INVALID, "[Set][ShapeRange]Invalid shape range entry. index = %zu, size = %zu",
  530. range_index, range.size());
  531. return PARAM_INVALID;
  532. }
  533. shape_range.emplace_back(range[kShapeRangeLow], range[kShapeRangeHigh]);
  534. GELOGD("Adding shape range: [%ld, %ld]", range[kShapeRangeLow], range[kShapeRangeHigh]);
  535. ++range_index;
  536. }
  537. }
  538. if (num_shape_ranges != range_index) {
  539. string reason = "is greater than the unknown dim size " + std::to_string(range_index);
  540. ErrorManager::GetInstance().ATCReportErrMessage("E19014", {"opname", "value", "reason"},
  541. {op_name,
  542. "shape range size " + std::to_string(num_shape_ranges),
  543. reason});
  544. GELOGE(PARAM_INVALID,
  545. "[Set][ShapeRange]The number of shape_range(%zu) mismatches that of unknown dims(%zu).",
  546. num_shape_ranges, range_index);
  547. return PARAM_INVALID;
  548. }
  549. if (range_index > 0) {
  550. ge_tensor_desc.SetShapeRange(shape_range);
  551. }
  552. return SUCCESS;
  553. }
  554. Status SingleOpParser::ParseSingleOpList(const std::string &file, std::vector<SingleOpBuildParam> &op_list) {
  555. int index = 0;
  556. try {
  557. Json single_op_list_json;
  558. auto ret = ReadJsonFile(file, single_op_list_json);
  559. if (ret != SUCCESS) {
  560. return ret;
  561. }
  562. int32_t compile_flag = 0;
  563. for (const Json &single_op_json : single_op_list_json) {
  564. SingleOpDesc single_op_desc;
  565. GELOGI("Parsing op[%d], jsonStr = %s", index, single_op_json.dump(kDumpJsonIndent).c_str());
  566. single_op_desc = single_op_json;
  567. GELOGD("Compile flag is %d.", single_op_desc.compile_flag);
  568. if (single_op_desc.compile_flag == 1) {
  569. compile_flag = single_op_desc.compile_flag;
  570. continue;
  571. }
  572. if (UpdateDynamicTensorName(single_op_desc.input_desc) != SUCCESS) {
  573. GELOGE(FAILED, "[Update][DynamicTensorName] failed for invalid input param!");
  574. REPORT_CALL_ERROR("E19999", "UpdateDynamicTensorName failed for invalid input param.");
  575. return FAILED;
  576. }
  577. if (!Validate(single_op_desc)) {
  578. GELOGE(PARAM_INVALID,
  579. "[Check][OpDesc]Validate the index[%d] of op failed when read json file[%s].", index, file.c_str());
  580. return PARAM_INVALID;
  581. }
  582. SingleOpBuildParam param;
  583. ret = ConvertToBuildParam(index, single_op_desc, param);
  584. if (ret != SUCCESS) {
  585. return ret;
  586. }
  587. param.compile_flag = compile_flag;
  588. op_list.emplace_back(param);
  589. GELOGI("Parse the index[%d] of op success", index);
  590. index += 1;
  591. }
  592. } catch (const nlohmann::json::exception &e) {
  593. REPORT_INNER_ERROR("E19999", "parse singleop file:%s failed, catch exception:%s, current index:%d",
  594. file.c_str(), e.what(), index);
  595. GELOGE(PARAM_INVALID, "[Parse][OpList] the index:%d of op failed when read json file:%s, exception:%s",
  596. index, file.c_str(), e.what());
  597. return PARAM_INVALID;
  598. }
  599. return SUCCESS;
  600. }
  601. } // namespace ge

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