jax icon indicating copy to clipboard operation
jax copied to clipboard

`jax.jit` returns `0.0` for `log(power(-1.0, 0.5))` while eager returns `nan`

Open CaelusHu opened this issue 1 month ago • 2 comments

Description

On CPU, jax.jit changes the result of log(power(x, 0.5)) for a negative real input from nan to a finite value.

import jax
import jax.numpy as jnp

jax.config.update("jax_enable_x64", True)

x = jnp.array(-1.0, dtype=jnp.float64)
f = lambda x: jnp.log(jnp.power(x, 0.5))

print(f(x))
print(jax.jit(f)(x))
print(jnp.power(x, 0.5))
print(jax.jit(lambda x: jnp.power(x, 0.5))(x))

Output:

nan
0.0
nan
nan

I would expect the jitted result to remain nan. The issue also reproduces with x ** 0.5 and jax.lax.pow(x, 0.5).

Disabling the XLA algebraic simplifier makes eager and jitted execution agree:

XLA_FLAGS=--xla_disable_hlo_passes=algsimp python repro.py
# eager=nan, jit=nan

This suggests that the simplifier is applying the equivalent of log(power(A, B)) -> B * log(abs(A)), which is not valid for a negative real base and a fractional exponent. The behavior is present in both JAX 0.10.2 and 0.11.2.

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

jax:      0.11.2
jaxlib:   0.11.2
numpy:    2.5.3
python:   3.13.7
platform: macOS 26.6.2 arm64
device:   CpuDevice(id=0)

CaelusHu avatar Oct 04 '26 09:10 CaelusHu

Looking at the compiled HLO, JAX lowers this cleanly as stablehlo.power followed by stablehlo.log, but XLA's algsimp fuses it into multiply(log(abs(x)), constant(0.5)):

%abs.1 = f64[] abs(%param_0.3)
%log.2 = f64[] log(%abs.1)
ROOT %multiply.1 = f64[] multiply(%log.2, %constant.4)

Interestingly this only triggers for non-integer exponents (tested with 0.25, 0.5, 1.5, etc.), whereas integer exponents like 1.0 and 3.0 correctly preserve nan. Because this rewrite happens in XLA's algebraic simplifier rather than JAX frontend lowering, this will need to be fixed upstream in openxla/xla.

hrideymarwah15 avatar Oct 15 '26 21:10 hrideymarwah15

Fixed by https://github.com/openxla/xla/pull/51029

jakevdp avatar Oct 19 '26 22:10 jakevdp