Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[paddle FE] Fix validation of model path for legacy version (#34509)

Pawel Raasz769a0e85e3a0
diff --git a/src/frontends/paddle/src/frontend.cpp b/src/frontends/paddle/src/frontend.cpp
index efc3abee265..862554b41b3 100644
--- a/src/frontends/paddle/src/frontend.cpp
+++ b/src/frontends/paddle/src/frontend.cpp
@@ -381,29 +381,35 @@ bool FrontEnd::supported_impl(const std::vector<ov::Any>& variants) const {
// Validating first path, it must contain a model
if (variants[0].is<std::string>()) {
- std::string suffix = ".pdmodel";
- std::string model_path = variants[0].as<std::string>();
- FRONT_END_GENERAL_CHECK(util::file_exists(model_path), "Could not open the file: \"", model_path, '"');
- if (!ov::util::ends_with(model_path, suffix)) {
- model_path += paddle::get_path_sep<char>() + "__model__";
+ auto model_path = ov::util::make_path(variants[0].as<std::string>());
+ if (ov::util::directory_exists(model_path)) {
+ model_path /= "__model__";
+ FRONT_END_GENERAL_CHECK(util::file_exists(model_path), "Could not open the file: ", model_path);
+ } else {
+ FRONT_END_GENERAL_CHECK(util::file_exists(model_path), "Could not open the file: ", model_path);
+ if (model_path.extension() != ".pdmodel") {
+ return false;
+ }
}
- std::ifstream model_str(model_path, std::ios::in | std::ifstream::binary);
+
+ std::ifstream model_str(model_path, std::ifstream::binary);
// It is possible to validate here that protobuf can read model from the stream,
// but it will complicate the check, while it should be as quick as possible
return model_str && model_str.is_open();
}
#if defined(OPENVINO_ENABLE_UNICODE_PATH_SUPPORT) && defined(_WIN32)
else if (variants[0].is<std::wstring>()) {
- std::wstring suffix = L".pdmodel";
- std::wstring model_path = variants[0].as<std::wstring>();
- FRONT_END_GENERAL_CHECK(util::file_exists(model_path),
- "Could not open the file: \"",
- util::path_to_string(model_path),
- '"');
- if (!ov::util::ends_with(model_path, suffix)) {
- model_path += paddle::get_path_sep<wchar_t>() + L"__model__";
+ auto model_path = ov::util::make_path(variants[0].as<std::wstring>());
+ if (ov::util::directory_exists(model_path)) {
+ model_path /= L"__model__";
+ FRONT_END_GENERAL_CHECK(util::file_exists(model_path), "Could not open the file: ", model_path);
+ } else {
+ FRONT_END_GENERAL_CHECK(util::file_exists(model_path), "Could not open the file: ", model_path);
+ if (model_path.extension() != L".pdmodel") {
+ return false;
+ }
}
- std::ifstream model_str(model_path.c_str(), std::ios::in | std::ifstream::binary);
+ std::ifstream model_str(model_path, std::ifstream::binary);
// It is possible to validate here that protobuf can read model from the stream,
// but it will complicate the check, while it should be as quick as possible
return model_str && model_str.is_open();
diff --git a/src/frontends/paddle/tests/basic_api.cpp b/src/frontends/paddle/tests/basic_api.cpp
index 82f2f87d9b0..208058e4635 100644
--- a/src/frontends/paddle/tests/basic_api.cpp
+++ b/src/frontends/paddle/tests/basic_api.cpp
@@ -4,6 +4,10 @@
#include "basic_api.hpp"
+#include "common_test_utils/common_utils.hpp"
+#include "common_test_utils/test_assertions.hpp"
+#include "openvino/frontend/paddle/frontend.hpp"
+#include "openvino/util/file_util.hpp"
#include "paddle_utils.hpp"
using namespace ov::frontend;
@@ -24,3 +28,49 @@ INSTANTIATE_TEST_SUITE_P(PaddleBasicTest,
::testing::Values(std::string(TEST_PADDLE_MODELS_DIRNAME)),
::testing::ValuesIn(models)),
FrontEndBasicTest::getTestCaseName);
+
+TEST(PaddleBasicTest, check_supported_legacy_model) {
+ const auto test_dir = std::filesystem::path(ov::test::utils::generateTestFilePrefix());
+ const auto legacy_model_path = test_dir / "paddle_legacy.model";
+
+ {
+ ov::util::create_directory_recursive(legacy_model_path);
+ std::ofstream model(legacy_model_path / "__model__");
+ model << "Fake model data which are not important for this test";
+ }
+
+ paddle::FrontEnd fe;
+
+ EXPECT_TRUE(fe.supported({{legacy_model_path.native()}, {false}}));
+ EXPECT_FALSE(fe.supported({{legacy_model_path}, {false}})) << "Should be true if std path supported";
+
+ std::filesystem::remove_all(test_dir);
+}
+
+TEST(PaddleBasicTest, check_supported_legacy_model_not_exists) {
+ const auto test_dir = std::filesystem::path(ov::test::utils::generateTestFilePrefix());
+ const auto legacy_model_path = test_dir / "dir.with_dot" / "paddle_legacy_model";
+
+ paddle::FrontEnd fe;
+
+ OV_EXPECT_THROW(fe.supported({{legacy_model_path.native()}, {false}}), ov::Exception, testing::_);
+ EXPECT_FALSE(fe.supported({{legacy_model_path}, false})) << "Should throw if std path supported";
+}
+
+TEST(PaddleBasicTest, check_supported_incorrect_extension) {
+ const auto test_dir = std::filesystem::path(ov::test::utils::generateTestFilePrefix());
+ const auto model_path = test_dir / "model.onnx";
+
+ {
+ ov::util::create_directory_recursive(test_dir);
+ std::ofstream model(model_path);
+ model << "Fake model data which are not important for this test";
+ }
+
+ paddle::FrontEnd fe;
+
+ EXPECT_FALSE(fe.supported({{model_path.native()}, {false}}));
+ EXPECT_FALSE(fe.supported({{model_path}, {false}}));
+
+ std::filesystem::remove_all(test_dir);
+}