guard_negative_gather_indices: offset gather_nd indices from batch_dims - #2794
Open
LeSingh1 wants to merge 1 commit into
Open
guard_negative_gather_indices: offset gather_nd indices from batch_dims#2794LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
gather_nd's indices address x starting at dimension batch_dims: its output is
K[:-1] + D[batch_dims + K[-1]:]. So a negative index at position j must be
offset by D[batch_dims + j]. The pass always slices x's shape from 0:
slice_shape = mb.slice_by_size(x=x_shape, begin=[0], size=indices_last_dim_expand)
With batch_dims > 0 that adds the wrong dimension size. For
x.shape == (2, 5, 3), indices.shape == (2, 2, 1), batch_dims == 1, an index of
-1 should become -1 + x.shape[1] == 4, but the pass rewrites it to
-1 + x.shape[0] == 1, so the model gathers a different element (and can go out
of range whenever x.shape[0] < -min(indices)).
Slice from batch_dims instead. batch_dims == 0 is unchanged.
The gather branch is already correct: gather's axis is absolute with respect to
x, and its indices are documented as -D[axis] <= v < D[axis], so x.shape[axis]
is the right offset whatever batch_dims is.
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.
Problem
gather_nd'sindicesaddressxstarting at dimensionbatch_dims— its documented output isV = K[:-1] + D[batch_dims + K[-1]:]. So a negative index at positionjof the last axis must be offset byD[batch_dims + j].guard_negative_gather_indicesalways slicesx's shape from0:With
batch_dims > 0that adds the wrong dimension size:The rewrite is fully constant-foldable here, so the wrong result is directly visible:
slice_by_sizebegin[0]— should be[1][[[1], [0]], [[1], [1]]][[[4], [0]], [[4], [1]]]-1should become-1 + x.shape[batch_dims]=-1 + 5=4; the pass produces-1 + x.shape[0]=1. The model gathers a different element, and goes out of range entirely wheneverx.shape[0] < -min(indices).This pass runs for iOS17+ targets, where
gather_ndno longer accepts negative indices — so this is exactly the path meant to make negative indices safe.Fix
Slice
x's shape frombatch_dimsinstead of0.batch_dims == 0is unchanged.The
gatherbranch is already correct and is left alone:gather'saxisis absolute with respect toxand its indices are documented as-D[axis] <= v < D[axis], sox.shape[axis]is the right offset whateverbatch_dimsis.Tests
TestGuardNegativeGatherIndices::test_guard_negative_gather_nd_indices_with_batch_dims[batch_dims=0,1]— asserts theslice_by_sizebegin and the constant-folded rewritten indices.batch_dims=1fails onmain;batch_dims=0passes both before and after, guarding against over-correction.The existing
test_guard_negative_gather_indices(which only uses the defaultbatch_dims=0) is untouched. I ran the whole class before and after; the pre-existing failure set is identical (this environment cannot load CoreML.framework, soassert_model_is_validfails there either way).Note: this touches
optimize_tensor_operation.pyandtest_passes.py, which my PR #2789 also modifies, in a different pass/class.