{"record":{"id":"897547dd91f509ca","repo":"jax-ml/jax","slug":"the-input-array-has-rank-x-ndim-however-axis-wa","errorCode":null,"errorMessage":"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.","messagePattern":"The input array has rank (.+?), however axis was not set to an explicit value\\. The axis argument is only optional for one-dimensional arrays\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/reductions.py","lineNumber":2308,"sourceCode":"    >>> 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\n    out = lax.concatenate(\n      [lax.full(zeros_shape, 0, dtype=out.dtype), out],\n      dimension=axis)\n  return out\n\n\n@export","sourceCodeStart":2290,"sourceCodeEnd":2326,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/reductions.py#L2290-L2326","documentation":"For jnp.cumulative_sum, the axis argument is only optional for 1-d arrays. If the input has rank > 1 and axis is None, JAX raises ValueError telling you to specify the axis explicitly (unlike cumsum, it will not flatten).","triggerScenarios":"Calling jnp.cumulative_sum(x) where x.ndim > 1, e.g. a (3, 4) matrix with axis omitted.","commonSituations":"Assuming cumulative_sum behaves like np.cumsum (which flattens by default) or like cumsum on the last axis; testing with 1-d data then deploying on batches.","solutions":["Pass an explicit axis: jnp.cumulative_sum(x, axis=1)","Use jnp.cumsum if flatten-over-all-elements semantics are actually wanted"],"exampleFix":"// before\njnp.cumulative_sum(batch)  # batch.ndim == 2\n// after\njnp.cumulative_sum(batch, axis=1)","handlingStrategy":"validation","validationCode":"import jax.numpy as jnp\n\ndef cumulative_sum_safe(x, axis=None, **kw):\n    if axis is None and jnp.ndim(x) > 1:\n        raise ValueError('specify axis for multi-dim cumulative_sum')\n    return jnp.cumulative_sum(x, axis=axis, **kw)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass axis for batched inputs","Write tests with 2-d fixtures, not just 1-d","Remember cumulative_sum does not flatten like cumsum"],"tags":["jax","numpy","cumulative-sum","axis-argument"],"backgroundTag":"missing-required-axis","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}