jax-ml/jax · error · ValueError
The input must be non-scalar to take a cumulative product…
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
cumulative_prod scans along an axis of the input; a scalar (0-dimensional) input has no axis to accumulate over. It fires when x is scalar and no axis can be inferred.
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…
- `a` array must be integer typed
- After moving axes to end, leading shape of a must match…
- Arguments to jax.numpy.gcd must be integers.
- Arguments to jax.numpy.lcm must be integers.
AI-assisted analysis of jax-ml/jax@1e1c6a8fc0 (2026-08-27).
Data as JSON: /api/errors/b03f3806ba73f98e.
Report an issue: GitHub.
Appendix: 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)