Repository navigation
Add ndtri to dpnp.scipy.special - #3223
abagusetty wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
I tested this head on a Data Center GPU Max 1100 and the Xeon Platinum 8480+ of the same host (OpenCL CPU) with compute-runtime 25.18 / IGC 2.11, and on three client GPUs with compute-runtime 26.40 / IGC 2.42: an Iris Xe (no fp64), an Arc Pro B70 and a Panther Lake H iGPU. All runs used oneAPI 2026.2, Python 3.13 and SciPy 1.18.1, with this head built in place.
The kernel is accurate. Against SciPy 1.18.1 it is within 5 ulp in float64 and 3.03 ulp in float32 on every device. The special values match SciPy: 0 and -0.0 give -inf, 1 gives inf, 0.5 gives 0, and NaN or anything outside [0, 1] gives NaN. Float32 subnormal inputs aren't flushed, and on the fp64 devices the result dtypes match SciPy for all 12 input kinds. The Intel math path runs without fp64. Points 1-5 have inline comments:
TestNdtri::test_inverse_of_cdffails on a device without fp64, in every run on the Iris Xe. In float32,pcan't pinxdown to the tolerance beyond |x| ≈ 3. The function is fine, and a narrower range fixes the test.- The docstring's verification example raises in dpnp, because
np.sqrt(2)gets a Python int. - The new docs section is called "Raw statistical functions" in SciPy and CuPy.
- The changelog entry links #3219 instead of this PR.
ndtrishould have the oneMKL VM path, as the other erf functions have. In float32 it gives 0.50 ulp instead of 3.03, at about the same speed.- Optional: CuPy's upstream
ndtritest. CuPy testsndtriincupyx_tests/scipy_tests/special_tests/test_statistics.py(linspace(0, 1, 1000)in float16, float32 and float64). dpnp hasn't synced that file intodpnp/tests/third_party/cupyx/yet. With only its imports changed,TestSpecial::test_ndtripasses on every device above. A sync here would skip, with a reason string, the tests of the functions dpnp doesn't have (ndtr,log_ndtr,logit,expit,boxcoxand others), andTestFusionSpecialtoo, since dpnp has nofuse.
Tests
| Check | Result |
|---|---|
test_special.py with CuPy's test_erf.py, -k erf_funcs in test_strides.py, test_sycl_queue.py and test_usm_type.py, and CuPy's test_statistics.py::TestSpecial::test_ndtri (point 6): 3 runs each on the Max 1100 and the Xeon, 1 run on each client GPU |
all pass, apart from test_inverse_of_cdf on the Iris Xe (point 1) |
The same tests with a --onemath build, on the Max 1100 and the Xeon |
all pass |
test_special.py and -k erf_funcs in test_strides.py and test_sycl_queue.py, on a build of this head with the 2026-12-04 nightly of the open-source DPC++ and SciPy installed, on the Max 1100 and the Xeon |
all pass. This build takes the Cephes impl::ndtri instead of normcdfinv, and is within 6 ulp (float64) and 4.2 ulp (float32) of SciPy |
Accuracy against SciPy 1.18.1, and against 50-digit mpmath on 4000 of the uniform points and on all the other points: 10^6 uniform points in (0, 1), 10**-k down to 1e-307, 1 - 10**-k, subnormals, the neighbours of 0, 0.5 and 1, and special values. Contiguous and [::-1], float64, float32 and float16 |
max ulp: float64 5, float32 3.03 on the GPUs and 2.77 on the CPU, float16 (computed in float32) 2.25. Every GPU gives the same maxima |
Mutants of the kernel: a sign flip, the input clamped to [0, 1], and sqrt(2) * erfinv(2x - 1) (which loses the lower tail) |
each one fails at least one new test, and only test_tails catches the lower-tail loss |
ndtri vs -sqrt(2) * erfcinv(2 * x) in dpnp, float64, n = 1e6 to 1e8, contiguous and strided |
2.4-6.2x faster on the Max 1100, 4.2-8.8x on the Xeon |
Size of _tensor_elementwise_impl |
+2.8 MB (+8.8 %), build time unchanged |
| assert_allclose(result, expected, rtol=self.tol) | ||
|
|
||
| def test_inverse_of_cdf(self): | ||
| x = dpnp.linspace(-4, 4, 33) |
There was a problem hiding this comment.
This test fails on a device without fp64. There x is float32 and tol is 100 * eps(float32), 1.19e-5. On an Iris Xe it fails in every run, always at the same three points:
| x | result | error / limit |
|---|---|---|
| -4 | -3.9999354 | 1.08 |
| 3.75 | 3.7498977 | 1.81 |
| 4 | 3.9997129 | 4.82 |
ndtri itself is fine there: its result is within 1.41 ulp of SciPy's float64 ndtri of the same float32 p. The test is ill-conditioned in float32:
- near
p = 1, one float32 ulp ofpmovesxby 4.5e-4 at x = 4 and by 1.7e-4 at x = 3.75; - at x = -4,
0.5 * (1 + erf(x / sqrt(2)))cancels, sopis already 2.7e-4 off Φ(-4), in relative terms.
The GitHub CI runs on CPU devices, which have fp64, so it can't show this.
Over [-3, 3] the test passes on the Iris Xe at 0.135 of the limit. test_tails still covers the tails on fp64 devices:
| x = dpnp.linspace(-4, 4, 33) | |
| # beyond |x| = 3 a float32 p can't resolve x to tol | |
| x = dpnp.linspace(-3, 3, 25) |
| ndtri = DPNPErf( | ||
| "ndtri", | ||
| ti._erf_result_type, | ||
| ti._ndtri, | ||
| _NDTRI_DOCSTRING, | ||
| ) |
There was a problem hiding this comment.
Please add the oneMKL VM path here. The other five functions in this file pass mkl_fn_to_call="_mkl_erf_to_call" and mkl_impl_fn, and oneMKL VM has this function as cdfnorminv (float and double). For ndtri that takes:
- a
MACRO_DEFINE_IMPL(cdfnorminv, Cdfnorminv)entry inextensions/vm/erf_funcs.cpp, registered as_ndtri; mkl_impl_fn="_ndtri"here.
This kernel stays where VM isn't used: in oneMath builds (the extension isn't built), on devices without fp64, and for non-contiguous input.
What VM gains is float32 accuracy. Max ulp against SciPy 1.18.1, with oneMKL 2026.2, over the same points as in the review body:
| Max 1100, float32 | Xeon, float32 | Max 1100, float64 | Xeon, float64 | |
|---|---|---|---|---|
normcdfinv (this PR) |
3.03 | 2.77 | 5 | 5 |
VM cdfnorminv, default mode (HA) |
0.50 | 0.55 | 4 | 4 |
A standalone program measured both, calling VM directly and running this kernel. Time is about the same. VM / kernel at n = 1e5 to 1e8:
- on the Max 1100: 0.81-1.14 for float32 and 1.03-1.19 for float64, where the ratios above 1.1 are calls of 12-28 µs;
- on the Xeon's OpenCL CPU device: 0.50-0.99, apart from float32 at n = 1e8 (1.46).
Both give the same special values as SciPy.
With the VM path, TestConsistency::test_ndtri also compares VM with the kernel. Today its inverse parameter runs the kernel both times.
| Verify that the standard normal cumulative distribution function of ``y`` is | ||
| ``x``: | ||
|
|
||
| >>> 0.5 * (1 + np.scipy.special.erf(y / np.sqrt(2))) |
There was a problem hiding this comment.
With np = dpnp, np.sqrt(2) raises TypeError: Input array must be any of supported type, but got <class 'int'>. The docs build doesn't run the examples, which is why it rendered. 2**0.5 prints the output shown:
| >>> 0.5 * (1 + np.scipy.special.erf(y / np.sqrt(2))) | |
| >>> 0.5 * (1 + np.scipy.special.erf(y / 2**0.5)) |
| Statistical functions | ||
| --------------------- |
There was a problem hiding this comment.
SciPy's scipy.special reference calls this section "Raw statistical functions", and so does CuPy's:
| Statistical functions | |
| --------------------- | |
| Raw statistical functions | |
| ------------------------- |
| * Added the `dpnp.random.Generator` API with `default_rng`, `BitGenerator`, `SeedSequence` and the `Philox4x3210` bit generator, sampling on the device with the device RNG API of oneMKL or oneMath [#3157](https://github.com/IntelPython/dpnp/pull/3157) | ||
| * Added `dpnp.argpartition` and `dpnp.ndarray.argpartition` to return the indices that would partition an array [#3138](https://github.com/IntelPython/dpnp/pull/3138) | ||
| * Added implementation of `dpnp.scipy.linalg.solve_triangular` (SciPy-compatible) [#3200](https://github.com/IntelPython/dpnp/pull/3200) | ||
| * Added implementation of `dpnp.scipy.special.ndtri` [#3219](https://github.com/IntelPython/dpnp/pull/3219) |
There was a problem hiding this comment.
Uh oh!
There was an error while loading. Please reload this page.