Skip to content

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

[GPU] Handle zero-size buffers in runtime instead of RemoteTensor WA (#39835)

Roman Lyamin5fb0379715ca
diff --git a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp b/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp
index 46acf462933..6662a3f5ddc 100644
--- a/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp
+++ b/src/plugins/intel_gpu/src/plugin/remote_tensor.cpp
@@ -366,18 +366,6 @@ void RemoteTensorImpl::allocate() {
}
auto& engine = context->get_engine();
-
- // Currently, clDeviceMemAllocINTEL returns memory address allocated to other input blob if the current blob is empty
- // W/A for this issue:
- // Allocate with non-empty shape and then reinterprete with original shape
- auto shape_copy = m_shape;
- for (auto &i : shape_copy) {
- if (i == 0)
- i = 1;
- }
-
- m_layout.set_partial_shape(shape_copy);
-
const bool reset = false;
switch (m_mem_type) {
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 c10aeada372..64c11ba801b 100644
--- a/src/plugins/intel_gpu/src/runtime/ocl/ocl_memory.cpp
+++ b/src/plugins/intel_gpu/src/runtime/ocl/ocl_memory.cpp
@@ -11,6 +11,7 @@
#include "ocl_engine.hpp"
#include "ocl_stream.hpp"
#include "ocl_event.hpp"
+#include <algorithm>
#include <stdexcept>
#include <vector>
@@ -50,11 +51,10 @@ static int get_cl_map_type(mem_lock_type type) {
}
}
-gpu_buffer::gpu_buffer(ocl_engine* engine,
- const layout& layout)
- : memory(engine, layout, allocation_type::cl_mem, nullptr)
- , _buffer(engine->get_cl_context(), CL_MEM_READ_WRITE, size()) {
- m_mem_tracker = std::make_shared<MemoryTracker>(engine, _buffer.get(), layout.bytes_count(), allocation_type::cl_mem);
+gpu_buffer::gpu_buffer(ocl_engine* engine, const layout& layout) : memory(engine, layout, allocation_type::cl_mem, nullptr) {
+ const auto actual_bytes_count = std::max<size_t>(_bytes_count, 1);
+ _buffer = cl::Buffer(engine->get_cl_context(), CL_MEM_READ_WRITE, actual_bytes_count);
+ m_mem_tracker = std::make_shared<MemoryTracker>(engine, _buffer.get(), actual_bytes_count, allocation_type::cl_mem);
}
gpu_buffer::gpu_buffer(ocl_engine* engine,
@@ -67,7 +67,7 @@ gpu_buffer::gpu_buffer(ocl_engine* engine,
void* gpu_buffer::lock(const stream& stream, mem_lock_type type) {
const auto& cl_stream = downcast<const ocl_stream>(stream);
std::lock_guard<std::mutex> locker(_mutex);
- if (0 == _lock_count) {
+ if (0 == _lock_count && size() != 0) {
try {
_mapped_ptr = cl_stream.get_cl_queue().enqueueMapBuffer(_buffer, CL_TRUE, get_cl_map_type(type), 0, size());
} catch (cl::Error const& err) {
@@ -82,7 +82,7 @@ void gpu_buffer::unlock(const stream& stream) {
const auto& cl_stream = downcast<const ocl_stream>(stream);
std::lock_guard<std::mutex> locker(_mutex);
_lock_count--;
- if (0 == _lock_count) {
+ if (0 == _lock_count && _mapped_ptr != nullptr) {
try {
cl::Event ev;
cl_stream.get_cl_queue().enqueueUnmapMemObject(_buffer, _mapped_ptr, nullptr, &ev);
@@ -535,7 +535,9 @@ void* gpu_usm::lock(const stream& stream, mem_lock_type type) {
std::lock_guard<std::mutex> locker(_mutex);
if (0 == _lock_count) {
const auto& cl_stream = downcast<const ocl_stream>(stream);
- if (get_allocation_type() == allocation_type::usm_device) {
+ if (get_allocation_type() == allocation_type::usm_device && _bytes_count == 0) {
+ _mapped_ptr = nullptr;
+ } else if (get_allocation_type() == allocation_type::usm_device) {
GPU_DEBUG_LOG << "Copy usm_device buffer to host buffer." << std::endl;
_host_buffer.allocateHost(_bytes_count);
// Always copy device data to host buffer (treat write as read_write internally).
@@ -551,7 +553,7 @@ void* gpu_usm::lock(const stream& stream, mem_lock_type type) {
} else {
_mapped_ptr = _buffer.get();
}
- } else if (get_allocation_type() == allocation_type::usm_device) {
+ } else if (get_allocation_type() == allocation_type::usm_device && _bytes_count != 0) {
if (type != mem_lock_type::read) {
_copy_back_to_device = true;
}
@@ -565,7 +567,7 @@ void gpu_usm::unlock(const stream& stream) {
OPENVINO_ASSERT(_lock_count != 0, "[GPU] Trying to unlock an already unlocked buffer");
_lock_count--;
if (0 == _lock_count) {
- if (get_allocation_type() == allocation_type::usm_device) {
+ if (get_allocation_type() == allocation_type::usm_device && _bytes_count != 0) {
if (_copy_back_to_device) {
const auto& cl_stream = downcast<const ocl_stream>(stream);
try {
diff --git a/src/plugins/intel_gpu/src/runtime/ze/ze_memory.cpp b/src/plugins/intel_gpu/src/runtime/ze/ze_memory.cpp
index 2acd3d21918..d81799bdfb3 100644
--- a/src/plugins/intel_gpu/src/runtime/ze/ze_memory.cpp
+++ b/src/plugins/intel_gpu/src/runtime/ze/ze_memory.cpp
@@ -372,7 +372,10 @@ void* gpu_usm::lock_impl(const stream& stream, mem_lock_type type) {
if (0 == _lock_count) {
auto& _ze_stream = downcast<const ze_stream>(stream);
auto alloc_type = get_allocation_type();
- if (alloc_type == allocation_type::usm_device || alloc_type == allocation_type::cl_mem) {
+ const bool is_device_mem = alloc_type == allocation_type::usm_device || alloc_type == allocation_type::cl_mem;
+ if (is_device_mem && _bytes_count == 0) {
+ _mapped_ptr = nullptr;
+ } else if (is_device_mem) {
GPU_DEBUG_LOG << "Copy usm_device buffer to host buffer." << std::endl;
auto *zero_engine = downcast<ze_engine>(_engine);
_host_buffer = allocate_usm_host(zero_engine->get_context(), _bytes_count);
@@ -429,6 +432,18 @@ void* gpu_usm::buffer_ptr() const {
}
event::ptr gpu_usm::fill(stream& stream, unsigned char pattern, const std::vector<event::ptr>& dep_events, bool blocking) {
+ if (_bytes_count == 0) {
+ if (dep_events.empty()) {
+ return create_event(stream, _bytes_count);
+ }
+ // Nothing to fill, but the returned event must still complete after dep_events like a real fill
+ auto ev = stream.enqueue_marker(dep_events);
+ if (blocking) {
+ ev->wait();
+ }
+ return ev;
+ }
+
auto& _ze_stream = downcast<ze_stream>(stream);
auto ev = _ze_stream.create_base_event();
auto ev_ze = downcast<ze::ze_base_event>(ev.get())->get_handle();
diff --git a/src/plugins/intel_gpu/tests/unit/module_tests/engine_test.cpp b/src/plugins/intel_gpu/tests/unit/module_tests/engine_test.cpp
index ae2fec20957..73df607b491 100644
--- a/src/plugins/intel_gpu/tests/unit/module_tests/engine_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/module_tests/engine_test.cpp
@@ -54,6 +54,46 @@ TEST(engine, memory_creation) {
ASSERT_EQ(std::dynamic_pointer_cast<simple_attached_memory>(mem)->lock(get_test_stream(), mem_lock_type::read), host_data.data());
}
+TEST(engine, zero_size_memory_creation) {
+ auto& engine = get_test_engine();
+ auto& stream = get_test_stream();
+ const layout zero_layout = {ov::PartialShape{1, 0, 2}, data_types::f32, format::bfyx};
+
+ std::vector<allocation_type> alloc_types = {allocation_type::cl_mem};
+ for (auto type : {allocation_type::usm_host, allocation_type::usm_device}) {
+ if (engine.supports_allocation(type))
+ alloc_types.push_back(type);
+ }
+
+ for (auto type : alloc_types) {
+ SCOPED_TRACE(type);
+ memory::ptr mem0 = nullptr;
+ memory::ptr mem1 = nullptr;
+ OV_ASSERT_NO_THROW(mem0 = engine.allocate_memory(zero_layout, type));
+ OV_ASSERT_NO_THROW(mem1 = engine.allocate_memory(zero_layout, type));
+ ASSERT_EQ(mem0->get_layout(), zero_layout);
+ ASSERT_EQ(mem0->size(), 0u);
+ ASSERT_EQ(mem0->get_allocation_type(), type);
+
+ // Empty allocations must still get distinct backing buffers
+ ASSERT_NE(mem0->get_internal_params().mem, nullptr);
+ ASSERT_NE(mem0->get_internal_params().mem, mem1->get_internal_params().mem);
+
+ OV_ASSERT_NO_THROW(mem0->fill(stream));
+ OV_ASSERT_NO_THROW((mem_lock<uint8_t, mem_lock_type::read_write>(mem0, stream)));
+ }
+}
+
+TEST(engine, zero_size_reinterpreted_cl_mem_lock) {
+ auto& engine = get_test_engine();
+ auto& stream = get_test_stream();
+ auto mem = engine.allocate_memory({ov::PartialShape{1, 1, 2}, data_types::f32, format::bfyx}, allocation_type::cl_mem);
+ auto zero_mem = engine.reinterpret_buffer(*mem, {ov::PartialShape{1, 0, 2}, data_types::f32, format::bfyx});
+
+ ASSERT_EQ(zero_mem->size(), 0u);
+ OV_ASSERT_NO_THROW((mem_lock<uint8_t, mem_lock_type::read>(zero_mem, stream)));
+}
+
TEST(engine, large_allocation) {
// This test is used for manual testing only.
GTEST_SKIP();
diff --git a/src/plugins/intel_gpu/tests/unit/module_tests/network_test.cpp b/src/plugins/intel_gpu/tests/unit/module_tests/network_test.cpp
index 394dff030c5..14dcd6f2922 100644
--- a/src/plugins/intel_gpu/tests/unit/module_tests/network_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/module_tests/network_test.cpp
@@ -5,6 +5,8 @@
#include "test_utils.h"
#include "intel_gpu/graph/network.hpp"
+#include "intel_gpu/plugin/remote_context.hpp"
+#include "intel_gpu/plugin/remote_tensor.hpp"
#include "intel_gpu/primitives/input_layout.hpp"
#include "intel_gpu/primitives/data.hpp"
#include "intel_gpu/primitives/activation.hpp"
@@ -55,6 +57,55 @@ TEST(network_test, model_with_empty_input_is_not_dynamic) {
ASSERT_FALSE(net.is_dynamic());
}
+static void run_zero_size_remote_tensor_input(ov::intel_gpu::TensorType tensor_type, const ov::PartialShape& port_shape) {
+ auto& engine = get_test_engine();
+ const ov::Shape zero_shape{1, 0, 2};
+ auto context = std::make_shared<ov::intel_gpu::RemoteContextImpl>("GPU", std::vector<cldnn::device::ptr>{engine.get_device()});
+ auto remote = std::make_shared<ov::intel_gpu::RemoteTensorImpl>(context, zero_shape, ov::element::f32, tensor_type);
+ auto input_mem = remote->get_memory();
+ ASSERT_EQ(remote->get_shape(), zero_shape);
+ ASSERT_EQ(remote->get_byte_size(), 0u);
+ ASSERT_EQ(input_mem->get_layout().get_partial_shape(), ov::PartialShape(zero_shape));
+ ASSERT_EQ(input_mem->size(), 0u);
+
+ auto const_mem = engine.allocate_memory({{1, 1, 2}, data_types::f32, format::bfyx});
+ set_values(const_mem, {1.0f, 2.0f});
+
+ topology topology;
+ topology.add(input_layout("input0", layout{port_shape, data_types::f32, format::bfyx}));
+ topology.add(data("input1", const_mem));
+ topology.add(concatenation("output", {input_info("input0"), input_info("input1")}, 1));
+
+ network net(engine, topology, {ov::intel_gpu::allow_new_shape_infer(true)});
+ OV_ASSERT_NO_THROW(net.set_input_data("input0", input_mem));
+ ASSERT_EQ(net.get_primitive("input0")->output_memory().get_layout().get_partial_shape(), ov::PartialShape(zero_shape));
+
+ // Remote memory must keep the zero dim, otherwise shape inference sees a non-empty input
+ auto outputs = net.execute();
+ auto output_mem = outputs.at("output").get_memory();
+ ASSERT_EQ(output_mem->get_layout().get_partial_shape(), ov::PartialShape({1, 1, 2}));
+ mem_lock<float, mem_lock_type::read> output_ptr(output_mem, get_test_stream());
+ ASSERT_EQ(output_ptr[0], 1.0f);
+ ASSERT_EQ(output_ptr[1], 2.0f);
+}
+
+TEST(network_test, zero_size_remote_tensor_input) {
+ auto& engine = get_test_engine();
+ std::vector<ov::intel_gpu::TensorType> tensor_types = {ov::intel_gpu::TensorType::BT_BUF_INTERNAL};
+ if (engine.supports_allocation(allocation_type::usm_device))
+ tensor_types.push_back(ov::intel_gpu::TensorType::BT_USM_DEVICE_INTERNAL);
+ if (engine.supports_allocation(allocation_type::usm_host))
+ tensor_types.push_back(ov::intel_gpu::TensorType::BT_USM_HOST_INTERNAL);
+
+ for (auto tensor_type : tensor_types) {
+ for (const auto& port_shape : {ov::PartialShape{1, 0, 2}, ov::PartialShape{1, -1, 2}}) {
+ SCOPED_TRACE(static_cast<int>(tensor_type));
+ SCOPED_TRACE(port_shape.to_string());
+ run_zero_size_remote_tensor_input(tensor_type, port_shape);
+ }
+ }
+}
+
TEST(network_test, model_with_dynamic_input_is_dynamic) {
auto& engine = get_test_engine();
ov::PartialShape input_shape = {1, -1};
diff --git a/src/plugins/intel_gpu/tests/unit/module_tests/ze/ze_usm_memory_test.cpp b/src/plugins/intel_gpu/tests/unit/module_tests/ze/ze_usm_memory_test.cpp
index 97195b70c17..a94dc61861e 100644
--- a/src/plugins/intel_gpu/tests/unit/module_tests/ze/ze_usm_memory_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/module_tests/ze/ze_usm_memory_test.cpp
@@ -236,4 +236,29 @@ TEST(ze_usm_memory, copy_small_to_large_buffer) {
OV_ASSERT_NO_THROW(small_mem->copy_to(*ctx.ze_test_stream, *large_mem, true));
}
+TEST(ze_usm_memory, zero_size_device_memory) {
+ auto ctx = create_ze_test_context();
+ const layout zero_layout = {{1, 0, 2}, data_types::f32, format::bfyx};
+
+ for (auto alloc_type : {allocation_type::usm_device, allocation_type::cl_mem}) {
+ if (!ctx.ze_test_engine->supports_allocation(alloc_type))
+ continue;
+ SCOPED_TRACE(alloc_type);
+ memory::ptr mem0 = nullptr;
+ memory::ptr mem1 = nullptr;
+ OV_ASSERT_NO_THROW(mem0 = ctx.ze_test_engine->allocate_memory(zero_layout, alloc_type));
+ OV_ASSERT_NO_THROW(mem1 = ctx.ze_test_engine->allocate_memory(zero_layout, alloc_type));
+ ASSERT_EQ(mem0->get_layout(), zero_layout);
+ ASSERT_EQ(mem0->size(), 0u);
+ ASSERT_NE(mem0->buffer_ptr(), nullptr);
+ ASSERT_NE(mem0->buffer_ptr(), mem1->buffer_ptr());
+
+ OV_ASSERT_NO_THROW(mem0->fill(*ctx.ze_test_stream));
+ void* ptr = nullptr;
+ OV_ASSERT_NO_THROW(ptr = mem0->lock(*ctx.ze_test_stream, mem_lock_type::read_write));
+ ASSERT_EQ(ptr, nullptr);
+ OV_ASSERT_NO_THROW(mem0->unlock(*ctx.ze_test_stream));
+ }
+}
+
#endif // OV_GPU_WITH_ZE_RT