Repository navigation
data: Fix negative-step, out-of-range and strided slices of Data - #3141
Open
chaitanya0305 wants to merge 1 commit into
Open
chaitanya0305 wants to merge 1 commit into
chaitanya0305 wants to merge 1 commit into
Conversation
Indexing Data with some slices selected different entries than NumPy: - with a negative step, a stop of -1 was read as "past index 0" instead of "the last entry", so e.g. f.data[5:-1:-1] returned 5..0 instead of nothing; - bounds beyond either end were not clipped, so e.g. f.data[7::-2] on a 7-point axis returned [5, 3, 1] instead of [6, 4, 2, 0]; - with MPI, assigning a replicated array through a strided slice cut `val` in global index units, ignoring the stride, and the "step > 1" fix-up paired the wrong axes once an integer index was involved. A negative-step slice was turned positive with the wrong start when the stop was not aligned to the stride, and `val` was flipped along the wrong axis. Such assignments stored wrong values or raised broadcast errors, depending on the number of ranks. Resolve slice bounds against the global axis size in _normalize_index, compute the part of `val` each rank owns with a new owned_positions helper, and derive the increasing slice and the flips of `val` from slice.indices in _process_args. Signed-off-by: Chaitanya Deshpande <[email protected]>
This branch has not been deployed
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.
Fixes #3140.
Indexing
Datawith some ordinary slices selected different entries than NumPy. Without MPI, this happened with a negative step and astopof -1, or with bounds beyond either end. With MPI, assignments through strided, negative-step or negatively bounded slices also stored wrong values, or raised broadcast errors, depending on the number of ranks. The issue has reproducers and root causes.Changes:
Data._normalize_indexresolves slice bounds against the global axis size as NumPy does. Open bounds stayNone, a negative-stepstopthat resolves to "past index 0" becomesNone, and empty selections become0:0. Modulo-indexed axes are left alone.owned_positions(idx, decomposition)indevito/data/utils.py(with a doctest). It gives the positions, within a strided global selection, that the calling rank owns.__setitem__uses it to cut the replicatedval, which replaces the "step size > 1" fix-up._process_argsderives the increasing slice fromrange(*slice.indices(n)). It gives everyvalaxis atransformentry, aligned toval's trailing dimensions, so the flip happens on the right axis.Tests:
TestDataBasic::test_slice_bounds_like_numpy(serial reads and writes, 6 cases): 5 fail onmain, all pass with this change.TestDataDistributed::test_setitem_strided(4 ranks, 7 assignments gathered and compared with NumPy): fails onmain, passes with this change. Ranks agree on assignment errors before gathering, so a failure on one rank can't hang the test.tests/test_data.pypasses (192 passed, 2 skipped, including the parallel tests).None, -9..9and steps -1..-3 (1200 cases), 600 random 2-D read/write slices without MPI, and 400 random 2-D assignments on 2 and 4 ranks against NumPy. All match.flake8,ruff check --previewandisort --check-onlyare clean on the changed files.