Skip to content
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

[QNN] MatMulAddFusion and Reshape Related Fusion #22494

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

centwang
Copy link
Contributor

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

@centwang centwang 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
@centwang centwang force-pushed the weicwang/matmul_add_fusion branch from a8388b7 to ca59611 Compare October 29, 2024 11:51
@centwang centwang changed the title Add More Cases to MatMulAddFusion [QNN] MatMulAddFusion and Reshape Related Fusion Oct 29, 2024
@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?

onnxruntime/core/optimizer/reshape_fusion.cc Outdated Show resolved Hide resolved
onnxruntime/test/optimizer/graph_transform_test.cc Outdated Show resolved Hide resolved
onnxruntime/core/optimizer/matmul_add_fusion.cc Outdated Show resolved Hide resolved
onnxruntime/core/optimizer/matmul_add_fusion.cc Outdated Show resolved Hide resolved
onnxruntime/core/optimizer/matmul_add_fusion.cc Outdated Show resolved Hide resolved
onnxruntime/test/optimizer/graph_transform_test.cc Outdated Show resolved Hide resolved
onnxruntime/test/optimizer/graph_transform_test.cc Outdated Show resolved Hide resolved
@centwang centwang force-pushed the weicwang/matmul_add_fusion branch from ca59611 to 47d4755 Compare November 25, 2024 05:49
onnxruntime/core/optimizer/matmul_add_fusion.cc Outdated Show resolved Hide resolved
onnxruntime/core/optimizer/reshape_fusion.cc Outdated Show resolved Hide resolved
onnxruntime/core/optimizer/reshape_fusion.cc Outdated Show resolved Hide resolved
Qnn_TensorType_t tensor_type = qnn_model_wrapper.GetTensorType(weight_tensor_name);
Qnn_DataType_t data_type = QNN_DATATYPE_FLOAT_32;
ORT_RETURN_IF_ERROR(utils::GetQnnDataType(false, weight_def.node_arg.TypeAsProto(), data_type));
const auto& weight_tensor_proto = qnn_model_wrapper.GetInitializerTensors().at(weight_tensor_name);
Copy link
Contributor

Choose a reason for hiding this comment

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

If the initializer is not constant you have to use the value at runtime as it could be overridden in each Run call.

So I would suggest fixing the test (if the initializers are not intended to be mutable) or updating the logic to not take the node if the initializer is mutable.

onnxruntime/test/providers/qnn/qnn_basic_test.cc Outdated Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants