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

[ARM CPU] Add Fp16 kernels for MatMulNBits #22651

Closed
wants to merge 56 commits into from
Closed

Conversation

fajin-corp
Copy link
Contributor

@fajin-corp fajin-corp commented Oct 30, 2024

Description

Add Fp16 kernels for MatMulNBits.
Support Fp16 A calculate using accuracy 2.

BlkLen:128/Symmetric:0/HasBias:1

Thread M N K Fp32 Time Fp16 Time Fp16 latency reduction
1 1 4096 3072 5086301 ns 2912092 ns 42.7%
1 1 4096 11008 17866090 ns 10989713 ns 38.5%
1 1 11008 3072 13763608 ns 7844626 ns 43.0%
1 4096 4096 3072 2843439224 ns 1954152587 ns 31.3%
8 1 4096 3072 627008 ns 371404 ns 40.8%
8 1 4096 11008 2229758 ns 1370499 ns 38.5%
8 1 11008 3072 1713451 ns 1008165 ns 41.2%
8 4096 4096 3072 374325569 ns 250992166 ns 32.9%

Motivation and Context

Add cross-device data type support.

@fajin-corp fajin-corp requested a review from a team as a code owner October 30, 2024 01:16
@amarin16
Copy link
Collaborator

amarin16 commented Oct 30, 2024

There seem to be conflicts in matmul_nbits.cc and matmul_4bits_test.cc #Resolved

@fajin-corp fajin-corp force-pushed the fajin/mmnbfp16armsimd branch from 3e095fc to 98b1e5f Compare October 30, 2024 18:24
Copy link
Contributor

@edgchen1 edgchen1 left a comment

Choose a reason for hiding this comment

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

initial review

return CompInt8;
}
// Fallback to fp16. If fp16 optimized path is not available, it will further fall back to fp32.
return CompFp16;
Copy link
Contributor

Choose a reason for hiding this comment

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

so this will return CompFp16 even if accuracy_level_attr is CompFp32?

Copy link
Contributor Author

@fajin-corp fajin-corp Oct 31, 2024

Choose a reason for hiding this comment

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

I don't see a point of using CompFp32 for fp16 input if CompFp16 is available. converting fp16 to fp32 does not bring more precision, and the casting only makes the performance worse.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that it doesn't make sense for fp16 input. for fp16 input, what do you think about treating the default accuracy level value (unset) as CompFp16 and treating an explicit accuracy level of CompFp32 as an error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if accuracy 1 is given for fp16 input, maybe show a warning and use compFp16?

Copy link
Contributor

Choose a reason for hiding this comment

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

sure, warning is good too

onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/core/mlas/inc/mlas_qnbit.h Show resolved Hide resolved
onnxruntime/core/mlas/inc/mlas_qnbit.h Outdated Show resolved Hide resolved
onnxruntime/core/mlas/lib/fp16_common.h Outdated Show resolved Hide resolved
onnxruntime/core/mlas/lib/sqnbitgemm.h Outdated Show resolved Hide resolved
onnxruntime/core/mlas/lib/sqnbitgemm.cpp Fixed Show fixed Hide fixed
onnxruntime/core/mlas/lib/sqnbitgemm.cpp Fixed Show fixed Hide fixed
onnxruntime/core/mlas/lib/sqnbitgemm.cpp Fixed Show fixed Hide fixed
onnxruntime/core/mlas/lib/sqnbitgemm.cpp Fixed Show fixed Hide fixed
onnxruntime/core/mlas/lib/sqnbitgemm.h Fixed Show fixed Hide fixed
@fajin-corp
Copy link
Contributor Author

resolved


In reply to: 2445691891

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

You can commit the suggested changes from lintrunner.

onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/core/mlas/lib/qnbitgemm.cpp Fixed Show fixed Hide fixed
onnxruntime/core/mlas/lib/qnbitgemm.cpp Fixed Show fixed Hide fixed
}
}

