warp icon indicating copy to clipboard operation
warp copied to clipboard

wp.erfinv() and wp.erfcinv() lose precision on CPU: erfinv(1e-20) returns 0, erfcinv(1e-20) returns inf

Open usmanmateen opened this issue 1 month ago • 1 comments

Bug Description

On CPU, wp.erfinv() and wp.erfcinv() lose precision for small arguments. erfinv(1e-20) returns 0.0 and erfcinv(1e-20) returns inf, while the same kernels on CUDA return the values SciPy gives. I found this by running the same kernels on the CPU and CUDA devices and comparing the results.

import numpy as np
import warp as wp


@wp.kernel
def erfinv_kernel(x: wp.array(dtype=wp.float64), out: wp.array(dtype=wp.float64)):
    i = wp.tid()
    out[i] = wp.erfinv(x[i])


@wp.kernel
def erfcinv_kernel(x: wp.array(dtype=wp.float64), out: wp.array(dtype=wp.float64)):
    i = wp.tid()
    out[i] = wp.erfcinv(x[i])


cases = [
    (erfinv_kernel, "erfinv ", [1e-20, -1e-20, 1e-7, -1e-7]),
    (erfcinv_kernel, "erfcinv", [1e-20, 1e-17, 3e-16, 1e-7]),
]
for kernel, name, values in cases:
    for device in ["cpu", "cuda:0"]:
        x = wp.array(values, dtype=wp.float64, device=device)
        out = wp.empty_like(x)
        wp.launch(kernel, dim=len(values), inputs=[x], outputs=[out], device=device)
        print(f"{name} {device:6}", out.numpy().tolist())

Output with Warp 1.18.0:

erfinv  cpu    [0.0, 0.0, 8.862269259701992e-08, -8.862269249862895e-08]
erfinv  cuda:0 [8.86226925452758e-21, -8.86226925452758e-21, 8.862269254527604e-08, -8.862269254527604e-08]
erfcinv cpu    [inf, inf, 5.745872392191181, 3.7665625817811255]
erfcinv cuda:0 [6.601580622355144, 6.062693998163569, 5.779414783219449, 3.766562581570838]

scipy.special.erfinv gives [8.86226925452758e-21, -8.86226925452758e-21, 8.862269254527604e-08, -8.862269254527604e-08] and scipy.special.erfcinv gives [6.601580622355143, 6.062693998163569, 5.779414783219451, 3.766562581570838]. On CPU, erfinv(1e-7) is also not the negative of erfinv(-1e-7).

Over 60,000 float64 arguments (uniform, log-spaced down to 1e-300, and close to the ends of the domain), compared with SciPy:

CPU CUDA
erfinv 24,829 more than 4 ulp off; 0.0 for 18,951 nonzero results; erfinv(-x) != -erfinv(x) for 10,370 at most 5 ulp
erfcinv 19,935 more than 4 ulp off; inf for 19,002 finite results at most 6 ulp

float32 shows the same pattern on CPU (up to 1.7e7 ulp for erfinv, inf for 1,965 erfcinv arguments), and at most 2.6 ulp on CUDA.

The CPU versions are in warp/native/crt.h. erfinv(z) returns ndtri((z + 1.0) / 2.0) / sqrt(2.0): for small z, z + 1.0 rounds away the low bits of z, or all of it, so ndtri gets 0.5 and returns 0. erfcinv(x) returns erfinv(1.0 - x), and 1.0 - x is exactly 1.0 once x is below about 1.1e-16, which gives inf.

Both can pass exact arguments to the Cephes code instead: erfcinv(x) = -ndtri(x / 2) / sqrt(2), and for erfinv, t = z / 2 in the central rational approximation and y = (1 - |z|) / 2 in the tails. With that, the CPU results above are within 6 ulp of SciPy for float64 and 0.5 ulp for float32, and erfinv is odd. I can open a PR with this change and tests.

System Information

  • Warp 1.18.0 (pip); crt.h is the same on main (bfd59c41d)
  • Ubuntu 26.04 (WSL2), x86_64, Python 3.12
  • NVIDIA GeForce RTX 5060 Ti, driver 610.88, CUDA 13

usmanmateen avatar Oct 16 '26 22:10 usmanmateen

This is fixed on main by 84d1bcc (GH-2201). With main, erfinv(1e-20) and erfcinv(1e-20) give the right values on CPU, along with the other cases from this issue, so this can be closed. I closed my PR #2103 since it duplicated that fix.

usmanmateen avatar Nov 18 '26 12:11 usmanmateen