Skip to content

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

[NPU] Add support for importing standard allocations from I/O tensors (#32187)

Bogdan Pereanuc3115fc0b18e
diff --git a/src/plugins/intel_npu/src/backend/include/zero_tensor.hpp b/src/plugins/intel_npu/src/backend/include/zero_tensor.hpp
index 29238f4c62a..cfd08d801ee 100644
--- a/src/plugins/intel_npu/src/backend/include/zero_tensor.hpp
+++ b/src/plugins/intel_npu/src/backend/include/zero_tensor.hpp
@@ -9,6 +9,7 @@
#include "intel_npu/config/config.hpp"
#include "intel_npu/utils/zero/zero_init.hpp"
+#include "intel_npu/utils/zero/zero_mem.hpp"
#include "openvino/runtime/common.hpp"
#include "openvino/runtime/itensor.hpp"
#include "openvino/runtime/so_ptr.hpp"
@@ -19,7 +20,6 @@ namespace intel_npu {
* @brief ZeroTensor API holding NPU device memory
* It keeps a data pointer allocated in the same Level Zero context.
*/
-
class ZeroTensor final : public ov::ITensor {
public:
/**
@@ -29,25 +29,25 @@ public:
* @param config NPU plugin configuration
* @param type Data type of tensor elements
* @param shape Tensor shape
- * @param isInput Indicates if the tensor is used as a network input ( true) or output (false)
+ * @param is_input Indicates if the tensor is used as a network input ( true) or output (false)
*/
ZeroTensor(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
const Config& config,
const ov::element::Type element_type,
const ov::Shape& shape,
- const bool isInput);
+ const bool is_input);
/**
* @brief Creates a ZeroTensor from the given tensor. This constructor will throw if the memory of the given tensor
* is not allocated in the level zero context specified through init_structs or in case the memory cannot be
* imported in that context ( to be implemented). ZeroTensor will keep a reference to the source tensor.
* @param init_structs Shared pointer to ZeroInitStructsHolder
- * @param user_tensor Tensor to create ZeroTensor from
* @param config NPU plugin configuration
+ * @param user_tensor Tensor to create ZeroTensor from
*/
ZeroTensor(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
- const ov::SoPtr<ov::ITensor>& user_tensor,
- const Config& config);
+ const Config& config,
+ const ov::SoPtr<ov::ITensor>& user_tensor);
void* data() override;
void* data(const ov::element::Type& type) override;
@@ -69,17 +69,12 @@ public:
void prevent_reuse();
bool can_be_reused();
- ~ZeroTensor();
+ ~ZeroTensor() override = default;
private:
- static void initialize_elements(void* data, const ov::element::Type& element_type, const ov::Shape& shape);
void update_strides() const;
size_t get_capacity() const;
size_t get_bytes_capacity() const;
- void destroy_elements(size_t begin_ind, size_t end_ind);
- void destroy_memory();
- void* allocate_zero_memory(const size_t bytes, const size_t alignment) noexcept;
- void deallocate_zero_memory(void* handle) noexcept;
std::shared_ptr<ZeroInitStructsHolder> _init_structs;
Logger _logger;
@@ -91,15 +86,12 @@ private:
mutable std::once_flag _strides_once;
void* _ptr = nullptr;
bool _reset_tensor_memory = false;
- uint32_t _zero_memory_flag = 0;
+ bool _is_input = false;
bool _can_be_reused = false;
- ov::SoPtr<ov::ITensor> _imported_tensor;
-};
+ ov::SoPtr<ov::ITensor> _user_tensor;
-class ZeroTensorException : public std::runtime_error {
-public:
- explicit ZeroTensorException(const std::string& msg) : std::runtime_error(msg) {}
+ std::shared_ptr<ZeroMem> _mem_ref;
};
} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp b/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp
index 3d2df2bbf05..3b95a4f91a3 100644
--- a/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp
+++ b/src/plugins/intel_npu/src/backend/src/zero_infer_request.cpp
@@ -4,8 +4,6 @@
#include "zero_infer_request.hpp"
-#include <ze_mem_import_system_memory_ext.h>
-
#include "intel_npu/common/itt.hpp"
#include "intel_npu/config/options.hpp"
#include "intel_npu/prefix.hpp"
@@ -389,9 +387,13 @@ void ZeroInferRequest::set_tensor(const ov::Output<const ov::Node>& port, const
OV_ITT_TASK_NEXT(ZERO_SET_TENSOR, "create zero tensor");
// Try to use the user tensor directly if its underlying data is already allocated in the same Level Zero
// context.
- levelZeroTensor = std::make_shared<ZeroTensor>(_initStructs, tensor, _config);
+ levelZeroTensor = std::make_shared<ZeroTensor>(_initStructs, _config, tensor);
updateCommandListArg = true;
- } catch (const ZeroTensorException&) {
+ } catch (const ZeroMemException& exception) {
+ _logger.debug("ZeroInferRequest::set_tensor - exception caught while trying to create a Level Zero tensor "
+ "from the user tensor: %s",
+ exception.what());
+
// Check if the current Level Zero tensor was previously shared with the user. If so, it cannot be reused;
// allocate a new tensor to back up the user tensor (which cannot be imported or used directly).
if (_dynamicBatchValueChanged || levelZeroTensor == nullptr || !levelZeroTensor->can_be_reused()) {
@@ -474,13 +476,16 @@ void ZeroInferRequest::set_tensors(const ov::Output<const ov::Node>& port,
try {
_logger.debug("ZeroInferRequest::set_tensors - create zero tensor");
OV_ITT_TASK_NEXT(ZERO_SET_TENSORS, "create zero tensor");
-
get_level_zero_input(foundPort.idx, i) =
- std::make_shared<ZeroTensor>(_initStructs, tensors.at(i), _config);
- } catch (const ZeroTensorException&) {
+ std::make_shared<ZeroTensor>(_initStructs, _config, tensors.at(i));
+ } catch (const ZeroMemException& exception) {
+ _logger.debug(
+ "ZeroInferRequest::set_tensors - exception caught while trying to create a Level Zero tensor "
+ "from the user tensor: %s",
+ exception.what());
+
_logger.debug("ZeroInferRequest::set_tensors - allocate locally L0 tensor");
OV_ITT_TASK_NEXT(ZERO_SET_TENSORS, "allocate tensor");
-
get_level_zero_input(foundPort.idx, i) = allocate_tensor(foundPort.idx, INPUT, batchSizeCandidate);
}
diff --git a/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp b/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp
index 3f58c55b2b6..e0ec467af5d 100644
--- a/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp
+++ b/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp
@@ -7,15 +7,12 @@
#include "intel_npu/config/options.hpp"
#include "intel_npu/utils/utils.hpp"
#include "intel_npu/utils/zero/zero_api.hpp"
-#include "intel_npu/utils/zero/zero_host_tensor.hpp"
+#include "intel_npu/utils/zero/zero_mem_pool.hpp"
#include "intel_npu/utils/zero/zero_remote_tensor.hpp"
-#include "intel_npu/utils/zero/zero_utils.hpp"
#include "openvino/core/memory_util.hpp"
#include "openvino/runtime/properties.hpp"
#include "openvino/runtime/tensor.hpp"
-namespace intel_npu {
-
namespace {
bool is_pointer_representable(const ov::element::Type& tensor_type, const ov::element::Type& type) {
if (type == ov::element::dynamic) {
@@ -28,34 +25,41 @@ bool is_pointer_representable(const ov::element::Type& tensor_type, const ov::el
}
} // namespace
+namespace intel_npu {
+
ZeroTensor::ZeroTensor(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
const Config& config,
const ov::element::Type element_type,
const ov::Shape& shape,
- const bool isInput)
+ const bool is_input)
: _init_structs(init_structs),
_logger("ZeroTensor", config.get<LOG_LEVEL>()),
_element_type{element_type},
_shape{shape},
_capacity{_shape},
_strides{},
- _strides_once{} {
+ _strides_once{},
+ _is_input(is_input) {
OPENVINO_ASSERT(_element_type.is_static());
- if (isInput) {
- _zero_memory_flag = ZE_HOST_MEM_ALLOC_FLAG_BIAS_WRITE_COMBINED;
- }
const auto byte_size = ov::util::get_memory_size_safe(element_type, _shape);
- OPENVINO_ASSERT(byte_size, "Cannot allocate memory for type: ", element_type, " and shape: ", _shape);
- auto data = allocate_zero_memory(*byte_size, utils::STANDARD_PAGE_SIZE);
- OPENVINO_ASSERT(*byte_size == 0 || data != nullptr, "Failed to allocate zero memory");
- initialize_elements(data, element_type, _shape);
+ OPENVINO_ASSERT(byte_size || byte_size.value() == 0,
+ "Cannot allocate memory for type: ",
+ element_type,
+ " and shape: ",
+ _shape);
+ _mem_ref = ZeroMemPool::get_instance().allocate_zero_memory(_init_structs,
+ byte_size.value(),
+ utils::STANDARD_PAGE_SIZE,
+ _is_input);
+ auto data = _mem_ref->data();
+ OPENVINO_ASSERT(data != nullptr, "Failed to allocate zero memory");
_ptr = data;
_can_be_reused = true;
}
ZeroTensor::ZeroTensor(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
- const ov::SoPtr<ov::ITensor>& user_tensor,
- const Config& config)
+ const Config& config,
+ const ov::SoPtr<ov::ITensor>& user_tensor)
: _init_structs(init_structs),
_logger("ZeroTensor", config.get<LOG_LEVEL>()),
_element_type{user_tensor->get_element_type()},
@@ -63,39 +67,26 @@ ZeroTensor::ZeroTensor(const std::shared_ptr<ZeroInitStructsHolder>& init_struct
_capacity{_shape},
_strides{_element_type.bitwidth() >= 8 ? user_tensor->get_strides() : ov::Strides{}},
_strides_once{},
- _imported_tensor(user_tensor) {
+ _user_tensor(user_tensor) {
OPENVINO_ASSERT(_element_type.is_static());
// Data pointer of the given user_tensor must be a valid address in the level zero context
// Check first if the given tensor is a ZeroRemoteTensor (which has a different method to expose the internal
- // storage) or ZeroHostTensor (it is a wrapper over ZeroRemoteTensor)
- auto remote_tensor = std::dynamic_pointer_cast<ZeroRemoteTensor>(_imported_tensor._ptr);
- auto host_tensor = std::dynamic_pointer_cast<ZeroHostTensor>(_imported_tensor._ptr);
- if (remote_tensor == nullptr && host_tensor == nullptr) {
- // case for regular user tensors and ZeroTensors
- if (zeroUtils::memory_was_allocated_in_the_same_l0_context(_init_structs->getContext(),
- _imported_tensor->data())) {
- _logger.debug("ZeroTensor::ZeroTensor - tensor was created in the same L0 context");
- _ptr = _imported_tensor->data();
- } else {
- _logger.debug("ZeroTensor::ZeroTensor - tensor was not created in the same L0 context");
- throw ZeroTensorException("Tensor was not created in the same zero context");
- }
+ // storage)
+ auto remote_tensor = std::dynamic_pointer_cast<ZeroRemoteTensor>(_user_tensor._ptr);
+ if (remote_tensor == nullptr) {
+ _ptr = _user_tensor->data();
} else {
- // case for ZeroRemoteTensors and ZeroHostTensors
- if (host_tensor != nullptr) {
- remote_tensor = host_tensor->get_impl();
- }
-
- if (zeroUtils::memory_was_allocated_in_the_same_l0_context(_init_structs->getContext(),
- remote_tensor->get_original_memory())) {
- _logger.debug("ZeroTensor::ZeroTensor - remote tensor was created in the same L0 context");
- _ptr = remote_tensor->get_original_memory();
- } else {
- _logger.debug("ZeroTensor::ZeroTensor - remote tensor was not created in the same L0 context");
- throw ZeroTensorException("Tensor was not created in the same zero context");
- }
+ _ptr = remote_tensor->get_original_memory();
}
+
+ // Check if [data, data + size] was previously imported or allocated in the current level zero context. In such case
+ // _mem_ref will keep a reference to that allocation. Otherwise the function will try to import it into the level
+ // zero context.
+ _logger.debug("ZeroTensor::ZeroTensor - get tensor from pool or import it");
+ _mem_ref = ZeroMemPool::get_instance().import_standard_allocation_memory(_init_structs,
+ _ptr,
+ _user_tensor->get_byte_size());
}
// Note: Override data() members to not used OpenVINO library code to improve performance
@@ -158,14 +149,6 @@ const ov::Strides& ZeroTensor::get_strides() const {
return _strides;
}
-void ZeroTensor::initialize_elements(void* data, const ov::element::Type& element_type, const ov::Shape& shape) {
- if (element_type == ov::element::Type_t::string) {
- auto num_elements = shape_size(shape);
- auto string_ptr = static_cast<std::string*>(data);
- std::uninitialized_fill_n(string_ptr, num_elements, std::string());
- }
-}
-
size_t ZeroTensor::get_capacity() const {
return shape_size(_capacity);
}
@@ -174,25 +157,6 @@ size_t ZeroTensor::get_bytes_capacity() const {
return ov::util::get_memory_size(get_element_type(), get_capacity());
}
-void ZeroTensor::destroy_elements(size_t begin_ind, size_t end_ind) {
- // it removes elements from tail
- if (get_element_type() == ov::element::Type_t::string) {
- auto strings = static_cast<std::string*>(_ptr);
- for (size_t ind = begin_ind; ind < end_ind; ++ind) {
- using std::string;
- strings[ind].~string();
- }
- }
-}
-
-void ZeroTensor::destroy_memory() {
- if (_imported_tensor == nullptr) {
- destroy_elements(0, get_capacity());
- deallocate_zero_memory(_ptr);
- _ptr = nullptr;
- }
-}
-
void ZeroTensor::set_shape(ov::Shape new_shape) {
if (_shape == new_shape) {
return;
@@ -201,22 +165,24 @@ void ZeroTensor::set_shape(ov::Shape new_shape) {
_shape = std::move(new_shape);
if (get_size() > get_capacity()) {
- if (_init_structs->getMutableCommandListExtVersion() < ZE_MAKE_VERSION(1, 0)) {
- OPENVINO_THROW("Re-shaping the tensor with a larger shape is not available using this driver version. "
- "Please update the driver to the latest version.");
- }
+ OPENVINO_ASSERT(_init_structs->getMutableCommandListExtVersion() >= ZE_MAKE_VERSION(1, 0),
+ "Re-shaping the tensor with a larger shape is not available using this driver version. "
+ "Please update the driver to the latest version.");
- if (_imported_tensor != nullptr) {
- OPENVINO_THROW("set_shape is not supported. Tensor re-allocation is not allowed for imported tensors.");
- }
+ OPENVINO_ASSERT(_user_tensor == nullptr,
+ "set_shape is not supported. Tensor re-allocation is not allowed for imported tensors.");
- destroy_memory();
+ _mem_ref.reset();
+ _ptr = nullptr;
// allocate buffer and initialize objects from scratch
_capacity = _shape;
- _ptr = allocate_zero_memory(get_bytes_capacity(), utils::STANDARD_PAGE_SIZE);
+ _mem_ref = ZeroMemPool::get_instance().allocate_zero_memory(_init_structs,
+ get_bytes_capacity(),
+ utils::STANDARD_PAGE_SIZE,
+ _is_input);
+ _ptr = _mem_ref->data();
OPENVINO_ASSERT(get_bytes_capacity() == 0 || _ptr != nullptr, "Failed to allocate zero memory");
- initialize_elements(_ptr, _element_type, _shape);
_reset_tensor_memory = true;
}
@@ -241,42 +207,4 @@ bool ZeroTensor::can_be_reused() {
return _can_be_reused;
}
-void* ZeroTensor::allocate_zero_memory(const size_t bytes, const size_t alignment) noexcept {
- size_t size = bytes + alignment - (bytes % alignment);
-
- ze_host_mem_alloc_desc_t desc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC, nullptr, _zero_memory_flag};
- void* data = nullptr;
- auto result = zeMemAllocHost(_init_structs->getContext(), &desc, size, alignment, &data);
-
- if (result == ZE_RESULT_SUCCESS) {
- return data;
- } else {
- _logger.error("L0 zeMemAllocHost result: %s, code %#X - %s",
- ze_result_to_string(result).c_str(),
- uint64_t(result),
- ze_result_to_description(result).c_str());
- return nullptr;
- }
-}
-
-void ZeroTensor::deallocate_zero_memory(void* handle) noexcept {
- auto result = zeMemFree(_init_structs->getContext(), handle);
- if (ZE_RESULT_SUCCESS != result) {
- _logger.error("L0 zeMemFree result: %s, code %#X - %s",
- ze_result_to_string(result).c_str(),
- uint64_t(result),
- ze_result_to_description(result).c_str());
- }
-}
-
-ZeroTensor::~ZeroTensor() {
- try {
- destroy_memory();
- } catch (const std::exception& ex) {
- _logger.error("Failed to destroy Zero Tensor: %s", ex.what());
- } catch (...) {
- _logger.error("Unexpected error when Zero Tensor is destroyed");
- }
-}
-
} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/backend/src/zero_variable_state.cpp b/src/plugins/intel_npu/src/backend/src/zero_variable_state.cpp
index bd186bbf54d..bb4462ba305 100644
--- a/src/plugins/intel_npu/src/backend/src/zero_variable_state.cpp
+++ b/src/plugins/intel_npu/src/backend/src/zero_variable_state.cpp
@@ -42,9 +42,9 @@ void ZeroVariableState::set_state(const ov::SoPtr<ov::ITensor>& new_state) {
_logger.debug("ZeroVariableState::set_state - create zero tensor");
// Try to use the user tensor directly if its underlying data is already allocated in the same Level Zero
// context.
- _zero_state = std::make_shared<ZeroTensor>(_init_structs, m_state, _config);
+ _zero_state = std::make_shared<ZeroTensor>(_init_structs, _config, m_state);
_is_zero_state_update_needed = true;
- } catch (const ZeroTensorException&) {
+ } catch (const ZeroMemException&) {
// Check if the current Level Zero tensor was previously shared with the user. If so, it cannot be reused;
// allocate a new tensor to back up the user tensor (which cannot be imported or used directly).
if (_zero_state == nullptr || !_zero_state->can_be_reused()) {
diff --git a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
index e97b2ac5439..3947c486685 100644
--- a/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
+++ b/src/plugins/intel_npu/src/plugin/npuw/base_sync_infer_request.cpp
@@ -204,9 +204,9 @@ void ov::npuw::IBaseInferRequest::handle_set_remote_input(const ov::Output<const
auto remote_ctx =
m_npuw_model->get_plugin()->get_core()->get_default_context(m_npuw_model->global_mem_device())._ptr;
auto zrh = remote_ctx->get_property().at(ov::intel_npu::l0_context.name());
- if (::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(
+ if (::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(
static_cast<ze_context_handle_t>(zrh.as<void*>()),
- tensor->data())) {
+ tensor->data()) > 0) {
if (tensor->is_continuous()) {
m_input_allocated.insert(tensor->data());
} else {
diff --git a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/utils.hpp b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/utils.hpp
index 01cc49fc38c..40759290dbb 100644
--- a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/utils.hpp
+++ b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/utils.hpp
@@ -37,7 +37,7 @@ private:
const size_t _align_size;
};
-static inline bool memory_and_size_aligned_to_standard_page_size(void* addr, size_t size) {
+static inline bool memory_and_size_aligned_to_standard_page_size(const void* addr, size_t size) {
auto addr_int = reinterpret_cast<uintptr_t>(addr);
// addr is aligned to standard page size
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 bdc2206f388..0a6b24a70fa 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
@@ -72,6 +72,12 @@ public:
}
return false;
}
+ inline bool isExternalMemoryStandardAllocationSupported() const {
+ return _external_memory_standard_allocation_supported;
+ }
+ inline bool isExternalMemoryFdWin32Supported() const {
+ return _external_memory_fd_win32_supported;
+ }
static const std::shared_ptr<ZeroInitStructsHolder>& getInstance();
@@ -99,6 +105,9 @@ private:
ze_api_version_t ze_drv_api_version = {};
ze_device_graph_properties_t compiler_properties = {};
+
+ bool _external_memory_standard_allocation_supported = false;
+ bool _external_memory_fd_win32_supported = false;
};
} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_mem.hpp b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_mem.hpp
new file mode 100644
index 00000000000..56bd2eaefea
--- /dev/null
+++ b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_mem.hpp
@@ -0,0 +1,77 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "intel_npu/utils/logger/logger.hpp"
+#include "intel_npu/utils/zero/zero_init.hpp"
+
+namespace intel_npu {
+
+class ZeroMem final {
+public:
+ /**
+ * @brief Allocates a new memory region in the level zero context provided through init_structs.
+ * @param init_structs Holder for the level zero structures.
+ * @param bytes Size in bytes of the memory that must be allocated.
+ * @param alignment Alignment needed for the memory; it must be a multiple of the standard page size
+ * @param is_input Optimize reads from this buffer. Specific level zero flags will be used for allocation in case
+ * the buffer is intended to be used as an input.
+ */
+ ZeroMem(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
+ const size_t bytes,
+ const size_t alignment,
+ const bool is_input);
+
+ /**
+ * @brief Imports an already allocated memory in the level zero context provided through init_structs.
+ * @param init_structs Holder for the level zero structures.
+ * @param data Memory to be imported
+ * @param bytes Size in bytes of the memory that must be allocated.
+ * @param is_input Optimize reads from this buffer. Specific level zero flags will be used for allocation in case
+ * the buffer is intended to be used as an input.
+ * @param standard_allocation If a CPU standard allocation is shared it must be set to true. Otherwise it will try
+ * to import DMA-BUF (on Linux) or WIN32 (on Windows) memory.
+ */
+ ZeroMem(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
+ const void* data,
+ const size_t bytes,
+ const bool is_input,
+ const bool standard_allocation);
+
+ /**
+ * @brief Return allocated memory
+ */
+ void* data();
+
+ /**
+ * @brief Return size of the allocated memory
+ */
+ size_t size();
+
+ /**
+ * @brief Return memory id of the allocated memory
+ */
+ uint64_t id();
+
+ ~ZeroMem();
+
+private:
+ std::shared_ptr<ZeroInitStructsHolder> _init_structs;
+ Logger _logger;
+
+ void* _ptr = nullptr;
+ size_t _size = 0;
+ uint64_t _id = 0;
+};
+
+/**
+ * @brief Default Zero Memory exception for the cases when we can not import a memory from the given tensor and must
+ * fallback on allocating a new zero memory and do memcpy
+ */
+class ZeroMemException final : public std::runtime_error {
+public:
+ explicit ZeroMemException(const std::string& msg) : std::runtime_error(msg) {}
+};
+} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_mem_pool.hpp b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_mem_pool.hpp
new file mode 100644
index 00000000000..22b3adbddf6
--- /dev/null
+++ b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_mem_pool.hpp
@@ -0,0 +1,91 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <future>
+#include <mutex>
+
+#include "intel_npu/utils/logger/logger.hpp"
+#include "intel_npu/utils/zero/zero_init.hpp"
+#include "intel_npu/utils/zero/zero_mem.hpp"
+
+namespace intel_npu {
+
+/**
+ * @brief ZeroMemPool API used to keep track of all allocations and memory imports performed in the level zero context.
+ * @details Provides methods to either allocate or import memory in the level zero context. Such method will also add an
+ * entry into the pool for the tracking ID that corresponds to that allocation. Provides a method to check if a given
+ * memory range was previously allocated or imported.
+ */
+class ZeroMemPool final {
+public:
+ ZeroMemPool();
+ ZeroMemPool(const ZeroMemPool& other) = delete;
+ ZeroMemPool(ZeroMemPool&& other) = delete;
+ void operator=(const ZeroMemPool&) = delete;
+ void operator=(ZeroMemPool&&) = delete;
+
+ /**
+ * @brief Get static instance.
+ */
+ static ZeroMemPool& get_instance();
+
+ /**
+ * @brief Returns a new memory region allocated in the level zero context and adds it to the pool.
+ * @param init_structs Holder for the level zero structures.
+ * @param bytes Size in bytes of the memory that must be allocated.
+ * @param alignment Alignment needed for the memory; it must be a multiple of the standard page size.
+ * @param is_input Memory is used only as input or not.
+ */
+ std::shared_ptr<ZeroMem> allocate_zero_memory(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
+ const size_t bytes,
+ const size_t alignment,
+ const bool is_input = false);
+
+ /**
+ * @brief Returns an imported shared(CMX-DMA in case of Linux, NT handle in case of Windows) memory in the level
+ * zero and adds it to the pool.
+ * @param init_structs Holder for the level zero structures.
+ * @param data Memory to be imported.
+ * @param bytes Size in bytes of the memory that must be allocated.
+ */
+ std::shared_ptr<ZeroMem> import_shared_memory(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
+ const void* data,
+ const size_t bytes);
+
+ /**
+ * @brief Performs a look-up in the pool to check if the entire range given by [data, data + bytes] was previously
+ * allocated or imported. Returns a reference to the ZeroMem object stored in the pool in case the check is
+ * successful, or tries to import memory in the level zero context, adds it to the pool, and returns it.
+ * @param init_structs Holder for the level zero structures.
+ * @param data User memory to be checked.
+ * @param bytes Size in bytes of the memory.
+ * @param is_input Memory is used only as input or not.
+ */
+ std::shared_ptr<ZeroMem> import_standard_allocation_memory(
+ const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
+ const void* data,
+ const size_t bytes,
+ const bool is_input = false);
+
+private:
+ std::shared_ptr<ZeroMem> import_standard_allocation(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
+ const void* data,
+ const size_t bytes,
+ const bool is_input);
+ void update_pool(const std::shared_ptr<intel_npu::ZeroMem>& zero_memory);
+ void delete_pool_entry(ZeroMem* ptr);
+
+ std::unordered_map<uint64_t, std::weak_ptr<ZeroMem>> _pool;
+ // pool to synchronize shared_ptr<ZeroMem> deleter and import_standard_allocation_memory method
+ std::unordered_map<uint64_t, std::promise<void>> _notify_pool;
+
+ // mutex to synchronize memory pool
+ std::mutex _mutex;
+ // mutex to synchronize shared_ptr<ZeroMem> deleter and import_standard_allocation_memory method
+ std::mutex _deleter_mutex;
+};
+
+} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_remote_tensor.hpp b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_remote_tensor.hpp
index 5a5ee585e6b..569bb4b0313 100644
--- a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_remote_tensor.hpp
+++ b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_remote_tensor.hpp
@@ -11,6 +11,7 @@
#include "intel_npu/utils/logger/logger.hpp"
#include "intel_npu/utils/zero/zero_init.hpp"
+#include "intel_npu/utils/zero/zero_mem.hpp"
#include "openvino/runtime/intel_npu/remote_properties.hpp"
#include "openvino/runtime/iremote_context.hpp"
#include "openvino/runtime/iremote_tensor.hpp"
@@ -70,15 +71,14 @@ public:
void* get_original_memory() const;
ze_context_handle_t get_zero_context_handle() const;
- ~ZeroRemoteTensor() override;
+ ~ZeroRemoteTensor() override = default;
private:
void allocate(const size_t bytes);
- bool deallocate() noexcept;
bool is_allocated() const noexcept;
void update_strides();
void update_properties();
- void copy_file_data_to_level_zero_memory();
+ void copy_file_data_to_level_zero_memory(const size_t size_to_read);
std::shared_ptr<ov::IRemoteContext> _context;
std::shared_ptr<ZeroInitStructsHolder> _init_structs;
@@ -97,10 +97,8 @@ private:
const void* _mem = nullptr;
void* _data = nullptr;
- bool _external_memory_support = false;
- bool _mmaped_file_support = false;
-
ov::Tensor _mmap_tensor;
+ std::shared_ptr<ZeroMem> _host_memory;
};
inline bool is_remote_tensor(const std::shared_ptr<ov::ITensor>& tensor) {
diff --git a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_utils.hpp b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_utils.hpp
index 5931761e1ea..427f9cd02f0 100644
--- a/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_utils.hpp
+++ b/src/plugins/intel_npu/src/utils/include/intel_npu/utils/zero/zero_utils.hpp
@@ -220,20 +220,17 @@ static inline std::string getLatestBuildError(ze_graph_dditable_ext_curr_t& _gra
}
}
-static inline bool memory_was_allocated_in_the_same_l0_context(ze_context_handle_t hContext, const void* ptr) {
+static inline uint64_t get_l0_context_memory_allocation_id(ze_context_handle_t handle, const void* ptr) {
ze_memory_allocation_properties_t desc = {};
desc.stype = ZE_STRUCTURE_TYPE_MEMORY_ALLOCATION_PROPERTIES;
- auto res = intel_npu::zeMemGetAllocProperties(hContext, ptr, &desc, nullptr);
- if (res == ZE_RESULT_SUCCESS) {
- if (desc.id) {
- if ((desc.type == ZE_MEMORY_TYPE_HOST) || (desc.type == ZE_MEMORY_TYPE_DEVICE) ||
- (desc.type == ZE_MEMORY_TYPE_SHARED)) {
- return true;
- }
- }
+ auto res = intel_npu::zeMemGetAllocProperties(handle, ptr, &desc, nullptr);
+ if (res == ZE_RESULT_SUCCESS && desc.id > 0 &&
+ ((desc.type == ZE_MEMORY_TYPE_HOST) || (desc.type == ZE_MEMORY_TYPE_DEVICE) ||
+ (desc.type == ZE_MEMORY_TYPE_SHARED))) {
+ return desc.id;
}
- return false;
+ return 0;
}
} // namespace zeroUtils
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 5414d0718ec..3835b7ec2a0 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
@@ -5,6 +5,7 @@
#include "intel_npu/utils/zero/zero_init.hpp"
#include <ze_command_queue_npu_ext.h>
+#include <ze_mem_import_system_memory_ext.h>
#include <regex>
@@ -307,6 +308,27 @@ ZeroInitStructsHolder::ZeroInitStructsHolder()
compiler_properties.stype = ZE_STRUCTURE_TYPE_DEVICE_GRAPH_PROPERTIES;
auto result = graph_dditable_ext_decorator->pfnDeviceGetGraphProperties(device_handle, &compiler_properties);
THROW_ON_FAIL_FOR_LEVELZERO("pfnDeviceGetGraphProperties", result);
+
+ // Discover if standard allocation is supported
+ ze_device_external_memory_properties_t external_memory_properties_desc = {};
+ external_memory_properties_desc.stype = ZE_STRUCTURE_TYPE_DEVICE_EXTERNAL_MEMORY_PROPERTIES;
+ auto res = zeDeviceGetExternalMemoryProperties(device_handle, &external_memory_properties_desc);
+ if (res == ZE_RESULT_SUCCESS) {
+#ifdef _WIN32
+ if (external_memory_properties_desc.memoryAllocationImportTypes & ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32) {
+ _external_memory_fd_win32_supported = true;
+ }
+#else
+ if (external_memory_properties_desc.memoryAllocationImportTypes & ZE_EXTERNAL_MEMORY_TYPE_FLAG_DMA_BUF) {
+ _external_memory_fd_win32_supported = true;
+ }
+#endif
+
+ if (external_memory_properties_desc.memoryAllocationImportTypes &
+ ZE_EXTERNAL_MEMORY_TYPE_FLAG_STANDARD_ALLOCATION) {
+ _external_memory_standard_allocation_supported = true;
+ }
+ }
}
const std::shared_ptr<ZeroInitStructsHolder>& ZeroInitStructsHolder::getInstance() {
diff --git a/src/plugins/intel_npu/src/utils/src/zero/zero_mem.cpp b/src/plugins/intel_npu/src/utils/src/zero/zero_mem.cpp
new file mode 100644
index 00000000000..f39df59b437
--- /dev/null
+++ b/src/plugins/intel_npu/src/utils/src/zero/zero_mem.cpp
@@ -0,0 +1,141 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "intel_npu/utils/zero/zero_mem.hpp"
+
+#include <ze_mem_import_system_memory_ext.h>
+
+#include "intel_npu/utils/utils.hpp"
+#include "intel_npu/utils/zero/zero_api.hpp"
+#include "intel_npu/utils/zero/zero_utils.hpp"
+
+namespace intel_npu {
+
+ZeroMem::ZeroMem(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
+ const size_t bytes,
+ const size_t alignment,
+ const bool is_input)
+ : _init_structs(init_structs),
+ _logger("ZeHostMem", Logger::global().level()),
+ _size((bytes + alignment - 1) & ~(alignment - 1)) {
+ uint32_t zero_memory_flag = 0;
+ if (is_input) {
+ zero_memory_flag = ZE_HOST_MEM_ALLOC_FLAG_BIAS_WRITE_COMBINED;
+ }
+ ze_host_mem_alloc_desc_t desc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC, nullptr, zero_memory_flag};
+ THROW_ON_FAIL_FOR_LEVELZERO("zeMemAllocHost",
+ zeMemAllocHost(_init_structs->getContext(), &desc, _size, alignment, &_ptr));
+
+ _id = zeroUtils::get_l0_context_memory_allocation_id(_init_structs->getContext(), _ptr);
+ OPENVINO_ASSERT(_id != 0, "Failed to get memory allocation id of the allocated memory");
+}
+
+ZeroMem::ZeroMem(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
+ const void* data,
+ const size_t bytes,
+ const bool is_input,
+ const bool standard_allocation)
+ : _init_structs(init_structs),
+ _logger("ZeHostMem", Logger::global().level()),
+ _size(bytes) {
+ if (standard_allocation) {
+ if (!_init_structs->isExternalMemoryStandardAllocationSupported()) {
+ throw ZeroMemException("Importing standard allocation is not supported with this driver version");
+ }
+
+ if (!utils::memory_and_size_aligned_to_standard_page_size(data, _size)) {
+ throw ZeroMemException(
+ "Importing standard allocation is not supported if memory is not aligned to standard page size");
+ }
+
+ // We need to check if the end of the current region is part of a previous imported region
+ // The other cases are handled by the driver
+ if (zeroUtils::get_l0_context_memory_allocation_id(
+ _init_structs->getContext(),
+ static_cast<void*>(static_cast<uint8_t*>(const_cast<void*>(data)) + _size)) > 0) {
+ throw ZeroMemException("Can not import a memory which is part of an existing allocation");
+ }
+
+ uint32_t zero_memory_flag = 0;
+ if (is_input) {
+ zero_memory_flag = ZE_HOST_MEM_ALLOC_FLAG_BIAS_WRITE_COMBINED;
+ }
+ _ze_external_memory_import_system_memory_t memory_import = {
+ ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_SYSTEM_MEMORY,
+ nullptr,
+ const_cast<void*>(data),
+ _size};
+ ze_host_mem_alloc_desc_t desc = {ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC, &memory_import, zero_memory_flag};
+ auto result = zeMemAllocHost(_init_structs->getContext(), &desc, _size, utils::STANDARD_PAGE_SIZE, &_ptr);
+
+ if (result != ZE_RESULT_SUCCESS) {
+ _logger.info("Importing memory through zeMemAllocHost failed, result: %s, code %#X - %s",
+ ze_result_to_string(result).c_str(),
+ uint64_t(result),
+ ze_result_to_description(result).c_str());
+
+ throw ZeroMemException("Importing memory failed");
+ }
+ } else {
+ OPENVINO_ASSERT(_init_structs->isExternalMemoryFdWin32Supported(),
+ "Remote tensor functionality is not supported with this driver version");
+
+ OPENVINO_ASSERT(data != nullptr, "Data pointer for importing memory can't be null");
+#ifdef _WIN32
+ // in the case of the Windows platform memory is locked by the D3D12 memory management - using
+ // zeMemAllocDevice to import memory
+ ze_external_memory_import_win32_handle_t memory_import = {ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_WIN32,
+ nullptr,
+ ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32,
+ const_cast<void*>(data),
+ nullptr};
+ ze_device_mem_alloc_desc_t desc = {ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC, &memory_import, 0, 0};
+ THROW_ON_FAIL_FOR_LEVELZERO("zeMemAllocDevice",
+ zeMemAllocDevice(_init_structs->getContext(),
+ &desc,
+ _size,
+ utils::STANDARD_PAGE_SIZE,
+ _init_structs->getDevice(),
+ &_ptr));
+#else
+ // in the case of Linux platforms memory could be changed after allocation - using zeMemAllocHost for
+ // importing memory
+ ze_external_memory_import_fd_t memory_import = {ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_FD,
+ nullptr,
+ ZE_EXTERNAL_MEMORY_TYPE_FLAG_DMA_BUF,
+ static_cast<int>(reinterpret_cast<intptr_t>(data))};
+ ze_host_mem_alloc_desc_t desc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC, &memory_import, 0};
+ THROW_ON_FAIL_FOR_LEVELZERO(
+ "zeMemAllocHost",
+ zeMemAllocHost(_init_structs->getContext(), &desc, _size, utils::STANDARD_PAGE_SIZE, &_ptr));
+#endif
+ }
+
+ _id = zeroUtils::get_l0_context_memory_allocation_id(_init_structs->getContext(), _ptr);
+ OPENVINO_ASSERT(_id != 0, "Failed to get memory allocation id of the imported memory");
+}
+
+void* ZeroMem::data() {
+ return _ptr;
+}
+
+size_t ZeroMem::size() {
+ return _size;
+}
+
+uint64_t ZeroMem::id() {
+ return _id;
+}
+
+ZeroMem::~ZeroMem() {
+ auto result = zeMemFree(_init_structs->getContext(), _ptr);
+ if (ZE_RESULT_SUCCESS != result) {
+ _logger.error("L0 zeMemFree result: %s, code %#X - %s",
+ ze_result_to_string(result).c_str(),
+ uint64_t(result),
+ ze_result_to_description(result).c_str());
+ }
+}
+
+} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/utils/src/zero/zero_mem_pool.cpp b/src/plugins/intel_npu/src/utils/src/zero/zero_mem_pool.cpp
new file mode 100644
index 00000000000..ee1a4cf6344
--- /dev/null
+++ b/src/plugins/intel_npu/src/utils/src/zero/zero_mem_pool.cpp
@@ -0,0 +1,130 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "intel_npu/utils/zero/zero_mem_pool.hpp"
+
+#include "intel_npu/utils/utils.hpp"
+#include "intel_npu/utils/zero/zero_utils.hpp"
+
+namespace intel_npu {
+
+ZeroMemPool::ZeroMemPool() {}
+
+ZeroMemPool& ZeroMemPool::get_instance() {
+ static ZeroMemPool instance;
+ return instance;
+}
+
+std::shared_ptr<ZeroMem> ZeroMemPool::allocate_zero_memory(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
+ const size_t bytes,
+ const size_t alignment,
+ const bool is_input) {
+ auto zero_memory =
+ std::shared_ptr<ZeroMem>(new ZeroMem(init_structs, bytes, alignment, is_input), [this](ZeroMem* ptr) {
+ delete_pool_entry(ptr);
+ });
+
+ std::lock_guard<std::mutex> lock(_mutex);
+ update_pool(zero_memory);
+
+ return zero_memory;
+}
+
+std::shared_ptr<ZeroMem> ZeroMemPool::import_shared_memory(const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
+ const void* data,
+ const size_t bytes) {
+ auto zero_memory =
+ std::shared_ptr<ZeroMem>(new ZeroMem(init_structs, data, bytes, false, false), [this](ZeroMem* ptr) {
+ delete_pool_entry(ptr);
+ });
+
+ std::lock_guard<std::mutex> lock(_mutex);
+ update_pool(zero_memory);
+
+ return zero_memory;
+}
+
+std::shared_ptr<ZeroMem> ZeroMemPool::import_standard_allocation_memory(
+ const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
+ const void* data,
+ const size_t bytes,
+ const bool is_input) {
+ std::lock_guard<std::mutex> lock(_mutex);
+ std::unique_lock<std::mutex> deleter_lock(_deleter_mutex);
+
+ auto memory_id = zeroUtils::get_l0_context_memory_allocation_id(init_structs->getContext(), data);
+ if (memory_id == 0) {
+ // try to import memory if it isn't part of the same zero context
+ return import_standard_allocation(init_structs, data, bytes, is_input);
+ }
+
+ if (_pool.find(memory_id) != _pool.end()) {
+ // found one weak pointer in the pool; check it if it's valid
+ auto obj = _pool.at(memory_id).lock();
+ if (obj) {
+ auto user_addr_end = static_cast<uint8_t*>(const_cast<void*>(data)) + bytes;
+ auto host_addr_end = static_cast<uint8_t*>(obj->data()) + obj->size();
+ if (user_addr_end > host_addr_end) {
+ throw ZeroMemException("Tensor memory range is out of bounds of the allocated host memory");
+ }
+
+ return obj;
+ } else {
+ // shared_ptr counter is 0, we can not lock memory; wait until the deleter frees the memory
+ std::shared_future<void> done_future = _notify_pool[memory_id].get_future().share();
+ // allow (any) deleter to be executed. it will remove the pool entry and deallocate the level zero memory
+ deleter_lock.unlock();
+ // waiting for the corresponding deleter to be executed
+ done_future.wait();
+ deleter_lock.lock();
+ _notify_pool.erase(memory_id);
+
+ // import again memory after make sure it is destroyed
+ return import_standard_allocation(init_structs, data, bytes, is_input);
+ }
+ }
+
+ throw ZeroMemException("Unexpected error");
+}
+
+std::shared_ptr<ZeroMem> ZeroMemPool::import_standard_allocation(
+ const std::shared_ptr<ZeroInitStructsHolder>& init_structs,
+ const void* data,
+ const size_t bytes,
+ const bool is_input) {
+ auto zero_memory =
+ std::shared_ptr<ZeroMem>(new ZeroMem(init_structs, data, bytes, is_input, true), [this](ZeroMem* ptr) {
+ auto memory_id = ptr->id();
+ delete_pool_entry(ptr);
+ if (_notify_pool.find(memory_id) != _notify_pool.end()) {
+ _notify_pool.at(memory_id).set_value();
+ }
+ });
+
+ update_pool(zero_memory);
+
+ return zero_memory;
+}
+
+void ZeroMemPool::update_pool(const std::shared_ptr<intel_npu::ZeroMem>& zero_memory) {
+ auto pair = std::make_pair(zero_memory->id(), zero_memory);
+
+#ifdef NPU_PLUGIN_DEVELOPER_BUILD
+ if (_pool.find(zero_memory->id()) != _pool.end()) {
+ if (_pool.at(zero_memory->id()).lock()) {
+ OPENVINO_THROW("Memory exists, at this point id shall not be used!");
+ }
+ }
+#endif
+
+ _pool.emplace(pair);
+}
+
+void ZeroMemPool::delete_pool_entry(ZeroMem* zero_memory) {
+ std::unique_lock lock(_deleter_mutex);
+ _pool.erase(zero_memory->id());
+ delete zero_memory;
+}
+
+} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/utils/src/zero/zero_remote_tensor.cpp b/src/plugins/intel_npu/src/utils/src/zero/zero_remote_tensor.cpp
index 6d5e16d8380..8be2f3ed6b7 100644
--- a/src/plugins/intel_npu/src/utils/src/zero/zero_remote_tensor.cpp
+++ b/src/plugins/intel_npu/src/utils/src/zero/zero_remote_tensor.cpp
@@ -4,30 +4,14 @@
#include "intel_npu/utils/zero/zero_remote_tensor.hpp"
-#include <ze_api.h>
-#include <ze_mem_import_system_memory_ext.h>
-
#include <fstream>
#include "intel_npu/utils/utils.hpp"
-#include "intel_npu/utils/zero/zero_api.hpp"
-#include "intel_npu/utils/zero/zero_utils.hpp"
+#include "intel_npu/utils/zero/zero_mem_pool.hpp"
#include "openvino/core/memory_util.hpp"
#include "openvino/runtime/tensor.hpp"
using namespace ov::intel_npu;
-
-namespace {
-
-static uint32_t to_alloc_flag(TensorType tensor_type) {
- if (tensor_type == TensorType::INPUT) {
- return ZE_HOST_MEM_ALLOC_FLAG_BIAS_WRITE_COMBINED;
- }
- return 0;
-}
-
-} // namespace
-
namespace intel_npu {
ZeroRemoteTensor::ZeroRemoteTensor(const std::shared_ptr<ov::IRemoteContext>& context,
@@ -54,25 +38,6 @@ ZeroRemoteTensor::ZeroRemoteTensor(const std::shared_ptr<ov::IRemoteContext>& co
const auto byte_size = ov::util::get_memory_size_safe(element_type, shape);
OPENVINO_ASSERT(byte_size, "Cannot allocate memory for type: ", element_type, " and shape: ", shape);
- ze_device_external_memory_properties_t desc = {};
- desc.stype = ZE_STRUCTURE_TYPE_DEVICE_EXTERNAL_MEMORY_PROPERTIES;
- auto res = zeDeviceGetExternalMemoryProperties(_init_structs->getDevice(), &desc);
- if (res == ZE_RESULT_SUCCESS) {
-#ifdef _WIN32
- if (desc.memoryAllocationImportTypes & ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32) {
- _external_memory_support = true;
- }
-#else
- if (desc.memoryAllocationImportTypes & ZE_EXTERNAL_MEMORY_TYPE_FLAG_DMA_BUF) {
- _external_memory_support = true;
- }
-#endif
-
- if (desc.memoryAllocationImportTypes & ZE_EXTERNAL_MEMORY_TYPE_FLAG_STANDARD_ALLOCATION) {
- _mmaped_file_support = true;
- }
- }
-
allocate(*byte_size);
}
@@ -132,41 +97,7 @@ std::shared_ptr<ov::IRemoteContext> ZeroRemoteTensor::get_context() const {
return _context;
}
-ZeroRemoteTensor::~ZeroRemoteTensor() {
- auto res = deallocate();
- if (!res) {
- _logger.error("ZeroRemoteTensor failed to free the memory");
- }
-}
-
-bool ZeroRemoteTensor::deallocate() noexcept {
- switch (_mem_type) {
- case MemType::L0_INTERNAL_BUF:
- case MemType::SHARED_BUF:
- case MemType::MMAPED_FILE: {
- if (_data) {
- auto result = zeMemFree(_init_structs->getContext(), _data);
- if (ZE_RESULT_SUCCESS != result) {
- if (ZE_RESULT_ERROR_UNINITIALIZED == result) {
- _logger.warning("ZeroRemoteTensor failed to free memory; Level zero context was already destroyed "
- "and memory was already released by the driver.");
- } else {
- _logger.error("zeMemFree failed %#X", uint64_t(result));
- return false;
- }
- }
-
- _data = nullptr;
- }
-
- return true;
- }
- default:
- return false;
- }
-}
-
-void ZeroRemoteTensor::copy_file_data_to_level_zero_memory() {
+void ZeroRemoteTensor::copy_file_data_to_level_zero_memory(const size_t size_to_read) {
if (!_file_descriptor.has_value()) {
OPENVINO_THROW("No parameter ", file_descriptor.name(), " found in parameters map");
}
@@ -179,6 +110,13 @@ void ZeroRemoteTensor::copy_file_data_to_level_zero_memory() {
_file_descriptor.value()._file_path,
", because the value exceeds std::streamsize limit");
+ OPENVINO_ASSERT(size_to_read <= static_cast<size_t>(std::numeric_limits<std::streamsize>::max()),
+ "Cannot set offset ",
+ size_to_read,
+ " from ",
+ size_to_read,
+ ", because the value exceeds std::streamsize limit");
+
std::streamoff offset = static_cast<std::streamoff>(_file_descriptor.value()._offset_in_bytes);
std::ifstream fin(_file_descriptor.value()._file_path, std::ios::binary);
@@ -190,20 +128,19 @@ void ZeroRemoteTensor::copy_file_data_to_level_zero_memory() {
OPENVINO_THROW("Offset is beyond the end of the file.");
}
- std::streamoff size_to_read = file_size - offset;
fin.seekg(offset, std::ios::beg);
- size_t aligned_size = utils::align_size_to_standard_page_size(size_to_read);
+ _host_memory = ZeroMemPool::get_instance().allocate_zero_memory(_init_structs,
+ size_to_read,
+ utils::STANDARD_PAGE_SIZE,
+ _tensor_type == TensorType::INPUT ? true : false);
+ _data = _host_memory->data();
- ze_host_mem_alloc_desc_t desc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC, nullptr, to_alloc_flag(_tensor_type)};
- THROW_ON_FAIL_FOR_LEVELZERO(
- "zeMemAllocHost",
- zeMemAllocHost(_init_structs->getContext(), &desc, aligned_size, utils::STANDARD_PAGE_SIZE, &_data));
-
- fin.read(static_cast<char*>(_data), size_to_read);
- OPENVINO_ASSERT(fin.gcount() == size_to_read,
+ std::streamoff bytes_to_read = static_cast<std::streamoff>(size_to_read);
+ fin.read(static_cast<char*>(_data), bytes_to_read);
+ OPENVINO_ASSERT(fin.gcount() == bytes_to_read,
"Cannot read ",
- size_to_read,
+ bytes_to_read,
" bytes from ",
_file_descriptor.value()._file_path);
}
@@ -211,99 +148,60 @@ void ZeroRemoteTensor::copy_file_data_to_level_zero_memory() {
void ZeroRemoteTensor::allocate(const size_t bytes) {
switch (_mem_type) {
case MemType::L0_INTERNAL_BUF: {
- size_t size = utils::align_size_to_standard_page_size(bytes);
-
- ze_host_mem_alloc_desc_t desc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC, nullptr, to_alloc_flag(_tensor_type)};
- THROW_ON_FAIL_FOR_LEVELZERO(
- "zeMemAllocHost",
- zeMemAllocHost(_init_structs->getContext(), &desc, size, utils::STANDARD_PAGE_SIZE, &_data));
+ _host_memory =
+ ZeroMemPool::get_instance().allocate_zero_memory(_init_structs,
+ bytes,
+ utils::STANDARD_PAGE_SIZE,
+ _tensor_type == TensorType::INPUT ? true : false);
+ _data = _host_memory->data();
break;
}
case MemType::SHARED_BUF: {
- if (!_external_memory_support) {
- OPENVINO_THROW("Remote tensor functionality is not supported with this driver version");
- }
-
// set up the request to import the external memory handle
-#ifdef _WIN32
- // in the case of the Windows platform memory is locked by the D3D12 memory management - using zeMemAllocDevice
- // to import memory
- ze_external_memory_import_win32_handle_t memory_import = {ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_WIN32,
- nullptr,
- ZE_EXTERNAL_MEMORY_TYPE_FLAG_OPAQUE_WIN32,
- const_cast<void*>(_mem),
- nullptr};
- ze_device_mem_alloc_desc_t desc = {ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC, &memory_import, 0, 0};
- THROW_ON_FAIL_FOR_LEVELZERO("zeMemAllocDevice",
- zeMemAllocDevice(_init_structs->getContext(),
- &desc,
- bytes,
- utils::STANDARD_PAGE_SIZE,
- _init_structs->getDevice(),
- &_data));
-#else
- // in the case of Linux platforms memory could be changed after allocation - using zeMemAllocHost for importing
- // memory
- ze_external_memory_import_fd_t memory_import = {ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_FD,
- nullptr,
- ZE_EXTERNAL_MEMORY_TYPE_FLAG_DMA_BUF,
- static_cast<int>(reinterpret_cast<intptr_t>(_mem))};
- ze_host_mem_alloc_desc_t desc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC, &memory_import, 0};
- THROW_ON_FAIL_FOR_LEVELZERO(
- "zeMemAllocHost",
- zeMemAllocHost(_init_structs->getContext(), &desc, bytes, utils::STANDARD_PAGE_SIZE, &_data));
-#endif
+ _host_memory = ZeroMemPool::get_instance().import_shared_memory(_init_structs, _mem, bytes);
+ _data = _host_memory->data();
break;
}
case MemType::MMAPED_FILE: {
// File_descriptor shall be set if mem_type is a mmaped file type.
- if (!_file_descriptor.has_value()) {
- OPENVINO_THROW("No parameter ", file_descriptor.name(), " found in parameters map");
- }
+ OPENVINO_ASSERT(_file_descriptor.has_value(),
+ "No parameter ",
+ file_descriptor.name(),
+ " found in parameters map");
- if (!_mmaped_file_support) {
+ if (!_init_structs->isExternalMemoryStandardAllocationSupported()) {
_logger.info("Importing mmaped memory isn't supported for this configuration. File data will be copied to "
"the level zero memory");
- copy_file_data_to_level_zero_memory();
+ copy_file_data_to_level_zero_memory(bytes);
break;
}
if (_tensor_type == TensorType::OUTPUT) {
- _logger.info("Importing mmaped memory isn't supported for output tensors. File data will be copied to the "
- "level zero memory");
-
- copy_file_data_to_level_zero_memory();
- break;
+ // It is impossible to work on output today since ov::read_tensor_data opens the file in read-only mode.
+ OPENVINO_THROW("Importing memory from a memory-mapped file is supported only for input tensors");
} else if (_tensor_type == TensorType::BINDED) {
_logger.warning("Importing memory from a memory-mapped file is supported only for input tensors");
_tensor_type = TensorType::INPUT;
}
- // memory map the file
_mmap_tensor = ov::read_tensor_data(_file_descriptor.value()._file_path,
- ov::element::u8,
- ov::PartialShape::dynamic(1),
+ _element_type,
+ _shape,
_file_descriptor.value()._offset_in_bytes,
true);
- size_t aligned_size = utils::align_size_to_standard_page_size(_mmap_tensor.get_byte_size());
-
- _ze_external_memory_import_system_memory_t memory_import = {
- ZE_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMPORT_SYSTEM_MEMORY,
- nullptr,
- _mmap_tensor.data(),
- aligned_size};
-
- uint32_t flag = ZE_HOST_MEM_ALLOC_FLAG_BIAS_WRITE_COMBINED;
- ze_host_mem_alloc_desc_t desc = {ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC, &memory_import, flag};
-
- auto result =
- zeMemAllocHost(_init_structs->getContext(), &desc, aligned_size, utils::STANDARD_PAGE_SIZE, &_data);
-
- if (result != ZE_RESULT_SUCCESS) {
+ try {
+ size_t aligned_size = utils::align_size_to_standard_page_size(bytes);
+ _host_memory = ZeroMemPool::get_instance().import_standard_allocation_memory(
+ _init_structs,
+ _mmap_tensor.data(),
+ aligned_size,
+ _tensor_type == TensorType::INPUT ? true : false);
+ _data = _host_memory->data();
+ } catch (const ZeroMemException&) {
_logger.info("Failed to import mmaped memory. File data will be copied to the level zero memory");
_mmap_tensor = {}; // destroy memory if it couldn't be imported
- copy_file_data_to_level_zero_memory();
+ copy_file_data_to_level_zero_memory(bytes);
}
break;
}
diff --git a/src/plugins/intel_npu/tests/functional/CMakeLists.txt b/src/plugins/intel_npu/tests/functional/CMakeLists.txt
index 6413ff60622..d009defbaf6 100644
--- a/src/plugins/intel_npu/tests/functional/CMakeLists.txt
+++ b/src/plugins/intel_npu/tests/functional/CMakeLists.txt
@@ -23,7 +23,7 @@ list(APPEND OPTIONAL_FUNC_TESTS_INCLUDES
)
list(APPEND OPTIONAL_FUNC_TESTS_LIBS openvino_npu_driver_compiler_adapter
- openvino_npu_level_zero_backend openvino_npu_zero_utils)
+ openvino_npu_level_zero_backend openvino_npu_zero_utils openvino_npu_logger_utils)
ov_add_test_target(
NAME
diff --git a/src/plugins/intel_npu/tests/functional/behavior/infer_request_run.hpp b/src/plugins/intel_npu/tests/functional/behavior/infer_request_run.hpp
index 7656962efc8..14c6e7134cc 100644
--- a/src/plugins/intel_npu/tests/functional/behavior/infer_request_run.hpp
+++ b/src/plugins/intel_npu/tests/functional/behavior/infer_request_run.hpp
@@ -15,13 +15,13 @@
#include <random>
#include <thread>
-#include "shared_test_classes/base/ov_behavior_test_utils.hpp"
#include "behavior/ov_infer_request/inference.hpp"
#include "common/npu_test_env_cfg.hpp"
#include "common/utils.hpp"
#include "functional_test_utils/ov_plugin_cache.hpp"
#include "intel_npu/npu_private_properties.hpp"
#include "intel_npu/utils/zero/zero_init.hpp"
+#include "intel_npu/utils/zero/zero_utils.hpp"
#include "openvino/core/any.hpp"
#include "openvino/core/node_vector.hpp"
#include "openvino/op/op.hpp"
@@ -30,6 +30,7 @@
#include "openvino/runtime/core.hpp"
#include "openvino/runtime/intel_npu/level_zero/level_zero.hpp"
#include "overload/overload_test_utils_npu.hpp"
+#include "shared_test_classes/base/ov_behavior_test_utils.hpp"
using CompilationParams = std::tuple<std::string, // Device name
ov::AnyMap // Config
@@ -1729,6 +1730,62 @@ TEST_P(CpuVaTensorsTests, SetMultipleRemoteAllignedAndNotAllignedTensors) {
}
}
+TEST_P(CpuVaTensorsTests, SetAndDestroyDifferentAlignedTensors) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ auto shape = Shape{1, 16, 16, 16};
+ auto shape_size = ov::shape_size(shape);
+ auto model = createModel(element::f32, shape, "N...");
+
+ compiled_model = core->compile_model(model, target_device, configuration);
+ ov::InferRequest inference_request0, inference_request1;
+
+ const auto input_byte_size = shape_size * sizeof(float);
+ auto input_data = static_cast<float*>(::operator new(input_byte_size, std::align_val_t(4096)));
+ // Create two tensors for the same buffer to mimic weights/data sharing among multiple inference requests
+ auto input_tensor0 = ov::Tensor{ov::element::f32, shape, input_data};
+ auto input_tensor1 = ov::Tensor{ov::element::f32, shape, input_data};
+
+ inference_request0 = compiled_model.create_infer_request();
+ inference_request1 = compiled_model.create_infer_request();
+
+ for (size_t i = 0; i < shape_size; ++i) {
+ input_data[i] = 6.f;
+ }
+
+ inference_request0.set_input_tensor(input_tensor0);
+ inference_request0.infer();
+ inference_request0.set_input_tensor(input_tensor1);
+ inference_request1.set_input_tensor(input_tensor1);
+ inference_request0.infer();
+
+ auto output_tensor0 = inference_request0.get_output_tensor();
+ inference_request0 = {};
+
+ inference_request1.infer();
+ inference_request1.set_input_tensor(input_tensor0);
+ inference_request1.infer();
+
+ auto output_tensor1 = inference_request1.get_output_tensor();
+ inference_request1 = {};
+
+ float expected_result = 7.f;
+
+ auto* output_tensor_data = reinterpret_cast<float*>(output_tensor0.data());
+ for (size_t j = 0; j < shape_size; ++j) {
+ EXPECT_NEAR(output_tensor_data[j], expected_result, 1e-5)
+ << " Expected=" << expected_result << ", actual=" << output_tensor_data[j] << " for index " << j;
+ }
+
+ output_tensor_data = reinterpret_cast<float*>(output_tensor1.data());
+ for (size_t j = 0; j < shape_size; ++j) {
+ EXPECT_NEAR(output_tensor_data[j], expected_result, 1e-5)
+ << " Expected=" << expected_result << ", actual=" << output_tensor_data[j] << " for index " << j;
+ }
+
+ ::operator delete(input_data, std::align_val_t(4096));
+}
+
TEST_P(CpuVaTensorsTests, checkResultsAfterStateTensorsUseImportCpuVa0) {
SKIP_IF_CURRENT_TEST_IS_DISABLED()
diff --git a/src/plugins/intel_npu/tests/functional/internal/backend/zero_tensor_tests.hpp b/src/plugins/intel_npu/tests/functional/internal/backend/zero_tensor_tests.hpp
index 4254185f77c..7001c2dc158 100644
--- a/src/plugins/intel_npu/tests/functional/internal/backend/zero_tensor_tests.hpp
+++ b/src/plugins/intel_npu/tests/functional/internal/backend/zero_tensor_tests.hpp
@@ -93,6 +93,7 @@ public:
utils::PluginCache::get().reset();
}
+ init_struct = nullptr;
APIBaseTest::TearDown();
}
};
@@ -101,19 +102,19 @@ TEST_P(ZeroTensorTests, AllocateDeleteAllocateZeroTensor) {
SKIP_IF_CURRENT_TEST_IS_DISABLED()
auto shape = Shape{1, 2, 2, 2};
+
auto zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, element_type, shape, true);
- ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- zero_tensor->data()));
+ ASSERT_TRUE(
+ ::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(), zero_tensor->data()));
zero_tensor = {};
zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, element_type, shape, false);
- ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- zero_tensor->data()));
+ ASSERT_TRUE(
+ ::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(), zero_tensor->data()));
void* address = zero_tensor->data();
zero_tensor = {};
- ASSERT_FALSE(
- ::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(), address));
+ ASSERT_FALSE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(), address));
}
TEST_P(ZeroTensorTests, CheckSetSmallerShape) {
@@ -136,8 +137,8 @@ TEST_P(ZeroTensorTests, CheckSetSmallerShape) {
EXPECT_EQ(new_shape_size, zero_tensor->get_size());
EXPECT_EQ(new_shape_size * element_type.size(), zero_tensor->get_byte_size());
EXPECT_EQ(data, zero_tensor->data());
- ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- zero_tensor->data()));
+ ASSERT_TRUE(
+ ::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(), zero_tensor->data()));
}
TEST_P(ZeroTensorTests, CheckSetBiggerShape) {
@@ -160,8 +161,8 @@ TEST_P(ZeroTensorTests, CheckSetBiggerShape) {
EXPECT_EQ(new_shape_size, zero_tensor->get_size());
EXPECT_EQ(new_shape_size * element_type.size(), zero_tensor->get_byte_size());
ASSERT_TRUE(zero_tensor->memory_address_changed());
- ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- zero_tensor->data()));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ zero_tensor->data()));
} else {
ASSERT_THROW(zero_tensor->set_shape(new_shape), ov::Exception);
}
@@ -293,8 +294,8 @@ TEST_P(ZeroTensorTests, CopyDefaultTensorExpectedThrow) {
// shape size is unaligned to standard page size, expect to fail
auto data = static_cast<float*>(::operator new(ov::shape_size(shape) * element_type.size()));
auto default_tensor = make_tensor(element_type, shape, data);
- ASSERT_THROW(auto zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, default_tensor, npu_config),
- ::intel_npu::ZeroTensorException);
+ ASSERT_THROW(auto zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, default_tensor),
+ ::intel_npu::ZeroMemException);
::operator delete(data);
}
@@ -306,7 +307,7 @@ TEST_P(ZeroTensorTests, CopyZeroTensorAndKeepAlive) {
auto zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, element_type, shape, true);
- auto copy_zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, zero_tensor, npu_config);
+ auto copy_zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, zero_tensor);
EXPECT_EQ(zero_tensor->get_shape(), copy_zero_tensor->get_shape());
EXPECT_EQ(zero_tensor->data(), copy_zero_tensor->data());
@@ -314,8 +315,8 @@ TEST_P(ZeroTensorTests, CopyZeroTensorAndKeepAlive) {
EXPECT_EQ(zero_tensor->get_size(), copy_zero_tensor->get_size());
zero_tensor = {};
- ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- copy_zero_tensor->data()));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ copy_zero_tensor->data()));
ASSERT_THROW(copy_zero_tensor->set_shape({1, 20, 20, 20}), ov::Exception);
}
@@ -328,7 +329,7 @@ TEST_P(ZeroTensorTests, CopyHostTensorAndKeepAlive) {
auto host_tensor = std::make_shared<::intel_npu::ZeroHostTensor>(zero_context, init_struct, element_type, shape);
- auto copy_zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, host_tensor, npu_config);
+ auto copy_zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, host_tensor);
EXPECT_EQ(host_tensor->get_shape(), copy_zero_tensor->get_shape());
EXPECT_EQ(host_tensor->data(), copy_zero_tensor->data());
@@ -336,8 +337,8 @@ TEST_P(ZeroTensorTests, CopyHostTensorAndKeepAlive) {
EXPECT_EQ(host_tensor->get_size(), copy_zero_tensor->get_size());
host_tensor = {};
- ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- copy_zero_tensor->data()));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ copy_zero_tensor->data()));
ASSERT_THROW(copy_zero_tensor->set_shape({1, 20, 20, 20}), ov::Exception);
}
@@ -351,7 +352,7 @@ TEST_P(ZeroTensorTests, CopyRemoteTensorAndKeepAlive) {
auto remote_tensor =
std::make_shared<::intel_npu::ZeroRemoteTensor>(zero_context, init_struct, element_type, shape);
- auto copy_zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, remote_tensor, npu_config);
+ auto copy_zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, remote_tensor);
EXPECT_EQ(remote_tensor->get_shape(), copy_zero_tensor->get_shape());
EXPECT_EQ(remote_tensor->get_original_memory(), copy_zero_tensor->data());
@@ -359,8 +360,8 @@ TEST_P(ZeroTensorTests, CopyRemoteTensorAndKeepAlive) {
EXPECT_EQ(remote_tensor->get_size(), copy_zero_tensor->get_size());
remote_tensor = {};
- ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- copy_zero_tensor->data()));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ copy_zero_tensor->data()));
ASSERT_THROW(copy_zero_tensor->set_shape({1, 20, 20, 20}), ov::Exception);
}
@@ -379,8 +380,200 @@ TEST_P(ZeroTensorTests, CopyRemoteTensorFromAnotherContextThrow) {
auto remote_tensor_impl = get_tensor_impl(remote_tensor);
ASSERT_THROW(
- auto zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, remote_tensor_impl, npu_config),
- ::intel_npu::ZeroTensorException);
+ auto zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, remote_tensor_impl),
+ ::intel_npu::ZeroMemException);
+}
+
+TEST_P(ZeroTensorTests, CheckStandardAllocation) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ auto shape = Shape{1, 64, 64, 64};
+
+ auto logger = ::intel_npu::Logger("CheckStandardAllocation", ov::log::Level::INFO);
+
+ // shape size is aligned to standard page size, align address as well
+ auto data =
+ static_cast<float*>(::operator new(ov::shape_size(shape) * element_type.size(), std::align_val_t(4096)));
+ auto default_tensor = make_tensor(element_type, shape, data);
+ std::shared_ptr<::intel_npu::ZeroTensor> zero_tensor;
+ if (init_struct->isExternalMemoryStandardAllocationSupported()) {
+ OV_ASSERT_NO_THROW(zero_tensor =
+ std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, default_tensor));
+ logger.info("Import memory");
+ } else {
+ ASSERT_THROW(zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, default_tensor),
+ ::intel_npu::ZeroMemException);
+ logger.error("Import memory is not supported, throw is expected.");
+ }
+
+ ::operator delete(data, std::align_val_t(4096));
+}
+
+TEST_P(ZeroTensorTests, UseSameStandardAllocationMultipleTimesCompatible) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ if (init_struct->isExternalMemoryStandardAllocationSupported()) {
+ auto shape = Shape{1, 64, 64, 64};
+
+ // shape size is aligned to standard page size, align address as well
+ auto data =
+ static_cast<float*>(::operator new(ov::shape_size(shape) * element_type.size(), std::align_val_t(4096)));
+ auto default_tensor = make_tensor(element_type, shape, data);
+ std::shared_ptr<::intel_npu::ZeroTensor> zero_tensor0;
+ OV_ASSERT_NO_THROW(zero_tensor0 =
+ std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, default_tensor));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ default_tensor->data()));
+
+ std::shared_ptr<::intel_npu::ZeroTensor> zero_tensor1;
+ OV_ASSERT_NO_THROW(zero_tensor1 =
+ std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, default_tensor));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ default_tensor->data()));
+
+ zero_tensor0 = {};
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ default_tensor->data()));
+
+ std::shared_ptr<::intel_npu::ZeroTensor> zero_tensor2;
+ OV_ASSERT_NO_THROW(zero_tensor2 =
+ std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, zero_tensor1));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ default_tensor->data()));
+
+ zero_tensor1 = {};
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ default_tensor->data()));
+
+ zero_tensor2 = {};
+ ASSERT_FALSE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ default_tensor->data()));
+
+ ::operator delete(data, std::align_val_t(4096));
+ }
+}
+
+TEST_P(ZeroTensorTests, UseRangePointerFromAlreadyImportedStandardAllocation) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ if (init_struct->isExternalMemoryStandardAllocationSupported()) {
+ auto shape = Shape{1, 64, 64, 64};
+
+ // shape size is aligned to standard page size, align address as well
+ auto data =
+ static_cast<float*>(::operator new(ov::shape_size(shape) * element_type.size(), std::align_val_t(4096)));
+ auto default_tensor = make_tensor(element_type, shape, data);
+ std::shared_ptr<::intel_npu::ZeroTensor> zero_tensor0;
+ OV_ASSERT_NO_THROW(zero_tensor0 =
+ std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, default_tensor));
+
+ auto smaller_shape = Shape{1, 2, 2, 2};
+ auto smaller_tensor = make_tensor(element_type, smaller_shape, data + 16);
+ std::shared_ptr<::intel_npu::ZeroTensor> zero_tensor1;
+ OV_ASSERT_NO_THROW(zero_tensor1 =
+ std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, smaller_tensor));
+
+ EXPECT_EQ(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ zero_tensor0->data()),
+ ::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ zero_tensor1->data()));
+ EXPECT_EQ(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ default_tensor->data()),
+ ::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ smaller_tensor->data()));
+ EXPECT_EQ(default_tensor->data(), zero_tensor0->data());
+ EXPECT_EQ(smaller_tensor->data(), zero_tensor1->data());
+ EXPECT_NE(zero_tensor0->data(), zero_tensor1->data());
+ EXPECT_EQ(static_cast<void*>(static_cast<float*>(zero_tensor0->data()) + 16), zero_tensor1->data());
+
+ ::operator delete(data, std::align_val_t(4096));
+ }
+}
+
+TEST_P(ZeroTensorTests, UseRangeOutOfBoundsPointerFromAlreadyImportedStandardAllocation) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ if (init_struct->isExternalMemoryStandardAllocationSupported()) {
+ auto shape = Shape{1, 64, 64, 64};
+
+ // shape size is aligned to standard page size, align address as well
+ auto data =
+ static_cast<float*>(::operator new(ov::shape_size(shape) * element_type.size(), std::align_val_t(4096)));
+ auto default_tensor = make_tensor(element_type, shape, data);
+ std::shared_ptr<::intel_npu::ZeroTensor> zero_tensor0;
+ OV_ASSERT_NO_THROW(zero_tensor0 =
+ std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, default_tensor));
+
+ auto different_shape = Shape{1, 64, 64, 64};
+ auto different_tensor = make_tensor(element_type, different_shape, data + 16);
+ std::shared_ptr<::intel_npu::ZeroTensor> zero_tensor1;
+ ASSERT_THROW(
+ zero_tensor1 = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, different_tensor),
+ ::intel_npu::ZeroMemException);
+
+ ::operator delete(data, std::align_val_t(4096));
+ }
+}
+
+TEST_P(ZeroTensorTests, UseBiggerMemoryFromAlreadyImportedStandardAllocation) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ if (init_struct->isExternalMemoryStandardAllocationSupported()) {
+ auto shape = Shape{1, 64, 64, 64};
+
+ // shape size is aligned to standard page size, align address as well
+ auto data =
+ static_cast<float*>(::operator new(ov::shape_size(shape) * element_type.size(), std::align_val_t(4096)));
+
+ auto smaller_shape = Shape{1, 16, 16, 16};
+ auto smaller_tensor = make_tensor(element_type, smaller_shape, data);
+ std::shared_ptr<::intel_npu::ZeroTensor> zero_tensor0;
+ OV_ASSERT_NO_THROW(zero_tensor0 =
+ std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, smaller_tensor));
+
+ auto bigger_tensor = make_tensor(element_type, shape, data);
+ std::shared_ptr<::intel_npu::ZeroTensor> zero_tensor1;
+ ASSERT_THROW(zero_tensor1 = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, bigger_tensor),
+ ::intel_npu::ZeroMemException);
+
+ ::operator delete(data, std::align_val_t(4096));
+ }
+}
+
+TEST_P(ZeroTensorTests, CreateSmallerZeroTensorsFromLargerRemoteTensor) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ std::shared_ptr<::intel_npu::IEngineBackend> engine_backend = std::make_shared<::intel_npu::ZeroEngineBackend>();
+ auto zero_context = std::make_shared<::intel_npu::RemoteContextImpl>(engine_backend);
+ auto shape = Shape{1, 64, 64, 64};
+
+ auto host_tensor = std::make_shared<::intel_npu::ZeroHostTensor>(zero_context, init_struct, element_type, shape);
+ void* data = host_tensor->data();
+
+ auto smaller_shape0 = Shape{1, 32, 32, 32};
+ auto smaller_tensor0 = make_tensor(element_type, smaller_shape0, host_tensor->data());
+ auto zero_tensor0 = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, smaller_tensor0);
+ EXPECT_EQ(smaller_shape0, zero_tensor0->get_shape());
+ EXPECT_EQ(host_tensor->data(), smaller_tensor0->data());
+
+ auto smaller_shape1 = Shape{1, 16, 16, 16};
+ auto smaller_tensor1 = make_tensor(element_type, smaller_shape1, smaller_tensor0->data());
+ auto zero_tensor1 = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, smaller_tensor1);
+ EXPECT_EQ(smaller_shape1, zero_tensor1->get_shape());
+ EXPECT_EQ(host_tensor->data(), smaller_tensor1->data());
+
+ host_tensor = {};
+ ASSERT_TRUE(
+ ::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(), zero_tensor0->data()));
+
+ zero_tensor0 = {};
+ smaller_tensor0 = {};
+ ASSERT_TRUE(
+ ::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(), zero_tensor1->data()));
+
+ smaller_tensor1 = {};
+ zero_tensor1 = {};
+ ASSERT_FALSE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(), data));
}
using ZeroTensorTestsCheckDataType = ZeroTensorTests;
@@ -393,7 +586,7 @@ TEST_P(ZeroTensorTestsCheckDataType, CopyZeroTensorAndCheckTensorDataType) {
auto zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, element_type, shape, true);
EXPECT_EQ(element_type, zero_tensor->get_element_type());
- auto copy_zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, zero_tensor, npu_config);
+ auto copy_zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, zero_tensor);
EXPECT_EQ(zero_tensor->get_element_type(), copy_zero_tensor->get_element_type());
}
diff --git a/src/plugins/intel_npu/tests/functional/internal/backend/zero_variable_state_tests.hpp b/src/plugins/intel_npu/tests/functional/internal/backend/zero_variable_state_tests.hpp
index 90c1b1de8e8..ca2d1fa3fb7 100644
--- a/src/plugins/intel_npu/tests/functional/internal/backend/zero_variable_state_tests.hpp
+++ b/src/plugins/intel_npu/tests/functional/internal/backend/zero_variable_state_tests.hpp
@@ -54,7 +54,7 @@ protected:
::intel_npu::Config npu_config = ::intel_npu::Config(options);
public:
- static std::string getTestCaseName(testing::TestParamInfo<CompilationParams> obj) {
+ static std::string getTestCaseName(const testing::TestParamInfo<CompilationParams>& obj) {
std::string targetDevice;
ov::AnyMap configuration;
std::tie(targetDevice, configuration) = obj.param;
@@ -88,6 +88,7 @@ public:
utils::PluginCache::get().reset();
}
+ init_struct = nullptr;
APIBaseTest::TearDown();
}
};
@@ -99,8 +100,8 @@ TEST_P(ZeroVariableStateTests, CreateZeroStateAndCheckAgainstZeroTensorState) {
auto zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, element::f32, shape, true);
auto zero_state =
std::make_shared<::intel_npu::ZeroVariableState>(init_struct, "state", zero_tensor, 1, 1, npu_config);
- ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- zero_state->get_state()->data()));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ zero_state->get_state()->data()));
EXPECT_EQ(zero_state->get_state()->data(), zero_tensor->data());
EXPECT_EQ(zero_state->get_state()->get_shape(), zero_tensor->get_shape());
@@ -124,13 +125,13 @@ TEST_P(ZeroVariableStateTests, CreateZeroStateAndUseSetStateWithZeroTensor) {
auto zero_tensor0 = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, element::f32, shape, true);
auto zero_state =
std::make_shared<::intel_npu::ZeroVariableState>(init_struct, "state", zero_tensor0, 1, 1, npu_config);
- ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- zero_state->get_state()->data()));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ zero_state->get_state()->data()));
auto zero_tensor1 = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, element::f32, shape, true);
zero_state->set_state(zero_tensor1);
- ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- zero_state->get_state()->data()));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ zero_state->get_state()->data()));
ASSERT_TRUE(zero_state->state_update_pending());
ASSERT_TRUE(zero_state->zero_state_update_pending());
@@ -159,18 +160,16 @@ TEST_P(ZeroVariableStateTests, CreateZeroStateAndUseSetStateWithNormalTensor) {
auto zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, element::f32, shape, true);
auto zero_state =
std::make_shared<::intel_npu::ZeroVariableState>(init_struct, "state", zero_tensor, 1, 1, npu_config);
- ASSERT_TRUE(
- ::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
zero_state->get_user_state()->data()));
// shape size is unaligned to standard page size, expect to fail
auto data = static_cast<float*>(::operator new(ov::shape_size(shape) * sizeof(ov::element::f32)));
auto tensor = make_tensor(ov::element::f32, shape, data);
zero_state->set_state(tensor);
- ASSERT_FALSE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- zero_state->get_state()->data()));
- ASSERT_TRUE(
- ::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
+ ASSERT_FALSE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ zero_state->get_state()->data()));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
zero_state->get_zero_state()->data()));
ASSERT_TRUE(zero_state->state_update_pending());
@@ -201,17 +200,16 @@ TEST_P(ZeroVariableStateTests, CreateZeroStateAndUseSetStateWithNormalTensorAfte
auto zero_tensor = std::make_shared<::intel_npu::ZeroTensor>(init_struct, npu_config, element::f32, shape, true);
auto zero_state =
std::make_shared<::intel_npu::ZeroVariableState>(init_struct, "state", zero_tensor, 1, 1, npu_config);
- ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- zero_state->get_state()->data()));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ zero_state->get_state()->data()));
// shape size is unaligned to standard page size, expect to fail
auto data = static_cast<float*>(::operator new(ov::shape_size(shape) * sizeof(ov::element::f32)));
auto tensor = make_tensor(ov::element::f32, shape, data);
zero_state->set_state(tensor);
- ASSERT_FALSE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- zero_state->get_state()->data()));
- ASSERT_TRUE(
- ::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
+ ASSERT_FALSE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ zero_state->get_state()->data()));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
zero_state->get_zero_state()->data()));
ASSERT_TRUE(zero_state->state_update_pending());
@@ -250,10 +248,9 @@ TEST_P(ZeroVariableStateTests, CreateZeroStateAndUseSetStateWithHostTensor) {
auto host_tensor =
std::make_shared<::intel_npu::ZeroHostTensor>(zero_context, init_struct, ov::element::f32, shape);
zero_state->set_state(host_tensor);
- ASSERT_TRUE(::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
- zero_state->get_state()->data()));
- ASSERT_TRUE(
- ::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
+ zero_state->get_state()->data()));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
zero_state->get_zero_state()->data()));
ASSERT_TRUE(zero_state->state_update_pending());
@@ -294,11 +291,9 @@ TEST_P(ZeroVariableStateTests, CreateZeroStateAndUseSetStateWithRemoteTensor) {
zero_state->set_state(remote_tensor);
auto zero_remote_tensor = std::dynamic_pointer_cast<::intel_npu::ZeroRemoteTensor>(zero_state->get_state()._ptr);
- ASSERT_TRUE(
- ::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
zero_remote_tensor->get_original_memory()));
- ASSERT_TRUE(
- ::intel_npu::zeroUtils::memory_was_allocated_in_the_same_l0_context(init_struct->getContext(),
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(),
zero_state->get_zero_state()->data()));
ASSERT_TRUE(zero_state->state_update_pending());
diff --git a/src/plugins/intel_npu/tests/functional/internal/utils/zero/zero_mem_pool_tests.cpp b/src/plugins/intel_npu/tests/functional/internal/utils/zero/zero_mem_pool_tests.cpp
new file mode 100644
index 00000000000..7b2e57e87db
--- /dev/null
+++ b/src/plugins/intel_npu/tests/functional/internal/utils/zero/zero_mem_pool_tests.cpp
@@ -0,0 +1,18 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "internal/utils/zero/zero_mem_pool_tests.hpp"
+
+#include "common/npu_test_env_cfg.hpp"
+#include "common/utils.hpp"
+#include "intel_npu/config/options.hpp"
+#include "intel_npu/npu_private_properties.hpp"
+
+using namespace ov::test::behavior;
+
+
+INSTANTIATE_TEST_SUITE_P(compatibility_smoke_BehaviorTest,
+ ZeroMemPoolTests,
+ ::testing::Values(ov::test::utils::DEVICE_NPU),
+ ZeroMemPoolTests::getTestCaseName);
diff --git a/src/plugins/intel_npu/tests/functional/internal/utils/zero/zero_mem_pool_tests.hpp b/src/plugins/intel_npu/tests/functional/internal/utils/zero/zero_mem_pool_tests.hpp
new file mode 100644
index 00000000000..441440217b7
--- /dev/null
+++ b/src/plugins/intel_npu/tests/functional/internal/utils/zero/zero_mem_pool_tests.hpp
@@ -0,0 +1,208 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <gmock/gmock-matchers.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <array>
+#include <cstddef>
+#include <exception>
+#include <memory>
+#include <random>
+#include <thread>
+
+#include "common/npu_test_env_cfg.hpp"
+#include "common/utils.hpp"
+#include "functional_test_utils/ov_plugin_cache.hpp"
+#include "intel_npu/common/npu.hpp"
+#include "intel_npu/config/config.hpp"
+#include "intel_npu/config/options.hpp"
+#include "intel_npu/utils/zero/zero_init.hpp"
+#include "intel_npu/utils/zero/zero_mem.hpp"
+#include "intel_npu/utils/zero/zero_mem_pool.hpp"
+#include "intel_npu/utils/zero/zero_utils.hpp"
+#include "openvino/core/any.hpp"
+#include "openvino/runtime/core.hpp"
+#include "shared_test_classes/base/ov_behavior_test_utils.hpp"
+
+#ifdef _WIN32
+# include <windows.h>
+# define SLEEP_MS(x) Sleep(x)
+#else
+# include <unistd.h>
+# define SLEEP_MS(x) usleep((x) * 1000)
+#endif
+
+using CompilationParams = std::string;
+
+using ::testing::AllOf;
+using ::testing::HasSubstr;
+
+namespace ov {
+namespace test {
+namespace behavior {
+class ZeroMemPoolTests : public ov::test::behavior::OVPluginTestBase,
+ public testing::WithParamInterface<CompilationParams> {
+protected:
+ std::shared_ptr<ov::Core> core = utils::PluginCache::get().core();
+ ov::AnyMap configuration;
+ std::shared_ptr<::intel_npu::ZeroInitStructsHolder> init_struct;
+ std::shared_ptr<::intel_npu::OptionsDesc> options = std::make_shared<::intel_npu::OptionsDesc>();
+ ::intel_npu::Config npu_config = ::intel_npu::Config(options);
+
+public:
+ static std::string getTestCaseName(const testing::TestParamInfo<CompilationParams>& obj) {
+ std::string target_device;
+ ov::AnyMap configuration;
+ target_device = obj.param;
+ std::replace(target_device.begin(), target_device.end(), ':', '_');
+ target_device = ov::test::utils::getTestsPlatformFromEnvironmentOr(ov::test::utils::DEVICE_NPU);
+
+ std::ostringstream result;
+ result << "targetDevice=" << target_device << "_";
+ result << "targetPlatform=" << ov::test::utils::getTestsPlatformFromEnvironmentOr(target_device) << "_";
+ if (!configuration.empty()) {
+ for (auto& configItem : configuration) {
+ result << "configItem=" << configItem.first << "_";
+ configItem.second.print(result);
+ }
+ }
+
+ return result.str();
+ }
+
+ void SetUp() override {
+ target_device = this->GetParam();
+
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ OVPluginTestBase::SetUp();
+
+ init_struct = ::intel_npu::ZeroInitStructsHolder::getInstance();
+ }
+
+ void TearDown() override {
+ if (!configuration.empty()) {
+ utils::PluginCache::get().reset();
+ }
+
+ init_struct = nullptr;
+ APIBaseTest::TearDown();
+ }
+};
+
+TEST_P(ZeroMemPoolTests, GetZeroMemoryData) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ void* data = ::operator new(4096, std::align_val_t(4096));
+
+ std::shared_ptr<::intel_npu::ZeroMem> get_zero_mem;
+
+ if (init_struct->isExternalMemoryStandardAllocationSupported()) {
+ OV_ASSERT_NO_THROW(
+ get_zero_mem =
+ ::intel_npu::ZeroMemPool::get_instance().import_standard_allocation_memory(init_struct, data, 4096));
+ ASSERT_TRUE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(), data));
+ } else {
+ ASSERT_THROW(
+ get_zero_mem =
+ ::intel_npu::ZeroMemPool::get_instance().import_standard_allocation_memory(init_struct, data, 4096),
+ ::intel_npu::ZeroMemException);
+ ASSERT_FALSE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(), data));
+ }
+
+ get_zero_mem = {};
+ ASSERT_FALSE(::intel_npu::zeroUtils::get_l0_context_memory_allocation_id(init_struct->getContext(), data));
+
+ ::operator delete(data, std::align_val_t(4096));
+}
+
+TEST_P(ZeroMemPoolTests, MultiThreadingReUseAlreadyAllocatedImportedMemory) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ if (init_struct->isExternalMemoryStandardAllocationSupported()) {
+ const int threads_no = 256;
+ std::array<std::thread, threads_no> threads;
+ std::array<std::shared_ptr<::intel_npu::ZeroMem>, 5> zero_mem;
+ std::array<void*, 3> data;
+
+ // Prep: add few entries in the pool
+ for (int i = 0; i < 3; i++) {
+ data[i] = ::operator new(4096, std::align_val_t(4096));
+ zero_mem[i] =
+ ::intel_npu::ZeroMemPool::get_instance().import_standard_allocation_memory(init_struct, data[i], 4096);
+ }
+ zero_mem[3] = ::intel_npu::ZeroMemPool::get_instance().allocate_zero_memory(init_struct, 4096, 4096);
+ zero_mem[4] = ::intel_npu::ZeroMemPool::get_instance().allocate_zero_memory(init_struct, 4096, 4096);
+
+ for (int i = 0; i < threads_no; ++i) {
+ threads[i] = std::thread([this, &zero_mem, i]() -> void {
+ for (int j = 0; j < 256; j++) {
+ std::shared_ptr<::intel_npu::ZeroMem> get_zero_mem;
+ OV_ASSERT_NO_THROW(get_zero_mem =
+ ::intel_npu::ZeroMemPool::get_instance().import_standard_allocation_memory(
+ init_struct,
+ zero_mem[i % 5]->data(),
+ 4096));
+ SLEEP_MS(0);
+ get_zero_mem = {};
+ }
+ });
+ }
+
+ for (int i = 0; i < threads_no; ++i) {
+ threads[i].join();
+ }
+
+ for (int i = 0; i < 3; i++) {
+ ::operator delete(data[i], std::align_val_t(4096));
+ }
+ }
+}
+
+TEST_P(ZeroMemPoolTests, MultiThreadingImportMemoryReUseAndDestroyIt) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ if (init_struct->isExternalMemoryStandardAllocationSupported()) {
+ const int threads_no = 256;
+ const int no_of_buffers = 5;
+ std::array<std::thread, threads_no> threads;
+ std::array<void*, no_of_buffers> data;
+
+ for (int i = 0; i < no_of_buffers; i++) {
+ data[i] = ::operator new(4096, std::align_val_t(4096));
+ }
+
+ for (int i = 0; i < threads_no; ++i) {
+ threads[i] = std::thread([this, &data, &threads_no, &no_of_buffers]() -> void {
+ for (int j = 0; j < threads_no; j++) {
+ std::shared_ptr<::intel_npu::ZeroMem> zero_mem;
+ OV_ASSERT_NO_THROW(zero_mem =
+ ::intel_npu::ZeroMemPool::get_instance().import_standard_allocation_memory(
+ init_struct,
+ data[j % no_of_buffers],
+ 4096));
+ SLEEP_MS(0);
+ if (j % 2 == 0) {
+ zero_mem = {};
+ }
+ }
+ });
+ }
+
+ for (int i = 0; i < threads_no; ++i) {
+ threads[i].join();
+ }
+
+ for (int i = 0; i < no_of_buffers; i++) {
+ ::operator delete(data[i], std::align_val_t(4096));
+ }
+ }
+}
+
+} // namespace behavior
+} // namespace test
+} // namespace ov
diff --git a/src/plugins/intel_npu/tests/functional/shared_tests_instances/npu_skip_func_tests.xml b/src/plugins/intel_npu/tests/functional/shared_tests_instances/npu_skip_func_tests.xml
index cea54539923..be10ceef231 100644
--- a/src/plugins/intel_npu/tests/functional/shared_tests_instances/npu_skip_func_tests.xml
+++ b/src/plugins/intel_npu/tests/functional/shared_tests_instances/npu_skip_func_tests.xml
@@ -838,5 +838,4 @@ Example:
<filter>.*WeightsSeparationOneShotTests.*</filter>
</filters>
</skip_config>
-
</skip_configs>