Repository navigation
[Fix][Relax][Frontend][Torch] Handle negative indices in the select converter - #20859
Open
ShawnNotFound wants to merge 1 commit into
Open
ShawnNotFound wants to merge 1 commit into
ShawnNotFound wants to merge 1 commit into
Conversation
… converter Fixes apache#20855. `select` passed a negative index straight to `relax.op.take`, whose default `fast` mode does no bounds handling, so `x[:, -1]` read memory before the buffer. Count the index from the end of the dimension when its size is static, and use `wrap` mode when it is symbolic. Add regression tests for the ExportedProgram and FX importers.
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 #20855.
select passed a negative index straight to relax.op.take, whose default fast mode does no bounds handling, so x[:, -1] (and x[-1], x.select(1, -1), ...) read memory before the buffer instead of the last element.
The converter now counts a negative index from the end of the dimension when the dimension size is static, and uses take with mode="wrap" when the size is symbolic. The same fix is applied to the ExportedProgram and FX importers.
Tests: added test_select_negative_index (static and dynamic dim, structural and numerical) to test_frontend_from_exported_program.py, and a negative-index case to test_select in test_frontend_from_fx.py. Both fail without the change. With it, both test files pass locally (torch 2.10.0 CPU, LLVM 18).