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

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