Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@celmore25
Copy link

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)}")

@celmore25 celmore25 requested review from a team as code owners June 24, 2025 04:46
@github-project-automation github-project-automation bot moved this to In progress in PR Tracker Jun 24, 2025
* added and registered documenation for cumulative product operator contract
* added a reference implementation of cumulative product similar to cumulative summation
* added unit tests for cumulative product operations

Signed-off-by: Clay Elmore <[email protected]>
@codecov
Copy link

codecov bot commented Jun 24, 2025

Codecov Report

Attention: Patch coverage is 11.00000% with 89 lines in your changes missing coverage. Please review.

Project coverage is 56.20%. Comparing base (625ab7a) to head (fae46e4).
Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
onnx/backend/test/case/node/cumprod.py 0.00% 69 Missing ⚠️
onnx/reference/ops/op_cum_prod.py 20.00% 20 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7075      +/-   ##
==========================================
- Coverage   56.40%   56.20%   -0.21%     
==========================================
  Files         510      512       +2     
  Lines       32721    32821     +100     
  Branches     3093     3099       +6     
==========================================
- Hits        18457    18446      -11     
- Misses      13410    13517     +107     
- Partials      854      858       +4     

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

Comment on lines +2017 to +2018
ONNX_OPERATOR_SET_SCHEMA(
CumProd,

Check notice

Code scanning / CodeQL

Unused static variable Note

Static variable dbg_count_check_Onnx_23_verCumProd is never read.
from onnx.reference.ops.op_conv_transpose import ConvTranspose
from onnx.reference.ops.op_cos import Cos
from onnx.reference.ops.op_cosh import Cosh
from onnx.reference.ops.op_cum_prod import CumProd

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'CumProd' is not used.
@justinchuby justinchuby added the topic: operator Issues related to ONNX operators label Jun 24, 2025
@celmore25 celmore25 changed the title feat: add cumulative product operator Cumulative Product Operator Jun 25, 2025
@celmore25
Copy link
Author

Related to the request in #6590

@justinchuby
Copy link
Member

@onnx/sig-operators-approvers

@justinchuby justinchuby added this to the 1.19 milestone Jun 26, 2025
@justinchuby
Copy link
Member

Thank you! I think we can wait for #7030 to setup opset24 as this operator will need to be included in a new opset (24)

@justinchuby
Copy link
Member

@celmore25 could you merge from main and move the operator to opset 24? Thank you

@justinchuby justinchuby modified the milestones: 1.19, 1.20 Jul 16, 2025
Copy link
Contributor

@cbourjau cbourjau left a comment

Choose a reason for hiding this comment

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

It would be great to push this over the finish line. Could you add some tests please?

.TypeConstraint(
"T",
OpSchema::numeric_types_for_math_reduction_ir4(),
"Constrain input and output types to high-precision numeric tensors.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I'd say that float16 is not "high-precision".

@github-project-automation github-project-automation bot moved this from In progress to Review in progress in PR Tracker Jul 23, 2025
@justinchuby
Copy link
Member

@celmore25 Hi! We have a branch cut by the end of this month. It would be great if we can merge this change before then. Did you get a chance to take a look at the comments? Thanks!

@justinchuby justinchuby added the review needed: operators approvers Require reviews from members of operators-approvers label Aug 28, 2025
@justinchuby
Copy link
Member

@celmore25 would you like to rebase from main? Thank you

@titaiwangms
Copy link
Contributor

@justinchuby @celmore25 We can make this in 1.20.

@justinchuby
Copy link
Member

@titaiwangms i can work with you to get this in. Maybe in a week

@titaiwangms
Copy link
Contributor

#7400

@github-project-automation github-project-automation bot moved this from Review in progress to Done in PR Tracker Oct 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review needed: operators approvers Require reviews from members of operators-approvers topic: operator Issues related to ONNX operators

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants