Skip to content

Fix dim order in op_permute #6432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 23, 2024
Merged

Conversation

Erik-Lundell
Copy link
Collaborator

No description provided.

SaoirseARM and others added 2 commits October 22, 2024 09:43
* hardtanh
* permute

Change-Id: Ia1802bdc37d365af382835b3c14174d841892927
Permute vector needs to be permuted when dim_order != (0, 1, 2, 3)

Change-Id: I2a35c6852376f9a57deeedd4fc38bda870e453a4
Signed-off-by: Oscar Andersson <[email protected]>
Signed-off-by: Erik Lundell <[email protected]>
Copy link

pytorch-bot bot commented Oct 22, 2024

🔗 Helpful Links

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

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

❌ 1 New Failure

As of commit b86ae33 with merge base 0aa802d (image):

NEW FAILURE - The following job has failed:

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

@facebook-github-bot facebook-github-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 Oct 22, 2024
@Erik-Lundell Erik-Lundell added ciflow/trunk partner: arm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Arm labels Oct 22, 2024
@@ -40,8 +88,33 @@ def define_node(
)
return

# The permutation vector describes a permutation P in default Pytorch dim_order.
# For rank 4, the default dim_order NCHW.
# E.g. (2,3,0,1) -> permute (n,c,h,w) to (w,c,n,h)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# E.g. (2,3,0,1) -> permute (n,c,h,w) to (w,c,n,h)
# E.g. (2,3,0,1) -> permute (n,c,h,w) to (h,w,n,c)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your suggestion is correct, messed this up.

P = permutation_vector_to_matrix(permutation_vector)

# The complete transformation is S * P * S_inverse.
transformation_matrix = S.matmul(P.matmul(S_inverse))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very cool, and I think I get what it is doing, but can you help me understand the math here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, the problem I am addressing is how a permutation vector expressed in nchw should be modified to work for nhwc. The key idea is to view the dim order as a coordinate system (the index (1,2,3,4) in nchw-coordinates is the same as (1,3,4,2) in nhwc) and to realize that both transformations between the systems and a permutation ops are linear operations that can be described by matrices.

For each nhwc-index in the incoming tensor, the full transformation of the permute op can then be described by:

  1. translate from nhwc to nchw
  2. do the permutation in nchw coordinates
  3. translate back to nhwc
    Since all steps are permutation transformations, we can combine them into one single permutation transformation, which can then be expressed by a permutation vector - this is the vector we were looking for.

ArmTester(
module,
example_inputs=test_data,
compile_spec=common.get_tosa_compile_spec(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we test permute to nhwc here at least for u85?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, right now we are only testing the case when we already are in nhwc for rank 4 tensors.

@facebook-github-bot
Copy link
Contributor

@digantdesai has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot merged commit f6778d5 into pytorch:main Oct 23, 2024
106 of 107 checks passed
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. partner: arm For backend delegation, kernels, demo, etc. from the 3rd-party partner, Arm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants