Skip to content

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

[GPU] Fix Maximum/Minimum NaN propagation (#37931)

Abhinav85e8cb27b4d0
diff --git a/src/plugins/intel_gpu/src/kernel_selector/kernels/eltwise/eltwise_kernel_base.cpp b/src/plugins/intel_gpu/src/kernel_selector/kernels/eltwise/eltwise_kernel_base.cpp
index d534a5ba313..a37b0d312a1 100644
--- a/src/plugins/intel_gpu/src/kernel_selector/kernels/eltwise/eltwise_kernel_base.cpp
+++ b/src/plugins/intel_gpu/src/kernel_selector/kernels/eltwise/eltwise_kernel_base.cpp
@@ -310,7 +310,36 @@ JitConstants EltwiseKernelBase::GetOperationsJitConstants(const eltwise_params&
op += cast_type + "f" + mode + "(" + input0_str + ", convert_float(" + input1_str + "))";
} else {
// input_0 != int && input_1 != int
- op += cast_type + "f" + mode + "(" + input0_str + ", " + input1_str + ")";
+ if (ew.mode == EltwiseMode::MODULU) {
+ op += cast_type + "fmod(" + input0_str + ", " + input1_str + ")";
+ } else {
+ // Match the OpenVINO reference implementation (std::max/std::min)
+ // instead of OpenCL fmax/fmin, which return the non-NaN operand
+ // and therefore silently drop NaN:
+ // std::max(a, b) == ((a < b) ? b : a)
+ // std::min(a, b) == ((b < a) ? b : a)
+ // A NaN first operand wins, a NaN second operand loses to the
+ // finite first operand - exactly the reference behavior.
+ // The ternary operator is used rather than select(): select()
+ // requires the condition to be a signed integer of the same
+ // width as the operands (short for half), while a comparison
+ // on half yields int, so f16 kernels fail to build with
+ // CL_BUILD_PROGRAM_FAILURE. A vector condition is valid in the
+ // ternary operator for the vectorized (block/vload) variants.
+ // The isnan() guard on the first operand makes the expression
+ // immune to compiler folding: the GPU compiler recognizes the
+ // bare comparison form as the hardware fmin/fmax instruction
+ // (which returns the non-NaN operand, the exact bug being
+ // fixed) and rewrites the ternary back to it. With a
+ // data-dependent isnan branch it cannot fold, and the guard is
+ // semantically identical to the reference: for a NaN first
+ // operand the reference already returns it unchanged, and for a
+ // finite first operand fmax/fmin and the comparison agree.
+ const std::string cmp = (ew.mode == EltwiseMode::MIN)
+ ? ("(" + input1_str + " < " + input0_str + ")")
+ : ("(" + input0_str + " < " + input1_str + ")");
+ op += cast_type + "(isnan(" + input0_str + ") ? " + input0_str + " : (" + cmp + " ? " + input1_str + " : " + input0_str + "))";
+ }
}
} break;
case EltwiseMode::POW:
diff --git a/src/plugins/intel_gpu/tests/unit/test_cases/eltwise_gpu_test.cpp b/src/plugins/intel_gpu/tests/unit/test_cases/eltwise_gpu_test.cpp
index 47ae749b40f..418f80fae6d 100644
--- a/src/plugins/intel_gpu/tests/unit/test_cases/eltwise_gpu_test.cpp
+++ b/src/plugins/intel_gpu/tests/unit/test_cases/eltwise_gpu_test.cpp
@@ -1345,6 +1345,91 @@ TEST(eltwise_gpu_f32, isnan_in1_float_out1_int) {
}
}
+// Maximum/Minimum must follow the OpenVINO reference (std::max/std::min):
+// max(a, b) == ((a < b) ? b : a) and min(a, b) == ((b < a) ? b : a), so a NaN
+// in the first operand propagates to the output while a NaN in the second
+// operand loses to the finite first operand. OpenCL fmax/fmin return the
+// non-NaN operand in both orders and silently dropped NaN (ticket 37730).
+template <typename T>
+void run_extremum_nan_propagation_test(cldnn::eltwise_mode mode) {
+ auto& engine = get_test_engine();
+
+ const auto nan = std::numeric_limits<float>::quiet_NaN();
+ const std::vector<float> in0 = {nan, 1.f, nan, 2.f, nan, 1.f, nan, 2.f,
+ nan, 1.f, nan, 2.f, nan, 1.f, nan, 2.f};
+ const std::vector<float> in1 = {1.5f, 1.5f, 1.5f, 1.5f,
+ -1.5f, -1.5f, -1.5f, -1.5f,
+ nan, nan, nan, nan,
+ 1.f, 3.f, 0.5f, 2.5f};
+
+ std::vector<float> expected;
+ expected.reserve(in0.size());
+ for (size_t i = 0; i < in0.size(); ++i) {
+ const float a = in0[i];
+ const float b = in1[i];
+ expected.push_back(mode == eltwise_mode::max ? (a < b ? b : a) : (b < a ? b : a));
+ }
+
+ std::vector<T> buf0, buf1;
+ for (size_t i = 0; i < in0.size(); ++i) {
+ buf0.push_back(static_cast<T>(in0[i]));
+ buf1.push_back(static_cast<T>(in1[i]));
+ }
+
+ auto input1 = engine.allocate_memory({ ov::element::from<T>(), format::bfyx, { 1, 4, 1, 4 } });
+ auto input2 = engine.allocate_memory({ ov::element::from<T>(), format::bfyx, { 1, 4, 1, 4 } });
+ set_values(input1, buf0);
+ set_values(input2, buf1);
+
+ topology topology;
+ topology.add(input_layout("input1", input1->get_layout()));
+ topology.add(input_layout("input2", input2->get_layout()));
+ topology.add(eltwise("eltwise", { input_info("input1"), input_info("input2") }, mode, DEFAULT_BROADCAST_SPEC));
+
+ network network(engine, topology, get_test_default_config(engine));
+ network.set_input_data("input1", input1);
+ network.set_input_data("input2", input2);
+
+ const auto outputs = network.execute();
+ ASSERT_EQ(outputs.size(), size_t(1));
+ ASSERT_EQ(outputs.begin()->first, "eltwise");
+
+ const auto output = outputs.at("eltwise").get_memory();
+ const cldnn::mem_lock<T, mem_lock_type::read> output_ptr(output, get_test_stream());
+
+ for (size_t i = 0; i < expected.size(); ++i) {
+ if (std::isnan(expected[i])) {
+ ASSERT_TRUE(std::isnan(static_cast<float>(output_ptr[i]))) << "at index " << i << ": expected NaN";
+ } else {
+ ASSERT_TRUE(floating_point_equal(static_cast<T>(expected[i]), output_ptr[i])) << "at index " << i;
+ }
+ }
+}
+
+TEST(eltwise_gpu_f32, nan_propagation_max) {
+ run_extremum_nan_propagation_test<float>(eltwise_mode::max);
+}
+
+TEST(eltwise_gpu_f32, nan_propagation_min) {
+ run_extremum_nan_propagation_test<float>(eltwise_mode::min);
+}
+
+TEST(eltwise_gpu_f16, nan_propagation_max) {
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_fp16) {
+ GTEST_SKIP() << "float16 combinations are skipped (cl_khr_fp16 is not supported).";
+ }
+ run_extremum_nan_propagation_test<ov::float16>(eltwise_mode::max);
+}
+
+TEST(eltwise_gpu_f16, nan_propagation_min) {
+ auto& engine = get_test_engine();
+ if (!engine.get_device_info().supports_fp16) {
+ GTEST_SKIP() << "float16 combinations are skipped (cl_khr_fp16 is not supported).";
+ }
+ run_extremum_nan_propagation_test<ov::float16>(eltwise_mode::min);
+}
+
TEST(eltwise_gpu_f32, dynamic_kernel_no_broadcast) {
auto& engine = get_test_engine();