Skip to content

data: Fix negative-step, out-of-range and strided slices of Data - #3141

Open
chaitanya0305 wants to merge 1 commit into
devitocodes:mainfrom
chaitanya0305:fix/data-negative-step-slices
Open

chaitanya0305 wants to merge 1 commit into
devitocodes:mainfrom
chaitanya0305:fix/data-negative-step-slices

Conversation

@chaitanya0305

Copy link
Copy Markdown

Fixes #3140.

Indexing Data with some ordinary slices selected different entries than NumPy. Without MPI, this happened with a negative step and a stop of -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_index resolves slice bounds against the global axis size as NumPy does. Open bounds stay None, a negative-step stop that resolves to "past index 0" becomes None, and empty selections become 0:0. Modulo-indexed axes are left alone.
  • New owned_positions(idx, decomposition) in devito/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 replicated val, which replaces the "step size > 1" fix-up.
  • _process_args derives the increasing slice from range(*slice.indices(n)). It gives every val axis a transform entry, aligned to val'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 on main, all pass with this change.
  • TestDataDistributed::test_setitem_strided (4 ranks, 7 assignments gathered and compared with NumPy): fails on main, passes with this change. Ranks agree on assignment errors before gathering, so a failure on one rank can't hang the test.
  • All of tests/test_data.py passes (192 passed, 2 skipped, including the parallel tests).
  • Outside the test suite, I checked every single-axis slice with bounds in None, -9..9 and 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 --preview and isort --check-only are clean on the changed files.

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 <dcchaitanyadeshpande@gmail.com>

This branch has not been deployed

No deployments
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.

Data slicing with negative steps, out-of-range bounds or (under MPI) strides selects or writes the wrong entries

1 participant