Skip to content

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

[NPU] Create import_size property for the remote properties (#39466)

Bogdan Pereanu88c3b27602b6
diff --git a/docs/articles_en/assets/snippets/npu_remote_objects_creation.cpp b/docs/articles_en/assets/snippets/npu_remote_objects_creation.cpp
index 71f2011d804..e5d8361320e 100644
--- a/docs/articles_en/assets/snippets/npu_remote_objects_creation.cpp
+++ b/docs/articles_en/assets/snippets/npu_remote_objects_creation.cpp
@@ -57,6 +57,18 @@ int main() {
//! [import_cpu_va]
}
+ {
+ //! [import_cpu_va_import_size]
+ void* standard_allocation = nullptr;
+ // Size in bytes of the whole allocation, page-aligned and not smaller than the tensor size
+ size_t allocation_size = 4096;
+ ov::AnyMap params = {{ov::intel_npu::mem_type.name(), ov::intel_npu::MemType::CPU_VA},
+ {ov::intel_npu::mem_handle.name(), standard_allocation},
+ {ov::intel_npu::import_size.name(), allocation_size}};
+ auto remote_tensor = npu_context.create_tensor(in_element_type, in_shape, params);
+ //! [import_cpu_va_import_size]
+ }
+
{
//! [wrap_dmabuf_fd]
int dma_buf_fd = 0;
diff --git a/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/npu-device/remote-tensor-api-npu-plugin.rst b/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/npu-device/remote-tensor-api-npu-plugin.rst
index 64693ddf64b..e3446963a17 100644
--- a/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/npu-device/remote-tensor-api-npu-plugin.rst
+++ b/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/npu-device/remote-tensor-api-npu-plugin.rst
@@ -121,6 +121,31 @@ For more details, see the code snippets below:
:language: cpp
:fragment: [allocate_level_zero_host]
+Importing More Memory Than the Tensor Size
+++++++++++++++++++++++++++++++++++++++++++
+
+By default, the size of the imported memory is computed from the tensor element type and shape.
+The page-alignment requirement applies to the size of the imported memory, so a tensor whose size is not
+a multiple of the page size cannot be imported from an allocation that is exactly as big as the tensor.
+In such cases, compute the tensor size from its element type and shape, round it up to the next multiple of the page size (4096 bytes),
+and set the ``ov::intel_npu::import_size`` property to that value. For example, a 256-byte tensor needs ``import_size`` of 4096,
+and a 6000-byte tensor needs 8192. To import more memory than that, set a bigger multiple of the page size.
+The allocation must be at least ``import_size`` bytes. The tensor shape and strides are not affected, only the size of the imported memory changes.
+
+.. doxygensnippet:: docs/articles_en/assets/snippets/npu_remote_objects_creation.cpp
+ :language: cpp
+ :fragment: [import_cpu_va_import_size]
+
+The property follows these rules:
+
+* If it is not set, the size is computed from the tensor element type and shape.
+* If it is set, it must not be smaller than the size computed from the tensor element type and shape.
+* For ``CPU_VA``, it must be a multiple of the page size (4096 bytes).
+* It can be set only when ``ov::intel_npu::mem_type`` is ``SHARED_BUF`` or ``CPU_VA``.
+ Setting it for any other memory type, such as ``L0_INTERNAL_BUF`` or ``MMAPED_FILE`` (file mapping is not supported),
+ or without ``ov::intel_npu::mem_type``, results in an error.
+* It can be set only per tensor, in the ``ov::AnyMap`` of ``ov::RemoteContext::create_tensor()``.
+ Setting it when creating the context results in an error.
Limitations
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/src/inference/include/openvino/runtime/intel_npu/remote_properties.hpp b/src/inference/include/openvino/runtime/intel_npu/remote_properties.hpp
index eac2d4e0dfb..10a7fa9ac0f 100644
--- a/src/inference/include/openvino/runtime/intel_npu/remote_properties.hpp
+++ b/src/inference/include/openvino/runtime/intel_npu/remote_properties.hpp
@@ -161,5 +161,25 @@ inline std::istream& operator>>(std::istream& is, TensorType& tensor_type) {
*/
static constexpr Property<TensorType> tensor_type{"TENSOR_TYPE"};
+/**
+ * @brief This key sets the size, in bytes, of the memory to import in a shared memory tensor parameter map.
+ * It allows importing more memory than the tensor itself needs.
+ *
+ * - If not set, the size is computed from the tensor element type and shape.
+ * - If set, it must not be smaller than the size computed from the tensor element type and shape.
+ * - For MemType::CPU_VA the imported size must be a multiple of the standard page size (4096 bytes). If the tensor
+ * size computed from the shape is not, set this property to that size rounded up to the next multiple of the
+ * page size (or to a bigger multiple, to import more memory). The imported allocation must be at least that big.
+ * - It can be set only when @ref mem_type is MemType::SHARED_BUF or MemType::CPU_VA.
+ * Setting it for any other memory type, including MemType::MMAPED_FILE, or without @ref mem_type,
+ * results in an error.
+ * - It can be set only per tensor, when creating a remote tensor. Setting it when creating the context
+ * results in an error.
+ *
+ * The tensor shape and strides are not affected: only the size of the imported memory changes.
+ * @ingroup ov_runtime_level_zero_npu_cpp_api
+ */
+static constexpr Property<std::size_t> import_size{"IMPORT_SIZE"};
+
} // namespace intel_npu
} // namespace ov
diff --git a/src/plugins/intel_npu/src/plugin/src/remote_context.cpp b/src/plugins/intel_npu/src/plugin/src/remote_context.cpp
index 15b478e60d3..edc902aabc1 100644
--- a/src/plugins/intel_npu/src/plugin/src/remote_context.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/remote_context.cpp
@@ -26,6 +26,13 @@ RemoteContextImpl::RemoteContextImpl(const ov::SoPtr<IEngineBackend>& engineBack
_tensor_type_object = zeroUtils::extract_object(remote_properties, tensor_type);
_mem_handle_object = zeroUtils::extract_object(remote_properties, mem_handle);
_file_descriptor_object = zeroUtils::extract_object(remote_properties, file_descriptor);
+
+ // Import_size depends on the memory being imported, so it makes sense only for a specific tensor.
+ if (remote_properties.find(import_size.name()) != remote_properties.end()) {
+ OPENVINO_THROW("Parameter ",
+ import_size.name(),
+ " cannot be set at context level, it can be set only when creating a tensor");
+ }
}
}
@@ -41,6 +48,7 @@ ov::SoPtr<ov::IRemoteTensor> RemoteContextImpl::create_tensor(const ov::element:
std::optional<ov::intel_npu::TensorType> tensor_type_object = std::nullopt;
std::optional<void*> mem_handle_object = std::nullopt;
std::optional<ov::intel_npu::FileDescriptor> file_descriptor_object = std::nullopt;
+ std::optional<std::size_t> import_size_object = std::nullopt;
if (!params.empty()) {
// Save local remote properties.
@@ -48,6 +56,7 @@ ov::SoPtr<ov::IRemoteTensor> RemoteContextImpl::create_tensor(const ov::element:
tensor_type_object = zeroUtils::extract_object(params, tensor_type);
mem_handle_object = zeroUtils::extract_object(params, mem_handle);
file_descriptor_object = zeroUtils::extract_object(params, file_descriptor);
+ import_size_object = zeroUtils::extract_object(params, import_size);
}
// Merge local remote properties with global remote properties.
@@ -65,8 +74,8 @@ ov::SoPtr<ov::IRemoteTensor> RemoteContextImpl::create_tensor(const ov::element:
}
// Mem_type shall be set if any other property is set.
- if (!mem_type_object.has_value() &&
- (mem_handle_object.has_value() || tensor_type_object.has_value() || file_descriptor_object.has_value())) {
+ if (!mem_type_object.has_value() && (mem_handle_object.has_value() || tensor_type_object.has_value() ||
+ file_descriptor_object.has_value() || import_size_object.has_value())) {
OPENVINO_THROW("Parameter ", mem_type.name(), " must be set");
}
@@ -80,6 +89,17 @@ ov::SoPtr<ov::IRemoteTensor> RemoteContextImpl::create_tensor(const ov::element:
OPENVINO_THROW("No parameter ", mem_handle.name(), " found in parameters map");
}
+ // Import_size can be set only if mem_type is a shared memory type or a CPU virtual address.
+ if (import_size_object.has_value() && mem_type_object.value() != MemType::SHARED_BUF &&
+ mem_type_object.value() != MemType::CPU_VA) {
+ OPENVINO_THROW("Parameter ",
+ import_size.name(),
+ " can be set only if ",
+ mem_type.name(),
+ " is SHARED_BUF or CPU_VA. It is not supported for other memory types, including "
+ "MMAPED_FILE");
+ }
+
return {std::make_shared<ZeroRemoteTensor>(get_this_shared_ptr(),
_init_structs,
type,
@@ -87,7 +107,8 @@ ov::SoPtr<ov::IRemoteTensor> RemoteContextImpl::create_tensor(const ov::element:
tensor_type_object.value_or(ov::intel_npu::TensorType::BINDED),
mem_type_object.value_or(ov::intel_npu::MemType::L0_INTERNAL_BUF),
mem_handle_object.value_or(nullptr),
- file_descriptor_object)};
+ file_descriptor_object,
+ import_size_object)};
}
ov::SoPtr<ov::ITensor> RemoteContextImpl::create_host_tensor(const ov::element::Type type, const ov::Shape& shape) {
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 d70d6fdb441..64d891e8e01 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
@@ -27,7 +27,8 @@ public:
ov::intel_npu::TensorType zero_tensor_type = ov::intel_npu::TensorType::BINDED,
ov::intel_npu::MemType memory_type = ov::intel_npu::MemType::L0_INTERNAL_BUF,
const void* memory = nullptr,
- const std::optional<ov::intel_npu::FileDescriptor>& file_desc = std::nullopt);
+ const std::optional<ov::intel_npu::FileDescriptor>& file_desc = std::nullopt,
+ std::optional<size_t> import_size = std::nullopt);
/**
* @brief Returns additional information associated with tensor
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 919dd16a4e1..23e9f368dd0 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
@@ -212,7 +212,8 @@ ZeroRemoteTensor::ZeroRemoteTensor(const std::shared_ptr<ov::IRemoteContext>& co
TensorType zero_tensor_type,
MemType memory_type,
const void* memory,
- const std::optional<FileDescriptor>& file_desc)
+ const std::optional<FileDescriptor>& file_desc,
+ std::optional<size_t> import_size)
: _context(context),
_init_structs(init_structs),
_element_type(element_type),
@@ -229,7 +230,20 @@ 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);
- allocate(*byte_size);
+ if (import_size.has_value()) {
+ OPENVINO_ASSERT(*import_size >= *byte_size,
+ "Import size (",
+ *import_size,
+ " bytes) is smaller than the tensor size (",
+ *byte_size,
+ " bytes) for type: ",
+ element_type,
+ " and shape: ",
+ shape);
+ }
+
+ // import_size is only accepted for imported memory, so other memory types always use the tensor size
+ allocate(import_size.value_or(*byte_size));
}
const ov::element::Type& ZeroRemoteTensor::get_element_type() const {
diff --git a/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/remote_run.cpp b/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/remote_run.cpp
index 2f03c18f07c..76e2bf08f4d 100644
--- a/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/remote_run.cpp
+++ b/src/plugins/intel_npu/tests/functional/behavior/remote_tensor_tests/remote_run.cpp
@@ -637,6 +637,185 @@ TEST_P(RemoteRunTests, ImportCpuVAUsingStandardRemoteTensorAPI) {
}
}
+TEST_P(RemoteRunTests, ImportCpuVAWithImportSizeBiggerThanTensor) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ if (std::make_shared<::intel_npu::ZeroInitStructsHolder>()->isExternalMemoryStandardAllocationSupported()) {
+ auto shape = Shape{1, 1024};
+ auto shape_size = ov::shape_size(shape);
+ auto model = createModel(element::f32, shape, "N...");
+
+ // import twice the memory required by the tensor
+ const size_t import_size = 2 * shape_size * sizeof(float);
+ auto data = ::operator new(import_size, std::align_val_t(4096));
+ for (size_t i = 0; i < shape_size; ++i) {
+ static_cast<float*>(data)[i] = 5.f;
+ }
+
+ ov::CompiledModel compiled_model;
+ ov::InferRequest inference_request;
+
+ auto context = core->get_default_context(target_device);
+ OV_ASSERT_NO_THROW(compiled_model = core->compile_model(model, target_device, configuration));
+ OV_ASSERT_NO_THROW(inference_request = compiled_model.create_infer_request());
+
+ ov::AnyMap params = {{ov::intel_npu::mem_type.name(), ov::intel_npu::MemType::CPU_VA},
+ {ov::intel_npu::mem_handle.name(), data},
+ {ov::intel_npu::import_size.name(), import_size}};
+ ov::RemoteTensor remote_tensor;
+ OV_ASSERT_NO_THROW(remote_tensor = context.create_tensor(ov::element::f32, shape, params));
+ EXPECT_EQ(remote_tensor.get_shape(), shape);
+
+ OV_ASSERT_NO_THROW(inference_request.set_input_tensor(remote_tensor));
+ OV_ASSERT_NO_THROW(inference_request.infer());
+
+ auto output_tensor = inference_request.get_output_tensor();
+ float* output_tensor_data = reinterpret_cast<float*>(output_tensor.data());
+
+ float expected_result = 6.0f;
+ for (size_t j = 0; j < output_tensor.get_size(); ++j) {
+ EXPECT_NEAR(output_tensor_data[j], expected_result, 1e-5)
+ << " Expected=" << expected_result << ", actual=" << output_tensor_data[j] << " for index " << j;
+ }
+
+ inference_request = {};
+ remote_tensor = {};
+
+ ::operator delete(data, std::align_val_t(4096));
+ } else {
+ GTEST_SKIP() << "Standard allocation is not supported by the driver";
+ }
+}
+
+TEST_P(RemoteRunTests, ImportCpuVAUnalignedTensorSizeWithAlignedImportSize) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ if (std::make_shared<::intel_npu::ZeroInitStructsHolder>()->isExternalMemoryStandardAllocationSupported()) {
+ // tensor size (256 bytes) is not aligned to the page size, so importing it without import_size fails
+ auto shape = Shape{1, 64};
+ auto shape_size = ov::shape_size(shape);
+ auto model = createModel(element::f32, shape, "N...");
+
+ const size_t import_size = 4096;
+ auto data = ::operator new(import_size, std::align_val_t(4096));
+ for (size_t i = 0; i < shape_size; ++i) {
+ static_cast<float*>(data)[i] = 5.f;
+ }
+
+ ov::CompiledModel compiled_model;
+ ov::InferRequest inference_request;
+
+ auto context = core->get_default_context(target_device);
+ OV_ASSERT_NO_THROW(compiled_model = core->compile_model(model, target_device, configuration));
+ OV_ASSERT_NO_THROW(inference_request = compiled_model.create_infer_request());
+
+ ov::AnyMap params = {{ov::intel_npu::mem_type.name(), ov::intel_npu::MemType::CPU_VA},
+ {ov::intel_npu::mem_handle.name(), data}};
+ ASSERT_THROW(auto remote_tensor = context.create_tensor(ov::element::f32, shape, params), std::exception);
+
+ params[ov::intel_npu::import_size.name()] = import_size;
+ ov::RemoteTensor remote_tensor;
+ OV_ASSERT_NO_THROW(remote_tensor = context.create_tensor(ov::element::f32, shape, params));
+ EXPECT_EQ(remote_tensor.get_shape(), shape);
+
+ OV_ASSERT_NO_THROW(inference_request.set_input_tensor(remote_tensor));
+ OV_ASSERT_NO_THROW(inference_request.infer());
+
+ auto output_tensor = inference_request.get_output_tensor();
+ float* output_tensor_data = reinterpret_cast<float*>(output_tensor.data());
+
+ float expected_result = 6.0f;
+ for (size_t j = 0; j < output_tensor.get_size(); ++j) {
+ EXPECT_NEAR(output_tensor_data[j], expected_result, 1e-5)
+ << " Expected=" << expected_result << ", actual=" << output_tensor_data[j] << " for index " << j;
+ }
+
+ inference_request = {};
+ remote_tensor = {};
+
+ ::operator delete(data, std::align_val_t(4096));
+ } else {
+ GTEST_SKIP() << "Standard allocation is not supported by the driver";
+ }
+}
+
+TEST_P(RemoteRunTests, ImportCpuVAWithImportSizeSmallerThanTensorExpectedError) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ auto shape = Shape{1, 1024};
+ const size_t tensor_size = ov::shape_size(shape) * sizeof(float);
+ auto data = ::operator new(tensor_size, std::align_val_t(4096));
+
+ auto context = core->get_default_context(target_device);
+
+ ov::AnyMap params = {{ov::intel_npu::mem_type.name(), ov::intel_npu::MemType::CPU_VA},
+ {ov::intel_npu::mem_handle.name(), data},
+ {ov::intel_npu::import_size.name(), tensor_size / 2}};
+ OV_EXPECT_THROW_HAS_SUBSTRING(context.create_tensor(ov::element::f32, shape, params),
+ ov::Exception,
+ "is smaller than the tensor size");
+ ::operator delete(data, std::align_val_t(4096));
+}
+
+TEST_P(RemoteRunTests, ImportSizeWithoutMemTypeExpectedError) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ auto shape = Shape{1, 1024};
+ const size_t tensor_size = ov::shape_size(shape) * sizeof(float);
+
+ auto context = core->get_default_context(target_device);
+
+ ov::AnyMap params = {{ov::intel_npu::import_size.name(), tensor_size}};
+ ASSERT_THROW(auto remote_tensor = context.create_tensor(ov::element::f32, shape, params), std::exception);
+}
+
+TEST_P(RemoteRunTests, ImportCpuVAWithUnalignedImportSizeExpectedError) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ auto shape = Shape{1, 64};
+ auto data = ::operator new(8192, std::align_val_t(4096));
+
+ auto context = core->get_default_context(target_device);
+
+ // bigger than the tensor size, but not a multiple of the page size
+ ov::AnyMap params = {{ov::intel_npu::mem_type.name(), ov::intel_npu::MemType::CPU_VA},
+ {ov::intel_npu::mem_handle.name(), data},
+ {ov::intel_npu::import_size.name(), size_t{4096 + 64}}};
+ ASSERT_THROW(auto remote_tensor = context.create_tensor(ov::element::f32, shape, params), std::exception);
+
+ ::operator delete(data, std::align_val_t(4096));
+}
+
+TEST_P(RemoteRunTests, ImportSizeSetInContextExpectedError) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ ov::AnyMap params = {{ov::intel_npu::mem_type.name(), ov::intel_npu::MemType::CPU_VA},
+ {ov::intel_npu::import_size.name(), size_t{4096}}};
+ ASSERT_THROW(auto context = core->create_context(target_device, params), std::exception);
+}
+
+TEST_P(RemoteRunTests, ImportSizeWithNonImportMemTypeExpectedError) {
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ auto shape = Shape{1, 1024};
+ const size_t tensor_size = ov::shape_size(shape) * sizeof(float);
+
+ auto context = core->get_default_context(target_device);
+
+ ov::AnyMap params = {{ov::intel_npu::mem_type.name(), ov::intel_npu::MemType::L0_INTERNAL_BUF},
+ {ov::intel_npu::import_size.name(), tensor_size}};
+ OV_EXPECT_THROW_HAS_SUBSTRING(context.create_tensor(ov::element::f32, shape, params),
+ ov::Exception,
+ "is SHARED_BUF or CPU_VA");
+
+ ov::AnyMap file_params = {{ov::intel_npu::mem_type.name(), ov::intel_npu::MemType::MMAPED_FILE},
+ {ov::intel_npu::file_descriptor.name(), ov::intel_npu::FileDescriptor("dummy_file.bin")},
+ {ov::intel_npu::import_size.name(), tensor_size}};
+ OV_EXPECT_THROW_HAS_SUBSTRING(context.create_tensor(ov::element::f32, shape, file_params),
+ ov::Exception,
+ "is SHARED_BUF or CPU_VA");
+}
+
TEST_P(RemoteRunTests, CheckRemoteTensorInternalBufSetPropertyInContext) {
// Skip test according to plugin specific disabled_test_patterns() (if any)
SKIP_IF_CURRENT_TEST_IS_DISABLED()