From 8a50bb5ba313ca639c63dd3ece7478e62656b588 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Mon, 30 Nov 2026 15:05:57 +0100 Subject: [PATCH 1/2] Add tests with subnormal inputs and results Exact NumPy comparisons for `nextafter`, `ldexp`, `spacing`, `frexp`/`modf`, `sum` and `max`/`min` with subnormal values, in float32 and float64, and in float16 for `frexp`/`modf` on devices with fp16. --- dpnp/tests/test_binary_ufuncs.py | 30 +++++++++++++++++++++ dpnp/tests/test_mathematical.py | 16 +++++++++++ dpnp/tests/test_statistics.py | 14 ++++++++++ dpnp/tests/test_sum.py | 13 +++++++++ dpnp/tests/test_unary_two_outputs_ufuncs.py | 15 +++++++++++ 5 files changed, 88 insertions(+) diff --git a/dpnp/tests/test_binary_ufuncs.py b/dpnp/tests/test_binary_ufuncs.py index 2e97047951a8..d0830b2b5855 100644 --- a/dpnp/tests/test_binary_ufuncs.py +++ b/dpnp/tests/test_binary_ufuncs.py @@ -558,6 +558,23 @@ def test_strides(self, stride, dt): expected = numpy.ldexp(mant[::stride], exp[::stride], out=out[::stride]) assert_equal(result, expected) + @pytest.mark.parametrize("dt", get_float_dtypes()) + def test_subnormal(self, dt): + fi = numpy.finfo(dt) + sub, tiny = fi.smallest_subnormal, fi.tiny + e_sub = fi.minexp - fi.nmant # exponent of the smallest subnormal + + # normal mantissas scaled to subnormals, then subnormal mantissas + mant = numpy.array([1, 1, 0.75, -1, sub, 3 * sub, tiny - sub], dtype=dt) + exp = numpy.array( + [e_sub, fi.minexp - 1, fi.minexp, e_sub, fi.nmant, 1, 1], dtype="i" + ) + imant, iexp = dpnp.array(mant), dpnp.array(exp) + + result = dpnp.ldexp(imant, iexp) + expected = numpy.ldexp(mant, exp) + assert_equal(result, expected) + def test_bool_exp(self): result = dpnp.ldexp(3.7, dpnp.array(True)) expected = numpy.ldexp(3.7, numpy.array(True)) @@ -708,6 +725,19 @@ def test_float_nan(self, dt): expected = numpy.nextafter(numpy.nan, a) assert_equal(result, expected) + @pytest.mark.parametrize("dt", get_float_dtypes()) + def test_subnormal(self, dt): + fi = numpy.finfo(dt) + sub, tiny = fi.smallest_subnormal, fi.tiny + + a = numpy.array([0, 0, sub, sub, tiny, tiny - sub, -tiny], dtype=dt) + b = numpy.array([1, -1, 0, 1, 0, numpy.inf, 0], dtype=dt) + ia, ib = dpnp.array(a), dpnp.array(b) + + result = dpnp.nextafter(ia, ib) + expected = numpy.nextafter(a, b) + assert_equal(result, expected) + @pytest.mark.skipif(not has_support_aspect16(), reason="no fp16 support") @pytest.mark.parametrize("val", [0x7C00, 0x8000], ids=["val1", "val2"]) def test_f16_strides(self, val): diff --git a/dpnp/tests/test_mathematical.py b/dpnp/tests/test_mathematical.py index 0f42cbf6c925..f3a2ace59f70 100644 --- a/dpnp/tests/test_mathematical.py +++ b/dpnp/tests/test_mathematical.py @@ -1771,6 +1771,22 @@ def test_zeros(self, dt): expected[1] *= -1 assert_allclose(result, expected) + @pytest.mark.parametrize("sign", [1, -1]) + @pytest.mark.parametrize("dt", get_float_dtypes()) + def test_subnormal(self, sign, dt): + fi = numpy.finfo(dt) + sub, tiny = fi.smallest_subnormal, fi.tiny + + a = numpy.array( + [sub, 2 * sub, 3 * sub, tiny - sub, tiny, 2 * tiny], dtype=dt + ) + a *= sign + ia = dpnp.array(a) + + result = dpnp.spacing(ia) + expected = numpy.spacing(a) + assert_equal(result, expected) + @pytest.mark.parametrize("dt", get_float_dtypes(no_float16=False)) @pytest.mark.parametrize("val", [1, 1e-5, 1000]) @pytest.mark.parametrize("xp", [numpy, dpnp]) diff --git a/dpnp/tests/test_statistics.py b/dpnp/tests/test_statistics.py index f91e923df82f..ef6b37e2618f 100644 --- a/dpnp/tests/test_statistics.py +++ b/dpnp/tests/test_statistics.py @@ -855,6 +855,20 @@ def test_error(self, func): with pytest.raises(NotImplementedError): getattr(dpnp, func)(ia, initial=6) + @pytest.mark.parametrize("axis", [None, 1]) + @pytest.mark.parametrize("dtype", get_float_dtypes()) + def test_subnormal(self, func, axis, dtype): + sub = numpy.finfo(dtype).smallest_subnormal + # a permutation of -5000..4999, so the extremes are not at the ends + n = 10**4 + a = ((numpy.arange(n) * 7919) % n - n // 2).astype(dtype) * sub + a = a.reshape(100, 100) + ia = dpnp.array(a) + + result = getattr(dpnp, func)(ia, axis=axis) + expected = getattr(numpy, func)(a, axis=axis) + assert_array_equal(result, expected) + class TestMinMax: @pytest.mark.parametrize("axis", [None, 0, 1, -1, 2, -2, (1, 2), (0, -2)]) diff --git a/dpnp/tests/test_sum.py b/dpnp/tests/test_sum.py index e646adc15ae3..acc770cb4e8f 100644 --- a/dpnp/tests/test_sum.py +++ b/dpnp/tests/test_sum.py @@ -92,6 +92,19 @@ def test_sum_float(dtype): assert_dtype_allclose(result, expected) +@pytest.mark.parametrize("axis", [None, 0, 1]) +@pytest.mark.parametrize("dtype", get_float_dtypes()) +def test_sum_subnormal(axis, dtype): + # every partial sum is an exact multiple of the smallest subnormal + sub = numpy.finfo(dtype).smallest_subnormal + a = numpy.resize(numpy.arange(1, 8, dtype=dtype), (250, 400)) * sub + ia = dpnp.array(a) + + result = dpnp.sum(ia, axis=axis) + expected = numpy.sum(a, axis=axis) + assert_array_equal(result, expected) + + def test_sum_int(): a = numpy.array([1, 0, 2, -3, -1, 2, 21, -9]) ia = dpnp.array(a) diff --git a/dpnp/tests/test_unary_two_outputs_ufuncs.py b/dpnp/tests/test_unary_two_outputs_ufuncs.py index 006b846eb236..46ac2d656aae 100644 --- a/dpnp/tests/test_unary_two_outputs_ufuncs.py +++ b/dpnp/tests/test_unary_two_outputs_ufuncs.py @@ -44,6 +44,21 @@ def test_basic(self, func, dt): assert_array_equal(res1, exp1) assert_array_equal(res2, exp2) + @pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES) + def test_subnormal(self, func, dt): + fi = numpy.finfo(dt) + sub, tiny = fi.smallest_subnormal, fi.tiny + + a = numpy.array( + [sub, 3 * sub, tiny - sub, -sub, -(tiny - sub)], dtype=dt + ) + ia = dpnp.array(a) + + res1, res2 = getattr(dpnp, func)(ia) + exp1, exp2 = getattr(numpy, func)(a) + assert_array_equal(res1, exp1) + assert_array_equal(res2, exp2) + @pytest.mark.parametrize("dt", ALL_FLOAT_DTYPES) def test_out(self, func, dt): a = numpy.array(5.7, dtype=dt) From 38c77c01c5982a15e9c62917197e925877154a44 Mon Sep 17 00:00:00 2001 From: Anton Volkov Date: Sat, 5 Dec 2026 00:12:40 +0100 Subject: [PATCH 2/2] Address review comments on the subnormal tests Add `TestNextafter::test_subnormal_to_zero`, which checks the sign of the zero that `nextafter` returns when it steps from the smallest subnormal to zero. IGC before 2.43.9 (Compute Runtime 26.44) returns +0.0 for the negative one, so the test is a non-strict xfail on Intel GPUs with an older Compute Runtime, which the new `is_old_gpu_driver` helper detects. Run the `ldexp`, `spacing` and `nextafter` subnormal tests in float16 too, and in the `max`/`min` test move both extremes away from the ends of the array and add `axis=0`. --- dpnp/tests/helper.py | 19 +++++++++++++++++++ dpnp/tests/test_binary_ufuncs.py | 23 +++++++++++++++++++++-- dpnp/tests/test_mathematical.py | 2 +- dpnp/tests/test_statistics.py | 4 ++-- 4 files changed, 43 insertions(+), 5 deletions(-) diff --git a/dpnp/tests/helper.py b/dpnp/tests/helper.py index 89ae73a69d79..b055129daa6c 100644 --- a/dpnp/tests/helper.py +++ b/dpnp/tests/helper.py @@ -1,4 +1,5 @@ import importlib.util +import re from enum import Enum from sys import platform @@ -497,6 +498,24 @@ def is_lts_driver(version=LTS_VERSION.V1_6, device=None): return dev.has_aspect_gpu and version.value in dev.driver_version +def is_old_gpu_driver(build, device=None): + """ + Return True if a test is running on an Intel GPU device whose Compute + Runtime build is older than `build` (e.g. 40734 for 26.44.40734), + False otherwise. + """ + dev = dpctl.select_default_device() if device is None else device + if not dev.has_aspect_gpu or dev.backend not in ( + dpctl.backend_type.level_zero, + dpctl.backend_type.opencl, + ): + return False + + # the build is the third field: "1.15.39122+14" (L0), "26.27.39122.14" + m = re.match(r"\d+\.\d+\.(\d+)", dev.driver_version) + return m is None or int(m.group(1)) < build + + def is_ptl(device=None): """ Return True if a test is running on Panther Lake with Iris Xe3 GPU device diff --git a/dpnp/tests/test_binary_ufuncs.py b/dpnp/tests/test_binary_ufuncs.py index 8ed8d951b68f..f223cc94b090 100644 --- a/dpnp/tests/test_binary_ufuncs.py +++ b/dpnp/tests/test_binary_ufuncs.py @@ -22,6 +22,7 @@ get_integer_dtypes, get_integer_float_dtypes, has_support_aspect16, + is_old_gpu_driver, numpy_version, ) from .third_party.cupy import testing @@ -558,7 +559,7 @@ def test_strides(self, stride, dt): expected = numpy.ldexp(mant[::stride], exp[::stride], out=out[::stride]) assert_equal(result, expected) - @pytest.mark.parametrize("dt", get_float_dtypes()) + @pytest.mark.parametrize("dt", get_float_dtypes(no_float16=False)) def test_subnormal(self, dt): fi = numpy.finfo(dt) sub, tiny = fi.smallest_subnormal, fi.tiny @@ -725,7 +726,7 @@ def test_float_nan(self, dt): expected = numpy.nextafter(numpy.nan, a) assert_equal(result, expected) - @pytest.mark.parametrize("dt", get_float_dtypes()) + @pytest.mark.parametrize("dt", get_float_dtypes(no_float16=False)) def test_subnormal(self, dt): fi = numpy.finfo(dt) sub, tiny = fi.smallest_subnormal, fi.tiny @@ -738,6 +739,24 @@ def test_subnormal(self, dt): expected = numpy.nextafter(a, b) assert_equal(result, expected) + @pytest.mark.xfail( + is_old_gpu_driver(40734), + reason="GSD-13906: fixed in IGC 2.43.9 (Compute Runtime 26.44)", + strict=False, + ) + @pytest.mark.parametrize("dt", get_float_dtypes(no_float16=False)) + def test_subnormal_to_zero(self, dt): + sub = numpy.finfo(dt).smallest_subnormal + + a = numpy.array([sub, sub, -sub, -sub], dtype=dt) + b = numpy.array([0, -1, 0, 1], dtype=dt) + ia, ib = dpnp.array(a), dpnp.array(b) + + result = dpnp.nextafter(ia, ib) + expected = numpy.nextafter(a, b) + assert_equal(result, expected) + assert_equal(dpnp.signbit(result), numpy.signbit(expected)) + @pytest.mark.skipif(not has_support_aspect16(), reason="no fp16 support") @pytest.mark.parametrize("val", [0x7C00, 0x8000], ids=["val1", "val2"]) def test_f16_strides(self, val): diff --git a/dpnp/tests/test_mathematical.py b/dpnp/tests/test_mathematical.py index f3a2ace59f70..1b2d19da7751 100644 --- a/dpnp/tests/test_mathematical.py +++ b/dpnp/tests/test_mathematical.py @@ -1772,7 +1772,7 @@ def test_zeros(self, dt): assert_allclose(result, expected) @pytest.mark.parametrize("sign", [1, -1]) - @pytest.mark.parametrize("dt", get_float_dtypes()) + @pytest.mark.parametrize("dt", get_float_dtypes(no_float16=False)) def test_subnormal(self, sign, dt): fi = numpy.finfo(dt) sub, tiny = fi.smallest_subnormal, fi.tiny diff --git a/dpnp/tests/test_statistics.py b/dpnp/tests/test_statistics.py index ef6b37e2618f..6d4f064c9510 100644 --- a/dpnp/tests/test_statistics.py +++ b/dpnp/tests/test_statistics.py @@ -855,13 +855,13 @@ def test_error(self, func): with pytest.raises(NotImplementedError): getattr(dpnp, func)(ia, initial=6) - @pytest.mark.parametrize("axis", [None, 1]) + @pytest.mark.parametrize("axis", [None, 0, 1]) @pytest.mark.parametrize("dtype", get_float_dtypes()) def test_subnormal(self, func, axis, dtype): sub = numpy.finfo(dtype).smallest_subnormal # a permutation of -5000..4999, so the extremes are not at the ends n = 10**4 - a = ((numpy.arange(n) * 7919) % n - n // 2).astype(dtype) * sub + a = ((numpy.arange(n) * 7919 + 1) % n - n // 2).astype(dtype) * sub a = a.reshape(100, 100) ia = dpnp.array(a)