jax-ml/jax · error · ValueError

reduce only supported for binary ufuncs

Error message

reduce only supported for binary ufuncs

What it means

ufunc.reduce (e.g. jnp.add.reduce) generalizes reduction over a binary operation, so it requires a ufunc with exactly two inputs (nin == 2). Calling .reduce on a unary ufunc such as jnp.negative raises this ValueError.

Source

Thrown at jax/_src/numpy/ufunc_api.py:243

      Similarly, :meth:`jax.numpy.logical_and.reduce` is equivalent to
      :func:`jax.numpy.all`:

      >>> jnp.logical_and.reduce(x > 2)
      Array([False, False,  True], dtype=bool)
      >>> jnp.all(x > 2, axis=0)
      Array([False, False,  True], dtype=bool)

      Some reductions do not correspond to any built-in aggregation function;
      for example here is the reduction of :func:`jax.numpy.bitwise_or` along
      the first axis of ``x``:

      >>> jnp.bitwise_or.reduce(x, axis=1)
      Array([3, 7], dtype=int32)
    """
    check_arraylike(f"{self.__name__}.reduce", a)
    if self.nin != 2:
      raise ValueError("reduce only supported for binary ufuncs")
    if self.nout != 1:
      raise ValueError("reduce only supported for functions returning a single value")
    if out is not None:
      raise NotImplementedError(f"out argument of {self.__name__}.reduce()")
    if initial is not None:
      check_arraylike(f"{self.__name__}.reduce", initial)
    if where is not None:
      check_arraylike(f"{self.__name__}.reduce", where)
      if self.identity is None and initial is None:
        raise ValueError(f"reduction operation {self.__name__!r} does not have an identity, "
                         "so to use a where mask one has to specify 'initial'.")
      if lax._dtype(where) != bool:
        raise ValueError(f"where argument must have dtype=bool; got dtype={lax._dtype(where)}")
    reduce = self.__static_props['reduce'] or self._reduce_via_scan
    return reduce(a, axis=axis, dtype=dtype, keepdims=keepdims, initial=initial, where=where)

  def _reduce_via_scan(self, arr: ArrayLike, axis: int | tuple[int, ...] | None = 0, dtype: DTypeLike | None = None,
                       keepdims: bool = False, initial: ArrayLike | None = None,

View on GitHub (pinned to 1e1c6a8fc0)

Solutions

  1. Only call .reduce on binary ufuncs (add, multiply, bitwise_or, maximum, ...)
  2. For unary or non-reducible ops, express the reduction differently (e.g. use jnp.sum, jnp.prod)

Example fix

// before
jnp.negative.reduce(x)
// after
-jnp.sum(x)
Defensive patterns

Strategy: validation

Validate before calling

assert ufunc.nin == 2, f'{ufunc.__name__} is not binary'

Type guard

def is_binary_ufunc(u): return u.nin == 2

Prevention

When it happens

Trigger: jnp.negative.reduce(x) or any unary/generic ufunc's .reduce method.

Common situations: Dynamic code that calls .reduce on an arbitrary ufunc object; assuming all numpy ufuncs support reduce.

Related errors


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