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
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions onnxruntime/python/tools/symbolic_shape_infer.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def __init__(self, int_max, auto_merge, guess_output_rank, verbose, prefix=""):
"Range": self._infer_Range,
"Reciprocal": self._pass_on_shape_and_type,
"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.

"ReduceProd": self._infer_ReduceProd,
"Reshape": self._infer_Reshape,
"Resize": self._infer_Resize,
Expand Down Expand Up @@ -1600,6 +1601,50 @@ def _infer_ReduceSum(self, node): # noqa: N802
)
)

def _infer_ReduceMean(self, node): # noqa: N802
keep_dims = get_attribute(node, "keepdims", 1)
opset = get_opset(self.out_mp_)

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable opset is not used.

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

if opset >= 13 and len(node.input) > 1:
tianleiwu marked this conversation as resolved.
Show resolved Hide resolved
axes = self._try_get_value(node, 1)
else:
axes = get_attribute(node, "axes")

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

rank = self._get_shape_rank(node, 0)
new_shape = self._new_symbolic_shape(rank, node)
vi.CopyFrom(
helper.make_tensor_value_info(
node.output[0],
self.known_vi_[node.input[0]].type.tensor_type.elem_type,
get_shape_from_sympy_shape(new_shape),
)
)
else:
input_shape = self._get_shape(node, 0)
assert input_shape, "ReduceMean Op: Reduction over an empty set of values yields undefined."

axes = [handle_negative_axis(a, len(input_shape)) for a in axes]
output_shape = []
for i, dim in enumerate(input_shape):
if i in axes:
if keep_dims == 1:
output_shape.append(1)
else:
continue
else:
output_shape.append(dim)
vi.CopyFrom(
helper.make_tensor_value_info(
node.output[0],
self.known_vi_[node.input[0]].type.tensor_type.elem_type,
output_shape,
)
)

def _infer_ReduceProd(self, node): # noqa: N802
axes = get_attribute(node, "axes")
keep_dims = get_attribute(node, "keepdims", 1)
Expand Down
Loading