jax-ml/jax · error · ValueError

The two inputs to `nextafter` must have the same dtype, but

Error message

The two inputs to `nextafter` must have the same dtype, but got {x.dtype} and {y.dtype}

What it means

pallas.utils.nextafter_lowering_helper requires both inputs to have identical dtypes; the bitwise next-representable-float algorithm is dtype-specific. Mismatched dtypes (float32 vs float64, or int vs float) raise ValueError.

Source

Thrown at jax/_src/pallas/utils.py:369

def sign_lowering_helper(x):
  if jnp.issubdtype(x.dtype, jnp.unsignedinteger):
    return (x != 0).astype(x.dtype)

  if jnp.issubdtype(x.dtype, jnp.integer):
    return (x > 0).astype(x.dtype) - (x < 0).astype(x.dtype)

  if jnp.issubdtype(x.dtype, jnp.floating):
    out = (x > 0.).astype(x.dtype) - (x < 0.).astype(x.dtype)
    return jnp.where(jnp.isnan(x), jnp.nan, out)

  raise NotImplementedError(f"sign_lowering_helper not implemented for {x.dtype}")


# based on https://github.com/openxla/xla/blob/a7a09d56c3599123f8148bbf3e44c9ebc04624b9/xla/mlir_hlo/mhlo/transforms/chlo_legalize_to_hlo/chlo_legalize_to_hlo.cc#L1339-L1422
def nextafter_lowering_helper(x, y):
  if x.dtype != y.dtype:
    raise ValueError(
        "The two inputs to `nextafter` must have the same dtype, but got"
        f" {x.dtype} and {y.dtype}"
    )

  if x.dtype not in (jnp.float32, jnp.float64):
    raise ValueError(
        f"`nextafter` only supports float32 and float64, but got {x.dtype}"
    )

  jnp_float, jnp_uint, np_float, np_uint, np_int = (
      jnp.float32, jnp.uint32, np.float32, np.uint32, np.int32,
  ) if x.dtype == jnp.float32 else (
      jnp.float64, jnp.uint64, np.float64, np.uint64, np.int64,
  )

  bitwidth = dtypes.itemsize_bits(x.dtype)

  x_as_int = x.view(jnp_uint)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Promote both to a common dtype: x, y = jax.lax.convert_element_type to same float type
  2. Ensure both operands originate from tensors of the same dtype

Example fix

# before
jnp.nextafter(x_f32, y_f64)
# after
jnp.nextafter(x_f32, y_f64.astype(jnp.float32))
Defensive patterns

Strategy: type-guard

Validate before calling

y = y.astype(x.dtype)

Type guard

def same_dtype(x, y): return x.dtype == y.dtype

Prevention

When it happens

Trigger: Calling jnp.nextafter(x_f32, y_f64) or with an integer operand inside a pallas triton kernel; mixing Python scalars that promote unexpectedly.

Common situations: Mixed-precision kernels; operands from different tensors where one was upcast earlier in the pipeline.

Related errors


AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27). Data as JSON: /api/errors/9e5103b24c2b7487. Report an issue: GitHub.