Skip to content

Commit 4282043

Browse files
praaszilya-lavrenovt-jankowskimlukasze
authoredJul 3, 2024
[core] Check if model file exists on read model (openvinotoolkit#24864)
### Details: - Add more detailed error messages when model and/or weights files not exist at path ### Tickets: - [CVS-142133](https://jira.devtools.intel.com/browse/CVS-142133) --------- Co-authored-by: Ilya Lavrenov <ilya.lavrenov@intel.com> Co-authored-by: Tomasz Jankowski <tomasz1.jankowski@intel.com> Co-authored-by: Michal Lukaszewski <michal.lukaszewski@intel.com>
1 parent 4506142 commit 4282043

File tree

25 files changed

+193
-27
lines changed

25 files changed

+193
-27
lines changed
 

‎src/common/util/include/openvino/util/file_util.hpp

+22
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ struct FileTraits<wchar_t> {
7777
}
7878
};
7979

80+
/**
81+
* @brief Convert path as char string to to a single-byte chain.
82+
* @param path Path as char string.
83+
* @return Reference to input path (no conversion).
84+
*/
85+
template <class Path,
86+
typename std::enable_if<std::is_same<typename std::decay<Path>::type, std::string>::value>::type* = nullptr>
87+
const std::string& path_to_string(const Path& path) {
88+
return path;
89+
}
90+
8091
#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
8192
/**
8293
* @brief Conversion from wide character string to a single-byte chain.
@@ -91,6 +102,17 @@ std::string wstring_to_string(const std::wstring& wstr);
91102
*/
92103
std::wstring string_to_wstring(const std::string& str);
93104

105+
/**
106+
* @brief Convert path as wide character string to a single-byte chain.
107+
* @param path Path as wide-char string.
108+
* @return A char string
109+
*/
110+
template <class Path,
111+
typename std::enable_if<std::is_same<typename std::decay<Path>::type, std::wstring>::value>::type* = nullptr>
112+
std::string path_to_string(const Path& path) {
113+
return ov::util::wstring_to_string(path);
114+
}
115+
94116
#endif
95117

96118
/// \brief Remove path components which would allow traversing up a directory tree.

