Skip to content

Commit 5971e0f

Browse files
authored
[core]Use Plugin descriptor to store plugin extensions (#32656)
### Details: - Update core implementation to use plugin descriptors to keep extension handlers per plugin ### Tickets: - N/A Signed-off-by: Raasz, Pawel <pawel.raasz@intel.com>
1 parent f5a0261 commit 5971e0f

File tree

2 files changed

+26
-40
lines changed

2 files changed

+26
-40
lines changed

src/inference/src/dev/core_impl.cpp

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ std::filesystem::path get_cache_model_path(const ov::AnyMap& config) {
273273
return it == config.end() ? std::filesystem::path{} : it->second.as<std::filesystem::path>();
274274
}
275275

276+
std::vector<ov::Extension::Ptr> try_get_extensions(const std::filesystem::path& path) {
277+
try {
278+
return ov::detail::load_extensions(path.native());
279+
} catch (const std::runtime_error&) {
280+
return {};
281+
}
282+
}
276283
} // namespace
277284

278285
bool ov::is_config_applicable(const std::string& user_device_name, const std::string& subprop_device_name) {
@@ -626,9 +633,8 @@ void ov::CoreImpl::register_plugins_in_registry(const std::string& xml_config_fi
626633

627634
if (extensionsNode) {
628635
FOREACH_CHILD (extensionNode, extensionsNode, "extension") {
629-
ov::util::FilePath extensionLocation =
630-
ov::util::to_file_path(pugixml::get_str_attr(extensionNode, "location").c_str());
631-
listOfExtentions.push_back(extensionLocation);
636+
const auto extension_location = pugixml::get_str_attr(extensionNode, "location");
637+
listOfExtentions.push_back(ov::util::make_path(extension_location));
632638
}
633639
}
634640

@@ -805,17 +811,17 @@ ov::Plugin ov::CoreImpl::get_plugin(const std::string& pluginName) const {
805811
// add plugin as extension itself
806812
std::lock_guard<std::mutex> g_lock(get_mutex());
807813

814+
std::vector<ov::Extension::Ptr> ext;
808815
if (desc.extensionCreateFunc) { // static OpenVINO case
809816
try {
810-
std::vector<ov::Extension::Ptr> ext;
811817
desc.extensionCreateFunc(ext);
812-
add_extensions_unsafe(ext, deviceName);
813818
} catch (const ov::Exception&) {
814819
// the same extension can be registered multiple times - ignore it!
815820
}
816821
} else {
817-
try_to_register_plugin_extensions(desc.libraryLocation, deviceName);
822+
ext = try_get_extensions(desc.libraryLocation);
818823
}
824+
std::move(ext.begin(), ext.end(), std::back_inserter(pluginRegistry.at(deviceName).extensions));
819825

820826
return plugins.emplace(deviceName, plugin).first->second;
821827
} catch (const ov::Exception& ex) {
@@ -1305,9 +1311,7 @@ void ov::CoreImpl::unload_plugin(const std::string& deviceName) {
13051311
if (it == plugins.end()) {
13061312
OPENVINO_THROW("Device with \"", deviceName, "\" name is not registered in the OpenVINO Runtime");
13071313
}
1308-
1309-
remove_extensions_for_device_unsafe(deviceName);
1310-
1314+
pluginRegistry[deviceName].extensions.clear();
13111315
plugins.erase(deviceName);
13121316
}
13131317

@@ -1455,43 +1459,33 @@ void ov::CoreImpl::set_property_for_device(const ov::AnyMap& configMap, const st
14551459
});
14561460
}
14571461
}
1458-
void ov::CoreImpl::add_extensions_unsafe(const std::vector<ov::Extension::Ptr>& exts,
1459-
const std::string& device_name) const {
1462+
void ov::CoreImpl::add_extensions_unsafe(const std::vector<ov::Extension::Ptr>& exts) const {
14601463
for (const auto& ext : exts) {
1461-
extensions.emplace_back(ext, device_name);
1464+
extensions.emplace_back(ext);
14621465
auto ext_obj = ext;
14631466
if (auto so_ext = std::dynamic_pointer_cast<ov::detail::SOExtension>(ext_obj))
14641467
ext_obj = so_ext->extension();
14651468
if (auto op_base_ext = std::dynamic_pointer_cast<ov::BaseOpExtension>(ext_obj)) {
14661469
for (const auto& attached_ext : op_base_ext->get_attached_extensions()) {
1467-
extensions.emplace_back(attached_ext, device_name);
1470+
extensions.emplace_back(attached_ext);
14681471
}
14691472
}
14701473
}
14711474
}
14721475

1473-
void ov::CoreImpl::remove_extensions_for_device_unsafe(const std::string& device_name) const {
1474-
extensions.erase(std::remove_if(extensions.begin(),
1475-
extensions.end(),
1476-
[&device_name](const auto& item) {
1477-
return item.second == device_name;
1478-
}),
1479-
extensions.end());
1480-
}
1481-
14821476
std::vector<ov::Extension::Ptr> ov::CoreImpl::get_extensions_copy() const {
14831477
std::lock_guard<std::mutex> lock(get_mutex());
1484-
std::vector<ov::Extension::Ptr> only_extensions;
1485-
only_extensions.reserve(extensions.size());
1486-
for (const auto& item : extensions) {
1487-
only_extensions.push_back(item.first);
1478+
auto only_extensions = extensions;
1479+
auto ext_it = std::back_inserter(only_extensions);
1480+
for (const auto& [_, plugin_desc] : pluginRegistry) {
1481+
std::copy(plugin_desc.extensions.begin(), plugin_desc.extensions.end(), ext_it);
14881482
}
14891483
return only_extensions;
14901484
};
14911485

1492-
void ov::CoreImpl::add_extension(const std::vector<ov::Extension::Ptr>& extensions, const std::string& device_name) {
1486+
void ov::CoreImpl::add_extension(const std::vector<ov::Extension::Ptr>& extensions) {
14931487
std::lock_guard<std::mutex> lock(get_mutex());
1494-
add_extensions_unsafe(extensions, device_name);
1488+
add_extensions_unsafe(extensions);
14951489
}
14961490

14971491
bool ov::CoreImpl::device_supports_model_caching(const std::string& device_name) const {

src/inference/src/dev/core_impl.hpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class CoreImpl : public ov::ICore, public std::enable_shared_from_this<ov::ICore
178178
std::vector<ov::util::FilePath> listOfExtentions;
179179
CreatePluginEngineFunc* pluginCreateFunc = nullptr;
180180
CreateExtensionFunc* extensionCreateFunc = nullptr;
181+
mutable std::vector<Extension::Ptr> extensions; // mutable because of lazy init
181182

182183
PluginDescriptor() = default;
183184

@@ -200,7 +201,7 @@ class CoreImpl : public ov::ICore, public std::enable_shared_from_this<ov::ICore
200201

201202
std::shared_ptr<ov::threading::ExecutorManager> m_executor_manager;
202203
mutable std::unordered_set<std::string> opsetNames;
203-
mutable std::vector<std::pair<ov::Extension::Ptr, std::string>> extensions;
204+
mutable std::vector<Extension::Ptr> extensions;
204205

205206
std::map<std::string, PluginDescriptor> pluginRegistry;
206207

@@ -233,17 +234,8 @@ class CoreImpl : public ov::ICore, public std::enable_shared_from_this<ov::ICore
233234
bool is_hidden_device(const std::string& device_name) const;
234235
void register_plugin_in_registry_unsafe(const std::string& device_name, PluginDescriptor& desc);
235236

236-
void try_to_register_plugin_extensions(const ov::util::Path& path, const std::string& device_name) const {
237-
try {
238-
auto plugin_extensions = ov::detail::load_extensions(path.native());
239-
add_extensions_unsafe(plugin_extensions, device_name);
240-
} catch (const std::runtime_error&) {
241-
// in case of shared library is not opened
242-
}
243-
}
244-
void add_extensions_unsafe(const std::vector<ov::Extension::Ptr>& extensions, const std::string& device_name) const;
245237

246-
void remove_extensions_for_device_unsafe(const std::string& device_name) const;
238+
void add_extensions_unsafe(const std::vector<ov::Extension::Ptr>& extensions) const;
247239

248240
std::vector<ov::Extension::Ptr> get_extensions_copy() const;
249241

@@ -308,7 +300,7 @@ class CoreImpl : public ov::ICore, public std::enable_shared_from_this<ov::ICore
308300
*/
309301
void set_property_for_device(const ov::AnyMap& configMap, const std::string& deviceName);
310302

311-
void add_extension(const std::vector<ov::Extension::Ptr>& extensions, const std::string& device_name = "");
303+
void add_extension(const std::vector<ov::Extension::Ptr>& extensions);
312304

313305
bool device_supports_model_caching(const std::string& device_name) const override;
314306

0 commit comments

Comments
 (0)