Skip to content

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

[NPU] Use weak pointer to safely destroy shared ptr when ref count is 0 (#32828)

Bogdan Pereanu9b900b2dce32
diff --git a/src/plugins/intel_npu/src/backend/include/zero_backend.hpp b/src/plugins/intel_npu/src/backend/include/zero_backend.hpp
index 5efccb84b95..b9175a3322e 100644
--- a/src/plugins/intel_npu/src/backend/include/zero_backend.hpp
+++ b/src/plugins/intel_npu/src/backend/include/zero_backend.hpp
@@ -15,7 +15,8 @@ namespace intel_npu {
class ZeroEngineBackend final : public IEngineBackend {
public:
ZeroEngineBackend();
- virtual ~ZeroEngineBackend();
+ ~ZeroEngineBackend() override = default;
+
const std::shared_ptr<IDevice> getDevice() const override;
const std::shared_ptr<IDevice> getDevice(const std::string&) const override;
const std::string getName() const override {
diff --git a/src/plugins/intel_npu/src/backend/src/zero_backend.cpp b/src/plugins/intel_npu/src/backend/src/zero_backend.cpp
index 1a40818c2ba..ed973d9eb6d 100644
--- a/src/plugins/intel_npu/src/backend/src/zero_backend.cpp
+++ b/src/plugins/intel_npu/src/backend/src/zero_backend.cpp
@@ -37,8 +37,6 @@ bool ZeroEngineBackend::isLUIDExtSupported() const {
return _initStruct->isExtensionSupported(std::string(ZE_DEVICE_LUID_EXT_NAME), ZE_MAKE_VERSION(1, 0));
}
-ZeroEngineBackend::~ZeroEngineBackend() = default;
-
const std::shared_ptr<IDevice> ZeroEngineBackend::getDevice() const {
if (_devices.empty()) {
_logger.debug("ZeroEngineBackend - getDevice() returning empty list");
diff --git a/src/plugins/intel_npu/src/plugin/include/remote_context.hpp b/src/plugins/intel_npu/src/plugin/include/remote_context.hpp
index b916259caa0..5cf5efc1bcc 100644
--- a/src/plugins/intel_npu/src/plugin/include/remote_context.hpp
+++ b/src/plugins/intel_npu/src/plugin/include/remote_context.hpp
@@ -21,6 +21,8 @@ class RemoteContextImpl : public ov::IRemoteContext {
public:
RemoteContextImpl(const ov::SoPtr<IEngineBackend>& engineBackend, const ov::AnyMap& remote_properties = {});
+ ~RemoteContextImpl() override = default;
+
/**
* @brief Returns name of a device on which underlying object is allocated.
* @return A device name string in fully specified format `<device_name>[.<device_id>[.<tile_id>]]` (e.g. GPU.0.1).
diff --git a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_api.hpp b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_api.hpp
index 80cfed03e7a..819ecdfe287 100644
--- a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_api.hpp
+++ b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_api.hpp
@@ -78,7 +78,7 @@ public:
~ZeroApi() = default;
- static const std::shared_ptr<ZeroApi>& getInstance();
+ static const std::shared_ptr<ZeroApi> getInstance();
#define symbol_statement(symbol) decltype(&::symbol) symbol;
symbols_list();
diff --git a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_init.hpp b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_init.hpp
index 2438bc92198..0eb4ce1ef31 100644
--- a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_init.hpp
+++ b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_init.hpp
@@ -75,7 +75,7 @@ public:
return _external_memory_fd_win32_supported;
}
- static const std::shared_ptr<ZeroInitStructsHolder>& getInstance();
+ static const std::shared_ptr<ZeroInitStructsHolder> getInstance();
ze_device_graph_properties_t getCompilerProperties();
diff --git a/src/plugins/intel_npu/src/utils/src/zero/zero_api.cpp b/src/plugins/intel_npu/src/utils/src/zero/zero_api.cpp
index cc52bfdfd32..a33fbbe29e5 100644
--- a/src/plugins/intel_npu/src/utils/src/zero/zero_api.cpp
+++ b/src/plugins/intel_npu/src/utils/src/zero/zero_api.cpp
@@ -4,6 +4,8 @@
#include "intel_npu/utils/zero/zero_api.hpp"
+#include <mutex>
+
#include "openvino/util/file_util.hpp"
#include "openvino/util/shared_object.hpp"
@@ -49,8 +51,16 @@ ZeroApi::ZeroApi() {
#undef symbol_statement
}
-const std::shared_ptr<ZeroApi>& ZeroApi::getInstance() {
- static std::shared_ptr<ZeroApi> instance = std::make_shared<ZeroApi>();
+const std::shared_ptr<ZeroApi> ZeroApi::getInstance() {
+ static std::mutex mutex;
+ static std::weak_ptr<ZeroApi> weak_instance;
+
+ std::lock_guard<std::mutex> lock(mutex);
+ auto instance = weak_instance.lock();
+ if (!instance) {
+ instance = std::make_shared<ZeroApi>();
+ weak_instance = instance;
+ }
return instance;
}
diff --git a/src/plugins/intel_npu/src/utils/src/zero/zero_init.cpp b/src/plugins/intel_npu/src/utils/src/zero/zero_init.cpp
index f0a97ae9055..a2d92b88ec5 100644
--- a/src/plugins/intel_npu/src/utils/src/zero/zero_init.cpp
+++ b/src/plugins/intel_npu/src/utils/src/zero/zero_init.cpp
@@ -7,6 +7,7 @@
#include <ze_command_queue_npu_ext.h>
#include <ze_mem_import_system_memory_ext.h>
+#include <mutex>
#include <regex>
#ifdef _WIN32
@@ -324,8 +325,16 @@ ZeroInitStructsHolder::ZeroInitStructsHolder()
}
}
-const std::shared_ptr<ZeroInitStructsHolder>& ZeroInitStructsHolder::getInstance() {
- static std::shared_ptr<ZeroInitStructsHolder> instance = std::make_shared<ZeroInitStructsHolder>();
+const std::shared_ptr<ZeroInitStructsHolder> ZeroInitStructsHolder::getInstance() {
+ static std::mutex mutex;
+ static std::weak_ptr<ZeroInitStructsHolder> weak_instance;
+
+ std::lock_guard<std::mutex> lock(mutex);
+ auto instance = weak_instance.lock();
+ if (!instance) {
+ instance = std::make_shared<ZeroInitStructsHolder>();
+ weak_instance = instance;
+ }
return instance;
}
@@ -353,6 +362,7 @@ ZeroInitStructsHolder::~ZeroInitStructsHolder() {
if (context) {
log.debug("ZeroInitStructsHolder - performing zeContextDestroy");
auto result = zeContextDestroy(context);
+ context = nullptr;
if (result != ZE_RESULT_SUCCESS) {
if (result == ZE_RESULT_ERROR_UNINITIALIZED) {
log.warning("zeContextDestroy failed to destroy the context; Level zero context was already destroyed");