Repository navigation
Add tests with subnormal inputs and results - #3212
antonwolfy wants to merge 2 commits into
Conversation
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.
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3212/index.html |
|
Array API standard conformance tests for dpnp=0.22.0dev3=np2py314h8d9cdd5_64 ran successfully. |
vlad-perevezentsev
left a comment
There was a problem hiding this comment.
The new tests pass on GPUs without fp64 (UHD 770/730) and on OpenCL CPUs, and their cases also match NumPy on an Arc B580: no flushing to zero anywhere. One case they miss is wrong on Intel GPUs today (comment below), plus two small nits.
| 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) |
There was a problem hiding this comment.
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.
| # 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 |
There was a problem hiding this comment.
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?
| assert_allclose(result, expected) | ||
|
|
||
| @pytest.mark.parametrize("sign", [1, -1]) | ||
| @pytest.mark.parametrize("dt", get_float_dtypes()) |
There was a problem hiding this comment.
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.
This PR adds tests that compare dpnp with NumPy on subnormal (denormal) floating-point values, which dpnp's own tests barely covered.
Since DPC++ 2026.1 the compiler no longer flushes denormals to zero in SYCL device code, so the device kernels are expected to keep subnormals as NumPy does. Only a few existing tests would notice if they didn't:
TestSpacing::test_zeros,TestSpacing::test_fp16and theGeneratortesttest_uniform_tiny_range.Each new test compares exactly with NumPy, in float32 and float64 (float64 only on devices with fp64):
TestNextafter::test_subnormal: steps from zero to the smallest subnormal and back, and across the boundary between subnormal and normal numbers.TestLdexp::test_subnormal: normal mantissas scaled into the subnormal range, and subnormal mantissas scaled back to normal numbers.TestSpacing::test_subnormal: spacing of subnormals and of the smallest normal numbers, both signs.TestUnaryTwoOutputs::test_subnormal:frexpandmodfof subnormals, also in float16 on devices with fp16.test_sum_subnormal: sums of 100,000 subnormals over each axis. Every partial sum is an exact multiple of the smallest subnormal, so the result doesn't depend on the order of summation.TestMaxMin::test_subnormal:maxandminof a shuffled range of subnormals of both signs.The denormal skip in the third party test
math_tests/test_floating.pystays as it is.