{"record":{"id":"0da2d633170f3c04","repo":"jax-ml/jax","slug":"accumulate-only-supported-for-binary-ufuncs","errorCode":null,"errorMessage":"accumulate only supported for binary ufuncs","messagePattern":"accumulate only supported for binary ufuncs","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/numpy/ufunc_api.py","lineNumber":372,"sourceCode":"      :func:`jax.numpy.cumprod` along the specified axis:\n\n      >>> jnp.multiply.accumulate(x, axis=1)\n      Array([[  1,   2,   6],\n             [  4,  20, 120]], dtype=int32)\n      >>> jnp.cumprod(x, axis=1)\n      Array([[  1,   2,   6],\n             [  4,  20, 120]], dtype=int32)\n\n      For other binary ufuncs, the accumulation is an operation not available\n      via standard APIs. For example, :meth:`jax.numpy.bitwise_or.accumulate`\n      is essentially a bitwise cumulative ``any``:\n\n      >>> jnp.bitwise_or.accumulate(x, axis=1)\n      Array([[1, 3, 3],\n             [4, 5, 7]], dtype=int32)\n    \"\"\"\n    if self.nin != 2:\n      raise ValueError(\"accumulate only supported for binary ufuncs\")\n    if self.nout != 1:\n      raise ValueError(\"accumulate only supported for functions returning a single value\")\n    if out is not None:\n      raise NotImplementedError(f\"out argument of {self.__name__}.accumulate()\")\n    accumulate = self.__static_props['accumulate'] or self._accumulate_via_scan\n    return accumulate(a, axis=axis, dtype=dtype)\n\n  def _accumulate_via_scan(self, arr: ArrayLike, axis: int = 0,\n                           dtype: DTypeLike | None = None) -> Array:\n    assert self.nin == 2 and self.nout == 1\n    check_arraylike(f\"{self.__name__}.accumulate\", arr)\n    arr = lax.asarray(arr)\n\n    if dtype is None:\n      dtype = api.eval_shape(self._func, lax._one(arr), lax._one(arr)).dtype\n\n    if axis is None or isinstance(axis, tuple):\n      raise ValueError(\"accumulate does not allow multiple axes\")","sourceCodeStart":354,"sourceCodeEnd":390,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/numpy/ufunc_api.py#L354-L390","documentation":"ufunc.accumulate (cumulative reduction, e.g. cumsum via jnp.add.accumulate) requires a binary ufunc (nin == 2). Calling .accumulate on a unary ufunc raises this ValueError.","triggerScenarios":"jnp.negative.accumulate(x) or .accumulate on any ufunc with a single input.","commonSituations":"Generic code that reflects over ufuncs; mistaking accumulate for a general scan over unary ops.","solutions":["Use only binary ufuncs with .accumulate","For cumulative unary effects, express them with jax.lax.scan or apply the op before accumulating"],"exampleFix":"// before\njnp.negative.accumulate(x)\n// after\njnp.add.accumulate(-x)","handlingStrategy":"validation","validationCode":"assert ufunc.nin == 2 before ufunc.accumulate(...)","typeGuard":"def is_binary_ufunc(u): return u.nin == 2","tryCatchPattern":null,"preventionTips":["Use jnp.cumsum/jnp.cumprod equivalents where possible"],"tags":["jax","ufunc","accumulate","api-misuse"],"backgroundTag":"unsupported-accumulate-operation","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}