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 642320196f [Relax][ONNX] Add CumProd operator support (#20937)
642320196f is described below

commit 642320196f5d08a2e734e2c3cdeac76c96fce7a1
Author: Anirudh Mittal <[email protected]>
AuthorDate: Fri Nov 13 03:06:40 2026 +0530

    [Relax][ONNX] Add CumProd operator support (#20937)
    
    This commit adds support for the ONNX CumProd operator in the Relax ONNX
    frontend.
    
    Key changes:
    - In `python/tvm/relax/frontend/onnx/onnx_frontend.py`, implemented the
    `CumProd` converter class (`_impl_v1`) supporting axis selection,
    exclusive cumprod, and reverse cumprod via `topi.flip`.
    - Registered 'CumProd' in `_get_convert_map()`.
    - In `tests/python/relax/test_frontend_onnx.py`, added comprehensive
    unit tests covering all combinations of reverse and exclusive, 1-D int32
    axis initializers, non-constant axis error handling, and axis shape
    validation.
    
    ---------
    
    Co-authored-by: Anirudh-65 <[email protected]>
---
 python/tvm/relax/frontend/onnx/onnx_frontend.py |  45 +++++++
 tests/python/relax/test_frontend_onnx.py        | 148 ++++++++++++++++++++++++
 2 files changed, 193 insertions(+)

diff --git a/python/tvm/relax/frontend/onnx/onnx_frontend.py 
b/python/tvm/relax/frontend/onnx/onnx_frontend.py
index badb3f9975..2284b4aa58 100644
--- a/python/tvm/relax/frontend/onnx/onnx_frontend.py
+++ b/python/tvm/relax/frontend/onnx/onnx_frontend.py
@@ -2386,6 +2386,50 @@ class CumSum(OnnxOpConverter):
         return data
 
 
+class CumProd(OnnxOpConverter):
+    """Converts an onnx CumProd node into an equivalent Relax expression."""
+
+    @classmethod
+    def _impl_v1(cls, bb, inputs, attr, params):
+        data = inputs[0]
+        exclusive = attr.get("exclusive", 0) != 0
+
+        axis = 0
+        if len(inputs) > 1 and inputs[1] is not None:
+            axis_input = get_constant(inputs[1], params)
+            if isinstance(axis_input, tvm_tirx.ir.GenericConst):
+                axis_data = axis_input.value.numpy()
+                if axis_data.ndim == 0:
+                    axis = int(axis_data.item())
+                elif axis_data.ndim == 1 and axis_data.shape[0] == 1:
+                    axis = int(axis_data.item())
+                else:
+                    raise ValueError(
+                        "CumProd axis input must be a scalar (0-D) or a 
single-element 1-D tensor, "
+                        f"got shape {axis_data.shape}"
+                    )
+            elif isinstance(axis_input, relax.Var):
+                axis_shape = axis_input.ty.shape if hasattr(axis_input.ty, 
"shape") else None
+                raise ValueError(
+                    "CumProd with non-constant axis input is not supported 
yet. "
+                    "ONNX permits runtime axis tensors, but Relax/TE currently 
requires a compile-time "
+                    f"constant axis for cumprod/flip. Got axis shape 
{axis_shape}"
+                )
+            else:
+                raise TypeError("CumProd axis input must be a Constant or Var")
+
+        if attr.get("reverse", 0) != 0:
+            data = bb.emit_te(topi.flip, data, axis=axis)
+
+        data = relax.op.cumprod(data, axis, exclusive=exclusive)
+        data = bb.normalize(data)
+
+        if attr.get("reverse", 0) != 0:
+            data = bb.emit_te(topi.flip, data, axis=axis)
+
+        return data
+
+
 class Squeeze(OnnxOpConverter):
     """Converts an onnx Squeeze node into an equivalent Relax expression."""
 
@@ -6228,6 +6272,7 @@ def _get_convert_map():
         "Clip": Clip,
         "Shape": Shape,
         "Pow": Pow,
+        "CumProd": CumProd,
         "CumSum": CumSum,
         "Squeeze": Squeeze,
         "Constant": Constant,
diff --git a/tests/python/relax/test_frontend_onnx.py 
b/tests/python/relax/test_frontend_onnx.py
index 5e8c408d61..db35224067 100644
--- a/tests/python/relax/test_frontend_onnx.py
+++ b/tests/python/relax/test_frontend_onnx.py
@@ -5373,6 +5373,154 @@ def test_cumsum_axis_shape_validation():
         from_onnx(model, opset=14, keep_params_in_input=True)
 
 
+def _np_cumprod(x, axis=0, reverse=False, exclusive=False):
+    if reverse:
+        x = np.flip(x, axis=axis)
+    if exclusive:
+        slices = [slice(None)] * x.ndim
+        pad_shape = list(x.shape)
+        pad_shape[axis] = 1
+        ones = np.ones(pad_shape, dtype=x.dtype)
+        slices[axis] = slice(0, -1)
+        x_shifted = np.concatenate([ones, x[tuple(slices)]], axis=axis)
+        res = np.cumprod(x_shifted, axis=axis)
+    else:
+        res = np.cumprod(x, axis=axis)
+    if reverse:
+        res = np.flip(res, axis=axis)
+    return res
+
+
+def _verify_cumprod(model, inputs, expected, opset=26, rtol=1e-5, atol=1e-5):
+    # ONNX Runtime 1.23.2 in TVM CI does not support opset 26 models,
+    # so we compile the imported Relax model and verify against NumPy 
reference.
+    tvm_model = from_onnx(model, opset=opset, keep_params_in_input=True)
+    tvm_model = relax.transform.DecomposeOpsForInference()(tvm_model)
+    tvm_model = relax.transform.LegalizeOps()(tvm_model)
+    tvm_model, params = relax.frontend.detach_params(tvm_model)
+
+    with tvm_tirx.ir.transform.PassContext(opt_level=3):
+        ex = tvm.compile(tvm_model, target="llvm")
+        vm = relax.VirtualMachine(ex, tvm_tirx.cpu())
+
+    input_list = [inputs[key.name] for key in tvm_model["main"].params if 
key.name in inputs]
+    if params:
+        input_list += params["main"]
+
+    vm.set_input("main", *input_list)
+    vm.invoke_stateful("main")
+    tvm_output = vm.get_outputs("main").numpy()
+
+    np.testing.assert_allclose(tvm_output, expected, rtol=rtol, atol=atol)
+
+
[email protected]("reverse", [True, False])
[email protected]("exclusive", [True, False])
+def test_cumprod(reverse, exclusive):
+    cumprod_node = helper.make_node(
+        "CumProd", ["x", "axis"], ["y"], reverse=reverse, exclusive=exclusive
+    )
+    shape = [4, 4]
+    graph = helper.make_graph(
+        [cumprod_node],
+        "cumprod_test",
+        inputs=[
+            helper.make_tensor_value_info("x", TensorProto.FLOAT, shape),
+        ],
+        initializer=[helper.make_tensor("axis", TensorProto.INT64, (), [1])],
+        outputs=[helper.make_tensor_value_info("y", TensorProto.FLOAT, shape)],
+    )
+
+    model = helper.make_model(
+        graph, producer_name="cumprod_test", 
opset_imports=[helper.make_opsetid("", 26)]
+    )
+    x = np.array(
+        [
+            [1.0, 1.2, 0.9, 1.1],
+            [0.8, 1.5, 0.7, 1.3],
+            [1.1, 0.9, 1.2, 0.8],
+            [1.4, 0.6, 1.0, 1.1],
+        ],
+        dtype=np.float32,
+    )
+    expected = _np_cumprod(x, axis=1, reverse=reverse, exclusive=exclusive)
+    _verify_cumprod(model, inputs={"x": x}, expected=expected)
+
+
+def test_cumprod_int32_1d_axis_initializer():
+    input_shape = [2, 3]
+
+    graph = helper.make_graph(
+        [
+            helper.make_node("CumProd", inputs=["X", "axis"], outputs=["Y"]),
+        ],
+        "cumprod_graph",
+        inputs=[
+            helper.make_tensor_value_info("X", onnx.TensorProto.DOUBLE, 
input_shape),
+        ],
+        initializer=[helper.make_tensor("axis", onnx.TensorProto.INT32, [1], 
[0])],
+        outputs=[helper.make_tensor_value_info("Y", onnx.TensorProto.DOUBLE, 
input_shape)],
+    )
+
+    model = helper.make_model(
+        graph, producer_name="cumprod_graph", 
opset_imports=[helper.make_opsetid("", 26)]
+    )
+    x = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float64)
+    expected = _np_cumprod(x, axis=0)
+    _verify_cumprod(model, inputs={"X": x}, expected=expected)
+
+
+def test_cumprod_dynamic_axis_not_supported():
+    input_shape = [2, 3]
+
+    graph = helper.make_graph(
+        [
+            helper.make_node("CumProd", inputs=["X", "axis"], outputs=["Y"]),
+        ],
+        "cumprod_dynamic_axis_graph",
+        inputs=[
+            helper.make_tensor_value_info("X", onnx.TensorProto.DOUBLE, 
input_shape),
+            helper.make_tensor_value_info("axis", onnx.TensorProto.INT32, [1], 
"axis"),
+        ],
+        outputs=[helper.make_tensor_value_info("Y", onnx.TensorProto.DOUBLE, 
input_shape)],
+    )
+
+    model = helper.make_model(
+        graph,
+        producer_name="cumprod_dynamic_axis_graph",
+        opset_imports=[helper.make_opsetid("", 26)],
+    )
+    with pytest.raises(ValueError, match="non-constant axis input is not 
supported"):
+        from_onnx(model, opset=26, keep_params_in_input=True)
+
+
+def test_cumprod_axis_shape_validation():
+    input_shape = [2, 3]
+
+    graph = helper.make_graph(
+        [
+            helper.make_node("CumProd", inputs=["X", "axis"], outputs=["Y"]),
+        ],
+        "cumprod_invalid_axis_shape_graph",
+        inputs=[
+            helper.make_tensor_value_info("X", onnx.TensorProto.DOUBLE, 
input_shape),
+        ],
+        initializer=[helper.make_tensor("axis", onnx.TensorProto.INT64, [2], 
[0, 1])],
+        outputs=[helper.make_tensor_value_info("Y", onnx.TensorProto.DOUBLE, 
input_shape)],
+    )
+
+    model = helper.make_model(
+        graph,
+        producer_name="cumprod_invalid_axis_shape_graph",
+        opset_imports=[helper.make_opsetid("", 26)],
+    )
+    with pytest.raises(
+        ValueError,
+        match=r"axis input must be a scalar \(0-D\) or a single-element 1-D 
tensor",
+    ):
+        from_onnx(model, opset=26, keep_params_in_input=True)
+
+
 def test_squeeze():
     def verify_squeeze(axis, expected):
         if axis:

Reply via email to