Skip to content

Add tests with subnormal inputs and results - #3212

Open
antonwolfy wants to merge 2 commits into
masterfrom
subnormal-tests
Open

antonwolfy wants to merge 2 commits into
masterfrom
subnormal-tests

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

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_fp16 and the Generator test test_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: frexp and modf of 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: max and min of a shuffled range of subnormals of both signs.

The denormal skip in the third party test math_tests/test_floating.py stays as it is.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

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.
@github-actions

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/pull/3212/index.html

@github-actions

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.22.0dev3=np2py314h8d9cdd5_64 ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 84.556%. remained the same — subnormal-tests into master

@vlad-perevezentsev vlad-perevezentsev left a comment

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 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.

Comment on lines +733 to +734
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)

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.

Comment on lines +862 to +864
# 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

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?

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants