| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "gelu_arm.h" |
| 16 | |
| 17 | #if __ARM_NEON |
| 18 | #include <arm_neon.h> |
| 19 | #include "arm_usability.h" |
| 20 | #include "neon_mathfun.h" |
| 21 | #if NCNN_ARM82 |
| 22 | #include "neon_mathfun_fp16s.h" |
| 23 | #endif |
| 24 | #endif // __ARM_NEON |
| 25 | |
| 26 | namespace ncnn { |
| 27 | |
| 28 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 29 | int GELU_arm::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const |
| 30 | { |
| 31 | int w = bottom_top_blob.w; |
| 32 | int h = bottom_top_blob.h; |
| 33 | int d = bottom_top_blob.d; |
| 34 | int elempack = bottom_top_blob.elempack; |
| 35 | int channels = bottom_top_blob.c; |
| 36 | int size = w * h * d * elempack; |
| 37 | |
| 38 | #pragma omp parallel for num_threads(opt.num_threads) |
| 39 | for (int q = 0; q < channels; q++) |
| 40 | { |
| 41 | __fp16* ptr = (__fp16*)bottom_top_blob.channel(q); |
| 42 | |
| 43 | int i = 0; |
| 44 | |
| 45 | for (; i + 3 < size; i += 4) |
| 46 | { |
| 47 | float32x4_t _pLoad = vcvt_f32_f16(vld1_f16(ptr)); |
| 48 | |
| 49 | float32x4_t _blob = vmulq_f32(_pLoad, _pLoad); |
| 50 | _blob = vmulq_f32(_pLoad, _blob); |
| 51 | _blob = vmulq_f32(vdupq_n_f32(0.044715f * 0.79788452f), _blob); |
| 52 | _blob = vmlaq_f32(_blob, vdupq_n_f32(0.79788452f), _pLoad); |
| 53 | _blob = tanh_ps(_blob); |
| 54 | _blob = vaddq_f32(vdupq_n_f32(1.f), _blob); |
| 55 | _blob = vmulq_f32(vdupq_n_f32(0.5f), vmulq_f32(_blob, _pLoad)); |
| 56 | vst1_f16(ptr, vcvt_f16_f32(_blob)); |
| 57 | ptr += 4; |
| 58 | } |
| 59 | |
| 60 | for (; i < size; i++) |
| 61 | { |
| 62 | float v = (float)*ptr; |
| 63 | v = 0.5f * v * (1.0f + tanhf(0.79788452f * (v + 0.044715f * v * v * v))); |
| 64 | *ptr = (__fp16)v; |
| 65 | ptr++; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | int GELU_arm::forward_inplace_fp16sa(Mat& bottom_top_blob, const Option& opt) const |
| 73 | { |
| 74 | int w = bottom_top_blob.w; |
| 75 | int h = bottom_top_blob.h; |
| 76 | int d = bottom_top_blob.d; |
| 77 | int elempack = bottom_top_blob.elempack; |
| 78 | int channels = bottom_top_blob.c; |
| 79 | int size = w * h * d * elempack; |
| 80 | |
| 81 | #pragma omp parallel for num_threads(opt.num_threads) |
| 82 | for (int q = 0; q < channels; q++) |
| 83 | { |
| 84 | __fp16* ptr = (__fp16*)bottom_top_blob.channel(q); |
| 85 | |
| 86 | int i = 0; |
| 87 | |
| 88 | for (; i + 7 < size; i += 8) |
| 89 | { |
| 90 | float16x8_t _pLoad = vld1q_f16(ptr); |
| 91 | |
| 92 | float16x8_t _blob = vmulq_f16(_pLoad, _pLoad); |
| 93 | _blob = vmulq_f16(_pLoad, _blob); |
| 94 | _blob = vmulq_f16(vdupq_n_f16(0.044715f * 0.79788452f), _blob); |
| 95 | _blob = vfmaq_f16(_blob, vdupq_n_f16(0.79788452f), _pLoad); |
| 96 | _blob = tanh_ps_f16(_blob); |
| 97 | _blob = vaddq_f16(vdupq_n_f16(1.f), _blob); |
| 98 | _blob = vmulq_f16(vdupq_n_f16(0.5f), vmulq_f16(_blob, _pLoad)); |
| 99 | vst1q_f16(ptr, _blob); |
| 100 | ptr += 8; |
| 101 | } |
| 102 | |
| 103 | for (; i < size; i++) |
| 104 | { |
| 105 | *ptr = (__fp16)0.5f * *ptr * (__fp16)(1.0f + tanhf((__fp16)0.79788452f * (*ptr + (__fp16)0.044715f * *ptr * *ptr * *ptr))); |
| 106 | ptr++; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | } // namespace ncnn |