Elffuss BraidHub

kikocis/ncnn public

master · 7.1 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2019 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 ARM_ACTIVATION_H
16#define ARM_ACTIVATION_H
17
18#include "fused_activation.h"
19
20#if __ARM_NEON
21#include <arm_neon.h>
22#include "neon_mathfun.h"
23
24static inline float32x4_t activation_ps(float32x4_t _v, int activation_type, const ncnn::Mat& activation_params)
25{
26 if (activation_type == 1)
27 {
28 const float32x4_t _zero = vdupq_n_f32(0.f);
29 _v = vmaxq_f32(_v, _zero);
30 }
31 else if (activation_type == 2)
32 {
33 const float32x4_t _zero = vdupq_n_f32(0.f);
34 const float32x4_t _slope = vdupq_n_f32(activation_params[0]);
35 const uint32x4_t _lemask = vcleq_f32(_v, _zero);
36 float32x4_t _ps = vmulq_f32(_v, _slope);
37 _v = vbslq_f32(_lemask, _ps, _v);
38 }
39 else if (activation_type == 3)
40 {
41 const float32x4_t _min = vdupq_n_f32(activation_params[0]);
42 const float32x4_t _max = vdupq_n_f32(activation_params[1]);
43 _v = vmaxq_f32(_v, _min);
44 _v = vminq_f32(_v, _max);
45 }
46 else if (activation_type == 4)
47 {
48 _v = sigmoid_ps(_v);
49 }
50 else if (activation_type == 5)
51 {
52 _v = vmulq_f32(_v, tanh_ps(log_ps(vaddq_f32(exp_ps(_v), vdupq_n_f32(1.f)))));
53 }
54 else if (activation_type == 6)
55 {
56 const float alpha = activation_params[0];
57 const float beta = activation_params[1];
58 const float32x4_t _zero = vdupq_n_f32(0.f);
59 const float32x4_t _one = vdupq_n_f32(1.f);
60 float32x4_t _ans = vdupq_n_f32(beta);
61 _ans = vmlaq_n_f32(_ans, _v, alpha);
62 _ans = vmaxq_f32(_ans, _zero);
63 _ans = vminq_f32(_ans, _one);
64 _v = vmulq_f32(_ans, _v);
65 }
66
67 return _v;
68}
69
70#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
71#include "arm_usability.h"
72#include "neon_mathfun_fp16s.h"
73
74static inline __fp16 activation_ss_f16(__fp16 v, int activation_type, const ncnn::Mat& activation_params)
75{
76 if (activation_type == 1)
77 {
78 v = std::max(v, (__fp16)0.f);
79 }
80 else if (activation_type == 2)
81 {
82 const __fp16 slope = (__fp16)(activation_params[0]);
83 v = v > 0.f ? v : v * slope;
84 }
85 else if (activation_type == 3)
86 {
87 const __fp16 min = (__fp16)(activation_params[0]);
88 const __fp16 max = (__fp16)(activation_params[1]);
89 if (v < min)
90 v = min;
91 if (v > max)
92 v = max;
93 }
94 else if (activation_type == 4)
95 {
96 v = (__fp16)1.f / ((__fp16)1.f + (__fp16)expf(-v));
97 }
98 else if (activation_type == 5)
99 {
100 v = v * (__fp16)tanhf(logf(expf((float)v) + 1.f));
101 }
102 else if (activation_type == 6)
103 {
104 const __fp16 alpha = (__fp16)(activation_params[0]);
105 const __fp16 beta = (__fp16)(activation_params[1]);
106 const __fp16 lower = -beta / alpha;
107 const __fp16 upper = ((__fp16)1.f / alpha) + lower;
108 if (v < lower)
109 v = (__fp16)0.f;
110 else if (v > upper)
111 ;
112 else
113 v = v * (v * alpha + beta);
114 }
115
116 return v;
117}
118
119static inline float16x4_t activation_ps_f16(float16x4_t _v, int activation_type, const ncnn::Mat& activation_params)
120{
121 if (activation_type == 1)
122 {
123 const float16x4_t _zero = vdup_n_f16(0.f);
124 _v = vmax_f16(_v, _zero);
125 }
126 else if (activation_type == 2)
127 {
128 const float16x4_t _zero = vdup_n_f16(0.f);
129#if defined(_MSC_VER) && !defined(__clang__)
130 const float16x4_t _slope = vcvt_f16_f32(vdupq_n_f32(activation_params[0]));
131#else
132 const float16x4_t _slope = vdup_n_f16((__fp16)activation_params[0]);
133#endif
134 const uint16x4_t _lemask = vcle_f16(_v, _zero);
135 float16x4_t _ps = vmul_f16(_v, _slope);
136 _v = vbsl_f16(_lemask, _ps, _v);
137 }
138 else if (activation_type == 3)
139 {
140 const float16x4_t _min = vdup_n_f16((__fp16)activation_params[0]);
141 const float16x4_t _max = vdup_n_f16((__fp16)activation_params[1]);
142 _v = vmax_f16(_v, _min);
143 _v = vmin_f16(_v, _max);
144 }
145 else if (activation_type == 4)
146 {
147 _v = sigmoid_ps_f16(_v);
148 }
149 else if (activation_type == 5)
150 {
151 _v = vmul_f16(_v, tanh_ps_f16(log_ps_f16(vadd_f16(exp_ps_f16(_v), vdup_n_f16(1.f)))));
152 }
153 else if (activation_type == 6)
154 {
155 const __fp16 alpha = (__fp16)activation_params[0];
156 const __fp16 beta = (__fp16)activation_params[1];
157 const float16x4_t _zero = vdup_n_f16(0.f);
158 const float16x4_t _one = vdup_n_f16(1.f);
159 float16x4_t _ans = vdup_n_f16(beta);
160 _ans = vfma_n_f16(_ans, _v, alpha);
161 _ans = vmax_f16(_ans, _zero);
162 _ans = vmin_f16(_ans, _one);
163 _v = vmul_f16(_ans, _v);
164 }
165
166 return _v;
167}
168
169static inline float16x8_t activation_ps_f16(float16x8_t _v, int activation_type, const ncnn::Mat& activation_params)
170{
171 if (activation_type == 1)
172 {
173 const float16x8_t _zero = vdupq_n_f16(0.f);
174 _v = vmaxq_f16(_v, _zero);
175 }
176 else if (activation_type == 2)
177 {
178 const float16x8_t _zero = vdupq_n_f16(0.f);
179#if defined(_MSC_VER) && !defined(__clang__)
180 const float16x4_t _slope0 = vcvt_f16_f32(vdupq_n_f32(activation_params[0]));
181 const float16x8_t _slope = vcombine_f16(_slope0, _slope0);
182#else
183 const float16x8_t _slope = vdupq_n_f16((__fp16)activation_params[0]);
184#endif
185 const uint16x8_t _lemask = vcleq_f16(_v, _zero);
186 float16x8_t _ps = vmulq_f16(_v, _slope);
187 _v = vbslq_f16(_lemask, _ps, _v);
188 }
189 else if (activation_type == 3)
190 {
191 const float16x8_t _min = vdupq_n_f16((__fp16)activation_params[0]);
192 const float16x8_t _max = vdupq_n_f16((__fp16)activation_params[1]);
193 _v = vmaxq_f16(_v, _min);
194 _v = vminq_f16(_v, _max);
195 }
196 else if (activation_type == 4)
197 {
198 _v = sigmoid_ps_f16(_v);
199 }
200 else if (activation_type == 5)
201 {
202 _v = vmulq_f16(_v, tanh_ps_f16(log_ps_f16(vaddq_f16(exp_ps_f16(_v), vdupq_n_f16(1.f)))));
203 }
204 else if (activation_type == 6)
205 {
206 const __fp16 alpha_fp16 = (__fp16)activation_params[0];
207 const __fp16 beta_fp16 = (__fp16)activation_params[1];
208 const float16x8_t _zero = vdupq_n_f16(0.f);
209 const float16x8_t _one = vdupq_n_f16(1.f);
210 float16x8_t _ans = vdupq_n_f16(beta_fp16);
211 _ans = vfmaq_n_f16(_ans, _v, alpha_fp16);
212 _ans = vmaxq_f16(_ans, _zero);
213 _ans = vminq_f16(_ans, _one);
214 _v = vmulq_f16(_ans, _v);
215 }
216 return _v;
217}
218#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
219#endif // __ARM_NEON
220
221#endif // ARM_ACTIVATION_H