Skip to content

Fix test tolerances for float16 precision in math tests#2828

Open
antonwolfy wants to merge 4 commits intomasterfrom
fix-tolerance-when-testing-against-cf-numpy
Open

Fix test tolerances for float16 precision in math tests#2828
antonwolfy wants to merge 4 commits intomasterfrom
fix-tolerance-when-testing-against-cf-numpy

Conversation

@antonwolfy
Copy link
Contributor

@antonwolfy antonwolfy commented Mar 25, 2026

This PR fixes test failures when testing with DPNP_TEST_ALL_INT_TYPES=1 against conda-forge's NumPy, where float16 precision is used in various scenarios requiring relaxed tolerances.

Issue

Tests were failing with tolerance violations when using int8/uint8 dtypes or when outputs have float16 precision. Float16 has approximately 3 decimal digits of precision, making the original strict tolerances (atol=1e-5, rtol=1e-7) inappropriate.

Changes

1. test_explog.py (commit f00eda8)

  • Updated _rtol_dict to handle float16 output dtype
  • Applied relaxed rtol=1e-3 for float16, rtol=1e-7 for other dtypes
  • Affects tests: exp, expm1, exp2, log, log10, log2, log1p, etc.

2. test_hyperbolic.py (commit dbb7199)

  • Updated _rtol_dict to handle float16 output dtype
  • Applied relaxed rtol=1e-3 for float16, rtol=1e-6 for other dtypes
  • Affects tests: sinh, cosh, tanh, arcsinh, arctanh, etc.

3. test_misc.py + testing framework (commit 9b108ec)

Framework enhancement:

  • Extended _resolve_tolerance() in testing/_loops.py to support callable tolerance functions
  • Callables receive signature: (result_dtype, **test_kwargs) enabling dynamic tolerance based on test parameters
  • Updated documentation and validation in _check_tolerance_keys()

Test implementation:

  • Added _interp_atol() method that dynamically adjusts tolerance based on dtype_x
  • Returns atol=1e-3 when dtype_x is int8/uint8/float16 (float16 intermediate precision in sin())
  • Returns atol=1e-5 for high-precision dtypes
  • Applied to all 11 test_interp* methods

This approach was necessary because test_interp has float16 precision in intermediate computation (sin(fx)) rather than output, requiring inspection of input dtypes rather than output dtypes.

  • 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?

@antonwolfy antonwolfy added this to the 0.20.0 release milestone Mar 25, 2026
@antonwolfy antonwolfy self-assigned this Mar 25, 2026
@github-actions
Copy link
Contributor

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

@github-actions
Copy link
Contributor

Array API standard conformance tests for dpnp=0.20.0dev4=py313h509198e_21 ran successfully.
Passed: 1356
Failed: 1
Skipped: 16

@coveralls
Copy link
Collaborator

Coverage Status

coverage: 81.977%. remained the same
when pulling 2013d47 on fix-tolerance-when-testing-against-cf-numpy
into 5e5dc24 on master.

@antonwolfy antonwolfy marked this pull request as ready for review March 25, 2026 16:09
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.

2 participants