jax icon indicating copy to clipboard operation
jax copied to clipboard

jax.jit returns nan for jnp.angle(jnp.logical_xor(t, t)) where eager returns 0.0

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

Description

jax.jit produces nan for jnp.angle(jnp.logical_xor(t, t)) while eager mode correctly returns 0.0. This is a JIT compiler correctness bug — XLA appears to mishandle the type conversion inside jnp.angle when its input is derived from jnp.logical_xor(t, t) under JIT.

Note: jnp.angle of a directly-created boolean zero array works correctly under JIT — the bug is specific to the combination with jnp.logical_xor.

Minimal Reproducer

import jax
import jax.numpy as jnp

t = jnp.array([1, 2, 3, 4], dtype=jnp.int32)

def f(t):
    return jnp.angle(jnp.logical_xor(t, t))

print(f(t))            # eager: [0. 0. 0. 0.]
print(jax.jit(f)(t))   # jit:   [nan nan nan nan]

# Note: angle of explicit bool zeros works fine under JIT
t2 = jnp.zeros((4,), dtype=jnp.bool_)
print(jax.jit(jnp.angle)(t2))  # [0. 0. 0. 0.] — correct

Expected vs Actual

eager:              [0. 0. 0. 0.]   ✓ correct
jit:                [nan nan nan nan] ✗ wrong
jit(angle(zeros)):  [0. 0. 0. 0.]   ✓ correct (bug is specific to logical_xor combination)

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 23 '26 04:06 anna-researcher

Thanks for the report; I reduced the issue to this sequence of direct XLA operations. The second print statement is all NaN on CPU and GPU, but correctly prints zeros on TPU:

import jax
import jax.numpy as jnp

def f(a):
  b = jax.lax.ne(a, 0)
  c = jax.lax.ne(a, 0)
  d = jax.lax.bitwise_xor(b, c)
  e = jax.lax.convert_element_type(d, 'float32')
  return jax.lax.atan2(e, e)

x = jnp.arange(5)
print(f(x))  # [0, 0, 0, 0, 0]
print(jax.jit(f)(x))  # [nan nan nan nan nan]

This looks like an issue with the XLA compiler; it seems to be using some incorrect optimization.

Interestingly, if you just c = b rather than recomputing ne(a, 0), the issue goes away. Looking at the compiled HLO, it appears the compiler recognizes that the xor result is a constant zero for that case.

jakevdp avatar Jun 23 '26 19:06 jakevdp

Reported upstream in https://github.com/openxla/xla/issues/44705

jakevdp avatar Jun 23 '26 20:06 jakevdp

@jakevdp In case this is useful, found an additional reproducer: The same atan2(0,0) NaN-under-JIT bug also affects jnp.polysub:

`import jax import jax.numpy as jnp

t = jnp.array([1, 2, 3], dtype=jnp.int32)

def model(t): return jnp.angle(jnp.polysub(t, t)) # polysub(x,x) = 0, angle(0) should be 0

print(model(t)) # [0. 0. 0.] print(jax.jit(model)(t)) # [nan nan nan]`

This confirms the root cause is in XLA's atan2 fusion when operating on zero inputs, not specific to logical_xor.

anna-researcher avatar Jun 29 '26 00:06 anna-researcher

This appears to be fixed as of the most recent JAX release. Thanks for the report!

jakevdp avatar Nov 10 '26 19:11 jakevdp