Skip to content

array API: add cumulative_sum and cumulative_prod#3731

Open
katlun-lgtm wants to merge 5 commits into
ml-explore:mainfrom
katlun-lgtm:array-api-cumulative
Open

array API: add cumulative_sum and cumulative_prod#3731
katlun-lgtm wants to merge 5 commits into
ml-explore:mainfrom
katlun-lgtm:array-api-cumulative

Conversation

@katlun-lgtm

Copy link
Copy Markdown
Contributor

Summary

Focused split from #3684. Adds cumulative_sum and cumulative_prod as the Array API standard equivalents of cumsum/cumprod.

Why separate names? (addressing @zcbenz's concern in #3684)

These are intentionally separate from cumsum/cumprod because they have different semantics on three axes:

Feature cumsum / cumprod cumulative_sum / cumulative_prod
axis=None error — axis required flattens the array first
Initial element not supported include_initial=True prepends identity (0 or 1), output length = input length + 1
dtype param not supported casts input before accumulating

include_initial is specified by the Array API standard and enables patterns like prefix-sum where you need the neutral element at position 0. It cannot be retrofitted onto cumsum/cumprod without a breaking change.

Files changed

  • python/src/ops.cpp — C++ lambda registrations
  • docs/src/python/ops.rst — alphabetical entries
  • python/tests/test_ops.pytest_cumulative_sum_prod

Related

Split from #3684 per reviewer feedback from @zcbenz.

@katlun-lgtm katlun-lgtm mentioned this pull request Jun 20, 2026
4 tasks

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be done by making cumsum support the args of cumulative_sum and then making cumulative_sum a pure alias.

@katlun-lgtm katlun-lgtm force-pushed the array-api-cumulative branch 4 times, most recently from 959d88d to 041ae18 Compare June 22, 2026 11:14
These are the Array API standard equivalents of cumsum/cumprod with
three key differences that justify the separate names:

1. axis=None (default) flattens the input first; cumsum/cumprod require
   an explicit axis.
2. include_initial=True prepends the identity element (0 for sum, 1 for
   prod) so the output length along axis is len+1.  This matches the
   Array API spec's include_initial parameter and has no equivalent in
   cumsum/cumprod.
3. dtype parameter casts the input before accumulating, matching NumPy
   2.0 / Array API behaviour.

Docs and tests included.

Part of the array API split from ml-explore#3684.
@katlun-lgtm katlun-lgtm force-pushed the array-api-cumulative branch from 041ae18 to a75dd87 Compare June 22, 2026 14:29
Without nb::sig(), nanobind's auto-generated __doc__ line 1 is parsed
as RST by Sphinx, causing 'Inline emphasis start-string without end-
string' warnings from the *, keyword-only separator.

With nb::sig() present, Sphinx recognises line 1 as a Python function
signature and skips RST markup parsing of that line.
The pure-alias approach (m.attr = m.attr) caused Sphinx to see
cumulative_sum.__doc__ starting with 'cumsum(...)' — a name mismatch
that prevented signature stripping, leaving '*,' to be parsed as RST
emphasis → 'Inline emphasis start-string without end-string'.

Separate bindings with their own nb::sig('def cumulative_sum(...)') fix
this: Sphinx sees the correct function name, strips the signature line,
and processes only the plain docstring body as RST.

cumsum and cumprod are restored to upstream-identical implementations.
cumulative_sum and cumulative_prod add dtype and include_initial params
as required by the array API standard.
@zcbenz

zcbenz commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Sorry if I was not clear but the correct way to fix this is to add the include_initial arg to existing cumsum op, and then make the cumulative_sum an alias of cumsum.

katlun-lgtm and others added 2 commits June 22, 2026 21:38
…e aliases

Per zcbenz review: extend cumsum and cumprod with dtype and
include_initial params, then expose cumulative_sum and cumulative_prod
as pure aliases (m.attr = m.attr).

Also remove cumulative_sum/cumulative_prod from ops.rst autosummary —
all other pure aliases (empty, pow, matrix_transpose) are intentionally
absent from ops.rst; listing them caused Sphinx to encounter a name
mismatch in the shared __doc__ (__doc__ starts with 'cumsum(...)' but
Sphinx is documenting 'cumulative_sum') which prevented signature
stripping and triggered an RST emphasis warning on '*, '.

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation of new parameter should live in the C++ version of the ops not the python wrapper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants