Skip to content

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

[GPU] Fix resample_opt preprocessor structure and support LINEAR_ONNX (#37985)

Deepak Soma Reddya1f131f5e2c1
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 60ef96f278b..396f35579f1 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
@@ -287,16 +287,50 @@ KERNEL (resample_opt)(__global INPUT0_TYPE* input,
const acc_vec_t top = TO_ACC_VEC_TYPE(DECODE_IN_VEC_TYPE(top_left)) + (TO_ACC_VEC_TYPE(DECODE_IN_VEC_TYPE(top_right)) - TO_ACC_VEC_TYPE(DECODE_IN_VEC_TYPE(top_left))) * dx;
const acc_vec_t bottom = TO_ACC_VEC_TYPE(DECODE_IN_VEC_TYPE(bottom_left)) + (TO_ACC_VEC_TYPE(DECODE_IN_VEC_TYPE(bottom_right)) - TO_ACC_VEC_TYPE(DECODE_IN_VEC_TYPE(bottom_left))) * dx;
acc_vec_t res = top + (bottom - top) * dy;
+#elif defined(SAMPLE_TYPE_LINEAR_ONNX)
+ // ONNX-compatible bilinear interpolation with configurable coordinate transformation
+ // (half_pixel, pytorch_half_pixel, align_corners, etc.) via get_original_coordinate().
+ // This enables resample_opt (priority 3) to handle LINEAR_ONNX instead of the slower
+ // resample_onnx (priority 4) for 4D inputs.
+ 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];
+
+ const ACCUMULATOR_TYPE iy = FUNC_CALL(get_original_coordinate)(y, SCALES[3], OUTPUT_SIZE_Y, PADDED_Y);
+ 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);
+ const ACCUMULATOR_TYPE dy1 = (in_y1 != in_y2) ? TO_ACCUMULATOR_TYPE(fabs(in_y - in_y1)) : 0.5f;
+ 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++) {
+ const ACCUMULATOR_TYPE ix = FUNC_CALL(get_original_coordinate)(x + out_x, SCALES[4], OUTPUT_SIZE_X, PADDED_X);
+ 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);
+ const ACCUMULATOR_TYPE dx1 = (in_x1 != in_x2) ? TO_ACCUMULATOR_TYPE(fabs(in_x - in_x1)) : 0.5f;
+ const ACCUMULATOR_TYPE dx2 = (in_x1 != in_x2) ? TO_ACCUMULATOR_TYPE(fabs(in_x - in_x2)) : 0.5f;
+
+ const acc_vec_t top_left = TO_ACC_VEC_TYPE(DECODE_IN_VEC_TYPE(READ_FUNC(input, INPUT0_GET_INDEX(b, feature_block, in_y1, in_x1))));
+ const acc_vec_t top_right = TO_ACC_VEC_TYPE(DECODE_IN_VEC_TYPE(READ_FUNC(input, INPUT0_GET_INDEX(b, feature_block, in_y1, in_x2))));
+ const acc_vec_t bottom_left = TO_ACC_VEC_TYPE(DECODE_IN_VEC_TYPE(READ_FUNC(input, INPUT0_GET_INDEX(b, feature_block, in_y2, in_x1))));
+ const acc_vec_t bottom_right = TO_ACC_VEC_TYPE(DECODE_IN_VEC_TYPE(READ_FUNC(input, INPUT0_GET_INDEX(b, feature_block, in_y2, in_x2))));
+
+ acc_vec_t res = TO_ACC_VEC_TYPE(dx2 * dy2 * top_left) +
+ TO_ACC_VEC_TYPE(dx1 * dy2 * top_right) +
+ TO_ACC_VEC_TYPE(dx2 * dy1 * bottom_left) +
+ TO_ACC_VEC_TYPE(dx1 * dy1 * bottom_right);
+#else
+#error [clDNN resample_opt.cl]: unsupported resample type
+#endif
+
+#ifndef SAMPLE_TYPE_NEAREST
#if HAS_FUSED_OPS
FUSED_OPS;
OUT_VEC_TYPE out = FUSED_OPS_RESULT;
#else
OUT_VEC_TYPE out = TO_OUT_VEC_TYPE(ACTIVATION(res, ACTIVATION_PARAMS));
#endif // #if HAS_FUSED_OPS
-#else // defined(SAMPLE_TYPE_LINEAR_ONNX)
-#error [clDNN resample_opt.cl]: unsupported resample type
-#endif
+#endif // !SAMPLE_TYPE_NEAREST
#if OUTPUT_DIMS == 5
WRITE_FUNC(output, OUTPUT_GET_INDEX(b, feature_block, z, y, (x + out_x)), out);
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/resample/resample_kernel_base.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/resample/resample_kernel_base.cpp
index a2e5ad7e72c..3a8bf453f15 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/resample/resample_kernel_base.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/resample/resample_kernel_base.cpp
@@ -185,16 +185,21 @@ JitConstants ResampleKernelBase::GetJitConstants(const resample_params& params)
});
if (params.resampleType == ResampleType::CAFFE_BILINEAR_INTERP) {
- if (axesUsed[0] == 1)
+ if (axesUsed[0] == 1) {
jit.AddConstant(MakeJitConstant("AXES_USED_B", 1));
- if (axesUsed[1] == 1)
+ }
+ if (axesUsed[1] == 1) {
jit.AddConstant(MakeJitConstant("AXES_USED_F", 1));
- if (axesUsed[2] == 1)
+ }
+ if (axesUsed[2] == 1) {
jit.AddConstant(MakeJitConstant("AXES_USED_Z", 1));
- if (axesUsed[3] == 1)
+ }
+ if (axesUsed[3] == 1) {
jit.AddConstant(MakeJitConstant("AXES_USED_Y", 1));
- if (axesUsed[4] == 1)
+ }
+ if (axesUsed[4] == 1) {
jit.AddConstant(MakeJitConstant("AXES_USED_X", 1));
+ }
jit.AddConstants({
MakeJitConstant("PADDED_B", b_size_padded),
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 834befa9e9b..722502cde5c 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
@@ -76,6 +76,7 @@ ParamsKey ResampleKernelOpt::GetSupportedKey() const {
k.EnableTensorPitches();
k.EnableBatching();
k.EnableResampleType(ResampleType::BILINEAR_INTERP);
+ k.EnableResampleType(ResampleType::LINEAR_ONNX);
k.EnableResampleType(ResampleType::NEAREST_NEIGHBOR);
k.EnableResampleType(ResampleType::CAFFE_BILINEAR_INTERP);
return k;
@@ -159,7 +160,8 @@ bool ResampleKernelOpt::Validate(const Params& p) const {
if ((input.GetDType() == Datatype::UINT8 || input.GetDType() == Datatype::INT8) &&
params.resampleType != ResampleType::NEAREST_NEIGHBOR &&
- params.resampleType != ResampleType::BILINEAR_INTERP) {
+ params.resampleType != ResampleType::BILINEAR_INTERP &&
+ params.resampleType != ResampleType::LINEAR_ONNX) {
DO_NOT_USE_THIS_KERNEL(p.layerID);
}
@@ -168,6 +170,21 @@ bool ResampleKernelOpt::Validate(const Params& p) const {
DO_NOT_USE_THIS_KERNEL(p.layerID);
}
+ // LINEAR_ONNX in resample_opt does not implement OOB-to-zero for padded
+ // coordinates; let resample_onnx (which does) handle those cases.
+ if (params.resampleType == ResampleType::LINEAR_ONNX) {
+ const auto& output = params.outputs[0];
+ if (input.Batch().v != output.Batch().v || input.Feature().v != output.Feature().v) {
+ DO_NOT_USE_THIS_KERNEL(p.layerID);
+ }
+
+ for (size_t i = 0; i < params.pads_begin.size(); ++i) {
+ if (params.pads_begin[i] != 0 || params.pads_end[i] != 0) {
+ DO_NOT_USE_THIS_KERNEL(p.layerID);
+ }
+ }
+ }
+
return true;
}
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 80189a3d3bd..c57a9fb39ed 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
@@ -9,6 +9,7 @@
#include <intel_gpu/primitives/resample.hpp>
#include <intel_gpu/primitives/reorder.hpp>
#include <intel_gpu/primitives/data.hpp>
+#include <intel_gpu/primitives/eltwise.hpp>
using namespace cldnn;
using namespace ::tests;
@@ -497,8 +498,9 @@ struct resample_random_test : testing::TestWithParam<resample_random_test_params
std::string kernel = "";
if (!is_caching_test) {
for (auto& info : net->get_primitives_info()) {
- if (info.original_id == "resample")
+ if (info.original_id == "resample") {
kernel = info.kernel_id;
+ }
}
}
}
@@ -2858,8 +2860,9 @@ TEST(resample_gpu, pillow_identity_resample_no_crash) {
// Fill with sequential values
std::vector<float> input_data(b * f * y * x);
- for (size_t i = 0; i < input_data.size(); ++i)
+ for (size_t i = 0; i < input_data.size(); ++i) {
input_data[i] = static_cast<float>(i + 1);
+ }
set_values(input_mem, input_data);
// Test both BILINEAR_PILLOW and BICUBIC_PILLOW with identity spatial size
@@ -2912,8 +2915,9 @@ void test_pillow_half_precision(data_types dt) {
const int32_t out_y = 8, out_x = 8;
std::vector<float> input_data(b * f * y * x);
- for (size_t i = 0; i < input_data.size(); ++i)
+ for (size_t i = 0; i < input_data.size(); ++i) {
input_data[i] = static_cast<float>(i % 16) - 7.0f;
+ }
std::vector<resample::InterpolateOp::InterpolateMode> modes = {
resample::InterpolateOp::InterpolateMode::BILINEAR_PILLOW,
@@ -2976,3 +2980,169 @@ TEST(resample_gpu, pillow_bf16) {
TEST(resample_gpu, basic_in2x3x2x2_nearest_cached) {
test_basic_in2x3x2x2_nearest<float>(true);
}
+
+// resample_opt handles LINEAR_ONNX for 4D inputs.
+TEST(resample_gpu, opt_linear_onnx_4d_f16) {
+ auto& engine = get_test_engine();
+
+ tensor input_size{1, 8, 13, 13};
+ tensor output_size{1, 8, 26, 26};
+ auto in_mem = engine.allocate_memory({ data_types::f16, format::bfyx, input_size });
+ std::vector<ov::float16> in_vals(input_size.count());
+ for (size_t i = 0; i < in_vals.size(); ++i) {
+ in_vals[i] = ov::float16(0.1f * static_cast<float>(i % 17) - 0.8f);
+ }
+ set_values<ov::float16>(in_mem, in_vals);
+
+ auto make_net = [&](const std::string& kernel) {
+ topology topo(input_layout("in", in_mem->get_layout()),
+ resample("resample", input_info("in"), output_size, 8,
+ resample::InterpolateOp::InterpolateMode::LINEAR_ONNX));
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::custom_outputs(std::vector<std::string>{ "resample" }));
+ ov::intel_gpu::ImplementationDesc impl = { format::b_fs_yx_fsv16, kernel };
+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{ { "resample", impl } }));
+ network net(engine, topo, config);
+ net.set_input_data("in", in_mem);
+ return net.execute().at("resample").get_memory();
+ };
+
+ auto out_ref = make_net("resample_ref");
+ auto out_opt = make_net("resample_opt");
+
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> ref_ptr(out_ref, get_test_stream());
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> opt_ptr(out_opt, get_test_stream());
+ for (size_t i = 0; i < ref_ptr.size(); ++i) {
+ ASSERT_TRUE(are_equal(ref_ptr[i], opt_ptr[i], 1e-2f))
+ << "Mismatch at " << i << ": ref " << ref_ptr[i] << " opt " << opt_ptr[i];
+ }
+}
+
+namespace {
+// Runs a single-node resample network without any forced implementation and returns the
+// selected kernel id (e.g. "resample_opt__f16") for the "resample" node. This exercises the
+// real kernel-selector path, so the assertions below fail if resample_opt is ever chosen for
+// an input it rejects (or never chosen for one it should handle).
+//
+// The input is created directly in b_fs_yx_fsv16 (a format both resample_opt and resample_onnx
+// support, unlike bfyx which only resample_ref handles) so that the selector actually has to
+// choose between resample_opt and resample_onnx. No force_implementations is used.
+std::string select_resample_kernel(const tensor& input_size,
+ const tensor& output_size,
+ std::vector<size_t> pads_begin,
+ std::vector<size_t> pads_end) {
+ auto& engine = get_test_engine();
+
+ auto in_layout = layout(data_types::f16, format::b_fs_yx_fsv16, input_size);
+ auto in_mem = engine.allocate_memory(in_layout);
+ std::vector<ov::float16> in_vals(input_size.count());
+ for (size_t i = 0; i < in_vals.size(); ++i) {
+ in_vals[i] = ov::float16(0.1f * static_cast<float>(i % 17) - 0.8f);
+ }
+ set_values<ov::float16>(in_mem, in_vals);
+
+ auto resample_prim = resample("resample", input_info("in"), output_size, 8,
+ resample::InterpolateOp::InterpolateMode::LINEAR_ONNX);
+ resample_prim.pads_begin = std::move(pads_begin);
+ resample_prim.pads_end = std::move(pads_end);
+
+ topology topo(input_layout("in", in_layout), resample_prim);
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::custom_outputs(std::vector<std::string>{ "resample" }));
+ network net(engine, topo, config);
+ net.set_input_data("in", in_mem);
+ net.execute();
+
+ for (const auto& info : net.get_primitives_info()) {
+ if (info.original_id == "resample") {
+ return info.kernel_id;
+ }
+ }
+ return {};
+}
+} // namespace
+
+// Selector-level coverage (no forced implementation): an unpadded 4-D LINEAR_ONNX resample
+// with unchanged batch/feature must select the higher-priority resample_opt kernel
+// (FORCE_PRIORITY_3), not fall back to resample_onnx (FORCE_PRIORITY_4) or resample_ref.
+TEST(resample_gpu, opt_linear_onnx_4d_selector) {
+ const std::string kernel_id = select_resample_kernel({ 1, 8, 13, 13 }, { 1, 8, 26, 26 }, {}, {});
+ EXPECT_EQ(kernel_id.rfind("resample_opt", 0), 0)
+ << "Expected resample_opt to be selected for an unpadded 4-D LINEAR_ONNX resample, got: " << kernel_id;
+}
+
+// Selector-level coverage (no forced implementation): a 4-D LINEAR_ONNX resample with
+// non-zero spatial padding must NOT select resample_opt (it does not implement OOB-to-zero
+// for padded coordinates), and must fall back to resample_onnx which does handle padding.
+TEST(resample_gpu, onnx_linear_onnx_4d_padding_selector) {
+ const std::string kernel_id = select_resample_kernel({ 1, 8, 13, 13 }, { 1, 8, 26, 26 },
+ { 0, 0, 1, 1 }, { 0, 0, 1, 1 });
+ // Starts with "resample_onnx" implies resample_opt (which rejects padding) was not selected.
+ EXPECT_EQ(kernel_id.rfind("resample_onnx", 0), 0)
+ << "Expected resample_onnx to be selected for a padded 4-D LINEAR_ONNX resample, got: " << kernel_id;
+}
+
+// Regression: the resample_opt kernel previously defined FUSED_OPS twice in NEAREST mode
+// (a branch-local block plus the shared one), which failed the CL build with
+// CL_BUILD_PROGRAM_FAILURE as soon as an eltwise op fused into the node. This forces
+// resample_opt for a NEAREST node that carries a fused eltwise.
+TEST(resample_gpu, opt_nearest_fused_eltwise_builds) {
+ auto& engine = get_test_engine();
+
+ tensor input_size{1, 8, 13, 13};
+ tensor output_size{1, 8, 26, 26};
+ auto in_mem = engine.allocate_memory({ data_types::f16, format::bfyx, input_size });
+ std::vector<ov::float16> in_vals(input_size.count());
+ for (size_t i = 0; i < in_vals.size(); ++i) {
+ in_vals[i] = ov::float16(0.1f * static_cast<float>(i % 13) - 0.6f);
+ }
+ set_values<ov::float16>(in_mem, in_vals);
+ // the scale has the resample output shape so the eltwise product broadcasts over it
+ auto scale = engine.allocate_memory({ data_types::f16, format::bfyx, output_size });
+ set_values<ov::float16>(scale, std::vector<ov::float16>(output_size.count(), ov::float16(0.5f)));
+
+ // eltwise wired after the resample; the pass manager fuses it into the resample node.
+ topology topo(input_layout("in", in_mem->get_layout()),
+ data("scale", scale),
+ resample("resample", input_info("in"), output_size, 8,
+ resample::InterpolateOp::InterpolateMode::NEAREST),
+ eltwise("scaled", input_info("resample"), input_info("scale"), eltwise_mode::prod),
+ reorder("output", input_info("scaled"), format::bfyx, data_types::f16));
+
+ ExecutionConfig config = get_test_default_config(engine);
+ config.set_property(ov::intel_gpu::optimize_data(true));
+ config.set_property(ov::intel_gpu::custom_outputs(std::vector<std::string>{ "output" }));
+ ov::intel_gpu::ImplementationDesc impl = { format::b_fs_yx_fsv16, "resample_opt" };
+ config.set_property(ov::intel_gpu::force_implementations(ov::intel_gpu::ImplForcingMap{ { "resample", impl } }));
+ network net(engine, topo, config);
+ net.set_input_data("in", in_mem);
+
+ // Before the FUSED_OPS structure fix this throws (CL build failure); it must now build and run.
+ const auto primitives_info = net.get_primitives_info();
+ const auto resample_info = std::find_if(primitives_info.begin(), primitives_info.end(), [](const primitive_info& info) {
+ return info.original_id == "resample";
+ });
+ ASSERT_NE(resample_info, primitives_info.end());
+ ASSERT_NE(std::find(resample_info->c_fused_ids.begin(), resample_info->c_fused_ids.end(), "scaled"),
+ resample_info->c_fused_ids.end());
+ auto out_opt = net.execute().at("output").get_memory();
+
+ // The nearest-2x sample picks one of the input values; the eltwise multiplies it by 0.5.
+ // We check that invariant instead of comparing against a free-selection reference, whose
+ // resample kernel may use a different coordinate convention for NEAREST.
+ std::unordered_set<float> in_set(in_vals.begin(), in_vals.end());
+ cldnn::mem_lock<ov::float16, mem_lock_type::read> opt_ptr(out_opt, get_test_stream());
+ for (size_t i = 0; i < opt_ptr.size(); ++i) {
+ float v = float(opt_ptr[i]);
+ ASSERT_TRUE(std::isfinite(v)) << "Non-finite output at " << i;
+ // v must equal 0.5 * input[j] for some input j.
+ bool found = false;
+ for (float src : in_set) {
+ if (std::abs(v - 0.5f * src) < 1e-2f) {
+ found = true;
+ break;
+ }
+ }
+ ASSERT_TRUE(found) << "Output " << v << " at " << i << " is not 0.5 * any input value.";
+ }
+}