Skip to content

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

[GPU] Fix out-of-bounds read of a partial x block in reduce_gpu_b_fs_yx_fsv16 (#38870)

Wilson Seok68f1f62208ee
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/reduce_gpu_b_fs_yx_fsv16.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/reduce_gpu_b_fs_yx_fsv16.cl
index da521f2693c..25c9f8e3a75 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/reduce_gpu_b_fs_yx_fsv16.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/reduce_gpu_b_fs_yx_fsv16.cl
@@ -137,6 +137,20 @@ inline uint FUNC(calc_linear_offset)(uint b, uint f, uint y, uint x) {
return index;
}
+// Reads READ_OFFSET x positions starting at x. A partial last block of a row is read per x position:
+// a full block read would run into the next row and, for the last row, past the end of the buffer.
+inline INPUT_VEC FUNC(read_x_block)(const __global INPUT0_TYPE* data, uint offset, uint x) {
+ if (INPUT0_SIZE_X % READ_OFFSET != 0 && x + READ_OFFSET > INPUT0_SIZE_X) {
+ INPUT_VEC input = (INPUT_VEC)(INPUT_INIT_VAL);
+ unroll_for (uint i = 0; i < READ_OFFSET; ++i) {
+ if (x + i < INPUT0_SIZE_X)
+ input[i] = DT_INPUT_BLOCK_READ(data, offset + i * FSV);
+ }
+ return input;
+ }
+ return BLOCK_READ(data, offset);
+}
+
REQD_SUB_GROUP_SIZE(SIMD)
KERNEL(reduce_fsv16)(
const __global INPUT0_TYPE* data,
@@ -340,13 +354,14 @@ uint offset = batch_out * input_batch_pitch + ((feature_out + FSV - 1) / FSV) *
#if REDUCE_FEATURE && (INPUT0_FEATURE_NUM % FSV != 0) && !ZERO_INVARIANT_REDUCTION
INPUT_VEC input = (INPUT_VEC)(INPUT_INIT_VAL);
if (fi + FSV <= INPUT0_FEATURE_NUM)
- input = BLOCK_READ(data, offset);
+ input = FUNC_CALL(read_x_block)(data, offset, xi);
else
if (fi + get_sub_group_local_id() < INPUT0_FEATURE_NUM)
for (int i = 0; i < READ_OFFSET; ++i)
- input[i] = data[offset + get_sub_group_local_id() + i * get_max_sub_group_size()];
+ if (xi + i < INPUT0_SIZE_X)
+ input[i] = data[offset + get_sub_group_local_id() + i * get_max_sub_group_size()];
#else
- INPUT_VEC input = BLOCK_READ(data, offset);
+ INPUT_VEC input = FUNC_CALL(read_x_block)(data, offset, xi);
#endif
unroll_for (int i = 0; i < READ_OFFSET; ++i)
@@ -370,8 +385,18 @@ uint offset = batch_out * input_batch_pitch + ((feature_out + FSV - 1) / FSV) *
ACTIVATION_VEC reduce_result = TO_ACTIVATION_VEC(final_acc);
#if HAS_FUSED_OPS
- FUSED_OPS_VECTOR;
- final_result = (OUTPUT_VEC)(FUSED_OPS_RESULT_VECTOR);
+ if (COMMON_OUTPUT_SIZE_X % READ_OFFSET == 0 || x + READ_OFFSET <= COMMON_OUTPUT_SIZE_X) {
+ FUSED_OPS_VECTOR;
+ final_result = (OUTPUT_VEC)(FUSED_OPS_RESULT_VECTOR);
+ } else {
+ // Fused inputs can have the output shape, so a vector load would also read past the row.
+ unroll_for (uint i = 0; i < READ_OFFSET; ++i) {
+ if (x + i < COMMON_OUTPUT_SIZE_X) {
+ FUSED_OPS_X_TAIL;
+ final_result[i] = FUSED_OPS_RESULT_X_TAIL;
+ }
+ }
+ }
#else
final_result = TO_OUTPUT_VEC(ACTIVATION(reduce_result, ACTIVATION_PARAMS));
#endif
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/reduce/reduce_kernel_b_fs_yx_fsv16.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/reduce/reduce_kernel_b_fs_yx_fsv16.cpp
index fd0c90cdd82..e7a5cc85150 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/reduce/reduce_kernel_b_fs_yx_fsv16.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/reduce/reduce_kernel_b_fs_yx_fsv16.cpp
@@ -187,6 +187,17 @@ JitConstants ReduceKernel_b_fs_yx_fsv16::GetJitConstants(const reduce_params& pa
IndexType::TENSOR_COORD,
Tensor::DataChannelName::X};
+ // Used per x position for a partial last x block, where vector loads would read past the row.
+ FusedOpsConfiguration conf_x_tail = {"_X_TAIL",
+ {"b", "f", "y", "(x + i)"},
+ var_name + "[i]",
+ input_dt,
+ 1,
+ LoadType::LT_ALIGNED_READ,
+ BoundaryCheck::DISABLED,
+ IndexType::TENSOR_COORD,
+ Tensor::DataChannelName::X};
+
if (cant_handle_vec16) {
FusedOpsConfiguration conf_vector_1 = {"_VECTOR_1",
idx_order,
@@ -212,7 +223,7 @@ JitConstants ReduceKernel_b_fs_yx_fsv16::GetJitConstants(const reduce_params& pa
jit.AddConstant(MakeJitConstant("FUSED_OPS_VECTOR", "{FUSED_OPS_VECTOR_1;final_result.lo=FUSED_OPS_RESULT_VECTOR_1;}"
"{FUSED_OPS_VECTOR_2;final_result.hi=FUSED_OPS_RESULT_VECTOR_2;}"));
jit.AddConstant(MakeJitConstant("FUSED_OPS_RESULT_VECTOR", "final_result"));
- jit.Merge(MakeFusedOpsJitConstants(params, {conf_scalar, conf_vector_1, conf_vector_2}));
+ jit.Merge(MakeFusedOpsJitConstants(params, {conf_scalar, conf_x_tail, conf_vector_1, conf_vector_2}));
} else {
FusedOpsConfiguration conf_vector = {"_VECTOR",
idx_order,
@@ -224,7 +235,7 @@ JitConstants ReduceKernel_b_fs_yx_fsv16::GetJitConstants(const reduce_params& pa
IndexType::TENSOR_COORD,
Tensor::DataChannelName::X};
- jit.Merge(MakeFusedOpsJitConstants(params, {conf_vector, conf_scalar}));
+ jit.Merge(MakeFusedOpsJitConstants(params, {conf_vector, conf_scalar, conf_x_tail}));
}
}
diff --git a/src/plugins/intel_gpu/tests/unit/fusions/reduce_fusion_test.cpp b/src/plugins/intel_gpu/tests/unit/fusions/reduce_fusion_test.cpp
index 1bc2245ab94..966bebe87ec 100644
--- a/src/plugins/intel_gpu/tests/unit/fusions/reduce_fusion_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/fusions/reduce_fusion_test.cpp
@@ -285,6 +285,46 @@ INSTANTIATE_TEST_SUITE_P(fusings_gpu_bf_axis, reduce_eltwise_activation_quantize
reduce_test_params{ CASE_REDUCE_U8_0, 2, 5, reduce_mode::log_sum, { 1, 0 }, true, "reduce_gpu_b_fs_yx_fsv16" }
}));
+// reduce_gpu_b_fs_yx_fsv16 reads x in blocks of 16 / 8 / 4 positions for 1 / 2 / 4-byte inputs when x is not reduced.
+// Here each row ends in a partial block, which must not be read as a full block from the input or a fused input.
+class reduce_fsv16_partial_x_block : public ReduceFusingTest {};
+TEST_P(reduce_fsv16_partial_x_block, eltwise_activation_quantize) {
+ auto p = GetParam();
+ update_out_shape(p);
+ create_topologies(
+ input_layout("input", get_input_layout(p, true)),
+ reorder("input_reorder", input_info("input"), p.input_format, p.data_type),
+ data("in_lo", get_mem(get_single_element_layout(p), min_random, 0)),
+ data("in_hi", get_mem(get_single_element_layout(p), 1, max_random)),
+ data("out_lo", get_mem(get_single_element_layout(p), -128)),
+ data("out_hi", get_mem(get_single_element_layout(p), 127)),
+ data("eltwise_data", get_mem(get_output_layout(p))),
+ reduce("reduce", input_info("input_reorder"), p.reduce_mode, p.reduce_axes, p.keep_dims),
+ eltwise("eltwise", { input_info("reduce"), input_info("eltwise_data") }, eltwise_mode::sum, p.default_type),
+ activation("activation", input_info("eltwise"), activation_func::relu),
+ quantize("quantize", input_info("activation"), input_info("in_lo"), input_info("in_hi"),
+ input_info("out_lo"), input_info("out_hi"), 256, data_types::i8),
+ reorder("output_reorder", input_info("quantize"), p.default_format, data_types::f32)
+ );
+
+ // Force the OCL kernel, so that it runs the fused ops instead of oneDNN.
+ ov::intel_gpu::ImplementationDesc reduce_impl = { p.input_format, p.kernel_name, impl_types::ocl };
+ cfg_fused.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{ { "reduce", reduce_impl } }));
+
+ tolerance = 1.f;
+ execute(p, false, true);
+}
+
+INSTANTIATE_TEST_SUITE_P(fusings_gpu, reduce_fsv16_partial_x_block, ::testing::ValuesIn(std::vector<reduce_test_params>{
+ reduce_test_params{ CASE_REDUCE_I8_4, 2, 5, reduce_mode::sum, { 2 }, true, "reduce_gpu_b_fs_yx_fsv16" },
+ reduce_test_params{ CASE_REDUCE_I8_0, 2, 5, reduce_mode::max, { 0 }, true, "reduce_gpu_b_fs_yx_fsv16" },
+ reduce_test_params{ CASE_REDUCE_U8_0, 2, 5, reduce_mode::max, { 1 }, true, "reduce_gpu_b_fs_yx_fsv16" },
+ reduce_test_params{ CASE_REDUCE_U8_4, 2, 5, reduce_mode::sum, { 0 }, true, "reduce_gpu_b_fs_yx_fsv16" },
+ reduce_test_params{ CASE_REDUCE_F16_4, 2, 5, reduce_mode::mean, { 2 }, true, "reduce_gpu_b_fs_yx_fsv16" },
+ reduce_test_params{ CASE_REDUCE_F16_0, 2, 5, reduce_mode::min, { 1 }, true, "reduce_gpu_b_fs_yx_fsv16" },
+ reduce_test_params{ CASE_REDUCE_F32_0, 2, 5, reduce_mode::sum, { 2 }, true, "reduce_gpu_b_fs_yx_fsv16" }
+}));
+
class reduce_scale_activation : public ReduceFusingTest {};
TEST_P(reduce_scale_activation, basic) {
auto p = GetParam();