Bound gather_nd / scatter_nd indices by the dims they actually address - #2806
Open
LeSingh1 wants to merge 1 commit into
Open
Bound gather_nd / scatter_nd indices by the dims they actually address#2806LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
iOS17 gather_nd and scatter_nd validate indices against the whole input
shape:
upper_bound = self.x.shape
if np.count_nonzero(np.logical_or(indices < 0, indices >= upper_bound)):
indices has shape (*K) and its last axis addresses only indices.shape[-1]
dims, starting after batch_dims for gather_nd. Comparing a (..., K) array
with a length rank(x) tuple only lines up when K == rank(x). Otherwise it
either fails to broadcast, or, when K is 1, compares every index against
every dim of x.
Both make in-bounds programs fail to convert. gather_nd on a rank 3 x with
indices of depth 2 raises "operands could not be broadcast together with
shapes (2,2) (3,)", and gather_nd with batch_dims=1 on a (2, 3) x rejects
index 2 for being >= x.shape[0], although it addresses x.shape[1].
Slice the shape down to the dims the indices address. Symbolic dims are
skipped, since they cannot be compared.
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.
iOS17
gather_ndandscatter_ndvalidateindicesagainst the full input shape:indiceshas shape(*K)and its last axis addresses onlyindices.shape[-1]dims, starting afterbatch_dimsforgather_nd. Comparing a(..., K)array against a lengthrank(x)tuple only lines up whenK == rank(x).Two in-bounds programs that fail to convert today:
The second is the
K == 1case, where the comparison broadcasts and then checks every index against every dim.Fix: slice the shape down to the dims the indices address —
data.shape[:K]forscatter_nd,x.shape[batch_dims : batch_dims + K]forgather_nd. Symbolic dims are skipped, since they cannot be compared. Out-of-bounds indices are still rejected, now against the right bound.Three new tests in
TestScatterNdandTestGatherNd, all failing onmain. The existingvalidate_indicestests are unchanged and still pass.