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
Support fs::path in register_plugin(s) & Core(xml_file_path) (#33043)
Alicja Miloszewska7227fa5eb2d4
diff --git a/src/bindings/python/src/pyopenvino/core/core.cpp b/src/bindings/python/src/pyopenvino/core/core.cppindex bf68976c563..a7f845bd89c 100644--- a/src/bindings/python/src/pyopenvino/core/core.cpp+++ b/src/bindings/python/src/pyopenvino/core/core.cpp@@ -28,7 +28,10 @@ void regclass_Core(py::module m) {"Core class instances, but in this case, the underlying plugins are created multiple times and not shared ""between several Core instances. The recommended way is to have a single Core instance per application.";- cls.def(py::init<const std::string&>(), py::arg("xml_config_file") = "");+ cls.def(py::init([](const py::object& xml_config_file) {+ return std::make_shared<ov::Core>(Common::utils::to_fs_path(xml_config_file));+ }),+ py::arg("xml_config_file") = "");cls.def("set_property",@@ -626,60 +629,44 @@ void regclass_Core(py::module m) {new_compiled = core.import_model(user_stream, "CPU"))");- cls.def(- "register_plugin",- [](ov::Core& self, const std::string& plugin_name, const std::string& device_name) {- self.register_plugin(plugin_name, device_name);- },- py::arg("plugin_name"),- py::arg("device_name"),- R"(- Register a new device and plugin which enable this device inside OpenVINO Runtime.-- :param plugin_name: A path (absolute or relative) or name of a plugin. Depending on platform,- `plugin_name` is wrapped with shared library suffix and prefix to identify- library full name E.g. on Linux platform plugin name specified as `plugin_name`- will be wrapped as `libplugin_name.so`.- :type plugin_name: str- :param device_name: A device name to register plugin for.- :type device_name: str- )");-cls.def("register_plugin",[](ov::Core& self,- const std::string& plugin_name,+ const py::object& plugin,const std::string& device_name,const std::map<std::string, py::object>& config) {- auto properties = Common::utils::properties_to_any_map(config);- self.register_plugin(plugin_name, device_name, properties);+ const auto properties = Common::utils::properties_to_any_map(config);+ self.register_plugin(Common::utils::to_fs_path(plugin), device_name, properties);},- py::arg("plugin_name"),+ py::arg("plugin"),py::arg("device_name"),- py::arg("config"),+ py::arg("config") = py::dict(),R"(Register a new device and plugin which enable this device inside OpenVINO Runtime.- :param plugin_name: A path (absolute or relative) or name of a plugin. Depending on platform,+ :param plugin: A path (absolute or relative) or name of a plugin. Depending on platform,`plugin_name` is wrapped with shared library suffix and prefix to identifylibrary full name E.g. on Linux platform plugin name specified as `plugin_name`will be wrapped as `libplugin_name.so`.- :type plugin_name: str+ :type plugin: Union[str, bytes, pathlib.Path]:param device_name: A device name to register plugin for.:type device_name: str:param config: Plugin default configuration:type config: dict[str, typing.Any], optional)");- cls.def("register_plugins",- &ov::Core::register_plugins,- py::arg("xml_config_file"),- R"(+ cls.def(+ "register_plugins",+ [](ov::Core& self, const py::object& xml_config_file) {+ self.register_plugins(Common::utils::to_fs_path(xml_config_file));+ },+ py::arg("xml_config_file"),+ R"(Registers a device plugin to OpenVINO Runtime Core instance using XML configurationfile with plugins description.:param xml_config_file: A path to .xml file with plugins to register.- :type xml_config_file: str+ :type xml_config_file: Union[str, bytes, pathlib.Path])");cls.def("unload_plugin",diff --git a/src/bindings/python/tests/test_runtime/test_core.py b/src/bindings/python/tests/test_runtime/test_core.pyindex c4bb6b8f3ae..7952ac22528 100644--- a/src/bindings/python/tests/test_runtime/test_core.py+++ b/src/bindings/python/tests/test_runtime/test_core.py@@ -393,6 +393,21 @@ def test_register_plugins():assert f"Cannot load library '{full_lib_name}'" in str(e.value)[email protected]_library+def test_core_register_plugins():+ device = "TEST_DEVICE"+ lib_name = "test_plugin"+ full_lib_name = lib_name + ".dll" if sys.platform == "win32" else "lib" + lib_name + ".so"+ plugins_xml = plugins_path(device, full_lib_name)++ core = Core(plugins_xml)+ os.remove(plugins_xml)++ with pytest.raises(RuntimeError) as e:+ core.get_versions(device)+ assert f"Cannot load library '{full_lib_name}'" in str(e.value)++def test_unload_plugin(device):core = Core()# Trigger plugin loadingdiff --git a/src/inference/include/openvino/runtime/core.hpp b/src/inference/include/openvino/runtime/core.hppindex d49234ba854..b84f84bb7b5 100644--- a/src/inference/include/openvino/runtime/core.hpp+++ b/src/inference/include/openvino/runtime/core.hpp@@ -40,6 +40,7 @@ class OPENVINO_RUNTIME_API Core {std::shared_ptr<Impl> _impl;public:+ ///@{/** @brief Constructs an OpenVINO Core instance with devices* and their plugins description.*@@ -55,6 +56,12 @@ public:*/explicit Core(const std::string& xml_config_file = {});+ explicit Core(const std::filesystem::path& xml_config_file);++ template <class TPath, std::enable_if_t<std::is_constructible_v<std::string, TPath>>* = nullptr>+ explicit Core(const TPath& xml_config_file) : Core(std::string(xml_config_file)) {}+ ///@}+/*** @brief Returns device plugins version information.* Device name can be complex and identify multiple devices at once like `HETERO:CPU,GPU`;@@ -822,6 +829,7 @@ public:*/std::vector<std::string> get_available_devices() const;+ ///@{/*** @brief Register a new device and plugin that enables this device inside OpenVINO Runtime.*@@ -833,13 +841,27 @@ public:* - If `plugin` specifies file name (`libplugin_name.so`) or plugin name (`plugin_name`), it will be searched by* file name (`libplugin_name.so`) in CWD or in paths pointed by PATH/LD_LIBRARY_PATH/DYLD_LIBRARY_PATH* environment variables depending on the platform.- * @note For security purposes it suggested to specify absolute path to register plugin.+ * @note For security, use an absolute path to register plugin.** @param device_name Device name to register a plugin for.* @param config Plugin configuration options*/void register_plugin(const std::string& plugin, const std::string& device_name, const ov::AnyMap& config = {});+ void register_plugin(const std::filesystem::path& plugin_path,+ const std::string& device_name,+ const ov::AnyMap& config = {});++ template <class Path>+ void register_plugin(const Path& plugin_path, const std::string& device_name, const AnyMap& config = {}) {+ if constexpr (std::is_constructible_v<std::string, Path>) {+ register_plugin(std::string(plugin_path), device_name, config);+ } else {+ register_plugin(std::filesystem::path(plugin_path), device_name, config);+ }+ }+ ///@}+/*** @brief Unloads the previously loaded plugin identified by @p device_name from OpenVINO Runtime.* The method is needed to remove loaded plugin instance and free its resources. If plugin for a@@ -849,6 +871,7 @@ public:*/void unload_plugin(const std::string& device_name);+ ///@{/** @brief Registers a device plugin to the OpenVINO Runtime Core instance using an XML configuration file with* plugins description.*@@ -875,12 +898,24 @@ public:* for different systems with different configurations.* - `properties` are set to a plugin via the ov::Core::set_property method.* - `extensions` are set to a plugin via the ov::Core::add_extension method.- * @note For security purposes it suggested to specify absolute path to register plugin.+ * @note For security, use an absolute path to register plugin.** @param xml_config_file A path to .xml file with plugins to register.*/void register_plugins(const std::string& xml_config_file);+ void register_plugins(const std::filesystem::path& xml_config_file);++ template <class Path>+ void register_plugins(const Path& xml_config_file) {+ if constexpr (std::is_constructible_v<std::string, Path>) {+ register_plugins(std::string(xml_config_file));+ } else {+ register_plugins(std::filesystem::path(xml_config_file));+ }+ }+ ///@}+/*** @brief Creates a new remote shared context object on the specified accelerator device* using specified plugin-specific low-level device API parameters (device handle, pointer, context, etc.).diff --git a/src/inference/src/cpp/core.cpp b/src/inference/src/cpp/core.cppindex 533b9f22301..e335a15f1ee 100644--- a/src/inference/src/cpp/core.cpp+++ b/src/inference/src/cpp/core.cpp@@ -77,6 +77,8 @@ Core::Core(const std::string& xml_config_file) {OV_CORE_CALL_STATEMENT(_impl->register_compile_time_plugins();)}+Core::Core(const std::filesystem::path& xml_config_file) : Core(ov::util::path_to_string(xml_config_file)) {}+std::map<std::string, Version> Core::get_versions(const std::string& device_name) const {OV_CORE_CALL_STATEMENT({ return _impl->get_versions(device_name); })}#ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT@@ -259,6 +261,12 @@ void Core::register_plugin(const std::string& plugin, const std::string& device_OV_CORE_CALL_STATEMENT(_impl->register_plugin(plugin, device_name, properties););}+void Core::register_plugin(const std::filesystem::path& plugin_path,+ const std::string& device_name,+ const ov::AnyMap& properties) {+ register_plugin(ov::util::path_to_string(plugin_path), device_name, properties);+}+void Core::unload_plugin(const std::string& device_name) {OV_CORE_CALL_STATEMENT({ov::DeviceIDParser parser(device_name);@@ -272,6 +280,10 @@ void Core::register_plugins(const std::string& xml_config_file) {OV_CORE_CALL_STATEMENT(_impl->register_plugins_in_registry(xml_config_file););}+void Core::register_plugins(const std::filesystem::path& xml_config_file) {+ register_plugins(ov::util::path_to_string(xml_config_file));+}+RemoteContext Core::create_context(const std::string& device_name, const AnyMap& params) {OPENVINO_ASSERT(device_name.find("HETERO") != 0, "HETERO device does not support remote context");OPENVINO_ASSERT(device_name.find("MULTI") != 0, "MULTI device does not support remote context");diff --git a/src/inference/tests/functional/ov_core_test.cpp b/src/inference/tests/functional/ov_core_test.cppindex 83e66da3a9f..47be9d28690 100644--- a/src/inference/tests/functional/ov_core_test.cpp+++ b/src/inference/tests/functional/ov_core_test.cpp@@ -52,9 +52,11 @@ protected:#endif};+class CoreBaseTestP : public ::testing::TestWithParam<ov::test::utils::StringPathVariant> {};+#ifndef OPENVINO_STATIC_LIBRARY-static void create_plugin_xml(const std::string& file_name, const std::string& plugin_name = "1") {+static void create_plugin_xml(const std::filesystem::path& file_name, const std::string& plugin_name = "1") {std::ofstream file(file_name);file << "<ie><plugins><plugin location=\"";@@ -69,7 +71,7 @@ static void create_plugin_xml(const std::string& file_name, const std::string& pfile.close();}-static void remove_plugin_xml(const std::string& file_name) {+static void remove_plugin_xml(const std::filesystem::path& file_name) {ov::test::utils::removeFile(file_name);}@@ -82,6 +84,37 @@ TEST_F(CoreBaseTest, LoadPluginXML) {remove_plugin_xml(xml_file_path);}+INSTANTIATE_TEST_SUITE_P(paths_variants, CoreBaseTestP, ::testing::Values("test_plugin.xml", L"test_plugin.xml"));++# ifdef OPENVINO_ENABLE_UNICODE_PATH_SUPPORT+INSTANTIATE_TEST_SUITE_P(unicode_paths_variants,+ CoreBaseTestP,+ ::testing::Values("test_plugin_这是.xml", L"test_plugin_这是.xml"));+# endif++TEST_P(CoreBaseTestP, LoadPluginXML) {+ const auto xml_file_path = ov::test::utils::to_fs_path(ov::test::utils::getOpenvinoLibDirectory()) /+ ov::test::utils::to_fs_path(GetParam());+ create_plugin_xml(xml_file_path, "1");+ ov::Core core(xml_file_path);+ auto versions = core.get_versions("1");+ EXPECT_FALSE(versions.empty());+ remove_plugin_xml(xml_file_path);+}++TEST_P(CoreBaseTestP, registerPlugins) {+ ov::Core core;+ std::string mock_plugin_name{"TEST_DEVICE"};+ const auto xml_file_path = ov::test::utils::to_fs_path(ov::test::utils::getOpenvinoLibDirectory()) /+ ov::test::utils::to_fs_path(GetParam());++ create_plugin_xml(xml_file_path, mock_plugin_name);+ EXPECT_NO_THROW(core.register_plugins(xml_file_path));+ auto versions = core.get_versions(mock_plugin_name);+ EXPECT_FALSE(versions.empty());+ remove_plugin_xml(xml_file_path);+}+TEST_F(CoreBaseTest, LoadPluginDifferentXMLExtension) {std::string xml_file_name = "test_plugin.test";std::string xml_file_path =diff --git a/src/inference/tests/functional/ov_register_plugin_test.cpp b/src/inference/tests/functional/ov_register_plugin_test.cppindex d3454fca902..cd3afd0c826 100644--- a/src/inference/tests/functional/ov_register_plugin_test.cpp+++ b/src/inference/tests/functional/ov_register_plugin_test.cpp@@ -184,6 +184,28 @@ TEST(RegisterPluginTests, registerNewPluginNoThrows) {core.unload_plugin(mock_plugin_name);}+class RegisterPluginTestP : public ::testing::TestWithParam<ov::test::utils::StringPathVariant> {};++INSTANTIATE_TEST_SUITE_P(paths_variants, RegisterPluginTestP, ::testing::Values("mock_engine", L"mock_engine"));++TEST_P(RegisterPluginTestP, registerNewPluginNoThrows) {+ ov::Core core;+ std::shared_ptr<ov::IPlugin> base_plugin = std::make_shared<ov::test::utils::MockPlugin>();+ std::shared_ptr<void> m_so;+ mockPlugin(core, base_plugin, m_so);++ std::string mock_plugin_name{"MOCK_HARDWARE_FS"};+ const auto dir_path = ov::test::utils::to_fs_path(ov::test::utils::getExecutableDirectory());++ const auto plugin_path =+ ov::util::make_plugin_library_name(dir_path, ov::test::utils::to_fs_path(GetParam()).concat(OV_BUILD_POSTFIX));++ OV_ASSERT_NO_THROW(core.register_plugin(plugin_path, mock_plugin_name));+ OV_ASSERT_NO_THROW(core.get_property(mock_plugin_name, ov::supported_properties));++ core.unload_plugin(mock_plugin_name);+}+TEST(RegisterPluginTests, registerExistingPluginThrows) {ov::Core core;auto plugin = std::make_shared<ov::test::utils::MockPlugin>();