Skip to content

Fix fp64 use and erfinv precision in the fallback erf kernels - #3228

Open
antonwolfy wants to merge 3 commits into
masterfrom
fix-erf-fallback-fp64
Open

antonwolfy wants to merge 3 commits into
masterfrom
fix-erf-fallback-fp64

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Builds without the Intel math extension (the open source DPC++ compiler, and the CUDA and HIP targets) take the impl:: kernels of erfinv.hpp and erfcx.hpp for dpnp.scipy.special.erfinv, erfcinv and erfcx. The icpx builds for Intel devices use the Intel math functions instead and are not affected. The fallback kernels had two problems:

  • They need fp64 for float32 input. They compute with double literals:

    • erfinv and erfcinv call ndtri with a double argument (ndtri(0.5 * (y + 1)), ndtri(0.5 * y)), and ndtri itself uses 1.0 - y0, -2.0 * sycl::log(y) and 1.0 / x;
    • the 700 polynomial coefficients of erfcx_y100, and five more literals in erfcx, are unsuffixed doubles.

    On a device without fp64, every one of these float kernels fails with Required aspect fp64 is not supported on the device (checked on an Iris Xe with a standalone kernel of each function). The fallback ndtri that Add ndtri to dpnp.scipy.special #3223 adds uses the same code, so this change covers it too.

  • erfinv loses precision near 0 and near ±1, in float32 and float64, because it rounds (1 + y) / 2 before calling ndtri:

    • erfinv(nextafter(1, 0)) returns inf instead of 3.83 (float32) or 5.86 (float64);
    • erfinv(2e-7) is 19 % off in float32 and 3.5e6 ulp off in float64.

Changes:

  • ndtri is split into its central and tail approximations, ndtri_central and ndtri_tail, with every constant written as Tp(...). For float64, ndtri does the same operations as before, in the same order, with one evaluation of the tail approximation for both tails.
  • erfinv passes those approximations exact arguments: y / 2 in the centre and (1 - |y|) / 2 in the tails, instead of a rounded (1 + y) / 2.
  • erfcinv and erfcx write every literal as Tp(...). The erfcx.hpp diff is large but mechanical: only the literals change.
  • For float, erfcinv computes ndtri(y / 2) with ndtri_half, whose tail takes log(y) - log(2) for a subnormal y: in float, y / 2 would round it, the smallest one to 0, which gives inf. A select picks the log argument, so there is still one tail evaluation. float64 keeps SciPy's behaviour, which rounds y / 2 the same way.
  • TestErfinv::test_near_zero_and_one checks erfinv near 0 and ±1 against SciPy with a relative tolerance, for a contiguous array and a reversed view. The view runs the SYCL kernel instead of oneMKL VM. All four cases fail on a fallback build without the fix.
  • TestErfcinv::test_float32_subnormal checks erfcinv of float32 subnormals against SciPy, through the same two paths.
  • The Build with Open Source LLVM SYCL compiler job now installs SciPy. It is the only job that builds the fallback, and without SciPy every test in test_special.py skips there. With SciPy, the other SciPy-dependent tests in dpnp/tests run in that job too.

Arithmetic in float only makes the float32 fallback a little less accurate where it was fine before. erfcinv computed in double and was within 0.8 ulp of SciPy; it is now within 5.8 ulp. In exchange, the kernels run without fp64 and stop using double arithmetic, which many consumer NVIDIA and AMD GPUs run at a small fraction of their float rate.

Tested on a build with the 2026-12-04 nightly of the open source DPC++ compiler and --onemath, on a Data Center GPU Max 1100 (Level Zero) and a Xeon Platinum 8480+ (OpenCL CPU):

  • test_special.py: 60 passed on both devices, including the 4 new cases. -k erf_funcs in test_strides.py, test_sycl_queue.py and test_usm_type.py passes as well.
  • Max ulp against SciPy 1.18.1, over 10^6 uniform points plus extra points near the bounds, on both devices:
float32, before → after float64, before → after
erfinv, |y| ≤ 0.1 3.7e6 → 1.4 3.9e6 → 7
erfinv near ±1 inf → 2.2 inf → 2
erfinv, uniform in (-1, 1) 165,602 → 5.9 7 → 7
erfcinv 0.8 → 5.8 unchanged
erfcx unchanged (x ≥ 0: 7.7-10.1 → 8.3-9.8) unchanged

