Skip to content

[Fix][Relax][Frontend][Torch] Handle negative indices in the select converter - #20859

Open
ShawnNotFound wants to merge 1 commit into
apache:mainfrom
ShawnNotFound:fix-20855-torch-select-negative-index
Open

ShawnNotFound wants to merge 1 commit into
apache:mainfrom
ShawnNotFound:fix-20855-torch-select-negative-index

Conversation

@ShawnNotFound

Copy link
Copy Markdown

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).

… 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

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.

[Bug][Relax][PyTorch] Negative index in x[:, -1] / select reads out of bounds after from_exported_program

1 participant