Skip to content

[QNN] MatMulAddFusion and Reshape Related Fusion#22494

Merged
jywu-msft merged 21 commits intomainfrom
weicwang/matmul_add_fusion
Feb 18, 2025
Merged

[QNN] MatMulAddFusion and Reshape Related Fusion#22494
jywu-msft merged 21 commits intomainfrom
weicwang/matmul_add_fusion

Conversation

@Lafi7e
Copy link
Contributor

@Lafi7e Lafi7e commented Oct 18, 2024

QNN EP relies on Gemm Op to use FullyConnected QNN Op to run the model, which is much faster than MatMul+Add. This PR fuses MatMul+Add when MatMul's 2nd input is 2D initializer, no matter the rank of the 1st input. If the 1st input is not 2D tensor, Reshape nodes will be added.

On QNN EP, the memory allocation is for each activation tensor, so Reshape/Squeeze/Unsqueeze is not no-op. This PR also add some fusion trying to remove redundant reshape nodes. For some QNN AI Hub models on specific device, without removing the Reshape nodes, it cannot finalize the graph when execution, but works well after removing.

Run below models with and without the change:
swin_tiny: Average inference time cost: 12.8077 ms | Average inference time cost: 23.956 ms
swin_base: Average inference time cost: 27.0639 ms | Average inference time cost: 57.6608 ms
convnext_tiny: Average inference time cost: 3.42956 ms | Average inference time cost: 16.1848 ms
openai_clip_CLIPTextEncoder: Average inference time cost: 5.96104 ms | Average inference time cost: 220.406 ms
openai_clip_CLIPImageEncoder: Average inference time cost: 41.8206 ms | Average inference time cost: 919.712 ms

NOTE that current change skips the Attention pattern because it not it will cause AttentionFusion to work. Ideally we need to adjust the AttentionFusion to support the Gemm pattern, but it requires big changes. Maybe we can do this in the future, say, when we want to run transformer models on QNN, since we don't have Attention QNN, we still want to fuse MatMul+Add in the Attention pattern to use FullyConnected in QNN side.

@Lafi7e Lafi7e force-pushed the weicwang/matmul_add_fusion branch from 7d3d515 to 0a05430 Compare October 21, 2024 03:34
snnn
snnn previously approved these changes Oct 21, 2024
@Lafi7e Lafi7e force-pushed the weicwang/matmul_add_fusion branch from a8388b7 to ca59611 Compare October 29, 2024 11:51
@Lafi7e Lafi7e changed the title Add More Cases to MatMulAddFusion [QNN] MatMulAddFusion and Reshape Related Fusion Oct 29, 2024
@Lafi7e Lafi7e requested review from cloudhan and jywu-msft October 29, 2024 11:52
@adrianlizarraga
Copy link
Contributor

@centwang Thank you for the PR. It looks like many unit tests and pipelines are still not passing. Could you please address those issues first?

@Lafi7e Lafi7e force-pushed the weicwang/matmul_add_fusion branch from ca59611 to 47d4755 Compare November 25, 2024 05:49
skottmckay
skottmckay previously approved these changes Jan 17, 2025
@adrianlizarraga adrianlizarraga requested review from HectorSVC and removed request for cloudhan February 12, 2025 04:02
@adrianlizarraga
Copy link
Contributor

@HectorSVC could you please take a look at this PR?

@adrianlizarraga adrianlizarraga added the ep:QNN issues related to QNN exeution provider label Feb 14, 2025
@adrianlizarraga
Copy link
Contributor

Hi @skottmckay, I think there are some unresolved comments. Would you be able to take another look?

@jywu-msft jywu-msft merged commit 03c6c2e into main Feb 18, 2025
96 of 98 checks passed
@jywu-msft jywu-msft deleted the weicwang/matmul_add_fusion branch February 18, 2025 21:22
guschmue pushed a commit that referenced this pull request Mar 6, 2025
QNN EP relies on Gemm Op to use FullyConnected QNN Op to run the model,
which is much faster than MatMul+Add. This PR fuses MatMul+Add when
MatMul's 2nd input is 2D initializer, no matter the rank of the 1st
input. If the 1st input is not 2D tensor, Reshape nodes will be added.

On QNN EP, the memory allocation is for each activation tensor, so
Reshape/Squeeze/Unsqueeze is not no-op. This PR also add some fusion
trying to remove redundant reshape nodes. For some QNN AI Hub models on
specific device, without removing the Reshape nodes, it cannot finalize
the graph when execution, but works well after removing.

Run below models with and without the change:
swin_tiny: Average inference time cost: 12.8077 ms | Average inference
time cost: 23.956 ms
swin_base: Average inference time cost: 27.0639 ms | Average inference
time cost: 57.6608 ms
convnext_tiny: Average inference time cost: 3.42956 ms | Average
inference time cost: 16.1848 ms
openai_clip_CLIPTextEncoder: Average inference time cost: 5.96104 ms |
Average inference time cost: 220.406 ms
openai_clip_CLIPImageEncoder: Average inference time cost: 41.8206 ms |
Average inference time cost: 919.712 ms

NOTE that current change skips the Attention pattern because it not it
will cause AttentionFusion to work. Ideally we need to adjust the
AttentionFusion to support the Gemm pattern, but it requires big
changes. Maybe we can do this in the future, say, when we want to run
transformer models on QNN, since we don't have Attention QNN, we still
want to fuse MatMul+Add in the Attention pattern to use FullyConnected
in QNN side.

---------

Co-authored-by: adrianlizarraga <adlizarraga@microsoft.com>
ashrit-ms pushed a commit that referenced this pull request Mar 17, 2025
QNN EP relies on Gemm Op to use FullyConnected QNN Op to run the model,
which is much faster than MatMul+Add. This PR fuses MatMul+Add when
MatMul's 2nd input is 2D initializer, no matter the rank of the 1st
input. If the 1st input is not 2D tensor, Reshape nodes will be added.

On QNN EP, the memory allocation is for each activation tensor, so
Reshape/Squeeze/Unsqueeze is not no-op. This PR also add some fusion
trying to remove redundant reshape nodes. For some QNN AI Hub models on
specific device, without removing the Reshape nodes, it cannot finalize
the graph when execution, but works well after removing.

Run below models with and without the change:
swin_tiny: Average inference time cost: 12.8077 ms | Average inference
time cost: 23.956 ms
swin_base: Average inference time cost: 27.0639 ms | Average inference
time cost: 57.6608 ms
convnext_tiny: Average inference time cost: 3.42956 ms | Average
inference time cost: 16.1848 ms
openai_clip_CLIPTextEncoder: Average inference time cost: 5.96104 ms |
Average inference time cost: 220.406 ms
openai_clip_CLIPImageEncoder: Average inference time cost: 41.8206 ms |
Average inference time cost: 919.712 ms

NOTE that current change skips the Attention pattern because it not it
will cause AttentionFusion to work. Ideally we need to adjust the
AttentionFusion to support the Gemm pattern, but it requires big
changes. Maybe we can do this in the future, say, when we want to run
transformer models on QNN, since we don't have Attention QNN, we still
want to fuse MatMul+Add in the Attention pattern to use FullyConnected
in QNN side.

---------

Co-authored-by: adrianlizarraga <adlizarraga@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ep:QNN issues related to QNN exeution provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants