Elffuss BraidHub

kikocis/ncnn public

master · 4.6 KiBRawPermalink
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 "neon_mathfun.h"
20#endif // __ARM_NEON
21
22#include "arm_usability.h"
23#include "cpu.h"
24
25namespace ncnn {
26
27GELU_arm::GELU_arm()
28{
29#if __ARM_NEON
30 support_packing = true;
31#if NCNN_ARM82
32 support_fp16_storage = cpu_support_arm_asimdhp();
33#endif
34#endif // __ARM_NEON
35
36#if NCNN_BF16
37 support_bf16_storage = true;
38#endif
39}
40
41int GELU_arm::create_pipeline(const Option& /*opt*/)
42{
43 if (!fast_gelu)
44 {
45 support_packing = false;
46 support_fp16_storage = false;
47 support_bf16_storage = false;
48 }
49 return 0;
50}
51
52int GELU_arm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
53{
54 if (!fast_gelu)
55 {
56 return GELU::forward_inplace(bottom_top_blob, opt);
57 }
58
59 int elembits = bottom_top_blob.elembits();
60
61#if NCNN_ARM82
62 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
63 {
64 if (opt.use_fp16_arithmetic)
65 return forward_inplace_fp16sa(bottom_top_blob, opt);
66 else
67 return forward_inplace_fp16s(bottom_top_blob, opt);
68 }
69#endif
70
71#if NCNN_BF16
72 if (opt.use_bf16_storage && elembits == 16)
73 return forward_inplace_bf16s(bottom_top_blob, opt);
74#endif
75
76 int w = bottom_top_blob.w;
77 int h = bottom_top_blob.h;
78 int d = bottom_top_blob.d;
79 int elempack = bottom_top_blob.elempack;
80 int channels = bottom_top_blob.c;
81 int size = w * h * d * elempack;
82
83 #pragma omp parallel for num_threads(opt.num_threads)
84 for (int q = 0; q < channels; q++)
85 {
86 float* ptr = bottom_top_blob.channel(q);
87
88 int i = 0;
89
90#if __ARM_NEON
91 for (; i + 3 < size; i += 4)
92 {
93 float32x4_t _pLoad = vld1q_f32(ptr);
94
95 float32x4_t _blob = vmulq_f32(_pLoad, _pLoad);
96 _blob = vmulq_f32(_pLoad, _blob);
97 _blob = vmulq_f32(vdupq_n_f32(0.044715f * 0.79788452f), _blob);
98 _blob = vmlaq_f32(_blob, vdupq_n_f32(0.79788452f), _pLoad);
99 _blob = tanh_ps(_blob);
100 _blob = vaddq_f32(vdupq_n_f32(1.f), _blob);
101 _blob = vmulq_f32(vdupq_n_f32(0.5f), vmulq_f32(_blob, _pLoad));
102 vst1q_f32(ptr, _blob);
103 ptr += 4;
104 }
105#endif
106 for (; i < size; i++)
107 {
108 // y = 0.5x * (1 + tanh(sqrt(2/Pi) * (x + 0.044715x^3)))
109 *ptr = 0.5f * *ptr * (1.0f + tanhf(0.79788452f * (*ptr + 0.044715f * *ptr * *ptr * *ptr)));
110
111 ptr++;
112 }
113 }
114
115 return 0;
116}
117
118#if NCNN_BF16
119int GELU_arm::forward_inplace_bf16s(Mat& bottom_top_blob, const Option& opt) const
120{
121 int w = bottom_top_blob.w;
122 int h = bottom_top_blob.h;
123 int d = bottom_top_blob.d;
124 int elempack = bottom_top_blob.elempack;
125 int channels = bottom_top_blob.c;
126 int size = w * h * d * elempack;
127
128 #pragma omp parallel for num_threads(opt.num_threads)
129 for (int q = 0; q < channels; q++)
130 {
131 unsigned short* ptr = bottom_top_blob.channel(q);
132
133 int i = 0;
134
135#if __ARM_NEON
136 for (; i + 3 < size; i += 4)
137 {
138 float32x4_t _pLoad = bfloat2float(vld1_u16(ptr));
139
140 float32x4_t _blob = vmulq_f32(_pLoad, _pLoad);
141 _blob = vmulq_f32(_pLoad, _blob);
142 _blob = vmulq_f32(vdupq_n_f32(0.044715f * 0.79788452f), _blob);
143 _blob = vmlaq_f32(_blob, vdupq_n_f32(0.79788452f), _pLoad);
144 _blob = tanh_ps(_blob);
145 _blob = vaddq_f32(vdupq_n_f32(1.f), _blob);
146 _blob = vmulq_f32(vdupq_n_f32(0.5f), vmulq_f32(_blob, _pLoad));
147 vst1_u16(ptr, float2bfloat(_blob));
148 ptr += 4;
149 }
150#endif // __ARM_NEON
151
152 for (; i < size; i++)
153 {
154 float v = bfloat16_to_float32(*ptr);
155 v = 0.5f * v * (1.0f + tanhf(0.79788452f * (v + 0.044715f * v * v * v)));
156 *ptr = float32_to_bfloat16(v);
157 ptr++;
158 }
159 }
160
161 return 0;
162}
163#endif // NCNN_BF16
164
165} // namespace ncnn