TPU: jnp.where(i > 0, ...) on a scan counter that starts at 0 takes the true branch at i == 0
Author: AshishKumar4Created Sep 28, 2026Updated Sep 28, 2026
LabelsbugXLA
Description
On TPU, a jnp.where gated on i > 0, where i is a lax.scan carry that starts at a constant 0, takes the true branch at i == 0:
import jax
import jax.numpy as jnp
@jax.jit
def gt_zero(x):
def body(carry, _):
i, x = carry
x = jnp.where(i > 0, x + 1.0, x)
return (i + 1, x), x
return jax.lax.scan(body, (jnp.int32(0), x), None, length=3)[1]
print(gt_zero(jnp.float32(0)))
print(jax.jit(gt_zero.__wrapped__, backend="cpu")(jnp.float32(0)))On a TPU v6e:
[1. 2. 3.]
[0. 1. 2.]The first line should be [0. 1. 2.], as on CPU. The result is also wrong with lax.cond(i > 0, ...) in place of jnp.where, and correct when the counter's start is passed in as an argument instead of a constant.
System info (python version, jaxlib version, accelerator, etc.)
jax: 0.11.2
jaxlib: 0.11.2
libtpu: 0.0.48
numpy: 2.5.3
python: 3.12.3
device info: TPU v6 lite-1, 1 local devices
process_count: 1Source: jax-ml/jax