OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[core] Make the internal implementation of Core.read_model(fs::path) the base implementation (#33973)
Alicja Miloszewskabfba353b1d68
diff --git a/src/inference/dev_api/openvino/runtime/icore.hpp b/src/inference/dev_api/openvino/runtime/icore.hppindex 928868da246..81e393588f8 100644--- a/src/inference/dev_api/openvino/runtime/icore.hpp+++ b/src/inference/dev_api/openvino/runtime/icore.hpp@@ -63,9 +63,9 @@ public:* @param properties Optional map of pairs: (property name, property value) relevant only for this read operation.* @return shared pointer to ov::Model*/- virtual std::shared_ptr<ov::Model> read_model(const std::string& model_path,- const std::string& bin_path,- const AnyMap& properties) const = 0;+ virtual std::shared_ptr<ov::Model> read_model(const std::filesystem::path& model_path,+ const std::filesystem::path& bin_path,+ const ov::AnyMap& properties) const = 0;virtual ov::AnyMap create_compile_config(const std::string& device_name, const ov::AnyMap& origConfig) const = 0;diff --git a/src/inference/include/openvino/runtime/core.hpp b/src/inference/include/openvino/runtime/core.hppindex f953b819fed..8f1e30ae525 100644--- a/src/inference/include/openvino/runtime/core.hpp+++ b/src/inference/include/openvino/runtime/core.hpp@@ -112,13 +112,20 @@ 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>- auto read_model(const Path& model_path, const Path& bin_path = {}, const ov::AnyMap& properties = {}) const {- if constexpr (std::is_same_v<typename Path::value_type, wchar_t>) {- return read_model(model_path.wstring(), bin_path.wstring(), properties);+ std::shared_ptr<ov::Model> read_model(const std::filesystem::path& model_path,+ const std::filesystem::path& bin_path = {},+ const ov::AnyMap& properties = {}) const;++ template <class Path>+ std::shared_ptr<ov::Model> read_model(const Path& model_path,+ const Path& bin_path = {},+ const ov::AnyMap& properties = {}) const {+ if constexpr (std::is_constructible_v<std::string, Path>) {+ return read_model(std::string(model_path), std::string(bin_path), properties);+ } else if constexpr (std::is_constructible_v<std::wstring, Path>) {+ return read_model(std::wstring(model_path), std::wstring(bin_path), properties);} else {- // use string conversion as default- return read_model(model_path.string(), bin_path.string(), properties);+ return read_model(std::filesystem::path(model_path), std::filesystem::path(bin_path), properties);}}/// @}@@ -141,22 +148,19 @@ public:* @{*/template <typename... Properties>- util::EnableIfAllStringAny<CompiledModel, Properties...> read_model(const std::string& model_path,- const std::string& bin_path,- Properties&&... properties) const {+ util::EnableIfAllStringAny<std::shared_ptr<ov::Model>, Properties...> read_model(const std::string& model_path,+ const std::string& bin_path,+ Properties&&... properties) const {return read_model(model_path, bin_path, AnyMap{std::forward<Properties>(properties)...});}template <class Path,class... Properties,std::enable_if_t<std::is_same_v<Path, std::filesystem::path> && (sizeof...(Properties) > 0)>* = nullptr>- auto read_model(const Path& model_path, const Path& bin_path, Properties&&... properties) const {- if constexpr (std::is_same_v<typename Path::value_type, wchar_t>) {- return read_model(model_path.wstring(), bin_path.wstring(), std::forward<Properties>(properties)...);- } else {- // use string conversion as default- return read_model(model_path.string(), bin_path.string(), std::forward<Properties>(properties)...);- }+ std::shared_ptr<ov::Model> read_model(const Path& model_path,+ const Path& bin_path,+ Properties&&... properties) const {+ return read_model(model_path, bin_path, AnyMap{std::forward<Properties>(properties)...});}/// @}diff --git a/src/inference/src/cpp/core.cpp b/src/inference/src/cpp/core.cppindex d08b6bed0b0..3996213da7a 100644--- a/src/inference/src/cpp/core.cpp+++ b/src/inference/src/cpp/core.cpp@@ -74,15 +74,22 @@ Core::Core(const std::filesystem::path& xml_config_file) : _impl(std::make_share}std::map<std::string, Version> Core::get_versions(const std::string& device_name) const {- OV_CORE_CALL_STATEMENT({ return _impl->get_versions(device_name); })}+ OV_CORE_CALL_STATEMENT(return _impl->get_versions(device_name););+}++std::shared_ptr<ov::Model> Core::read_model(const std::filesystem::path& model_path,+ const std::filesystem::path& bin_path,+ const ov::AnyMap& properties) const {+ OV_ITT_SCOPED_REGION_BASE(ov::itt::domains::OV, "Read model");+ OV_CORE_CALL_STATEMENT(return _impl->read_model(model_path, bin_path, properties););+}+#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORTstd::shared_ptr<ov::Model> Core::read_model(const std::wstring& model_path,const std::wstring& bin_path,const ov::AnyMap& properties) const {OV_ITT_SCOPED_REGION_BASE(ov::itt::domains::OV, "Read model");- OV_CORE_CALL_STATEMENT(return _impl->read_model(ov::util::wstring_to_string(model_path),- ov::util::wstring_to_string(bin_path),- properties););+ return read_model(ov::util::make_path(model_path), ov::util::make_path(bin_path), properties);}#endif@@ -90,7 +97,7 @@ std::shared_ptr<ov::Model> Core::read_model(const std::string& model_path,const std::string& bin_path,const AnyMap& properties) const {OV_ITT_SCOPED_REGION_BASE(ov::itt::domains::OV, "Read model");- OV_CORE_CALL_STATEMENT(return _impl->read_model(model_path, bin_path, properties););+ return read_model(ov::util::make_path(model_path), ov::util::make_path(bin_path), properties);}std::shared_ptr<ov::Model> Core::read_model(const std::string& model, const ov::Tensor& weights) const {diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cppindex bb319299f76..29292b77c26 100644--- a/src/inference/src/dev/core_impl.cpp+++ b/src/inference/src/dev/core_impl.cpp@@ -1726,16 +1726,13 @@ void ov::CoreImpl::add_mutex(const std::string& dev_name) {m_dev_mutexes[dev_name];}-std::shared_ptr<ov::Model> ov::CoreImpl::read_model(const std::string& modelPath,- const std::string& binPath,+std::shared_ptr<ov::Model> ov::CoreImpl::read_model(const std::filesystem::path& model_path,+ const std::filesystem::path& bin_path,const AnyMap& properties) const {OV_ITT_SCOPE(FIRST_INFERENCE, ov::itt::domains::ReadTime, "CoreImpl::read_model from file");auto local_core_config = m_core_config;local_core_config.set(properties, {});- return ov::util::read_model(ov::util::make_path(modelPath),- ov::util::make_path(binPath),- get_extensions_copy(),- local_core_config.get_enable_mmap());+ return ov::util::read_model(model_path, bin_path, get_extensions_copy(), local_core_config.get_enable_mmap());}std::shared_ptr<ov::Model> ov::CoreImpl::read_model(const std::string& model,diff --git a/src/inference/src/dev/core_impl.hpp b/src/inference/src/dev/core_impl.hppindex 27ec21ffbbe..7302874d7cb 100644--- a/src/inference/src/dev/core_impl.hpp+++ b/src/inference/src/dev/core_impl.hpp@@ -302,9 +302,9 @@ public:std::shared_ptr<ov::Model> read_model(const std::shared_ptr<AlignedBuffer>& model,const std::shared_ptr<AlignedBuffer>& weights) const override;- std::shared_ptr<ov::Model> read_model(const std::string& model_path,- const std::string& bin_path,- const AnyMap& properties) const override;+ std::shared_ptr<ov::Model> read_model(const std::filesystem::path& model_path,+ const std::filesystem::path& bin_path,+ const ov::AnyMap& properties) const override;ov::SoPtr<ov::ICompiledModel> compile_model(const std::shared_ptr<const ov::Model>& model,const std::string& device_name,diff --git a/src/inference/src/dev/iplugin.cpp b/src/inference/src/dev/iplugin.cppindex 8b796e9f028..4e6d63e839d 100644--- a/src/inference/src/dev/iplugin.cpp+++ b/src/inference/src/dev/iplugin.cpp@@ -80,7 +80,7 @@ std::shared_ptr<ov::ICompiledModel> ov::IPlugin::compile_model(const std::stringconst ov::AnyMap& properties) const {auto core = get_core();OPENVINO_ASSERT(core);- const auto model = core->read_model(model_path, {}, properties);+ const auto model = core->read_model(util::make_path(model_path), {}, properties);auto local_properties = properties;if (!ov::is_virtual_device(get_device_name())) {CoreConfig::remove_core(local_properties);diff --git a/src/inference/src/model_reader.cpp b/src/inference/src/model_reader.cppindex 9d079f389c1..0ecf76c40b8 100644--- a/src/inference/src/model_reader.cpp+++ b/src/inference/src/model_reader.cpp@@ -148,8 +148,7 @@ std::shared_ptr<ov::Model> read_model(const std::filesystem::path& model_path,model_path," Please check that model format: ",model_path.extension(),- " is supported and the model is correct.",- " Available frontends: ",+ " is supported and the model is correct. Available frontends: ",FEs);}diff --git a/src/inference/tests/functional/ov_core_test.cpp b/src/inference/tests/functional/ov_core_test.cppindex 1112b2ee59b..849d15034f4 100644--- a/src/inference/tests/functional/ov_core_test.cpp+++ b/src/inference/tests/functional/ov_core_test.cpp@@ -240,27 +240,29 @@ TEST_F(CoreBaseTest, compile_model_with_std_fs_path) {#endif}-TEST_P(UnicodePathTest, core_compile_model) {- const std::string model_name = "test-model";+TEST_P(UnicodePathTest, read_compile_model) {+ const std::string model_name = "test-model.xml";const auto prefix_dir = utils::generateTestFilePrefix();- const auto test_dir = std::filesystem::path(prefix_dir) / fs_path_from_variant();- const auto model_path = test_dir / std::filesystem::path(model_name + ".xml");- const auto weight_path = test_dir / std::filesystem::path(model_name + ".bin");- ov::test::utils::generate_test_model(model_path, weight_path);+ const auto model_path =+ std::filesystem::path(prefix_dir) / utils::to_fs_path(GetParam()) / std::filesystem::path(model_name);+ ov::test::utils::generate_test_model(model_path, "");ov::Core core;- {- const auto model = core.compile_model(model_path);- EXPECT_TRUE(model);- }- {- const auto devices = core.get_available_devices();+ const auto visitor = [&](const auto& param) {+ using ParamT = std::decay_t<decltype(param)>;- const auto model = core.compile_model(model_path, devices.at(0), ov::AnyMap{});- EXPECT_TRUE(model);- }+ const auto sep = ov::test::utils::FileTraits<typename ParamT::value_type>::file_separator;+ const auto model_path = ParamT(prefix_dir.begin(), prefix_dir.end()) + sep + param + sep ++ ParamT(model_name.begin(), model_name.end());++ const auto model = core.read_model(model_path);+ EXPECT_NE(model, nullptr);+ const auto compiled_model = core.compile_model(model_path);+ EXPECT_TRUE(compiled_model);+ };+ run_test_visitor(visitor);std::filesystem::remove_all(prefix_dir);}diff --git a/src/plugins/intel_npu/src/plugin/src/plugin.cpp b/src/plugins/intel_npu/src/plugin/src/plugin.cppindex 3cae1228737..6bdcec9592c 100644--- a/src/plugins/intel_npu/src/plugin/src/plugin.cpp+++ b/src/plugins/intel_npu/src/plugin/src/plugin.cpp@@ -20,6 +20,7 @@#include "npuw/llm_compiled_model.hpp"#include "npuw/serialization.hpp"#include "openvino/core/rt_info/weightless_caching_attributes.hpp"+#include "openvino/util/file_util.hpp"#include "openvino/op/constant.hpp"#include "openvino/op/parameter.hpp"#include "openvino/runtime/intel_npu/properties.hpp"@@ -1036,7 +1037,8 @@ std::shared_ptr<ov::ICompiledModel> Plugin::parse(const ov::Tensor& tensorBig,". A \".bin\" or \".onnx\" extension was expected.");}- originalModel = get_core()->read_model(xmlPath, weightsPath, properties);+ originalModel =+ get_core()->read_model(ov::util::make_path(xmlPath), ov::util::make_path(weightsPath), properties);} else {OPENVINO_THROW("Attempted to load a weightless compiled model, but no weights have been provided");}diff --git a/src/tests/test_utils/common_test_utils/include/common_test_utils/file_utils.hpp b/src/tests/test_utils/common_test_utils/include/common_test_utils/file_utils.hppindex 5650fefcb36..4bb26053bdd 100644--- a/src/tests/test_utils/common_test_utils/include/common_test_utils/file_utils.hpp+++ b/src/tests/test_utils/common_test_utils/include/common_test_utils/file_utils.hpp@@ -92,6 +92,25 @@ struct FileTraits<wchar_t> {#endif}};+template <>+struct FileTraits<char16_t> {+ static constexpr const auto file_separator =+#ifdef _WIN32+ u'\\';+#else+ u'/';+#endif+};++template <>+struct FileTraits<char32_t> {+ static constexpr const auto file_separator =+#ifdef _WIN32+ U'\\';+#else+ U'/';+#endif+};template <class T>inline std::string to_string_c_locale(T value) {diff --git a/src/tests/test_utils/common_test_utils/include/common_test_utils/unicode_utils.hpp b/src/tests/test_utils/common_test_utils/include/common_test_utils/unicode_utils.hppindex 419cd6dac73..32bbee70445 100644--- a/src/tests/test_utils/common_test_utils/include/common_test_utils/unicode_utils.hpp+++ b/src/tests/test_utils/common_test_utils/include/common_test_utils/unicode_utils.hpp@@ -169,6 +169,10 @@ namespace ov::test {class UnicodePathTest : public testing::Test, public ::testing::WithParamInterface<utils::StringPathVariant> {protected:std::filesystem::path get_path_param() const;- std::filesystem::path fs_path_from_variant() const;+ template <class TestVisitor>+ auto run_test_visitor(TestVisitor&& func) {+ return std::visit(func, GetParam());+ }};+} // namespace ov::testdiff --git a/src/tests/test_utils/common_test_utils/src/unicode_utils.cpp b/src/tests/test_utils/common_test_utils/src/unicode_utils.cppindex b89222bc391..377a07f0660 100644--- a/src/tests/test_utils/common_test_utils/src/unicode_utils.cpp+++ b/src/tests/test_utils/common_test_utils/src/unicode_utils.cpp@@ -35,37 +35,14 @@ std::filesystem::path UnicodePathTest::get_path_param() const {},GetParam());}-std::filesystem::path UnicodePathTest::fs_path_from_variant() const {- return std::visit(- [](const auto& p) {- return std::filesystem::path(p);- },- GetParam());-}-INSTANTIATE_TEST_SUITE_P(string_paths,- UnicodePathTest,- testing::Values("test_encoder/test_encoder.encrypted/",- "test_encoder/test_encoder.encrypted"));--INSTANTIATE_TEST_SUITE_P(u16_paths,- UnicodePathTest,- testing::Values(u"test_encoder/dot.folder", u"test_encoder/dot.folder/"));--INSTANTIATE_TEST_SUITE_P(u32_paths,- UnicodePathTest,- testing::Values(U"test_encoder/dot.folder", U"test_encoder/dot.folder/"));--INSTANTIATE_TEST_SUITE_P(wstring_paths,- UnicodePathTest,- testing::Values(L"test_encoder/test_encoder.encrypted",- L"test_encoder/test_encoder.encrypted/"));+const static auto test_paths = testing::Values("test_folder", L"test_folder", u"test_folder", U"test_folder");+INSTANTIATE_TEST_SUITE_P(test_paths, UnicodePathTest, test_paths);#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT-INSTANTIATE_TEST_SUITE_P(- unicode_paths,- UnicodePathTest,- testing::Values("这是.folder", L"这是_folder", L"这是_folder/", u"这是_folder/", U"这是_folder/"));+const static auto unicode_paths = testing::Values("这是_folder", L"这是_folder", u"这是_folder", U"这是_folder");++INSTANTIATE_TEST_SUITE_P(unicode_paths, UnicodePathTest, unicode_paths);#endif} // namespace ov::testdiff --git a/src/tests/test_utils/common_test_utils/tests/file_util_test.cpp b/src/tests/test_utils/common_test_utils/tests/file_util_test.cppindex c2031475ec7..5a5a079a5d3 100644--- a/src/tests/test_utils/common_test_utils/tests/file_util_test.cpp+++ b/src/tests/test_utils/common_test_utils/tests/file_util_test.cpp@@ -636,7 +636,33 @@ TEST_F(FileUtilTest, androidWithCutFileSizeTest) {}#endif-TEST_P(UnicodePathTest, create_directories) {+using FileUtilTestP = UnicodePathTest;++INSTANTIATE_TEST_SUITE_P(string_paths,+ FileUtilTestP,+ testing::Values("test_encoder/test_encoder.encrypted/",+ "test_encoder/test_encoder.encrypted"));+INSTANTIATE_TEST_SUITE_P(u16_paths,+ FileUtilTestP,+ testing::Values(u"test_encoder/dot.folder", u"test_encoder/dot.folder/"));++INSTANTIATE_TEST_SUITE_P(u32_paths,+ FileUtilTestP,+ testing::Values(U"test_encoder/dot.folder", U"test_encoder/dot.folder/"));++INSTANTIATE_TEST_SUITE_P(wstring_paths,+ FileUtilTestP,+ testing::Values(L"test_encoder/test_encoder.encrypted",+ L"test_encoder/test_encoder.encrypted/"));++#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT+INSTANTIATE_TEST_SUITE_P(+ unicode_paths,+ FileUtilTestP,+ testing::Values("这是.folder", L"这是_folder", L"这是_folder/", u"这是_folder/", U"这是_folder/"));+#endif++TEST_P(FileUtilTestP, create_directories) {const auto test_dir = utils::generateTestFilePrefix();const auto path = std::filesystem::path(test_dir) / get_path_param();const auto exp_path = std::filesystem::path(test_dir) / utils::to_fs_path(GetParam());diff --git a/src/tests/test_utils/unit_test_utils/mocks/openvino/runtime/mock_icore.hpp b/src/tests/test_utils/unit_test_utils/mocks/openvino/runtime/mock_icore.hppindex 9c1967fa12b..0f9ca1068e3 100644--- a/src/tests/test_utils/unit_test_utils/mocks/openvino/runtime/mock_icore.hpp+++ b/src/tests/test_utils/unit_test_utils/mocks/openvino/runtime/mock_icore.hpp@@ -53,7 +53,7 @@ public:MOCK_METHOD(std::shared_ptr<ov::Model>, read_model, (const std::string&, const ov::Tensor&, bool), (const));MOCK_METHOD(std::shared_ptr<ov::Model>,read_model,- (const std::string&, const std::string&, const ov::AnyMap&),+ (const std::filesystem::path&, const std::filesystem::path&, const ov::AnyMap&),(const));MOCK_METHOD(std::shared_ptr<ov::Model>,read_model,