From c6db3828a550f6e1c5c32bea12efb24728cd7a7d Mon Sep 17 00:00:00 2001 From: zhangxiaokun Date: Wed, 20 Apr 2022 12:27:47 +0800 Subject: [PATCH] Fix error code of layer invalid --- parser/caffe/caffe_parser.cc | 17 +++++++++++++---- parser/caffe/caffe_parser.h | 2 ++ tests/st/parser_st_utils.cc | 14 +++++++++++++- tests/st/parser_st_utils.h | 1 + tests/st/testcase/test_caffe_parser.cc | 19 +++++++++++++++++++ tests/ut/parser/parser_ut_utils.cc | 13 +++++++++++++ tests/ut/parser/parser_ut_utils.h | 1 + .../caffe_parser_testcase/caffe_parser_unittest.cc | 21 ++++++++++++++++++++- 8 files changed, 82 insertions(+), 6 deletions(-) diff --git a/parser/caffe/caffe_parser.cc b/parser/caffe/caffe_parser.cc index 25b5f95..e74b697 100644 --- a/parser/caffe/caffe_parser.cc +++ b/parser/caffe/caffe_parser.cc @@ -1435,6 +1435,18 @@ Status CaffeModelParser::SaveDataLayerTops(const domi::caffe::LayerParameter &la return SUCCESS; } +Status CaffeModelParser::ReportLayerInvalid(const domi::caffe::NetParameter &proto, const std::string &path) const { + if (proto.layers_size() > 0) { + ErrorManager::GetInstance().ATCReportErrMessage("E11021", {"realpath"}, {path}); + GELOGE(FAILED, "[Check][Size]The model file[%s] is consisted of layers-structure which is deprecated in Caffe " + "and unsupported in ATC. The \"layers\" should be changed to \"layer\".", path.c_str()); + } else { + ErrorManager::GetInstance().ATCReportErrMessage("E11022"); + GELOGE(FAILED, "[Check][Size]net layer num is zero, prototxt file may be invalid."); + } + return FAILED; +} + Status CaffeModelParser::Parse(const char *model_path, ge::ComputeGraphPtr &graph) { bool has_error = false; GE_CHECK_NOTNULL(model_path); @@ -1458,10 +1470,7 @@ Status CaffeModelParser::Parse(const char *model_path, ge::ComputeGraphPtr &grap "[Parse][Model] by custom proto failed, model path: %s.", model_path); if (proto_message.layer_size() == 0) { - ErrorManager::GetInstance().ATCReportErrMessage("E11021", {"realpath"}, {model_path}); - GELOGE(FAILED, "[Check][Size]The model file[%s] is consisted of layers-structure which is deprecated in Caffe " - "and unsupported in ATC. The \"layers\" should be changed to \"layer\".", model_path); - return FAILED; + return ReportLayerInvalid(proto_message, model_path); } GE_RETURN_WITH_LOG_IF_ERROR(ProtoTypePassManager::Instance().Run(&proto_message, domi::CAFFE), diff --git a/parser/caffe/caffe_parser.h b/parser/caffe/caffe_parser.h index 36bf244..fcfc294 100644 --- a/parser/caffe/caffe_parser.h +++ b/parser/caffe/caffe_parser.h @@ -313,6 +313,8 @@ class PARSER_FUNC_VISIBILITY CaffeModelParser : public domi::ModelParser { Status SaveDataLayerTops(const domi::caffe::LayerParameter &layer); + Status ReportLayerInvalid(const domi::caffe::NetParameter &proto, const std::string &path) const; + std::map node_map; // key: blob name, value: layer name and index diff --git a/tests/st/parser_st_utils.cc b/tests/st/parser_st_utils.cc index 3ec7869..8a87100 100644 --- a/tests/st/parser_st_utils.cc +++ b/tests/st/parser_st_utils.cc @@ -21,7 +21,9 @@ #include #include #include - +#include +#include +#include namespace ge { void ParerSTestsUtils::ClearParserInnerCtx() { @@ -131,4 +133,14 @@ void ParerSTestsUtils::WriteProtoToBinaryFile(const google::protobuf::Message &p out.close(); delete[] buf; } + +void ParerSTestsUtils::WriteProtoToTextFile(const google::protobuf::Message &proto, const char *filename) { + const int32_t fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 384U); + if (fd >= 0) { + google::protobuf::io::FileOutputStream output(fd); + google::protobuf::TextFormat::Print(proto, &output); + output.Close(); + close(fd); + } +} } // namespace ge diff --git a/tests/st/parser_st_utils.h b/tests/st/parser_st_utils.h index bf957fe..fef86e0 100644 --- a/tests/st/parser_st_utils.h +++ b/tests/st/parser_st_utils.h @@ -31,6 +31,7 @@ class ParerSTestsUtils { static MemBuffer* MemBufferFromFile(const char *path); static bool ReadProtoFromText(const char *file, google::protobuf::Message *message); static void WriteProtoToBinaryFile(const google::protobuf::Message &proto, const char *filename); + static void WriteProtoToTextFile(const google::protobuf::Message &proto, const char *filename); }; } // namespace ge diff --git a/tests/st/testcase/test_caffe_parser.cc b/tests/st/testcase/test_caffe_parser.cc index 81c655c..b57ad90 100644 --- a/tests/st/testcase/test_caffe_parser.cc +++ b/tests/st/testcase/test_caffe_parser.cc @@ -228,6 +228,25 @@ TEST_F(STestCaffeParser, acl_caffe_parser) { caffe_op_map.clear(); ret = ge::aclgrphParseCaffe(model_file.c_str(), weight_file.c_str(), parser_params, graph); EXPECT_EQ(ret, GRAPH_FAILED); + + { + proto.set_name("empty_layer"); + auto &layers = *proto.add_layers(); + layers.set_name("layers"); + + proto.clear_layer(); + const std::string empty_layer = case_dir + "/origin_models/empty_layer.pbtxt"; + ParerSTestsUtils::WriteProtoToTextFile(proto, empty_layer.c_str()); + EXPECT_EQ(ge::aclgrphParseCaffe(empty_layer.c_str(), weight_file.c_str(), parser_params, graph), FAILED); + + proto.clear_layers(); + const std::string empty_layers = case_dir + "/origin_models/empty_layers.pbtxt"; + ParerSTestsUtils::WriteProtoToTextFile(proto, empty_layers.c_str()); + EXPECT_EQ(ge::aclgrphParseCaffe(empty_layers.c_str(), weight_file.c_str(), parser_params, graph), FAILED); + + unlink(empty_layer.c_str()); + unlink(empty_layers.c_str()); + } } TEST_F(STestCaffeParser, modelparser_parsefrommemory_success) diff --git a/tests/ut/parser/parser_ut_utils.cc b/tests/ut/parser/parser_ut_utils.cc index 6cb3818..12d3da8 100644 --- a/tests/ut/parser/parser_ut_utils.cc +++ b/tests/ut/parser/parser_ut_utils.cc @@ -21,6 +21,9 @@ #include #include #include +#include +#include +#include namespace ge { void ParerUTestsUtils::ClearParserInnerCtx() { @@ -131,6 +134,16 @@ void ParerUTestsUtils::WriteProtoToBinaryFile(const google::protobuf::Message &p delete[] buf; } +void ParerUTestsUtils::WriteProtoToTextFile(const google::protobuf::Message &proto, const char *filename) { + const int32_t fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 384U); + if (fd >= 0) { + google::protobuf::io::FileOutputStream output(fd); + google::protobuf::TextFormat::Print(proto, &output); + output.Close(); + close(fd); + } +} + namespace ut { NodePtr GraphBuilder::AddNode(const std::string &name, const std::string &type, int in_cnt, int out_cnt, Format format, DataType data_type, std::vector shape) { diff --git a/tests/ut/parser/parser_ut_utils.h b/tests/ut/parser/parser_ut_utils.h index 4f95e4b..bbc52c6 100644 --- a/tests/ut/parser/parser_ut_utils.h +++ b/tests/ut/parser/parser_ut_utils.h @@ -32,6 +32,7 @@ class ParerUTestsUtils { static MemBuffer* MemBufferFromFile(const char *path); static bool ReadProtoFromText(const char *file, google::protobuf::Message *message); static void WriteProtoToBinaryFile(const google::protobuf::Message &proto, const char *filename); + static void WriteProtoToTextFile(const google::protobuf::Message &proto, const char *filename); }; namespace ut { diff --git a/tests/ut/parser/testcase/caffe_parser_testcase/caffe_parser_unittest.cc b/tests/ut/parser/testcase/caffe_parser_testcase/caffe_parser_unittest.cc index cbf06c8..4649e3d 100755 --- a/tests/ut/parser/testcase/caffe_parser_testcase/caffe_parser_unittest.cc +++ b/tests/ut/parser/testcase/caffe_parser_testcase/caffe_parser_unittest.cc @@ -267,10 +267,29 @@ TEST_F(UtestCaffeParser, acl_caffe_parser) { EXPECT_EQ(ret, GRAPH_FAILED); ret = ge::aclgrphParseCaffe(model_file.c_str(), weight_file.c_str(), graph); EXPECT_EQ(ret, GRAPH_FAILED); - + caffe_op_map.clear(); ret = ge::aclgrphParseCaffe(model_file.c_str(), weight_file.c_str(), parser_params, graph); EXPECT_EQ(ret, GRAPH_FAILED); + + { + proto.set_name("empty_layer"); + auto &layers = *proto.add_layers(); + layers.set_name("layers"); + + proto.clear_layer(); + const std::string empty_layer = case_dir + "/caffe_model/empty_layer.pbtxt"; + ParerUTestsUtils::WriteProtoToTextFile(proto, empty_layer.c_str()); + EXPECT_EQ(ge::aclgrphParseCaffe(empty_layer.c_str(), weight_file.c_str(), parser_params, graph), FAILED); + + proto.clear_layers(); + const std::string empty_layers = case_dir + "/caffe_model/empty_layers.pbtxt"; + ParerUTestsUtils::WriteProtoToTextFile(proto, empty_layers.c_str()); + EXPECT_EQ(ge::aclgrphParseCaffe(empty_layers.c_str(), weight_file.c_str(), parser_params, graph), FAILED); + + unlink(empty_layer.c_str()); + unlink(empty_layers.c_str()); + } } TEST_F(UtestCaffeParser, ParseFromMemory_success)