fix(optimizer): prevent Shape/Gather from folding to constants when shape has negative (unknown) dims#2964
Open
justinchuby with Copilot wants to merge 2 commits into
Open
fix(optimizer): prevent Shape/Gather from folding to constants when shape has negative (unknown) dims#2964justinchuby with Copilot wants to merge 2 commits into
justinchuby with Copilot wants to merge 2 commits into
Conversation
…re negative (-1) When a tensor shape has `-1` as an integer (representing an unknown/dynamic dimension), the `Shape` and `Gather` handlers were incorrectly folding to constants containing `-1`. This caused downstream `Reshape` ops to receive multiple `-1` values (e.g., `[-1, -1]`), which is invalid in ONNX (at most one `-1` is allowed in a Reshape shape). The fix adds a non-negativity check: only fold `Shape`/`Gather` to a constant when all dimension values are non-negative integers (i.e., all are known static positive values). Fixes #2963
Copilot
AI
changed the title
[WIP] Fix invalid reshape issue after FoldConstantsPass
fix(optimizer): prevent Shape/Gather from folding to constants when shape has negative (unknown) dims
Jul 14, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the optimizer’s FoldConstantsPass against ONNX-IR shapes that encode unknown dimensions as the integer -1, preventing invalid constant-folding that can propagate multiple -1 values into Reshape shapes.
Changes:
- Add a non-negativity guard to
Shapeconstant folding so-1(unknown) dims do not become literal constants. - Add the same non-negativity guard to the
Gather-from-symbolic-shape folding path to avoid folding negative gathered dims. - Add a regression test covering the
Shape→Gather→Concat→Reshapepattern when the input shape contains integer-1dims.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| onnxscript/optimizer/_constant_folding.py | Prevents folding Shape/Gather results to constants when any folded dimension is negative (unknown). |
| onnxscript/optimizer/_constant_folding_test.py | Adds regression coverage for the -1-dim folding bug scenario involving Shape/Gather/Reshape. |
Comment on lines
+546
to
547
| if all(isinstance(d, int) and d >= 0 for d in shape_slice): | ||
| return op.Constant(value_ints=ir.AttrInt64s("value_ints", list(shape_slice))) |
Comment on lines
+709
to
+722
| model_text = """ | ||
| <ir_version: 7, opset_import: [ "" : 17]> | ||
| agraph (float[N, 512, 4, 1] x) => (float[?] val_5) | ||
| { | ||
| shape = Shape(x) | ||
| index_0 = Constant <value_int=0> () | ||
| gathered = Gather <axis=0> (shape, index_0) | ||
| axes = Constant <value_ints=[0]> () | ||
| unsqueezed = Unsqueeze(gathered, axes) | ||
| neg_one = Constant <value_ints=[-1]> () | ||
| concat_shape = Concat <axis=0> (unsqueezed, neg_one) | ||
| val_5 = Reshape(x, concat_shape) | ||
| } | ||
| """ |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2964 +/- ##
==========================================
- Coverage 72.61% 72.61% -0.01%
==========================================
Files 263 263
Lines 32034 32048 +14
Branches 3013 3014 +1
==========================================
+ Hits 23263 23273 +10
- Misses 7748 7751 +3
- Partials 1023 1024 +1 ☔ View full report in Codecov by Harness. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
FoldConstantsPassincorrectly foldsShape→Gather→Concat→Reshapepatterns when the input tensor has unknown dimensions stored as integer-1in the IR (as opposed toir.SymbolicDim). Since-1is anint, the all-integer check passes, causingShapeto fold intoConstant(value_ints=[-1, 512, 1, ...]).Gatherthen returns a constant-1, which propagates into theReshapeshape — producing an invalid shape like[-1, -1](ONNX allows at most one-1in a Reshape shape).Changes
Shapehandler: add non-negativity guard — only fold to a constant when all shape dims are known positive integers:Gatherhandler: same guard on the symbolic-shape gather path, preventing folding when any gathered dimension value is negative.test_shape_gather_concat_reshape_with_neg1_dim— verifies thatShapeis preserved andReshapenever receives multiple-1values when the input shape contains-1integer dims.The root cause: ONNX IR deserializes unknown dims (
dim_value=-1in the proto) as integer-1, notir.SymbolicDim. The existingSymbolicDimpath was safe; the-1-as-int path was not.