Skip to content

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

[GPU] Fix align_corners Interpolate mismatch between static and dynamic shapes (#39758)

David Nam375fffb47792
diff --git a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/resample_opt.cl b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/resample_opt.cl
index 39d66207c89..65b6f3d1715 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/resample_opt.cl
+++ b/src/plugins/intel_gpu/src/kernel_selector/cl_kernels/resample_opt.cl
@@ -47,6 +47,8 @@ inline float FUNC(get_original_coordinate)(float num, float scale, int length_re
return 0.f;
if (num == 0.f || num == (float)(length_resized - 1))
return num == 0.f ? 0.f : (float)(length_original - 1);
+ // Interpolating modes are built with -cl-fp32-correctly-rounded-divide-sqrt (ResampleKernelOpt::GetKernelsData),
+ // which makes this division match ref_divide() of resample_ref
return (float)((int)num * (length_original - 1)) / (float)(length_resized - 1);
#else
#error [clDNN resample_opt.cl]: coordinate transformation mode - not supported
@@ -309,7 +311,24 @@ KERNEL (resample_opt)(__global INPUT0_TYPE* input,
const int PADDED_Y = INPUT0_SIZE_Y + PADS_BEGIN[3] + PADS_END[3];
const int PADDED_X = INPUT0_SIZE_X + PADS_BEGIN[4] + PADS_END[4];
+#if OUTPUT_X_BLOCK_SIZE <= SUB_GROUP_SIZE
+ // All lanes share x and y, so instead of every lane evaluating all coordinates of the block, lane i evaluates the
+ // one of output column x + i (and the first spare lane the one of row y) and the values are broadcast.
+ const uint lane = get_sub_group_local_id();
+#if OUTPUT_X_BLOCK_SIZE < SUB_GROUP_SIZE
+ const bool row_lane = lane == OUTPUT_X_BLOCK_SIZE;
+ const float lane_coord = FUNC_CALL(get_original_coordinate)(row_lane ? y : x + (int)(lane % OUTPUT_X_BLOCK_SIZE),
+ row_lane ? SCALES[3] : SCALES[4],
+ row_lane ? OUTPUT_SIZE_Y : OUTPUT_SIZE_X,
+ row_lane ? PADDED_Y : PADDED_X);
+ const ACCUMULATOR_TYPE iy = sub_group_broadcast(lane_coord, OUTPUT_X_BLOCK_SIZE);
+#else
+ const float lane_coord = FUNC_CALL(get_original_coordinate)(x + (int)lane, SCALES[4], OUTPUT_SIZE_X, PADDED_X);
+ const ACCUMULATOR_TYPE iy = FUNC_CALL(get_original_coordinate)(y, SCALES[3], OUTPUT_SIZE_Y, PADDED_Y);
+#endif
+#else
const ACCUMULATOR_TYPE iy = FUNC_CALL(get_original_coordinate)(y, SCALES[3], OUTPUT_SIZE_Y, PADDED_Y);
+#endif
float in_y = fmax(0.0f, fmin(iy, (float)(PADDED_Y - 1)));
int in_y1 = min((int)in_y, PADDED_Y - 1);
int in_y2 = min(in_y1 + 1, PADDED_Y - 1);
@@ -317,7 +336,11 @@ KERNEL (resample_opt)(__global INPUT0_TYPE* input,
const ACCUMULATOR_TYPE dy2 = (in_y1 != in_y2) ? TO_ACCUMULATOR_TYPE(fabs(in_y - in_y2)) : 0.5f;
unroll_for (uint out_x = 0; out_x < OUTPUT_X_BLOCK_SIZE; out_x++) {
+#if OUTPUT_X_BLOCK_SIZE <= SUB_GROUP_SIZE
+ const ACCUMULATOR_TYPE ix = sub_group_broadcast(lane_coord, out_x);
+#else
const ACCUMULATOR_TYPE ix = FUNC_CALL(get_original_coordinate)(x + out_x, SCALES[4], OUTPUT_SIZE_X, PADDED_X);
+#endif
float in_x = fmax(0.0f, fmin(ix, (float)(PADDED_X - 1)));
int in_x1 = min((int)in_x, PADDED_X - 1);
int in_x2 = min(in_x1 + 1, PADDED_X - 1);
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/resample/resample_kernel_opt.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/resample/resample_kernel_opt.cpp
index b2675a07fe0..0d67d79eba9 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/resample/resample_kernel_opt.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/resample/resample_kernel_opt.cpp
@@ -264,6 +264,16 @@ Datatype ResampleKernelOpt::GetUnitType(const base_params& params) const {
}
KernelsData ResampleKernelOpt::GetKernelsData(const Params& params) const {
- return GetCommonKernelsData(params);
+ auto kernels_data = GetCommonKernelsData(params);
+ // ALIGN_CORNERS source coordinates come from an fp32 division. For the interpolating modes make it correctly
+ // rounded, so the coordinates match resample_ref (ref_divide(), used for planar layouts and thus dynamic shapes)
+ // and the reference: the default OpenCL division may be off by an ULP, which at coordinates in the hundreds shifts
+ // the interpolation weights by ~1e-5 (static vs dynamic shape mismatch).
+ const auto& resample_prim_params = static_cast<const resample_params&>(params);
+ if (!kernels_data.empty() && resample_prim_params.coordTransMode == CoordinateTransformationMode::ALIGN_CORNERS &&
+ resample_prim_params.resampleType != ResampleType::NEAREST_NEIGHBOR) {
+ kernels_data[0].kernels[0].code.kernelString->options += " -cl-fp32-correctly-rounded-divide-sqrt";
+ }
+ return kernels_data;
}
} // namespace kernel_selector
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/resample_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/resample_gpu_test.cpp
index 92636ae0488..29b4716e795 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/resample_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/resample_gpu_test.cpp
@@ -2510,6 +2510,54 @@ INSTANTIATE_TEST_SUITE_P(resample_opt_smoke_linear_onnx_4d_simple,
}
));
+// resample_opt (blocked layouts, picked for static shapes) must compute ALIGN_CORNERS source coordinates bit-exactly
+// like resample_ref (planar layouts, picked for dynamic shapes). For non-trivial ratios a plain fp32 division differs
+// from ref_divide() by an ULP, which at coordinates in the hundreds shifts the interpolation weights by ~1e-5..1e-4
+// (static vs dynamic shape mismatch). Shapes follow the DPT head of Depth-Anything-V2 (x, y order).
+INSTANTIATE_TEST_SUITE_P(resample_opt_smoke_linear_onnx_4d_align_corners,
+ resample_opt_random_test,
+ testing::ValuesIn(
+ std::vector<resample_opt_random_test_params>{
+ { data_types::f32, {1, 16, 19, 19}, {1, 16, 37, 37}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::ALIGN_CORNERS },
+ { data_types::f32, {1, 16, 37, 37}, {1, 16, 74, 74}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::ALIGN_CORNERS },
+ { data_types::f32, {1, 16, 74, 74}, {1, 16, 148, 148}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::ALIGN_CORNERS },
+ { data_types::f32, {1, 16, 148, 8}, {1, 16, 296, 15}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::ALIGN_CORNERS },
+ { data_types::f32, {1, 16, 296, 8}, {1, 16, 518, 15}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::ALIGN_CORNERS },
+ { data_types::f32, {1, 32, 296, 8}, {1, 32, 518, 15}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv32, format::b_fs_yx_fsv32, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::ALIGN_CORNERS },
+ { data_types::f32, {16, 16, 296, 4}, {16, 16, 518, 7}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::bs_fs_yx_bsv16_fsv16, format::bs_fs_yx_bsv16_fsv16, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::ALIGN_CORNERS },
+ // x block of 16 (one coordinate per lane) and of 17 (more than a sub-group, computed per column)
+ { data_types::f32, {1, 16, 50, 9}, {1, 16, 96, 17}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::ALIGN_CORNERS },
+ { data_types::f32, {1, 16, 26, 5}, {1, 16, 51, 9}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::ALIGN_CORNERS },
+ }
+ ));
+
+// Covers the x block sizes of resample_opt (1, 2, 16 and 17 columns per work-item) with the other coordinate modes.
+INSTANTIATE_TEST_SUITE_P(resample_opt_smoke_linear_onnx_4d_coord_modes,
+ resample_opt_random_test,
+ testing::ValuesIn(
+ std::vector<resample_opt_random_test_params>{
+ { data_types::f16, {1, 16, 26, 5}, {1, 16, 51, 9}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::HALF_PIXEL },
+ { data_types::f16, {1, 16, 50, 9}, {1, 16, 96, 17}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::PYTORCH_HALF_PIXEL },
+ { data_types::f16, {1, 16, 19, 19}, {1, 16, 37, 37}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::TF_HALF_PIXEL_FOR_NN },
+ { data_types::f16, {1, 32, 296, 8}, {1, 32, 518, 15}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv32, format::b_fs_yx_fsv32, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::ASYMMETRIC },
+ { data_types::f16, {1, 16, 296, 8}, {1, 16, 518, 15}, 1, resample::InterpolateOp::InterpolateMode::LINEAR_ONNX, 1, format::b_fs_yx_fsv16, format::b_fs_yx_fsv16, {}, {},
+ resample::InterpolateOp::CoordinateTransformMode::ALIGN_CORNERS },
+ }
+ ));
+
INSTANTIATE_TEST_SUITE_P(resample_opt_smoke_5d_nearest,
resample_opt_random_test,
testing::ValuesIn(