jax.experimental.jet: Bugs for zero-arg primitives (e.g. iota) and in series scaling for discrete primitives (e.g. clz, ...)
Author: paulgekelerCreated Sep 28, 2026Updated Sep 28, 2026
Labelsbug
Description
Hi all,
While implementing additional primitive rules for jet, I encountered some issues when dealing with certain primitives:
jetcan't handle primitives with zeroprimalsandseriesarguments:iota()primitive takes no arguments forprimalsandseries. Thejetcall then fails withValueError: "jet terms have inconsistent lengths for different arguments":
def f():
return lax.iota(jnp.int32, size=5)
primal_out, series_out = jet(f, (), ())- Dtype casting error in factorial scaling for non-inexact arrays (occur in e.g.
argmax,top_kindex outputs orclz/population_countpure integer ops) under strict dtype promotion: The series terms are scaled by factorials iffactorial_scaled = True. When a primitive returns int/bool outputs (like theint32indices fromapprox_top_k) or uses them as inputs (e.g.clz), attempting scalar multiplication raises a JAX dtype casting error if dtype promotion is strict. Or it succeeds and silently corrupts the output dtype (returns a weakly typed float).
Minimal reproducible examples:
(To reproduce iota zero-arg bug see above)
To reproduce dtype casting:
import jax
from jax import lax
import jax.numpy as jnp
from jax.experimental.jet import jet
x = jnp.array([True, False, True])
terms = [jnp.array([False, True, False])]
with jax.numpy_dtype_promotion('strict'):
jet(lax.bitwise_not, (x,), (terms,))The input scaling crash can also be reproduced by jetting any function taking in int primals and series under strict dtype promotion. For output scaling reproduction
import jax
from jax import lax
import jax.numpy as jnp
from jax.experimental.jet import jet
x = jnp.array([1.0, 2.0, 3.0])
y = jnp.array([0.5, 2.5, 1.5])
terms_x = [jnp.array([0.1, 0.1, 0.1])]
terms_y = [jnp.array([0.1, 0.1, 0.1])]
with jax.numpy_dtype_promotion('strict'):
jet(lax.lt, (x, y), (terms_x, terms_y))Note, that the reproduction of the type casting silently succeeds under jax default standard dtype promotion.
Proposed Solutions:
- Default to
order = 0in caseseriesis empty. Theall(t is zero_series for t in series_in)check inprocess_primitivealready handles this correctly.
try:
(order,) = set(map(len, series))
except ValueError:
if not series:
order = 0
else:
msg = "jet terms have inconsistent lengths for different arguments"
raise ValueError(msg) from None- Prevent
seriesfactorial scaling for non-inexact dtypes injet: Ensure term scaling only applies to leaves wherejnp.issubdtype(jnp.result_type(primals), jnp.inexact)isTrue. Non-inexact leaves should leave their series unscaled (or as zero_series).
import operator
...
# Input scaling
if factorial_scaled:
series = [
scale_leaf(p, s, operator.truediv) for p, s in zip(primals, series)
]
...
# Output scaling
if factorial_scaled:
out_terms = [scale_leaf(p, s) for p, s in zip(out_primals, out_terms)]
...
# Define fun for conditional scaling
def scale_leaf(primals, series, op=operator.mul):
if series is zero_series or not jnp.issubdtype(
jnp.result_type(primals), jnp.inexact
):
return series
return [op(t, fact(order + 1)) for order, t in enumerate(series)]I'd be happy to write a PR for this along with the other added primitive rules:
le_to_plt_to_peq_to_pargmin_pargmax_pclz_ppopulation_count_pmulhi_preduce_and_preduce_or_preduce_xor_pnextafter_pcbrt_ptan_pasin_pacos_patan_pscatter_p
They are mostly zero propagation, linear or basic composition primitives.
System info (python version, jaxlib version, accelerator, etc.)
jax: 0.12.0.dev20260924+75e6d85d61
jaxlib: 0.11.2
numpy: 2.5.3
python: 3.12.12 (main, Oct 9 2025, 11:07:00) [Clang 17.0.0 (clang-1700.3.19.1)]
device info: cpu-1, 1 local devices"
process_count: 1
platform: uname_result(system='Darwin', node='Mac', release='27.0.0', version='Darwin Kernel Version 27.0.0: Tue Aug 11 21:20:40 PDT 2026; root:xnu-13432.1.9~1/RELEASE_ARM64_T6020', machine='arm64')Source: jax-ml/jax