fix(optimizer): prevent Shape/Gather from folding to constants when shape has negative (unknown) dims#2964
Draft
justinchuby with Copilot wants to merge 2 commits into
Draft
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
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.