diff --git a/dpnp/tests/test_binary_ufuncs.py b/dpnp/tests/test_binary_ufuncs.py index 226700df8f2..8ed8d951b68 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 0f42cbf6c92..f3a2ace59f7 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 f91e923df82..ef6b37e2618 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 e646adc15ae..acc770cb4e8 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 006b846eb23..46ac2d656aa 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)