‎src/core/tests/frontend/frontend_manager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ TEST(FrontEndManagerTest, testDefaultFrontEnd) {
117117
FrontEndManager fem;
118118
fem.register_front_end("mock1", mock_fe_path());
119119
FrontEnd::Ptr fe;
120-
ASSERT_NO_THROW(fe = fem.load_by_model(""));
120+
ASSERT_NO_THROW(fe = fem.load_by_model());
121121
ASSERT_EQ(nullptr, fe);
122122

123123
class MockFrontEnd : public FrontEnd {};

‎src/frontends/common/include/openvino/frontend/frontend.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ class FRONTEND_API FrontEnd {
152152

153153
virtual InputModel::Ptr load_impl(const std::vector<ov::Any>& variants) const;
154154

155+
void validate_path(const std::string& path) const;
156+
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
157+
void validate_path(const std::wstring& path) const;
158+
#endif
159+
155160
std::vector<ov::Extension::Ptr> m_extensions;
156161

157162
private:

‎src/frontends/common/src/frontend.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,17 @@ std::string FrontEnd::get_name() const {
108108
}
109109
FRONTEND_RETURN_STATEMENT("Getting frontend name", m_actual->get_name();)
110110
}
111+
112+
void FrontEnd::validate_path(const std::string& path) const {
113+
FRONT_END_GENERAL_CHECK(util::directory_exists(path) || util::file_exists(path),
114+
get_name(),
115+
": Could not open the file: \"",
116+
path,
117+
'"');
118+
}
119+
120+
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
121+
void FrontEnd::validate_path(const std::wstring& path) const {
122+
validate_path(ov::util::wstring_to_string(path));
123+
}
124+
#endif

‎src/frontends/ir/src/frontend.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ bool FrontEnd::supported_impl(const std::vector<ov::Any>& variants) const {
7474
const auto& model_variant = variants[0];
7575
if (model_variant.is<std::string>()) {
7676
const auto& path = model_variant.as<std::string>();
77+
validate_path(path);
7778
local_model_stream.open(path, std::ios::in | std::ifstream::binary);
7879
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
7980
} else if (model_variant.is<std::wstring>()) {
8081
const auto& path = model_variant.as<std::wstring>();
82+
validate_path(path);
8183
local_model_stream.open(path.c_str(), std::ios::in | std::ifstream::binary);
8284
#endif
8385
} else if (model_variant.is<std::istream*>()) {
@@ -149,6 +151,7 @@ InputModel::Ptr FrontEnd::load_impl(const std::vector<ov::Any>& variants) const
149151

150152
if (model_variant.is<std::string>()) {
151153
const auto& tmp_path = model_variant.as<std::string>();
154+
validate_path(tmp_path);
152155
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
153156
model_path = ov::util::string_to_wstring(tmp_path.c_str());
154157
#else
@@ -158,6 +161,7 @@ InputModel::Ptr FrontEnd::load_impl(const std::vector<ov::Any>& variants) const
158161
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
159162
} else if (model_variant.is<std::wstring>()) {
160163
model_path = model_variant.as<std::wstring>();
164+
validate_path(model_path);
161165
local_model_stream.open(model_path.c_str(), std::ios::in | std::ifstream::binary);
162166
#endif
163167
} else if (model_variant.is<std::istream*>()) {

‎src/frontends/ir/tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ ov_add_test_target(
1414
gtest_main
1515
openvino::runtime::dev
1616
common_test_utils
17+
frontend_shared_test_classes
1718
INCLUDES
1819
"${CMAKE_CURRENT_SOURCE_DIR}/../include"
1920
ADD_CLANG_FORMAT

‎src/frontends/ir/tests/frontend_test_basic.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//
44

5+
#include "common_test_utils/test_assertions.hpp"
56
#include "frontend_test.hpp"
67
#include "openvino/opsets/opset1.hpp"
78
#include "openvino/opsets/opset3.hpp"
89
#include "openvino/opsets/opset6.hpp"
10+
#include "utils.hpp"
911

1012
class IRFrontendTests : public ::testing::Test, public IRFrontendTestsImpl {
1113
protected:
@@ -1400,3 +1402,38 @@ TEST_F(IRFrontendTests, DetectionOutput) {
14001402
ASSERT_NO_THROW(model = getWithIRFrontend(testModel));
14011403
ASSERT_TRUE(!!model);
14021404
}
1405+
1406+
TEST_F(IRFrontendTests, load_model_not_exists_at_path) {
1407+
const auto model_name = "not_existing_model";
1408+
auto error_msg = std::string("Could not open the file: ");
1409+
auto model_file_path = FrontEndTestUtils::make_model_path(model_name);
1410+
error_msg += '"' + model_file_path + '"';
1411+
1412+
auto fem = ov::frontend::FrontEndManager();
1413+
auto fe = fem.load_by_framework("ir");
1414+
1415+
OV_EXPECT_THROW(fe->supported({model_file_path}), ov::Exception, testing::HasSubstr(error_msg));
1416+
OV_EXPECT_THROW(fe->load(model_file_path), ov::Exception, testing::HasSubstr(error_msg));
1417+
}
1418+
1419+
TEST_F(IRFrontendTests, load_model_weights_not_exist_at_path) {
1420+
const auto error_msg = std::string(" cannot be opened");
1421+
const auto name_prefix = ov::test::utils::generateTestFilePrefix();
1422+
const auto model_file_path = name_prefix + "existing_model.xml";
1423+
const auto weights_file_path = name_prefix + "not_existing_weights.bin";
1424+
1425+
{
1426+
std::ofstream model_file;
1427+
model_file.open(model_file_path);
1428+
model_file.close();
1429+
}
1430+
1431+
auto fem = ov::frontend::FrontEndManager();
1432+
auto fe = fem.load_by_framework("ir");
1433+
1434+
OV_EXPECT_THROW(fe->load(model_file_path, weights_file_path),
1435+
ov::Exception,
1436+
testing::HasSubstr(weights_file_path + error_msg));
1437+
1438+
std::remove(model_file_path.c_str());
1439+
}

‎src/frontends/onnx/frontend/src/frontend.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,13 @@ bool FrontEnd::supported_impl(const std::vector<ov::Any>& variants) const {
192192
std::ifstream model_stream;
193193
if (variants[0].is<std::string>()) {
194194
const auto path = variants[0].as<std::string>();
195+
validate_path(path);
195196
model_stream.open(path, std::ios::in | std::ifstream::binary);
196197
}
197198
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
198199
else if (variants[0].is<std::wstring>()) {
199200
const auto path = variants[0].as<std::wstring>();
201+
validate_path(path);
200202
model_stream.open(path.c_str(), std::ios::in | std::ifstream::binary);
201203
}
202204
#endif

‎src/frontends/onnx/onnx_common/src/parser.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ModelProto parse_from_file(const std::string& file_path) {
2121
std::ifstream file_stream{file_path.c_str(), std::ios::in | std::ios::binary};
2222

2323
if (!file_stream.is_open()) {
24-
OPENVINO_THROW("Could not open the file: " + file_path);
24+
OPENVINO_THROW("Could not open the file: \"" + file_path, '"');
2525
};
2626

2727
auto model_proto = parse_from_istream(file_stream);
@@ -34,7 +34,7 @@ ModelProto parse_from_file(const std::wstring& file_path) {
3434
std::ifstream file_stream{file_path.c_str(), std::ios::in | std::ios::binary};
3535

3636
if (!file_stream.is_open()) {
37-
OPENVINO_THROW("Could not open the file: " + ov::util::wstring_to_string(file_path));
37+
OPENVINO_THROW("Could not open the file: \"", ov::util::wstring_to_string(file_path), '"');
3838
};
3939

4040
auto model_proto = parse_from_istream(file_stream);

‎src/frontends/onnx/tests/load_from.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <fstream>
99

10+
#include "common_test_utils/test_assertions.hpp"
1011
#include "onnx_utils.hpp"
1112
#include "utils.hpp"
1213

@@ -43,6 +44,19 @@ TEST_P(FrontEndLoadFromTest, testLoadFromStreamAndPassPath) {
4344
ASSERT_NE(function, nullptr);
4445
}
4546

47+
TEST_P(FrontEndLoadFromTest, load_model_not_exists_at_path) {
48+
const auto model_name = "not_existing_model";
49+
auto error_msg = std::string("Could not open the file: ");
50+
auto model_file_path = FrontEndTestUtils::make_model_path(model_name);
51+
error_msg += '"' + model_file_path + '"';
52+
53+
auto fem = ov::frontend::FrontEndManager();
54+
auto fe = fem.load_by_framework("onnx");
55+
56+
OV_EXPECT_THROW(fe->supported({model_file_path}), ov::Exception, testing::HasSubstr(error_msg));
57+
OV_EXPECT_THROW(fe->load(model_file_path), ov::Exception, testing::HasSubstr(error_msg));
58+
}
59+
4660
INSTANTIATE_TEST_SUITE_P(ONNXLoadTest,
4761
FrontEndLoadFromTest,
4862
::testing::Values(getTestData()),

‎src/frontends/onnx/tests/tests_python/test_frontend_onnx.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,15 @@ def test_load_by_model():
318318
decoded_function = fe.decode(model)
319319
assert decoded_function
320320

321-
assert not fem.load_by_model("test.xx")
322-
assert not fem.load_by_model("onnx.yy")
321+
with pytest.raises(Exception) as e:
322+
fem.load_by_model("test.xx")
323+
324+
assert e.match(r'Could not open the file: "test.xx"')
325+
326+
with pytest.raises(Exception) as e:
327+
fem.load_by_model("onnx.yy")
328+
329+
assert e.match(r'Could not open the file: "onnx.yy"')
323330

324331

325332
def test_onnx_conversion_extension_check_attributes():

‎src/frontends/paddle/src/frontend.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ bool FrontEnd::supported_impl(const std::vector<ov::Any>& variants) const {
378378
if (variants[0].is<std::string>()) {
379379
std::string suffix = ".pdmodel";
380380
std::string model_path = variants[0].as<std::string>();
381+
FRONT_END_GENERAL_CHECK(util::file_exists(model_path), "Could not open the file: \"", model_path, '"');
381382
if (!ov::util::ends_with(model_path, suffix)) {
382383
model_path += paddle::get_path_sep<char>() + "__model__";
383384
}
@@ -390,6 +391,10 @@ bool FrontEnd::supported_impl(const std::vector<ov::Any>& variants) const {
390391
else if (variants[0].is<std::wstring>()) {
391392
std::wstring suffix = L".pdmodel";
392393
std::wstring model_path = variants[0].as<std::wstring>();
394+
FRONT_END_GENERAL_CHECK(util::file_exists(model_path),
395+
"Could not open the file: \"",
396+
util::path_to_string(model_path),
397+
'"');
393398
if (!ov::util::ends_with(model_path, suffix)) {
394399
model_path += paddle::get_path_sep<wchar_t>() + L"__model__";
395400
}

‎src/frontends/paddle/src/input_model.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,10 @@ InputModel::InputModelImpl::InputModelImpl(const std::basic_string<T>& path,
399399
std::ifstream weights_stream;
400400
std::ifstream pb_stream(get_model_path<T>(path, &weights_stream).c_str(), std::ios::in | std::ifstream::binary);
401401

402-
FRONT_END_GENERAL_CHECK(pb_stream && pb_stream.is_open(), "Model file doesn't exist");
402+
FRONT_END_GENERAL_CHECK(pb_stream && pb_stream.is_open(),
403+
"Could not open the file: \"",
404+
util::path_to_string(path),
405+
'"');
403406
FRONT_END_GENERAL_CHECK(m_fw_ptr->ParseFromIstream(&pb_stream), "Model can't be parsed");
404407
// According to Paddle, the saved model has the framework version
405408
// For example Paddle 2.1.0 is encoded as 2001000. 0 means the latest framework.
@@ -650,4 +653,4 @@ void InputModel::set_tensor_value(const Place::Ptr& place, const void* value) {
650653

651654
} // namespace paddle
652655
} // namespace frontend
653-
} // namespace ov
656+
} // namespace ov

‎src/frontends/tensorflow/src/frontend.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,8 @@ ov::frontend::InputModel::Ptr FrontEnd::load_impl(const std::vector<ov::Any>& va
282282
HashTableKeysValuesMap{},
283283
graph_iterator->get_checkpoint_v1_reader(),
284284
false);
285-
}
286-
auto saved_model_tags = paths[1];
287-
if (GraphIteratorSavedModel::is_supported(model_path)) {
285+
} else if (GraphIteratorSavedModel::is_supported(model_path)) {
286+
auto saved_model_tags = paths[1];
288287
std::shared_ptr<GraphIteratorSavedModel> graph_iterator;
289288
graph_iterator = std::make_shared<GraphIteratorSavedModel>(model_path, saved_model_tags, mmap_enabled);
290289
return std::make_shared<InputModel>(graph_iterator,

‎src/frontends/tensorflow/src/graph_iterator_meta.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class GraphIteratorMeta : public GraphIteratorProto {
4545

4646
template <typename T>
4747
static bool is_supported(const std::basic_string<T>& path) {
48+
FRONT_END_GENERAL_CHECK(util::directory_exists(path) || util::file_exists(path),
49+
"Could not open the file: \"",
50+
util::path_to_string(path),
51+
'"');
4852
try {
4953
std::ifstream mg_stream(path.c_str(), std::ios::in | std::ifstream::binary);
5054
auto metagraph_def = std::make_shared<::tensorflow::MetaGraphDef>();

‎src/frontends/tensorflow/src/graph_iterator_proto.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "openvino/frontend/exception.hpp"
1717
#include "openvino/frontend/graph_iterator.hpp"
1818
#include "openvino/frontend/tensorflow/decoder.hpp"
19+
#include "openvino/util/file_util.hpp"
1920
#include "ov_tensorflow/graph.pb.h"
2021

2122
namespace ov {
@@ -141,6 +142,10 @@ class GraphIteratorProto : public GraphIterator {
141142
/// \brief Check if the input file is supported
142143
template <typename T>
143144
static bool is_supported(const std::basic_string<T>& path) {
145+
FRONT_END_GENERAL_CHECK(util::directory_exists(path) || util::file_exists(path),
146+
"Could not open the file: \"",
147+
util::path_to_string(path),
148+
'"');
144149
try {
145150
#if defined(__MINGW32__) || defined(__MINGW64__)
146151
std::ifstream pb_stream(std::filesystem::path(path), std::ios::in | std::ifstream::binary);

‎src/frontends/tensorflow/src/graph_iterator_proto_txt.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "google/protobuf/text_format.h"
1414
#include "graph_iterator_proto.hpp"
1515
#include "openvino/frontend/exception.hpp"
16+
#include "openvino/util/file_util.hpp"
1617

1718
namespace ov {
1819
namespace frontend {
@@ -59,6 +60,10 @@ class GraphIteratorProtoTxt : public GraphIteratorProto {
5960
/// \brief Check if the input file is supported
6061
template <typename T>
6162
static bool is_supported(const std::basic_string<T>& path) {
63+
FRONT_END_GENERAL_CHECK(util::directory_exists(path) || util::file_exists(path),
64+
"Could not open the file: \"",
65+
util::path_to_string(path),
66+
'"');
6267
try {
6368
std::ifstream pbtxt_stream(path.c_str(), std::ios::in);
6469
bool model_exists = (pbtxt_stream && pbtxt_stream.is_open());

‎src/frontends/tensorflow/src/graph_iterator_saved_model.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ bool GraphIteratorSavedModel::is_valid_signature(const ::tensorflow::SignatureDe
3030
}
3131

3232
bool GraphIteratorSavedModel::is_supported(const std::string& path) {
33-
return ov::util::directory_exists(path) && ov::util::file_exists(ov::util::path_join({path, "saved_model.pb"}));
33+
if (ov::util::directory_exists(path)) {
34+
FRONT_END_GENERAL_CHECK(util::file_exists(ov::util::path_join({path, "saved_model.pb"})),
35+
"Could not open the file: \"",
36+
util::path_to_string(ov::util::path_join({path, "saved_model.pb"})),
37+
'"');
38+
return true;
39+
} else {
40+
return false;
41+
}
3442
}
3543

3644
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)

‎src/frontends/tensorflow_lite/src/graph_iterator_flatbuffer.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ template <typename T>
3030
std::basic_string<T> get_model_extension() {}
3131
template <>
3232
std::basic_string<char> get_model_extension<char>();
33+
3334
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
3435
template <>
3536
std::basic_string<wchar_t> get_model_extension<wchar_t>();
@@ -58,6 +59,10 @@ class GraphIteratorFlatBuffer : public GraphIterator {
5859
/// Verifies file is supported
5960
template <typename T>
6061
static bool is_supported(const std::basic_string<T>& path) {
62+
FRONT_END_GENERAL_CHECK(util::file_exists(path),
63+
"Could not open the file: \"",
64+
util::path_to_string(path),
65+
'"');
6166
try {
6267
if (!ov::util::ends_with<T>(path, get_model_extension<T>())) {
6368
return false;

‎src/frontends/tensorflow_lite/tests/convert_unsupported.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//
44

5+
#include "common_test_utils/test_assertions.hpp"
56
#include "convert_model.hpp"
67
#include "tf_utils.hpp"
78
#include "utils.hpp"
@@ -16,8 +17,8 @@ TEST(FrontEndConvertModelTest, test_zerolen) {
1617
InputModel::Ptr inputModel;
1718
ASSERT_NO_THROW(frontEnd = fem.load_by_framework(TF_LITE_FE));
1819
ASSERT_NE(frontEnd, nullptr);
19-
auto model_filename =
20-
FrontEndTestUtils::make_model_path(string(TEST_TENSORFLOW_LITE_MODELS_DIRNAME) + string("zerolen.tflite"));
20+
auto model_filename = FrontEndTestUtils::make_model_path(string(TEST_TENSORFLOW_LITE_MODELS_DIRNAME) +
21+
string("bad_header/zerolen.tflite"));
2122
ASSERT_NO_THROW(inputModel = frontEnd->load(model_filename));
2223
ASSERT_NE(inputModel, nullptr);
2324
shared_ptr<ov::Model> model;
@@ -44,8 +45,8 @@ TEST(FrontEndConvertModelTest, test_wrong_pos) {
4445
InputModel::Ptr inputModel;
4546
ASSERT_NO_THROW(frontEnd = fem.load_by_framework(TF_LITE_FE));
4647
ASSERT_NE(frontEnd, nullptr);
47-
auto model_filename =
48-
FrontEndTestUtils::make_model_path(string(TEST_TENSORFLOW_LITE_MODELS_DIRNAME) + string("wrong_pos.tflite"));
48+
auto model_filename = FrontEndTestUtils::make_model_path(string(TEST_TENSORFLOW_LITE_MODELS_DIRNAME) +
49+
string("bad_header/wrong_pos.tflite"));
4950
ASSERT_NO_THROW(inputModel = frontEnd->load(model_filename));
5051
ASSERT_NE(inputModel, nullptr);
5152
shared_ptr<ov::Model> model;

‎src/frontends/tests/frontend/shared/src/basic_api.cpp

+16-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
#include "basic_api.hpp"
66

7+
#include "common_test_utils/test_assertions.hpp"
78
#include "utils.hpp"
89

910
using namespace ov::frontend;
1011

1112
std::string FrontEndBasicTest::getTestCaseName(const testing::TestParamInfo<BasicTestParam>& obj) {
12-
std::string fe, path, fileName;
13-
std::tie(fe, path, fileName) = obj.param;
13+
const auto& fe = std::get<0>(obj.param);
14+
const auto& fileName = std::get<2>(obj.param);
1415
return fe + "_" + FrontEndTestUtils::fileToTestName(fileName);
1516
}
1617

@@ -173,3 +174,16 @@ TEST_P(FrontEndBasicTest, testInputModel_overrideAll_empty) {
173174
EXPECT_FALSE(m_inputModel->get_place_by_tensor_name(name)->is_input());
174175
});
175176
}
177+
178+
TEST_P(FrontEndBasicTest, load_model_not_exists_at_path) {
179+
const auto model_name = "not_existing_model.tflite";
180+
auto error_msg = std::string("Could not open the file: \"");
181+
const auto model_file_path = FrontEndTestUtils::make_model_path(model_name);
182+
error_msg += model_file_path;
183+
184+
auto fem = ov::frontend::FrontEndManager();
185+
auto fe = fem.load_by_framework(m_feName);
186+
187+
OV_EXPECT_THROW(fe->supported({model_file_path}), ov::Exception, testing::HasSubstr(error_msg));
188+
OV_EXPECT_THROW(fe->load(model_file_path), ov::Exception, testing::HasSubstr(error_msg));
189+
}

‎src/inference/tests/unit/core.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "openvino/runtime/device_id_parser.hpp"
1717
#include "openvino/util/file_util.hpp"
1818

19-
using namespace testing;
2019
using namespace ov::util;
2120

2221
TEST(CoreTests, Throw_on_register_plugin_twice) {

‎tests/layer_tests/mo_python_api_tests/test_mo_convert_pytorch.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1123,8 +1123,9 @@ def test_failed_extension(self):
11231123
from openvino.tools.mo import convert_model
11241124

11251125
with self.assertRaisesRegex(Exception, ".*PyTorch Frontend doesn't support provided model type.*"):
1126-
with tempfile.NamedTemporaryFile() as tmpfile:
1126+
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
11271127
convert_model(tmpfile.name, framework="pytorch")
1128+
os.remove(tmpfile.name)
11281129

11291130

11301131
def create_pytorch_layer_norm(tmp_dir):

‎tests/layer_tests/ovc_python_api_tests/test_pytorch.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1328,11 +1328,13 @@ def relu_bad(n):
13281328

13291329
def test_failed_extension(self):
13301330
import tempfile
1331+
import os
13311332
from openvino.tools.ovc import convert_model
13321333

13331334
with self.assertRaisesRegex(Exception, ".*Cannot recognize input model.*"):
1334-
with tempfile.NamedTemporaryFile() as tmpfile:
1335+
with tempfile.NamedTemporaryFile(delete=False) as tmpfile:
13351336
convert_model(tmpfile.name)
1337+
os.remove(tmpfile.name)
13361338

13371339

13381340
def create_model_three_inputs():

‎tools/mo/unit_tests/moc_tf_fe/conversion_incorrect_models_test.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,15 @@ def test_conversion_empty_model(self):
6060
),
6161
]
6262
for use_new_frontend, use_legacy_frontend, framework, exp_reg_exp in test_cases:
63-
with tempfile.NamedTemporaryFile(mode='w') as tmp, self.assertRaisesRegex(Exception,
64-
exp_reg_exp):
63+
with tempfile.NamedTemporaryFile(
64+
mode="w", delete=False
65+
) as tmp, self.assertRaisesRegex(Exception, exp_reg_exp):
6566
tmp.write("")
67+
# on Windows tmp file must be not deleted on close to avoid remove it when reopened by MO
6668
convert_model(tmp.name,
6769
use_new_frontend=use_new_frontend, use_legacy_frontend=use_legacy_frontend,
6870
framework=framework)
71+
os.remove(tmp.name)
6972

7073
def test_conversion_fake_model_with_no_ext(self):
7174
test_cases = [
@@ -91,9 +94,15 @@ def test_conversion_fake_model_with_no_ext(self):
9194
),
9295
]
9396
for use_new_frontend, use_legacy_frontend, framework, exp_reg_exp in test_cases:
94-
with tempfile.NamedTemporaryFile(mode='w') as tmp, self.assertRaisesRegex(Exception,
95-
exp_reg_exp):
97+
with tempfile.NamedTemporaryFile(
98+
mode="w", delete=False
99+
) as tmp, self.assertRaisesRegex(Exception, exp_reg_exp):
96100
tmp.write("1212234\n12312")
97-
convert_model(tmp.name,
98-
use_new_frontend=use_new_frontend, use_legacy_frontend=use_legacy_frontend,
99-
framework=framework)
101+
# on Windows tmp file must be not deleted on close to avoid remove it when reopened by MO
102+
convert_model(
103+
tmp.name,
104+
use_new_frontend=use_new_frontend,
105+
use_legacy_frontend=use_legacy_frontend,
106+
framework=framework,
107+
)
108+
os.remove(tmp.name)

0 commit comments

Comments
 (0)
Please sign in to comment.