Elffuss BraidHub

kikocis/ncnn public

master · 14.5 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 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 "unaryop_arm.h"
16
17// #include <fenv.h>
18#include <float.h>
19
20#if __ARM_NEON
21#include <arm_neon.h>
22#include "arm_usability.h"
23#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
24#include "neon_mathfun_fp16s.h"
25#endif
26#endif // __ARM_NEON
27
28namespace ncnn {
29
30#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
31template<typename Op>
32static int unary_op_inplace_fp16s(Mat& a, const Option& opt)
33{
34 Op op;
35
36 int w = a.w;
37 int h = a.h;
38 int d = a.d;
39 int channels = a.c;
40 int elempack = a.elempack;
41 int size = w * h * d * elempack;
42
43 #pragma omp parallel for num_threads(opt.num_threads)
44 for (int q = 0; q < channels; q++)
45 {
46 __fp16* ptr = a.channel(q);
47
48 int i = 0;
49 for (; i + 15 < size; i += 16)
50 {
51 float16x8_t _p0 = vld1q_f16(ptr);
52 float16x8_t _p1 = vld1q_f16(ptr + 8);
53 _p0 = op.func_pack8(_p0);
54 _p1 = op.func_pack8(_p1);
55 vst1q_f16(ptr, _p0);
56 vst1q_f16(ptr + 8, _p1);
57 ptr += 16;
58 }
59 for (; i + 7 < size; i += 8)
60 {
61 float16x8_t _p = vld1q_f16(ptr);
62 _p = op.func_pack8(_p);
63 vst1q_f16(ptr, _p);
64 ptr += 8;
65 }
66 for (; i + 3 < size; i += 4)
67 {
68 float16x4_t _p = vld1_f16(ptr);
69 _p = op.func_pack4(_p);
70 vst1_f16(ptr, _p);
71 ptr += 4;
72 }
73 for (; i < size; i++)
74 {
75 *ptr = op.func(*ptr);
76 ptr++;
77 }
78 }
79
80 return 0;
81}
82
83namespace UnaryOp_arm_functor {
84
85struct unary_op_abs_fp16s
86{
87 __fp16 func(const __fp16& x) const
88 {
89 return (__fp16)fabsf(x);
90 }
91 float16x4_t func_pack4(const float16x4_t& x) const
92 {
93 return vabs_f16(x);
94 }
95 float16x8_t func_pack8(const float16x8_t& x) const
96 {
97 return vabsq_f16(x);
98 }
99};
100
101struct unary_op_neg_fp16s
102{
103 __fp16 func(const __fp16& x) const
104 {
105 return -x;
106 }
107 float16x4_t func_pack4(const float16x4_t& x) const
108 {
109 return vneg_f16(x);
110 }
111 float16x8_t func_pack8(const float16x8_t& x) const
112 {
113 return vnegq_f16(x);
114 }
115};
116
117struct unary_op_floor_fp16s
118{
119 __fp16 func(const __fp16& x) const
120 {
121 return (__fp16)floorf(x);
122 }
123 float16x4_t func_pack4(const float16x4_t& x) const
124 {
125 return vcvt_f16_s16(vcvtm_s16_f16(x));
126 }
127 float16x8_t func_pack8(const float16x8_t& x) const
128 {
129 return vcvtq_f16_s16(vcvtmq_s16_f16(x));
130 }
131};
132
133struct unary_op_ceil_fp16s
134{
135 __fp16 func(const __fp16& x) const
136 {
137 return (__fp16)ceilf(x);
138 }
139 float16x4_t func_pack4(const float16x4_t& x) const
140 {
141 return vcvt_f16_s16(vcvtp_s16_f16(x));
142 }
143 float16x8_t func_pack8(const float16x8_t& x) const
144 {
145 return vcvtq_f16_s16(vcvtpq_s16_f16(x));
146 }
147};
148
149struct unary_op_square_fp16s
150{
151 __fp16 func(const __fp16& x) const
152 {
153 return x * x;
154 }
155 float16x4_t func_pack4(const float16x4_t& x) const
156 {
157 return vmul_f16(x, x);
158 }
159 float16x8_t func_pack8(const float16x8_t& x) const
160 {
161 return vmulq_f16(x, x);
162 }
163};
164
165struct unary_op_sqrt_fp16s
166{
167 __fp16 func(const __fp16& x) const
168 {
169 return (__fp16)sqrtf(x);
170 }
171 float16x4_t func_pack4(const float16x4_t& x) const
172 {
173 return vsqrt_f16(x);
174 }
175 float16x8_t func_pack8(const float16x8_t& x) const
176 {
177 return vsqrtq_f16(x);
178 }
179};
180
181struct unary_op_rsqrt_fp16s
182{
183 __fp16 func(const __fp16& x) const
184 {
185 return (__fp16)1.f / (__fp16)sqrtf(x);
186 }
187 float16x4_t func_pack4(const float16x4_t& x) const
188 {
189 float16x4_t _reciprocal = vrsqrte_f16(x);
190 _reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, _reciprocal), _reciprocal), _reciprocal);
191 // _reciprocal = vmul_f16(vrsqrts_f16(vmul_f16(x, _reciprocal), _reciprocal), _reciprocal);
192 return _reciprocal;
193 }
194 float16x8_t func_pack8(const float16x8_t& x) const
195 {
196 float16x8_t _reciprocal = vrsqrteq_f16(x);
197 _reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, _reciprocal), _reciprocal), _reciprocal);
198 // _reciprocal = vmulq_f16(vrsqrtsq_f16(vmulq_f16(x, _reciprocal), _reciprocal), _reciprocal);
199 return _reciprocal;
200 }
201};
202
203struct unary_op_exp_fp16s
204{
205 __fp16 func(const __fp16& x) const
206 {
207 return (__fp16)expf(x);
208 }
209 float16x4_t func_pack4(const float16x4_t& x) const
210 {
211 return exp_ps_f16(x);
212 }
213 float16x8_t func_pack8(const float16x8_t& x) const
214 {
215 return exp_ps_f16(x);
216 }
217};
218
219struct unary_op_log_fp16s
220{
221 __fp16 func(const __fp16& x) const
222 {
223 return (__fp16)logf(x);
224 }
225 float16x4_t func_pack4(const float16x4_t& x) const
226 {
227 return log_ps_f16(x);
228 }
229 float16x8_t func_pack8(const float16x8_t& x) const
230 {
231 return log_ps_f16(x);
232 }
233};
234
235struct unary_op_sin_fp16s
236{
237 __fp16 func(const __fp16& x) const
238 {
239 return (__fp16)sinf(x);
240 }
241 float16x4_t func_pack4(const float16x4_t& x) const
242 {
243 return sin_ps_f16(x);
244 }
245 float16x8_t func_pack8(const float16x8_t& x) const
246 {
247 return sin_ps_f16(x);
248 }
249};
250
251struct unary_op_cos_fp16s
252{
253 __fp16 func(const __fp16& x) const
254 {
255 return (__fp16)cosf(x);
256 }
257 float16x4_t func_pack4(const float16x4_t& x) const
258 {
259 return cos_ps_f16(x);
260 }
261 float16x8_t func_pack8(const float16x8_t& x) const
262 {
263 return cos_ps_f16(x);
264 }
265};
266
267struct unary_op_tan_fp16s
268{
269 __fp16 func(const __fp16& x) const
270 {
271 return (__fp16)tanf(x);
272 }
273 float16x4_t func_pack4(const float16x4_t& x) const
274 {
275 // TODO neon optimize
276 __fp16 tmp[4];
277 vst1_f16(tmp, x);
278 tmp[0] = tanf(tmp[0]);
279 tmp[1] = tanf(tmp[1]);
280 tmp[2] = tanf(tmp[2]);
281 tmp[3] = tanf(tmp[3]);
282 return vld1_f16(tmp);
283 }
284 float16x8_t func_pack8(const float16x8_t& x) const
285 {
286 // TODO neon optimize
287 __fp16 tmp[8];
288 vst1q_f16(tmp, x);
289 tmp[0] = tanf(tmp[0]);
290 tmp[1] = tanf(tmp[1]);
291 tmp[2] = tanf(tmp[2]);
292 tmp[3] = tanf(tmp[3]);
293 tmp[4] = tanf(tmp[4]);
294 tmp[5] = tanf(tmp[5]);
295 tmp[6] = tanf(tmp[6]);
296 tmp[7] = tanf(tmp[7]);
297 return vld1q_f16(tmp);
298 }
299};
300
301struct unary_op_asin_fp16s
302{
303 __fp16 func(const __fp16& x) const
304 {
305 return (__fp16)asinf(x);
306 }
307 float16x4_t func_pack4(const float16x4_t& x) const
308 {
309 // TODO neon optimize
310 __fp16 tmp[4];
311 vst1_f16(tmp, x);
312 tmp[0] = asinf(tmp[0]);
313 tmp[1] = asinf(tmp[1]);
314 tmp[2] = asinf(tmp[2]);
315 tmp[3] = asinf(tmp[3]);
316 return vld1_f16(tmp);
317 }
318 float16x8_t func_pack8(const float16x8_t& x) const
319 {
320 // TODO neon optimize
321 __fp16 tmp[8];
322 vst1q_f16(tmp, x);
323 tmp[0] = asinf(tmp[0]);
324 tmp[1] = asinf(tmp[1]);
325 tmp[2] = asinf(tmp[2]);
326 tmp[3] = asinf(tmp[3]);
327 tmp[4] = asinf(tmp[4]);
328 tmp[5] = asinf(tmp[5]);
329 tmp[6] = asinf(tmp[6]);
330 tmp[7] = asinf(tmp[7]);
331 return vld1q_f16(tmp);
332 }
333};
334
335struct unary_op_acos_fp16s
336{
337 __fp16 func(const __fp16& x) const
338 {
339 return (__fp16)acosf(x);
340 }
341 float16x4_t func_pack4(const float16x4_t& x) const
342 {
343 // TODO neon optimize
344 __fp16 tmp[4];
345 vst1_f16(tmp, x);
346 tmp[0] = acosf(tmp[0]);
347 tmp[1] = acosf(tmp[1]);
348 tmp[2] = acosf(tmp[2]);
349 tmp[3] = acosf(tmp[3]);
350 return vld1_f16(tmp);
351 }
352 float16x8_t func_pack8(const float16x8_t& x) const
353 {
354 // TODO neon optimize
355 __fp16 tmp[8];
356 vst1q_f16(tmp, x);
357 tmp[0] = acosf(tmp[0]);
358 tmp[1] = acosf(tmp[1]);
359 tmp[2] = acosf(tmp[2]);
360 tmp[3] = acosf(tmp[3]);
361 tmp[4] = acosf(tmp[4]);
362 tmp[5] = acosf(tmp[5]);
363 tmp[6] = acosf(tmp[6]);
364 tmp[7] = acosf(tmp[7]);
365 return vld1q_f16(tmp);
366 }
367};
368
369struct unary_op_atan_fp16s
370{
371 __fp16 func(const __fp16& x) const
372 {
373 return (__fp16)atanf(x);
374 }
375 float16x4_t func_pack4(const float16x4_t& x) const
376 {
377 // TODO neon optimize
378 __fp16 tmp[4];
379 vst1_f16(tmp, x);
380 tmp[0] = atanf(tmp[0]);
381 tmp[1] = atanf(tmp[1]);
382 tmp[2] = atanf(tmp[2]);
383 tmp[3] = atanf(tmp[3]);
384 return vld1_f16(tmp);
385 }
386 float16x8_t func_pack8(const float16x8_t& x) const
387 {
388 // TODO neon optimize
389 __fp16 tmp[8];
390 vst1q_f16(tmp, x);
391 tmp[0] = atanf(tmp[0]);
392 tmp[1] = atanf(tmp[1]);
393 tmp[2] = atanf(tmp[2]);
394 tmp[3] = atanf(tmp[3]);
395 tmp[4] = atanf(tmp[4]);
396 tmp[5] = atanf(tmp[5]);
397 tmp[6] = atanf(tmp[6]);
398 tmp[7] = atanf(tmp[7]);
399 return vld1q_f16(tmp);
400 }
401};
402
403struct unary_op_reciprocal_fp16s
404{
405 __fp16 func(const __fp16& x) const
406 {
407 return (__fp16)1.f / x;
408 }
409 float16x4_t func_pack4(const float16x4_t& x) const
410 {
411 float16x4_t _reciprocal = vrecpe_f16(x);
412 _reciprocal = vmul_f16(vrecps_f16(x, _reciprocal), _reciprocal);
413 // _reciprocal = vmul_f16(vrecps_f16(x, _reciprocal), _reciprocal);
414 return _reciprocal;
415 }
416 float16x8_t func_pack8(const float16x8_t& x) const
417 {
418 float16x8_t _reciprocal = vrecpeq_f16(x);
419 _reciprocal = vmulq_f16(vrecpsq_f16(x, _reciprocal), _reciprocal);
420 // _reciprocal = vmulq_f16(vrecpsq_f16(x, _reciprocal), _reciprocal);
421 return _reciprocal;
422 }
423};
424
425struct unary_op_tanh_fp16s
426{
427 __fp16 func(const __fp16& x) const
428 {
429 return (__fp16)tanhf(x);
430 }
431 float16x4_t func_pack4(const float16x4_t& x) const
432 {
433 return tanh_ps_f16(x);
434 }
435 float16x8_t func_pack8(const float16x8_t& x) const
436 {
437 return tanh_ps_f16(x);
438 }
439};
440
441struct unary_op_log10_fp16s
442{
443 __fp16 func(const __fp16& x) const
444 {
445 return (__fp16)log10f(x);
446 }
447 float16x4_t func_pack4(const float16x4_t& x) const
448 {
449 return vmul_f16(log_ps_f16(x), vdup_n_f16(0.434294481903));
450 }
451 float16x8_t func_pack8(const float16x8_t& x) const
452 {
453 return vmulq_f16(log_ps_f16(x), vdupq_n_f16(0.434294481903));
454 }
455};
456
457struct unary_op_round_fp16s
458{
459 __fp16 func(const __fp16& x) const
460 {
461 // round to nearest even
462#if NCNN_GNU_INLINE_ASM
463 // return (x + 1536.f) - 1536.f;
464 __fp16 y;
465 const __fp16 magic = 1536.f;
466 asm volatile(
467 "fadd %h0, %h1, %h2 \n"
468 "fsub %h0, %h0, %h2 \n"
469 : "=w"(y)
470 : "w"(x), "w"(magic)
471 :);
472 return y;
473#else
474#ifdef FE_TONEAREST
475 int old_rm = fegetround();
476 fesetround(FE_TONEAREST);
477#endif
478 __fp16 y = (__fp16)nearbyintf(x);
479#ifdef FE_TONEAREST
480 fesetround(old_rm);
481#endif
482 return y;
483#endif
484 }
485 float16x4_t func_pack4(const float16x4_t& x) const
486 {
487 return vrndn_f16(x);
488 }
489 float16x8_t func_pack8(const float16x8_t& x) const
490 {
491 return vrndnq_f16(x);
492 }
493};
494
495struct unary_op_trunc_fp16s
496{
497 __fp16 func(const __fp16& x) const
498 {
499 return (__fp16)truncf(x);
500 }
501 float16x4_t func_pack4(const float16x4_t& x) const
502 {
503 return vrnd_f16(x);
504 }
505 float16x8_t func_pack8(const float16x8_t& x) const
506 {
507 return vrndq_f16(x);
508 }
509};
510
511} // namespace UnaryOp_arm_functor
512
513int UnaryOp_arm::forward_inplace_fp16s(Mat& bottom_top_blob, const Option& opt) const
514{
515 using namespace UnaryOp_arm_functor;
516
517 if (op_type == Operation_ABS)
518 return unary_op_inplace_fp16s<unary_op_abs_fp16s>(bottom_top_blob, opt);
519
520 if (op_type == Operation_NEG)
521 return unary_op_inplace_fp16s<unary_op_neg_fp16s>(bottom_top_blob, opt);
522
523 if (op_type == Operation_FLOOR)
524 return unary_op_inplace_fp16s<unary_op_floor_fp16s>(bottom_top_blob, opt);
525
526 if (op_type == Operation_CEIL)
527 return unary_op_inplace_fp16s<unary_op_ceil_fp16s>(bottom_top_blob, opt);
528
529 if (op_type == Operation_SQUARE)
530 return unary_op_inplace_fp16s<unary_op_square_fp16s>(bottom_top_blob, opt);
531
532 if (op_type == Operation_SQRT)
533 return unary_op_inplace_fp16s<unary_op_sqrt_fp16s>(bottom_top_blob, opt);
534
535 if (op_type == Operation_RSQRT)
536 return unary_op_inplace_fp16s<unary_op_rsqrt_fp16s>(bottom_top_blob, opt);
537
538 if (op_type == Operation_EXP)
539 return unary_op_inplace_fp16s<unary_op_exp_fp16s>(bottom_top_blob, opt);
540
541 if (op_type == Operation_LOG)
542 return unary_op_inplace_fp16s<unary_op_log_fp16s>(bottom_top_blob, opt);
543
544 if (op_type == Operation_SIN)
545 return unary_op_inplace_fp16s<unary_op_sin_fp16s>(bottom_top_blob, opt);
546
547 if (op_type == Operation_COS)
548 return unary_op_inplace_fp16s<unary_op_cos_fp16s>(bottom_top_blob, opt);
549
550 if (op_type == Operation_TAN)
551 return unary_op_inplace_fp16s<unary_op_tan_fp16s>(bottom_top_blob, opt);
552
553 if (op_type == Operation_ASIN)
554 return unary_op_inplace_fp16s<unary_op_asin_fp16s>(bottom_top_blob, opt);
555
556 if (op_type == Operation_ACOS)
557 return unary_op_inplace_fp16s<unary_op_acos_fp16s>(bottom_top_blob, opt);
558
559 if (op_type == Operation_ATAN)
560 return unary_op_inplace_fp16s<unary_op_atan_fp16s>(bottom_top_blob, opt);
561
562 if (op_type == Operation_RECIPROCAL)
563 return unary_op_inplace_fp16s<unary_op_reciprocal_fp16s>(bottom_top_blob, opt);
564
565 if (op_type == Operation_TANH)
566 return unary_op_inplace_fp16s<unary_op_tanh_fp16s>(bottom_top_blob, opt);
567
568 if (op_type == Operation_LOG10)
569 return unary_op_inplace_fp16s<unary_op_log10_fp16s>(bottom_top_blob, opt);
570
571 if (op_type == Operation_ROUND)
572 return unary_op_inplace_fp16s<unary_op_round_fp16s>(bottom_top_blob, opt);
573
574 if (op_type == Operation_TRUNC)
575 return unary_op_inplace_fp16s<unary_op_trunc_fp16s>(bottom_top_blob, opt);
576
577 return 0;
578}
579#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
580
581} // namespace ncnn