jax
4bbb23df - Improve erf_inv accuracy near |x| = 1.

Commit
46 days ago
Improve erf_inv accuracy near |x| = 1. This change is gated behind a temporary flag (jax_accurate_erf_inv), but the intention is to flip it on by default. erf_inv computed w = -log1p(-x * x). Rounding x * x before the log1p loses roughly half the bits of 1 - x^2 when |x| is close to 1, which gave errors of ~65 ULP in f32 and ~500000 ULP in f64. Use w = -log((1 - x) * (1 + x)) instead: 1 - |x| is exact for |x| >= 0.5, so the product has no cancellation error. For small |x| the polynomial only depends on the absolute error of w. This also replaces a log1p with a log, which is cheaper on TPU. This matches the evaluation in Giles' reference CUDA code ("Approximating the erfinv function", Table 5). JAX now lowers erf_inv itself rather than emitting chlo.erf_inv. The same fix is applied to the Pallas lowering. Max f32 error over all 2^32 inputs: CPU, GPU, TPU v6e, TPU 7x: 65.5 -> 3.76 ULP TPU v5p: 35 -> 6.35 ULP TPU v3/v4/v5e: unchanged (427 ULP) f64 on CPU/GPU (sampled): ~500000 -> 2.32 ULP. PiperOrigin-RevId: 995141067
Author
Parents
Loading