jax-ml/jax · error · ValueError
The input must be non-scalar to take a cumulative product, h
Error message
The input must be non-scalar to take a cumulative product, however a scalar value or scalar array was given.
What it means
jnp.cumulative_prod requires a non-scalar input; a 0-d array or scalar has no axis to accumulate over, so JAX raises ValueError before canonicalizing the axis.
Source
Thrown at jax/_src/numpy/reductions.py:2363
See Also:
- :func:`jax.numpy.cumprod`: alternative API for cumulative product.
- :func:`jax.numpy.nancumprod`: cumulative product while ignoring NaN values.
- :func:`jax.numpy.multiply.accumulate`: cumulative product via the ufunc API.
Examples:
>>> x = jnp.array([[1, 2, 3],
... [4, 5, 6]])
>>> jnp.cumulative_prod(x, axis=1)
Array([[ 1, 2, 6],
[ 4, 20, 120]], dtype=int32)
>>> jnp.cumulative_prod(x, axis=1, include_initial=True)
Array([[ 1, 1, 2, 6],
[ 1, 4, 20, 120]], dtype=int32)
"""
x = ensure_arraylike("cumulative_prod", x)
if x.ndim == 0:
raise ValueError(
"The input must be non-scalar to take a cumulative product, however a "
"scalar value or scalar array was given."
)
if axis is None:
axis = 0
if x.ndim > 1:
raise ValueError(
f"The input array has rank {x.ndim}, however axis was not set to an "
"explicit value. The axis argument is only optional for one-dimensional "
"arrays.")
axis = canonicalize_axis(axis, x.ndim)
if dtype is not None:
dtype = dtypes.check_and_canonicalize_user_dtype(dtype)
out = _cumulative_reduction("cumulative_prod", control_flow.cumprod, x, axis, dtype)
if include_initial:
zeros_shape = list(x.shape)
zeros_shape[axis] = 1View on GitHub (pinned to 1e1c6a8fc0)
Solutions
- Reshape to at least 1-d: jnp.cumulative_prod(x[None])
- Guard with an ndim check in generic accumulate helpers
Example fix
// before jnp.cumulative_prod(jnp.prod(x)) // after jnp.cumulative_prod(jnp.prod(x, keepdims=True))
Defensive patterns
Strategy: validation
Validate before calling
import jax.numpy as jnp
def cumulative_prod_safe(x, **kw):
if jnp.ndim(x) == 0:
x = jnp.reshape(x, (1,))
return jnp.cumulative_prod(x, **kw) Type guard
def is_nonscalar(x) -> bool:
return jnp.asarray(x).ndim > 0 Prevention
- Guard ndim in generic accumulate utilities
- Avoid feeding scalar reduction outputs into cumulative ops
When it happens
Trigger: Calling jnp.cumulative_prod(jnp.asarray(5)) or with any x.ndim == 0 input.
Common situations: Chaining cumulative products after full reductions; per-element loops feeding Python scalars into jnp functions.
Related errors
- The input must be non-scalar to take a cumulative sum, howev
- len() of unsized object
- numpy masked arrays are not supported as direct inputs to JA
- Unsupported scalar attribute type: {type(val)}
- NumPy arrays with zero strides are not supported as MLIR att
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/b03f3806ba73f98e.
Report an issue: GitHub.