Skip to content

Fix tf.reshape() to reject multiple -1 dims consistently - #125093

Merged
copybara-service[bot] merged 2 commits into
tensorflow:masterfrom
AnupamKumar-1:fix-reshape-multiple-unknown-dims-124877
Aug 13, 2026
Merged

copybara-service[bot] merged 2 commits into
tensorflow:masterfrom
AnupamKumar-1:fix-reshape-multiple-unknown-dims-124877

Conversation

@AnupamKumar-1

Copy link
Copy Markdown
Contributor

Fixes #124877

tf.reshape() only allows one component of shape to be -1 (per its
docstring), but the multi-unknown-dim check in shape inference was
nested inside a rank-known branch for the input tensor, so it was
skipped whenever the input's rank wasn't statically known at trace
time. Eager mode and jit_compile=True both correctly reject reshapes
with two -1 dims, but XLA autoclustering hit this skipped path and
silently returned a result instead of raising.

The check for multiple unknown dims in the target shape now runs
unconditionally in SetOutputShapeForReshape, before the input-rank
branch, so all three paths raise the same
"Only one input size may be -1, not both X and Y" error.

Added regression tests in array_ops_test.cc covering: unknown input
rank with two -1s, two -1s at non-adjacent positions, and the exact
shapes from #124877.

@google-ml-butler google-ml-butler Bot added the size:M CL Change Size: Medium label Aug 11, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the shape inference logic for the Reshape operator to eagerly detect and reject shapes containing multiple unknown dimensions (i.e., more than one -1). However, the reviewer identified a critical regression: a dimension can be marked as unknown if the target shape tensor is dynamic and not statically constant, rather than explicitly being -1. Unconditionally raising an error in this case would break valid dynamic reshapes. The feedback recommends only raising the error when the shape tensor is a known constant and adding a corresponding test case to prevent future regressions.

Comment thread tensorflow/core/ops/array_ops.cc Resolved
Comment thread tensorflow/core/ops/array_ops_test.cc Resolved
@AnupamKumar-1
AnupamKumar-1 force-pushed the fix-reshape-multiple-unknown-dims-124877 branch from a509f38 to 881a33b Compare August 11, 2026 19:29
@nithyak0204
nithyak0204 requested a review from a team August 12, 2026 04:13
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Aug 12, 2026
@nithyak0204 nithyak0204 added comp:core issues related to core part of tensorflow prtype:bugfix PR to fix a bug labels Aug 12, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Aug 12, 2026
@AnupamKumar-1
AnupamKumar-1 force-pushed the fix-reshape-multiple-unknown-dims-124877 branch from 881a33b to a33eaa6 Compare August 12, 2026 10:25
@dmiltr3
dmiltr3 self-requested a review August 12, 2026 18:23
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Aug 12, 2026
@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue Aug 12, 2026
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Aug 12, 2026
@AnupamKumar-1

Copy link
Copy Markdown
Contributor Author

@nithyak0204 , Could you please re-trigger the Windows CI check when convenient? Thanks!

@nithyak0204 nithyak0204 removed the awaiting review Pull request awaiting review label Aug 13, 2026
@copybara-service
copybara-service Bot merged commit e27935d into tensorflow:master Aug 13, 2026
16 of 17 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Aug 13, 2026
@AnupamKumar-1
AnupamKumar-1 deleted the fix-reshape-multiple-unknown-dims-124877 branch August 13, 2026 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:core issues related to core part of tensorflow prtype:bugfix PR to fix a bug ready to pull PR ready for merge process size:M CL Change Size: Medium

Projects

Status: Merged

5 participants