Elffuss BraidHub

kikocis/ncnn public

master · 5.0 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 "sigmoid_arm.h"
16
17#if __ARM_NEON
18#include <arm_neon.h>
19#include "arm_usability.h"
20#include "neon_mathfun.h"
21#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
22#include "neon_mathfun_fp16s.h"
23#endif
24#endif // __ARM_NEON
25
26namespace ncnn {
27
28#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
29int Sigmoid_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 channels = bottom_top_blob.c;
35 int elempack = bottom_top_blob.elempack;
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 = bottom_top_blob.channel(q);
42
43 int i = 0;
44 for (; i + 15 < size; i += 16)
45 {
46 float16x8_t _p01 = vld1q_f16(ptr);
47 float16x8_t _p23 = vld1q_f16(ptr + 8);
48 float32x4_t _p0 = vcvt_f32_f16(vget_low_f16(_p01));
49 float32x4_t _p1 = vcvt_f32_f16(vget_high_f16(_p01));
50 float32x4_t _p2 = vcvt_f32_f16(vget_low_f16(_p23));
51 float32x4_t _p3 = vcvt_f32_f16(vget_high_f16(_p23));
52 _p0 = sigmoid_ps(_p0);
53 _p1 = sigmoid_ps(_p1);
54 _p2 = sigmoid_ps(_p2);
55 _p3 = sigmoid_ps(_p3);
56 _p01 = vcombine_f16(vcvt_f16_f32(_p0), vcvt_f16_f32(_p1));
57 _p23 = vcombine_f16(vcvt_f16_f32(_p2), vcvt_f16_f32(_p3));
58 vst1q_f16(ptr, _p01);
59 vst1q_f16(ptr + 8, _p23);
60 ptr += 16;
61 }
62 for (; i + 7 < size; i += 8)
63 {
64 float16x8_t _p = vld1q_f16(ptr);
65 float32x4_t _p0 = vcvt_f32_f16(vget_low_f16(_p));
66 float32x4_t _p1 = vcvt_f32_f16(vget_high_f16(_p));
67 _p0 = sigmoid_ps(_p0);
68 _p1 = sigmoid_ps(_p1);
69 _p = vcombine_f16(vcvt_f16_f32(_p0), vcvt_f16_f32(_p1));
70 vst1q_f16(ptr, _p);
71 ptr += 8;
72 }
73 for (; i + 3 < size; i += 4)
74 {
75 float32x4_t _p = vcvt_f32_f16(vld1_f16(ptr));
76 _p = sigmoid_ps(_p);
77 vst1_f16(ptr, vcvt_f16_f32(_p));
78 ptr += 4;
79 }
80 for (; i < size; i++)
81 {
82 float v = (float)*ptr;
83 v = 1.f / (1.f + expf(-v));
84 *ptr = (__fp16)v;
85
86 ptr++;
87 }
88 }
89
90 return 0;
91}
92
93int Sigmoid_arm::forward_inplace_fp16sa(Mat& bottom_top_blob, const Option& opt) const
94{
95 int w = bottom_top_blob.w;
96 int h = bottom_top_blob.h;
97 int d = bottom_top_blob.d;
98 int channels = bottom_top_blob.c;
99 int elempack = bottom_top_blob.elempack;
100 int size = w * h * d * elempack;
101
102 #pragma omp parallel for num_threads(opt.num_threads)
103 for (int q = 0; q < channels; q++)
104 {
105 __fp16* ptr = bottom_top_blob.channel(q);
106
107 int i = 0;
108 for (; i + 31 < size; i += 32)
109 {
110 float16x8_t _p0 = vld1q_f16(ptr);
111 float16x8_t _p1 = vld1q_f16(ptr + 8);
112 float16x8_t _p2 = vld1q_f16(ptr + 16);
113 float16x8_t _p3 = vld1q_f16(ptr + 24);
114 _p0 = sigmoid_ps_f16(_p0);
115 _p1 = sigmoid_ps_f16(_p1);
116 _p2 = sigmoid_ps_f16(_p2);
117 _p3 = sigmoid_ps_f16(_p3);
118 vst1q_f16(ptr, _p0);
119 vst1q_f16(ptr + 8, _p1);
120 vst1q_f16(ptr + 16, _p2);
121 vst1q_f16(ptr + 24, _p3);
122 ptr += 32;
123 }
124 for (; i + 15 < size; i += 16)
125 {
126 float16x8_t _p0 = vld1q_f16(ptr);
127 float16x8_t _p1 = vld1q_f16(ptr + 8);
128 _p0 = sigmoid_ps_f16(_p0);
129 _p1 = sigmoid_ps_f16(_p1);
130 vst1q_f16(ptr, _p0);
131 vst1q_f16(ptr + 8, _p1);
132 ptr += 16;
133 }
134 for (; i + 7 < size; i += 8)
135 {
136 float16x8_t _p = vld1q_f16(ptr);
137 _p = sigmoid_ps_f16(_p);
138 vst1q_f16(ptr, _p);
139 ptr += 8;
140 }
141 for (; i + 3 < size; i += 4)
142 {
143 float16x4_t _p = vld1_f16(ptr);
144 _p = sigmoid_ps_f16(_p);
145 vst1_f16(ptr, _p);
146 ptr += 4;
147 }
148 for (; i < size; i++)
149 {
150 __fp16 v = *ptr;
151 v = 1.f / (1.f + expf(-v));
152 *ptr = v;
153
154 ptr++;
155 }
156 }
157
158 return 0;
159}
160#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
161
162} // namespace ncnn