{"record":{"id":"0862387b428cec31","repo":"jax-ml/jax","slug":"the-input-must-be-non-scalar-to-take-a-cumulative","errorCode":null,"errorMessage":"The input must be non-scalar to take a cumulative sum, however a scalar value or scalar array was given.","messagePattern":"The input must be non-scalar to take a cumulative sum, however a scalar value or scalar array was given\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/reductions.py","lineNumber":2301,"sourceCode":"\n  See Also:\n    - :func:`jax.numpy.cumsum`: alternative API for cumulative sum.\n    - :func:`jax.numpy.nancumsum`: cumulative sum while ignoring NaN values.\n    - :func:`jax.numpy.add.accumulate`: cumulative sum via the ufunc API.\n\n  Examples:\n    >>> x = jnp.array([[1, 2, 3],\n    ...                [4, 5, 6]])\n    >>> jnp.cumulative_sum(x, axis=1)\n    Array([[ 1,  3,  6],\n           [ 4,  9, 15]], dtype=int32)\n    >>> jnp.cumulative_sum(x, axis=1, include_initial=True)\n    Array([[ 0,  1,  3,  6],\n           [ 0,  4,  9, 15]], dtype=int32)\n  \"\"\"\n  x = ensure_arraylike(\"cumulative_sum\", x)\n  if x.ndim == 0:\n    raise ValueError(\n      \"The input must be non-scalar to take a cumulative sum, 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 = _cumsum_with_promotion(x, axis=axis, dtype=dtype)\n  if include_initial:\n    zeros_shape = list(x.shape)\n    zeros_shape[axis] = 1","sourceCodeStart":2283,"sourceCodeEnd":2319,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/reductions.py#L2283-L2319","documentation":"jnp.cumulative_sum requires an array with at least one dimension; a scalar (0-d) input has no axis to accumulate over, so JAX raises ValueError immediately.","triggerScenarios":"Calling jnp.cumulative_sum(jnp.asarray(3.0)) or passing a Python scalar that becomes a 0-d array; x.ndim == 0.","commonSituations":"Feeding the output of an all-reduction (e.g. jnp.sum without keepdims) into cumulative_sum; looping over per-sample scalars in data pipelines.","solutions":["Reshape to 1-d first: jnp.cumulative_sum(x.reshape(1)) or x[None]","Check x.ndim > 0 before calling in generic code","Use keepdims=True on the upstream reduction so the input stays non-scalar"],"exampleFix":"// before\njnp.cumulative_sum(jnp.sum(x))\n// after\njnp.cumulative_sum(jnp.sum(x, keepdims=True))","handlingStrategy":"validation","validationCode":"import jax.numpy as jnp\n\ndef cumulative_sum_safe(x, **kw):\n    if jnp.ndim(x) == 0:\n        x = jnp.reshape(x, (1,))\n    return jnp.cumulative_sum(x, **kw)","typeGuard":"def is_nonscalar(x) -> bool:\n    return jnp.asarray(x).ndim > 0","tryCatchPattern":null,"preventionTips":["Check ndim before cumulative ops","Keep upstream reductions with keepdims=True when chaining"],"tags":["jax","numpy","cumulative-sum","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"}