Repository navigation
Fix fp64 use and erfinv precision in the fallback erf kernels - #3228
Open
antonwolfy wants to merge 3 commits into
Open
antonwolfy wants to merge 3 commits into
antonwolfy wants to merge 3 commits into
Conversation
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
requested review from
ndgrigorian and
vlad-perevezentsev
as code owners
December 5, 2026 17:16
7 tasks done
Contributor
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3228/index.html |
Collaborator
Contributor
|
Array API standard conformance tests for dpnp=0.22.0dev3+81.g4268c3a67 ran successfully. |
There was a problem hiding this comment.
🟡 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Builds without the Intel math extension (the open source DPC++ compiler, and the CUDA and HIP targets) take the
impl::kernels oferfinv.hppanderfcx.hppfordpnp.scipy.special.erfinv,erfcinvanderfcx. 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
float32input. They compute with double literals:erfinvanderfcinvcallndtriwith a double argument (ndtri(0.5 * (y + 1)),ndtri(0.5 * y)), andndtriitself uses1.0 - y0,-2.0 * sycl::log(y)and1.0 / x;erfcx_y100, and five more literals inerfcx, are unsuffixed doubles.On a device without fp64, every one of these
floatkernels fails withRequired aspect fp64 is not supported on the device(checked on an Iris Xe with a standalone kernel of each function). The fallbackndtrithat Addndtritodpnp.scipy.special#3223 adds uses the same code, so this change covers it too.erfinvloses precision near 0 and near ±1, in float32 and float64, because it rounds(1 + y) / 2before callingndtri:erfinv(nextafter(1, 0))returnsinfinstead of 3.83 (float32) or 5.86 (float64);erfinv(2e-7)is 19 % off in float32 and 3.5e6 ulp off in float64.Changes:
ndtriis split into its central and tail approximations,ndtri_centralandndtri_tail, with every constant written asTp(...). For float64,ndtridoes the same operations as before, in the same order, with one evaluation of the tail approximation for both tails.erfinvpasses those approximations exact arguments:y / 2in the centre and(1 - |y|) / 2in the tails, instead of a rounded(1 + y) / 2.erfcinvanderfcxwrite every literal asTp(...). Theerfcx.hppdiff is large but mechanical: only the literals change.erfcinvcomputesndtri(y / 2)withndtri_half, whose tail takeslog(y) - log(2)for a subnormaly: in float,y / 2would round it, the smallest one to 0, which givesinf. A select picks the log argument, so there is still one tail evaluation. float64 keeps SciPy's behaviour, which roundsy / 2the same way.TestErfinv::test_near_zero_and_onecheckserfinvnear 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_subnormalcheckserfcinvof float32 subnormals against SciPy, through the same two paths.Build with Open Source LLVM SYCL compilerjob now installs SciPy. It is the only job that builds the fallback, and without SciPy every test intest_special.pyskips there. With SciPy, the other SciPy-dependent tests indpnp/testsrun in that job too.Arithmetic in
floatonly makes the float32 fallback a little less accurate where it was fine before.erfcinvcomputed 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_funcsintest_strides.py,test_sycl_queue.pyandtest_usm_type.pypasses as well.erfinv, |y| ≤ 0.1erfinvnear ±1inf→ 2.2inf→ 2erfinv, uniform in (-1, 1)erfcinverfcxA reversed view gives the same results as a contiguous array. On the Iris Xe, a standalone
floatkernel of each fallback function fails with the fp64 aspect error before the change and runs after it, within 5.0 ulp of SciPy forerfinv, 5.8 forerfcinv, 4.3 forndtriand 9.8 forerfcx(x ≥ 0). The icpx build, which doesn't compile this code, passes the new test on the same devices.Performance:
erf_funcs.cppis 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 readssycl/ext/intel/math.hpp, not these headers.erfinvanderfcinv, 1 round, before the floaterfcinvsubnormal change)float32 gets faster because it no longer computes in double, and float64 doesn't get slower anywhere.