OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
[GPU] Report cpu_va_size and cpu_va_access in CPU_VA remote tensor properties (#39857)
Abhinav Sainie6cc7561d688
diff --git a/src/bindings/python/tests/test_runtime/test_remote_api.py b/src/bindings/python/tests/test_runtime/test_remote_api.pyindex b8b6dcf2c46..823293501c5 100644--- a/src/bindings/python/tests/test_runtime/test_remote_api.py+++ b/src/bindings/python/tests/test_runtime/test_remote_api.py@@ -3,6 +3,7 @@# SPDX-License-Identifier: Apache-2.0import ctypes+import mathimport mmapimport osimport pytest@@ -138,6 +139,39 @@ def test_create_tensor_from_host_address_gpu(device, to_address):assert np.array_equal(result.data, expected)+ "GPU" not in os.environ.get("TEST_DEVICE", ""),+ reason="Test can be only performed on GPU device!",+)+def test_cpu_va_tensor_params_round_trip_gpu(device):+ core = ov.Core()+ context = core.get_default_context(device)+ cacheline_size = core.get_property(device, "GPU_CACHELINE_SIZE")+ # Imported host memory has to be a multiple of the cacheline (OpenCL) and of the page size (Level Zero)+ expected = np.arange(math.lcm(cacheline_size, mmap.PAGESIZE) // 4, dtype=np.float32)+ buffer = page_aligned_array(expected.shape, np.float32)+ buffer[:] = expected++ tensor = context.create_tensor(+ ov.Type.f32,+ ov.Shape(expected.shape),+ {"SHARED_MEM_TYPE": "CPU_VA", "CPU_VA": buffer.ctypes.data, "CPU_VA_SIZE": buffer.nbytes},+ )++ # get_params() must expose everything create_tensor() needs to wrap the same memory again,+ # including the buffer size and access mode, not only the address.+ params = tensor.get_params()+ assert params["CPU_VA_SIZE"].value == buffer.nbytes+ # The tensor was created without CPU_VA_ACCESS, so the default READ_WRITE mode must be reported+ assert params["CPU_VA_ACCESS"].astype(str) == "READ_WRITE"++ alias = context.create_tensor(ov.Type.f32, ov.Shape(expected.shape), params)+ result = ov.Tensor(ov.Type.f32, ov.Shape(expected.shape))+ alias.copy_to(result)++ assert np.array_equal(result.data, expected)++@pytest.mark.skipif("GPU" not in os.environ.get("TEST_DEVICE", ""),reason="Test can be only performed on GPU device!",diff --git a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp b/src/plugins/intel_gpu/src/plugin/remote_tensor.cppindex e301b7c913b..2183d55142b 100644--- a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp+++ b/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp@@ -190,6 +190,20 @@ RemoteTensorImpl::RemoteTensorImpl(RemoteContextImpl::Ptr context,// TODO: enable RemoteTensor for SYCL_RT once SYCL interop is wired up.OPENVINO_THROW("[GPU] RemoteTensor is not supported with SYCL runtime yet");#endif+ if (m_mem_type == TensorType::BT_CPU_VA && m_va_mem.size < 0) {+ // Resolve the "derive from tensor shape" size (-1) before the cache key is computed, so that the cache key,+ // the imported host buffer and the reported cpu_va_size always agree. Otherwise re-wrapping the memory with+ // the reported (explicit) size would miss the cache and import the same host memory into a second buffer.+ // Zero dimensions are counted as 1, in the same way allocate() imports empty tensors.+ auto import_shape = m_shape;+ for (auto& dim : import_shape) {+ if (dim == 0) {+ dim = 1;+ }+ }+ const cldnn::layout import_layout{ov::PartialShape{import_shape}, m_element_type, m_layout.format};+ m_va_mem.size = static_cast<int64_t>(import_layout.bytes_count());+ }update_hash();allocate();}@@ -402,7 +416,8 @@ void RemoteTensorImpl::allocate() {break;}case TensorType::BT_CPU_VA: {- const auto buffer_size = m_va_mem.size > -1 ? m_va_mem.size : m_layout.bytes_count();+ // The size was resolved in the constructor if the tensor was created with size -1.+ const auto buffer_size = static_cast<size_t>(m_va_mem.size);if (m_va_mem.access == ov::intel_gpu::AccessMode::READ) {// host_read_only is set for the file-mmap case, since the host side will never write it either.m_memory_object = engine.create_hostbuffer(static_cast<const void*>(m_va_mem.ptr),@@ -557,14 +572,21 @@ void RemoteTensorImpl::update_properties() {ov::intel_gpu::mem_handle(params.mem),};break;- case TensorType::BT_CPU_VA:+ case TensorType::BT_CPU_VA: {+ // Report the size and access mode together with the pointer so that the properties returned by+ // get_properties() are sufficient to re-wrap the same memory with create_tensor(), which requires+ // cpu_va_size. This is the size the host buffer was imported with (resolved in the constructor for size -1),+ // so it is also correct for tensors that were found in the memory cache.m_properties = {ov::intel_gpu::shared_mem_type(ov::intel_gpu::SharedMemType::CPU_VA),ov::intel_gpu::ocl_context(params.context),ov::intel_gpu::mem_handle(params.mem),ov::intel_gpu::cpu_va(m_va_mem.ptr),+ ov::intel_gpu::cpu_va_size(m_va_mem.size),+ ov::intel_gpu::cpu_va_access(m_va_mem.access),};break;+ }case TensorType::BT_USM_HOST_INTERNAL:m_properties = {ov::intel_gpu::shared_mem_type(ov::intel_gpu::SharedMemType::USM_HOST_BUFFER),diff --git a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/ocl_remote_tensor_tests.cpp b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/ocl_remote_tensor_tests.cppindex e6b6f4b0260..505af02a411 100644--- a/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/ocl_remote_tensor_tests.cpp+++ b/src/plugins/intel_gpu/tests/functional/remote_tensor_tests/ocl_remote_tensor_tests.cpp@@ -746,6 +746,119 @@ INSTANTIATE_TEST_SUITE_P(::testing::ValuesIn(ov_dynamic)),OVRemoteTensorInputBlob_Test::getTestCaseName);+TEST(OVRemoteTensorTests, smoke_cpuVaTensorParamsRoundTrip) {+ ov::Core core;+ auto context = core.get_default_context(ov::test::utils::DEVICE_GPU);++ // CPU_VA memory has to be cacheline aligned and its size a multiple of the cacheline, page alignment satisfies both+ const auto page_size = static_cast<size_t>(ov::util::get_system_page_size());+ ASSERT_GT(page_size, 0u);+ const ov::Shape shape{page_size / sizeof(float)};+ const size_t element_count = ov::shape_size(shape);+ const size_t byte_size = element_count * sizeof(float);++ void* ptr = ov::util::aligned_alloc(byte_size, page_size);+ ASSERT_NE(ptr, nullptr);+ auto* data = static_cast<float*>(ptr);+ for (size_t i = 0; i < element_count; ++i) {+ data[i] = static_cast<float>(i);+ }++ {+ const ov::AnyMap params{{ov::intel_gpu::shared_mem_type.name(), ov::intel_gpu::SharedMemType::CPU_VA},+ {ov::intel_gpu::cpu_va.name(), static_cast<ov::intel_gpu::gpu_handle_param>(ptr)},+ {ov::intel_gpu::cpu_va_size.name(), static_cast<int64_t>(byte_size)},+ {ov::intel_gpu::cpu_va_access.name(), ov::intel_gpu::AccessMode::READ}};+ auto tensor = context.create_tensor(ov::element::f32, shape, params);++ // get_params() must expose everything create_tensor() needs to wrap the same memory again:+ // not only the address, but also the buffer size (mandatory for create_tensor) and the access mode.+ const auto reported = tensor.get_params();+ ASSERT_EQ(reported.count(ov::intel_gpu::cpu_va.name()), 1u);+ ASSERT_EQ(reported.count(ov::intel_gpu::cpu_va_size.name()), 1u);+ ASSERT_EQ(reported.count(ov::intel_gpu::cpu_va_access.name()), 1u);+ EXPECT_EQ(reported.at(ov::intel_gpu::cpu_va.name()).as<ov::intel_gpu::gpu_handle_param>(), ptr);+ EXPECT_EQ(reported.at(ov::intel_gpu::cpu_va_size.name()).as<int64_t>(), static_cast<int64_t>(byte_size));+ EXPECT_EQ(reported.at(ov::intel_gpu::cpu_va_access.name()).as<ov::intel_gpu::AccessMode>(),+ ov::intel_gpu::AccessMode::READ);++ auto alias = context.create_tensor(ov::element::f32, shape, reported);+ ov::Tensor host(ov::element::f32, shape);+ alias.copy_to(host);+ const auto* host_data = host.data<float>();+ for (size_t i = 0; i < element_count; ++i) {+ ASSERT_FLOAT_EQ(host_data[i], static_cast<float>(i)) << "Mismatch at index " << i;+ }+ }++ ov::util::aligned_free(ptr);+}++TEST(OVRemoteTensorTests, smoke_cpuVaTensorDerivedSizeRoundTripReusesBuffer) {+ ov::Core core;+ auto context = core.get_default_context(ov::test::utils::DEVICE_GPU);++ const auto page_size = static_cast<size_t>(ov::util::get_system_page_size());+ ASSERT_GT(page_size, 0u);+ const ov::Shape shape{page_size / sizeof(float)};++ void* ptr = ov::util::aligned_alloc(page_size, page_size);+ ASSERT_NE(ptr, nullptr);++ {+ // Size -1 means "derive from the tensor shape"; the access mode is left at its READ_WRITE default+ const ov::AnyMap params{{ov::intel_gpu::shared_mem_type.name(), ov::intel_gpu::SharedMemType::CPU_VA},+ {ov::intel_gpu::cpu_va.name(), static_cast<ov::intel_gpu::gpu_handle_param>(ptr)},+ {ov::intel_gpu::cpu_va_size.name(), static_cast<int64_t>(-1)}};+ auto tensor = context.create_tensor(ov::element::f32, shape, params);++ const auto reported = tensor.get_params();+ EXPECT_EQ(reported.at(ov::intel_gpu::cpu_va_size.name()).as<int64_t>(), static_cast<int64_t>(page_size));+ EXPECT_EQ(reported.at(ov::intel_gpu::cpu_va_access.name()).as<ov::intel_gpu::AccessMode>(), ov::intel_gpu::AccessMode::READ_WRITE);++ // Wrapping the same memory again with the reported (explicit) size must reuse the already imported buffer+ // instead of importing the same host memory into a second buffer.+ auto alias = context.create_tensor(ov::element::f32, shape, reported);+ EXPECT_EQ(alias.get_params().at(ov::intel_gpu::mem_handle.name()).as<ov::intel_gpu::gpu_handle_param>(),+ reported.at(ov::intel_gpu::mem_handle.name()).as<ov::intel_gpu::gpu_handle_param>());+ }++ ov::util::aligned_free(ptr);+}++TEST(OVRemoteTensorTests, smoke_cpuVaTensorEmptyShapeReportsImportedSize) {+ ov::Core core;+ auto context = core.get_default_context(ov::test::utils::DEVICE_GPU);++ const auto page_size = static_cast<size_t>(ov::util::get_system_page_size());+ ASSERT_GT(page_size, 0u);+ // An empty tensor imports its memory as if the zero dimension were 1, i.e. page_size bytes here+ const ov::Shape shape{0, page_size / sizeof(float)};++ void* ptr = ov::util::aligned_alloc(page_size, page_size);+ ASSERT_NE(ptr, nullptr);++ {+ const ov::AnyMap params{{ov::intel_gpu::shared_mem_type.name(), ov::intel_gpu::SharedMemType::CPU_VA},+ {ov::intel_gpu::cpu_va.name(), static_cast<ov::intel_gpu::gpu_handle_param>(ptr)},+ {ov::intel_gpu::cpu_va_size.name(), static_cast<int64_t>(-1)}};+ auto first = context.create_tensor(ov::element::f32, shape, params);+ // The second tensor is served from the memory cache and must report the same imported size+ auto second = context.create_tensor(ov::element::f32, shape, params);+ for (const auto& tensor : {first, second}) {+ EXPECT_EQ(tensor.get_params().at(ov::intel_gpu::cpu_va_size.name()).as<int64_t>(), static_cast<int64_t>(page_size));+ }++ // The reported parameters must be usable to wrap the memory again+ ov::RemoteTensor alias;+ OV_ASSERT_NO_THROW(alias = context.create_tensor(ov::element::f32, shape, second.get_params()));+ EXPECT_EQ(alias.get_params().at(ov::intel_gpu::mem_handle.name()).as<ov::intel_gpu::gpu_handle_param>(),+ first.get_params().at(ov::intel_gpu::mem_handle.name()).as<ov::intel_gpu::gpu_handle_param>());+ }++ ov::util::aligned_free(ptr);+}+TEST(OVRemoteTensorTests, smoke_MixedTensorTypes) {#if defined(ANDROID)GTEST_SKIP();