Skip to content

[Fix][Relax][ONNX] Import TopK indices as int64#19973

Open
viiccwen wants to merge 1 commit into
apache:mainfrom
viiccwen:fix/onnx-topk-indices-int64
Open

[Fix][Relax][ONNX] Import TopK indices as int64#19973
viiccwen wants to merge 1 commit into
apache:mainfrom
viiccwen:fix/onnx-topk-indices-int64

Conversation

@viiccwen

@viiccwen viiccwen commented Jul 9, 2026

Copy link
Copy Markdown

Fixes #19972

ONNX specifies that the second output of TopK, indices, has element type int64, and the ONNX TopK operator spec constrains the index tensor type to tensor(int64): https://onnx.ai/onnx/operators/onnx__TopK.html

The Relax ONNX frontend previously called relax.op.topk without specifying the output indices dtype, so Relax used its default int32 indices.

This can make otherwise valid ONNX graphs fail during import when the TopK indices are consumed by later integer/index operations that use ONNX's usual int64 constants. One example is TopK -> Div, where Relax rejects the binary operation because the imported TopK indices are int32 while the divisor is int64.

This patch passes dtype="int64" when importing ONNX TopK, matching the ONNX operator spec. It also updates the existing TopK frontend test to check output dtypes, so the imported indices must match ONNX Runtime's int64 output.

Verification:

  • uv run --no-sync python -m pytest tests/python/relax/test_frontend_onnx.py::test_topk -q

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the ONNX frontend's TopK operator implementations (_impl_v11 and _impl_v1) to explicitly specify dtype="int64" when calling relax.op.topk. Additionally, the corresponding test_topk test is updated to enable data type checking. I have no feedback to provide as there are no review comments.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@viiccwen

viiccwen commented Jul 9, 2026

Copy link
Copy Markdown
Author

cc @guan404ming. 🙌

@viiccwen viiccwen force-pushed the fix/onnx-topk-indices-int64 branch from 80cffed to 319a74b Compare July 10, 2026 03:00
ONNX specifies that TopK's second output, indices, has int64 element type. The Relax ONNX frontend previously used relax.op.topk without specifying the indices dtype, so Relax used its default int32 indices.

This can make valid ONNX graphs fail or produce an incorrect imported type when TopK indices are consumed by later integer or indexing operations.

Pass dtype="int64" when importing ONNX TopK and enable dtype checking in the TopK frontend test.

Signed-off-by: viiccwen <vicwen@apache.org>
@viiccwen viiccwen force-pushed the fix/onnx-topk-indices-int64 branch from 2db4420 to 0a140b4 Compare July 10, 2026 03:46

@tlopex tlopex left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@guan404ming

Copy link
Copy Markdown
Member

LGTM but the ci seems not worked for now.

cc @tqchen

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 ONNX frontend imports TopK indices as int32 instead of int64

3 participants