-
Notifications
You must be signed in to change notification settings - Fork 98
Fix Issue #2736 #2769
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
base: main
Are you sure you want to change the base?
Fix Issue #2736 #2769
Conversation
|
@microsoft-github-policy-service agree |
| for i, dim in enumerate(shape_value.dims): | ||
| if isinstance(dim, ir.SymbolicDim) and dim.value is None: | ||
| dynamic_indices.append(i) | ||
|
|
Check warning
Code scanning / lintrunner
RUFF/W293 Warning
See https://docs.astral.sh/ruff/rules/blank-line-with-whitespace
| for i, dim in enumerate(shape_value.dims): | ||
| if isinstance(dim, ir.SymbolicDim) and dim.value is None: | ||
| dynamic_indices.append(i) | ||
|
|
Check warning
Code scanning / lintrunner
EDITORCONFIG-CHECKER/editorconfig Warning
| if not isinstance(dim, int): | ||
| all_others_static = False | ||
| break | ||
|
|
Check warning
Code scanning / lintrunner
RUFF/W293 Warning
See https://docs.astral.sh/ruff/rules/blank-line-with-whitespace
| if not isinstance(dim, int): | ||
| all_others_static = False | ||
| break | ||
|
|
Check warning
Code scanning / lintrunner
EDITORCONFIG-CHECKER/editorconfig Warning
| new_shape_list.append(-1) | ||
| else: | ||
| new_shape_list.append(dim) | ||
|
|
Check warning
Code scanning / lintrunner
RUFF/W293 Warning
See https://docs.astral.sh/ruff/rules/blank-line-with-whitespace
| len(nodes), 0, | ||
| f"Expected no {op_type} nodes after optimization, found {len(nodes)}" | ||
| ) | ||
|
|
Check warning
Code scanning / lintrunner
EDITORCONFIG-CHECKER/editorconfig Warning
| len(reshape_nodes) == 1 or len(identity_nodes) == 1, | ||
| f"Expected one Reshape or Identity node, found {len(reshape_nodes)} Reshape and {len(identity_nodes)} Identity" | ||
| ) | ||
|
|
Check warning
Code scanning / lintrunner
RUFF/W293 Warning
See https://docs.astral.sh/ruff/rules/blank-line-with-whitespace
| len(reshape_nodes) == 1 or len(identity_nodes) == 1, | ||
| f"Expected one Reshape or Identity node, found {len(reshape_nodes)} Reshape and {len(identity_nodes)} Identity" | ||
| ) | ||
|
|
Check warning
Code scanning / lintrunner
EDITORCONFIG-CHECKER/editorconfig Warning
| shape_input = reshape_node.inputs[1] | ||
| self.assertIsNotNone(shape_input, "Reshape shape input should not be None") | ||
| self.assertIsNotNone(shape_input.const_value, "Shape input should be a constant") | ||
|
|
Check warning
Code scanning / lintrunner
RUFF/W293 Warning
See https://docs.astral.sh/ruff/rules/blank-line-with-whitespace
| shape_input = reshape_node.inputs[1] | ||
| self.assertIsNotNone(shape_input, "Reshape shape input should not be None") | ||
| self.assertIsNotNone(shape_input.const_value, "Shape input should be a constant") | ||
|
|
Check warning
Code scanning / lintrunner
EDITORCONFIG-CHECKER/editorconfig Warning
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2769 +/- ##
==========================================
- Coverage 70.21% 70.15% -0.06%
==========================================
Files 228 228
Lines 27316 27354 +38
Branches 2769 2780 +11
==========================================
+ Hits 19179 19191 +12
- Misses 7188 7211 +23
- Partials 949 952 +3 ☔ View full report in Codecov by Sentry. |
Fix Issue #2736
This change extends constant folding for
Reshapeto eliminate dynamic shapesubgraphs of the form
Shape → Gather → Concat → Reshapewhen the target shapecontains exactly one dynamic dimension. The optimizer rewrites the reshape to
use a static
-1dimension, relying on ONNX Reshape semantics to infer thedynamic value from the input element count. This removes runtime shape
evaluation and enables further canonicalization.
Key Changes
onnxscript/optimizer/_constant_folding.pyUpdated the
reshapeoptimizer to detect reshape targets with exactly onedynamic dimension and replace the dynamic shape computation with a static
shape containing
-1.onnxscript/optimizer/_constant_folding_test.pyAdded a unit test to verify that dynamic shape subgraphs (
Shape,Gather,Concat) are eliminated and that the reshape is either rewritten with-1or folded away entirely.