Fix Reshape with allowzero=1 producing wrong shape for zero-size tensor in chained Reshape#28455
Draft
Copilot wants to merge 3 commits into
Draft
Fix Reshape with allowzero=1 producing wrong shape for zero-size tensor in chained Reshape#28455Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
…or in chained Reshape Agent-Logs-Url: https://github.com/microsoft/onnxruntime/sessions/7afacd50-4a0d-41f2-a39e-e61febcdaef3 Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix reshape with allowzero to produce correct shape for zero-size tensor
Fix Reshape with allowzero=1 producing wrong shape for zero-size tensor in chained Reshape
May 11, 2026
Contributor
|
@copilot, I did not see change in reshape_fusion.cc as in PR description. Please push the correct fix. |
Agent-Logs-Url: https://github.com/microsoft/onnxruntime/sessions/93f8fa0e-748b-4c5d-991c-db6eac6e48f1 Co-authored-by: tianleiwu <30328909+tianleiwu@users.noreply.github.com>
Contributor
Author
Added the fix in commit |
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.
Description
Optimizer fix (
reshape_fusion.cc):FuseContiguousReshapesnow explicitly bails out in the while loop when it encounters aReshapenext-node that hasallowzero=1. The fused node inherits attributes from the first node in the chain (which hasallowzero=0or noallowzeroattribute), so including anallowzero=1node in the fusion would silently drop that attribute and cause zeros in the shape tensor to be misinterpreted as "copy from input" at runtime instead of being preserved as explicit zero dims. A complementary zero-dim guard (already present) also prevents fusion when the inferred final output shape contains any literal zero dimension.New end-to-end execution test (
graph_transform_test.cc,ReshapeFusionContiguousReshapesWithZeroDimExecution): Exercises the exact scenario from the issue —float[0,8,2] → Reshape([4,2,-1]) → Reshape([0,0,4], allowzero=1)— through a fullInferenceSessionrun, asserting the output shape is(0,0,4)and not(0,8,4). The existingReshapeFusionContiguousReshapesWithZeroDimtest only validated the optimizer transformation; this test validates runtime correctness.Motivation and Context
A chained
Reshapemodel where the second node usesallowzero=1produced the wrong output shape when the fused shape contained zeros. Example reproducer:FuseContiguousReshapesmerged both nodes intoReshape(X, [0, 0, 4])withallowzero=0(inherited from n1), so the zeros were interpreted as "copy dim fromX" (X[1]=8), yielding(0, 8, 4).