From e55dcec81f82fe079c4f6019be7b7c4fe97b91d7 Mon Sep 17 00:00:00 2001 From: Bogdan Pereanu Date: Mon, 17 Nov 2025 12:48:58 +0000 Subject: [PATCH] =?UTF-8?q?[NPU]=20Use=20weak=20pointer=20to=20safely=20de?= =?UTF-8?q?stroy=20shared=20ptr=20when=C2=A0ref=20count=20is=200=20(#32828?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Details: - *Use a weak pointer to safely destroy the shared ptr when the ref count from the application is 0, and not increase it in the static method* ### Tickets: - *CVS-176148* Signed-off-by: Bogdan Pereanu --- .../intel_npu/src/backend/include/zero_backend.hpp | 3 ++- .../intel_npu/src/backend/src/zero_backend.cpp | 2 -- .../src/plugin/include/remote_context.hpp | 2 ++ .../include/intel_npu/utils/zero/zero_api.hpp | 2 +- .../include/intel_npu/utils/zero/zero_init.hpp | 2 +- .../intel_npu/src/utils/src/zero/zero_api.cpp | 14 ++++++++++++-- .../intel_npu/src/utils/src/zero/zero_init.cpp | 14 ++++++++++++-- 7 files changed, 30 insertions(+), 9 deletions(-) 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 5efccb84b95b0e..b9175a3322e2a7 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 getDevice() const override; const std::shared_ptr 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 1a40818c2ba0e8..ed973d9eb6d032 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 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 b916259caa0c64..5cf5efc1bcc179 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& 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 `[.[.]]` (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 80cfed03e7a946..819ecdfe28786b 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 @@ class ZeroApi { ~ZeroApi() = default; - static const std::shared_ptr& getInstance(); + static const std::shared_ptr 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 0a6b24a70fa4a8..06b6bc29b73fb4 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 @@ -79,7 +79,7 @@ class ZeroInitStructsHolder final { return _external_memory_fd_win32_supported; } - static const std::shared_ptr& getInstance(); + static const std::shared_ptr getInstance(); private: void initNpuDriver(); 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 cc52bfdfd32962..a33fbbe29e586c 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 + #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::getInstance() { - static std::shared_ptr instance = std::make_shared(); +const std::shared_ptr ZeroApi::getInstance() { + static std::mutex mutex; + static std::weak_ptr weak_instance; + + std::lock_guard lock(mutex); + auto instance = weak_instance.lock(); + if (!instance) { + instance = std::make_shared(); + 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 3835b7ec2a0d90..2fc977b764ee8d 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 #include +#include #include #include "intel_npu/utils/zero/zero_utils.hpp" @@ -331,8 +332,16 @@ ZeroInitStructsHolder::ZeroInitStructsHolder() } } -const std::shared_ptr& ZeroInitStructsHolder::getInstance() { - static std::shared_ptr instance = std::make_shared(); +const std::shared_ptr ZeroInitStructsHolder::getInstance() { + static std::mutex mutex; + static std::weak_ptr weak_instance; + + std::lock_guard lock(mutex); + auto instance = weak_instance.lock(); + if (!instance) { + instance = std::make_shared(); + weak_instance = instance; + } return instance; } @@ -340,6 +349,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");