A reversed view gives the same results as a contiguous array. On the Iris Xe, a standalone float kernel of each fallback function fails with the fp64 aspect error before the change and runs after it, within 5.0 ulp of SciPy for erfinv, 5.8 for erfcinv, 4.3 for ndtri and 9.8 for erfcx (x ≥ 0). The icpx build, which doesn't compile this code, passes the new test on the same devices.

Performance:

  • The icpx builds for Intel devices compile the same code as before. erf_funcs.cpp is the only file that includes these headers, and with this change its object file and device bitcode are byte-identical to master's: the device pass reads sycl/ext/intel/math.hpp, not these headers.
  • In builds that use the fallback, the time relative to master, as the median over 3 rounds of the median of 20 calls (after a 300 ms warm-up for the standalone kernels), for every function at n = 1e6, 1e7 and 1e8, contiguous and with stride 2:
Device float32 float64
Data Center GPU Max 1100, the dpnp build above (erfinv and erfcinv, 1 round, before the float erfcinv subnormal change) 0.42-0.88 0.95-1.01
Arc Pro B70, standalone kernels built with icpx 0.09-0.54 0.95-1.00
Panther Lake H GPU, standalone kernels built with icpx 0.07-0.39 0.96-1.01
Core i5-13400 (OpenCL CPU), standalone kernels built with icpx 0.47-0.85 0.91-1.01

float32 gets faster because it no longer computes in double, and float64 doesn't get slower anywhere.

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

Builds without the Intel math extension (the open source DPC++ compiler, and the CUDA and HIP targets) use the `impl::` kernels of `erfinv.hpp` and `erfcx.hpp`. They computed with double literals, so their `float` kernels needed fp64, and `erfinv` rounded `(1 + y) / 2` before calling `ndtri`, which lost precision near 0 and ±1.

`ndtri` is split into its central and tail approximations, `erfinv` passes them exact arguments, and every literal is written as `Tp(...)`. The open source DPC++ job installs SciPy, so the new test runs there.
@antonwolfy antonwolfy self-assigned this Dec 5, 2026
@antonwolfy antonwolfy added this to the 0.22.0 release milestone Dec 5, 2026
@antonwolfy antonwolfy mentioned this pull request Dec 5, 2026
7 tasks done
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

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

@coveralls

coveralls commented Dec 5, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 84.61%. remained the same — fix-erf-fallback-fp64 into master

@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The float32 erfcinv fallback underflows the smallest positive input to zero, returning infinity instead of a finite result, and needs a tail calculation fix with regression coverage.

1 open issue
What changed in this PR

Updates dpnp’s fallback error-function kernels to avoid fp64 arithmetic for float32 inputs and improve erfinv precision near zero and ±1.

Changes:

  • Separates central and tail approximations and uses dtype-matched arithmetic.
  • Adds precision regression tests for contiguous and reversed arrays.
  • Enables SciPy-dependent tests in the open-source SYCL build and documents the fixes.
File Description
dpnp/​tests/​test_special.py Adds erfinv precision regression tests
dpnp/​libtensor/​include/​kernels/​elementwise_functions/​erfinv.hpp Refactors approximations and removes float32 promotion
dpnp/​libtensor/​include/​kernels/​elementwise_functions/​erfcx.hpp Converts floating-point literals to the input dtype
CHANGELOG.md Documents fallback kernel fixes
.github/​workflows/​os-llvm-sycl-build.yml Installs SciPy to enable dependent tests

Review effort: Balanced

Calling `ndtri_tail` from two branches made a sub-group with work-items in both tails run it twice, so float64 `erfcinv` and `ndtri` were up to 1.5x slower than master on GPUs. `ndtri` now picks the tail argument first, as master does, and makes one tail call.
In float arithmetic `0.5 * y` rounds a subnormal `y`, so `erfcinv` returned `inf` for the smallest one and lost precision for odd multiples of it. For float, `erfcinv` now calls `ndtri_half(y)`, whose tail takes `log(y) - log(2)` for a subnormal `y`. A select picks the log argument, so the kernel keeps one tail evaluation and other inputs give the same results. float64 keeps SciPy's rounding.

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