diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/CL/cl_kernels/activation_float_helpers.h | 5 | ||||
-rw-r--r-- | src/core/NEON/NEMath.h | 18 | ||||
-rw-r--r-- | src/core/NEON/NEMath.inl | 50 | ||||
-rw-r--r-- | src/core/NEON/wrapper/intrinsics/erf.h | 51 | ||||
-rw-r--r-- | src/core/NEON/wrapper/intrinsics/intrinsics.h | 3 | ||||
-rw-r--r-- | src/core/Utils.cpp | 3 |
6 files changed, 125 insertions, 5 deletions
diff --git a/src/core/CL/cl_kernels/activation_float_helpers.h b/src/core/CL/cl_kernels/activation_float_helpers.h index 91d7197889..3f93c8d6fc 100644 --- a/src/core/CL/cl_kernels/activation_float_helpers.h +++ b/src/core/CL/cl_kernels/activation_float_helpers.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020 Arm Limited. + * Copyright (c) 2019-2020, 2022 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -69,6 +69,9 @@ // Linear Activation #define linear_op(DATA_TYPE, VEC_SIZE, x, A_VAL, B_VAL) (MLA((DATA_TYPE)B_VAL, (DATA_TYPE)A_VAL, x)) +// GELU Activation +#define gelu_op(DATA_TYPE, VEC_SIZE, x, A_VAL, B_VAL) (x * (DATA_TYPE)0.5 * ((DATA_TYPE)1.0 + erf(x / (DATA_TYPE)1.41421356237))) + // Identity Activation #define identity_op(DATA_TYPE, VEC_SIZE, x, A_VAL, B_VAL) (x) diff --git a/src/core/NEON/NEMath.h b/src/core/NEON/NEMath.h index 8118c4701f..9e81c38ad8 100644 --- a/src/core/NEON/NEMath.h +++ b/src/core/NEON/NEMath.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2021 Arm Limited. + * Copyright (c) 2016-2022 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -94,6 +94,14 @@ float32x4_t vtaylor_polyq_f32(float32x4_t x, const std::array<float32x4_t, 8> &c */ float32x4_t vexpq_f32(float32x4_t x); +/** Calculate error function + * + * @param[in] x Input vector in F32 format. + * + * @return The calculated erf. + */ +float32x4_t verfq_f32(float32x4_t x); + /** Calculate logarithm * * @param[in] x Input vector value in F32 format. @@ -308,6 +316,14 @@ float16x8_t vinvsqrtq_f16(float16x8_t x); */ float16x8_t vexpq_f16(float16x8_t x); +/** Calculate error function + * + * @param[in] x Input vector in F16 format. + * + * @return The calculated erf. + */ +float16x8_t verfq_f16(float16x8_t x); + /** Calculate n power of a number. * * pow(x,n) = e^(n*log(x)) diff --git a/src/core/NEON/NEMath.inl b/src/core/NEON/NEMath.inl index 05cf3013bc..1b0b894153 100644 --- a/src/core/NEON/NEMath.inl +++ b/src/core/NEON/NEMath.inl @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016-2021 Arm Limited. + * Copyright (c) 2016-2022 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -166,6 +166,43 @@ inline float32x4_t vexpq_f32(float32x4_t x) return poly; } +#ifdef __aarch64__ +inline float32x4_t verfq_f32(float32x4_t x) +{ + static const float erffdata[4] = { 0.278393f, 0.230389f, 0.000972f, 0.078108f }; + static const float32x4_t coeffdata = vld1q_f32(erffdata); + static const float32x4_t onev{ vdupq_n_f32(1.0f) }; + + uint32x4_t selector = vcltzq_f32(x); + + float32x4_t absx = vabsq_f32(x); + float32x4_t absx2 = vmulq_f32(x, x); + float32x4_t absx3 = vmulq_f32(absx2, absx); + float32x4_t absx4 = vmulq_f32(absx2, absx2); + + float32x4_t denom = onev; + denom = vfmaq_laneq_f32(denom, absx, coeffdata, 0); + denom = vfmaq_laneq_f32(denom, absx2, coeffdata, 1); + denom = vfmaq_laneq_f32(denom, absx3, coeffdata, 2); + denom = vfmaq_laneq_f32(denom, absx4, coeffdata, 3); + + denom = vmulq_f32(denom, denom); + denom = vmulq_f32(denom, denom); + + float32x4_t fract = onev; + fract = vdivq_f32(fract, denom); + + float32x4_t result = onev; + result = vsubq_f32(result, fract); + + float32x4_t inverse = vnegq_f32(result); + + result = vbslq_f32(selector, inverse, result); + + return result; +} +#endif // #ifdef __aarch64__ + inline float32x4_t vlogq_f32(float32x4_t x) { static const int32x4_t CONST_127 = vdupq_n_s32(127); // 127 @@ -517,6 +554,17 @@ inline float16x8_t vexpq_f16(float16x8_t x) return res; } +#ifdef __aarch64__ +inline float16x8_t verfq_f16(float16x8_t x) +{ + const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x)); + const float32x4_t x_low = vcvt_f32_f16(vget_low_f16(x)); + + const float16x8_t res = vcombine_f16(vcvt_f16_f32(verfq_f32(x_low)), vcvt_f16_f32(verfq_f32(x_high))); + return res; +} +#endif // #ifdef __aarch64__ + inline float16x8_t vlogq_f16(float16x8_t x) { const float32x4_t x_high = vcvt_f32_f16(vget_high_f16(x)); diff --git a/src/core/NEON/wrapper/intrinsics/erf.h b/src/core/NEON/wrapper/intrinsics/erf.h new file mode 100644 index 0000000000..e2207648e5 --- /dev/null +++ b/src/core/NEON/wrapper/intrinsics/erf.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2022 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef ARM_COMPUTE_WRAPPER_ERF_H +#define ARM_COMPUTE_WRAPPER_ERF_H + +#include "src/core/NEON/NEMath.h" +#include <arm_neon.h> + +namespace arm_compute +{ +namespace wrapper +{ +#define VERF_IMPL(vtype, prefix, postfix) \ + inline vtype verf(const vtype &a) \ + { \ + return prefix##_##postfix(a); \ + } + +VERF_IMPL(float32x4_t, verfq, f32) +#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC +VERF_IMPL(float16x8_t, verfq, f16) +#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC + +#undef VERF_IMPL + +} // namespace wrapper +} // namespace arm_compute + +#endif /* ARM_COMPUTE_WRAPPER_ERF_H */ diff --git a/src/core/NEON/wrapper/intrinsics/intrinsics.h b/src/core/NEON/wrapper/intrinsics/intrinsics.h index 871d9cc5ac..0256e0a8c8 100644 --- a/src/core/NEON/wrapper/intrinsics/intrinsics.h +++ b/src/core/NEON/wrapper/intrinsics/intrinsics.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2021 Arm Limited. + * Copyright (c) 2018-2022 Arm Limited. * * SPDX-License-Identifier: MIT * @@ -39,6 +39,7 @@ #include "src/core/NEON/wrapper/intrinsics/div.h" #include "src/core/NEON/wrapper/intrinsics/dup_n.h" #include "src/core/NEON/wrapper/intrinsics/eor.h" +#include "src/core/NEON/wrapper/intrinsics/erf.h" #include "src/core/NEON/wrapper/intrinsics/exp.h" #include "src/core/NEON/wrapper/intrinsics/ext.h" #include "src/core/NEON/wrapper/intrinsics/gethigh.h" diff --git a/src/core/Utils.cpp b/src/core/Utils.cpp index 904362e1b4..48eb8b9bf5 100644 --- a/src/core/Utils.cpp +++ b/src/core/Utils.cpp @@ -177,7 +177,8 @@ const std::string &string_from_activation_func(ActivationLayerInfo::ActivationFu { ActivationLayerInfo::ActivationFunction::SQUARE, "SQUARE" }, { ActivationLayerInfo::ActivationFunction::TANH, "TANH" }, { ActivationLayerInfo::ActivationFunction::IDENTITY, "IDENTITY" }, - { ActivationLayerInfo::ActivationFunction::HARD_SWISH, "HARD_SWISH" } + { ActivationLayerInfo::ActivationFunction::HARD_SWISH, "HARD_SWISH" }, + { ActivationLayerInfo::ActivationFunction::GELU, "GELU" } }; |