Fix dpnp.ndarray.flat indexing edge cases - #3045
Open
antonwolfy wants to merge 20 commits into
Open
Conversation
Contributor
|
View rendered docs @ https://intelpython.github.io/dpnp/pull/3045/index.html |
Contributor
|
Array API standard conformance tests for dpnp=0.21.0dev7=py314ha0e2e8e_33 ran successfully. |
Collaborator
antonwolfy
marked this pull request as ready for review
August 28, 2026 16:37
antonwolfy
requested review from
ndgrigorian and
vlad-perevezentsev
as code owners
August 28, 2026 16:37
ndgrigorian
reviewed
Sep 1, 2026
Support slices, ellipsis, empty tuple, and integer/boolean array indices in flatiter __getitem__/__setitem__, reusing regular array indexing for validation. Reject numpy.newaxis (None), raise IndexError for out-of-bounds integer array indices, and reject assignment with a 0-D index, matching NumPy (gh-28590). Return copies from __getitem__ rather than views. SAT-8204
Cover slices, ellipsis, empty tuple, integer/boolean array indices, newaxis rejection, out-of-bounds, non-contiguous write-back, and copy-not-view semantics. Cross-check against NumPy where behavior is shared, and gate NumPy 2.4-only cases (numpy-gh-28590) with testing.with_requires.
Match numpy's np.put-style cycling when a flat assignment value is shorter than the selection (dpnp.put broadcasts instead). Enable the previously-disabled slice/ellipsis/empty-tuple parametrizations in the cupy flatiter iterate tests, and drop the IndexError cases that became valid indices in numpy 2.4 (numpy-gh-28590).
Expand the flatiter and ndarray.flat docstrings to align with NumPy, documenting supported basic and advanced indexing and adding See Also and Examples sections. Fix a broken dpnp.flat cross-reference in the ndarray.flatten docstring.
Resolve a scalar integer or slice flat index to positions directly instead of allocating arange(size) and indexing it, so a single-element or slice assignment no longer materializes a full index array.
A 1-D flat iterator takes a single index, so unwrap a 1-element index tuple to its element before validation. This makes an out-of-bounds array index wrapped in a tuple (e.g. arr.flat[(array([5]),)]) raise IndexError as in NumPy, instead of silently wrapping.
Validate an out-of-bounds flat index by inspecting the raw index on the host (numpy) when it is not already a device array, instead of always uploading it via dpnp.asarray and reducing on device. Add tests for usm_ndarray and empty index keys.
A scalar flat index targets a single element, so assigning an array (ndim >= 1) value now raises ValueError to match NumPy and dpnp's own scalar element assignment, instead of silently taking the first value.
Gate the single-item array-assignment test on numpy>=2.4 (the 0-d array index only raises there), and remove the numpy>=2.4 gate from the boolean-mask and array-out-of-bounds tests, which already behave identically on older NumPy.
A flat iterator is 1-D and takes a single index, so a tuple with more than one element (e.g. arr.flat[..., 2]) now raises IndexError as in NumPy, instead of silently absorbing the extra dimensions and, for setitem, mutating the array.
Catch only TypeError/ValueError from numpy.asarray when resolving a flat index for bounds-checking, so unexpected errors propagate. Add a ragged-index test covering the fallback.
Fold tuple-unwrap and invalid-key rejection into _normalize_key (called by getitem/setitem), inline the single-use flat view, and abbreviate a comment.
A boolean flat index is only valid as an ndarray mask; a boolean scalar (True/False or 0-d bool array) or a boolean list now raises IndexError, matching NumPy (gh-28590) and avoiding a whole-array overwrite on assignment. Re-enable the boolean-scalar case in the cupy iterate IndexError tests and add dpnp tests for the rejected forms.
Gate the single-element-tuple and tuple-wrapped out-of-bounds tests on numpy>=2.4, since tuple indexing of a flat iterator became valid only in NumPy 2.4 (numpy-gh-28590). Assert the iterated element count so an early-stop __next__ regression is caught despite zip truncation.
antonwolfy
force-pushed
the
fix/SAT-8204-flatiter-indexing
branch
from
September 3, 2026 15:41
f7c9349 to
e314321
Compare
Single-element tuple indexing and tuple-wrapped out-of-bounds already behave the same on NumPy 2.0, so drop the unnecessary numpy>=2.4 gate to keep the tests running on the older-NumPy CI leg.
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.
This PR reworks
dpnp.flatiter(dpnp.ndarray.flat) indexing so it aligns with NumPy's flat-iterator semantics, which were tightened in NumPy 2.4.Previously
dpnp.ndarray.flataccepted only a single integer index and raisedTypeErrorfor everything else. Indexing now delegates to regular array indexing of the flattened array, so it supports the full set of flat index types and matches NumPy's error behavior.