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] Remove extensions on plugin unload (#32382)
Oleg Pipikind5eb1716af7f
diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cppindex 86843b71c79..4062187fd2e 100644--- a/src/inference/src/dev/core_impl.cpp+++ b/src/inference/src/dev/core_impl.cpp@@ -804,12 +804,12 @@ ov::Plugin ov::CoreImpl::get_plugin(const std::string& pluginName) const {try {std::vector<ov::Extension::Ptr> ext;desc.extensionCreateFunc(ext);- add_extensions_unsafe(ext);+ add_extensions_unsafe(ext, deviceName);} catch (const ov::Exception&) {// the same extension can be registered multiple times - ignore it!}} else {- try_to_register_plugin_extensions(desc.libraryLocation);+ try_to_register_plugin_extensions(desc.libraryLocation, deviceName);}return plugins.emplace(deviceName, plugin).first->second;@@ -1301,6 +1301,8 @@ void ov::CoreImpl::unload_plugin(const std::string& deviceName) {OPENVINO_THROW("Device with \"", deviceName, "\" name is not registered in the OpenVINO Runtime");}+ remove_extensions_for_device_unsafe(deviceName);+plugins.erase(deviceName);}@@ -1448,29 +1450,43 @@ void ov::CoreImpl::set_property_for_device(const ov::AnyMap& configMap, const st});}}-void ov::CoreImpl::add_extensions_unsafe(const std::vector<ov::Extension::Ptr>& exts) const {+void ov::CoreImpl::add_extensions_unsafe(const std::vector<ov::Extension::Ptr>& exts,+ const std::string& device_name) const {for (const auto& ext : exts) {- extensions.emplace_back(ext);+ extensions.emplace_back(ext, device_name);auto ext_obj = ext;if (auto so_ext = std::dynamic_pointer_cast<ov::detail::SOExtension>(ext_obj))ext_obj = so_ext->extension();if (auto op_base_ext = std::dynamic_pointer_cast<ov::BaseOpExtension>(ext_obj)) {for (const auto& attached_ext : op_base_ext->get_attached_extensions()) {- extensions.emplace_back(attached_ext);+ extensions.emplace_back(attached_ext, device_name);}}}}+void ov::CoreImpl::remove_extensions_for_device_unsafe(const std::string& device_name) const {+ extensions.erase(std::remove_if(extensions.begin(),+ extensions.end(),+ [&device_name](const auto& item) {+ return item.second == device_name;+ }),+ extensions.end());+}+std::vector<ov::Extension::Ptr> ov::CoreImpl::get_extensions_copy() const {std::lock_guard<std::mutex> lock(get_mutex());- std::vector<ov::Extension::Ptr> extensions_copy(extensions.begin(), extensions.end());- return extensions_copy;+ std::vector<ov::Extension::Ptr> only_extensions;+ only_extensions.reserve(extensions.size());+ for (const auto& item : extensions) {+ only_extensions.push_back(item.first);+ }+ return only_extensions;};-void ov::CoreImpl::add_extension(const std::vector<ov::Extension::Ptr>& extensions) {+void ov::CoreImpl::add_extension(const std::vector<ov::Extension::Ptr>& extensions, const std::string& device_name) {std::lock_guard<std::mutex> lock(get_mutex());- add_extensions_unsafe(extensions);+ add_extensions_unsafe(extensions, device_name);}bool ov::CoreImpl::device_supports_model_caching(const std::string& device_name) const {diff --git a/src/inference/src/dev/core_impl.hpp b/src/inference/src/dev/core_impl.hppindex 806d2a06455..d68663be6db 100644--- a/src/inference/src/dev/core_impl.hpp+++ b/src/inference/src/dev/core_impl.hpp@@ -200,7 +200,7 @@ private:std::shared_ptr<ov::threading::ExecutorManager> m_executor_manager;mutable std::unordered_set<std::string> opsetNames;- mutable std::vector<ov::Extension::Ptr> extensions;+ mutable std::vector<std::pair<ov::Extension::Ptr, std::string>> extensions;std::map<std::string, PluginDescriptor> pluginRegistry;@@ -233,15 +233,17 @@ private:bool is_hidden_device(const std::string& device_name) const;void register_plugin_in_registry_unsafe(const std::string& device_name, PluginDescriptor& desc);- void try_to_register_plugin_extensions(const ov::util::Path& path) const {+ void try_to_register_plugin_extensions(const ov::util::Path& path, const std::string& device_name) const {try {auto plugin_extensions = ov::detail::load_extensions(path.native());- add_extensions_unsafe(plugin_extensions);+ add_extensions_unsafe(plugin_extensions, device_name);} catch (const std::runtime_error&) {// in case of shared library is not opened}}- void add_extensions_unsafe(const std::vector<ov::Extension::Ptr>& extensions) const;+ void add_extensions_unsafe(const std::vector<ov::Extension::Ptr>& extensions, const std::string& device_name) const;++ void remove_extensions_for_device_unsafe(const std::string& device_name) const;std::vector<ov::Extension::Ptr> get_extensions_copy() const;@@ -306,7 +308,7 @@ public:*/void set_property_for_device(const ov::AnyMap& configMap, const std::string& deviceName);- void add_extension(const std::vector<ov::Extension::Ptr>& extensions);+ void add_extension(const std::vector<ov::Extension::Ptr>& extensions, const std::string& device_name = "");bool device_supports_model_caching(const std::string& device_name) const override;diff --git a/src/inference/tests/functional/ov_register_plugin_test.cpp b/src/inference/tests/functional/ov_register_plugin_test.cppindex c042a0d4460..d3454fca902 100644--- a/src/inference/tests/functional/ov_register_plugin_test.cpp+++ b/src/inference/tests/functional/ov_register_plugin_test.cpp@@ -88,6 +88,85 @@ TEST(RegisterPluginTests, getVersionforNoRegisteredPluginNoThrows) {OV_ASSERT_NO_THROW(core.get_versions("MOCK_HARDWARE"));}+namespace {+int get_mock_extension_counter(const std::string& so_path) {+ auto so = ov::util::load_shared_object(so_path.c_str());+ using CreateFunction = int();+ auto extension_count = reinterpret_cast<CreateFunction*>(ov::util::get_symbol(so, "get_mock_extension_counter"))();+ return extension_count;+}+} // namespace++TEST(RegisterPluginTests, removeExtensionOnPluginUnload) {+ ov::Core core;+ std::string mock_plugin_name{"MOCK_HARDWARE"};++ auto mock_plugin_path = ov::util::make_plugin_library_name(ov::test::utils::getExecutableDirectory(),+ std::string("mock_engine") + OV_BUILD_POSTFIX);+ EXPECT_EQ(get_mock_extension_counter(mock_plugin_path), 0);++ OV_ASSERT_NO_THROW(core.register_plugin(mock_plugin_path, mock_plugin_name));++ // call get_versions to force plugin loading+ OV_ASSERT_NO_THROW(core.get_versions("MOCK_HARDWARE"));++ const std::string model = R"V0G0N(+<net name="Network" version="11">+ <layers>+ <layer name="in1" type="Parameter" id="0" version="opset1">+ <data element_type="f32" shape="2,2,2,1"/>+ <output>+ <port id="0" precision="FP32">+ <dim>2</dim>+ <dim>2</dim>+ <dim>2</dim>+ <dim>1</dim>+ </port>+ </output>+ </layer>+ <layer name="operation" id="1" type="MockOp" version="custom_opset">+ <input>+ <port id="0" precision="FP32">+ <dim>2</dim>+ <dim>2</dim>+ <dim>2</dim>+ <dim>1</dim>+ </port>+ </input>+ <output>+ <port id="1" precision="FP32">+ <dim>2</dim>+ <dim>2</dim>+ <dim>2</dim>+ <dim>1</dim>+ </port>+ </output>+ </layer>+ <layer name="output" type="Result" id="2" version="opset1">+ <input>+ <port id="0" precision="FP32">+ <dim>2</dim>+ <dim>2</dim>+ <dim>2</dim>+ <dim>1</dim>+ </port>+ </input>+ </layer>+ </layers>+ <edges>+ <edge from-layer="0" from-port="0" to-layer="1" to-port="0"/>+ <edge from-layer="1" from-port="1" to-layer="2" to-port="0"/>+ </edges>+</net>+)V0G0N";+ EXPECT_NO_THROW(core.read_model(model, ov::Tensor()));+ EXPECT_EQ(get_mock_extension_counter(mock_plugin_path), 1);++ core.unload_plugin(mock_plugin_name);+ EXPECT_THROW(core.read_model(model, ov::Tensor()), ov::Exception);+ EXPECT_EQ(get_mock_extension_counter(mock_plugin_path), 0);+}+TEST(RegisterPluginTests, registerNewPluginNoThrows) {ov::Core core;auto plugin = std::make_shared<ov::test::utils::MockPlugin>();diff --git a/src/plugins/hetero/tests/functional/hetero_tests.cpp b/src/plugins/hetero/tests/functional/hetero_tests.cppindex 4800bb2cd60..d7074b03a2e 100644--- a/src/plugins/hetero/tests/functional/hetero_tests.cpp+++ b/src/plugins/hetero/tests/functional/hetero_tests.cpp@@ -918,3 +918,20 @@ void ov::hetero::tests::HeteroTests::SetUp() {reg_plugin_type<MockPluginGPU>("MOCKGPU");}}++void ov::hetero::tests::HeteroTests::TearDown() {+ for (const auto& plugin : m_mock_plugins) {+ try {+ core.unload_plugin(plugin->get_device_name());+ } catch (...) {+ }+ }+ m_mock_plugins = {};+ clearMockPlugin();+ m_so.reset();+}++void ov::hetero::tests::HeteroTests::clearMockPlugin() {+ ASSERT_TRUE(m_so);+ ov::test::utils::make_std_function<void()>(m_so, "ClearTargets")();+}diff --git a/src/plugins/hetero/tests/functional/hetero_tests.hpp b/src/plugins/hetero/tests/functional/hetero_tests.hppindex b3bb85ba78a..389bfe455a4 100644--- a/src/plugins/hetero/tests/functional/hetero_tests.hpp+++ b/src/plugins/hetero/tests/functional/hetero_tests.hpp@@ -20,6 +20,7 @@ public:ov::Core core;void SetUp() override;+ void TearDown() override;std::shared_ptr<ov::Model> create_model_with_subtract(bool dynamic = false);std::shared_ptr<ov::Model> create_model_with_subtract_reshape(bool dynamic = false);@@ -47,6 +48,8 @@ private:void reg_plugin_type(const std::string& device_name);void reg_plugin(std::shared_ptr<ov::IPlugin>& plugin);++ void clearMockPlugin();};} // namespace testsdiff --git a/src/tests/test_utils/unit_test_utils/mocks/mock_engine/mock_extensions.cpp b/src/tests/test_utils/unit_test_utils/mocks/mock_engine/mock_extensions.cppnew file mode 100644index 00000000000..a412c978938--- /dev/null+++ b/src/tests/test_utils/unit_test_utils/mocks/mock_engine/mock_extensions.cpp@@ -0,0 +1,40 @@+// Copyright (C) 2018-2025 Intel Corporation+// SPDX-License-Identifier: Apache-2.0+//++#include "openvino/core/op_extension.hpp"+#include "openvino/op/op.hpp"++class MockOp : public ov::op::Op {+public:+ OPENVINO_OP("MockOp", "custom_opset");+ MockOp() = default;++ bool visit_attributes(ov::AttributeVisitor& visitor) override {+ return true;+ }++ void validate_and_infer_types() override {+ set_output_size(1);+ set_output_type(0, get_input_element_type(0), get_input_partial_shape(0));+ }++ std::shared_ptr<Node> clone_with_new_inputs(const ov::OutputVector& inputs) const override {+ return nullptr;+ }+};++namespace {+auto shared_mock_op_extension = std::make_shared<ov::OpExtension<MockOp>>();+}++OPENVINO_EXTENSION_C_API int get_mock_extension_counter();++int get_mock_extension_counter() {+ const int basic_internal_counter = 1;+ return shared_mock_op_extension.use_count() - basic_internal_counter;+}++OPENVINO_CREATE_EXTENSIONS(std::vector<ov::Extension::Ptr>({+ shared_mock_op_extension,+}));