Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions dpnp/tests/test_binary_ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Comment on lines +733 to +734

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test has no -sub → 0 case, and assert_equal does not check the sign of zero. On Intel GPUs this case is wrong: nextafter(-smallest_subnormal, 0) gives +0.0, NumPy gives -0.0:

import numpy, dpnp

for dt in (dpnp.float16, dpnp.float32):
    a = dpnp.array([-numpy.finfo(dt).smallest_subnormal], dtype=dt)
    print(dt.__name__, dpnp.signbit(dpnp.nextafter(a, 0)))
# UHD 770/730: float16 [False], float32 [False]; Arc B580: float16 [False], float32 [True]; CPU and numpy: [True]

Could you add (-sub, 0) with a signbit check, as test_signed_zero does? On GPUs it then needs a kernel fix or an xfail with an issue.

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):
Expand Down
16 changes: 16 additions & 0 deletions dpnp/tests/test_mathematical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: float16 has the narrowest subnormal range, and spacing has its own float16 branch. Could the new spacing, ldexp and nextafter tests use get_float_dtypes(no_float16=False), as the modf/frexp test does? With float16 their cases pass on UHD 770, UHD 730, Arc B580 and the CPUs.

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])
Expand Down
14 changes: 14 additions & 0 deletions dpnp/tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +862 to +864

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: The minimum is the first element here: index 0 gives 0 - n // 2 = -5000. (numpy.arange(n) * 7919 + 1) % n puts both extremes in the middle (indices 2321 and 4642). Could axis=0 be added too, as in test_sum_subnormal?

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)])
Expand Down
13 changes: 13 additions & 0 deletions dpnp/tests/test_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions dpnp/tests/test_unary_two_outputs_ufuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading