Skip to content

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

[NPU] Detach the imported allocation for the custom tensor after inference finishes execution (#34499)

Bogdan Pereanu34c37ff07a7b
diff --git a/src/plugins/intel_npu/src/backend/include/zero_infer_request.hpp b/src/plugins/intel_npu/src/backend/include/zero_infer_request.hpp
index 8f526529b92..b709fa49020 100644
--- a/src/plugins/intel_npu/src/backend/include/zero_infer_request.hpp
+++ b/src/plugins/intel_npu/src/backend/include/zero_infer_request.hpp
@@ -79,6 +79,7 @@ protected:
std::optional<size_t> batchSizeCandidate = std::nullopt);
virtual void prepare_inputs();
+ virtual void prepare_outputs();
const std::shared_ptr<ZeroInitStructsHolder> _initStructs;
const std::shared_ptr<IGraph> _graph;
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 469c29bf699..0b0e296d952 100644
--- a/src/plugins/intel_npu/src/backend/include/zero_tensor.hpp
+++ b/src/plugins/intel_npu/src/backend/include/zero_tensor.hpp
@@ -72,6 +72,9 @@ public:
void prevent_reuse();
bool can_be_reused();
+ void allocate_data();
+ void detach_imported_allocation_for_custom_tensor();
+
~ZeroTensor() override;
private:
@@ -94,6 +97,7 @@ private:
bool _can_be_reused = false;
std::shared_ptr<ZeroMem> _mem_ref;
+ bool _is_custom_user_tensor = false;
};
} // namespace intel_npu
diff --git a/src/plugins/intel_npu/src/backend/src/zero_dynamic_infer_request.cpp b/src/plugins/intel_npu/src/backend/src/zero_dynamic_infer_request.cpp
index 11699ba325a..ac3324f3c0f 100644
--- a/src/plugins/intel_npu/src/backend/src/zero_dynamic_infer_request.cpp
+++ b/src/plugins/intel_npu/src/backend/src/zero_dynamic_infer_request.cpp
@@ -187,6 +187,7 @@ void ZeroDynamicInferRequest::infer_async() {
predict_shapes(outputPros);
check_tensor_and_predicted_shapes(outputPros);
prepare_inputs();
+ prepare_outputs();
update_tensor(outputPros);
OV_ITT_TASK_NEXT(ZERO_INFER, "push");
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 19b691095fb..4d7b3c3c13c 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
@@ -71,7 +71,7 @@ ZeroInferRequest::ZeroInferRequest(const std::shared_ptr<ZeroInitStructsHolder>&
_initStructs(initStructs),
_graph(compiledModel->get_graph()),
_config(config),
- _logger("ZeroInferRequest", config.get<LOG_LEVEL>()),
+ _logger("ZeroInferRequest", _config.get<LOG_LEVEL>()),
_levelZeroInputTensors(_metadata.inputs.size(), std::vector<std::shared_ptr<ZeroTensor>>(1, nullptr)),
_levelZeroOutputTensors(_metadata.outputs.size(), nullptr) {
_logger.debug("ZeroInferRequest::ZeroInferRequest - checking level zero attributes and allocating tensors");
@@ -681,6 +681,7 @@ void ZeroInferRequest::infer_async() {
_logger.debug("InferRequest::infer_async started");
OV_ITT_TASK_CHAIN(ZERO_INFER, itt::domains::LevelZeroBackend, "infer_async", "start");
prepare_inputs();
+ prepare_outputs();
OV_ITT_TASK_NEXT(ZERO_INFER, "push");
_pipeline->push();
@@ -730,42 +731,50 @@ void ZeroInferRequest::prepare_inputs() {
if (is_batched_input(inputIndex)) {
if (batch_size.has_value()) {
for (size_t i = 0; i < userTensor.size(); i++) {
- void* userBuffer = get_tensor_data_ptr(userTensor.at(i)._ptr);
- void* levelZeroBuffer = get_level_zero_input(inputIndex, i)->data();
+ const auto& levelZeroTensor = get_level_zero_input(inputIndex, i);
+ OPENVINO_ASSERT(levelZeroTensor, "Input zero tensor is not allocated.");
+
+ const auto& userTensorPtr = userTensor.at(i);
+ void* userBuffer = get_tensor_data_ptr(userTensorPtr._ptr);
+ void* levelZeroBuffer = levelZeroTensor->data();
+
+ if (levelZeroBuffer == nullptr) {
+ levelZeroTensor->allocate_data();
+ levelZeroBuffer = levelZeroTensor->data();
+ }
if (userBuffer != levelZeroBuffer) {
- if (userBuffer == nullptr || levelZeroBuffer == nullptr) {
- OPENVINO_THROW("Empty buffer");
- }
+ OPENVINO_ASSERT(userBuffer != nullptr && levelZeroBuffer != nullptr, "Empty buffer");
_logger.debug(
"Batched Tensors - Tensor by index: %zu is not allocated in the current Level Zero "
"context, copy bytes from user tensor: %zu, into L0 with expected size: %zu",
inputIndex,
- userTensor.at(i)->get_byte_size(),
- get_level_zero_input(inputIndex, i)->get_byte_size());
+ userTensorPtr->get_byte_size(),
+ levelZeroTensor->get_byte_size());
OV_ITT_TASK_NEXT(ZERO_INFER, "memcpy");
- userTensor.at(i)->copy_to(get_level_zero_input(inputIndex, i));
+ userTensorPtr->copy_to(levelZeroTensor);
}
}
} else {
+ const auto& levelZeroTensor = get_level_zero_input(inputIndex);
+ OPENVINO_ASSERT(levelZeroTensor, "Input zero tensor is not allocated.");
+
_logger.debug("Batched Tensors - Tensor by index: %zu is not allocated in the current Level Zero "
- "context or must be "
- "in a continued memory space, copy into L0 with size: %zu",
+ "context or must be in a continued memory space, copy into L0 with size: %zu",
inputIndex,
- get_level_zero_input(inputIndex)->get_byte_size());
+ levelZeroTensor->get_byte_size());
size_t copied_bytes_from_user = 0;
for (size_t i = 0; i < userTensor.size(); i++) {
- auto viewTensor =
- ov::make_tensor(get_level_zero_input(inputIndex)->get_element_type(),
- get_level_zero_input(inputIndex)->get_shape(),
- static_cast<unsigned char*>(get_level_zero_input(inputIndex)->data()) +
- (i * userTensor.at(i)->get_byte_size()));
+ auto viewTensor = ov::make_tensor(
+ levelZeroTensor->get_element_type(),
+ levelZeroTensor->get_shape(),
+ static_cast<unsigned char*>(levelZeroTensor->data()) + (i * userTensor.at(i)->get_byte_size()));
userTensor.at(i)->copy_to(viewTensor);
copied_bytes_from_user += userTensor.at(i)->get_byte_size();
}
- OPENVINO_ASSERT(get_level_zero_input(inputIndex)->get_byte_size() == copied_bytes_from_user,
+ OPENVINO_ASSERT(levelZeroTensor->get_byte_size() == copied_bytes_from_user,
"Bytes copied must be equal");
}
@@ -778,23 +787,41 @@ void ZeroInferRequest::prepare_inputs() {
continue;
}
+ const auto& levelZeroTensor = get_level_zero_input(inputIndex);
+ OPENVINO_ASSERT(levelZeroTensor, "Input zero tensor is not allocated.");
+
void* userBuffer = get_tensor_data_ptr(userTensor.at(SINGLE_TENSOR)._ptr);
- void* levelZeroBuffer = get_level_zero_input(inputIndex)->data();
+ void* levelZeroBuffer = levelZeroTensor->data();
- if (userBuffer == nullptr || levelZeroBuffer == nullptr) {
- OPENVINO_THROW("Empty buffer");
+ if (levelZeroBuffer == nullptr) {
+ levelZeroTensor->allocate_data();
+ levelZeroBuffer = levelZeroTensor->data();
}
if (userBuffer != levelZeroBuffer) {
+ OPENVINO_ASSERT(userBuffer != nullptr && levelZeroBuffer != nullptr, "Empty buffer");
+
_logger.info("Tensor is not allocated in the current Level Zero context");
OV_ITT_TASK_NEXT(ZERO_INFER, "memcpy");
- userTensor.at(SINGLE_TENSOR)->copy_to(get_level_zero_input(inputIndex));
+ userTensor.at(SINGLE_TENSOR)->copy_to(levelZeroTensor);
}
++inputIndex;
}
}
+void ZeroInferRequest::prepare_outputs() {
+ OV_ITT_TASK_CHAIN(ZERO_INFER, itt::domains::LevelZeroBackend, "infer_async", "prepare_outputs");
+ for (size_t outputIndex = 0; outputIndex < _userOutputTensors.size(); ++outputIndex) {
+ const auto& levelZeroTensor = _levelZeroOutputTensors.at(outputIndex);
+ OPENVINO_ASSERT(levelZeroTensor, "Output zero tensor is not allocated.");
+
+ if (levelZeroTensor->data() == nullptr) {
+ levelZeroTensor->allocate_data();
+ }
+ }
+}
+
void ZeroInferRequest::get_result() {
OV_ITT_TASK_CHAIN(ZERO_RESULT, itt::domains::LevelZeroBackend, "get_result", "pull");
_logger.debug("InferRequest::get_result start");
@@ -819,8 +846,9 @@ void ZeroInferRequest::get_result() {
tensorToBeReshaped->set_shape(actualDims);
}
+ const auto& levelZeroTensor = _levelZeroOutputTensors.at(outputIndex);
void* userBuffer = get_tensor_data_ptr(userTensor._ptr);
- void* levelZeroBuffer = _levelZeroOutputTensors.at(outputIndex)->data();
+ void* levelZeroBuffer = levelZeroTensor->data();
if (userBuffer == nullptr || levelZeroBuffer == nullptr) {
OPENVINO_THROW("Empty buffer");
@@ -829,12 +857,20 @@ void ZeroInferRequest::get_result() {
if (userBuffer != levelZeroBuffer) {
_logger.info("Output tensor by index: %zu is not allocated in the current Level Zero context", outputIndex);
OV_ITT_TASK_NEXT(ZERO_RESULT, "memcpy");
- _levelZeroOutputTensors.at(outputIndex)->copy_to(userTensor._ptr);
+ levelZeroTensor->copy_to(userTensor._ptr);
}
+ levelZeroTensor->detach_imported_allocation_for_custom_tensor();
+
++outputIndex;
}
+ for (size_t inputIndex = 0; inputIndex < _levelZeroInputTensors.size(); ++inputIndex) {
+ for (const auto& levelZeroTensor : get_level_zero_inputs(inputIndex)) {
+ levelZeroTensor->detach_imported_allocation_for_custom_tensor();
+ }
+ }
+
OV_ITT_TASK_NEXT(ZERO_RESULT, "reset");
_pipeline->reset();
_logger.debug("InferRequest::get_result finished");
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 1250c365b51..51528b56dce 100644
--- a/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp
+++ b/src/plugins/intel_npu/src/backend/src/zero_tensor.cpp
@@ -87,6 +87,10 @@ ZeroTensor::ZeroTensor(const std::shared_ptr<ZeroInitStructsHolder>& init_struct
_ptr = static_cast<uint8_t*>(mem_handle_object.value()) + ov::get_tensor_data_offset(*remote_tensor);
}
} else {
+ if (std::dynamic_pointer_cast<ZeroTensor>(_user_tensor._ptr) == nullptr) {
+ _is_custom_user_tensor = true;
+ }
+
_ptr = _user_tensor->data();
}
@@ -230,6 +234,20 @@ bool ZeroTensor::can_be_reused() {
return _can_be_reused;
}
+void ZeroTensor::allocate_data() {
+ _logger.debug("ZeroTensor::allocate_data - import the tensor data");
+ _ptr = _user_tensor->data();
+ _mem_ref = ZeroMemPool::get_instance().import_standard_allocation_memory(_init_structs, _ptr, _bytes_capacity);
+}
+
+void ZeroTensor::detach_imported_allocation_for_custom_tensor() {
+ if (_is_custom_user_tensor) {
+ _logger.debug("ZeroTensor::detach_imported_allocation_for_custom_tensor - deallocate the tensor data");
+ _mem_ref = nullptr;
+ _ptr = nullptr;
+ }
+}
+
ZeroTensor::~ZeroTensor() {
_mem_ref = nullptr; // Ensure that zero memory is destroyed before the user tensor is released
}
diff --git a/src/plugins/intel_npu/tests/functional/behavior/batched_tensors_tests/batched_run.hpp b/src/plugins/intel_npu/tests/functional/behavior/batched_tensors_tests/batched_run.hpp
index d48a64e97e1..4038d3d24bd 100644
--- a/src/plugins/intel_npu/tests/functional/behavior/batched_tensors_tests/batched_run.hpp
+++ b/src/plugins/intel_npu/tests/functional/behavior/batched_tensors_tests/batched_run.hpp
@@ -358,6 +358,55 @@ TEST_P(BatchedTensorsRunTests, SetInputDifferentTensorsMultipleInferMCL) {
::operator delete(buffer, std::align_val_t(4096));
}
+TEST_P(BatchedTensorsRunTests, checkResultsAfterRunningWithSameRawMemoryMultipleTimes) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED();
+
+ size_t batch = 4;
+ auto one_shape = Shape{1, 16, 16, 16};
+ auto batch_shape = Shape{batch, 16, 16, 16};
+ auto one_shape_size = ov::shape_size(one_shape);
+ auto model = BatchedTensorsRunTests::create_n_inputs(2, element::f32, batch_shape, "N...");
+ auto execNet = core->compile_model(model, target_device, configuration);
+ ov::InferRequest req = execNet.create_infer_request();
+
+ float* buffer0 = static_cast<float*>(::operator new(one_shape_size * sizeof(float), std::align_val_t(4096)));
+ float* buffer1 = static_cast<float*>(::operator new(one_shape_size * sizeof(float), std::align_val_t(4096)));
+ float* buffer2 = static_cast<float*>(::operator new(one_shape_size * sizeof(float), std::align_val_t(4096)));
+ float* buffer3 = static_cast<float*>(::operator new(one_shape_size * sizeof(float), std::align_val_t(4096)));
+
+ std::vector<ov::Tensor> tensors;
+ tensors.push_back(ov::Tensor(element::f32, one_shape, buffer0));
+ tensors.push_back(ov::Tensor(element::f32, one_shape, buffer1));
+ tensors.push_back(ov::Tensor(element::f32, one_shape, buffer2));
+ tensors.push_back(ov::Tensor(element::f32, one_shape, buffer3));
+
+ req.set_tensors("tensor_input0", tensors);
+
+ for (size_t i = 0; i < 10; i++) {
+ for (size_t j = 0; j < one_shape_size; ++j) {
+ buffer0[j] = static_cast<float>(i);
+ buffer1[j] = static_cast<float>(i);
+ buffer2[j] = static_cast<float>(i);
+ buffer3[j] = static_cast<float>(i);
+ }
+
+ req.infer();
+
+ auto output_tensor = req.get_tensor("tensor_output0");
+ auto* output_data = output_tensor.data<float>();
+ for (size_t j = 0; j < one_shape_size * batch; ++j) {
+ ASSERT_EQ(output_data[j], static_cast<float>(i) + 1.0f)
+ << "Run " << i << ": Expected=" << static_cast<float>(i) + 1.0f << ", actual=" << output_data[j]
+ << " for index " << j;
+ }
+ }
+
+ ::operator delete(buffer0, std::align_val_t(4096));
+ ::operator delete(buffer1, std::align_val_t(4096));
+ ::operator delete(buffer2, std::align_val_t(4096));
+ ::operator delete(buffer3, std::align_val_t(4096));
+}
+
TEST_P(BatchedTensorsRunTests, SetInputDifferentRemoteTensorsMultipleInferMCL) {
SKIP_IF_CURRENT_TEST_IS_DISABLED();
diff --git a/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.cpp b/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.cpp
index 94a198df44b..a65b1afbd33 100644
--- a/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.cpp
+++ b/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.cpp
@@ -52,10 +52,9 @@ INSTANTIATE_TEST_SUITE_P(smoke_BehaviorTest,
::testing::ValuesIn(configsInferRequestRunTests)),
InferRequestRunTests::getTestCaseName);
-const std::vector<ov::AnyMap> batchingConfigs = {
- {ov::log::level(ov::log::Level::WARNING), ov::intel_npu::batch_mode(ov::intel_npu::BatchMode::PLUGIN)},
- {ov::log::level(ov::log::Level::WARNING), ov::intel_npu::batch_mode(ov::intel_npu::BatchMode::COMPILER)},
- {ov::log::level(ov::log::Level::WARNING), ov::intel_npu::batch_mode(ov::intel_npu::BatchMode::AUTO)}};
+const std::vector<ov::AnyMap> batchingConfigs = {{ov::intel_npu::batch_mode(ov::intel_npu::BatchMode::PLUGIN)},
+ {ov::intel_npu::batch_mode(ov::intel_npu::BatchMode::COMPILER)},
+ {ov::intel_npu::batch_mode(ov::intel_npu::BatchMode::AUTO)}};
INSTANTIATE_TEST_SUITE_P(smoke_BehaviorTest,
BatchingRunTests,
diff --git a/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.hpp b/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.hpp
index 9caa8e8ecc7..c273c4dcd21 100644
--- a/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.hpp
+++ b/src/plugins/intel_npu/tests/functional/behavior/ov_infer_request/infer_request_run.hpp
@@ -21,6 +21,7 @@
#include "intel_npu/utils/zero/zero_init.hpp"
#include "intel_npu/utils/zero/zero_types.hpp"
#include "openvino/core/any.hpp"
+#include "openvino/core/log.hpp"
#include "openvino/core/node_vector.hpp"
#include "openvino/opsets/opset8.hpp"
#include "openvino/runtime/compiled_model.hpp"
@@ -35,6 +36,22 @@ using CompilationParams = std::tuple<std::string, // Device name
using ::testing::AllOf;
using ::testing::HasSubstr;
+namespace {
+class LogCallbackGuard {
+public:
+ explicit LogCallbackGuard(const std::function<void(std::string_view)>& callback) {
+ ov::util::set_log_callback(callback);
+ }
+
+ ~LogCallbackGuard() {
+ ov::util::reset_log_callback();
+ }
+
+ LogCallbackGuard(const LogCallbackGuard&) = delete;
+ LogCallbackGuard& operator=(const LogCallbackGuard&) = delete;
+};
+} // namespace
+
namespace ov {
namespace test {
namespace behavior {
@@ -193,8 +210,7 @@ TEST_P(InferRequestRunTests, MultipleExecutorStreamsTestsAsyncInfers) {
using ProfilingBlob = InferRequestRunTests;
TEST_P(ProfilingBlob, NoProfilingCompileProfilingImport) {
- std::shared_ptr<::intel_npu::ZeroInitStructsHolder> initStructs =
- std::make_shared<::intel_npu::ZeroInitStructsHolder>();
+ std::shared_ptr<::intel_npu::ZeroInitStructsHolder> initStructs = ::intel_npu::ZeroInitStructsHolder::getInstance();
if (initStructs->getGraphDdiTable().version() < ZE_MAKE_VERSION(1, 16)) {
std::cout << "Skip since driver extension version is lower than expected\n";
GTEST_SKIP();
@@ -222,8 +238,7 @@ TEST_P(ProfilingBlob, NoProfilingCompileProfilingImport) {
}
TEST_P(ProfilingBlob, ProfilingCompileNoProfilingImport) {
- std::shared_ptr<::intel_npu::ZeroInitStructsHolder> initStructs =
- std::make_shared<::intel_npu::ZeroInitStructsHolder>();
+ std::shared_ptr<::intel_npu::ZeroInitStructsHolder> initStructs = ::intel_npu::ZeroInitStructsHolder::getInstance();
if (initStructs->getGraphDdiTable().version() < ZE_MAKE_VERSION(1, 16)) {
std::cout << "Skip since driver extension version is lower than expected\n";
GTEST_SKIP();
@@ -247,8 +262,7 @@ TEST_P(ProfilingBlob, ProfilingCompileNoProfilingImport) {
}
TEST_P(ProfilingBlob, ProfilingCompileProfilingImport) {
- std::shared_ptr<::intel_npu::ZeroInitStructsHolder> initStructs =
- std::make_shared<::intel_npu::ZeroInitStructsHolder>();
+ std::shared_ptr<::intel_npu::ZeroInitStructsHolder> initStructs = ::intel_npu::ZeroInitStructsHolder::getInstance();
if (initStructs->getGraphDdiTable().version() < ZE_MAKE_VERSION(1, 16)) {
std::cout << "Skip since driver extension version is lower than expected\n";
GTEST_SKIP();
@@ -904,7 +918,7 @@ TEST_P(RunSeqTests, CheckMultipleRunsSeq2) {
return property == intel_npu::run_inferences_sequentially.name();
});
- if (std::make_shared<::intel_npu::ZeroInitStructsHolder>()->getCommandQueueDdiTable().version() >=
+ if (::intel_npu::ZeroInitStructsHolder::getInstance()->getCommandQueueDdiTable().version() >=
ZE_MAKE_VERSION(1, 1)) {
ASSERT_TRUE(isRunInferencesSequentially);
@@ -2107,6 +2121,195 @@ TEST_P(CpuVaTensorsTests, checkResultsAfterStateTensorsUseImportCpuVa1) {
::operator delete(state_data[2], std::align_val_t(4096));
}
+TEST_P(CpuVaTensorsTests, checkResultsAfterRawMemoryIsDestroyedAndReallocatedAfterEachRun) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED();
+
+ auto zeroInit = ::intel_npu::ZeroInitStructsHolder::getInstance();
+ if (!zeroInit->isExternalMemoryStandardAllocationSupported()) {
+ GTEST_SKIP() << "External memory standard allocation is not supported on this platform.";
+ }
+
+ ov::Core internal_core;
+ ov::InferRequest inference_request;
+ std::string logs;
+ std::mutex logs_mutex;
+ auto shape = Shape{1, 16, 16, 16};
+ auto shape_size = ov::shape_size(shape);
+ auto model = createModel(ov::element::f32, shape, "N...");
+
+ // Keep this std::function alive while logging is active.
+ std::function<void(std::string_view)> log_cb = [&](std::string_view msg) {
+ std::lock_guard<std::mutex> lock(logs_mutex);
+ logs.append(msg);
+ logs.push_back('\n');
+ };
+
+ internal_core.set_property(target_device, ov::log::level(ov::log::Level::DEBUG));
+ {
+ // don't flood console with messages from model compilation
+ LogCallbackGuard log_callback_guard(log_cb);
+ ov::CompiledModel compiled_model = internal_core.compile_model(model, target_device, configuration);
+ inference_request = compiled_model.create_infer_request();
+ }
+ logs.clear();
+
+ for (size_t i = 0; i < 10; ++i) {
+ float* input_data = static_cast<float*>(::operator new(shape_size * sizeof(float), std::align_val_t(4096)));
+ float* output_data = static_cast<float*>(::operator new(shape_size * sizeof(float), std::align_val_t(4096)));
+ for (size_t j = 0; j < shape_size; ++j) {
+ input_data[j] = static_cast<float>(i);
+ }
+
+ {
+ LogCallbackGuard log_callback_guard(log_cb);
+ inference_request.set_input_tensor(ov::Tensor{ov::element::f32, shape, input_data});
+ inference_request.set_output_tensor(ov::Tensor{ov::element::f32, shape, output_data});
+ inference_request.infer();
+ }
+
+ ASSERT_NE(logs.find("deallocate the tensor data"), std::string::npos);
+
+ logs.clear();
+
+ for (size_t j = 0; j < shape_size; ++j) {
+ EXPECT_NEAR(output_data[j], input_data[j] + 1.0f, 1e-5)
+ << "Run " << i << ": Expected=" << input_data[j] + 1.0f << ", actual=" << output_data[j]
+ << " for index " << j;
+ }
+
+ ::operator delete(input_data, std::align_val_t(4096));
+ ::operator delete(output_data, std::align_val_t(4096));
+ }
+}
+
+TEST_P(CpuVaTensorsTests, checkResultsAfterRunningWithSameRawMemoryMultipleTimes) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED();
+
+ auto zeroInit = ::intel_npu::ZeroInitStructsHolder::getInstance();
+ if (!zeroInit->isExternalMemoryStandardAllocationSupported()) {
+ GTEST_SKIP() << "External memory standard allocation is not supported on this platform.";
+ }
+
+ ov::Core internal_core;
+ ov::InferRequest inference_request;
+ std::string logs;
+ std::mutex logs_mutex;
+ auto shape = Shape{1, 16, 16, 16};
+ auto shape_size = ov::shape_size(shape);
+ auto model = createModel(ov::element::f32, shape, "N...");
+
+ // Keep this std::function alive while logging is active.
+ std::function<void(std::string_view)> log_cb = [&](std::string_view msg) {
+ std::lock_guard<std::mutex> lock(logs_mutex);
+ logs.append(msg);
+ logs.push_back('\n');
+ };
+
+ float* input_data = static_cast<float*>(::operator new(shape_size * sizeof(float), std::align_val_t(4096)));
+ float* output_data = static_cast<float*>(::operator new(shape_size * sizeof(float), std::align_val_t(4096)));
+
+ internal_core.set_property(target_device, ov::log::level(ov::log::Level::DEBUG));
+ {
+ // don't flood console with messages from model compilation
+ LogCallbackGuard log_callback_guard(log_cb);
+ ov::CompiledModel compiled_model = internal_core.compile_model(model, target_device, configuration);
+ inference_request = compiled_model.create_infer_request();
+ inference_request.set_input_tensor(ov::Tensor{ov::element::f32, shape, input_data});
+ inference_request.set_output_tensor(ov::Tensor{ov::element::f32, shape, output_data});
+ }
+ logs.clear();
+
+ for (size_t i = 0; i < 10; ++i) {
+ for (size_t j = 0; j < shape_size; ++j) {
+ input_data[j] = static_cast<float>(i);
+ }
+
+ {
+ LogCallbackGuard log_callback_guard(log_cb);
+ inference_request.infer();
+ }
+
+ if (i == 0) {
+ ASSERT_EQ(logs.find("import the tensor data"), std::string::npos);
+ } else {
+ ASSERT_NE(logs.find("import the tensor data"), std::string::npos);
+ }
+ ASSERT_NE(logs.find("deallocate the tensor data"), std::string::npos);
+
+ logs.clear();
+
+ for (size_t j = 0; j < shape_size; ++j) {
+ EXPECT_NEAR(output_data[j], input_data[j] + 1.0f, 1e-5)
+ << "Run " << i << ": Expected=" << input_data[j] + 1.0f << ", actual=" << output_data[j]
+ << " for index " << j;
+ }
+ }
+
+ ::operator delete(input_data, std::align_val_t(4096));
+ ::operator delete(output_data, std::align_val_t(4096));
+}
+
+TEST_P(CpuVaTensorsTests, checkResultsAfterRunningWithSameZeroTensorMultipleTimes) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED();
+
+ auto zeroInit = ::intel_npu::ZeroInitStructsHolder::getInstance();
+ if (!zeroInit->isExternalMemoryStandardAllocationSupported()) {
+ GTEST_SKIP() << "External memory standard allocation is not supported on this platform.";
+ }
+
+ ov::Core internal_core;
+ ov::InferRequest inference_request;
+ float *input_data, *output_data;
+ ov::Tensor input_tensor, output_tensor;
+ std::string logs;
+ std::mutex logs_mutex;
+ auto shape = Shape{1, 16, 16, 16};
+ auto shape_size = ov::shape_size(shape);
+ auto model = createModel(ov::element::f32, shape, "N...");
+
+ // Keep this std::function alive while logging is active.
+ std::function<void(std::string_view)> log_cb = [&](std::string_view msg) {
+ std::lock_guard<std::mutex> lock(logs_mutex);
+ logs.append(msg);
+ logs.push_back('\n');
+ };
+
+ internal_core.set_property(target_device, ov::log::level(ov::log::Level::DEBUG));
+ {
+ // don't flood console with messages from model compilation
+ LogCallbackGuard log_callback_guard(log_cb);
+ ov::CompiledModel compiled_model = internal_core.compile_model(model, target_device, configuration);
+ inference_request = compiled_model.create_infer_request();
+ input_tensor = inference_request.get_input_tensor();
+ output_tensor = inference_request.get_output_tensor();
+ input_data = input_tensor.data<float>();
+ output_data = output_tensor.data<float>();
+ }
+ logs.clear();
+
+ for (size_t i = 0; i < 10; ++i) {
+ for (size_t j = 0; j < shape_size; ++j) {
+ input_data[j] = static_cast<float>(i);
+ }
+
+ {
+ LogCallbackGuard log_callback_guard(log_cb);
+ inference_request.infer();
+ }
+
+ ASSERT_EQ(logs.find("import the tensor data"), std::string::npos);
+ ASSERT_EQ(logs.find("deallocate the tensor data"), std::string::npos);
+
+ logs.clear();
+
+ for (size_t j = 0; j < shape_size; ++j) {
+ EXPECT_NEAR(output_data[j], input_data[j] + 1.0f, 1e-5)
+ << "Run " << i << ": Expected=" << input_data[j] + 1.0f << ", actual=" << output_data[j]
+ << " for index " << j;
+ }
+ }
+}
+
} // namespace behavior
} // namespace test
} // namespace ov