Skip to content

Conversation

@winskuo-quic
Copy link
Collaborator

@winskuo-quic winskuo-quic commented Nov 10, 2025

Summary

QNN doesn't have much 5D permute optimization, which causes Vit running slower than CPU.
image
Switched pattern from unsqueeze->permute 5d->squeeze to permute 4d.

Improvements: 150ms/inference -> 4.2ms/inference.

Test plan

Pass Vit UT

@pytorch-bot
Copy link

pytorch-bot bot commented Nov 10, 2025

🔗 Helpful Links

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

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

❌ 4 New Failures, 1 Cancelled Job, 4 Unrelated Failures

As of commit da35397 with merge base d07a49a (image):

NEW FAILURES - The following jobs have failed:

CANCELLED JOB - The following job was cancelled. Please retry:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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 Nov 10, 2025
@github-actions
Copy link

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.

@winskuo-quic
Copy link
Collaborator Author

Hi @cccclai,
This PR is to address the issue you have reported about QNN running slower than CPU for Vit.
I have tested SM8750 with this PR, and inference speed improves from 140ms/inference -> 4.1ms/inference, which should be around 35 times faster.
I have made a source level transformation on the Vit model.
The bottleneck is caused by the 5D permute pattern here: https://github.com/pytorch/pytorch/blob/3cfbf98ea9d937d23f3700168b22706c957308ce/torch/nn/functional.py#L5825.
I did source level transformation because this pattern is a bit specific and it is hard to generalize.
Please have a look and let me know if you have any questions.
Thanks


# Copied from torch/nn/functional.py
# QNN does not have 5D permute optimization. Fuse to a single 4D optimization
# Changed unsqueeze(0).transpose(0, -2).squeeze(-2) to permute(2, 0, 1, 3)
Copy link
Contributor

Choose a reason for hiding this comment

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

It can be a pass ideally, is it correct?

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.

2 participants