-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Introducing BF16 Pointwise NCHWc Convolution for Arm64 #26838
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
base: main
Are you sure you want to change the base?
Changes from all commits
cec0e7f
de54706
614e47b
020fc07
4aa292b
e74061b
0d61757
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| #include "core/framework/op_kernel.h" | ||
| #include "core/providers/cpu/nn/conv_attributes.h" | ||
| #include "core/providers/cpu/nn/pool.h" | ||
| #include "core/session/onnxruntime_session_options_config_keys.h" | ||
| #include "contrib_ops/cpu/fused_activation.h" | ||
|
|
||
| namespace onnxruntime { | ||
|
|
@@ -43,6 +44,10 @@ class NchwcConv final : public OpKernel { | |
| public: | ||
| NchwcConv(const OpKernelInfo& info) : OpKernel(info), conv_attrs_(info) { | ||
| ORT_ENFORCE(GetFusedActivationAttr(info, activation_).IsOK()); | ||
| #if defined(__aarch64__) && defined(__linux__) | ||
| auto config_ops = info.GetConfigOptions().GetConfigEntry(kOrtSessionOptionsMlasGemmFastMathArm64Bfloat16); | ||
|
Member
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.
Contributor
Author
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. Probably not. It is assigned a value inside onnxruntime_session_options_config_keys.h
In turn,
Member
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. Unless I am missing something GetConfigEntry() returns std::optional which may be not there, and that needs to be accounted for. GetConfigOrDefault may be a better way of handling that.
Contributor
Author
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. |
||
| use_fastmath_mode_ = (config_ops == "1") && MlasBf16AccelerationSupported(); | ||
| #endif | ||
| } | ||
|
|
||
| Status Compute(OpKernelContext* context) const override; | ||
|
|
@@ -51,6 +56,9 @@ class NchwcConv final : public OpKernel { | |
| ConvAttributes conv_attrs_; | ||
|
|
||
| MLAS_ACTIVATION activation_; | ||
| #if defined(__aarch64__) && defined(__linux__) | ||
hariharans29 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| bool use_fastmath_mode_{false}; | ||
| #endif | ||
| }; | ||
|
|
||
| class NchwcPoolBase : public PoolBase { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| /*++ | ||
|
|
||
| Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
|
||
| Licensed under the MIT License. | ||
|
|
||
| Module Name: | ||
|
|
||
| sbconv_kernel_neon.cpp | ||
|
|
||
| Abstract: | ||
|
|
||
| This module implements bfloat16 precision convolution kernels for ARM NEON. | ||
|
|
||
| --*/ | ||
|
|
||
| #if defined(MLAS_USE_ARM_NEON_NCHWC) && defined(__linux__) | ||
|
|
||
| #include "mlasi.h" | ||
| #include "sconv.h" | ||
|
|
||
| constexpr size_t BlockSize = MLAS_PLATFORM::MLAS_NEON_NCHWC_BLOCK_SIZE; | ||
|
|
||
| // | ||
| // BF16 Pointwise (1x1) Convolution Kernel using SBGEMM. | ||
| // | ||
| void MLASCALL | ||
| MlasConvPointwiseBf16KernelNeon( | ||
| const float* Input, | ||
| const float* Filter, | ||
| float* Output, | ||
| size_t StrideWidth, | ||
| size_t InputChannels, | ||
| size_t FilterCount, | ||
| size_t InputStride, | ||
| size_t FilterStride, | ||
| size_t OutputStride, | ||
| size_t OutputCount, | ||
| const float* Bias, | ||
| unsigned KernelFlags | ||
| ) | ||
| { | ||
| const bool AccumulateOutput = (KernelFlags & MLAS_CONV_KERNEL_FLAG_ACCUMULATE_OUTPUT) != 0; | ||
| const bool BiasAddition = (KernelFlags & MLAS_CONV_KERNEL_FLAG_BIAS_ADDITION) != 0; | ||
| const bool ReluActivation = (KernelFlags & MLAS_CONV_KERNEL_FLAG_RELU_ACTIVATION) != 0; | ||
|
|
||
| const size_t StrideWidthElements = StrideWidth / sizeof(float); | ||
| const size_t InputStrideElements = InputStride / sizeof(float); | ||
| const size_t FilterStrideElements = FilterStride / sizeof(float); | ||
| const size_t OutputStrideElements = OutputStride / sizeof(float); | ||
|
|
||
| // SBGEMM only adds bias when ZeroMode=true. When accumulating (ZeroMode=false), | ||
| // pre-add bias to existing output before the GEMM operations. | ||
| if (BiasAddition && AccumulateOutput) { | ||
| for (size_t f = 0; f < FilterCount; f++) { | ||
| float* output = Output + f * OutputStrideElements; | ||
| const float32x4_t b0 = MlasLoadFloat32x4(&Bias[f * BlockSize]); | ||
| const float32x4_t b1 = MlasLoadFloat32x4(&Bias[f * BlockSize + 4]); | ||
| const float32x4_t b2 = MlasLoadFloat32x4(&Bias[f * BlockSize + 8]); | ||
| const float32x4_t b3 = MlasLoadFloat32x4(&Bias[f * BlockSize + 12]); | ||
| for (size_t i = 0; i < OutputCount; i++) { | ||
| MlasStoreFloat32x4(&output[i * BlockSize], MlasAddFloat32x4(b0, MlasLoadFloat32x4(&output[i * BlockSize]))); | ||
| MlasStoreFloat32x4(&output[i * BlockSize + 4], MlasAddFloat32x4(b1, MlasLoadFloat32x4(&output[i * BlockSize + 4]))); | ||
| MlasStoreFloat32x4(&output[i * BlockSize + 8], MlasAddFloat32x4(b2, MlasLoadFloat32x4(&output[i * BlockSize + 8]))); | ||
| MlasStoreFloat32x4(&output[i * BlockSize + 12], MlasAddFloat32x4(b3, MlasLoadFloat32x4(&output[i * BlockSize + 12]))); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Build SBGEMM params for all (filter, input_channel) combinations. | ||
| // FilterCount <= 4, InputChannels <= 8, so max 32 elements. | ||
| // Bias is set on all elements but SBGEMM only uses it when ZeroMode=true. | ||
| MLAS_SBGEMM_DATA_PARAMS gemm_params[32]; | ||
|
|
||
| size_t idx = 0; | ||
| for (size_t f = 0; f < FilterCount; f++) { | ||
| const float* filter = Filter + f * FilterStrideElements; | ||
| float* output = Output + f * OutputStrideElements; | ||
| for (size_t ic = 0; ic < InputChannels; ic++, idx++) { | ||
| gemm_params[idx].A = Input + ic * InputStrideElements; | ||
| gemm_params[idx].B = filter + ic * BlockSize * BlockSize; | ||
| gemm_params[idx].C = output; | ||
| gemm_params[idx].lda = StrideWidthElements; | ||
| gemm_params[idx].ldb = BlockSize; | ||
| gemm_params[idx].ldc = BlockSize; | ||
| gemm_params[idx].Bias = BiasAddition ? (Bias + f * BlockSize) : nullptr; | ||
| gemm_params[idx].AIsfp32 = true; | ||
| gemm_params[idx].BIsfp32 = true; | ||
| gemm_params[idx].ZeroMode = (ic == 0) && !AccumulateOutput; | ||
| gemm_params[idx].OutputProcessor = nullptr; | ||
| } | ||
| } | ||
|
|
||
| MlasSBGemmBatch(OutputCount, BlockSize, BlockSize, idx, gemm_params, nullptr); | ||
|
|
||
| if (ReluActivation) { | ||
| const float32x4_t ZeroVector = MlasBroadcastFloat32x4(0.0f); | ||
| for (size_t f = 0; f < FilterCount; f++) { | ||
| float* output = Output + f * OutputStrideElements; | ||
| for (size_t i = 0; i < OutputCount; i++) { | ||
| MlasStoreFloat32x4(&output[i * BlockSize], MlasMaximumFloat32x4(MlasLoadFloat32x4(&output[i * BlockSize]), ZeroVector)); | ||
| MlasStoreFloat32x4(&output[i * BlockSize + 4], MlasMaximumFloat32x4(MlasLoadFloat32x4(&output[i * BlockSize + 4]), ZeroVector)); | ||
| MlasStoreFloat32x4(&output[i * BlockSize + 8], MlasMaximumFloat32x4(MlasLoadFloat32x4(&output[i * BlockSize + 8]), ZeroVector)); | ||
| MlasStoreFloat32x4(&output[i * BlockSize + 12], MlasMaximumFloat32x4(MlasLoadFloat32x4(&output[i * BlockSize + 12]), ZeroVector)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif |
Uh oh!
There was an error while loading. Please reload this page.