SIGN IN SIGN UP

Fix manual permute within channels_last block (#3932)

Summary:
Pull Request resolved: https://github.com/pytorch/executorch/pull/3932

Our Channels Last reshape pass propagates channels_last computation for as long as possible in order to reduce the number of computes. As a result, we propagate the ending to_contiguous permute past actual permute operations. This means we end up computing permutes when the input is in channels last. As a result, we need to recalculate the permute order in order to correctly perform the permute within the channels last block.

Consider a manual permute reversing the dims within the channels last block below:
```
--> to_channels_last([0, 2, 3, 1])
--> permute([3, 2, 1, 0]) # needs to rewrite [3, 2, 1, 0] in channels last form
--> to_contiguous([0, 3, 1, 2])
```

In order to calculate the correct permutation order, we must perform the following
```
--> to_channels_last([0, 2, 3, 1])
--> to_contiguous([0, 3, 1, 2])
--> permute([3, 2, 1, 0])
--> to_channels_last([0, 2, 3, 1])
--> to_contiguous([0, 3, 1, 2])
```

We can then combine the middle permutes:
```
--> to_channels_last([0, 2, 3, 1])
--> permute([2, 3, 0, 1]) # [3, 2, 1, 0] in channels last form
--> to_contiguous([0, 3, 1, 2])
```

Reviewed By: digantdesai, kirklandsign

Differential Revision: D58393086

fbshipit-source-id: 2a8f2fdbcb3820d9edc557088831dcb9613f9b21
M
Max Ren committed
b39dbadf9267783b18a64547f2be39d1cdb9ad79
Parent: 6840b9d
Committed by Facebook GitHub Bot <facebook-github-bot@users.noreply.github.com> on 6/11/2024, 6:11:48 PM