fix(transforms): convert view_copy to unsqueeze when the added dim is 0#20603
Open
durvesh1992 wants to merge 1 commit into
Open
fix(transforms): convert view_copy to unsqueeze when the added dim is 0#20603durvesh1992 wants to merge 1 commit into
durvesh1992 wants to merge 1 commit into
Conversation
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.
🔗 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.
|
|
This PR needs a
|
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.
Summary
ViewCopyToSqueezeUnsqueezePassrewrites aview_copythat adds a size-1 dimension into anunsqueeze_copy. It decides whether to do the rewrite with:find_unsqueeze_dimreturns the index of the added size-1 dimension, andNonewhen the shapes don't match an unsqueeze. When the size-1 dim is added at the front, the returned index is0— which is falsy — soif unsqueeze_dim:skips the rewrite and theview_copyis left in place for that case (e.g.[3, 4] -> [1, 3, 4]).The fix is to guard on
is not None:(The squeeze branch is unaffected:
find_squeeze_dimsreturns a non-empty list orNone, so itsif squeeze_dims:check is correct.)Test plan
Adds
backends/transforms/test/test_view_copy_to_squeeze_unsqueeze.pywith three cases: unsqueeze at dim 0 (regression for this bug), unsqueeze at dim 1, and squeeze.test_unsqueeze_at_dim_0fails — theview_copyis not converted (graph still containsaten.view_copy).Run: