Skip to content

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

[NPU] Update graph arg address in case memory was re-imported (#34612)

Bogdan Pereanu8c0995613a05
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 941bdd09c69..e60646ac458 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
@@ -756,6 +756,9 @@ void ZeroInferRequest::prepare_inputs() {
if (levelZeroBuffer == nullptr) {
levelZeroTensor->allocate_data();
levelZeroBuffer = levelZeroTensor->data();
+ _pipeline->update_graph_arguments(_metadata.inputs.at(inputIndex).indexUsedByDriver,
+ levelZeroTensor,
+ i);
}
if (userBuffer != levelZeroBuffer) {
@@ -811,6 +814,7 @@ void ZeroInferRequest::prepare_inputs() {
if (levelZeroBuffer == nullptr) {
levelZeroTensor->allocate_data();
levelZeroBuffer = levelZeroTensor->data();
+ _pipeline->update_graph_arguments(_metadata.inputs.at(inputIndex).indexUsedByDriver, levelZeroTensor);
}
if (userBuffer != levelZeroBuffer) {
@@ -833,6 +837,7 @@ void ZeroInferRequest::prepare_outputs() {
if (levelZeroTensor->data() == nullptr) {
levelZeroTensor->allocate_data();
+ _pipeline->update_graph_arguments(_metadata.outputs.at(outputIndex).indexUsedByDriver, levelZeroTensor);
}
}
}
diff --git a/src/plugins/intel_npu/src/utils/src/zero/zero_tensor.cpp b/src/plugins/intel_npu/src/utils/src/zero/zero_tensor.cpp
index b733cdbf9b0..1ae5f70c545 100644
--- a/src/plugins/intel_npu/src/utils/src/zero/zero_tensor.cpp
+++ b/src/plugins/intel_npu/src/utils/src/zero/zero_tensor.cpp
@@ -6,6 +6,7 @@
#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"
@@ -84,7 +85,8 @@ 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) {
+ if (std::dynamic_pointer_cast<ZeroTensor>(_user_tensor._ptr) == nullptr &&
+ std::dynamic_pointer_cast<ZeroHostTensor>(_user_tensor._ptr) == nullptr) {
_is_custom_user_tensor = true;
}
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 c273c4dcd21..27126ce1e1d 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
@@ -2310,6 +2310,115 @@ TEST_P(CpuVaTensorsTests, checkResultsAfterRunningWithSameZeroTensorMultipleTime
}
}
+TEST_P(CpuVaTensorsTests, checkResultsAfterRunningWithSameZeroHostTensorMultipleTimes) {
+ 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;
+ auto context = internal_core.get_default_context(target_device);
+ 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');
+ };
+
+ ov::Tensor input_tensor = context.create_host_tensor(ov::element::f32, shape);
+ ov::Tensor output_tensor = context.create_host_tensor(ov::element::f32, shape);
+ float* input_data = input_tensor.data<float>();
+ float* output_data = output_tensor.data<float>();
+
+ 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(input_tensor);
+ inference_request.set_output_tensor(output_tensor);
+ }
+ 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;
+ }
+ }
+}
+
+TEST_P(CpuVaTensorsTests, checkResultsAfterRunningWithSameRawMemoryMultipleTimesAllocatingRandomZeroMemoryInBetween) {
+ 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;
+ auto context = internal_core.get_default_context(target_device);
+ auto shape = Shape{1, 16, 16, 16};
+ auto shape_size = ov::shape_size(shape);
+ auto model = createModel(ov::element::f32, shape, "N...");
+ std::vector<ov::Tensor> input_tensor;
+
+ 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)));
+
+ ov::CompiledModel compiled_model = internal_core.compile_model(model, target_device, configuration);
+ auto 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});
+
+ for (size_t i = 0; i < 5; ++i) {
+ for (size_t j = 0; j < shape_size; ++j) {
+ input_data[j] = static_cast<float>(i);
+ }
+
+ input_tensor.push_back(context.create_host_tensor(ov::element::f32, shape));
+ input_tensor.push_back(context.create_host_tensor(ov::element::f32, shape));
+ input_tensor.push_back(context.create_host_tensor(ov::element::f32, shape));
+ input_tensor.push_back(context.create_host_tensor(ov::element::f32, shape));
+
+ OV_ASSERT_NO_THROW(inference_request.infer());
+
+ 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));
+}
+
} // namespace behavior
} // namespace test
} // namespace ov