void SQ4BitGemm_CompInt8(

Check warning

Code scanning / CodeQL

Poorly documented large function Warning

Poorly documented function: fewer than 2% comments for a function of 127 lines.
onnxruntime/core/mlas/lib/qnbitgemm.cpp Dismissed Show dismissed Hide dismissed
onnxruntime/core/mlas/lib/qnbitgemm.cpp Dismissed Show dismissed Hide dismissed
onnxruntime/core/mlas/lib/qnbitgemm.cpp Dismissed Show dismissed Hide dismissed
onnxruntime/core/mlas/lib/qnbitgemm.h Fixed Show fixed Hide fixed
PackedQuantBData = reinterpret_cast<std::byte*>(MlasAlignAddress(PackedQuantBWorkspace, 32));
QuantBBlkSum = reinterpret_cast<T*>(PackedQuantBData + PackedQuantBDataSize);
QuantBBlkSum = reinterpret_cast<T*>(MlasAlignAddress(QuantBBlkSum, MlasQNBitQuantBBlkSumAlignment()));
PackedQuantBScale = reinterpret_cast<T*>(reinterpret_cast<std::byte*>(QuantBBlkSum) + BlkSumSize);

Check failure

Code scanning / CodeQL

Suspicious pointer scaling High

This pointer might have type
float
(size 4), but this pointer arithmetic is done with type byte * (size 1).
This pointer might have type
MLFloat16
(size 2), but this pointer arithmetic is done with type byte * (size 1).
Copy link
Contributor

@edgchen1 edgchen1 left a comment

Choose a reason for hiding this comment

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

looks good. had a few comments.

onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/contrib_ops/cpu/quantization/matmul_nbits.cc Outdated Show resolved Hide resolved
onnxruntime/core/mlas/inc/mlas_qnbit.h Show resolved Hide resolved
onnxruntime/core/mlas/inc/mlas_qnbit.h Outdated Show resolved Hide resolved
onnxruntime/core/mlas/lib/qnbitgemm.h Show resolved Hide resolved
template <typename ElementType>
std::vector<ElementType> RandomVectorUniform(
typename std::enable_if_t<!std::is_same_v<ElementType, MLAS_FP16>, std::vector<ElementType>>
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: would it be simpler to have a specialization for MLAS_FP16 instead of two enable_ifs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

since this is a .h file included in multiple .cpp files, using specialization will trigger redefinition. so I chose to use enable_if

@fajin-corp fajin-corp force-pushed the fajin/mmnbfp16armsimd branch from 57ef96b to 037db3f Compare November 8, 2024 22:40
@fajin-corp fajin-corp force-pushed the fajin/mmnbfp16armsimd branch from 037db3f to 8050f0a Compare November 8, 2024 23:51
fajin-corp added a commit that referenced this pull request Nov 12, 2024
### Description
A break down PR of #22651
Add fp16 kernels.



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
fajin-corp added a commit that referenced this pull request Nov 14, 2024
### Description
A break-down PR of #22651
Op API change only.
- add template to functions and classes that support fp32 and fp16
- rename functions, classes and files that support fp32 and fp16 from
SQNBxxx to QNBxxx


### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
fajin-corp added a commit that referenced this pull request Nov 15, 2024
### Description
A breakdown PR of #22651



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
@fajin-corp fajin-corp closed this Nov 15, 2024
ishwar-raut1 pushed a commit to ishwar-raut1/onnxruntime that referenced this pull request Nov 19, 2024
### Description
A break down PR of microsoft#22651
Add fp16 kernels.



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
ishwar-raut1 pushed a commit to ishwar-raut1/onnxruntime that referenced this pull request Nov 19, 2024
### Description
A break-down PR of microsoft#22651
Op API change only.
- add template to functions and classes that support fp32 and fp16
- rename functions, classes and files that support fp32 and fp16 from
SQNBxxx to QNBxxx


### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
ishwar-raut1 pushed a commit to ishwar-raut1/onnxruntime that referenced this pull request Nov 19, 2024
### Description
A breakdown PR of microsoft#22651



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
guschmue pushed a commit that referenced this pull request Dec 2, 2024
### Description
A break down PR of #22651
Add fp16 kernels.



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
guschmue pushed a commit that referenced this pull request Dec 2, 2024
### Description
A break-down PR of #22651
Op API change only.
- add template to functions and classes that support fp32 and fp16
- rename functions, classes and files that support fp32 and fp16 from
SQNBxxx to QNBxxx


### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
guschmue pushed a commit that referenced this pull request Dec 2, 2024
### Description
A breakdown PR of #22651



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
ankitm3k pushed a commit to intel/onnxruntime that referenced this pull request Dec 11, 2024
### Description
A break down PR of microsoft#22651
Add fp16 kernels.



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
ankitm3k pushed a commit to intel/onnxruntime that referenced this pull request Dec 11, 2024
### Description
A break-down PR of microsoft#22651
Op API change only.
- add template to functions and classes that support fp32 and fp16
- rename functions, classes and files that support fp32 and fp16 from
SQNBxxx to QNBxxx


### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
ankitm3k pushed a commit to intel/onnxruntime that referenced this pull request Dec 11, 2024
### Description
A breakdown PR of microsoft#22651



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
ankitm3k pushed a commit to intel/onnxruntime that referenced this pull request Dec 11, 2024
### Description
A break down PR of microsoft#22651
Add fp16 kernels.



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
ankitm3k pushed a commit to intel/onnxruntime that referenced this pull request Dec 11, 2024
### Description
A break-down PR of microsoft#22651
Op API change only.
- add template to functions and classes that support fp32 and fp16
- rename functions, classes and files that support fp32 and fp16 from
SQNBxxx to QNBxxx


### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
ankitm3k pushed a commit to intel/onnxruntime that referenced this pull request Dec 11, 2024
### Description
A breakdown PR of microsoft#22651



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
ankitm3k pushed a commit to intel/onnxruntime that referenced this pull request Dec 11, 2024
### Description
A break down PR of microsoft#22651
Add fp16 kernels.



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
ankitm3k pushed a commit to intel/onnxruntime that referenced this pull request Dec 11, 2024
### Description
A break-down PR of microsoft#22651
Op API change only.
- add template to functions and classes that support fp32 and fp16
- rename functions, classes and files that support fp32 and fp16 from
SQNBxxx to QNBxxx


### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
ankitm3k pushed a commit to intel/onnxruntime that referenced this pull request Dec 11, 2024
### Description
A breakdown PR of microsoft#22651



### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
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.

3 participants