jax-ml/jax · error · NotImplementedError

In order to best JIT compile `rankdata`, we cannot know whet

Error message

In order to best JIT compile `rankdata`, we cannot know whether `x` contains nans. Please check if nans exist in `x` outside of the `rankdata` function.

What it means

jax.scipy.stats.rankdata with nan_policy='raise' raises NotImplementedError because NaN detection is data-dependent and cannot be JIT-traced; users must check for NaNs outside the function.

Source

Thrown at jax/_src/scipy/stats/_core.py:190

    Array([1., 3., 2.], dtype=float32)

    >>> x = jnp.array([1, 3, 2, 3])
    >>> rankdata(x)
    Array([1. , 3.5, 2. , 3.5], dtype=float32)
  """
  check_arraylike("rankdata", a)

  if nan_policy not in ["propagate", "omit", "raise"]:
    raise ValueError(
      f"Illegal nan_policy value {nan_policy!r}; expected one of "
      "{'propagate', 'omit', 'raise'}"
    )
  if nan_policy == "omit":
    raise NotImplementedError(
      f"Logic for `nan_policy` of {nan_policy} is not implemented"
    )
  if nan_policy == "raise":
    raise NotImplementedError(
      "In order to best JIT compile `rankdata`, we cannot know whether `x` "
      "contains nans. Please check if nans exist in `x` outside of the "
      "`rankdata` function."
    )

  if method not in ("average", "min", "max", "dense", "ordinal"):
    raise ValueError(f"unknown method '{method}'")

  if axis is not None:
    return jnp.apply_along_axis(rankdata, axis, a, method)

  a = jnp.ravel(a)
  out_dtype = dtypes.default_float_dtype()

  def _rankdata(a: Array) -> Array:
    arr, sorter = lax.sort_key_val(a, jnp.arange(a.size))
    inv = invert_permutation(sorter)

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Check for NaNs before calling: if jnp.isnan(x).any(): raise ...; then use nan_policy='propagate' or omit the kwarg.
  2. Move the NaN check to the data-preparation stage of the pipeline.

Example fix

// before
jax.scipy.stats.rankdata(x, nan_policy='raise')

// after
assert not jnp.isnan(x).any(), "x contains NaNs"
jax.scipy.stats.rankdata(x)  # default: propagate
Defensive patterns

Strategy: validation

Validate before calling

if jnp.isnan(x).any():
    raise ValueError("x contains NaNs")
jax.scipy.stats.rankdata(x)

Prevention

When it happens

Trigger: Calling jax.scipy.stats.rankdata(a, nan_policy='raise') — unconditionally raises, NaNs present or not.

Common situations: Wanting fail-fast NaN semantics inside jitted pipelines; ported scipy code with 'raise' policy.

Related errors


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