jax icon indicating copy to clipboard operation
jax copied to clipboard

jax.jit incorrectly returns 'nan' for jnp.cross(x, x) / jnp.cross(x, x) where eager returns 1.0

Open anna-researcher opened this issue 5 months ago • 1 comments

Description

jax.jit produces nan for a function that divides jnp.cross(x, x) by itself, while eager mode correctly returns 1.0. This is a compiler correctness bug — eager and JIT disagree on the numerical result.

The root cause appears to be an XLA optimization that recognizes jnp.cross(x, x) as a mathematically zero expression (since x × x = 0 in exact arithmetic) and substitutes 0.0, causing 0.0 / 0.0 = nan. However, jnp.cross(x, x) in float32 produces a near-zero but non-zero value due to floating point arithmetic (e.g. 2.83e-09), so the division should yield 1.0.

Minimal Reproducer

import jax
import jax.numpy as jnp

t = jnp.array([[0.1, 0.9]], dtype=jnp.float32)

def f(x):
    c = jnp.cross(x, x)
    return c / c

print(f(t))            # eager:  [1.]   ✓ correct
print(jax.jit(f)(t))  # jit:    [nan]  ✗ wrong

Expected vs Actual Output

eager:  [1.]
jit:    [nan]

jnp.cross(x, x) produces [2.83e-09] in float32 (not exactly zero due to floating point), so c / c should return [1.]. Under jax.jit, XLA appears to fold cross(x, x) to 0.0 symbolically and then compute 0.0 / 0.0 = nan.

System info (python version, jaxlib version, accelerator, etc.)

jax:    0.10.1
jaxlib: 0.10.1
numpy:  2.4.6
python: 3.12.13 | packaged by Anaconda, Inc. | (main, Mar 19 2026, 20:20:58) [GCC 14.3.0]
device info: cpu-1, 1 local devices
process_count: 1
platform: uname_result(system='Linux', node='unicorn-login-01', release='6.8.0-110-generic', version='#110-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 19 15:09:20 UTC 2026', machine='x86_64')
JAX_PLATFORMS=cpu

anna-researcher avatar Jun 19 '26 23:06 anna-researcher

Hi @anna-researcher

Thanks for the detailed issue report and the exact reproduction snippet.

Root Cause

During compilation under jax.jit, JAX lowering rules and XLA algebraic simplification passes identify jnp.cross(x, x) as a mathematically zero expression ($x \times x = 0$ in exact symbolic math) and fold the intermediate sub-graph to exact 0.0. In floating-point arithmetic, eager evaluation of jnp.cross(x, x) produces a non-zero residual (2.83e-09 in FP32), allowing c / c to return 1.0. Under jax.jit, substituting exact 0.0 causes the division c / c to evaluate 0.0 / 0.0, producing nan.

Proposed Solution

  1. In jax/_src/lax/lax.py (and JAX cross lowering rules), guard self-cross product transformations to prevent unsafe symbolic zero-folding during compilation when division or reciprocal ops consume the intermediate result.
  2. Ensure algebraic simplification passes preserve strict floating-point evaluation rules for self-referential array divisions.
  3. Add a regression unit test in tests/lax_numpy_test.py asserting that jax.jit returns 1.0 for jnp.cross(x, x) / jnp.cross(x, x) across CPU and GPU backends.

Would you like me to open a Pull Request with this compiler simplification fix and accompanying regression tests?

Saad-Mallebhari avatar Aug 06 '26 17:08 Saad-Mallebhari