Skip to content

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

[GPU] tensor from nthandle (#35543)

Michal Miotkeb884f5d506b
diff --git a/docs/articles_en/assets/snippets/gpu/remote_objects_creation.cpp b/docs/articles_en/assets/snippets/gpu/remote_objects_creation.cpp
index 35e2c86af16..b2a7cb4170e 100644
--- a/docs/articles_en/assets/snippets/gpu/remote_objects_creation.cpp
+++ b/docs/articles_en/assets/snippets/gpu/remote_objects_creation.cpp
@@ -1,3 +1,7 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
#include <openvino/runtime/core.hpp>
#include <openvino/runtime/intel_gpu/properties.hpp>
#include <openvino/runtime/intel_gpu/ocl/ocl.hpp>
@@ -14,6 +18,7 @@ cl_context get_cl_context();
cl_command_queue get_cl_queue();
cl::Buffer allocate_buffer(size_t size);
cl::Image2D allocate_image(size_t size);
+ov::intel_gpu::ocl::os_handle_param get_shared_handle();
#ifdef WIN32
@@ -62,6 +67,16 @@ int main() {
//! [wrap_cl_image]
}
+{
+ //! [wrap_shared_handle]
+ auto shared_handle = get_shared_handle();
+ auto remote_tensor = gpu_context.create_tensor(in_element_type,
+ in_shape,
+ shared_handle,
+ ov::intel_gpu::MemType::SHARED_BUF);
+ //! [wrap_shared_handle]
+}
+
{
//! [allocate_usm_device]
auto remote_tensor = gpu_context.create_usm_device_tensor(in_element_type, in_shape);
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 adac7c64a9e..8a014404459 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
@@ -254,6 +254,19 @@ For more details, see the code snippets below:
:language: cpp
:fragment: [wrap_cl_image]
+ .. tab-item:: external shared handle
+ :sync: external-shared-handle
+
+ Use this overload when your application already owns an OS-level shared memory handle
+ (for example, DX12 NT handle on Windows or DMA-BUF file descriptor on Linux).
+
+ .. doxygensnippet:: docs/articles_en/assets/snippets/gpu/remote_objects_creation.cpp
+ :language: cpp
+ :fragment: [wrap_shared_handle]
+
+ The ``shape`` and ``element type`` must describe the same memory layout as the external buffer.
+ The handle must remain valid for the whole lifetime of the created remote tensor.
+
.. tab-item:: biplanar NV12 surface
:sync: biplanar-nv12-surface
diff --git a/src/inference/include/openvino/runtime/intel_gpu/ocl/ocl.hpp b/src/inference/include/openvino/runtime/intel_gpu/ocl/ocl.hpp
index da8c296db76..a7e743d76f8 100644
--- a/src/inference/include/openvino/runtime/intel_gpu/ocl/ocl.hpp
+++ b/src/inference/include/openvino/runtime/intel_gpu/ocl/ocl.hpp
@@ -38,8 +38,19 @@ namespace ocl {
* @brief Shortcut for defining a handle parameter
* @ingroup ov_runtime_ocl_gpu_cpp_api
*/
+
using gpu_handle_param = void*;
+/**
+ * @brief Shortcut for defining a HANDLE on windows or file descriptor on linux
+ * @ingroup ov_runtime_ocl_gpu_cpp_api
+ */
+#ifdef __linux__
+using os_handle_param = int;
+#else
+using os_handle_param = void*;
+#endif
+
/**
* @brief This class represents an abstraction for GPU plugin remote tensor
* which can be shared with user-supplied OpenCL buffer.
@@ -58,6 +69,7 @@ public:
{{std::string(ov::intel_gpu::mem_handle.name()), {}},
{std::string(ov::intel_gpu::shared_mem_type.name()),
{ov::Any(ov::intel_gpu::SharedMemType::OCL_BUFFER).as<std::string>(),
+ ov::Any(ov::intel_gpu::SharedMemType::BUFFER_FROM_HANDLE).as<std::string>(),
ov::Any(ov::intel_gpu::SharedMemType::DX_BUFFER).as<std::string>()}}});
}
@@ -307,6 +319,30 @@ public:
return create_tensor(type, shape, params).as<ClImage2DTensor>();
}
+ /**
+ * @brief This function is used to obtain a remote tensor object from a user-supplied external memory handle
+ * The API mirrors the NPU pointer-based create_tensor form.
+ * @param type Tensor element type
+ * @param shape Tensor shape
+ * @param shared_buffer External memory handle from another API (DX12 shared NT handle on Windows passed as void*,
+ * DMA-BUF fd on Linux passed as int)
+ * @param memory_type Memory type to use; only MemType::SHARED_BUF is currently supported
+ * @return A remote tensor instance
+ */
+ ClBufferTensor create_tensor(const element::Type type,
+ const Shape& shape,
+ os_handle_param shared_buffer,
+ const MemType memory_type) {
+#ifndef __linux__
+ OPENVINO_ASSERT(shared_buffer != nullptr, "shared_buffer must not be nullptr for SHARED_BUF memory type");
+#endif
+ OPENVINO_ASSERT(memory_type == MemType::SHARED_BUF,
+ "Only SHARED_BUF memory type is supported for raw buffer pointer or NT handle");
+ AnyMap params = {{ov::intel_gpu::shared_mem_type.name(), ov::intel_gpu::SharedMemType::BUFFER_FROM_HANDLE},
+ {ov::intel_gpu::os_handle.name(), shared_buffer}};
+ return create_tensor(type, shape, params).as<ClBufferTensor>();
+ }
+
/**
* @brief This function is used to obtain remote tensor object from user-supplied USM pointer
* @param type Tensor element type
diff --git a/src/inference/include/openvino/runtime/intel_gpu/remote_properties.hpp b/src/inference/include/openvino/runtime/intel_gpu/remote_properties.hpp
index c44c2d2f0d5..977580aef39 100644
--- a/src/inference/include/openvino/runtime/intel_gpu/remote_properties.hpp
+++ b/src/inference/include/openvino/runtime/intel_gpu/remote_properties.hpp
@@ -17,6 +17,12 @@ namespace intel_gpu {
using gpu_handle_param = void*;
+#ifdef __linux__
+using os_handle_param = int;
+#else
+using os_handle_param = void*;
+#endif
+
/**
* @brief Enum to define the type of the shared context
* @ingroup ov_runtime_ocl_gpu_cpp_api
@@ -103,13 +109,23 @@ static constexpr Property<gpu_handle_param> va_device{"VA_DEVICE"};
* @ingroup ov_runtime_ocl_gpu_cpp_api
*/
enum class SharedMemType {
- OCL_BUFFER = 0, //!< Shared OpenCL buffer blob
- OCL_IMAGE2D = 1, //!< Shared OpenCL 2D image blob
- USM_USER_BUFFER = 2, //!< Shared USM pointer allocated by user
- USM_HOST_BUFFER = 3, //!< Shared USM pointer type with host allocation type allocated by plugin
- USM_DEVICE_BUFFER = 4, //!< Shared USM pointer type with device allocation type allocated by plugin
- VA_SURFACE = 5, //!< Shared video decoder surface or D3D 2D texture blob
- DX_BUFFER = 6 //!< Shared D3D buffer blob
+ OCL_BUFFER = 0, //!< Shared OpenCL buffer blob
+ OCL_IMAGE2D = 1, //!< Shared OpenCL 2D image blob
+ USM_USER_BUFFER = 2, //!< Shared USM pointer allocated by user
+ USM_HOST_BUFFER = 3, //!< Shared USM pointer type with host allocation type allocated by plugin
+ USM_DEVICE_BUFFER = 4, //!< Shared USM pointer type with device allocation type allocated by plugin
+ VA_SURFACE = 5, //!< Shared video decoder surface or D3D 2D texture blob
+ DX_BUFFER = 6, //!< Shared D3D buffer blob
+ BUFFER_FROM_HANDLE = 7, //!< OS-level external memory handle (e.g. DX12 NT handle on Windows,
+ //!< DMA-BUF fd on Linux) imported by the plugin into a cl_mem
+};
+
+/**
+ * @brief Enum to define memory type for pointer-based tensor sharing API.
+ * @ingroup ov_runtime_ocl_gpu_cpp_api
+ */
+enum class MemType {
+ SHARED_BUF = 0, //!< Shared OpenCL buffer handle passed as void* or int
};
/** @cond INTERNAL */
@@ -129,6 +145,8 @@ inline std::ostream& operator<<(std::ostream& os, const SharedMemType& share_mem
return os << "VA_SURFACE";
case SharedMemType::DX_BUFFER:
return os << "DX_BUFFER";
+ case SharedMemType::BUFFER_FROM_HANDLE:
+ return os << "BUFFER_FROM_HANDLE";
default:
OPENVINO_THROW("Unsupported memory type");
}
@@ -151,6 +169,8 @@ inline std::istream& operator>>(std::istream& is, SharedMemType& share_mem_type)
share_mem_type = SharedMemType::VA_SURFACE;
} else if (str == "DX_BUFFER") {
share_mem_type = SharedMemType::DX_BUFFER;
+ } else if (str == "BUFFER_FROM_HANDLE") {
+ share_mem_type = SharedMemType::BUFFER_FROM_HANDLE;
} else {
OPENVINO_THROW("Unsupported memory type: ", str);
}
@@ -172,6 +192,12 @@ static constexpr Property<SharedMemType> shared_mem_type{"SHARED_MEM_TYPE"};
*/
static constexpr Property<gpu_handle_param> mem_handle{"MEM_HANDLE"};
+/**
+ * @brief This key identifies system memory handle (fd on Linux, NT handle on Windows)
+ * @ingroup ov_runtime_ocl_gpu_cpp_api
+ */
+static constexpr Property<os_handle_param> os_handle{"OS_HANDLE"};
+
/**
* @brief This key identifies video decoder surface handle
* in a shared memory blob parameter map
diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/common_utils.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/common_utils.hpp
index 90066acfc64..a5f1d9e3379 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/common_utils.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/common_utils.hpp
@@ -23,6 +23,7 @@ enum class TensorType {
BT_EMPTY,
BT_BUF_INTERNAL,
BT_BUF_SHARED,
+ BT_BUF_SHARED_FROM_HANDLE,
BT_USM_SHARED,
BT_USM_HOST_INTERNAL,
BT_USM_DEVICE_INTERNAL,
diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_context.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_context.hpp
index 7b0cd80f934..15956e73683 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_context.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_context.hpp
@@ -83,6 +83,7 @@ private:
std::string get_device_name(const std::map<std::string, RemoteContextImpl::Ptr>& known_contexts, const cldnn::device::ptr current_device) const;
std::shared_ptr<ov::IRemoteTensor> reuse_surface(const ov::element::Type type, const ov::Shape& shape, const ov::AnyMap& params);
std::shared_ptr<ov::IRemoteTensor> reuse_memory(const ov::element::Type type, const ov::Shape& shape, cldnn::shared_handle mem, TensorType tensor_type);
+ std::shared_ptr<ov::IRemoteTensor> reuse_memory_from_handle(const ov::element::Type type, const ov::Shape& shape, ov::intel_gpu::os_handle_param handle, TensorType tensor_type);
std::shared_ptr<ov::IRemoteTensor> create_buffer(const ov::element::Type type, const ov::Shape& shape);
std::shared_ptr<ov::IRemoteTensor> create_usm(const ov::element::Type type, const ov::Shape& shape, TensorType alloc_type);
void check_if_shared() const;
diff --git a/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_tensor.hpp b/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_tensor.hpp
index aff2d1159fc..1c5f64a4d91 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_tensor.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/plugin/remote_tensor.hpp
@@ -40,7 +40,13 @@ public:
TensorType mem_type = TensorType::BT_BUF_INTERNAL,
cldnn::shared_handle mem = nullptr,
cldnn::shared_surface surf = 0,
- uint32_t plane = 0);
+ uint32_t plane = 0,
+#ifdef __linux__
+ ov::intel_gpu::os_handle_param os_handle = -1
+#else
+ ov::intel_gpu::os_handle_param os_handle = nullptr
+#endif
+);
~RemoteTensorImpl() override;
const AnyMap& get_properties() const override;
@@ -81,6 +87,7 @@ private:
TensorType m_mem_type;
cldnn::shared_handle m_mem;
+ ov::intel_gpu::os_handle_param m_os_handle;
cldnn::shared_surface m_surf;
uint32_t m_plane;
size_t m_hash = 0;
diff --git a/src/plugins/intel_gpu/include/intel_gpu/runtime/engine.hpp b/src/plugins/intel_gpu/include/intel_gpu/runtime/engine.hpp
index 0fbec6aeb90..374586c1d22 100644
--- a/src/plugins/intel_gpu/include/intel_gpu/runtime/engine.hpp
+++ b/src/plugins/intel_gpu/include/intel_gpu/runtime/engine.hpp
@@ -13,6 +13,7 @@
#include "execution_config.hpp"
#include "engine_configuration.hpp"
#include "kernel_builder.hpp"
+#include "openvino/runtime/intel_gpu/remote_properties.hpp"
#include <memory>
#include <set>
@@ -72,6 +73,9 @@ public:
/// Create shared memory object using user-supplied memory buffer @p buf using specified @p layout
memory_ptr share_buffer(const layout& layout, shared_handle buf);
+ //Create memory object from user-supplied shared handle e.g from system HANDLE created by DX12
+ virtual memory_ptr import_buffer(const layout& layout, ov::intel_gpu::os_handle_param external_handle) = 0;
+
/// Create shared memory object using user-supplied USM pointer @p usm_ptr using specified @p layout
memory_ptr share_usm(const layout& layout, shared_handle usm_ptr);
diff --git a/src/plugins/intel_gpu/src/plugin/remote_context.cpp b/src/plugins/intel_gpu/src/plugin/remote_context.cpp
index c59149c898d..f932abe14eb 100644
--- a/src/plugins/intel_gpu/src/plugin/remote_context.cpp
+++ b/src/plugins/intel_gpu/src/plugin/remote_context.cpp
@@ -169,6 +169,10 @@ ov::SoPtr<ov::IRemoteTensor> RemoteContextImpl::create_tensor(const ov::element:
mem = extract_object(params, ov::intel_gpu::dev_object_handle);
check_if_shared();
#endif
+ } else if (ov::intel_gpu::SharedMemType::BUFFER_FROM_HANDLE == mem_type) {
+ tensor_type = TensorType::BT_BUF_SHARED_FROM_HANDLE;
+ ov::intel_gpu::os_handle_param handle = extract_object(params, ov::intel_gpu::os_handle);
+ return { reuse_memory_from_handle(type, shape, handle, tensor_type), nullptr };
} else {
OPENVINO_THROW("[GPU] Unsupported shared object type ", mem_type);
}
@@ -227,6 +231,13 @@ std::shared_ptr<ov::IRemoteTensor> RemoteContextImpl::reuse_memory(const ov::ele
return std::make_shared<RemoteTensorImpl>(get_this_shared_ptr(), shape, type, tensor_type, mem);
}
+std::shared_ptr<ov::IRemoteTensor> RemoteContextImpl::reuse_memory_from_handle(const ov::element::Type type,
+ const ov::Shape& shape,
+ ov::intel_gpu::os_handle_param handle,
+ TensorType tensor_type) {
+ return std::make_shared<RemoteTensorImpl>(get_this_shared_ptr(), shape, type, tensor_type, nullptr, 0, 0, handle);
+}
+
std::shared_ptr<ov::IRemoteTensor> RemoteContextImpl::create_buffer(const ov::element::Type type, const ov::Shape& shape) {
return std::make_shared<RemoteTensorImpl>(get_this_shared_ptr(), shape, type, TensorType::BT_BUF_INTERNAL);
}
diff --git a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp b/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp
index a9656d73399..74ff175b250 100644
--- a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp
+++ b/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp
@@ -149,13 +149,15 @@ RemoteTensorImpl::RemoteTensorImpl(RemoteContextImpl::Ptr context,
TensorType mem_type,
cldnn::shared_handle mem,
cldnn::shared_surface surf,
- uint32_t plane)
+ uint32_t plane,
+ ov::intel_gpu::os_handle_param os_handle)
: m_context(context)
, m_element_type(element_type)
, m_shape(shape)
, m_layout(cldnn::layout{ov::PartialShape{shape}, element_type, cldnn::format::get_default_format(shape.size())})
, m_mem_type(mem_type)
, m_mem(mem)
+ , m_os_handle(os_handle)
, m_surf(surf)
, m_plane(plane) {
update_hash();
@@ -342,6 +344,10 @@ void RemoteTensorImpl::allocate() {
m_memory_object = engine.share_buffer(m_layout, m_mem);
break;
}
+ case TensorType::BT_BUF_SHARED_FROM_HANDLE: {
+ m_memory_object = engine.import_buffer(m_layout, m_os_handle);
+ break;
+ }
case TensorType::BT_USM_SHARED: {
m_memory_object = engine.share_usm(m_layout, m_mem);
break;
@@ -382,6 +388,7 @@ const std::string& RemoteTensorImpl::get_device_name() const {
bool RemoteTensorImpl::is_shared() const noexcept {
return m_mem_type == TensorType::BT_BUF_SHARED ||
+ m_mem_type == TensorType::BT_BUF_SHARED_FROM_HANDLE ||
m_mem_type == TensorType::BT_USM_SHARED ||
m_mem_type == TensorType::BT_IMG_SHARED ||
m_mem_type == TensorType::BT_SURF_SHARED ||
@@ -395,6 +402,7 @@ bool RemoteTensorImpl::supports_caching() const {
void RemoteTensorImpl::update_hash() {
if (supports_caching()) {
m_hash = cldnn::hash_combine(0, m_mem);
+ m_hash = cldnn::hash_combine(m_hash, m_os_handle);
m_hash = cldnn::hash_combine(m_hash, m_surf);
m_hash = cldnn::hash_combine(m_hash, m_plane);
m_hash = cldnn::hash_combine(m_hash, m_shape.size());
@@ -453,6 +461,13 @@ void RemoteTensorImpl::update_properties() {
ov::intel_gpu::mem_handle(params.mem),
};
break;
+ case TensorType::BT_BUF_SHARED_FROM_HANDLE:
+ m_properties = {
+ ov::intel_gpu::shared_mem_type(ov::intel_gpu::SharedMemType::BUFFER_FROM_HANDLE),
+ ov::intel_gpu::ocl_context(params.context),
+ ov::intel_gpu::mem_handle(params.mem),
+ };
+ break;
case TensorType::BT_USM_SHARED:
m_properties = {
ov::intel_gpu::shared_mem_type(ov::intel_gpu::SharedMemType::USM_USER_BUFFER),
diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp
index f318d7b03ec..f5a8296a79f 100644
--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp
+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp
@@ -5,6 +5,7 @@
#include "ocl_engine.hpp"
#include "intel_gpu/runtime/utils.hpp"
#include "intel_gpu/graph/serialization/binary_buffer.hpp" // For CACHE_PAGE_SIZE
+#include "openvino/runtime/intel_gpu/remote_properties.hpp"
#include "ocl_kernel.hpp"
#include "ocl_kernel_builder.hpp"
@@ -15,7 +16,6 @@
#include <string>
#include <vector>
#include <memory>
-#include <set>
#include <stdexcept>
// NOTE: Due to buggy scope transition of warnings we need to disable warning in place of use/instantation
@@ -48,6 +48,16 @@ ocl_error::ocl_error(cl::Error const& err)
: ov::Exception("[GPU] " + std::string(err.what()) + std::string(", error code: ") + std::to_string(err.err())) {}
OPENVINO_SUPPRESS_DEPRECATED_END
+namespace {
+cl_platform_id get_platform_id_for_device(const cl::Device& device) {
+ cl_platform_id platform = nullptr;
+ cl_int err = clGetDeviceInfo(device.get(), CL_DEVICE_PLATFORM, sizeof(platform), &platform, nullptr);
+ OPENVINO_ASSERT(err == CL_SUCCESS && platform != nullptr,
+ "[GPU] Failed to retrieve CL_DEVICE_PLATFORM, error: ", err);
+ return platform;
+}
+} // namespace
+
ocl_engine::ocl_engine(const device::ptr dev, runtime_types runtime_type)
: engine(dev) {
OPENVINO_ASSERT(runtime_type == runtime_types::ocl, "[GPU] Invalid runtime type specified for OCL engine. Only OCL runtime is supported");
@@ -98,6 +108,71 @@ allocation_type ocl_engine::detect_usm_allocation_type(const void* memory) const
: allocation_type::unknown;
}
+memory::ptr ocl_engine::import_buffer(const layout& layout, ov::intel_gpu::os_handle_param external_handle) {
+#ifdef __linux__
+ OPENVINO_ASSERT(external_handle >= 0, "[GPU] External memory handle must be a valid file descriptor");
+#else
+ OPENVINO_ASSERT(external_handle != nullptr, "[GPU] External memory handle must not be null");
+#endif
+ OPENVINO_ASSERT(extension_supported("cl_khr_external_memory"),
+ "[GPU] Selected OpenCL device does not advertise cl_khr_external_memory; "
+ "external memory import is not supported");
+
+#ifndef CL_VERSION_3_0
+ OPENVINO_THROW("[GPU] External memory import is not supported on this platform");
+#else
+#ifdef _WIN32
+ constexpr auto handle_type_token = CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR;
+#elif defined(__linux__)
+ constexpr auto handle_type_token = CL_EXTERNAL_MEMORY_HANDLE_DMA_BUF_KHR;
+#else
+ OPENVINO_THROW("[GPU] External memory import is not supported on this platform");
+#endif
+
+ cl_mem_properties props[] = {
+ static_cast<cl_mem_properties>(handle_type_token),
+#ifdef __linux__
+ static_cast<cl_mem_properties>(external_handle),
+#else
+ static_cast<cl_mem_properties>(reinterpret_cast<intptr_t>(external_handle)),
+#endif
+ 0,
+ };
+
+ cl_int errcode = CL_SUCCESS;
+ auto cl_ctx = static_cast<cl_context>(get_user_context());
+ OPENVINO_ASSERT(cl_ctx != nullptr, "[GPU] OpenCL context is null while importing external buffer");
+ const auto byte_size = layout.bytes_count();
+ cl_mem imported = clCreateBufferWithProperties(cl_ctx, props, CL_MEM_READ_WRITE, byte_size, nullptr, &errcode);
+ OPENVINO_ASSERT(errcode == CL_SUCCESS && imported != nullptr,
+ "[GPU] Failed to import external memory handle via clCreateBufferWithProperties, error: ",
+ errcode);
+
+ cl_platform_id platform = get_platform_id_for_device(get_cl_device());
+ auto& svc_stream = downcast<ocl_stream>(get_service_stream());
+ cl_command_queue q = svc_stream.get_cl_queue().get();
+ cl_int acquire_err = cl::ExternalMemoryHelper::acquire(platform, q, imported);
+ if (acquire_err != CL_SUCCESS) {
+ clReleaseMemObject(imported);
+ OPENVINO_THROW("[GPU] clEnqueueAcquireExternalMemObjectsKHR failed or unavailable, error: ", acquire_err);
+ }
+ clFinish(q);
+ cl::Buffer buf(imported, true);
+ auto memory = std::make_shared<ocl::gpu_buffer_from_handle>(this, layout, buf, nullptr);
+ clReleaseMemObject(imported);
+ return memory;
+#endif
+}
+
+void ocl_engine::release_external_memory(cl_mem mem) const {
+ cl_platform_id platform = get_platform_id_for_device(get_cl_device());
+ auto& opencl_stream = downcast<ocl_stream>(get_service_stream());
+ cl_command_queue q = opencl_stream.get_cl_queue().get();
+ // If the extension entrypoint is missing, the cl_mem refcount drop on dtor will still proceed.
+ cl::ExternalMemoryHelper::release(platform, q, mem);
+ clFinish(q);
+}
+
memory::ptr ocl_engine::allocate_memory(const layout& layout, allocation_type type, bool reset) {
OPENVINO_ASSERT(!layout.is_dynamic() || layout.has_upper_bound(), "[GPU] Can't allocate memory for dynamic layout");
diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.hpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.hpp
index 8bef5dfd0f2..9315eb65d0f 100644
--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.hpp
+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.hpp
@@ -10,8 +10,6 @@
#include "ocl_device.hpp"
#include <memory>
-#include <set>
-#include <vector>
#include <utility>
#include <string>
@@ -30,8 +28,11 @@ public:
memory_ptr create_subbuffer(const memory& memory, const layout& new_layout, size_t offset) override;
memory_ptr create_mmap_hostbuffer(const void* mmapped_address, size_t data_size, allocation_type _allocation_type, const layout output_layout) override;
memory_ptr reinterpret_buffer(const memory& memory, const layout& new_layout) override;
+ memory_ptr import_buffer(const layout&, ov::intel_gpu::os_handle_param external_handle) override;
bool is_the_same_buffer(const memory& mem1, const memory& mem2) override;
+ void release_external_memory(cl_mem) const;
+
void* get_user_context() const override;
allocation_type get_default_allocation_type() const override { return allocation_type::cl_mem; }
diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_ext.hpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_ext.hpp
index c5609b8fdf6..4f11d7d28ff 100644
--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_ext.hpp
+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_ext.hpp
@@ -693,6 +693,56 @@ public:
};
#endif
+class ExternalMemoryHelper {
+ typedef CL_API_ENTRY cl_int(CL_API_CALL * PFN_clEnqueueAcquireExternalMemObjectsKHR)(
+ cl_command_queue /* command_queue */,
+ cl_uint /* num_mem_objects */,
+ const cl_mem* /* mem_objects */,
+ cl_uint /* num_events_in_wait_list */,
+ const cl_event* /* event_wait_list */,
+ cl_event* /* event */);
+
+ typedef CL_API_ENTRY cl_int(CL_API_CALL * PFN_clEnqueueReleaseExternalMemObjectsKHR)(
+ cl_command_queue /* command_queue */,
+ cl_uint /* num_mem_objects */,
+ const cl_mem* /* mem_objects */,
+ cl_uint /* num_events_in_wait_list */,
+ const cl_event* /* event_wait_list */,
+ cl_event* /* event */);
+public:
+
+ static cl_int acquire(cl_platform_id platform, cl_command_queue queue, const cl_mem& mem) {
+ auto pfn = get_acquire(platform);
+ if (pfn == nullptr)
+ return CL_INVALID_OPERATION;
+ return pfn(queue, 1, &mem, 0, nullptr, nullptr);
+ }
+
+ static cl_int release(cl_platform_id platform, cl_command_queue queue, const cl_mem& mem) {
+ auto pfn = get_release(platform);
+ if (pfn == nullptr)
+ return CL_INVALID_OPERATION;
+ return pfn(queue, 1, &mem, 0, nullptr, nullptr);
+ }
+private:
+ static PFN_clEnqueueAcquireExternalMemObjectsKHR get_acquire(cl_platform_id platform) {
+ static PFN_clEnqueueAcquireExternalMemObjectsKHR fn = nullptr;
+ if (!fn) {
+ fn = try_load_entrypoint<PFN_clEnqueueAcquireExternalMemObjectsKHR>(platform, "clEnqueueAcquireExternalMemObjectsKHR");
+ }
+ return fn;
+ }
+
+ static PFN_clEnqueueReleaseExternalMemObjectsKHR get_release(cl_platform_id platform) {
+ static PFN_clEnqueueReleaseExternalMemObjectsKHR fn = nullptr;
+ if (!fn) {
+ fn = try_load_entrypoint<PFN_clEnqueueReleaseExternalMemObjectsKHR>(platform, "clEnqueueReleaseExternalMemObjectsKHR");
+ }
+ return fn;
+ }
+
+};
+
class PlatformVA : public Platform {
public:
//! \brief Default constructor - initializes to NULL.
diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_memory.cpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_memory.cpp
index e168a076c32..9e2818c4e14 100644
--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_memory.cpp
+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_memory.cpp
@@ -219,6 +219,11 @@ dnnl::memory gpu_buffer::get_onednn_grouped_memory(dnnl::memory::desc desc, cons
}
#endif
+gpu_buffer_from_handle::~gpu_buffer_from_handle() {
+ auto cl_engine = downcast<const ocl_engine>(_engine);
+ cl_engine->release_external_memory(static_cast<cl_mem>(_buffer.get()));
+}
+
gpu_image2d::gpu_image2d(ocl_engine* engine, const layout& layout)
: lockable_gpu_mem()
, memory(engine, layout, allocation_type::cl_mem, nullptr)
diff --git a/src/plugins/intel_gpu/src/runtime/ocl/ocl_memory.hpp b/src/plugins/intel_gpu/src/runtime/ocl/ocl_memory.hpp
index c942e106283..a2cc3db172c 100644
--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_memory.hpp
+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_memory.hpp
@@ -31,7 +31,8 @@ struct lockable_gpu_mem {
};
struct gpu_buffer : public lockable_gpu_mem, public memory {
- gpu_buffer(ocl_engine* engine, const layout& new_layout, const cl::Buffer& buffer, std::shared_ptr<MemoryTracker> mem_tracker);
+ gpu_buffer(ocl_engine* engine, const layout& new_layout, const cl::Buffer& buffer,
+ std::shared_ptr<MemoryTracker> mem_tracker);
gpu_buffer(ocl_engine* engine, const layout& layout);
void* lock(const stream& stream, mem_lock_type type = mem_lock_type::read_write) override;
@@ -60,6 +61,11 @@ protected:
cl::Buffer _buffer;
};
+struct gpu_buffer_from_handle : public gpu_buffer {
+ using gpu_buffer::gpu_buffer; // constructor inheritance
+ ~gpu_buffer_from_handle() override;
+};
+
struct gpu_image2d : public lockable_gpu_mem, public memory {
gpu_image2d(ocl_engine* engine, const layout& new_layout, const cl::Image2D& buffer, std::shared_ptr<MemoryTracker> mem_tracker);
gpu_image2d(ocl_engine* engine, const layout& layout);
diff --git a/src/plugins/intel_gpu/src/runtime/ze/ze_engine.cpp b/src/plugins/intel_gpu/src/runtime/ze/ze_engine.cpp
index 8e2a81f5de2..832ebdd31be 100644
--- a/src/plugins/intel_gpu/src/runtime/ze/ze_engine.cpp
+++ b/src/plugins/intel_gpu/src/runtime/ze/ze_engine.cpp
@@ -97,6 +97,10 @@ memory::ptr ze_engine::allocate_memory(const layout& layout, allocation_type typ
}
}
+memory::ptr ze_engine::import_buffer(const layout& layout, ov::intel_gpu::os_handle_param external_handle) {
+ OPENVINO_NOT_IMPLEMENTED;
+}
+
memory::ptr ze_engine::reinterpret_buffer(const memory& memory, const layout& new_layout) {
OPENVINO_ASSERT(memory.get_engine() == this, "[GPU] trying to reinterpret buffer allocated by a different engine");
OPENVINO_ASSERT(new_layout.format.is_image() == memory.get_layout().format.is_image(),
diff --git a/src/plugins/intel_gpu/src/runtime/ze/ze_engine.hpp b/src/plugins/intel_gpu/src/runtime/ze/ze_engine.hpp
index f383d3575bf..0082bf6996a 100644
--- a/src/plugins/intel_gpu/src/runtime/ze/ze_engine.hpp
+++ b/src/plugins/intel_gpu/src/runtime/ze/ze_engine.hpp
@@ -23,6 +23,7 @@ public:
backend_types backend_type() const override { return backend_types::ze; };
memory_ptr allocate_memory(const layout& layout, allocation_type type, bool reset = true) override;
+ memory_ptr import_buffer(const layout& layout, ov::intel_gpu::os_handle_param external_handle) override;
memory_ptr reinterpret_handle(const layout& new_layout, shared_mem_params params) override;
memory_ptr create_subbuffer(const memory& memory, const layout& new_layout, size_t byte_offset) override;
memory_ptr create_mmap_hostbuffer(const void* mmapped_address, size_t data_size, allocation_type _allocation_type, const layout output_layout) override;
diff --git a/src/plugins/intel_gpu/tests/functional/CMakeLists.txt b/src/plugins/intel_gpu/tests/functional/CMakeLists.txt
index f4db96602ca..29655d0aa4c 100644
--- a/src/plugins/intel_gpu/tests/functional/CMakeLists.txt
+++ b/src/plugins/intel_gpu/tests/functional/CMakeLists.txt
@@ -55,8 +55,24 @@ if(libva_FOUND)
endif()
if(WIN32)
- target_compile_definitions(${TARGET_NAME} PRIVATE ENABLE_DX11)
- target_link_libraries(${TARGET_NAME} PRIVATE d3d11 dxgi)
+ include(CheckIncludeFileCXX)
+ # DX11 and DX12 SDK headers may be available independently; enable each
+ # interop test set only when its corresponding header is present to avoid
+ # build breaks on environments that ship only one of the SDKs.
+ check_include_file_cxx(d3d11.h OV_GPU_FUNC_TESTS_HAVE_D3D11_H)
+ check_include_file_cxx(d3d12.h OV_GPU_FUNC_TESTS_HAVE_D3D12_H)
+
+ if(OV_GPU_FUNC_TESTS_HAVE_D3D11_H)
+ target_compile_definitions(${TARGET_NAME} PRIVATE ENABLE_DX11)
+ target_link_libraries(${TARGET_NAME} PRIVATE d3d11)
+ endif()
+ if(OV_GPU_FUNC_TESTS_HAVE_D3D12_H)
+ target_compile_definitions(${TARGET_NAME} PRIVATE ENABLE_DX12)
+ target_link_libraries(${TARGET_NAME} PRIVATE d3d12)
+ endif()
+ if(OV_GPU_FUNC_TESTS_HAVE_D3D11_H OR OV_GPU_FUNC_TESTS_HAVE_D3D12_H)
+ target_link_libraries(${TARGET_NAME} PRIVATE dxgi dxguid)
+ endif()
endif()
ov_build_target_faster(${TARGET_NAME} PCH)
diff --git a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/dx11_nthandle.cpp b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/dx11_nthandle.cpp
new file mode 100644
index 00000000000..8d03463c6f6
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/dx11_nthandle.cpp
@@ -0,0 +1,289 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#if defined(OV_GPU_WITH_OCL_RT) && defined(_WIN32) && defined(ENABLE_DX11)
+#include <array>
+#include <cstring>
+#include <gtest/gtest.h>
+#include <vector>
+
+#ifndef NOMINMAX
+#define NOMINMAX
+#define NOMINMAX_DEFINED_SHARED_BUF_TEST
+#endif
+#include <atlbase.h>
+#include <d3d11_1.h>
+#include <dxgi1_2.h>
+#ifdef NOMINMAX_DEFINED_SHARED_BUF_TEST
+#undef NOMINMAX
+#undef NOMINMAX_DEFINED_SHARED_BUF_TEST
+#endif
+#include "openvino/runtime/core.hpp"
+#include "openvino/runtime/intel_gpu/ocl/ocl.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/result.hpp"
+#include "common_test_utils/test_constants.hpp"
+
+namespace {
+bool get_context_device_luid(cl_context cl_ctx, std::array<unsigned char, CL_LUID_SIZE_KHR>& cl_luid) {
+ size_t devices_size = 0;
+ if (clGetContextInfo(cl_ctx, CL_CONTEXT_DEVICES, 0, nullptr, &devices_size) != CL_SUCCESS ||
+ devices_size < sizeof(cl_device_id)) {
+ return false;
+ }
+
+ std::vector<cl_device_id> cl_devices(devices_size / sizeof(cl_device_id));
+ if (clGetContextInfo(cl_ctx, CL_CONTEXT_DEVICES, devices_size, cl_devices.data(), nullptr) != CL_SUCCESS ||
+ cl_devices.empty()) {
+ return false;
+ }
+
+ cl_bool cl_luid_valid = CL_FALSE;
+ if (clGetDeviceInfo(cl_devices[0], CL_DEVICE_LUID_VALID_KHR, sizeof(cl_luid_valid), &cl_luid_valid, nullptr) !=
+ CL_SUCCESS ||
+ cl_luid_valid != CL_TRUE) {
+ return false;
+ }
+
+ return clGetDeviceInfo(cl_devices[0], CL_DEVICE_LUID_KHR, cl_luid.size(), cl_luid.data(), nullptr) == CL_SUCCESS;
+}
+
+// Keep data unchanged while still forcing an explicit output tensor write path.
+std::shared_ptr<ov::Model> make_copy_model(const ov::Shape& shape) {
+ auto param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shape);
+ auto zero = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1}, {0.0f});
+ auto add = std::make_shared<ov::op::v1::Add>(param, zero);
+ auto result = std::make_shared<ov::op::v0::Result>(add);
+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{param});
+}
+
+
+struct Dx11TestContext {
+ CComPtr<ID3D11Device> device;
+ CComPtr<ID3D11DeviceContext> device_ctx;
+};
+
+struct Dx11SharedBuffer {
+ CComPtr<ID3D11Texture2D> buffer;
+ HANDLE shared_handle = nullptr;
+};
+
+void close_nt_handle(HANDLE& handle) {
+ if (handle != nullptr) {
+ CloseHandle(handle);
+ handle = nullptr;
+ }
+}
+
+struct NtHandleGuard {
+ HANDLE& handle;
+
+ ~NtHandleGuard() {
+ close_nt_handle(handle);
+ }
+};
+
+Dx11TestContext create_dx11_test_context(const std::array<unsigned char, CL_LUID_SIZE_KHR>& target_luid) {
+ IDXGIFactory* raw_factory = nullptr;
+ HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory), reinterpret_cast<void**>(&raw_factory));
+ EXPECT_FALSE(FAILED(hr));
+ if (FAILED(hr)) {
+ return {};
+ }
+ CComPtr<IDXGIFactory> factory(raw_factory);
+ if (!factory) {
+ return {};
+ }
+
+ UINT adapter_index = 0;
+ IDXGIAdapter* raw_adapter = nullptr;
+ while (factory->EnumAdapters(adapter_index, &raw_adapter) != DXGI_ERROR_NOT_FOUND) {
+ CComPtr<IDXGIAdapter> adapter(raw_adapter);
+ DXGI_ADAPTER_DESC desc{};
+ adapter->GetDesc(&desc);
+
+ std::array<unsigned char, CL_LUID_SIZE_KHR> adapter_luid{};
+ memcpy(adapter_luid.data(), &desc.AdapterLuid, sizeof(desc.AdapterLuid));
+ if (memcmp(adapter_luid.data(), target_luid.data(), target_luid.size()) != 0) {
+ ++adapter_index;
+ continue;
+ }
+
+ D3D_FEATURE_LEVEL feature_levels[] = {D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0};
+ D3D_FEATURE_LEVEL feature_level;
+ ID3D11Device* raw_device = nullptr;
+ ID3D11DeviceContext* raw_ctx = nullptr;
+ hr = D3D11CreateDevice(adapter,
+ D3D_DRIVER_TYPE_UNKNOWN,
+ nullptr,
+ 0,
+ feature_levels,
+ ARRAYSIZE(feature_levels),
+ D3D11_SDK_VERSION,
+ &raw_device,
+ &feature_level,
+ &raw_ctx);
+ if (FAILED(hr)) {
+ return {};
+ }
+
+ return {CComPtr<ID3D11Device>(raw_device), CComPtr<ID3D11DeviceContext>(raw_ctx)};
+ }
+
+ return {};
+}
+
+Dx11SharedBuffer create_dx11_shared_buffer(ID3D11Device* device, size_t byte_size, const void* data = nullptr) {
+ // D3D11 does not allow SHARED_NTHANDLE on ID3D11Buffer; use an R32_FLOAT 4x4 Texture2D as backing storage.
+ const UINT element_count = static_cast<UINT>(byte_size / sizeof(float));
+ const UINT tex_width = 4;
+ const UINT tex_height = element_count / tex_width;
+ D3D11_TEXTURE2D_DESC desc{};
+ desc.Width = tex_width;
+ desc.Height = tex_height;
+ desc.MipLevels = 1;
+ desc.ArraySize = 1;
+ desc.Format = DXGI_FORMAT_R32_FLOAT;
+ desc.SampleDesc.Count = 1;
+ desc.SampleDesc.Quality = 0;
+ desc.Usage = D3D11_USAGE_DEFAULT;
+ // Keep UAV-capable shared buffer; CPU writes are done via UpdateSubresource.
+ desc.BindFlags = D3D11_BIND_UNORDERED_ACCESS;
+ desc.CPUAccessFlags = 0;
+ desc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_NTHANDLE | D3D11_RESOURCE_MISC_SHARED;
+
+ D3D11_SUBRESOURCE_DATA init_data{};
+ init_data.pSysMem = data;
+ init_data.SysMemPitch = tex_width * static_cast<UINT>(sizeof(float));
+ init_data.SysMemSlicePitch = init_data.SysMemPitch * tex_height;
+
+ ID3D11Texture2D* raw_texture = nullptr;
+ HRESULT hr = device->CreateTexture2D(&desc, data ? &init_data : nullptr, &raw_texture);
+
+ if (FAILED(hr)) {
+ return {};
+ }
+ CComPtr<ID3D11Texture2D> shared_texture(raw_texture);
+
+ HANDLE shared_handle = nullptr;
+ CComPtr<IDXGIResource1> dxgi_resource;
+ hr = shared_texture->QueryInterface(__uuidof(IDXGIResource1), reinterpret_cast<void**>(&dxgi_resource));
+ if (FAILED(hr)) {
+ return {};
+ }
+ if (dxgi_resource) {
+ hr = dxgi_resource->CreateSharedHandle(nullptr,
+ DXGI_SHARED_RESOURCE_READ | DXGI_SHARED_RESOURCE_WRITE,
+ nullptr,
+ &shared_handle);
+ }
+ if (FAILED(hr)) {
+ return {};
+ }
+ if (shared_handle == nullptr) {
+ return {};
+ }
+
+ return {shared_texture, shared_handle};
+}
+
+CComPtr<ID3D11Texture2D> open_dx11_shared_buffer(ID3D11Device* device, HANDLE shared_handle) {
+ CComPtr<ID3D11Device1> device1;
+ HRESULT hr = device->QueryInterface(__uuidof(ID3D11Device1), reinterpret_cast<void**>(&device1));
+ EXPECT_FALSE(FAILED(hr));
+ ID3D11Texture2D* raw_opened_texture = nullptr;
+ hr = device1->OpenSharedResource1(shared_handle,
+ __uuidof(ID3D11Texture2D),
+ reinterpret_cast<void**>(&raw_opened_texture));
+ if(FAILED(hr)) {
+ return {};
+ }
+ return CComPtr<ID3D11Texture2D>(raw_opened_texture);
+}
+
+TEST(GpuSharedBufferRemoteTensor, smoke_Dx11RemoteInputToRemoteOutputCopyAndCompare) {
+#ifndef CL_VERSION_3_0
+ GTEST_SKIP() << "OpenCL version 3.0 is required for external memory sharing";
+#endif
+ //test work on 32.101.7076 - not tried with older driver
+ ov::Core core;
+ const ov::Shape shape{16};
+ const size_t element_count = ov::shape_size(shape);
+ const size_t byte_size = element_count * sizeof(float);
+
+ std::string target_device = ov::test::utils::DEVICE_GPU;
+ auto ctx = core.get_default_context(target_device).as<ov::intel_gpu::ocl::ClContext>();
+ auto params = ctx.get_params();
+ auto it = params.find(ov::intel_gpu::ocl_context.name());
+ ASSERT_TRUE(it != params.end()) << "Failed to get OpenCL context for " << target_device;
+
+ // Extract LUID from OpenCL context
+ auto cl_ctx = static_cast<cl_context>(it->second.as<ov::intel_gpu::ocl::gpu_handle_param>());
+ std::array<unsigned char, CL_LUID_SIZE_KHR> cl_luid{};
+ ASSERT_TRUE(get_context_device_luid(cl_ctx, cl_luid)) << "Failed to get LUID for " << target_device;
+
+ // Create DX11 context for the selected GPU's LUID
+ Dx11TestContext dx11 = create_dx11_test_context(cl_luid);
+ ASSERT_TRUE(dx11.device != nullptr) << "Failed to create DX11 context for " << target_device;
+
+ std::vector<float> input_init(element_count, 2.0f);
+ auto dx_input_shared = create_dx11_shared_buffer(dx11.device, byte_size, input_init.data());
+ NtHandleGuard input_handle_guard{dx_input_shared.shared_handle};
+ std::vector<float> output_init(element_count, 0.0f);
+ auto dx_output_shared = create_dx11_shared_buffer(dx11.device, byte_size, output_init.data());
+ NtHandleGuard output_handle_guard{dx_output_shared.shared_handle};
+
+ auto dx_input_buffer = open_dx11_shared_buffer(dx11.device,
+ dx_input_shared.shared_handle);
+ ASSERT_TRUE(dx_input_buffer != nullptr) << "Failed to open shared input buffer in DX11 context for " << target_device;
+
+ auto dx_output_buffer = open_dx11_shared_buffer(dx11.device,
+ dx_output_shared.shared_handle);
+ ASSERT_TRUE(dx_output_buffer != nullptr) << "Failed to open shared output buffer in DX11 context for " << target_device;
+
+ // Initialize opened shared input texture explicitly to avoid driver-dependent init visibility.
+ const UINT row_pitch = 4u * static_cast<UINT>(sizeof(float)); // 4 floats per row
+ dx11.device_ctx->UpdateSubresource(dx_input_buffer,
+ 0,
+ nullptr,
+ input_init.data(),
+ row_pitch,
+ static_cast<UINT>(byte_size));
+ dx11.device_ctx->Flush();
+
+
+
+ auto remote_input_tensor = ctx.create_tensor(ov::element::f32,
+ shape,
+ dx_input_shared.shared_handle,
+ ov::intel_gpu::MemType::SHARED_BUF);
+ auto remote_output_tensor = ctx.create_tensor(ov::element::f32,
+ shape,
+ dx_output_shared.shared_handle,
+ ov::intel_gpu::MemType::SHARED_BUF);
+
+ auto model = make_copy_model(shape);
+ auto compiled = core.compile_model(model, ctx);
+ auto infer_req = compiled.create_infer_request();
+ infer_req.set_tensor(compiled.input(), remote_input_tensor);
+ infer_req.set_tensor(compiled.output(), remote_output_tensor);
+
+ ov::Tensor host_input(ov::element::f32, shape);
+ remote_input_tensor.copy_to(host_input);
+ const auto* input_values = host_input.data<const float>();
+ for (size_t i = 0; i < element_count; ++i) {
+ EXPECT_FLOAT_EQ(input_values[i], 2.0f) << "Input mismatch at index " << i;
+ }
+ infer_req.infer();
+ ov::Tensor host_output(ov::element::f32, shape);
+ remote_output_tensor.copy_to(host_output);
+ const auto* output_values = host_output.data<const float>();
+ for (size_t i = 0; i < element_count; ++i) {
+ EXPECT_FLOAT_EQ(output_values[i], 2.0f) << "Mismatch at index " << i;
+ }
+}
+} // namespace
+#endif
diff --git a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/dx12_nthandle.cpp b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/dx12_nthandle.cpp
new file mode 100644
index 00000000000..ea357e8cc75
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/dx12_nthandle.cpp
@@ -0,0 +1,312 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#if defined(OV_GPU_WITH_OCL_RT) && defined(_WIN32) && defined(ENABLE_DX12)
+#include <array>
+#include <cstring>
+#include <gtest/gtest.h>
+#include <vector>
+
+
+#ifndef NOMINMAX
+#define NOMINMAX
+#define NOMINMAX_DEFINED_SHARED_BUF_TEST
+#endif
+#include <atlbase.h>
+#include <d3d12.h>
+#include <dxgi1_4.h>
+#ifdef NOMINMAX_DEFINED_SHARED_BUF_TEST
+#undef NOMINMAX
+#undef NOMINMAX_DEFINED_SHARED_BUF_TEST
+#endif
+
+#include "openvino/runtime/core.hpp"
+#include "openvino/runtime/intel_gpu/ocl/ocl.hpp"
+#include "openvino/op/add.hpp"
+#include "openvino/op/constant.hpp"
+#include "openvino/op/parameter.hpp"
+#include "openvino/op/result.hpp"
+#include "common_test_utils/test_constants.hpp"
+
+namespace {
+bool get_context_device_luid(cl_context cl_ctx, std::array<unsigned char, CL_LUID_SIZE_KHR>& cl_luid) {
+ size_t devices_size = 0;
+ if (clGetContextInfo(cl_ctx, CL_CONTEXT_DEVICES, 0, nullptr, &devices_size) != CL_SUCCESS ||
+ devices_size < sizeof(cl_device_id)) {
+ return false;
+ }
+
+ std::vector<cl_device_id> cl_devices(devices_size / sizeof(cl_device_id));
+ if (clGetContextInfo(cl_ctx, CL_CONTEXT_DEVICES, devices_size, cl_devices.data(), nullptr) != CL_SUCCESS ||
+ cl_devices.empty()) {
+ return false;
+ }
+
+ cl_bool cl_luid_valid = CL_FALSE;
+ if (clGetDeviceInfo(cl_devices[0], CL_DEVICE_LUID_VALID_KHR, sizeof(cl_luid_valid), &cl_luid_valid, nullptr) != CL_SUCCESS ||
+ cl_luid_valid != CL_TRUE) {
+ return false;
+ }
+
+ return clGetDeviceInfo(cl_devices[0], CL_DEVICE_LUID_KHR, cl_luid.size(), cl_luid.data(), nullptr) == CL_SUCCESS;
+}
+
+// Keep data unchanged while still forcing an explicit output tensor write path.
+std::shared_ptr<ov::Model> make_copy_model(const ov::Shape& shape) {
+ auto param = std::make_shared<ov::op::v0::Parameter>(ov::element::f32, shape);
+ auto zero = ov::op::v0::Constant::create(ov::element::f32, ov::Shape{1}, {0.0f});
+ auto add = std::make_shared<ov::op::v1::Add>(param, zero);
+ auto result = std::make_shared<ov::op::v0::Result>(add);
+ return std::make_shared<ov::Model>(ov::ResultVector{result}, ov::ParameterVector{param});
+}
+
+
+struct Dx12TestContext {
+ CComPtr<IDXGIAdapter1> adapter;
+ CComPtr<ID3D12Device> device;
+ CComPtr<ID3D12CommandQueue> command_queue;
+};
+
+struct Dx12SharedBuffer {
+ CComPtr<ID3D12Resource> resource;
+ HANDLE shared_handle = nullptr; // NT handle; caller must CloseHandle when done
+};
+
+
+static bool gpu_wait(ID3D12CommandQueue* command_queue, ID3D12Device* device) {
+ ID3D12Fence* raw_fence = nullptr;
+ HRESULT hr = device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&raw_fence));
+ if (FAILED(hr)) return false;
+ CComPtr<ID3D12Fence> fence(raw_fence);
+
+ HANDLE event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
+ if (!event) return false;
+
+ const UINT64 fence_value = 1;
+ command_queue->Signal(fence, fence_value);
+ if (fence->GetCompletedValue() < fence_value) {
+ fence->SetEventOnCompletion(fence_value, event);
+ WaitForSingleObject(event, INFINITE);
+ }
+ CloseHandle(event);
+ return true;
+}
+
+Dx12TestContext create_dx12_test_context(const std::array<unsigned char, CL_LUID_SIZE_KHR>& target_luid) {
+ IDXGIFactory4* raw_factory = nullptr;
+ HRESULT hr = CreateDXGIFactory1(IID_PPV_ARGS(&raw_factory));
+ if (FAILED(hr)) {
+ return {};
+ }
+ CComPtr<IDXGIFactory4> factory(raw_factory);
+ if (!factory) {
+ return {};
+ }
+
+ UINT adapter_index = 0;
+ IDXGIAdapter1* raw_adapter = nullptr;
+ while (factory->EnumAdapters1(adapter_index, &raw_adapter) != DXGI_ERROR_NOT_FOUND) {
+ CComPtr<IDXGIAdapter1> adapter(raw_adapter);
+ DXGI_ADAPTER_DESC1 desc{};
+ adapter->GetDesc1(&desc);
+
+ std::array<unsigned char, CL_LUID_SIZE_KHR> adapter_luid{};
+ memcpy(adapter_luid.data(), &desc.AdapterLuid, sizeof(desc.AdapterLuid));
+ if ((desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) ||
+ memcmp(adapter_luid.data(), target_luid.data(), target_luid.size()) != 0) {
+ ++adapter_index;
+ continue;
+ }
+
+ ID3D12Device* raw_device = nullptr;
+ hr = D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&raw_device));
+ if (FAILED(hr)) {
+ return {};
+ }
+ CComPtr<ID3D12Device> device(raw_device);
+
+ D3D12_COMMAND_QUEUE_DESC queue_desc{};
+ queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
+ ID3D12CommandQueue* raw_queue = nullptr;
+ hr = device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&raw_queue));
+ if (FAILED(hr)) {
+ return {};
+ }
+ return {adapter, device, CComPtr<ID3D12CommandQueue>(raw_queue)};
+ }
+
+ return {};
+}
+
+Dx12SharedBuffer create_dx12_shared_buffer(ID3D12Device* device,
+ ID3D12CommandQueue* command_queue,
+ size_t byte_size,
+ const void* data = nullptr) {
+ D3D12_HEAP_PROPERTIES heap_props{};
+ heap_props.Type = D3D12_HEAP_TYPE_DEFAULT;
+
+ D3D12_RESOURCE_DESC resource_desc{};
+ resource_desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
+ resource_desc.Alignment = 0;
+ resource_desc.Width = byte_size;
+ resource_desc.Height = 1;
+ resource_desc.DepthOrArraySize = 1;
+ resource_desc.MipLevels = 1;
+ resource_desc.Format = DXGI_FORMAT_UNKNOWN;
+ resource_desc.SampleDesc.Count = 1;
+ resource_desc.SampleDesc.Quality = 0;
+ resource_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
+ resource_desc.Flags = D3D12_RESOURCE_FLAG_NONE;
+
+ ID3D12Resource* raw_resource = nullptr;
+ HRESULT hr = device->CreateCommittedResource(&heap_props,
+ D3D12_HEAP_FLAG_SHARED,
+ &resource_desc,
+ D3D12_RESOURCE_STATE_COMMON,
+ nullptr,
+ IID_PPV_ARGS(&raw_resource));
+ if(FAILED(hr)) {
+ return {};
+ }
+ CComPtr<ID3D12Resource> resource(raw_resource);
+ if (!resource) {
+ return {};
+ }
+
+ HANDLE shared_handle = nullptr;
+ hr = device->CreateSharedHandle(resource, nullptr, GENERIC_ALL, nullptr, &shared_handle);
+ if (FAILED(hr)) {
+ return {};
+ }
+ if (shared_handle == nullptr) {
+ return {};
+ }
+
+ if (data && resource) {
+ D3D12_HEAP_PROPERTIES upload_heap{};
+ upload_heap.Type = D3D12_HEAP_TYPE_UPLOAD;
+
+ D3D12_RESOURCE_DESC upload_desc = resource_desc;
+ upload_desc.Flags = D3D12_RESOURCE_FLAG_NONE;
+
+ ID3D12Resource* raw_upload = nullptr;
+ hr = device->CreateCommittedResource(&upload_heap,
+ D3D12_HEAP_FLAG_NONE,
+ &upload_desc,
+ D3D12_RESOURCE_STATE_GENERIC_READ,
+ nullptr,
+ IID_PPV_ARGS(&raw_upload));
+ if (FAILED(hr)) {
+ return {};
+ }
+ CComPtr<ID3D12Resource> upload_resource(raw_upload);
+
+ if (upload_resource) {
+ void* mapped = nullptr;
+ D3D12_RANGE read_range{0, 0};
+ upload_resource->Map(0, &read_range, &mapped);
+ memcpy(mapped, data, byte_size);
+ upload_resource->Unmap(0, nullptr);
+
+ ID3D12CommandAllocator* raw_allocator = nullptr;
+ device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&raw_allocator));
+ CComPtr<ID3D12CommandAllocator> allocator(raw_allocator);
+
+ ID3D12GraphicsCommandList* raw_cmd_list = nullptr;
+ device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator, nullptr,
+ IID_PPV_ARGS(&raw_cmd_list));
+ CComPtr<ID3D12GraphicsCommandList> cmd_list(raw_cmd_list);
+
+ D3D12_RESOURCE_BARRIER barrier{};
+ barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
+ barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
+ barrier.Transition.pResource = resource;
+ barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COMMON;
+ barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_DEST;
+ barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
+ cmd_list->ResourceBarrier(1, &barrier);
+
+ cmd_list->CopyBufferRegion(resource, 0, upload_resource, 0, byte_size);
+
+ barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_COPY_DEST;
+ barrier.Transition.StateAfter = D3D12_RESOURCE_STATE_COMMON;
+ cmd_list->ResourceBarrier(1, &barrier);
+ cmd_list->Close();
+
+ ID3D12CommandList* cmd_lists[] = {cmd_list};
+ command_queue->ExecuteCommandLists(1, cmd_lists);
+ gpu_wait(command_queue, device);
+ }
+ }
+ return {resource, shared_handle};
+}
+
+TEST(GpuSharedBufferRemoteTensor, smoke_Dx12RemoteInputToRemoteOutputCopyAndCompare) {
+#ifndef CL_VERSION_3_0
+ GTEST_SKIP() << "OpenCL version 3.0 is required for external memory sharing";
+#endif
+ //test work on 32.101.7076 - not tried with older driver
+ ov::Core core;
+ const ov::Shape shape{16'000};
+ const size_t element_count = ov::shape_size(shape);
+ const size_t byte_size = element_count * sizeof(float);
+
+ std::string target_device = ov::test::utils::DEVICE_GPU;
+ auto candidate_ctx = core.get_default_context(target_device).as<ov::intel_gpu::ocl::ClContext>();
+ auto params = candidate_ctx.get_params();
+ auto it = params.find(ov::intel_gpu::ocl_context.name());
+ ASSERT_TRUE(it != params.end()) << "Failed to get OpenCL context for " << target_device;
+
+ // Extract LUID from OpenCL context
+ auto cl_ctx = static_cast<cl_context>(it->second.as<ov::intel_gpu::ocl::gpu_handle_param>());
+ std::array<unsigned char, CL_LUID_SIZE_KHR> cl_luid{};
+ ASSERT_TRUE(get_context_device_luid(cl_ctx, cl_luid)) << "Failed to get LUID for " << target_device;
+ // Create DX12 context for the selected GPU's LUID
+ Dx12TestContext dx12 = create_dx12_test_context(cl_luid);
+ ASSERT_TRUE(dx12.device) << "Failed to create DX12 context for " << target_device;
+
+ std::vector<float> input_init(element_count, 2.0f);
+ auto dx_input_shared = create_dx12_shared_buffer(dx12.device, dx12.command_queue,
+ byte_size, input_init.data());
+ std::vector<float> output_init(element_count, 0.0f);
+ auto dx_output_shared = create_dx12_shared_buffer(dx12.device, dx12.command_queue, byte_size, output_init.data());
+ ASSERT_NE(dx_input_shared.shared_handle, nullptr);
+ ASSERT_NE(dx_output_shared.shared_handle, nullptr);
+
+ ov::RemoteTensor remote_input_tensor;
+ ov::RemoteTensor remote_output_tensor;
+
+ remote_input_tensor = candidate_ctx.create_tensor(ov::element::f32, shape,
+ dx_input_shared.shared_handle,
+ ov::intel_gpu::MemType::SHARED_BUF);
+ remote_output_tensor = candidate_ctx.create_tensor(ov::element::f32, shape,
+ dx_output_shared.shared_handle,
+ ov::intel_gpu::MemType::SHARED_BUF);
+
+ auto model = make_copy_model(shape);
+ auto compiled = core.compile_model(model, candidate_ctx);
+ auto infer_req = compiled.create_infer_request();
+ infer_req.set_tensor(compiled.input(), remote_input_tensor);
+ infer_req.set_tensor(compiled.output(), remote_output_tensor);
+ ov::Tensor host_input(ov::element::f32, shape);
+ remote_input_tensor.copy_to(host_input);
+ const auto* input_values = host_input.data<const float>();
+ for (size_t i = 0; i < element_count; ++i) {
+ EXPECT_FLOAT_EQ(input_values[i], 2.0f) << "Input mismatch at index " << i;
+ }
+ infer_req.infer();
+ ov::Tensor host_output(ov::element::f32, shape);
+ remote_output_tensor.copy_to(host_output);
+ const auto* output_values = host_output.data<const float>();
+ for (size_t i = 0; i < element_count; ++i) {
+ EXPECT_FLOAT_EQ(output_values[i], 2.0f) << "Mismatch at index " << i;
+ }
+
+ CloseHandle(dx_input_shared.shared_handle);
+ dx_input_shared.shared_handle = nullptr;
+ CloseHandle(dx_output_shared.shared_handle);
+ dx_output_shared.shared_handle = nullptr;
+}
+} // namespace
+#endif
diff --git a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/dx12_remote_run.cpp b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/dx12_remote_run.cpp
new file mode 100644
index 00000000000..2417954077e
--- /dev/null
+++ b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/dx12_remote_run.cpp
@@ -0,0 +1,290 @@
+// Copyright (C) 2018-2026 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#if defined(_WIN32) && defined(ENABLE_DX12)
+#include <gtest/gtest.h>
+
+#include <common_test_utils/subgraph_builders/conv_pool_relu.hpp>
+#include "openvino/core/any.hpp"
+#include "openvino/core/memory_util.hpp"
+#include "openvino/runtime/compiled_model.hpp"
+#include "openvino/runtime/core.hpp"
+#include "openvino/runtime/intel_gpu/ocl/ocl.hpp"
+#include "openvino/runtime/intel_gpu/remote_properties.hpp"
+#include "shared_test_classes/base/ov_behavior_test_utils.hpp"
+
+#include <d3d12.h>
+#include <wrl.h>
+
+namespace {
+
+std::shared_ptr<ov::Model> make_model() {
+ std::vector<size_t> inputShape = {1, 2, 32, 32};
+ ov::element::Type_t ngPrc = ov::element::Type_t::f32;
+ return ov::test::utils::make_conv_pool_relu(inputShape, ngPrc);
+}
+
+class DX12RemoteRunTests : public ov::test::behavior::OVPluginTestBase {
+protected:
+ std::shared_ptr<ov::Core> core = ov::test::utils::PluginCache::get().core();
+ ov::AnyMap configuration;
+ std::shared_ptr<ov::Model> ov_model;
+
+ Microsoft::WRL::ComPtr<ID3D12Device> device;
+ Microsoft::WRL::ComPtr<ID3D12Heap> heap = nullptr;
+ Microsoft::WRL::ComPtr<ID3D12Resource> placed_resources = nullptr;
+ Microsoft::WRL::ComPtr<ID3D12Resource> comitted_resource;
+
+ HANDLE shared_mem = nullptr;
+
+public:
+ void SetUp() override {
+#ifndef CL_VERSION_3_0
+ GTEST_SKIP() << "OpenCL version 3.0 is required for external memory sharing";
+#endif
+ //tests works on 32.101.7076 - not tried with older driver
+ target_device = ov::test::utils::DEVICE_GPU;
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ OVPluginTestBase::SetUp();
+ ov_model = make_model();
+ createDevice();
+ }
+
+ void TearDown() override {
+ if (!configuration.empty()) {
+ ov::test::utils::PluginCache::get().reset();
+ }
+
+ APIBaseTest::TearDown();
+ }
+
+ void createDevice() {
+ auto res = D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(device.ReleaseAndGetAddressOf()));
+ ASSERT_TRUE(SUCCEEDED(res)) << "D3D12CreateDevice failed";
+ }
+
+ void createHeap(const size_t byte_size) {
+ const size_t size = (byte_size + (static_cast<size_t>(D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT) - 1)) &
+ ~(static_cast<size_t>(D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT) - 1);
+
+ D3D12_HEAP_DESC desc_heap{};
+ desc_heap.SizeInBytes = size;
+ desc_heap.Properties.Type = D3D12_HEAP_TYPE_CUSTOM;
+ desc_heap.Properties.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_NOT_AVAILABLE;
+ desc_heap.Properties.MemoryPoolPreference = D3D12_MEMORY_POOL_L0;
+ desc_heap.Properties.CreationNodeMask = 1;
+ desc_heap.Properties.VisibleNodeMask = 1;
+ desc_heap.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
+ desc_heap.Flags = D3D12_HEAP_FLAG_SHARED_CROSS_ADAPTER | D3D12_HEAP_FLAG_SHARED;
+ auto res = device->CreateHeap(&desc_heap, IID_PPV_ARGS(heap.ReleaseAndGetAddressOf()));
+ ASSERT_TRUE(SUCCEEDED(res)) << "CreateHeap failed.";
+
+ res = device->CreateSharedHandle(heap.Get(), nullptr, GENERIC_ALL, nullptr, &shared_mem);
+ ASSERT_TRUE(SUCCEEDED(res)) << "CreateSharedHandle failed.";
+ }
+
+ void createPlacedResources(const size_t byte_size) {
+ D3D12_RESOURCE_DESC desc_resource{};
+ desc_resource.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
+ desc_resource.Alignment = D3D12_DEFAULT_RESOURCE_PLACEMENT_ALIGNMENT;
+ desc_resource.Width = byte_size;
+ desc_resource.Height = 1;
+ desc_resource.DepthOrArraySize = 1;
+ desc_resource.MipLevels = 1;
+ desc_resource.Format = DXGI_FORMAT_UNKNOWN;
+ desc_resource.SampleDesc.Count = 1;
+ desc_resource.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
+ desc_resource.Flags = D3D12_RESOURCE_FLAG_ALLOW_CROSS_ADAPTER | D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
+ auto res = device->CreatePlacedResource(heap.Get(),
+ 0,
+ &desc_resource,
+ D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
+ nullptr,
+ IID_PPV_ARGS(placed_resources.ReleaseAndGetAddressOf()));
+ ASSERT_TRUE(SUCCEEDED(res)) << "CreatePlacedResource failed.";
+ }
+
+ void createComittedResources(const size_t byte_size) {
+ D3D12_HEAP_PROPERTIES heap_properties{};
+ heap_properties.Type = D3D12_HEAP_TYPE_UPLOAD;
+ heap_properties.CreationNodeMask = 1;
+ heap_properties.VisibleNodeMask = 1;
+
+ D3D12_RESOURCE_DESC resource_desc{};
+ resource_desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
+ resource_desc.Width = byte_size;
+ resource_desc.Height = 1;
+ resource_desc.DepthOrArraySize = 1;
+ resource_desc.MipLevels = 1;
+ resource_desc.SampleDesc.Count = 1;
+ resource_desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
+
+ auto res = device->CreateCommittedResource(&heap_properties,
+ D3D12_HEAP_FLAG_NONE,
+ &resource_desc,
+ D3D12_RESOURCE_STATE_GENERIC_READ,
+ nullptr,
+ IID_PPV_ARGS(comitted_resource.ReleaseAndGetAddressOf()));
+ ASSERT_TRUE(SUCCEEDED(res)) << "CreateCommittedResource failed.";
+ }
+
+ void createResources(const size_t byte_size) {
+ createHeap(byte_size);
+ createPlacedResources(byte_size);
+ createComittedResources(byte_size);
+ }
+
+ void copyResources(const size_t byte_size) {
+ Microsoft::WRL::ComPtr<ID3D12CommandQueue> command_queue;
+ Microsoft::WRL::ComPtr<ID3D12CommandAllocator> command_allocator;
+ Microsoft::WRL::ComPtr<ID3D12GraphicsCommandList4> command_list;
+ Microsoft::WRL::ComPtr<ID3D12Fence> fence;
+ uint32_t fence_value = 0;
+
+ D3D12_COMMAND_QUEUE_DESC desc{};
+ desc.Type = D3D12_COMMAND_LIST_TYPE_COMPUTE;
+ desc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL;
+ desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
+ desc.NodeMask = 0;
+ auto res = device->CreateCommandQueue(&desc, IID_PPV_ARGS(command_queue.ReleaseAndGetAddressOf()));
+ ASSERT_TRUE(SUCCEEDED(res)) << "CreateCommandQueue failed.";
+
+ res = device->CreateFence(0, D3D12_FENCE_FLAG_SHARED, IID_PPV_ARGS(fence.ReleaseAndGetAddressOf()));
+ ASSERT_TRUE(SUCCEEDED(res)) << "CreateFence failed.";
+
+ res = device.Get()->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_COMPUTE,
+ IID_PPV_ARGS(command_allocator.ReleaseAndGetAddressOf()));
+ ASSERT_TRUE(SUCCEEDED(res)) << "CreateCommandAllocator failed.";
+ res = device->CreateCommandList(0,
+ D3D12_COMMAND_LIST_TYPE_COMPUTE,
+ command_allocator.Get(),
+ nullptr,
+ IID_PPV_ARGS(command_list.ReleaseAndGetAddressOf()));
+ ASSERT_TRUE(SUCCEEDED(res)) << "CreateCommandList failed.";
+
+ command_list->CopyBufferRegion(placed_resources.Get(), 0, comitted_resource.Get(), 0, byte_size);
+ res = command_list->Close();
+ ASSERT_TRUE(SUCCEEDED(res)) << "Close command list failed.";
+
+ ID3D12CommandList* command_lists[] = {command_list.Get()};
+ command_queue->ExecuteCommandLists(ARRAYSIZE(command_lists), command_lists);
+ res = command_queue->Signal(fence.Get(), ++fence_value);
+ ASSERT_TRUE(SUCCEEDED(res)) << "Signal command queue failed.";
+
+ volatile auto event = CreateEvent(nullptr, FALSE, FALSE, nullptr);
+ res = fence->SetEventOnCompletion(fence_value, event);
+ ASSERT_TRUE(SUCCEEDED(res)) << "SetEventOnCompletion failed.";
+ WaitForSingleObject(event, INFINITE);
+ }
+};
+
+TEST_F(DX12RemoteRunTests, smoke_CheckRemoteTensorSharedBuf) {
+ // Skip test according to plugin specific disabled_test_patterns() (if any)
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ ov::CompiledModel compiled_model;
+ ov::InferRequest inference_request;
+
+ OV_ASSERT_NO_THROW(compiled_model = core->compile_model(ov_model, target_device, configuration));
+ OV_ASSERT_NO_THROW(inference_request = compiled_model.create_infer_request());
+ auto tensor = inference_request.get_input_tensor();
+
+ const auto byte_size = ov::util::get_memory_size(ov::element::f32, shape_size(tensor.get_shape()));
+
+ auto context = core->get_default_context(target_device).as<ov::intel_gpu::ocl::ClContext>();
+
+ createHeap(byte_size);
+
+ auto remote_tensor = context.create_tensor(ov::element::f32, tensor.get_shape(), shared_mem, ov::intel_gpu::MemType::SHARED_BUF);
+
+ ov::Tensor check_remote_tensor;
+ ASSERT_NO_THROW(check_remote_tensor = remote_tensor);
+ ASSERT_THROW(check_remote_tensor.data(), ov::Exception);
+
+ OV_ASSERT_NO_THROW(inference_request.set_input_tensor(check_remote_tensor));
+ OV_ASSERT_NO_THROW(inference_request.infer());
+}
+
+TEST_F(DX12RemoteRunTests, smoke_CheckRemoteTensorSharedBuChangingTensors) {
+ // Skip test according to plugin specific disabled_test_patterns() (if any)
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+ ov::CompiledModel compiled_model;
+ ov::InferRequest inference_request;
+ OV_ASSERT_NO_THROW(compiled_model = core->compile_model(ov_model, target_device, configuration));
+ OV_ASSERT_NO_THROW(inference_request = compiled_model.create_infer_request());
+ auto tensor = inference_request.get_input_tensor();
+ const auto byte_size = ov::util::get_memory_size(ov::element::f32, shape_size(tensor.get_shape()));
+ auto context = core->get_default_context(target_device).as<ov::intel_gpu::ocl::ClContext>();;
+ createHeap(byte_size);
+ auto remote_tensor = context.create_tensor(ov::element::f32, tensor.get_shape(), shared_mem, ov::intel_gpu::MemType::SHARED_BUF);
+ ov::Tensor check_remote_tensor;
+ ASSERT_NO_THROW(check_remote_tensor = remote_tensor);
+ ASSERT_THROW(check_remote_tensor.data(), ov::Exception);
+ OV_ASSERT_NO_THROW(inference_request.set_input_tensor(check_remote_tensor));
+ OV_ASSERT_NO_THROW(inference_request.infer());
+ // set random input tensor
+ std::vector<float> random_buffer_tensor(shape_size(tensor.get_shape()), 1);
+ ov::Tensor random_tensor_input{ov::element::f32, tensor.get_shape(), random_buffer_tensor.data()};
+
+ OV_ASSERT_NO_THROW(inference_request.set_input_tensor(random_tensor_input));
+ OV_ASSERT_NO_THROW(inference_request.infer());
+
+ // set random output tensor
+ auto output_tensor = inference_request.get_output_tensor();
+
+ std::vector<float> output_random_buffer_tensor(shape_size(output_tensor.get_shape()), 1);
+ ov::Tensor outputrandom_tensor_input{ov::element::f32, output_tensor.get_shape(), output_random_buffer_tensor.data()};
+ OV_ASSERT_NO_THROW(inference_request.set_output_tensor(outputrandom_tensor_input));
+ OV_ASSERT_NO_THROW(inference_request.infer());
+}
+
+TEST_F(DX12RemoteRunTests, smoke_CheckOutputDataFromMultipleRuns) {
+ // Skip test according to plugin specific disabled_test_patterns() (if any)
+ SKIP_IF_CURRENT_TEST_IS_DISABLED()
+
+ ov::CompiledModel compiled_model;
+ ov::InferRequest inference_request;
+ OV_ASSERT_NO_THROW(compiled_model = core->compile_model(ov_model, target_device, configuration));
+ OV_ASSERT_NO_THROW(inference_request = compiled_model.create_infer_request());
+ auto tensor = inference_request.get_input_tensor();
+
+ auto shape = tensor.get_shape();
+ const auto byte_size = ov::util::get_memory_size(ov::element::f32, shape_size(shape));
+ tensor = {};
+
+ createResources(byte_size);
+ void* mem;
+ comitted_resource.Get()->Map(0, nullptr, &mem);
+ for(int i = 0; i < shape_size(shape); ++i) {
+ static_cast<float*>(mem)[i] = 99;
+ }
+ comitted_resource.Get()->Unmap(0, nullptr);
+ copyResources(byte_size);
+
+ auto context = core->get_default_context(target_device).as<ov::intel_gpu::ocl::ClContext>();
+
+ auto output_tensor = inference_request.get_output_tensor();
+ std::vector<float> output_data_one(output_tensor.get_size());
+ ov::Tensor output_data_tensor_one{ov::element::f32, output_tensor.get_shape(), output_data_one.data()};
+
+ auto remote_tensor = context.create_tensor(ov::element::f32, shape, shared_mem, ov::intel_gpu::MemType::SHARED_BUF);
+ OV_ASSERT_NO_THROW(inference_request.set_input_tensor(remote_tensor));
+ OV_ASSERT_NO_THROW(inference_request.set_output_tensor(output_data_tensor_one));
+ OV_ASSERT_NO_THROW(inference_request.infer());
+
+ std::vector<float> output_data_two(output_tensor.get_size());
+ ov::Tensor output_data_tensor_two{ov::element::f32, output_tensor.get_shape(), output_data_two.data()};
+
+ std::vector<float> input_data(shape_size(shape), 99);
+ ov::Tensor input_data_tensor{ov::element::f32, shape, input_data.data()};
+ OV_ASSERT_NO_THROW(inference_request.set_input_tensor(input_data_tensor));
+ OV_ASSERT_NO_THROW(inference_request.set_output_tensor(output_data_tensor_two));
+ OV_ASSERT_NO_THROW(inference_request.infer());
+
+ EXPECT_NE(output_data_one.data(), output_data_two.data());
+ for (size_t i = 0; i < output_data_one.size(); ++i) {
+ EXPECT_FLOAT_EQ(output_data_one[i], output_data_two[i]) << "Output mismatch at index " << i << output_data_one[i] << " vs " << output_data_two[i];
+ }
+}
+} // namespace
+#endif // defined(_WIN32) && defined(ENABLE_DX12)