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] Fix out-of-bounds input read in convolution_gpu_bfyx_f16_depthwise (#38535)
Wilson Seokc8566fcc7760
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.clindex 639b070deaf..2a9cf1fe568 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@@ -80,9 +80,32 @@ KERNEL(convolution_gpu_bfyx_f16_depthwise)(FILTER_TYPE wei_21 = DT_FILTER_BLOCK_READ(weights, filter_offset + 2 * FILTER_Y_PITCH * FEATURE_SLICE_SIZE + 1 * FEATURE_SLICE_SIZE);FILTER_TYPE wei_22 = DT_FILTER_BLOCK_READ(weights, filter_offset + 2 * FILTER_Y_PITCH * FEATURE_SLICE_SIZE + 2 * FEATURE_SLICE_SIZE);- 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);+ const uint src_line_0 = input_offset + (input_y + 0) * input_y_pitch + (input_x) * input_x_pitch;+ const uint src_line_1 = input_offset + (input_y + 1) * input_y_pitch + (input_x) * input_x_pitch;+ const uint src_line_2 = input_offset + (input_y + 2) * input_y_pitch + (input_x) * input_x_pitch;++ // In the last x block an 8-wide read can run past the padded row, and past the buffer end on the last row.+ const int src_line_avail = (INPUT0_PAD_BEFORE_SIZE_X + INPUT0_SIZE_X + INPUT0_PAD_AFTER_SIZE_X) - (INPUT0_PAD_BEFORE_SIZE_X + input_x);++ INPUT_TYPE8 src_block_0;+ INPUT_TYPE8 src_block_1;+ INPUT_TYPE8 src_block_2;+ if (src_line_avail >= 8) {+ src_block_0 = DT_INPUT_BLOCK_READ8(input, src_line_0);+ src_block_1 = DT_INPUT_BLOCK_READ8(input, src_line_1);+ src_block_2 = DT_INPUT_BLOCK_READ8(input, src_line_2);+ } else {+ src_block_0 = (INPUT_TYPE8)(INPUT0_VAL_ZERO);+ src_block_1 = (INPUT_TYPE8)(INPUT0_VAL_ZERO);+ src_block_2 = (INPUT_TYPE8)(INPUT0_VAL_ZERO);+ unroll_for (int i = 0; i < 8; i++) {+ if (i < src_line_avail) {+ src_block_0[i] = DT_INPUT_BLOCK_READ(input, src_line_0 + i * input_x_pitch);+ src_block_1[i] = DT_INPUT_BLOCK_READ(input, src_line_1 + i * input_x_pitch);+ src_block_2[i] = DT_INPUT_BLOCK_READ(input, src_line_2 + i * input_x_pitch);+ }+ }+ }const bool valid_x6 = (x + 6) < OUTPUT_SIZE_X;const bool valid_x7 = (x + 7) < OUTPUT_SIZE_X;@@ -181,11 +204,17 @@ KERNEL(convolution_gpu_bfyx_f16_depthwise)(INPUT_TYPE src[X_BLOCK_SIZE * FILTER_SIZE_Y * FILTER_SIZE_X];unroll_for (uint k = 0; k < X_BLOCK_SIZE; k++) {+ // Pixels past the output width can lie past the input buffer end, so they are not read.+ const bool valid_k = (x + k) < OUTPUT_SIZE_X;unroll_for (uint i = 0; i < FILTER_SIZE_Y; i++) {unroll_for (uint j = 0; j < FILTER_SIZE_X; j++) {- src[k * FILTER_SIZE_Y * FILTER_SIZE_X + i * FILTER_SIZE_X + j] = DT_INPUT_BLOCK_READ(input, input_offset +- (input_y + (i * DILATION_SIZE_Y)) * input_y_pitch +- (input_x + (j * DILATION_SIZE_X) + k * STRIDE_SIZE_X) * input_x_pitch);+ INPUT_TYPE val = INPUT0_VAL_ZERO;+ if (valid_k) {+ val = DT_INPUT_BLOCK_READ(input, input_offset ++ (input_y + (i * DILATION_SIZE_Y)) * input_y_pitch ++ (input_x + (j * DILATION_SIZE_X) + k * STRIDE_SIZE_X) * input_x_pitch);+ }+ src[k * FILTER_SIZE_Y * FILTER_SIZE_X + i * FILTER_SIZE_X + j] = val;}}}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.cppindex b309779b5cd..45082b1f913 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@@ -11767,7 +11767,7 @@ TEST(export_import_convolution_f32_gpu, convolution_gpu_bfyx_f16_depthwise_x_blotest_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) {+static void test_convolution_f32_gpu_bfyx_f16_depthwise_x_tail(size_t stride) {auto& engine = get_test_engine();if (engine.get_device_info().supports_immad) {@@ -11782,6 +11782,8 @@ TEST(convolution_f32_fw_gpu, convolution_gpu_f32_convolution_gpu_bfyx_f16_depthwconstexpr int y = 1;constexpr int x = 9;constexpr int filter_size = 3;+ const int out_x = static_cast<int>((x + 2 - filter_size) / stride + 1);+ const int out_y = static_cast<int>((y + 2 - filter_size) / stride + 1);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);@@ -11806,12 +11808,12 @@ TEST(convolution_f32_fw_gpu, convolution_gpu_f32_convolution_gpu_bfyx_f16_depthw"weights",no_bias,f, // groups- {1, 1}, // stride+ {stride, stride},{1, 1}, // dilation{1, 1}, // pad begin{1, 1}, // pad endtrue), // grouped- reorder("out", input_info("conv"), {data_types::f32, format::bfyx, tensor{b, f, x, y}})+ reorder("out", input_info("conv"), {data_types::f32, format::bfyx, tensor{b, f, out_x, out_y}}));ExecutionConfig cfg = get_test_default_config(engine);@@ -11842,7 +11844,7 @@ TEST(convolution_f32_fw_gpu, convolution_gpu_f32_convolution_gpu_bfyx_f16_depthwref[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+ static_cast<int>(stride), static_cast<int>(stride),0.0f, // bias1, 1, // dilation y, x1, 1, // input padding y, x@@ -11862,10 +11864,19 @@ TEST(convolution_f32_fw_gpu, convolution_gpu_f32_convolution_gpu_bfyx_f16_depthwASSERT_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]);+ ASSERT_NEAR(out_depthwise[i], out_ref[i], 1e-5f);}}+TEST(convolution_f32_fw_gpu, convolution_gpu_f32_convolution_gpu_bfyx_f16_depthwise) {+ test_convolution_f32_gpu_bfyx_f16_depthwise_x_tail(1);+}++// Stride 2 takes the generic (non-3x3-stride-1) path.+TEST(convolution_f32_fw_gpu, convolution_gpu_f32_convolution_gpu_bfyx_f16_depthwise_stride2) {+ test_convolution_f32_gpu_bfyx_f16_depthwise_x_tail(2);+}+TEST(convolution_f32_fw_gpu, basic_convolution_no_bias_swap_xy) {// Filter : 2x2x1x3// Stride : 1x1