Skip to content

Commit 42335aa

Browse files
[GPU] Fix not to request 0 bytes layout allocation to OpenCL
1 parent 64eb742 commit 42335aa

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/plugins/intel_gpu/src/runtime/ocl/ocl_engine.cpp

+30-5
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ allocation_type ocl_engine::detect_usm_allocation_type(const void* memory) const
128128
bool ocl_engine::check_allocatable(const layout& layout, allocation_type type) {
129129
OPENVINO_ASSERT(supports_allocation(type) || type == allocation_type::cl_mem, "[GPU] Unsupported allocation type: ", type);
130130

131+
// if (layout.bytes_count() == 0) {
132+
// return false;
133+
// }
134+
131135
bool exceed_allocatable_mem_size = (layout.bytes_count() > get_device_info().max_alloc_mem_size);
132136

133137
// When dynamic shape upper bound makes bigger buffer, then return false.
@@ -172,25 +176,46 @@ bool ocl_engine::check_allocatable(const layout& layout, allocation_type type) {
172176
memory::ptr ocl_engine::allocate_memory(const layout& layout, allocation_type type, bool reset) {
173177
OPENVINO_ASSERT(!layout.is_dynamic() || layout.has_upper_bound(), "[GPU] Can't allocate memory for dynamic layout");
174178

175-
check_allocatable(layout, type);
179+
if (!check_allocatable(layout, type))
180+
return nullptr;
181+
182+
auto zero_bytes_layout = false;
183+
auto non_zero_layout = layout;
184+
if (layout.bytes_count() == 0) {
185+
cldnn::layout zero_dim_layout = layout;
186+
auto mem_ps = zero_dim_layout.get_partial_shape();
187+
for (size_t k = 0; k < mem_ps.size(); k++) {
188+
if (mem_ps[k] == 0)
189+
mem_ps[k] = 1;
190+
}
191+
192+
non_zero_layout = cldnn::layout(mem_ps, zero_dim_layout.data_type, zero_dim_layout.format);
193+
zero_bytes_layout = true;
194+
}
195+
196+
176197

177198
try {
178199
memory::ptr res = nullptr;
179200
if (layout.format.is_image_2d()) {
180-
res = std::make_shared<ocl::gpu_image2d>(this, layout);
201+
res = std::make_shared<ocl::gpu_image2d>(this, non_zero_layout);
181202
} else if (type == allocation_type::cl_mem) {
182-
res = std::make_shared<ocl::gpu_buffer>(this, layout);
203+
res = std::make_shared<ocl::gpu_buffer>(this, non_zero_layout);
183204
} else {
184-
res = std::make_shared<ocl::gpu_usm>(this, layout, type);
205+
res = std::make_shared<ocl::gpu_usm>(this, non_zero_layout, type);
185206
}
186207

187-
if (reset || res->is_memory_reset_needed(layout)) {
208+
if (reset || res->is_memory_reset_needed(non_zero_layout)) {
188209
auto ev = res->fill(get_service_stream());
189210
if (ev) {
190211
get_service_stream().wait_for_events({ev});
191212
}
192213
}
193214

215+
if (zero_bytes_layout) {
216+
res = reinterpret_buffer(*res, layout);
217+
}
218+
194219
return res;
195220
} catch (const cl::Error& clErr) {
196221
switch (clErr.err()) {

0 commit comments

Comments
 (0)