Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SymbolicShapeInference] Support 10 Reduce* ops #22722

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

yf711
Copy link
Contributor

@yf711 yf711 commented Nov 5, 2024

Description

Extend existing Reduce_Sum func to all 10 Reduce-series ops
i.e ReduceMean def: https://onnx.ai/onnx/operators/onnx__ReduceMean.html

Op Defintions (bold: diff)
ReduceL1 Computes the L1 norm of the input tensor’s elements along the provided axes. The resulting tensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then the resulting tensor has the reduced dimension pruned. Input tensors of rank zero are valid. Reduction over an empty set of values yields 0.
ReduceL2 Computes the L2 norm of the input tensor’s elements along the provided axes. The resulting tensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then the resulting tensor has the reduced dimension pruned. Input tensors of rank zero are valid. Reduction over an empty set of values yields 0.
ReduceLogSum Computes the log sum of the input tensor’s elements along the provided axes. The resulting tensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then the resulting tensor has the reduced dimension pruned. Input tensors of rank zero are valid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or undefined otherwise.
ReduceLogSumExp Computes the log sum exponent of the input tensor’s elements along the provided axes. The resulting tensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then the resulting tensor has the reduced dimension pruned. Input tensors of rank zero are valid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or undefined otherwise.
ReduceMax Computes the max of the input tensor’s elements along the provided axes. The resulting tensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then the resulting tensor has the reduced dimension pruned. Input tensors of rank zero are valid. Reduction over an empty set of values yields minus infinity (if supported by the datatype) or the minimum value of the data type otherwise. If the input data type is Boolean, the comparison should consider False < True.
ReduceMean Computes the mean of the input tensor’s elements along the provided axes. The resulting tensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then the resulting tensor has the reduced dimension pruned. Input tensors of rank zero are valid. Reduction over an empty set of values yields undefined.
ReduceMin Computes the min of the input tensor’s elements along the provided axes. The resulting tensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then the resulting tensor has the reduced dimension pruned. Input tensors of rank zero are valid. Reduction over an empty set of values yields plus infinity (if supported by the datatype) or the maximum value of the data type otherwise. If the input data type is Boolean, the comparison should consider False < True.
ReduceProd Computes the product of the input tensor’s elements along the provided axes. The resulting tensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then the resulting tensor has the reduced dimension pruned. Input tensors of rank zero are valid. Reduction over an empty set of values yields 1.
ReduceSum Computes the sum of the input tensor’s elements along the provided axes. The resulting tensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then the resulting tensor has the reduced dimension pruned. Input tensors of rank zero are valid. Reduction over an empty set of values yields 0.
ReduceSumSquare Computes the sum square of the input tensor’s elements along the provided axes. The resulting tensor has the same rank as the input if keepdims equals 1. If keepdims equals 0, then the resulting tensor has the reduced dimension pruned. Input tensors of rank zero are valid. Reduction over an empty set of values yields 0.

Motivation and Context

#22662
This ReduceMean op is not supported by current script

Comment on lines 168 to 169
"ReduceSum": self._infer_ReduceSum,
"ReduceMean": self._infer_ReduceMean,
Copy link
Contributor

@tianleiwu tianleiwu Nov 5, 2024

Choose a reason for hiding this comment

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

ReduceSum and ReduceMean could use same shape infer function.
Also applies to ReduceMin, ReduceMax, ReduceProd etc.

vi = self.known_vi_[node.output[0]]

if axes is None:
assert keep_dims == 1, "ReduceMean Op: Cannot infer shape when axes is unknown and keepdims is not 1."
Copy link
Contributor

Choose a reason for hiding this comment

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

opset 18 add an attribute noop_with_empty_axes, shall we handle it here.
https://onnx.ai/onnx/operators/onnx__ReduceMean.html#reducemean-18

@@ -1567,46 +1575,58 @@
)
)

def _infer_ReduceSum(self, node): # noqa: N802
# This func takes care of Reduce*** ops,

Check warning

Code scanning / lintrunner

RUFF/W291 Warning

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

You can commit the suggested changes from lintrunner.

Comment on lines 1577 to +1579

def _infer_ReduceSum(self, node): # noqa: N802
# This func takes care of Reduce*** ops,
# including ReduceSum, ReduceMean, ReduceMin, ReduceMax, ReduceProd, etc
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
def _infer_ReduceSum(self, node): # noqa: N802
# This func takes care of Reduce*** ops,
# including ReduceSum, ReduceMean, ReduceMin, ReduceMax, ReduceProd, etc
# This func takes care of Reduce*** ops,
# including ReduceSum, ReduceMean, ReduceMin, ReduceMax, ReduceProd, etc

Comment on lines 1611 to 1613
assert input_shape, f"{node.op_type} Op: Reduction over an empty set of values yields undefined"

axes = [handle_negative_axis(a, len(input_shape)) for a in axes]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert input_shape, f"{node.op_type} Op: Reduction over an empty set of values yields undefined"
axes = [handle_negative_axis(a, len(input_shape)) for a in axes]
assert input_shape, f"{node.op_type} Op: Reduction over an empty set of values yields undefined"
axes = [handle_negative_axis(a, len(input_shape)) for a in axes]

keep_dims = get_attribute(node, "keepdims", 1)
if get_opset(self.out_mp_) >= 13 and len(node.input) > 1:
# ReduceSum changes axes to input[1] in opset 13
opset = get_opset(self.out_mp_)

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable opset is not used.
keep_dims = get_attribute(node, "keepdims", 1)
if get_opset(self.out_mp_) >= 13 and len(node.input) > 1:
# ReduceSum changes axes to input[1] in opset 13
opset = get_opset(self.out_mp_)

Check warning

Code scanning / lintrunner

RUFF/F841 Warning

Local variable opset is assigned to but never used.
See https://docs.astral.sh/ruff/rules/unused-variable
@yf711 yf711 changed the title Add ReduceMean shape infer to SymbolicShapeInference [SymbolicShapeInference] Support 10 Reduce* ops Nov 19, 2024
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

You can commit the suggested changes from lintrunner.

Comment on lines +1593 to +1595

if axes is None or (isinstance(axes, list) and len(axes) == 0):
# No reduction, output shape is the same as input shape
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if axes is None or (isinstance(axes, list) and len(axes) == 0):
# No reduction, output shape is the same as input shape
if axes is None or (isinstance(axes, list) and len(axes) == 0):
# No reduction, output shape is the same as input shape


vi = self.known_vi_[node.output[0]]

if axes is None or (isinstance(axes, list) and len(axes) == 0):

Check warning

Code scanning / lintrunner

RUFF/W291 Warning

@tianleiwu
Copy link
Contributor

LGTM. Please use "lintrunner -a" to format the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants