#41041·jax

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:

  1. jet can't handle primitives with zero primals and series arguments: iota() primitive takes no arguments for primals and series. The jet call then fails with ValueError: "jet terms have inconsistent lengths for different arguments":
python
def f():
  return lax.iota(jnp.int32, size=5)

primal_out, series_out = jet(f, (), ())
  1. Dtype casting error in factorial scaling for non-inexact arrays (occur in e.g. argmax, top_k index outputs or clz/population_count pure integer ops) under strict dtype promotion: The series terms are scaled by factorials if factorial_scaled = True. When a primitive returns int/bool outputs (like the int32 indices from approx_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:

python
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

python
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:

  1. Default to order = 0 in case series is empty. The all(t is zero_series for t in series_in) check in process_primitive already handles this correctly.
python
  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
  1. Prevent series factorial scaling for non-inexact dtypes in jet: Ensure term scaling only applies to leaves where jnp.issubdtype(jnp.result_type(primals), jnp.inexact) is True. Non-inexact leaves should leave their series unscaled (or as zero_series).
python
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_p
  • lt_to_p
  • eq_to_p
  • argmin_p
  • argmax_p
  • clz_p
  • population_count_p
  • mulhi_p
  • reduce_and_p
  • reduce_or_p
  • reduce_xor_p
  • nextafter_p
  • cbrt_p
  • tan_p
  • asin_p
  • acos_p
  • atan_p
  • scatter_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')