Skip to main content

numba_fail.py failed because the callee used a NumPy function outside Numba’s supported subset — the what was disallowed. JAX’s failure mode is different in kind: jax.jit doesn’t type-check your function body against a support table. It traces it, once, with abstract stand-ins for the arguments, and records the array operations that fire. Anything in the body that isn’t an array operation — an if branching on a value, a print — still runs, but runs once, at trace time, over those abstract stand-ins. So the failures below aren’t “unsupported function”; they’re “this line needed a concrete Python value, and traced code doesn’t have one”.

import jax
import jax.numpy as jnp

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

Control flow on a traced value

Ordinary Python, the kind that works fine eagerly:

def relu(x):
    if x > 0:
        return x
    else:
        return 0.0


relu(3.0), relu(-3.0)
(3.0, 0.0)
try:
    jax.jit(relu)(3.0)
except Exception as e:
    print(f"{type(e).__name__}: {e}")
TracerBoolConversionError: Attempted boolean conversion of traced array with shape bool[].
The error occurred while tracing the function relu at /tmp/ipykernel_675324/3348139164.py:1 for jit. This concrete value was not available in Python because it depends on the value of the argument x.
See https://docs.jax.dev/en/latest/errors.html#jax.errors.TracerBoolConversionError

if x > 0 calls bool() on x. Eagerly, x is a concrete float and bool() just works. Under jit, x arrives as a tracer — an abstract placeholder standing in for “some float, shape (), to be supplied later” — and a tracer has no truth value to give, because tracing happens once, before any real number is known. This is python_as_metaprogramming_lang.ipynb’s point made concrete: the if is host-language control flow, deciding which trace to record, and the host language needs the answer now, at trace time — but x won’t have an answer until the compiled program actually runs.

Two different fixes, because there are two different things you might have meant:

Data, not control flow. Compute both branches and select elementwise — this is what the function almost certainly meant, and it’s now a value the tracer can happily push straight through into the compiled program:

def relu_traceable(x):
    return jnp.where(x > 0, x, 0.0)


relu_jax = jax.jit(relu_traceable)
relu_jax(3.0), relu_jax(-3.0)
An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.
(Array(3., dtype=float64, weak_type=True),
 Array(0., dtype=float64, weak_type=True))

Genuine host-language branching. Sometimes the branch really is about which trace to record, not about the data — e.g. an axis argument, or a boolean mode flag chosen once by the caller, not looped over at runtime. Tell jit to keep that one argument as an ordinary Python value — never abstracted, never turned into a tracer — via static_argnames:

def scale(x, double):
    if double:
        return 2.0 * x
    return x


scale_jax = jax.jit(scale, static_argnames="double")
scale_jax(3.0, True), scale_jax(3.0, False)
(Array(6., dtype=float64, weak_type=True),
 Array(3., dtype=float64, weak_type=True))

jax.make_jaxpr shows what “kept static” bought: with double fixed at trace time, the if ran as plain Python and only the branch actually taken got recorded — one jaxpr with a mul, one with no arithmetic at all:

print(jax.make_jaxpr(scale, static_argnums=1)(3.0, True))
print(jax.make_jaxpr(scale, static_argnums=1)(3.0, False))
{ lambda ; a:f64[]. let b:f64[] = mul 2.0:f64[] a in (b,) }
{ lambda ; a:f64[]. let  in (a,) }

The cost mirrors shape-based recompilation from intro_jax.ipynb: every distinct static value is its own trace and its own compile, cached separately. Fine for a flag with two settings passed from Python; a trap if the “static” argument is secretly a per-call loop counter.

NumPy duck-typing that isn’t

jnp.ndarray mimics np.ndarray’s API closely enough to fool the eye, but JAX arrays are immutable — a functional-programming requirement, not an oversight, since tracing and automatic differentiation both need every operation to produce a new value rather than mutate one in place. The NumPy idiom for “set one element” doesn’t survive that:

x = jnp.arange(5.0)
try:
    x[0] = 10.0
except Exception as e:
    print(f"{type(e).__name__}: {e}")
TypeError: JAX arrays are immutable and do not support in-place item assignment. Instead of x[idx] = y, use x = x.at[idx].set(y) or another .at[] method: https://docs.jax.dev/en/latest/_autosummary/jax.numpy.ndarray.at.html

No trace needed to hit this — plain eager JAX already refuses it, which is the tell that it’s not a jit-specific restriction but a property of the array type itself. The functional replacement builds a new array via .at[idx].set(value), mirroring the in-place spelling closely enough to be a mechanical rewrite once you know to look for it:

x.at[0].set(10.0), x  # returns a new array; x itself is untouched
(Array([10.,  1.,  2.,  3.,  4.], dtype=float64),
 Array([0., 1., 2., 3., 4.], dtype=float64))