Skip to content

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

[Core][Unicode] read_model supports std::filesystem::path initialized with type std::wstring (#30843)

Wang, Yang42307ca36384
diff --git a/src/inference/include/openvino/runtime/core.hpp b/src/inference/include/openvino/runtime/core.hpp
index b6e9725dc42..1ee2c64b7ed 100644
--- a/src/inference/include/openvino/runtime/core.hpp
+++ b/src/inference/include/openvino/runtime/core.hpp
@@ -84,6 +84,12 @@ public:
std::shared_ptr<ov::Model> read_model(const std::wstring& model_path,
const std::wstring& bin_path = {},
const ov::AnyMap& properties = {}) const;
+ template <class Path,
+ std::enable_if_t<std::is_same_v<Path, std::filesystem::path> &&
+ std::is_same_v<typename Path::value_type, wchar_t>>* = nullptr>
+ auto read_model(const Path& model_path, const Path& bin_path = {}, const ov::AnyMap& properties = {}) const {
+ return read_model(model_path.wstring(), bin_path.wstring(), properties);
+ }
#endif
/**
@@ -106,7 +112,9 @@ public:
const std::string& bin_path = {},
const ov::AnyMap& properties = {}) const;
- template <class Path, std::enable_if_t<std::is_same_v<Path, std::filesystem::path>>* = nullptr>
+ template <class Path,
+ std::enable_if_t<std::is_same_v<Path, std::filesystem::path> &&
+ std::is_same_v<typename Path::value_type, char>>* = nullptr>
auto read_model(const Path& model_path, const Path& bin_path = {}, const ov::AnyMap& properties = {}) const {
return read_model(model_path.string(), bin_path.string(), properties);
}
diff --git a/src/inference/tests/functional/ov_core_test.cpp b/src/inference/tests/functional/ov_core_test.cpp
index f2bbcf6f3ac..3ea9f66f11a 100644
--- a/src/inference/tests/functional/ov_core_test.cpp
+++ b/src/inference/tests/functional/ov_core_test.cpp
@@ -8,6 +8,7 @@
#include "common_test_utils/common_utils.hpp"
#include "common_test_utils/file_utils.hpp"
+#include "common_test_utils/unicode_utils.hpp"
#include "functional_test_utils/test_model/test_model.hpp"
#include "openvino/runtime/core.hpp"
#include "openvino/util/file_util.hpp"
@@ -19,13 +20,36 @@ protected:
model_file_name = prefix + name + ".xml";
weight_file_name = prefix + name + ".bin";
ov::test::utils::generate_test_model(model_file_name, weight_file_name);
+#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
+ for (std::size_t testIndex = 0; testIndex < ov::test::utils::test_unicode_postfix_vector.size(); testIndex++) {
+ std::wstring postfix_w = ov::util::string_to_wstring(prefix) + L"_" +
+ ov::test::utils::test_unicode_postfix_vector[testIndex] +
+ ov::util::string_to_wstring(name);
+ auto model_file_path_w = postfix_w + L".xml";
+ auto weight_file_path_w = postfix_w + L".bin";
+ ov::test::utils::generate_test_model(model_file_path_w, weight_file_path_w);
+ model_files_name_w.push_back(model_file_path_w);
+ weight_files_name_w.push_back(weight_file_path_w);
+ }
+#endif
}
void TearDown() override {
ov::test::utils::removeIRFiles(model_file_name, weight_file_name);
+#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
+ for (auto& m_path_w : model_files_name_w) {
+ ov::test::utils::removeFile(m_path_w);
+ }
+ for (auto& w_path_w : weight_files_name_w) {
+ ov::test::utils::removeFile(w_path_w);
+ }
+#endif
}
std::string model_file_name, weight_file_name;
+#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
+ std::vector<std::wstring> model_files_name_w, weight_files_name_w;
+#endif
};
#ifndef OPENVINO_STATIC_LIBRARY
@@ -114,6 +138,33 @@ TEST_F(CoreBaseTest, LoadOVFolderOverCWPathPluginXML) {
#endif
+#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
+TEST_F(CoreBaseTest, read_model_with_std_fs_path_unicode) {
+ generate_test_model_files("test-model");
+ ov::Core core;
+ for (std::size_t testIndex = 0; testIndex < ov::test::utils::test_unicode_postfix_vector.size(); testIndex++) {
+ std::wstring model_file_name_w = model_files_name_w[testIndex];
+ std::wstring weight_file_name_w = weight_files_name_w[testIndex];
+ std::filesystem::path model_path, weight_path;
+# ifdef _WIN32
+ model_path = std::filesystem::path(model_file_name_w);
+ weight_path = std::filesystem::path(weight_file_name_w);
+# else
+ model_path = std::filesystem::path(ov::util::wstring_to_string(model_file_name_w));
+ weight_path = std::filesystem::path(ov::util::wstring_to_string(weight_file_name_w));
+# endif
+
+ {
+ const auto model = core.read_model(model_path);
+ EXPECT_NE(model, nullptr);
+ }
+ {
+ const auto model = core.read_model(model_path, weight_path);
+ EXPECT_NE(model, nullptr);
+ }
+ }
+}
+#endif
namespace ov::test {
TEST_F(CoreBaseTest, read_model_with_std_fs_path) {
generate_test_model_files("test-model");
diff --git a/src/tests/test_utils/functional_test_utils/include/functional_test_utils/test_model/test_model.hpp b/src/tests/test_utils/functional_test_utils/include/functional_test_utils/test_model/test_model.hpp
index 0d05b84a38d..059b5763c0f 100644
--- a/src/tests/test_utils/functional_test_utils/include/functional_test_utils/test_model/test_model.hpp
+++ b/src/tests/test_utils/functional_test_utils/include/functional_test_utils/test_model/test_model.hpp
@@ -14,6 +14,21 @@ namespace ov {
namespace test {
namespace utils {
+#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
+/**
+ * @brief generates IR files (XML and BIN files) with the test model.
+ * Passed reference vector is filled with OpenVINO operations to validate after the network reading.
+ * @param model_path used to serialize the generated network
+ * @param weights_path used to serialize the generated weights
+ * @param input_type input element type of the generated model
+ * @param input_shape dims on the input layer of the generated model
+ */
+void generate_test_model(const std::wstring& model_path,
+ const std::wstring& weights_path,
+ const ov::element::Type& input_type = ov::element::f32,
+ const ov::PartialShape& input_shape = {1, 3, 227, 227});
+#endif
+
/**
* @brief generates IR files (XML and BIN files) with the test model.
* Passed reference vector is filled with OpenVINO operations to validate after the network reading.
diff --git a/src/tests/test_utils/functional_test_utils/src/test_model/test_model.cpp b/src/tests/test_utils/functional_test_utils/src/test_model/test_model.cpp
index d77fb248652..b596f14bfa5 100644
--- a/src/tests/test_utils/functional_test_utils/src/test_model/test_model.cpp
+++ b/src/tests/test_utils/functional_test_utils/src/test_model/test_model.cpp
@@ -8,11 +8,26 @@
#include "openvino/core/partial_shape.hpp"
#include "openvino/pass/manager.hpp"
#include "openvino/pass/serialize.hpp"
+#include "openvino/util/wstring_convert_util.hpp"
namespace ov {
namespace test {
namespace utils {
-
+#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT
+void generate_test_model(const std::wstring& model_path,
+ const std::wstring& weights_path,
+ const ov::element::Type& input_type,
+ const ov::PartialShape& input_shape) {
+ ov::pass::Manager manager;
+# ifdef _WIN32
+ manager.register_pass<ov::pass::Serialize>(model_path, weights_path);
+# else
+ manager.register_pass<ov::pass::Serialize>(ov::util::wstring_to_string(model_path),
+ ov::util::wstring_to_string(weights_path));
+# endif
+ manager.run_passes(ov::test::utils::make_conv_pool_relu(input_shape.to_shape(), input_type));
+}
+#endif
void generate_test_model(const std::string& model_path,
const std::string& weights_path,
const ov::element::Type& input_type,