Skip to content

[Reland] Cumulative Product Operator#7400

Merged
titaiwangms merged 9 commits intoonnx:mainfrom
titaiwangms:titaiwang/finish_cumulative_product_op
Dec 16, 2025
Merged

[Reland] Cumulative Product Operator#7400
titaiwangms merged 9 commits intoonnx:mainfrom
titaiwangms:titaiwang/finish_cumulative_product_op

Conversation

@titaiwangms
Copy link
Copy Markdown
Contributor

Cumulative Product Operator

Description of the Operator

The cumulative product operator cumprod performs cumulative product of the input elements along the given axis. By default, it will do the product inclusively meaning the first element is copied as is. Through an exclusive attribute, this behavior can change to exclude the first element. It can also perform product in the opposite direction of the axis. For that, set reverse attribute to 1. Note, the behavior of the cumprod operator is built to mirror exactly what the existing cumulative sum operator cumsum does as of operator version 14.

The documentation of the existing cumprod operator in NumPy is available here.

Example:

input_x = [1, 2, 3]
axis=0
output = [1, 2, 6]
exclusive=1
output = [0, 1, 2]
exclusive=0
reverse=1
output = [6, 6, 3]
exclusive=1
reverse=1
output = [6, 3, 0]

Reference Implementation

The implemenation below provides a simple example of how to use the CumProd operator in ONNX. It creates a single node model with a CumProd node and saves it to a file. It then creates a reference evaluator for the model and runs inference on it. It compares the output of the model with the output of the NumPy cumprod function.

import numpy as np
from onnx import helper, TensorProto, save_model
from onnx.reference import ReferenceEvaluator

# Define inputs
x = helper.make_tensor_value_info('x', TensorProto.FLOAT, [3, 4])
axis = helper.make_tensor_value_info('axis', TensorProto.INT32, [])

# Define outputs  
y = helper.make_tensor_value_info('y', TensorProto.FLOAT, [3, 4])

# Create the CumProd node
cumprod_node = helper.make_node(
    'CumProd',
    inputs=['x', 'axis'],
    outputs=['y'],
    name='cumprod_node'
)

# Create the graph
graph = helper.make_graph(
    nodes=[cumprod_node],
    name='CumProdExample',
    inputs=[x, axis],
    outputs=[y]
)

# Create the model
model = helper.make_model(graph, producer_name='cumprod-example')
model.opset_import[0].version = 23
model.ir_version = 10

# Save the model
save_model(model, "temp_cumprod.onnx")

# Create reference evaluator for the model
ref_evaluator = ReferenceEvaluator("temp_cumprod.onnx")

# Prepare inputs
x_input = np.array([[1.0, 2.0, 3.0, 4.0],
                    [2.0, 2.0, 2.0, 2.0],
                    [1.0, 3.0, 1.0, 3.0]], dtype=np.float32)
axis_input = np.array(1, dtype=np.int32)  # Cumulative product along axis 1

# Run inference
outputs = ref_evaluator.run(None, {
    'x': x_input,
    'axis': axis_input
})

print(f"Input:\n{x_input}")
print(f"Axis: {axis_input}")
print(f"CumProd Output:\n{outputs[0]}")

# Compare with NumPy
numpy_result = np.cumprod(x_input, axis=1)
print(f"NumPy cumprod:\n{numpy_result}")
print(f"Results match: {np.allclose(outputs[0], numpy_result)}")

Credits to @celmore25 (#7075)

Signed-off-by: Ti-Tai Wang <titaiwang@microsoft.com>
Signed-off-by: Ti-Tai Wang <titaiwang@microsoft.com>
Signed-off-by: Ti-Tai Wang <titaiwang@microsoft.com>
Signed-off-by: Ti-Tai Wang <titaiwang@microsoft.com>
@titaiwangms titaiwangms requested review from a team as code owners October 16, 2025 22:53
@github-project-automation github-project-automation Bot moved this to In progress in PR Tracker Oct 16, 2025
@titaiwangms titaiwangms added the topic: operator Issues related to ONNX operators label Oct 16, 2025
@titaiwangms titaiwangms added this to the 1.20 milestone Oct 16, 2025
@titaiwangms
Copy link
Copy Markdown
Contributor Author

@gramalingam @justinchuby Should I register the new op under 24? I am under the impression that 1.20 is the first release that we are not introducing new opset version.

Comment thread onnx/defs/math/defs.cc Fixed
Signed-off-by: Ti-Tai Wang <titaiwang@microsoft.com>
@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 16, 2025

Codecov Report

❌ Patch coverage is 27.72277% with 73 lines in your changes missing coverage. Please review.
✅ Project coverage is 55.20%. Comparing base (34462d4) to head (cebee93).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
onnx/backend/test/case/node/cumprod.py 0.00% 69 Missing ⚠️
onnx/reference/ops/op_cum_prod.py 91.30% 1 Missing and 1 partial ⚠️
onnx/reference/ops/op_cum_sum.py 33.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7400      +/-   ##
==========================================
- Coverage   55.28%   55.20%   -0.08%     
==========================================
  Files         513      515       +2     
  Lines       32228    32324      +96     
  Branches     2895     2897       +2     
==========================================
+ Hits        17816    17844      +28     
- Misses      13619    13688      +69     
+ Partials      793      792       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment thread onnx/defs/operator_sets.h Outdated
@titaiwangms titaiwangms modified the milestones: 1.20, 1.21 Oct 17, 2025
Signed-off-by: Ti-Tai Wang <titaiwang@microsoft.com>
Signed-off-by: Ti-Tai Wang <titaiwang@microsoft.com>
Comment thread onnx/defs/math/defs.cc
Comment on lines +1916 to +1917
ONNX_OPERATOR_SET_SCHEMA(
CumProd,

Check notice

Code scanning / CodeQL

Unused static variable Note

Static variable dbg_count_check_Onnx_26_verCumProd is never read.
Signed-off-by: Ti-Tai Wang <titaiwang@microsoft.com>
@titaiwangms titaiwangms enabled auto-merge (squash) December 15, 2025 23:37
Copy link
Copy Markdown
Member

@justinchuby justinchuby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@titaiwangms titaiwangms merged commit a85600e into onnx:main Dec 16, 2025
41 of 42 checks passed
@github-project-automation github-project-automation Bot moved this from In progress to Done in PR Tracker Dec 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic: operator Issues related to ONNX operators

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants