Skip to content

fix(transforms): convert view_copy to unsqueeze when the added dim is 0#20603

Open
durvesh1992 wants to merge 1 commit into
pytorch:mainfrom
durvesh1992:fix/view-copy-unsqueeze-dim-0
Open

fix(transforms): convert view_copy to unsqueeze when the added dim is 0#20603
durvesh1992 wants to merge 1 commit into
pytorch:mainfrom
durvesh1992:fix/view-copy-unsqueeze-dim-0

Conversation

@durvesh1992

Copy link
Copy Markdown

Summary

ViewCopyToSqueezeUnsqueezePass rewrites a view_copy that adds a size-1 dimension into an unsqueeze_copy. It decides whether to do the rewrite with:

unsqueeze_dim = self.find_unsqueeze_dim(input_shape, view_shape)
if unsqueeze_dim:
    ...

find_unsqueeze_dim returns the index of the added size-1 dimension, and None when the shapes don't match an unsqueeze. When the size-1 dim is added at the front, the returned index is 0 — which is falsy — so if unsqueeze_dim: skips the rewrite and the view_copy is left in place for that case (e.g. [3, 4] -> [1, 3, 4]).

The fix is to guard on is not None:

if unsqueeze_dim is not None:

(The squeeze branch is unaffected: find_squeeze_dims returns a non-empty list or None, so its if squeeze_dims: check is correct.)

Test plan

Adds backends/transforms/test/test_view_copy_to_squeeze_unsqueeze.py with three cases: unsqueeze at dim 0 (regression for this bug), unsqueeze at dim 1, and squeeze.

  • Before the fix, test_unsqueeze_at_dim_0 fails — the view_copy is not converted (graph still contains aten.view_copy).
  • After the fix, all three tests pass.

Run:

python -m unittest backends.transforms.test.test_view_copy_to_squeeze_unsqueeze -v

ViewCopyToSqueezeUnsqueezePass used `if unsqueeze_dim:` to decide
whether find_unsqueeze_dim found an added size-1 dimension. find_unsqueeze_dim
returns the index of the added dim and None when the shapes don't match an
unsqueeze. When the size-1 dim is added at the front the index is 0, which is
falsy, so the view_copy -> unsqueeze_copy rewrite was silently skipped for
that case. Guard with `is not None` instead.

Add a unit test covering unsqueeze at dim 0 (regression), unsqueeze at dim 1,
and the squeeze case.
@durvesh1992 durvesh1992 requested a review from kimishpatel as a code owner June 29, 2026 20:14
@pytorch-bot

pytorch-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/20603

Note: Links to docs will display an error until the docs builds have been completed.

⚠️ 11 Awaiting Approval

As of commit 8ccf67a with merge base b331ebd (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 29, 2026
@linux-foundation-easycla

Copy link
Copy Markdown

CLA Missing ID

  • ❌ The email address for the commit (8ccf67a) is not linked to the GitHub account, preventing the EasyCLA check. Consult this Help Article and GitHub Help to resolve. (To view the commit's email address, add .patch at the end of this PR page's URL.) For further assistance with EasyCLA, please visit our EasyCLA portal and chat with our support bot.

@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant