Skip to content

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

[GPU] Fix out-of-bound access in convolution_gpu_bfyx_f16_depthwise kernel (#34285)

Roman Lyamin0aedf78616ae
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/convolution_gpu_bfyx_f16_depthwise.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/convolution_gpu_bfyx_f16_depthwise.cl
index 5a73d05e716..639b070deaf 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/convolution_gpu_bfyx_f16_depthwise.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/convolution_gpu_bfyx_f16_depthwise.cl
@@ -83,12 +83,27 @@ KERNEL(convolution_gpu_bfyx_f16_depthwise)(
INPUT_TYPE8 src_block_0 = DT_INPUT_BLOCK_READ8(input, input_offset + (input_y + 0) * input_y_pitch + (input_x) * input_x_pitch);
INPUT_TYPE8 src_block_1 = DT_INPUT_BLOCK_READ8(input, input_offset + (input_y + 1) * input_y_pitch + (input_x) * input_x_pitch);
INPUT_TYPE8 src_block_2 = DT_INPUT_BLOCK_READ8(input, input_offset + (input_y + 2) * input_y_pitch + (input_x) * input_x_pitch);
- INPUT_TYPE src_tail_00 = DT_INPUT_BLOCK_READ(input, input_offset + (input_y + 0) * input_y_pitch + (input_x + 8) * input_x_pitch);
- INPUT_TYPE src_tail_01 = DT_INPUT_BLOCK_READ(input, input_offset + (input_y + 0) * input_y_pitch + (input_x + 9) * input_x_pitch);
- INPUT_TYPE src_tail_10 = DT_INPUT_BLOCK_READ(input, input_offset + (input_y + 1) * input_y_pitch + (input_x + 8) * input_x_pitch);
- INPUT_TYPE src_tail_11 = DT_INPUT_BLOCK_READ(input, input_offset + (input_y + 1) * input_y_pitch + (input_x + 9) * input_x_pitch);
- INPUT_TYPE src_tail_20 = DT_INPUT_BLOCK_READ(input, input_offset + (input_y + 2) * input_y_pitch + (input_x + 8) * input_x_pitch);
- INPUT_TYPE src_tail_21 = DT_INPUT_BLOCK_READ(input, input_offset + (input_y + 2) * input_y_pitch + (input_x + 9) * input_x_pitch);
+
+ const bool valid_x6 = (x + 6) < OUTPUT_SIZE_X;
+ const bool valid_x7 = (x + 7) < OUTPUT_SIZE_X;
+
+ INPUT_TYPE src_tail_00 = INPUT0_VAL_ZERO;
+ INPUT_TYPE src_tail_01 = INPUT0_VAL_ZERO;
+ INPUT_TYPE src_tail_10 = INPUT0_VAL_ZERO;
+ INPUT_TYPE src_tail_11 = INPUT0_VAL_ZERO;
+ INPUT_TYPE src_tail_20 = INPUT0_VAL_ZERO;
+ INPUT_TYPE src_tail_21 = INPUT0_VAL_ZERO;
+
+ if (valid_x6) {
+ src_tail_00 = DT_INPUT_BLOCK_READ(input, input_offset + (input_y + 0) * input_y_pitch + (input_x + 8) * input_x_pitch);
+ src_tail_10 = DT_INPUT_BLOCK_READ(input, input_offset + (input_y + 1) * input_y_pitch + (input_x + 8) * input_x_pitch);
+ src_tail_20 = DT_INPUT_BLOCK_READ(input, input_offset + (input_y + 2) * input_y_pitch + (input_x + 8) * input_x_pitch);
+ }
+ if (valid_x7) {
+ src_tail_01 = DT_INPUT_BLOCK_READ(input, input_offset + (input_y + 0) * input_y_pitch + (input_x + 9) * input_x_pitch);
+ src_tail_11 = DT_INPUT_BLOCK_READ(input, input_offset + (input_y + 1) * input_y_pitch + (input_x + 9) * input_x_pitch);
+ src_tail_21 = DT_INPUT_BLOCK_READ(input, input_offset + (input_y + 2) * input_y_pitch + (input_x + 9) * input_x_pitch);
+ }
#if X_BLOCK_SIZE == 8
for (uint i = 0; i < X_BLOCK_SIZE - 2; i++)
@@ -105,7 +120,7 @@ KERNEL(convolution_gpu_bfyx_f16_depthwise)(
dst[i] = mad(src_block_2[i + 1], wei_21, dst[i]);
dst[i] = mad(src_block_2[i + 2], wei_22, dst[i]);
}
- {
+ if (valid_x6) {
dst[6] = mad(src_block_0[6], wei_00, dst[6]);
dst[6] = mad(src_block_0[7], wei_01, dst[6]);
dst[6] = mad(src_tail_00, wei_02, dst[6]);
@@ -118,7 +133,7 @@ KERNEL(convolution_gpu_bfyx_f16_depthwise)(
dst[6] = mad(src_block_2[7], wei_21, dst[6]);
dst[6] = mad(src_tail_20, wei_22, dst[6]);
}
- {
+ if (valid_x7) {
dst[7] = mad(src_block_0[7], wei_00, dst[7]);
dst[7] = mad(src_tail_00, wei_01, dst[7]);
dst[7] = mad(src_tail_01, wei_02, dst[7]);
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp
index 5815586a639..4b855b6bc6f 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/convolution_gpu_test.cpp
@@ -11503,6 +11503,104 @@ TEST(export_import_convolution_f32_gpu, convolution_gpu_bfyx_f16_depthwise_x_blo
test_convolution_f32_gpu_convolution_gpu_bfyx_f16_depthwise_x_block_size_1<ov::float16>(true);
}
+TEST(convolution_f32_fw_gpu, convolution_gpu_f32_convolution_gpu_bfyx_f16_depthwise) {
+ auto& engine = get_test_engine();
+
+ if (engine.get_device_info().supports_immad) {
+ return;
+ }
+
+ tests::random_generator rg(GET_SUITE_NAME);
+
+ // Tail case for X-blocked path (8 + tail)
+ constexpr int b = 1;
+ constexpr int f = 16;
+ constexpr int y = 1;
+ constexpr int x = 9;
+ constexpr int filter_size = 3;
+
+ auto input_data = rg.generate_random_4d<float>(b, f, y, x, -1, 1);
+ auto weights_data = rg.generate_random_4d<float>(f, 1, filter_size, filter_size, -1, 1);
+
+ auto input = engine.allocate_memory({data_types::f32, format::bfyx, tensor{b, f, x, y}});
+ auto weights = engine.allocate_memory({
+ data_types::f32,
+ format::goiyx,
+ tensor{group(f), batch(1), feature(1), spatial(filter_size, filter_size)}
+ });
+
+ set_values(input, flatten_4d<float>(format::bfyx, input_data));
+ set_values(weights, flatten_4d<float>(format::bfyx, weights_data));
+
+ auto run_depthwise = [&]() -> std::vector<float> {
+ topology topology(
+ input_layout("input", input->get_layout()),
+ reorder("input_fsv16", input_info("input"), {data_types::f32, format::b_fs_yx_fsv16, tensor{b, f, x, y}}),
+ data("weights", weights),
+ convolution("conv",
+ input_info("input_fsv16"),
+ "weights",
+ no_bias,
+ f, // groups
+ {1, 1}, // stride
+ {1, 1}, // dilation
+ {1, 1}, // pad begin
+ {1, 1}, // pad end
+ true), // grouped
+ reorder("out", input_info("conv"), {data_types::f32, format::bfyx, tensor{b, f, x, y}})
+ );
+
+ ExecutionConfig cfg = get_test_default_config(engine);
+ cfg.set_property(ov::intel_gpu::optimize_data(true));
+ cfg.set_property(ov::intel_gpu::force_implementations(
+ ov::intel_gpu::ImplForcingMap{
+ {"conv", {format::b_fs_yx_fsv16, "convolution_gpu_bfyx_f16_depthwise", impl_types::ocl}}
+ }));
+
+ network net(engine, topology, cfg);
+ net.set_input_data("input", input);
+
+ auto outputs = net.execute();
+ auto out_mem = outputs.at("out").get_memory();
+ cldnn::mem_lock<float> out_ptr(out_mem, get_test_stream());
+
+ std::vector<float> out(out_mem->count());
+ for (size_t i = 0; i < out.size(); ++i) {
+ out[i] = out_ptr[i];
+ }
+ return out;
+ };
+
+ auto run_ref = [&]() -> std::vector<float> {
+ VVVVF<float> ref(b, VVVF<float>(f));
+ for (int bi = 0; bi < b; ++bi) {
+ for (int ofi = 0; ofi < f; ++ofi) {
+ ref[bi][ofi] = reference_convolve<float, float, float>(
+ input_data[bi], // [ifm][y][x]
+ weights_data[ofi], // [1][ky][kx] for depthwise
+ 1, 1, // stride y, x
+ 0.0f, // bias
+ 1, 1, // dilation y, x
+ 1, 1, // input padding y, x
+ 0, 0, // output padding y, x
+ ofi, ofi + 1, // f_begin, f_end
+ true, // depthwise
+ false // grouped
+ );
+ }
+ }
+ return flatten_4d<float>(format::bfyx, ref);
+ };
+
+ std::vector<float> out_depthwise, out_ref;
+ ASSERT_NO_THROW(out_depthwise = run_depthwise());
+ ASSERT_NO_THROW(out_ref = run_ref());
+
+ ASSERT_EQ(out_depthwise.size(), out_ref.size());
+ for (size_t i = 0; i < out_depthwise.size(); ++i) {
+ ASSERT_EQ(out_depthwise[i], out_ref[i]);
+ }
+}
TEST(convolution_f32_fw_gpu, basic_convolution_no_bias_swap_xy) {
// Filter : 2x2x1x3