Skip to content

Fix dpnp.ndarray.flat indexing edge cases - #3045

Open
antonwolfy wants to merge 20 commits into
masterfrom
fix/SAT-8204-flatiter-indexing
Open

Fix dpnp.ndarray.flat indexing edge cases#3045
antonwolfy wants to merge 20 commits into
masterfrom
fix/SAT-8204-flatiter-indexing

Conversation

@antonwolfy

@antonwolfy antonwolfy commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

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.flat accepted only a single integer index and raised TypeError for 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.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

@antonwolfy antonwolfy added this to the 0.21.0 release milestone Aug 27, 2026
@antonwolfy antonwolfy self-assigned this Aug 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/pull/3045/index.html

@github-actions

github-actions Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.21.0dev7=py314ha0e2e8e_33 ran successfully.
Passed: 1375
Failed: 0
Skipped: 7

@coveralls

coveralls commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 78.525% (+0.03%) from 78.492% — fix/SAT-8204-flatiter-indexing into master

@antonwolfy
antonwolfy marked this pull request as ready for review August 28, 2026 16:37
Comment thread dpnp/dpnp_flatiter.py Outdated
Comment thread dpnp/dpnp_flatiter.py Outdated
Comment thread dpnp/dpnp_flatiter.py Outdated
Comment thread dpnp/tests/third_party/cupy/indexing_tests/test_iterate.py
Comment thread dpnp/dpnp_flatiter.py
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
antonwolfy force-pushed the fix/SAT-8204-flatiter-indexing branch from f7c9349 to e314321 Compare September 3, 2026 15:41
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.
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.

4 participants