This is an automated email from the ASF dual-hosted git repository.
tlopex pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git
The following commit(s) were added to refs/heads/main by this push:
new a0ffe06def [Fix][Relax][ONNX] Convert OneHot depth to int (#20908)
a0ffe06def is described below
commit a0ffe06defbbc7dd7d6207fb40d4e5cc68329eff
Author: Zhewen Tan <[email protected]>
AuthorDate: Sun Nov 15 14:52:53 2026 +0800
[Fix][Relax][ONNX] Convert OneHot depth to int (#20908)
This PR converts a constant ONNX OneHot depth value to a Python integer
before passing it to Relax. ONNX permits floating-point depth tensors
and specifies conversion to int64 before use.
The OneHot frontend test now covers both int64 and float depth tensors.
Fixes #20895
Testing:
- python -m pytest -q tests/python/relax/test_frontend_onnx.py -k onehot
- pre-commit run --files python/tvm/relax/frontend/onnx/onnx_frontend.py
tests/python/relax/test_frontend_onnx.py
---
python/tvm/relax/frontend/onnx/onnx_frontend.py | 2 +-
tests/python/relax/test_frontend_onnx.py | 5 +++--
2 files changed, 4 insertions(+), 3 deletions(-)
diff --git a/python/tvm/relax/frontend/onnx/onnx_frontend.py
b/python/tvm/relax/frontend/onnx/onnx_frontend.py
index 2284b4aa58..65103d786c 100644
--- a/python/tvm/relax/frontend/onnx/onnx_frontend.py
+++ b/python/tvm/relax/frontend/onnx/onnx_frontend.py
@@ -5455,7 +5455,7 @@ class OneHot(OnnxOpConverter):
assert isinstance(depth, tvm_tirx.ir.GenericConst), (
"Only constant depth currently supported."
)
- depth = depth.value.numpy().tolist()
+ depth = int(depth.value.numpy().item())
assert isinstance(values, tvm_tirx.ir.GenericConst), (
"Only constant values currently supported."
)
diff --git a/tests/python/relax/test_frontend_onnx.py
b/tests/python/relax/test_frontend_onnx.py
index db35224067..7f540a53f6 100644
--- a/tests/python/relax/test_frontend_onnx.py
+++ b/tests/python/relax/test_frontend_onnx.py
@@ -11104,7 +11104,8 @@ def test_flatten_dynamic():
verify_flatten_dynamic_ir(2, ExpectedDynamicAxis2)
-def test_onehot():
[email protected]("depth_dtype", [TensorProto.INT64, TensorProto.FLOAT])
+def test_onehot(depth_dtype):
one_hot_node = helper.make_node("OneHot", ["indices", "depth", "values"],
["y"], axis=1)
graph = helper.make_graph(
[one_hot_node],
@@ -11113,7 +11114,7 @@ def test_onehot():
helper.make_tensor_value_info("indices", TensorProto.INT64, [2,
2]),
],
initializer=[
- helper.make_tensor("depth", TensorProto.INT64, [], [10]),
+ helper.make_tensor("depth", depth_dtype, [], [10]),
helper.make_tensor("values", TensorProto.FLOAT, [2], [3, 1]),
],
outputs=[helper.make_tensor_value_info("y", TensorProto.FLOAT, [2, 10,
2])],