Elffuss BraidHub

kikocis/ncnn public

master · 9.3 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 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#ifndef X86_ACTIVATION_H
16#define X86_ACTIVATION_H
17
18#include "mat.h"
19#include "fused_activation.h"
20#include "x86_usability.h"
21
22#if __SSE2__
23#include <emmintrin.h>
24#include "sse_mathfun.h"
25
26static NCNN_FORCEINLINE __m128 sigmoid_sse(__m128 inputs)
27{
28 const __m128 one = _mm_set1_ps(1.0f);
29 return _mm_div_ps(one, _mm_add_ps(one, exp_ps(_mm_sub_ps(_mm_setzero_ps(), inputs))));
30}
31
32static NCNN_FORCEINLINE __m128 tanh_sse(__m128 inputs)
33{
34 const __m128 one = _mm_set1_ps(1.0f);
35 const __m128 two = _mm_set1_ps(2.0f);
36 return _mm_sub_ps(_mm_mul_ps(sigmoid_sse(_mm_mul_ps(inputs, two)), two), one);
37}
38
39static NCNN_FORCEINLINE __m128 mish_sse(__m128 inputs)
40{
41 return _mm_mul_ps(inputs, tanh_sse(log_ps(_mm_add_ps(exp_ps(inputs), _mm_set1_ps(1.f)))));
42}
43
44static NCNN_FORCEINLINE __m128 swish_sse(__m128 inputs)
45{
46 return _mm_mul_ps(inputs, sigmoid_sse(inputs));
47}
48
49static NCNN_FORCEINLINE __m128 hardswish_sse(__m128 inputs, __m128 a, __m128 b)
50{
51 const __m128 one = _mm_set1_ps(1.0f);
52 b = _mm_add_ps(_mm_mul_ps(inputs, a), b);
53 b = _mm_max_ps(b, _mm_setzero_ps());
54 b = _mm_min_ps(b, one);
55 return _mm_mul_ps(b, inputs);
56}
57
58static NCNN_FORCEINLINE __m128 lrelu_sse(__m128 inputs, float slope)
59{
60 __m128 pos = _mm_max_ps(_mm_setzero_ps(), inputs);
61 __m128 neg = _mm_min_ps(_mm_setzero_ps(), inputs);
62 return _mm_add_ps(pos, _mm_mul_ps(_mm_set1_ps(slope), neg));
63}
64
65static NCNN_FORCEINLINE __m128 prelu_sse(__m128 inputs, __m128 alphas)
66{
67 __m128 pos = _mm_max_ps(_mm_setzero_ps(), inputs);
68 __m128 neg = _mm_min_ps(_mm_setzero_ps(), inputs);
69 return _mm_add_ps(pos, _mm_mul_ps(alphas, neg));
70}
71
72static NCNN_FORCEINLINE __m128 elu_sse(__m128 inputs, __m128 alphas)
73{
74 __m128 pos = _mm_max_ps(_mm_setzero_ps(), inputs);
75 __m128 neg = _mm_min_ps(_mm_setzero_ps(), inputs);
76 neg = _mm_sub_ps(exp_ps(neg), _mm_set1_ps(1.f));
77 return _mm_add_ps(pos, _mm_mul_ps(alphas, neg));
78}
79
80static NCNN_FORCEINLINE __m128 activation_sse(__m128 _v, int activation_type, const ncnn::Mat& activation_params)
81{
82 // Process fused activations
83 switch (activation_type)
84 {
85 case 1:
86 {
87 // Relu
88 return _mm_max_ps(_v, _mm_setzero_ps());
89 }
90 case 2:
91 {
92 // Leaky relu
93 return lrelu_sse(_v, activation_params[0]);
94 }
95 case 3:
96 {
97 // min max clip
98 __m128 min = _mm_set1_ps(activation_params[0]);
99 __m128 max = _mm_set1_ps(activation_params[1]);
100 return _mm_min_ps(_mm_max_ps(_v, min), max);
101 }
102 case 4:
103 {
104 // Sigmoid
105 return sigmoid_sse(_v);
106 }
107 case 5:
108 {
109 return mish_sse(_v);
110 }
111 case 6:
112 {
113 __m128 _a = _mm_set1_ps(activation_params[0]);
114 __m128 _b = _mm_set1_ps(activation_params[1]);
115 return hardswish_sse(_v, _a, _b);
116 }
117 }
118
119 return _v;
120}
121
122#if __AVX__
123#include <immintrin.h>
124#include "avx_mathfun.h"
125
126static NCNN_FORCEINLINE __m256 sigmoid_avx(__m256 inputs)
127{
128 const __m256 one = _mm256_set1_ps(1.0f);
129 return _mm256_div_ps(one, _mm256_add_ps(one, exp256_ps(_mm256_sub_ps(_mm256_setzero_ps(), inputs))));
130}
131
132static NCNN_FORCEINLINE __m256 tanh_avx(__m256 inputs)
133{
134 const __m256 one = _mm256_set1_ps(1.0f);
135 const __m256 two = _mm256_set1_ps(2.0f);
136#if __FMA__
137 return _mm256_fmsub_ps(sigmoid_avx(_mm256_mul_ps(inputs, two)), two, one);
138#else
139 return _mm256_sub_ps(_mm256_mul_ps(sigmoid_avx(_mm256_mul_ps(inputs, two)), two), one);
140#endif
141}
142
143static NCNN_FORCEINLINE __m256 mish_avx(__m256 inputs)
144{
145 return _mm256_mul_ps(inputs, tanh_avx(log256_ps(_mm256_add_ps(exp256_ps(inputs), _mm256_set1_ps(1.f)))));
146}
147
148static NCNN_FORCEINLINE __m256 swish_avx(__m256 inputs)
149{
150 return _mm256_mul_ps(inputs, sigmoid_avx(inputs));
151}
152
153static NCNN_FORCEINLINE __m256 hardswish_avx(__m256 inputs, __m256 a, __m256 b)
154{
155 const __m256 one = _mm256_set1_ps(1.0f);
156 b = _mm256_comp_fmadd_ps(inputs, a, b);
157 b = _mm256_max_ps(b, _mm256_setzero_ps());
158 b = _mm256_min_ps(b, one);
159 return _mm256_mul_ps(b, inputs);
160}
161
162static NCNN_FORCEINLINE __m256 lrelu_avx(__m256 inputs, float slope)
163{
164 __m256 pos = _mm256_max_ps(_mm256_setzero_ps(), inputs);
165 __m256 neg = _mm256_min_ps(_mm256_setzero_ps(), inputs);
166 return _mm256_add_ps(pos, _mm256_mul_ps(_mm256_set1_ps(slope), neg));
167}
168
169static NCNN_FORCEINLINE __m256 prelu_avx(__m256 inputs, __m256 alphas)
170{
171 __m256 pos = _mm256_max_ps(_mm256_setzero_ps(), inputs);
172 __m256 neg = _mm256_min_ps(_mm256_setzero_ps(), inputs);
173 return _mm256_add_ps(pos, _mm256_mul_ps(alphas, neg));
174}
175
176static NCNN_FORCEINLINE __m256 elu_avx(__m256 inputs, __m256 alphas)
177{
178 __m256 pos = _mm256_max_ps(_mm256_setzero_ps(), inputs);
179 __m256 neg = _mm256_min_ps(_mm256_setzero_ps(), inputs);
180 neg = _mm256_sub_ps(exp256_ps(neg), _mm256_set1_ps(1.f));
181 return _mm256_add_ps(pos, _mm256_mul_ps(alphas, neg));
182}
183
184static NCNN_FORCEINLINE __m256 activation_avx(__m256 _v, int activation_type, const ncnn::Mat& activation_params)
185{
186 // Process fused activations
187 switch (activation_type)
188 {
189 case 1:
190 {
191 // Relu
192 return _mm256_max_ps(_v, _mm256_setzero_ps());
193 }
194 case 2:
195 {
196 // Leaky relu
197 return lrelu_avx(_v, activation_params[0]);
198 }
199 case 3:
200 {
201 // min max clip
202 __m256 min = _mm256_set1_ps(activation_params[0]);
203 __m256 max = _mm256_set1_ps(activation_params[1]);
204 return _mm256_min_ps(_mm256_max_ps(_v, min), max);
205 }
206 case 4:
207 {
208 // Sigmoid
209 return sigmoid_avx(_v);
210 }
211 case 5:
212 {
213 return mish_avx(_v);
214 }
215 case 6:
216 {
217 __m256 _a = _mm256_set1_ps(activation_params[0]);
218 __m256 _b = _mm256_set1_ps(activation_params[1]);
219 return hardswish_avx(_v, _a, _b);
220 }
221 }
222
223 return _v;
224}
225
226#if __AVX512F__
227#include "avx512_mathfun.h"
228
229static NCNN_FORCEINLINE __m512 sigmoid_avx512(__m512 inputs)
230{
231 const __m512 one = _mm512_set1_ps(1.0f);
232 return _mm512_div_ps(one, _mm512_add_ps(one, exp512_ps(_mm512_sub_ps(_mm512_setzero_ps(), inputs))));
233}
234
235static NCNN_FORCEINLINE __m512 tanh_avx512(__m512 inputs)
236{
237 const __m512 one = _mm512_set1_ps(1.0f);
238 const __m512 two = _mm512_set1_ps(2.0f);
239 return _mm512_fmsub_ps(sigmoid_avx512(_mm512_mul_ps(inputs, two)), two, one);
240}
241
242static NCNN_FORCEINLINE __m512 mish_avx512(__m512 inputs)
243{
244 return _mm512_mul_ps(inputs, tanh_avx512(log512_ps(_mm512_add_ps(exp512_ps(inputs), _mm512_set1_ps(1.f)))));
245}
246
247static NCNN_FORCEINLINE __m512 swish_avx512(__m512 inputs)
248{
249 return _mm512_mul_ps(inputs, sigmoid_avx512(inputs));
250}
251
252static NCNN_FORCEINLINE __m512 hardswish_avx512(__m512 inputs, __m512 a, __m512 b)
253{
254 const __m512 one = _mm512_set1_ps(1.0f);
255 b = _mm512_fmadd_ps(inputs, a, b);
256 b = _mm512_max_ps(b, _mm512_setzero_ps());
257 b = _mm512_min_ps(b, one);
258 return _mm512_mul_ps(b, inputs);
259}
260
261static NCNN_FORCEINLINE __m512 lrelu_avx512(__m512 inputs, float slope)
262{
263 __mmask16 _is_negative = _mm512_cmp_ps_mask(inputs, _mm512_setzero_ps(), _CMP_LT_OQ);
264 return _mm512_mask_mul_ps(inputs, _is_negative, inputs, _mm512_set1_ps(slope));
265}
266
267static NCNN_FORCEINLINE __m512 elu_avx512(__m512 inputs, __m512 alphas)
268{
269 __m512 pos = _mm512_max_ps(_mm512_setzero_ps(), inputs);
270 __m512 neg = _mm512_min_ps(_mm512_setzero_ps(), inputs);
271 neg = _mm512_sub_ps(exp512_ps(neg), _mm512_set1_ps(1.f));
272 return _mm512_add_ps(pos, _mm512_mul_ps(alphas, neg));
273}
274
275static NCNN_FORCEINLINE __m512 prelu_avx512(__m512 inputs, __m512 alphas)
276{
277 __m512 pos = _mm512_max_ps(_mm512_setzero_ps(), inputs);
278 __m512 neg = _mm512_min_ps(_mm512_setzero_ps(), inputs);
279 return _mm512_add_ps(pos, _mm512_mul_ps(alphas, neg));
280}
281
282static NCNN_FORCEINLINE __m512 activation_avx512(__m512 _v, int activation_type, const ncnn::Mat& activation_params)
283{
284 // Process fused activations
285 switch (activation_type)
286 {
287 case 1:
288 {
289 // Relu
290 return _mm512_max_ps(_v, _mm512_setzero_ps());
291 }
292 case 2:
293 {
294 // Leaky relu
295 return lrelu_avx512(_v, activation_params[0]);
296 }
297 case 3:
298 {
299 // min max clip
300 __m512 min = _mm512_set1_ps(activation_params[0]);
301 __m512 max = _mm512_set1_ps(activation_params[1]);
302 return _mm512_min_ps(_mm512_max_ps(_v, min), max);
303 }
304 case 4:
305 {
306 // Sigmoid
307 return sigmoid_avx512(_v);
308 }
309 case 5:
310 {
311 return mish_avx512(_v);
312 }
313 case 6:
314 {
315 __m512 _a = _mm512_set1_ps(activation_params[0]);
316 __m512 _b = _mm512_set1_ps(activation_params[1]);
317 return hardswish_avx512(_v, _a, _b);
318 }
319 }
320
321 return _v;
322}
323#endif // __AVX512F__
324#endif // __AVX__
325#endif // __SSE2__
326
327#endif // X86_ACTIVATION_H