[Relax][ONNX] Fix TopK scalar K extraction in from_onnx#19573
Merged
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the ONNX frontend's TopK implementation to use .item() when converting the k input to an integer, preventing crashes with 1-D tensors. The reviewer suggested using get_constant for consistency and to handle cases where k is a parameter, while also identifying similar patterns in other operators that may require future fixes.
22f91b3 to
fe677e5
Compare
cchung100m
approved these changes
May 18, 2026
cchung100m
left a comment
Contributor
There was a problem hiding this comment.
LGTM, thanks to @javierdejesusda 😄
Member
|
@javierdejesusda Could you resolve the conflict so that we can merge it in? |
tlopex
approved these changes
May 19, 2026
TopK._impl_v11 extracted k with int(k.data.numpy()), which raises TypeError because ONNX emits K as a single-element 1-D tensor constant rather than a 0-d scalar. Resolve k via get_constant and extract the scalar with .item(), matching the Trilu and Reshape converters. get_constant also handles k passed as a parameter when keep_params_in_input=True. Fixes apache#19571
fe677e5 to
9317176
Compare
Contributor
Author
|
Thanks @tlopex. Rebased onto main and resolved the conflict, keeping the reviewed get_constant(inputs[1], params) + .item() extraction. |
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.
Root Cause
TopK._impl_v11extractedkwithint(k.data.numpy()). ONNX emitsKas a single-element 1-D tensor constant, sonumpy()returns a 1-D array andint()raisesTypeError: only 0-dimensional arrays can be converted to Python scalars, failing conversion of any model with aTopKnode.Solution
Resolve
kwithget_constant(inputs[1], params)and extract the scalar with.item(), matching theTriluandReshapeconverters in the same file.get_constantalso handleskarriving as a parameter whenkeep_params_in_input=True.Test Plan
test_topkintests/python/relax/test_frontend_onnx.pyalready buildsKas a single-element 1-D INT64 constant, so it exercises this path..item()returns the scalar for both single-element 1-D and 0-d constants.Issue
Fixes #19571