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 6be0db44c7 [Relax][Onnx][Resize] Fix ROI values when tensor ROI is 
Empty pass node Constant (#18691)
6be0db44c7 is described below

commit 6be0db44c7a8cec44bb4583b994b73ad93693f6b
Author: Nguyen Duy Loc <[email protected]>
AuthorDate: Fri Jan 30 20:35:20 2026 +0700

    [Relax][Onnx][Resize] Fix ROI values when tensor ROI is Empty pass node 
Constant (#18691)
    
    This PR fix ROI values when tensor ROI is Empty pass Constant node in
    Resize Operator
    ### Description:
    - Fix ROI values when tensor ROI is Empty pass Constant node
    - Create test with Constant and Tensor ROI is Empty
    
    ### Step to Reproduce:
    - Deploy yolov5n.onnx using TVM. When executing
    relax.transform.LegalizeOps() is happen error: "IndexError: list index
    out of range" at roi array, because roi = []
    - Error log:
    > "File ~/Contributes/tvm/python/tvm/topi/image/resize.py:615, in
    _resize_2d()
    >     608     in_x = x1 * (image_width - 1) + w_scale * x
    >     609 else:
    >     610     in_x = get_inx(
    >     611         x,
    >     612         image_width,
    >     613         target_width,
    >     614         coordinate_transformation_mode,
    > --> 615         roi[1],
    >     616         roi[3],
    >     617         width_use_int_div,
    >     618     )
    >     619     in_y = get_inx(
    >     620         y,
    >     621         image_height,
    >    (...)    626         height_use_int_div,
    >     627     )
    >     629 if method == "nearest_neighbor":
    > IndexError: list index out of range"
    - Graph:
    <img width="1000" height="500" alt="PR"
    
src="https://github.com/user-attachments/assets/e112511a-43a4-4555-afc6-e22d62ec095e";
    />
    
    
    ### Resolve:
    - On the Frontend, when encountering a case where ROI is constant and
    ROI is empty, the handling is the same as when ROI = None (fill in the
    ROI tensor with 0.0).
---
 python/tvm/relax/frontend/onnx/onnx_frontend.py |  4 ++-
 tests/python/relax/test_frontend_onnx.py        | 45 +++++++++++++++++++------
 2 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/python/tvm/relax/frontend/onnx/onnx_frontend.py 
b/python/tvm/relax/frontend/onnx/onnx_frontend.py
index c71fd96caf..784be639dd 100644
--- a/python/tvm/relax/frontend/onnx/onnx_frontend.py
+++ b/python/tvm/relax/frontend/onnx/onnx_frontend.py
@@ -2277,6 +2277,8 @@ class Resize(OnnxOpConverter):
                 roi = roi.data.numpy().tolist()
                 if len(roi) == 2 * ndims:
                     roi = roi[2:ndims] + roi[ndims + 2 : 2 * ndims]
+                elif len(roi) == 0:
+                    roi = [0.0] * (2 * (ndims - 2))
             else:
                 roi = relax.op.concat(
                     [
@@ -2309,7 +2311,7 @@ class Resize(OnnxOpConverter):
             elif isinstance(sizes, relax.expr.ShapeExpr):
                 sizes = [int(val.value) for val in sizes.values][2:]
             else:
-                assert f"Type {type(size)} for size is currently unsupported."
+                assert f"Type {type(sizes)} for size is currently unsupported."
 
         if ndims == 3:
             return bb.emit_te(
diff --git a/tests/python/relax/test_frontend_onnx.py 
b/tests/python/relax/test_frontend_onnx.py
index b4b3baeb4d..62af660a31 100644
--- a/tests/python/relax/test_frontend_onnx.py
+++ b/tests/python/relax/test_frontend_onnx.py
@@ -2694,11 +2694,12 @@ def test_tile(dynamic):
 def _generate_roi_cases():
     # Base case when with_roi is False
     roi_list = [
-        pytest.param(False, None, id="no_roi"),
+        pytest.param(False, None, False, id="no_roi"),
     ]
 
-    # Valid when with_roi is True
+    # Valid when with_roi is True and with_constant is True/False
     roi_cases = [
+        [],
         [0.0, 0.0, 0.0, 0.0],
         [0.0, 0.0, 1.0, 1.0],
         [0.1, 0.1, 0.9, 0.9],
@@ -2709,27 +2710,51 @@ def _generate_roi_cases():
         [0.1, 0.2, 0.9, 0.8],
     ]
     for roi in roi_cases:
-        roi_list.append(pytest.param(True, roi, id=f"roi_{'_'.join(str(x) for 
x in roi)}"))
+        roi_list.append(pytest.param(True, roi, True, 
id=f"roi_{'_'.join(str(x) for x in roi)}"))
+        roi_list.append(pytest.param(True, roi, False, 
id=f"roi_{'_'.join(str(x) for x in roi)}"))
 
     return roi_list
 
 
[email protected]("with_roi, roi_list", _generate_roi_cases())
-def test_resize(with_roi, roi_list):
[email protected]("with_roi, roi_list, with_constant", 
_generate_roi_cases())
+def test_resize(with_roi, roi_list, with_constant):
+    nodes = []
     resize_node = helper.make_node(
         "Resize", ["X", "roi" if with_roi else "", "scales"], ["Y"], 
mode="cubic"
     )
 
+    if with_roi and with_constant:
+        roi_tensor = helper.make_tensor(
+            name="roi",
+            data_type=TensorProto.FLOAT,
+            dims=[len(roi_list)],
+            vals=roi_list,
+        )
+
+        roi_const_node = helper.make_node(
+            "Constant",
+            inputs=[],
+            outputs=["roi"],
+            value=roi_tensor,
+        )
+        nodes.append(roi_const_node)
+
+    nodes.append(resize_node)
+
+    initializers = [
+        helper.make_tensor("scales", TensorProto.FLOAT, [4], [1.0, 1.0, 2.0, 
2.0]),
+    ]
+
+    if with_roi and not with_constant:
+        initializers.append(helper.make_tensor("roi", TensorProto.FLOAT, 
[len(roi_list)], roi_list))
+
     graph = helper.make_graph(
-        [resize_node],
+        nodes,
         "resize_test",
         inputs=[
             helper.make_tensor_value_info("X", TensorProto.FLOAT, [1, 3, 32, 
32]),
         ],
-        initializer=[
-            helper.make_tensor("scales", TensorProto.FLOAT, [4], [1.0, 1.0, 
2.0, 2.0]),
-            *([helper.make_tensor("roi", TensorProto.FLOAT, [4], roi_list)] if 
with_roi else []),
-        ],
+        initializer=initializers,
         outputs=[
             helper.make_tensor_value_info("Y", TensorProto.FLOAT, [1, 3, 64, 
64]),
         ],

Reply via email to