-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat: Add BFloat16 support for Gemm and MatMul CPU operators #26317
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
Open
snnn
wants to merge
21
commits into
main
Choose a base branch
from
bfloat16_cpu_support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+284
−18
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
4d31693
feat: Add bfloat16 support for Gemm and MatMul on CPU
snnn 74a3353
feat: Add BFloat16 support for Gemm and MatMul CPU operators
snnn d077b53
mlas.h: expand bf16 support guard without whitespace noise
8157277
remove
d25bbe5
Format code
e419576
revert
e36ac3a
Initialize cpuinfo in Env (Posix+Windows); remove redundant cpuinfo_i…
f76aca5
Update includes to renamed core/common/endian.h
207726d
Rename float16.h to core/common/float16.h and update includes
ef80220
update
5ecf919
Merge remote-tracking branch 'origin/main' into bfloat16_cpu_support
snnn 49a5e52
fix build
snnn ff426fb
update
snnn 640cdab
update
snnn a915e40
fix
snnn 9de02aa
update
snnn 1a52ca2
Fix training code
snnn 715518f
update
snnn 14ae0f1
update
snnn 36d23a7
Merge remote-tracking branch 'origin/main' into bfloat16_cpu_support
0c02307
format code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -180,6 +180,53 @@ void Gemm<double, ThreadPool>(CBLAS_TRANSPOSE TransA, CBLAS_TRANSPOSE TransB, pt | |
} | ||
#endif | ||
|
||
template <> | ||
void Gemm<BFloat16, ThreadPool>(CBLAS_TRANSPOSE TransA, CBLAS_TRANSPOSE TransB, ptrdiff_t M, | ||
ptrdiff_t N, ptrdiff_t K, BFloat16 alpha, const BFloat16* A, const BFloat16* B, BFloat16 beta, | ||
BFloat16* C, ThreadPool*) { | ||
auto C_mat = EigenMatrixMap<Eigen::bfloat16>(reinterpret_cast<Eigen::bfloat16*>(C), N, M); | ||
if (beta == BFloat16(0.f)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is beta input, local var or a mistyped output? It is not clear from the code. |
||
C_mat.setZero(); | ||
} else { | ||
Eigen::bfloat16 beta_bfloat(static_cast<float>(beta)); | ||
C_mat *= beta_bfloat; | ||
} | ||
Eigen::bfloat16 alpha_bfloat(static_cast<float>(alpha)); | ||
|
||
switch (TransA) { | ||
case CblasNoTrans: { | ||
switch (TransB) { | ||
case CblasNoTrans: | ||
C_mat.noalias() += alpha_bfloat * (ConstEigenMatrixMap<Eigen::bfloat16>(reinterpret_cast<const Eigen::bfloat16*>(B), N, K) * | ||
ConstEigenMatrixMap<Eigen::bfloat16>(reinterpret_cast<const Eigen::bfloat16*>(A), K, M)); | ||
return; | ||
case CblasTrans: | ||
C_mat.noalias() += alpha_bfloat * (ConstEigenMatrixMap<Eigen::bfloat16>(reinterpret_cast<const Eigen::bfloat16*>(B), K, N).transpose() * | ||
ConstEigenMatrixMap<Eigen::bfloat16>(reinterpret_cast<const Eigen::bfloat16*>(A), K, M)); | ||
return; | ||
default: | ||
ORT_THROW("CblasNoTrans Unexpected CBLAS_TRANSPOSE for TransB of ", TransB); | ||
} | ||
} | ||
case CblasTrans: { | ||
switch (TransB) { | ||
case CblasNoTrans: | ||
C_mat.noalias() += alpha_bfloat * (ConstEigenMatrixMap<Eigen::bfloat16>(reinterpret_cast<const Eigen::bfloat16*>(B), N, K) * | ||
ConstEigenMatrixMap<Eigen::bfloat16>(reinterpret_cast<const Eigen::bfloat16*>(A), M, K).transpose()); | ||
return; | ||
case CblasTrans: | ||
C_mat.noalias() += alpha_bfloat * (ConstEigenMatrixMap<Eigen::bfloat16>(reinterpret_cast<const Eigen::bfloat16*>(B), K, N).transpose() * | ||
ConstEigenMatrixMap<Eigen::bfloat16>(reinterpret_cast<const Eigen::bfloat16*>(A), M, K).transpose()); | ||
return; | ||
default: | ||
ORT_THROW("CblasTrans Unexpected CBLAS_TRANSPOSE for TransB of ", TransB); | ||
} | ||
} | ||
default: | ||
ORT_THROW("Unexpected CBLAS_TRANSPOSE for TransA of ", TransA); | ||
} | ||
} | ||
|
||
template <> | ||
void MatMul<float>(ptrdiff_t M, ptrdiff_t N, ptrdiff_t K, const float* A, const float* B, float* C, ThreadPool* threadpool) { | ||
MlasGemm(CblasNoTrans, CblasNoTrans, M, N, K, 1.f, A, K, B, N, 0.f, C, N, threadpool); | ||
|
@@ -194,6 +241,11 @@ void MatMul<double>(ptrdiff_t M, ptrdiff_t N, ptrdiff_t K, const double* A, cons | |
EIGEN_MATMUL_FUNCTION(double) | ||
#endif | ||
|
||
template <> | ||
void MatMul<BFloat16>(ptrdiff_t M, ptrdiff_t N, ptrdiff_t K, const BFloat16* A, const BFloat16* B, BFloat16* C, ThreadPool* threadpool) { | ||
Gemm<BFloat16, ThreadPool>(CblasNoTrans, CblasNoTrans, M, N, K, BFloat16(1.f), A, B, BFloat16(0.f), C, threadpool); | ||
} | ||
|
||
template <> | ||
void GemmEx<float, ThreadPool>(CBLAS_TRANSPOSE TransA, CBLAS_TRANSPOSE TransB, ptrdiff_t M, ptrdiff_t N, ptrdiff_t K, | ||
float alpha, const float* A, int lda, const float* B, int ldb, float beta, float* C, | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we nee a macro here if cpuinfo supported?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added code to force the library be available on Windows.