Skip to content

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

[GPU] Enable Linux VA-surface remote-tensor caching (#38686)

Aleksandr Voronb458a6b82e54
diff --git a/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device/remote-tensor-api-gpu-plugin.rst b/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device/remote-tensor-api-gpu-plugin.rst
index f0d09e97b9e..257d266d5e5 100644
--- a/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device/remote-tensor-api-gpu-plugin.rst
+++ b/docs/articles_en/openvino-workflow/running-inference/inference-devices-and-modes/gpu-device/remote-tensor-api-gpu-plugin.rst
@@ -504,6 +504,26 @@ pointers or the ``VASurfaceID`` handle, as shown in the examples below:
infer_request.start_async();
infer_request.wait();
+.. important::
+
+ The application is responsible for the lifetime of the ``VADisplay`` and every ``VASurfaceID`` passed to the
+ plugin. Keep the exact VA surface allocation valid until both of the following conditions are met:
+
+ * All remote tensors created from the surface, including their copies, are destroyed.
+ * Every ``ov::InferRequest`` to which such a tensor was passed is destroyed, or has completed an inference after
+ a replacement tensor was assigned to the same input.
+
+ If a remote tensor is passed to the ``AUTO`` or ``MULTI`` plugin, keep the VA surface valid until the compiled
+ model is destroyed. These virtual-device plugins use internal infer requests whose tensor references
+ are not exposed to the application.
+
+ Do not destroy a VA surface or allow its numeric ID to be recycled before these observable conditions are met.
+ Deleting a shared VA surface earlier makes subsequent use of its OpenCL memory object undefined, as specified
+ by the ``cl_intel_va_api_media_sharing`` extension.
+
+ Pixel contents may change between inference calls while the same surface allocation remains alive. Synchronize VA
+ writes before inference; the plugin acquires and releases the shared surface through the OpenCL
+ interoperability API for every execution.
.. important::
diff --git a/src/inference/include/openvino/runtime/intel_gpu/ocl/va.hpp b/src/inference/include/openvino/runtime/intel_gpu/ocl/va.hpp
index 3a6763805c2..063ea146e16 100644
--- a/src/inference/include/openvino/runtime/intel_gpu/ocl/va.hpp
+++ b/src/inference/include/openvino/runtime/intel_gpu/ocl/va.hpp
@@ -120,6 +120,12 @@ public:
* @param width A width of Y plane
* @param nv12_surf NV12 `VASurfaceID` to create NV12 from
* @return A pair of remote tensors for each plane
+ * @note The application must keep the `VADisplay` and the exact VA surface allocation identified by
+ * `nv12_surf` valid until both returned tensors and all their copies are destroyed. For every infer request to
+ * which either tensor was passed, also keep the surface valid until the request is destroyed or an inference using
+ * a replacement tensor has completed. If either tensor is passed to the AUTO or MULTI plugin, keep the surface
+ * valid until the compiled model is destroyed because its internal requests are not exposed to the
+ * application. Destroying the surface or recycling its ID earlier results in undefined behavior.
*/
std::pair<VASurfaceTensor, VASurfaceTensor> create_tensor_nv12(const size_t height,
const size_t width,
@@ -140,6 +146,12 @@ public:
* @param surface A `VASurfaceID` to create remote tensor from
* @param plane An index of a plane inside `VASurfaceID` to create tensor from
* @return A remote tensor wrapping `VASurfaceID`
+ * @note The application must keep the `VADisplay` and the exact VA surface allocation identified by `surface`
+ * valid until the returned tensor and all its copies are destroyed. For every infer request to which the tensor was
+ * passed, also keep the surface valid until the request is destroyed or an inference using a replacement tensor has
+ * completed. If the tensor is passed to the AUTO or MULTI plugin, keep the surface valid until the compiled model
+ * is destroyed because its internal requests are not exposed to the application. Destroying the surface or
+ * recycling its ID earlier results in undefined behavior.
*/
inline VASurfaceTensor create_tensor(const element::Type type,
const Shape& shape,
diff --git a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp b/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp
index 318f1b34fce..46acf462933 100644
--- a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp
+++ b/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp
@@ -477,11 +477,7 @@ bool RemoteTensorImpl::is_shared() const noexcept {
}
bool RemoteTensorImpl::supports_caching() const {
-#ifdef _WIN32
return is_shared() && !m_mapped_memory;
-#else
- return is_shared() && !m_mapped_memory && m_mem_type != TensorType::BT_SURF_SHARED;
-#endif
}
void RemoteTensorImpl::update_hash() {
diff --git a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/helpers.hpp b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/helpers.hpp
index bd428e61b2e..901b7c03f43 100644
--- a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/helpers.hpp
+++ b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/helpers.hpp
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
//
+#include <algorithm>
#include <string>
#include <utility>
#include <vector>
@@ -259,6 +260,71 @@ struct VADevice {
return status == VA_STATUS_SUCCESS ? surface : VA_INVALID_SURFACE;
}
+ bool fill_nv12_surface(VASurfaceID surface,
+ size_t width,
+ size_t height,
+ uint8_t y_value,
+ uint8_t u_value,
+ uint8_t v_value) const {
+ if (vaSyncSurface(_display, surface) != VA_STATUS_SUCCESS)
+ return false;
+
+ VAImageFormat format{};
+ format.fourcc = VA_FOURCC_NV12;
+ format.byte_order = VA_LSB_FIRST;
+ format.bits_per_pixel = 12;
+
+ VAImage image{};
+ if (vaCreateImage(_display,
+ &format,
+ static_cast<int>(width),
+ static_cast<int>(height),
+ &image) != VA_STATUS_SUCCESS) {
+ return false;
+ }
+
+ void* data = nullptr;
+ if (vaMapBuffer(_display, image.buf, &data) != VA_STATUS_SUCCESS) {
+ vaDestroyImage(_display, image.image_id);
+ return false;
+ }
+
+ const bool valid_image = image.num_planes >= 2 && image.format.fourcc == VA_FOURCC_NV12;
+ if (valid_image) {
+ auto* base = static_cast<uint8_t*>(data);
+ for (size_t row = 0; row < height; ++row)
+ std::fill_n(base + image.offsets[0] + row * image.pitches[0], width, y_value);
+
+ for (size_t row = 0; row < height / 2; ++row) {
+ auto* uv_row = base + image.offsets[1] + row * image.pitches[1];
+ for (size_t column = 0; column < width; column += 2) {
+ uv_row[column] = u_value;
+ uv_row[column + 1] = v_value;
+ }
+ }
+ }
+
+ const auto unmap_status = vaUnmapBuffer(_display, image.buf);
+ auto put_status = VA_STATUS_ERROR_OPERATION_FAILED;
+ if (unmap_status == VA_STATUS_SUCCESS) {
+ put_status = vaPutImage(_display,
+ surface,
+ image.image_id,
+ 0,
+ 0,
+ static_cast<unsigned int>(width),
+ static_cast<unsigned int>(height),
+ 0,
+ 0,
+ static_cast<unsigned int>(width),
+ static_cast<unsigned int>(height));
+ }
+ const auto destroy_status = vaDestroyImage(_display, image.image_id);
+ const auto sync_status = vaSyncSurface(_display, surface);
+ return valid_image && unmap_status == VA_STATUS_SUCCESS && put_status == VA_STATUS_SUCCESS &&
+ destroy_status == VA_STATUS_SUCCESS && sync_status == VA_STATUS_SUCCESS;
+ }
+
// Releases the surface and invalidates the passed id
void destroy_surface(VASurfaceID& surface) const {
if (surface == VA_INVALID_SURFACE)
diff --git a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/va_remote_tensor_tests.cpp b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/va_remote_tensor_tests.cpp
index b693699e151..7c90a2db403 100644
--- a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/va_remote_tensor_tests.cpp
+++ b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/va_remote_tensor_tests.cpp
@@ -5,6 +5,7 @@
#if (defined(OV_GPU_WITH_OCL_RT) || defined(OV_GPU_WITH_ZE_RT)) && !defined(_WIN32) && defined(ENABLE_LIBVA) && defined(ENABLE_LIBVA_DRM)
#include <algorithm>
+#include <numeric>
#include <string>
#include <vector>
@@ -127,17 +128,15 @@ TEST_F(OVRemoteTensorVA_Test, smoke_repeated_import_of_the_same_surface) {
}
}
-// Surface imports are excluded from the remote context memory cache
-// so every import creates a new cl_mem for the surface.
-TEST_F(OVRemoteTensorVA_Test, smoke_surface_import_is_not_cached) {
+TEST_F(OVRemoteTensorVA_Test, smoke_surface_import_is_cached_while_alive) {
ov::Core core;
ov::intel_gpu::ocl::VAContext context(core, va_device.get());
auto first = context.create_tensor_nv12(surface_height, surface_width, surface);
auto second = context.create_tensor_nv12(surface_height, surface_width, surface);
- ASSERT_NE(first.first.get(), second.first.get());
- ASSERT_NE(first.second.get(), second.second.get());
+ ASSERT_EQ(first.first.get(), second.first.get());
+ ASSERT_EQ(first.second.get(), second.second.get());
// Both imports still describe the very same surface
ASSERT_EQ(static_cast<VASurfaceID>(second.first), static_cast<VASurfaceID>(first.first));
@@ -182,13 +181,45 @@ TEST_F(OVRemoteTensorVA_Test, smoke_recycled_surface_id_is_reimported) {
ov::Core core;
ov::intel_gpu::ocl::VAContext context(core, va_device.get());
+ auto parameter = std::make_shared<ov::op::v0::Parameter>(
+ ov::element::u8,
+ ov::Shape{1, 3, surface_height, surface_width});
+ auto model = std::make_shared<ov::Model>(ov::OutputVector{parameter}, ov::ParameterVector{parameter});
+
+ using namespace ov::preprocess;
+ auto preprocessor = PrePostProcessor(model);
+ preprocessor.input().tensor().set_element_type(ov::element::u8)
+ .set_color_format(ColorFormat::NV12_TWO_PLANES, {"y", "uv"})
+ .set_memory_type(ov::intel_gpu::memory_type::surface);
+ preprocessor.input().preprocess().convert_color(ColorFormat::BGR);
+ preprocessor.input().model().set_layout("NCHW");
+ model = preprocessor.build();
+
+ auto compiled_model = core.compile_model(model, context);
+ auto request = compiled_model.create_infer_request();
+ const auto infer_mean = [&](VASurfaceID input_surface) {
+ auto nv12 = context.create_tensor_nv12(surface_height, surface_width, input_surface);
+ request.set_input_tensor(0, nv12.first);
+ request.set_input_tensor(1, nv12.second);
+ request.infer();
+
+ auto output = request.get_output_tensor();
+ const auto* output_data = output.data<const uint8_t>();
+ return std::accumulate(output_data, output_data + output.get_size(), 0.0) / output.get_size();
+ };
+
const VASurfaceID original_id = surface;
- // The original import is kept alive, so the driver can't reuse its cl_mem for the new import
- auto original = context.create_tensor_nv12(surface_height, surface_width, surface);
- const auto original_mem_y = original.first.get();
- const auto original_mem_uv = original.second.get();
+ ASSERT_TRUE(va_device.fill_nv12_surface(surface, surface_width, surface_height, 16, 128, 128));
+ const auto original_mean = infer_mean(surface);
- // Emulate decoder teardown and recreation until VA-API reassigns the released id
+ VASurfaceID replacement_surface = va_device.create_nv12_surface(surface_width, surface_height);
+ ASSERT_NE(replacement_surface, VA_INVALID_SURFACE);
+ ASSERT_NE(replacement_surface, original_id);
+ ASSERT_TRUE(va_device.fill_nv12_surface(replacement_surface, surface_width, surface_height, 128, 128, 128));
+ infer_mean(replacement_surface);
+
+ // Keep the request and compiled model alive, but process a replacement input before destroying the old surface.
+ // This releases the request and plugin references to the old import, so its weak cache entry must expire.
bool is_recycled = false;
const size_t max_attempts = 8;
for (size_t attempt = 0; attempt < max_attempts && !is_recycled; attempt++) {
@@ -198,18 +229,16 @@ TEST_F(OVRemoteTensorVA_Test, smoke_recycled_surface_id_is_reimported) {
is_recycled = surface == original_id;
}
- if (!is_recycled)
+ if (!is_recycled) {
+ va_device.destroy_surface(replacement_surface);
GTEST_SKIP() << "VA-API did not reassign the released surface id, cache aliasing can't be exercised";
+ }
- auto reimported = context.create_tensor_nv12(surface_height, surface_width, surface);
+ ASSERT_TRUE(va_device.fill_nv12_surface(surface, surface_width, surface_height, 235, 128, 128));
+ const auto reimported_mean = infer_mean(surface);
+ va_device.destroy_surface(replacement_surface);
- // The new surface produces the same cache key as the destroyed one
- ASSERT_EQ(static_cast<VASurfaceID>(reimported.first), original_id);
- ASSERT_EQ(reimported.first.get_shape(), original.first.get_shape());
- ASSERT_EQ(reimported.second.get_shape(), original.second.get_shape());
- // it must be imported from scratch instead of being served from the cache
- ASSERT_NE(reimported.first.get(), original_mem_y);
- ASSERT_NE(reimported.second.get(), original_mem_uv);
+ ASSERT_GT(reimported_mean, original_mean + 200.0);
}
#endif // (OV_GPU_WITH_OCL_RT || OV_GPU_WITH_ZE_RT) && !_WIN32 && ENABLE_LIBVA && ENABLE_LIBVA_DRM