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 all commits
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
151 changes: 73 additions & 78 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,67 +63,65 @@ 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();
const MlasExpConstants<T>& Get_Exp_Constants();

template <>
MLAS_FORCEINLINE
MlasExpConstants<float16x8_t> Get_Exp_Constants<float16x8_t>() {
const 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 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;
}

// Range reduction + polynomial approximation. Refer algorithm details to MlasComputeExpVector.
template<typename T>
MLAS_FORCEINLINE
T Exp_Vector_Fp16(T x) {
const auto constants = Get_Exp_Constants<T>();
const auto& constants = Get_Exp_Constants<T>();
auto clamped_x = MlasClampFloat16(x, constants.LowerRange, constants.UpperRange);

// integral
Expand Down Expand Up @@ -242,7 +239,7 @@ void Exp_Kernel_Fp16(const MLAS_FP16* Input, MLAS_FP16* Output, size_t N) {
template<typename T>
MLAS_FORCEINLINE
T SumExp_Vector_Fp16(T x, T negative_maximum) {
const auto constants = Get_Exp_Constants<T>();
const auto& constants = Get_Exp_Constants<T>();
auto clamped_x = MlasMaximumFloat16(MlasAddFloat16(x, negative_maximum), constants.LowerRangeSumExp);

// integral
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,53 +429,51 @@ 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();
const MlasTanhConstants<T>& Get_Tanh_Constants();

template <>
MLAS_FORCEINLINE
MlasTanhConstants<float16x8_t> Get_Tanh_Constants<float16x8_t>() {
const 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 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;
}

// TODO(fajin): optimize polynomial coefficients
template <typename T>
MLAS_FORCEINLINE
T Tanh_Vector_Fp16(T x) {
const auto constants = Get_Tanh_Constants<T>();
const auto& constants = Get_Tanh_Constants<T>();
x = MlasClampFloat16(x, constants.LowerRange, constants.UpperRange);

T x_2 = MlasMultiplyFloat16(x, x);
Expand Down
Loading