Skip to content

[Relax][ONNX] Fix TopK scalar K extraction in from_onnx#19573

Merged
tlopex merged 1 commit into
apache:mainfrom
javierdejesusda:fix/19571-onnx-topk-scalar
May 19, 2026
Merged

[Relax][ONNX] Fix TopK scalar K extraction in from_onnx#19573
tlopex merged 1 commit into
apache:mainfrom
javierdejesusda:fix/19571-onnx-topk-scalar

Conversation

@javierdejesusda

@javierdejesusda javierdejesusda commented May 17, 2026

Copy link
Copy Markdown
Contributor

Root Cause

TopK._impl_v11 extracted k with int(k.data.numpy()). ONNX emits K as a single-element 1-D tensor constant, so numpy() returns a 1-D array and int() raises TypeError: only 0-dimensional arrays can be converted to Python scalars, failing conversion of any model with a TopK node.

Solution

Resolve k with get_constant(inputs[1], params) and extract the scalar with .item(), matching the Trilu and Reshape converters in the same file. get_constant also handles k arriving as a parameter when keep_params_in_input=True.

Test Plan

test_topk in tests/python/relax/test_frontend_onnx.py already builds K as 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

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

Comment thread python/tvm/relax/frontend/onnx/onnx_frontend.py
@javierdejesusda javierdejesusda force-pushed the fix/19571-onnx-topk-scalar branch from 22f91b3 to fe677e5 Compare May 17, 2026 04:37

@cchung100m cchung100m 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.

LGTM, thanks to @javierdejesusda 😄

@tlopex

tlopex commented May 19, 2026

Copy link
Copy Markdown
Member

@javierdejesusda Could you resolve the conflict so that we can merge it in?

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
@javierdejesusda javierdejesusda force-pushed the fix/19571-onnx-topk-scalar branch from fe677e5 to 9317176 Compare May 19, 2026 07:14
@javierdejesusda

Copy link
Copy Markdown
Contributor Author

Thanks @tlopex. Rebased onto main and resolved the conflict, keeping the reviewed get_constant(inputs[1], params) + .item() extraction.

@tlopex tlopex merged commit 1720d30 into apache:main May 19, 2026
6 checks passed
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] TopK operator crashes during from_onnx() conversion

3 participants