Skip to content

Fix IndexError in RemovePermutesAroundElementwiseOps for rank-mismatched views (#19713)#19713

Merged
meta-codesync[bot] merged 1 commit into
mainfrom
export-D105787161
May 21, 2026
Merged

Fix IndexError in RemovePermutesAroundElementwiseOps for rank-mismatched views (#19713)#19713
meta-codesync[bot] merged 1 commit into
mainfrom
export-D105787161

Conversation

@mcremon-meta
Copy link
Copy Markdown
Contributor

@mcremon-meta mcremon-meta commented May 21, 2026

Summary:

Fix an IndexError crash in the RemovePermutesAroundElementwiseOps
compiler pass that occurs when a squeeze/unsqueeze view_copy node is
included in a subgraph via upstream traversal with a permutation whose
rank does not match the view input tensor rank.

The bug manifests as IndexError: tuple index out of range in
update_view_copy at the line:

unpermuted_in = [in_shape[inverse_permute[i]] for i in range(len(in_shape))]

because inverse_permute contains indices from a rank-N permutation
but in_shape has fewer than N dimensions.

The fix adds a validation guard at the top of permute_subgraph() that
checks all view_copy nodes for rank consistency between their
node_start_permute and their input tensor shape. If a mismatch is
found, the entire subgraph optimisation is skipped, preserving graph
correctness. The return type of permute_subgraph is changed from
None to bool so the caller only marks the graph as modified when the
subgraph was actually transformed.

Also adds a regression test that constructs the exact graph pattern
(upstream unsqueeze view_copy with a 2D constant feeding into a 3D
permuted add) that triggered the crash.

Reviewed By: ethansfng

Differential Revision: D105787161

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 21, 2026

🔗 Helpful Links

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

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

❌ You can merge normally! (2 Unrelated Failures), 1 Unclassified Failure

As of commit 8b56e75 with merge base da01860 (image):

UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:

  • Check Labels / Check labels (gh) (this job did not run on the merge base, so DrCI cannot tell whether the failure is pre-existing)
    RuntimeError: GraphQL query

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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 May 21, 2026
@github-actions github-actions Bot added ciflow/trunk module: arm Issues related to arm backend labels May 21, 2026
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented May 21, 2026

@mcremon-meta has exported this pull request. If you are a Meta employee, you can view the originating Diff in D105787161.

@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.

…hed views (#19713)

Summary:

Fix an IndexError crash in the RemovePermutesAroundElementwiseOps
compiler pass that occurs when a squeeze/unsqueeze view_copy node is
included in a subgraph via upstream traversal with a permutation whose
rank does not match the view input tensor rank.

The bug manifests as `IndexError: tuple index out of range` in
`update_view_copy` at the line:
```
unpermuted_in = [in_shape[inverse_permute[i]] for i in range(len(in_shape))]
```
because `inverse_permute` contains indices from a rank-N permutation
but `in_shape` has fewer than N dimensions.

The fix adds a validation guard at the top of `permute_subgraph()` that
checks all view_copy nodes for rank consistency between their
`node_start_permute` and their input tensor shape. If a mismatch is
found, the entire subgraph optimisation is skipped, preserving graph
correctness. The return type of `permute_subgraph` is changed from
`None` to `bool` so the caller only marks the graph as modified when the
subgraph was actually transformed.

Also adds a regression test that constructs the exact graph pattern
(upstream unsqueeze view_copy with a 2D constant feeding into a 3D
permuted add) that triggered the crash.

Reviewed By: ethansfng

Differential Revision: D105787161
@meta-codesync meta-codesync Bot changed the title Fix IndexError in RemovePermutesAroundElementwiseOps for rank-mismatched views Fix IndexError in RemovePermutesAroundElementwiseOps for rank-mismatched views (#19713) May 21, 2026
@meta-codesync meta-codesync Bot force-pushed the export-D105787161 branch from 64d5570 to 8b56e75 Compare May 21, 2026 05:35
@meta-codesync meta-codesync Bot merged commit 6e255d1 into main May 21, 2026
462 of 468 checks passed
@meta-codesync meta-codesync Bot deleted the export-D105787161 branch May 21, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported meta-exported module: arm Issues related to arm backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants