-
Notifications
You must be signed in to change notification settings - Fork 3k
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
"ReduceProd": self._infer_ReduceProd, | ||
"Reshape": self._infer_Reshape, | ||
"Resize": self._infer_Resize, | ||
|
@@ -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." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. opset 18 add an attribute |
||
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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReduceSum
andReduceMean
could use same shape infer function.Also applies to
ReduceMin
,ReduceMax
,ReduceProd
etc.