Skip to content

Fix eager tf.while_loop corrupting shape for single-var loop - #125616

Merged
copybara-service[bot] merged 2 commits into
tensorflow:masterfrom
AnupamKumar-1:fix-while-loop-eager-single-var-indexing
Aug 31, 2026
Merged

copybara-service[bot] merged 2 commits into
tensorflow:masterfrom
AnupamKumar-1:fix-while-loop-eager-single-var-indexing

Conversation

@AnupamKumar-1

Copy link
Copy Markdown
Contributor

Fixes #125538.

tf.while_loop could corrupt the shape of a single-element loop_vars list when the body returned a bare Tensor, e.g. tf.while_loop(cond, lambda x: x + 1, [x]). In eager mode, the Tensor could be incorrectly iterated over between iterations, changing its shape from (1, 1) to (1,) and causing a later indexing error.

Root cause - The eager execution path did not re-pack a bare Tensor body result when called through the v2 tf.while_loop API. As a result, the loop variable lost its original list structure and was implicitly unpacked using Python iteration.

Graph mode and XLA were unaffected because they preserve the structured loop-variable signature during tracing.

Fix - Always re-pack bare Tensor/TensorArray body results using the original top-level container type (list or tuple). This preserves loop-variable structure and prevents shape corruption across iterations.

Malformed body functions now also fail fast with a clear structure-mismatch error.

Testing - Added a regression test covering a single loop variable with a bare-Tensor body.

@google-ml-butler google-ml-butler Bot added the size:S CL Change Size: Small label Aug 19, 2026
@google-ml-butler
google-ml-butler Bot requested a review from cantonios August 19, 2026 05:54
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Aug 19, 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 updates the eager execution path of while_loop to preserve the original container type (list or tuple) of the loop variables when they are packed, and adds a corresponding unit test. The review feedback correctly identifies a potential issue where using isinstance to determine the container type could fail or corrupt structures for subclasses like namedtuple, and suggests checking the exact type instead.

Comment thread tensorflow/python/ops/while_loop.py Resolved
@nithyak0204
nithyak0204 requested a review from a team August 19, 2026 08:36
@nithyak0204 nithyak0204 added comp:ops OPs related issues prtype:bugfix PR to fix a bug labels Aug 19, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Aug 19, 2026
@dmiltr3
dmiltr3 self-requested a review August 19, 2026 15:47
@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 19, 2026
@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue Aug 19, 2026
@nithyak0204 nithyak0204 removed the awaiting review Pull request awaiting review label Aug 20, 2026
@nithyak0204 nithyak0204 added ready to pull PR ready for merge process and removed ready to pull PR ready for merge process labels Aug 27, 2026
@copybara-service
copybara-service Bot merged commit 517a9b1 into tensorflow:master Aug 31, 2026
20 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Aug 31, 2026
@AnupamKumar-1
AnupamKumar-1 deleted the fix-while-loop-eager-single-var-indexing branch August 31, 2026 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:ops OPs related issues kokoro:force-run Tests on submitted change prtype:bugfix PR to fix a bug ready to pull PR ready for merge process size:S CL Change Size: Small

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

tf.while_loop condition with tuple indexing x[(0,0)] fails in eager but works under XLA [Recent/Qwen]

4 participants