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.

acl_graph_parser_util.cc 42 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
3 years ago
3 years ago
3 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
3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13. #include "parser/common/acl_graph_parser_util.h"
  14. #include <dlfcn.h>
  15. #include <regex.h>
  16. #include <cstdlib>
  17. #include <ctime>
  18. #include <fstream>
  19. #include "common/string_util.h"
  20. #include "common/util.h"
  21. #include "common/util/error_manager/error_manager.h"
  22. #include "external/ge/ge_api_types.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "framework/omg/parser/parser_types.h"
  25. #include "ge/ge_api_types.h"
  26. #include "google/protobuf/io/coded_stream.h"
  27. #include "google/protobuf/io/zero_copy_stream_impl.h"
  28. #include "graph/debug/ge_attr_define.h"
  29. #include "graph/opsproto_manager.h"
  30. #include "graph/utils/type_utils.h"
  31. #include "omg/parser/parser_inner_ctx.h"
  32. #include "parser/common/register_tbe.h"
  33. #include "tbe_plugin_loader.h"
  34. #include "mmpa/mmpa_api.h"
  35. using google::protobuf::io::CodedInputStream;
  36. using google::protobuf::io::FileInputStream;
  37. using google::protobuf::io::ZeroCopyInputStream;
  38. using namespace ge::parser;
  39. namespace {
  40. const std::string kGraphDefaultName = "domi_default";
  41. /// The maximum length of the file.
  42. /// Based on the security coding specification and the current actual (protobuf) model size, it is determined as 2G-1
  43. const int kMaxFileSizeLimit = INT_MAX;
  44. const int kMaxBuffSize = 256;
  45. const int kProtoReadBytesLimit = INT_MAX; // Max size of 2 GB minus 1 byte.
  46. const int kWarningThreshold = 536870912 * 2; // 536870912 represent 512M
  47. const uint32_t kSetOutputWithNodeAndIndex = 0x1;
  48. const uint32_t kSetOutputWithTensorName = 0x2;
  49. const uint32_t kSetOutputModeMixed = 0x3;
  50. const std::set<domi::FrameworkType> kSupportTensorAsOutput = {
  51. domi::CAFFE,
  52. domi::ONNX
  53. };
  54. static string GetSoPath() {
  55. Dl_info dl_info;
  56. if (dladdr(reinterpret_cast<void *>(&GetSoPath), &dl_info) == 0) {
  57. GELOGW("Failed to read so_path!");
  58. return string();
  59. } else {
  60. std::string so_path = dl_info.dli_fname;
  61. char path[PATH_MAX] = {0};
  62. if (so_path.length() >= PATH_MAX) {
  63. GELOGW("File path is too long!");
  64. return string();
  65. }
  66. if (realpath(so_path.c_str(), path) == nullptr) {
  67. GELOGW("Failed to get realpath of %s", so_path.c_str());
  68. return string();
  69. }
  70. so_path = path;
  71. so_path = so_path.substr(0, so_path.rfind('/') + 1);
  72. return so_path;
  73. }
  74. }
  75. static void GetOpsProtoPath(string &opsproto_path) {
  76. GELOGD("Start to get ops proto path schedule.");
  77. const char *path_env = std::getenv("ASCEND_OPP_PATH");
  78. if (path_env != nullptr) {
  79. string path = path_env;
  80. string file_path = ge::parser::RealPath(path.c_str());
  81. if (file_path.empty()) {
  82. REPORT_INNER_ERROR("E19999", "File path %s is invalid.", path.c_str());
  83. GELOGE(ge::FAILED, "[Get][Path] File path %s is invalid.", path.c_str());
  84. return;
  85. }
  86. opsproto_path = (path + "/op_proto/custom/" + ":") + (path + "/op_proto/built-in/");
  87. GELOGI("Get opsproto so path from env : %s", path.c_str());
  88. return;
  89. }
  90. string path_base = GetSoPath();
  91. GELOGI("path_base is %s", path_base.c_str());
  92. path_base = path_base.substr(0, path_base.rfind('/'));
  93. path_base = path_base.substr(0, path_base.rfind('/') + 1);
  94. opsproto_path = (path_base + "ops/op_proto/custom/" + ":") + (path_base + "ops/op_proto/built-in/");
  95. }
  96. static void GetAclParams(const std::map<ge::AscendString, ge::AscendString> &parser_params, const string &key,
  97. string &value) {
  98. for (auto &ele : parser_params) {
  99. const char *key_ascend = ele.first.GetString();
  100. if (key_ascend == nullptr) {
  101. GELOGW("Input options key is null, Please check!");
  102. continue;
  103. }
  104. string key_str = key_ascend;
  105. if (key == key_str) {
  106. const char *value_ascend = ele.second.GetString();
  107. if (value_ascend == nullptr) {
  108. value = "";
  109. } else {
  110. value = value_ascend;
  111. }
  112. return;
  113. }
  114. }
  115. value = "";
  116. return;
  117. }
  118. static bool CheckDigitStr(std::string &str) {
  119. for (char c : str) {
  120. if (!isdigit(c)) {
  121. REPORT_CALL_ERROR("E19999", "param str:%s is not positive integer", str.c_str());
  122. GELOGE(domi::FAILED, "[Check][Param] Value[%s] is not positive integer", str.c_str());
  123. return false;
  124. }
  125. }
  126. return true;
  127. }
  128. } // namespace
  129. namespace ge {
  130. static bool CheckInputTrueOrFalse(const std::string &s, const std::string &atc_param) {
  131. if ((s == "true") || (s == "false")) {
  132. return true;
  133. } else {
  134. ErrorManager::GetInstance().ATCReportErrMessage("E10005", {"parameter", "value"}, {atc_param, s});
  135. GELOGE(PARAM_INVALID, "[Check][Param] Input parameter[%s]'s value[%s] must be true or false.",
  136. atc_param.c_str(), s.c_str());
  137. return false;
  138. }
  139. }
  140. static Status CheckOutNode(ge::OpDescPtr op_desc, int32_t index) {
  141. int32_t out_size = op_desc->GetOutputsSize();
  142. if (index < 0 || index >= out_size) {
  143. GELOGE(domi::FAILED, "[Check][Param]out_node [%s] output index:%d must be smaller "
  144. "than node output size:%d and can not be negative!", op_desc->GetName().c_str(), index, out_size);
  145. std::string fail_reason = "output index:" + to_string(index) +
  146. " must be smaller than output size:" + to_string(out_size) + " and can not be negative!";
  147. ErrorManager::GetInstance().ATCReportErrMessage("E10003", {"parameter", "value", "reason"},
  148. {"out_nodes", op_desc->GetName(), fail_reason});
  149. return domi::FAILED;
  150. }
  151. return domi::SUCCESS;
  152. }
  153. domi::Status AclGrphParseUtil::LoadOpsProtoLib() {
  154. string opsproto_path;
  155. GetOpsProtoPath(opsproto_path);
  156. GELOGI("Get opsproto path is %s", opsproto_path.c_str());
  157. OpsProtoManager *manager = OpsProtoManager::Instance();
  158. map<string, string> option_tmp;
  159. option_tmp.emplace(std::pair<string, string>(string("ge.opsProtoLibPath"), opsproto_path));
  160. bool is_proto_init = manager->Initialize(option_tmp);
  161. if (!is_proto_init) {
  162. REPORT_INNER_ERROR("E19999", "OpsProtoManager init failed because ops proto path:%s is invalid.",
  163. opsproto_path.c_str());
  164. GELOGE(FAILED, "[Invoke][Initialize] Load ops_proto lib failed, ops proto path:%s is invalid.",
  165. opsproto_path.c_str());
  166. return FAILED;
  167. }
  168. return SUCCESS;
  169. }
  170. void AclGrphParseUtil::SaveCustomCaffeProtoPath() {
  171. GELOGD("Enter save custom caffe proto path.");
  172. std::string path_base = GetSoPath();
  173. path_base = path_base.substr(0, path_base.rfind('/'));
  174. path_base = path_base.substr(0, path_base.rfind('/') + 1);
  175. ge::GetParserContext().caffe_proto_path = path_base + "include/proto/";
  176. string custom_op_path;
  177. const char *path_env = std::getenv("ASCEND_OPP_PATH");
  178. if (path_env != nullptr) {
  179. std::string path = path_env;
  180. custom_op_path = path + "/framework/custom/caffe/";
  181. GELOGI("Get custom proto path from env : %s", path_env);
  182. GetParserContext().custom_proto_path = custom_op_path;
  183. return;
  184. }
  185. custom_op_path = path_base + "ops/framework/custom/caffe/";
  186. ge::GetParserContext().custom_proto_path = custom_op_path;
  187. return;
  188. }
  189. // Initialize PARSER, load custom op plugin
  190. // options will be used later for parser decoupling
  191. domi::Status AclGrphParseUtil::AclParserInitialize(const std::map<std::string, std::string> &options) {
  192. GELOGT(TRACE_INIT, "AclParserInitialize start");
  193. // check init status
  194. if (parser_initialized) {
  195. GELOGW("AclParserInitialize is called more than once");
  196. return SUCCESS;
  197. }
  198. // load custom op plugin
  199. TBEPluginLoader::Instance().LoadPluginSo(options);
  200. // load and save custom op proto for prediction
  201. (void)LoadOpsProtoLib();
  202. SaveCustomCaffeProtoPath();
  203. auto op_registry = domi::OpRegistry::Instance();
  204. if (op_registry == nullptr) {
  205. REPORT_CALL_ERROR("E19999", "Call OpRegistry::Instance failed, ret nullptr.");
  206. GELOGE(FAILED, "[Get][OpRegistry] instance failed");
  207. return FAILED;
  208. }
  209. auto it = options.find(ge::FRAMEWORK_TYPE);
  210. if (it == options.end()) {
  211. REPORT_INNER_ERROR("E19999", "Can not find ge.frameworkType in param options");
  212. GELOGE(FAILED, "[Check][Param]Can not find ge.frameworkType in options");
  213. return FAILED;
  214. }
  215. std::string fmk_type = it->second;
  216. std::vector<OpRegistrationData> registrationDatas = op_registry->registrationDatas;
  217. GELOGI("The size of registrationDatas in parser is: %zu", registrationDatas.size());
  218. for (OpRegistrationData &reg_data : registrationDatas) {
  219. if (std::to_string(reg_data.GetFrameworkType()) == fmk_type) {
  220. (void)OpRegistrationTbe::Instance()->Finalize(reg_data, false);
  221. (void)domi::OpRegistry::Instance()->Register(reg_data);
  222. }
  223. }
  224. // set init status
  225. if (!parser_initialized) {
  226. // Initialize success, first time calling initialize
  227. parser_initialized = true;
  228. }
  229. GELOGT(TRACE_STOP, "AclParserInitialize finished");
  230. return SUCCESS;
  231. }
  232. void AclGrphParseUtil::SetDefaultFormat() {
  233. if (ge::GetParserContext().type == domi::TENSORFLOW) {
  234. ge::GetParserContext().format = domi::DOMI_TENSOR_NHWC;
  235. } else {
  236. ge::GetParserContext().format = domi::DOMI_TENSOR_NCHW;
  237. }
  238. }
  239. domi::Status AclGrphParseUtil::ParseAclOutputNodes(const string &out_nodes) {
  240. try {
  241. ge::GetParserContext().out_nodes_map.clear();
  242. ge::GetParserContext().user_out_nodes.clear();
  243. ge::GetParserContext().user_out_tensors.clear();
  244. ge::GetParserContext().default_out_nodes.clear();
  245. // parse output node
  246. if (!out_nodes.empty()) {
  247. uint32_t set_output_mode = 0;
  248. vector<string> nodes_v = StringUtils::Split(out_nodes, ';');
  249. for (const string &node : nodes_v) {
  250. vector<string> key_value_v = StringUtils::Split(node, ':');
  251. if (key_value_v.size() != 2) { // The size must be 2.
  252. if (key_value_v.size() == 1 && kSupportTensorAsOutput.count(ge::GetParserContext().type) > 0) {
  253. set_output_mode |= kSetOutputWithTensorName;
  254. if (set_output_mode == kSetOutputModeMixed) {
  255. break;
  256. }
  257. ge::GetParserContext().user_out_tensors.push_back(node);
  258. continue;
  259. }
  260. ErrorManager::GetInstance().ATCReportErrMessage(
  261. "E10001", {"parameter", "value", "reason"},
  262. {"out_nodes", node, "the correct format is \"node_name1:0; node_name1:1; node_name2:0\""});
  263. GELOGE(PARAM_INVALID, "[Check][Param] The input format of out_nodes is invalid, the correct format is "
  264. "\"node_name1:0; node_name1:1; node_name2:0\", while the actual input is %s.",
  265. node.c_str());
  266. return PARAM_INVALID;
  267. }
  268. set_output_mode |= kSetOutputWithNodeAndIndex;
  269. if (set_output_mode == kSetOutputModeMixed) {
  270. break;
  271. }
  272. // stoi: The method may throw an exception: invalid_argument/out_of_range
  273. if (!CheckDigitStr(key_value_v[1])) {
  274. ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
  275. {"out_nodes", out_nodes, "is not positive integer"});
  276. GELOGE(PARAM_INVALID, "[Check][Param] This str:%s must be digit string, while the actual input is %s",
  277. key_value_v[1].c_str(), out_nodes.c_str());
  278. return PARAM_INVALID;
  279. }
  280. auto iter = ge::GetParserContext().out_nodes_map.find(key_value_v[0]);
  281. int32_t index = stoi(StringUtils::Trim(key_value_v[1]));
  282. GELOGD("Get output info: node[%s] and index[%d]", key_value_v[0].c_str(), index);
  283. if (iter != ge::GetParserContext().out_nodes_map.end()) {
  284. iter->second.emplace_back(index);
  285. } else {
  286. std::vector<int32_t> index_v;
  287. index_v.emplace_back(index);
  288. ge::GetParserContext().out_nodes_map.emplace(key_value_v[0], index_v);
  289. }
  290. ge::GetParserContext().user_out_nodes.emplace_back(key_value_v[0], index);
  291. }
  292. if (set_output_mode == kSetOutputModeMixed) {
  293. ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
  294. {"--out_nodes", out_nodes, "is not all index or top_name"});
  295. GELOGE(PARAM_INVALID, "[Parse][Param]This out_nodes str must be all index or tensor_name, "
  296. "while the actual input is %s", out_nodes.c_str());
  297. return PARAM_INVALID;
  298. }
  299. }
  300. } catch (std::invalid_argument &) {
  301. GELOGE(PARAM_INVALID, "[Check][Param] Invalid of out_nodes: %s ", out_nodes.c_str());
  302. ErrorManager::GetInstance().ATCReportErrMessage("E10014", {"parameter", "value"}, {"out_nodes", out_nodes});
  303. return PARAM_INVALID;
  304. } catch (std::out_of_range &) {
  305. GELOGE(PARAM_INVALID, "[Check][Param] Invalid of out_nodes: %s ", out_nodes.c_str());
  306. ErrorManager::GetInstance().ATCReportErrMessage("E10013", {"parameter", "value"}, {"out_nodes", out_nodes});
  307. return PARAM_INVALID;
  308. }
  309. return SUCCESS;
  310. }
  311. domi::Status AclGrphParseUtil::ParseAclOutputFp16NodesFormat(const string &is_output_fp16) {
  312. if (is_output_fp16.empty()) {
  313. return SUCCESS;
  314. }
  315. vector<domiTensorFormat_t> &output_formats = ge::GetParserContext().output_formats;
  316. output_formats.clear();
  317. vector<string> node_format_vec = StringUtils::Split(is_output_fp16, ',');
  318. for (auto &is_fp16 : node_format_vec) {
  319. StringUtils::Trim(is_fp16);
  320. if (!CheckInputTrueOrFalse(is_fp16, "is_output_adjust_hw_layout")) {
  321. GELOGE(PARAM_INVALID, "[Check][Param]Invalid Param, is_output_adjust_hw_layout "
  322. "only support true/false: but is [%s]", is_output_fp16.c_str());
  323. return PARAM_INVALID;
  324. }
  325. if (is_fp16 == "false") {
  326. output_formats.push_back(DOMI_TENSOR_ND);
  327. } else if (is_fp16 == "true") {
  328. output_formats.push_back(domi::DOMI_TENSOR_NC1HWC0);
  329. }
  330. }
  331. return SUCCESS;
  332. }
  333. domi::Status AclGrphParseUtil::ParseAclEnableScope(const string &enable_scope_fusion_passes) {
  334. ge::GetParserContext().enable_scope_fusion_passes.clear();
  335. if (enable_scope_fusion_passes.empty()) {
  336. return SUCCESS;
  337. }
  338. ge::GetParserContext().enable_scope_fusion_passes = enable_scope_fusion_passes;
  339. return SUCCESS;
  340. }
  341. void AclGrphParseUtil::AddAttrsForInputNodes(const vector<string> &adjust_fp16_format_vec,
  342. const string &fp16_nodes_name, uint32_t index, OpDescPtr &op_desc) {
  343. if (AttrUtils::SetStr(op_desc, ATTR_ATC_USER_DEFINE_DATATYPE, TypeUtils::DataTypeToSerialString(DT_FLOAT16))) {
  344. if ((index < adjust_fp16_format_vec.size()) && (adjust_fp16_format_vec[index] == "true")) {
  345. GELOGI("This node [%s] should be set NC1HWC0", fp16_nodes_name.c_str());
  346. if (!AttrUtils::SetStr(op_desc, ATTR_ATC_USER_DEFINE_FORMAT, TypeUtils::FormatToSerialString(FORMAT_NC1HWC0))) {
  347. GELOGW("This node [%s] set NC1HWC0 failed", fp16_nodes_name.c_str());
  348. }
  349. }
  350. }
  351. }
  352. domi::Status AclGrphParseUtil::ParseAclInputFp16Nodes(const ComputeGraphPtr &graph, const string &input_fp16_nodes,
  353. const string &is_input_adjust_hw_layout) {
  354. GE_CHECK_NOTNULL(graph);
  355. vector<string> adjust_fp16_format_vec;
  356. if (!is_input_adjust_hw_layout.empty()) {
  357. adjust_fp16_format_vec = StringUtils::Split(is_input_adjust_hw_layout, ',');
  358. for (auto &s : adjust_fp16_format_vec) {
  359. StringUtils::Trim(s);
  360. if (!CheckInputTrueOrFalse(s, "is_input_adjust_hw_layout")) {
  361. GELOGE(PARAM_INVALID, "[Check][Param] Invalid Param, is_input_adjust_hw_layout "
  362. "only support true/false: but is [%s]", is_input_adjust_hw_layout.c_str());
  363. return PARAM_INVALID;
  364. }
  365. }
  366. }
  367. if (input_fp16_nodes.empty()) {
  368. return SUCCESS;
  369. }
  370. GELOGI("The input_fp16_nodes is set %s", input_fp16_nodes.c_str());
  371. vector<string> input_fp16_nodes_vec = StringUtils::Split(input_fp16_nodes, ';');
  372. for (uint32_t i = 0; i < input_fp16_nodes_vec.size(); ++i) {
  373. ge::NodePtr node = graph->FindNode(input_fp16_nodes_vec[i]);
  374. if (node == nullptr) {
  375. ErrorManager::GetInstance().ATCReportErrMessage("E10016", {"parameter", "opname"},
  376. {"input_fp16_nodes", input_fp16_nodes_vec[i]});
  377. GELOGE(PARAM_INVALID, "[Check][Param] Input parameter[input_fp16_nodes]'s opname[%s] is not exist in model",
  378. input_fp16_nodes_vec[i].c_str());
  379. return PARAM_INVALID;
  380. }
  381. auto op_desc = node->GetOpDesc();
  382. GE_CHECK_NOTNULL(op_desc);
  383. if (op_desc->GetType() != ge::parser::DATA) {
  384. ErrorManager::GetInstance().ATCReportErrMessage("E10017", {"parameter", "opname"},
  385. {"input_fp16_nodes", input_fp16_nodes_vec[i]});
  386. GELOGE(PARAM_INVALID, "[Check][Param] Input parameter[input_fp16_nodes]'s opname[%s] is not a input opname",
  387. input_fp16_nodes_vec[i].c_str());
  388. return PARAM_INVALID;
  389. }
  390. AddAttrsForInputNodes(adjust_fp16_format_vec, input_fp16_nodes_vec[i], i, op_desc);
  391. }
  392. return SUCCESS;
  393. }
  394. void AclGrphParseUtil::CreateOutputNodesInfo(std::vector<std::pair<ge::NodePtr, int32_t>> &output_nodes_info,
  395. std::vector<std::string> &output_nodes_name) {
  396. output_nodes_name.clear();
  397. auto &out_tensor_names = ge::GetParserContext().out_tensor_names;
  398. if (out_tensor_names.empty()) {
  399. // tf process, no top name.
  400. for (const auto output_node_info : output_nodes_info) {
  401. std::string node_name = output_node_info.first->GetName();
  402. int32_t index = output_node_info.second;
  403. output_nodes_name.push_back(node_name + ":" + std::to_string(index));
  404. }
  405. return;
  406. }
  407. // Need add top name after node_name:index
  408. for (size_t i = 0; i < output_nodes_info.size(); ++i) {
  409. auto node = output_nodes_info[i].first;
  410. int32_t index = output_nodes_info[i].second;
  411. std::string node_name = node->GetName();
  412. if (i < out_tensor_names.size()) {
  413. auto output_desc = node->GetOpDesc()->MutableOutputDesc(static_cast<uint32_t>(index));
  414. (void)AttrUtils::SetStr(output_desc, ATTR_NAME_ORIGIN_OUTPUT_TENSOR_NAME, out_tensor_names[i]);
  415. std::string output_name = node->GetName() + ":" + std::to_string(index) + ":" + out_tensor_names[i];
  416. output_nodes_name.push_back(output_name);
  417. GELOGD("Output[%zu] name[%s]", i, output_name.c_str());
  418. } else {
  419. GELOGW("Get top name of node [%s] fail.", node_name.c_str());
  420. output_nodes_name.push_back(node_name + ":" + std::to_string(index));
  421. }
  422. }
  423. }
  424. domi::Status AclGrphParseUtil::GetOutputLeaf(NodePtr node,
  425. std::vector<std::pair<ge::NodePtr, int32_t>> &output_nodes_info) {
  426. ge::OpDescPtr tmpDescPtr = node->GetOpDesc();
  427. if (tmpDescPtr == nullptr) {
  428. REPORT_INNER_ERROR("E19999", "param node has no opdesc.");
  429. GELOGE(domi::FAILED, "[Get][OpDesc] param node has no opdesc.");
  430. return domi::FAILED;
  431. }
  432. size_t size = tmpDescPtr->GetOutputsSize();
  433. if (node->GetType() != ge::parser::NETOUTPUT) {
  434. for (size_t index = 0; index < size; ++index) {
  435. output_nodes_info.push_back(std::make_pair(node, index));
  436. GELOGD("Get output leaf node:%s.", node->GetName().c_str());
  437. }
  438. } else {
  439. const auto in_anchors = node->GetAllInDataAnchors();
  440. for (auto in_anchor : in_anchors) {
  441. auto out_anchor = in_anchor->GetPeerOutAnchor();
  442. if (out_anchor == nullptr) {
  443. REPORT_INNER_ERROR("E19999", "Get leaf node op desc fail.");
  444. GELOGE(domi::FAILED, "[Invoke][GetPeerOutAnchor] Get leaf node op desc fail.");
  445. return domi::FAILED;
  446. }
  447. auto out_node = out_anchor->GetOwnerNode();
  448. output_nodes_info.push_back(std::make_pair(out_node, out_anchor->GetIdx()));
  449. }
  450. }
  451. return SUCCESS;
  452. }
  453. domi::Status AclGrphParseUtil::GetDefaultOutInfo(ge::ComputeGraphPtr &compute_graph,
  454. std::vector<std::pair<ge::NodePtr, int32_t>> &output_nodes_info) {
  455. std::vector<std::pair<std::string, int32_t>> default_out_nodes = ge::GetParserContext().default_out_nodes;
  456. if (!default_out_nodes.empty()) {
  457. for (uint32_t i = 0; i < default_out_nodes.size(); ++i) {
  458. ge::NodePtr out_node = compute_graph->FindNode(default_out_nodes[i].first);
  459. if (out_node == nullptr) {
  460. ErrorManager::GetInstance().ATCReportErrMessage("E10016", {"parameter", "opname"},
  461. {"out_nodes", default_out_nodes[i].first});
  462. GELOGE(domi::FAILED, "[Check][Param] Can not find out_nodes(%d) (%s) in graph.",
  463. i, default_out_nodes[i].first.c_str());
  464. return domi::FAILED;
  465. }
  466. output_nodes_info.push_back(std::make_pair(out_node, default_out_nodes[i].second));
  467. GELOGD("Get default output node:%s.", out_node->GetName().c_str());
  468. }
  469. return domi::SUCCESS;
  470. }
  471. for (ge::NodePtr node : compute_graph->GetDirectNode()) {
  472. if (!node->GetInAllNodes().empty() && node->GetOutAllNodes().empty()) {
  473. Status ret = GetOutputLeaf(node, output_nodes_info);
  474. GE_CHK_BOOL_RET_STATUS(ret == SUCCESS, ret, "[Invoke][GetOutputLeaf] Find leaf fail.");
  475. }
  476. }
  477. return domi::SUCCESS;
  478. }
  479. domi::Status AclGrphParseUtil::SetOutputNodeInfo(ge::Graph &graph,
  480. const std::map<AscendString, AscendString> &parser_params) {
  481. (void)parser_params;
  482. ge::ComputeGraphPtr compute_graph = ge::GraphUtils::GetComputeGraph(graph);
  483. GE_CHECK_NOTNULL(compute_graph);
  484. std::vector<std::pair<std::string, int32_t>> user_out_nodes = ge::GetParserContext().user_out_nodes;
  485. std::vector<domiTensorFormat_t> output_formats = ge::GetParserContext().output_formats;
  486. std::vector<std::pair<ge::NodePtr, int32_t>> output_nodes_info;
  487. std::vector<std::string> output_nodes_name;
  488. // User declared outputs
  489. for (uint32_t i = 0; i < user_out_nodes.size(); ++i) {
  490. ge::NodePtr out_node = compute_graph->FindNode(user_out_nodes[i].first);
  491. if (out_node == nullptr) {
  492. ErrorManager::GetInstance().ATCReportErrMessage("E10016", {"parameter", "opname"},
  493. {"out_nodes", user_out_nodes[i].first});
  494. GELOGE(domi::FAILED, "[Check][Param] Can not find out_nodes(%d) (%s) in graph.",
  495. i, user_out_nodes[i].first.c_str());
  496. return domi::FAILED;
  497. }
  498. auto op_desc = out_node->GetOpDesc();
  499. GE_CHECK_NOTNULL(op_desc);
  500. if (CheckOutNode(op_desc, user_out_nodes[i].second) != SUCCESS) {
  501. GELOGE(domi::FAILED, "[CheckOut][Node] (%s) fail.", user_out_nodes[i].first.c_str());
  502. return domi::FAILED;
  503. }
  504. // add user_define_output_nodes attr.
  505. (void)ge::AttrUtils::SetStr(op_desc, ATTR_ATC_USER_DEFINE_OUTPUT_NODES, "true");
  506. if (i < output_formats.size()) {
  507. if (output_formats[i] == domi::DOMI_TENSOR_NC1HWC0) {
  508. GELOGI("The output node [%s] should be set NC1HWC0", user_out_nodes[i].first.c_str());
  509. vector<string> output_fp16_5hd_vec;
  510. (void)ge::AttrUtils::GetListStr(op_desc, "_user_defined_output_fp16_5hd", output_fp16_5hd_vec);
  511. output_fp16_5hd_vec.push_back(std::to_string(user_out_nodes[i].second) + ":" + "NC1HWC0");
  512. (void)ge::AttrUtils::SetListStr(op_desc, "_user_defined_output_fp16_5hd", output_fp16_5hd_vec);
  513. }
  514. }
  515. output_nodes_info.push_back(std::make_pair(out_node, user_out_nodes[i].second));
  516. }
  517. // default output node (leaf)
  518. if (user_out_nodes.empty()) {
  519. if (GetDefaultOutInfo(compute_graph, output_nodes_info) != SUCCESS) {
  520. REPORT_CALL_ERROR("E19999", "GetDefaultOutInfo failed for graph:%s", compute_graph->GetName().c_str());
  521. GELOGE(domi::FAILED, "[Invoke][GetDefaultOutInfo] failed, graph:%s.", compute_graph->GetName().c_str());
  522. return domi::FAILED;
  523. }
  524. }
  525. CreateOutputNodesInfo(output_nodes_info, output_nodes_name);
  526. compute_graph->SetGraphOutNodesInfo(output_nodes_info);
  527. ge::GetParserContext().net_out_nodes = output_nodes_name;
  528. GELOGI("Set graph %s output node success.", compute_graph->GetName().c_str());
  529. return domi::SUCCESS;
  530. }
  531. domi::Status AclGrphParseUtil::CheckOptions(const std::map<AscendString, AscendString> &parser_params) {
  532. for (auto &ele : parser_params) {
  533. const char *key_ascend = ele.first.GetString();
  534. if (key_ascend == nullptr) {
  535. ErrorManager::GetInstance().ATCReportErrMessage("E10016", {"parameter", "opname"},
  536. {"parser_params", "null AscendString"});
  537. GELOGE(PARAM_INVALID, "[Check][Param] Input options key is null, Please check!");
  538. return PARAM_INVALID;
  539. }
  540. string key_str = key_ascend;
  541. auto it = ge::ir_option::ir_parser_suppported_options.find(key_str);
  542. if (it == ge::ir_option::ir_parser_suppported_options.end()) {
  543. ErrorManager::GetInstance().ATCReportErrMessage("E10016", {"parameter", "opname"}, {"parser_params", key_str});
  544. GELOGE(PARAM_INVALID, "[Check][Param] Input options include unsupported option(%s).Please check!", key_ascend);
  545. return PARAM_INVALID;
  546. }
  547. }
  548. return SUCCESS;
  549. }
  550. domi::Status AclGrphParseUtil::ParseParamsBeforeGraph(const std::map<AscendString, AscendString> &parser_params,
  551. string &graph_name) {
  552. GELOGI("Parse graph user options start.");
  553. ge::GetParserContext().input_nodes_format_map.clear();
  554. ge::GetParserContext().output_formats.clear();
  555. ge::GetParserContext().user_input_dims.clear();
  556. ge::GetParserContext().input_dims.clear();
  557. ge::GetParserContext().op_conf_map.clear();
  558. ge::GetParserContext().user_out_nodes.clear();
  559. ge::GetParserContext().default_out_nodes.clear();
  560. ge::GetParserContext().out_nodes_map.clear();
  561. ge::GetParserContext().user_out_tensors.clear();
  562. ge::GetParserContext().net_out_nodes.clear();
  563. ge::GetParserContext().out_tensor_names.clear();
  564. ge::GetParserContext().data_tensor_names.clear();
  565. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(CheckOptions(parser_params) != SUCCESS,
  566. return PARAM_INVALID, "[Check][Options] Parse paragrams invalid, graph:%s.",
  567. graph_name.c_str());
  568. // support paragrams: out_nodes, is_output_adjust_hw_layout, output, enable_scope_fusion_passes
  569. SetDefaultFormat();
  570. string out_nodes;
  571. GetAclParams(parser_params, ge::ir_option::OUT_NODES, out_nodes);
  572. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ParseAclOutputNodes(out_nodes) != SUCCESS,
  573. return PARAM_INVALID,
  574. "[Invoke][ParseAclOutputNodes] Parse out_nodes failed, graph:%s.", graph_name.c_str());
  575. string is_output_adjust_hw_layout;
  576. GetAclParams(parser_params, ge::ir_option::IS_OUTPUT_ADJUST_HW_LAYOUT, is_output_adjust_hw_layout);
  577. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ParseAclOutputFp16NodesFormat(is_output_adjust_hw_layout) != SUCCESS,
  578. return PARAM_INVALID,
  579. "[Invoke][ParseAclOutputFp16NodesFormat] Parse is_output_adjust_hw_layout failed, "
  580. "graph:%s.", graph_name.c_str());
  581. string tmp_name;
  582. GetAclParams(parser_params, ge::ir_option::OUTPUT, tmp_name);
  583. graph_name = tmp_name.empty() ? (kGraphDefaultName + "_" + ge::parser::CurrentTimeInStr()) : tmp_name;
  584. string enable_scope_fusion_passes;
  585. GetAclParams(parser_params, ge::ir_option::ENABLE_SCOPE_FUSION_PASSES, enable_scope_fusion_passes);
  586. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ParseAclEnableScope(enable_scope_fusion_passes) != SUCCESS,
  587. return PARAM_INVALID,
  588. "[Invoke][ParseAclEnableScope] Parse enable_scope_fusion_passes failed, graph:%s.",
  589. graph_name.c_str());
  590. return SUCCESS;
  591. }
  592. domi::Status AclGrphParseUtil::ParseParamsAfterGraph(ge::Graph &graph,
  593. const std::map<AscendString, AscendString> &parser_params) {
  594. // support paragrams: input_fp16_nodes, is_input_adjust_hw_layout,
  595. ComputeGraphPtr compute_graph = GraphUtils::GetComputeGraph(graph);
  596. GE_CHECK_NOTNULL(compute_graph);
  597. string input_fp16_nodes;
  598. GetAclParams(parser_params, ge::ir_option::INPUT_FP16_NODES, input_fp16_nodes);
  599. string is_input_adjust_hw_layout;
  600. GetAclParams(parser_params, ge::ir_option::IS_INPUT_ADJUST_HW_LAYOUT, is_input_adjust_hw_layout);
  601. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  602. ParseAclInputFp16Nodes(compute_graph, input_fp16_nodes, is_input_adjust_hw_layout) != SUCCESS,
  603. return PARAM_INVALID, "[Invoke][ParseAclInputFp16Nodes] Parse input_fp16_nodes failed, graph:%s",
  604. compute_graph->GetName().c_str());
  605. return SUCCESS;
  606. }
  607. namespace parser {
  608. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::string RealPath(const char *path) {
  609. if (path == nullptr) {
  610. GELOGE(ge::FAILED, "path pointer is NULL.");
  611. return "";
  612. }
  613. if (strlen(path) >= PATH_MAX) {
  614. ErrorManager::GetInstance().ATCReportErrMessage("E19002", {"filepath", "size"}, {path, std::to_string(PATH_MAX)});
  615. GELOGE(ge::FAILED, "[Check][Param] Path[%s] len is too long, it must be less than %d", path, PATH_MAX);
  616. return "";
  617. }
  618. // Nullptr is returned when the path does not exist or there is no permission
  619. // Return absolute path when path is accessible
  620. std::string res;
  621. char resolved_path[PATH_MAX] = {0};
  622. if (realpath(path, resolved_path) != nullptr) {
  623. res = resolved_path;
  624. }
  625. return res;
  626. }
  627. // Get file length
  628. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY long GetFileLength(const std::string &input_file) {
  629. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(input_file.empty(),
  630. REPORT_INNER_ERROR("E19999", "input_file path is null, check invalid.");
  631. return -1, "[Check][Param] input_file path is null.");
  632. std::string real_path = RealPath(input_file.c_str());
  633. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(),
  634. REPORT_INPUT_ERROR("E19000", std::vector<std::string>({"path", "errmsg"}),
  635. std::vector<std::string>({real_path, strerror(errno)}));
  636. return -1, "[Get][Path] input_file path '%s' not valid", input_file.c_str());
  637. unsigned long long file_length = 0;
  638. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(mmGetFileSize(input_file.c_str(), &file_length) != EN_OK,
  639. ErrorManager::GetInstance().ATCReportErrMessage("E19001", {"file", "errmsg"},
  640. {input_file, strerror(errno)});
  641. return -1, "[Open][File] [%s] failed. %s", input_file.c_str(), strerror(errno));
  642. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((file_length == 0 || file_length > kMaxFileSizeLimit),
  643. REPORT_INPUT_ERROR(
  644. "E19015", std::vector<std::string>({"file", "size", "maxsize"}),
  645. std::vector<std::string>({input_file, std::to_string(file_length),
  646. std::to_string(kMaxFileSizeLimit)}));
  647. return -1, "[Check][Param] File[%s] size %lld is out of range(0,%d).",
  648. input_file.c_str(), file_length, kMaxFileSizeLimit);
  649. return static_cast<long>(file_length);
  650. }
  651. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY uint64_t GetCurrentTimestamp() {
  652. struct timeval tv{};
  653. int ret = gettimeofday(&tv, nullptr);
  654. GE_LOGE_IF(ret != 0, "[Func][GetTimeOfDay] may failed: ret=%d", ret);
  655. auto total_use_time = tv.tv_usec + tv.tv_sec * 1000000; // 1000000: seconds to microseconds
  656. return static_cast<uint64_t>(total_use_time);
  657. }
  658. static bool ReadProtoFromCodedInputStream(CodedInputStream &coded_stream, Message *proto) {
  659. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(proto == nullptr,
  660. REPORT_INNER_ERROR("E19999", "param proto is nullptr, check invalid");
  661. return false, "[Check][Param] incorrect parameter. nullptr == proto");
  662. coded_stream.SetTotalBytesLimit(kProtoReadBytesLimit);
  663. return proto->ParseFromCodedStream(&coded_stream);
  664. }
  665. /** @ingroup domi_common
  666. * @brief Read all data from binary file
  667. * @param [in] file_name File path
  668. * @param [out] buffer The address of the output memory, which needs to be released by the caller
  669. * @param [out] length Output memory size
  670. * @return false fail
  671. * @return true success
  672. */
  673. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadBytesFromBinaryFile(const char *file_name, char **buffer,
  674. int &length) {
  675. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((file_name == nullptr),
  676. REPORT_INNER_ERROR("E19999", "param file_name is nullptr, check invalid");
  677. return false, "[Check][Param] incorrect parameter. file is nullptr");
  678. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((buffer == nullptr),
  679. REPORT_INNER_ERROR("E19999", "param buffer is nullptr, check invalid");
  680. return false, "[Check][Param] incorrect parameter. buffer is nullptr");
  681. std::string real_path = RealPath(file_name);
  682. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(),
  683. REPORT_INNER_ERROR("E19999", "file path '%s' not valid, realpath failed", file_name);
  684. return false, "[Check][Param]file path '%s' not valid, realpath failed", file_name);
  685. std::ifstream file(real_path.c_str(), std::ios::binary | std::ios::ate);
  686. if (!file.is_open()) {
  687. REPORT_INNER_ERROR("E19999", "read file %s failed", file_name);
  688. GELOGE(ge::FAILED, "[Read][File] %s failed.", file_name);
  689. return false;
  690. }
  691. length = static_cast<int>(file.tellg());
  692. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((length <= 0), file.close(); REPORT_INNER_ERROR("E19999", "file length <= 0");
  693. return false, "[Check][Param] file length <= 0");
  694. file.seekg(0, std::ios::beg);
  695. *buffer = new(std::nothrow) char[length]();
  696. GE_CHK_BOOL_TRUE_EXEC_RET_STATUS(*buffer == nullptr, false, file.close();
  697. REPORT_CALL_ERROR("E19999", "new an object failed."),
  698. "[Create][Buffer] new an object failed.");
  699. file.read(*buffer, length);
  700. file.close();
  701. return true;
  702. }
  703. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadProtoFromBinaryFile(const char *file, Message *proto) {
  704. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((file == nullptr || proto == nullptr),
  705. REPORT_INNER_ERROR("E19999", "param file or proto is nullptr, check invalid");
  706. return false, "[Check][Param] Input parameter file or proto is nullptr!");
  707. std::string real_path = RealPath(file);
  708. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(),
  709. REPORT_INNER_ERROR("E19999", "file path '%s' not valid, realpath failed", file);
  710. return false, "[Check][Param]pb file path '%s' not valid, realpath failed", file);
  711. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(GetFileLength(real_path) == -1, return false, "[Get][FileLength]file size not valid.");
  712. std::ifstream fs(real_path, std::ifstream::in | std::ifstream::binary);
  713. if (!fs.is_open()) {
  714. ErrorManager::GetInstance().ATCReportErrMessage("E19001", {"file", "errmsg"}, {file, "ifstream is_open failed"});
  715. GELOGE(ge::FAILED, "[Open][RealPath][%s] failed.", file);
  716. return false;
  717. }
  718. google::protobuf::io::IstreamInputStream istream(&fs);
  719. google::protobuf::io::CodedInputStream coded_stream(&istream);
  720. bool ret = ReadProtoFromCodedInputStream(coded_stream, proto);
  721. fs.close();
  722. if (!ret) {
  723. ErrorManager::GetInstance().ATCReportErrMessage("E19005", {"file"}, {file});
  724. GELOGE(ge::FAILED, "[Read][Proto] Parse file[%s] failed.", file);
  725. return ret;
  726. }
  727. return ret;
  728. }
  729. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadProtoFromArray(const void *data, int size, Message *proto) {
  730. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((proto == nullptr || data == nullptr || size == 0),
  731. REPORT_INNER_ERROR("E19999", "param proto or data is nullptr "
  732. "or size is 0, check invalid"); return false,
  733. "[Check][Param]incorrect parameter. proto is nullptr || data is nullptr || size is 0");
  734. google::protobuf::io::CodedInputStream coded_stream(reinterpret_cast<uint8_t *>(const_cast<void *>(data)), size);
  735. return ReadProtoFromCodedInputStream(coded_stream, proto);
  736. }
  737. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadProtoFromText(const char *file,
  738. google::protobuf::Message *message) {
  739. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((file == nullptr || message == nullptr),
  740. REPORT_INNER_ERROR("E19999", "param file or message is nullptr, check invalid");
  741. return false,
  742. "[Check][Param]incorrect parameter. nullptr == file || nullptr == message");
  743. std::string real_path = RealPath(file);
  744. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(real_path.empty(),
  745. ErrorManager::GetInstance().ATCReportErrMessage("E19000", {"path", "errmsg"},
  746. {file, strerror(errno)});
  747. return false, "[Check][Param]Path[%s]'s realpath is empty, errmsg[%s]", file,
  748. strerror(errno));
  749. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(GetFileLength(real_path) == -1, return false, "[Check][Param] file size not valid.");
  750. std::ifstream fs(real_path.c_str(), std::ifstream::in);
  751. if (!fs.is_open()) {
  752. REPORT_INNER_ERROR("E19999", "open file:%s failed", real_path.c_str());
  753. GELOGE(ge::FAILED, "[Open][ProtoFile] failed, real path is '%s' when orginal file path is '%s'.",
  754. real_path.c_str(), file);
  755. return false;
  756. }
  757. google::protobuf::io::IstreamInputStream input(&fs);
  758. bool ret = google::protobuf::TextFormat::Parse(&input, message);
  759. GE_IF_BOOL_EXEC(!ret, ErrorManager::GetInstance().ATCReportErrMessage("E19018", {"protofile"}, {file});
  760. GELOGE(ret, "[Parse][File] [%s] through [google::protobuf::TextFormat::Parse] failed, "
  761. "please check whether the file is a valid protobuf format file.", file));
  762. fs.close();
  763. return ret;
  764. }
  765. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY bool ReadProtoFromMem(const char *data, int size,
  766. google::protobuf::Message *message) {
  767. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((data == nullptr || message == nullptr),
  768. REPORT_INNER_ERROR("E19999", "param data or message is nullptr,check invalid");
  769. return false,
  770. "[Check][Param] incorrect parameter. data is nullptr || message is nullptr");
  771. std::string str(data, static_cast<size_t>(size));
  772. std::istringstream fs(str);
  773. google::protobuf::io::IstreamInputStream input(&fs);
  774. bool ret = google::protobuf::TextFormat::Parse(&input, message);
  775. GE_IF_BOOL_EXEC(!ret, REPORT_CALL_ERROR("E19999", "parse failed, please check your text file.");
  776. GELOGE(ret, "[Call][Parse] ret fail, please check your text file."));
  777. return ret;
  778. }
  779. ///
  780. /// @brief get the Original Type of FrameworkOp
  781. /// @param [in] node
  782. /// @param [out] type
  783. /// @return Status
  784. ///
  785. Status GetOriginalType(const ge::NodePtr &node, string &type) {
  786. GE_CHECK_NOTNULL(node);
  787. type = node->GetType();
  788. GE_IF_BOOL_EXEC(type != FRAMEWORKOP, return SUCCESS);
  789. GE_CHECK_NOTNULL(node->GetOpDesc());
  790. bool ret = ge::AttrUtils::GetStr(node->GetOpDesc(), ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, type);
  791. if (!ret) {
  792. REPORT_CALL_ERROR("E19999", "Get FrameWorkOp original type [%s] from node:%s failed.",
  793. type.c_str(), node->GetName().c_str());
  794. GELOGE(INTERNAL_ERROR, "[Invoke][GetStr] Get FrameWorkOp original type [%s] from node:%s failed",
  795. type.c_str(), node->GetName().c_str());
  796. return INTERNAL_ERROR;
  797. }
  798. GELOGD("Get FrameWorkOp original type [%s]", type.c_str());
  799. return SUCCESS;
  800. }
  801. FMK_FUNC_HOST_VISIBILITY bool ValidateStr(const std::string &str, const std::string &mode) {
  802. char ebuff[kMaxBuffSize];
  803. regex_t reg;
  804. int cflags = REG_EXTENDED | REG_NOSUB;
  805. int ret = regcomp(&reg, mode.c_str(), cflags);
  806. if (ret) {
  807. regerror(ret, &reg, ebuff, kMaxBuffSize);
  808. GELOGW("regcomp failed, reason: %s", ebuff);
  809. regfree(&reg);
  810. return true;
  811. }
  812. ret = regexec(&reg, str.c_str(), 0, nullptr, 0);
  813. if (ret) {
  814. regerror(ret, &reg, ebuff, kMaxBuffSize);
  815. GELOGE(ge::PARAM_INVALID, "[Invoke][RegExec] failed, reason: %s", ebuff);
  816. regfree(&reg);
  817. return false;
  818. }
  819. regfree(&reg);
  820. return true;
  821. }
  822. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY std::string CurrentTimeInStr() {
  823. std::time_t now = std::time(nullptr);
  824. std::tm *ptm = std::localtime(&now);
  825. if (ptm == nullptr) {
  826. GELOGE(ge::FAILED, "[Invoke][LocalTime] failed.");
  827. return "";
  828. }
  829. const int kTimeBufferLen = 32;
  830. char buffer[kTimeBufferLen + 1] = {0};
  831. // format: 20171122042550
  832. std::strftime(buffer, kTimeBufferLen, "%Y%m%d%H%M%S", ptm);
  833. return std::string(buffer);
  834. }
  835. } // namespace parser
  836. } // namespace ge