Fix row corruption in batched multi-level cumulative scan - #3063
Merged
Conversation
The intermediate block-scan update in inclusive_scan_iter used an unstrided (NoOp) row offset into the strided per-row scan buffers, so cumulative_sum/cumulative_prod along an axis returned wrong results whenever the scan needed >=3 reduction levels (axis > chunk_size**2) with more than one batch row. Use a Strided1DIndexer with the per-row stride instead. Add a regression test exercising the >=3-level batched scan path.
Contributor
|
View rendered docs @ https://intelpython.github.io/dpnp/index.html |
Contributor
|
Array API standard conformance tests for dpnp=0.21.0dev8=py314ha0e2e8e_8 ran successfully. |
Collaborator
antonwolfy
marked this pull request as ready for review
September 8, 2026 15:10
antonwolfy
requested review from
ndgrigorian and
vlad-perevezentsev
as code owners
September 8, 2026 15:10
ndgrigorian
reviewed
Sep 8, 2026
ndgrigorian
approved these changes
Sep 8, 2026
ndgrigorian
left a comment
Collaborator
There was a problem hiding this comment.
LGTM other than minor nit
ndgrigorian
self-requested a review
September 8, 2026 15:41
ndgrigorian
approved these changes
Sep 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
dpnp.cumsum,dpnp.cumprod, and theirnan/cumulative_*variants (includingdpnp.tensor.cumulative_sum/cumulative_prod) could silently return incorrect results when accumulating along an axis of a multi-row array, with no error raised. The corruption is triggered when the accumulation axis is long enough to require three or more reduction levels in the internal block-scan (axis length greater thanchunk_size ** 2, wherechunk_size = wg_size * n_wi, i.e. roughly a million elements on GPU and about four million on CPU) and the array has more than one batch row. The elementwise total is conserved but redistributed across rows.Root cause
In the batched scan driver
inclusive_scan_iter(accumulators.hpp), the intermediate block-scan fixupupdate_local_chunkspassed aNoOpIndexerfor the per-row (iter) offset into thesrcbuffer. The intermediate block-scan buffers, however, are laid out strided per row with a stride ofsrc_size. Thelocal_scansread already used the correctiter_gid * local_strideoffset, but thesrcwrite offset was unstrided, so rows overwrote each other. This cancels out only wheniter_nelems == 1(a single row) or when fewer than three levels are needed, which is why the bug went unnoticed.Fix
Give
update_local_chunksaStrided1DIndexer{iter_nelems, src_size}for the row offset instead of theNoOpIndexer(the within-rowout_indexerstaysNoOp, since the level buffers are contiguous within a row).