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] Fix fp16 const initialization on no-fp16 platform #23978

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 64 additions & 69 deletions onnxruntime/core/mlas/lib/softmax_kernel_neon_fp16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Module Name:
#include "softmax.h"
#include "softmax_kernel_neon.h"

// TODO(fajin): intra-loop parallelism
namespace softmax_neon {

template <typename T>
Expand All @@ -44,7 +43,7 @@ struct MlasExpConstants {
T MaximumExponent;
};

const MlasExpConstants<_mlas_fp16_> ExpConstantsFp16 = {
constexpr MlasExpConstants<_mlas_fp16_> ExpConstantsFp16 = {
0xcc55, // -25 * ln2
0x498c, // 16 * ln2
0xc95f, // -15.5 * ln2
Expand All @@ -64,59 +63,57 @@ const MlasExpConstants<_mlas_fp16_> ExpConstantsFp16 = {
0x3C00, // 15
};

const MlasExpConstants<float16x8_t> ExpConstantsFp16x8 = {
MlasBroadcastFloat16x8(ExpConstantsFp16.LowerRange),
MlasBroadcastFloat16x8(ExpConstantsFp16.UpperRange),
MlasBroadcastFloat16x8(ExpConstantsFp16.LowerRangeSumExp),
MlasBroadcastFloat16x8(ExpConstantsFp16.UpperRangeSumExp),
MlasBroadcastFloat16x8(ExpConstantsFp16.RoundingBias),
MlasBroadcastFloat16x8(ExpConstantsFp16.Log2Reciprocal),
MlasBroadcastFloat16x8(ExpConstantsFp16.Log2High),
MlasBroadcastFloat16x8(ExpConstantsFp16.Log2Mid),
MlasBroadcastFloat16x8(ExpConstantsFp16.Log2Low),
MlasBroadcastFloat16x8(ExpConstantsFp16.poly_0),
MlasBroadcastFloat16x8(ExpConstantsFp16.poly_1),
MlasBroadcastFloat16x8(ExpConstantsFp16.poly_2),
MlasBroadcastFloat16x8(ExpConstantsFp16.poly_3),
MlasBroadcastFloat16x8(ExpConstantsFp16.poly_4),
MlasBroadcastFloat16x8(ExpConstantsFp16.poly_56),
MlasBroadcastFloat16x8(ExpConstantsFp16.MinimumExponent),
MlasBroadcastFloat16x8(ExpConstantsFp16.MaximumExponent),
};

const MlasExpConstants<float16x4_t> ExpConstantsFp16x4 = {
MlasBroadcastFloat16x4(ExpConstantsFp16.LowerRange),
MlasBroadcastFloat16x4(ExpConstantsFp16.UpperRange),
MlasBroadcastFloat16x4(ExpConstantsFp16.LowerRangeSumExp),
MlasBroadcastFloat16x4(ExpConstantsFp16.UpperRangeSumExp),
MlasBroadcastFloat16x4(ExpConstantsFp16.RoundingBias),
MlasBroadcastFloat16x4(ExpConstantsFp16.Log2Reciprocal),
MlasBroadcastFloat16x4(ExpConstantsFp16.Log2High),
MlasBroadcastFloat16x4(ExpConstantsFp16.Log2Mid),
MlasBroadcastFloat16x4(ExpConstantsFp16.Log2Low),
MlasBroadcastFloat16x4(ExpConstantsFp16.poly_0),
MlasBroadcastFloat16x4(ExpConstantsFp16.poly_1),
MlasBroadcastFloat16x4(ExpConstantsFp16.poly_2),
MlasBroadcastFloat16x4(ExpConstantsFp16.poly_3),
MlasBroadcastFloat16x4(ExpConstantsFp16.poly_4),
MlasBroadcastFloat16x4(ExpConstantsFp16.poly_56),
MlasBroadcastFloat16x4(ExpConstantsFp16.MinimumExponent),
MlasBroadcastFloat16x4(ExpConstantsFp16.MaximumExponent),
};

template <typename T>
MLAS_FORCEINLINE
MlasExpConstants<T> Get_Exp_Constants();

template <>
MLAS_FORCEINLINE
MlasExpConstants<float16x8_t> Get_Exp_Constants<float16x8_t>() {
const static MlasExpConstants<float16x8_t> ExpConstantsFp16x8 = {
MlasBroadcastFloat16x8(ExpConstantsFp16.LowerRange),
MlasBroadcastFloat16x8(ExpConstantsFp16.UpperRange),
MlasBroadcastFloat16x8(ExpConstantsFp16.LowerRangeSumExp),
MlasBroadcastFloat16x8(ExpConstantsFp16.UpperRangeSumExp),
MlasBroadcastFloat16x8(ExpConstantsFp16.RoundingBias),
MlasBroadcastFloat16x8(ExpConstantsFp16.Log2Reciprocal),
MlasBroadcastFloat16x8(ExpConstantsFp16.Log2High),
MlasBroadcastFloat16x8(ExpConstantsFp16.Log2Mid),
MlasBroadcastFloat16x8(ExpConstantsFp16.Log2Low),
MlasBroadcastFloat16x8(ExpConstantsFp16.poly_0),
MlasBroadcastFloat16x8(ExpConstantsFp16.poly_1),
MlasBroadcastFloat16x8(ExpConstantsFp16.poly_2),
MlasBroadcastFloat16x8(ExpConstantsFp16.poly_3),
MlasBroadcastFloat16x8(ExpConstantsFp16.poly_4),
MlasBroadcastFloat16x8(ExpConstantsFp16.poly_56),
MlasBroadcastFloat16x8(ExpConstantsFp16.MinimumExponent),
MlasBroadcastFloat16x8(ExpConstantsFp16.MaximumExponent),
};
return ExpConstantsFp16x8;
}

template <>
MLAS_FORCEINLINE
MlasExpConstants<float16x4_t> Get_Exp_Constants<float16x4_t>() {
const static MlasExpConstants<float16x4_t> ExpConstantsFp16x4 = {
MlasBroadcastFloat16x4(ExpConstantsFp16.LowerRange),
MlasBroadcastFloat16x4(ExpConstantsFp16.UpperRange),
MlasBroadcastFloat16x4(ExpConstantsFp16.LowerRangeSumExp),
MlasBroadcastFloat16x4(ExpConstantsFp16.UpperRangeSumExp),
MlasBroadcastFloat16x4(ExpConstantsFp16.RoundingBias),
MlasBroadcastFloat16x4(ExpConstantsFp16.Log2Reciprocal),
MlasBroadcastFloat16x4(ExpConstantsFp16.Log2High),
MlasBroadcastFloat16x4(ExpConstantsFp16.Log2Mid),
MlasBroadcastFloat16x4(ExpConstantsFp16.Log2Low),
MlasBroadcastFloat16x4(ExpConstantsFp16.poly_0),
MlasBroadcastFloat16x4(ExpConstantsFp16.poly_1),
MlasBroadcastFloat16x4(ExpConstantsFp16.poly_2),
MlasBroadcastFloat16x4(ExpConstantsFp16.poly_3),
MlasBroadcastFloat16x4(ExpConstantsFp16.poly_4),
MlasBroadcastFloat16x4(ExpConstantsFp16.poly_56),
MlasBroadcastFloat16x4(ExpConstantsFp16.MinimumExponent),
MlasBroadcastFloat16x4(ExpConstantsFp16.MaximumExponent),
};
return ExpConstantsFp16x4;
}

Expand Down Expand Up @@ -419,7 +416,7 @@ struct MlasTanhConstants {
T beta_0;
};

const MlasTanhConstants<_mlas_fp16_> TanhConstantsFp16 = {
constexpr MlasTanhConstants<_mlas_fp16_> TanhConstantsFp16 = {
0xc308, // -3.51562
0x4308, // 3.51562
0x0001,
Expand All @@ -432,45 +429,43 @@ const MlasTanhConstants<_mlas_fp16_> TanhConstantsFp16 = {
0x1d03,
};

const MlasTanhConstants<float16x8_t> TanhConstantsFp16x8 = {
MlasBroadcastFloat16x8(TanhConstantsFp16.LowerRange),
MlasBroadcastFloat16x8(TanhConstantsFp16.UpperRange),
MlasBroadcastFloat16x8(TanhConstantsFp16.alpha_7),
MlasBroadcastFloat16x8(TanhConstantsFp16.alpha_5),
MlasBroadcastFloat16x8(TanhConstantsFp16.alpha_3),
MlasBroadcastFloat16x8(TanhConstantsFp16.alpha_1),
MlasBroadcastFloat16x8(TanhConstantsFp16.beta_6),
MlasBroadcastFloat16x8(TanhConstantsFp16.beta_4),
MlasBroadcastFloat16x8(TanhConstantsFp16.beta_2),
MlasBroadcastFloat16x8(TanhConstantsFp16.beta_0),
};

const MlasTanhConstants<float16x4_t> TanhConstantsFp16x4 = {
MlasBroadcastFloat16x4(TanhConstantsFp16.LowerRange),
MlasBroadcastFloat16x4(TanhConstantsFp16.UpperRange),
MlasBroadcastFloat16x4(TanhConstantsFp16.alpha_7),
MlasBroadcastFloat16x4(TanhConstantsFp16.alpha_5),
MlasBroadcastFloat16x4(TanhConstantsFp16.alpha_3),
MlasBroadcastFloat16x4(TanhConstantsFp16.alpha_1),
MlasBroadcastFloat16x4(TanhConstantsFp16.beta_6),
MlasBroadcastFloat16x4(TanhConstantsFp16.beta_4),
MlasBroadcastFloat16x4(TanhConstantsFp16.beta_2),
MlasBroadcastFloat16x4(TanhConstantsFp16.beta_0),
};

template <typename T>
MLAS_FORCEINLINE
MlasTanhConstants<T> Get_Tanh_Constants();

template <>
MLAS_FORCEINLINE
MlasTanhConstants<float16x8_t> Get_Tanh_Constants<float16x8_t>() {
const static MlasTanhConstants<float16x8_t> TanhConstantsFp16x8 = {
MlasBroadcastFloat16x8(TanhConstantsFp16.LowerRange),
MlasBroadcastFloat16x8(TanhConstantsFp16.UpperRange),
MlasBroadcastFloat16x8(TanhConstantsFp16.alpha_7),
MlasBroadcastFloat16x8(TanhConstantsFp16.alpha_5),
MlasBroadcastFloat16x8(TanhConstantsFp16.alpha_3),
MlasBroadcastFloat16x8(TanhConstantsFp16.alpha_1),
MlasBroadcastFloat16x8(TanhConstantsFp16.beta_6),
MlasBroadcastFloat16x8(TanhConstantsFp16.beta_4),
MlasBroadcastFloat16x8(TanhConstantsFp16.beta_2),
MlasBroadcastFloat16x8(TanhConstantsFp16.beta_0),
};
return TanhConstantsFp16x8;
}

template <>
MLAS_FORCEINLINE
MlasTanhConstants<float16x4_t> Get_Tanh_Constants<float16x4_t>() {
const static MlasTanhConstants<float16x4_t> TanhConstantsFp16x4 = {
MlasBroadcastFloat16x4(TanhConstantsFp16.LowerRange),
MlasBroadcastFloat16x4(TanhConstantsFp16.UpperRange),
MlasBroadcastFloat16x4(TanhConstantsFp16.alpha_7),
MlasBroadcastFloat16x4(TanhConstantsFp16.alpha_5),
MlasBroadcastFloat16x4(TanhConstantsFp16.alpha_3),
MlasBroadcastFloat16x4(TanhConstantsFp16.alpha_1),
MlasBroadcastFloat16x4(TanhConstantsFp16.beta_6),
MlasBroadcastFloat16x4(TanhConstantsFp16.beta_4),
MlasBroadcastFloat16x4(TanhConstantsFp16.beta_2),
MlasBroadcastFloat16x4(TanhConstantsFp16.beta_0),
};
return TanhConstantsFp16x4;
}

Expand Down
Loading