{"record":{"id":"b03f3806ba73f98e","repo":"jax-ml/jax","slug":"the-input-must-be-non-scalar-to-take-a-cumulative-b03f38","errorCode":null,"errorMessage":"The input must be non-scalar to take a cumulative product, however a scalar value or scalar array was given.","messagePattern":"The input must be non-scalar to take a cumulative product, however a scalar value or scalar array was given\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/reductions.py","lineNumber":2363,"sourceCode":"\n  See Also:\n    - :func:`jax.numpy.cumprod`: alternative API for cumulative product.\n    - :func:`jax.numpy.nancumprod`: cumulative product while ignoring NaN values.\n    - :func:`jax.numpy.multiply.accumulate`: cumulative product via the ufunc API.\n\n  Examples:\n    >>> x = jnp.array([[1, 2, 3],\n    ...                [4, 5, 6]])\n    >>> jnp.cumulative_prod(x, axis=1)\n    Array([[  1,   2,   6],\n           [  4,  20, 120]], dtype=int32)\n    >>> jnp.cumulative_prod(x, axis=1, include_initial=True)\n    Array([[  1,   1,   2,   6],\n           [  1,   4,  20, 120]], dtype=int32)\n  \"\"\"\n  x = ensure_arraylike(\"cumulative_prod\", x)\n  if x.ndim == 0:\n    raise ValueError(\n      \"The input must be non-scalar to take a cumulative product, however a \"\n      \"scalar value or scalar array was given.\"\n    )\n  if axis is None:\n    axis = 0\n    if x.ndim > 1:\n      raise ValueError(\n        f\"The input array has rank {x.ndim}, however axis was not set to an \"\n        \"explicit value. The axis argument is only optional for one-dimensional \"\n        \"arrays.\")\n\n  axis = canonicalize_axis(axis, x.ndim)\n  if dtype is not None:\n    dtype = dtypes.check_and_canonicalize_user_dtype(dtype)\n  out = _cumulative_reduction(\"cumulative_prod\", control_flow.cumprod, x, axis, dtype)\n  if include_initial:\n    zeros_shape = list(x.shape)\n    zeros_shape[axis] = 1","sourceCodeStart":2345,"sourceCodeEnd":2381,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/reductions.py#L2345-L2381","documentation":"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.","triggerScenarios":"Calling jnp.cumulative_prod(jnp.asarray(5)) or with any x.ndim == 0 input.","commonSituations":"Chaining cumulative products after full reductions; per-element loops feeding Python scalars into jnp functions.","solutions":["Reshape to at least 1-d: jnp.cumulative_prod(x[None])","Guard with an ndim check in generic accumulate helpers"],"exampleFix":"// before\njnp.cumulative_prod(jnp.prod(x))\n// after\njnp.cumulative_prod(jnp.prod(x, keepdims=True))","handlingStrategy":"validation","validationCode":"import jax.numpy as jnp\n\ndef cumulative_prod_safe(x, **kw):\n    if jnp.ndim(x) == 0:\n        x = jnp.reshape(x, (1,))\n    return jnp.cumulative_prod(x, **kw)","typeGuard":"def is_nonscalar(x) -> bool:\n    return jnp.asarray(x).ndim > 0","tryCatchPattern":null,"preventionTips":["Guard ndim in generic accumulate utilities","Avoid feeding scalar reduction outputs into cumulative ops"],"tags":["jax","numpy","cumulative-prod","scalar-input"],"backgroundTag":"scalar-input-to-array-op","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}