Elffuss BraidHub

kikocis/ncnn public

master · 5.5 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2017 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 "neon_mathfun.h"
20#endif // __ARM_NEON
21
22#include "arm_usability.h"
23
24#include "cpu.h"
25
26namespace ncnn {
27
28Sigmoid_arm::Sigmoid_arm()
29{
30#if __ARM_NEON
31 support_packing = true;
32#if NCNN_ARM82
33 support_fp16_storage = cpu_support_arm_asimdhp();
34#endif
35#endif // __ARM_NEON
36
37#if NCNN_BF16
38 support_bf16_storage = true;
39#endif
40}
41
42int Sigmoid_arm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
43{
44 int elembits = bottom_top_blob.elembits();
45
46#if NCNN_ARM82
47 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
48 {
49 if (opt.use_fp16_arithmetic)
50 return forward_inplace_fp16sa(bottom_top_blob, opt);
51 else
52 return forward_inplace_fp16s(bottom_top_blob, opt);
53 }
54#endif
55
56#if NCNN_BF16
57 if (opt.use_bf16_storage && elembits == 16)
58 return forward_inplace_bf16s(bottom_top_blob, opt);
59#endif
60
61 int w = bottom_top_blob.w;
62 int h = bottom_top_blob.h;
63 int d = bottom_top_blob.d;
64 int channels = bottom_top_blob.c;
65 int elempack = bottom_top_blob.elempack;
66 int size = w * h * d * elempack;
67
68 #pragma omp parallel for num_threads(opt.num_threads)
69 for (int q = 0; q < channels; q++)
70 {
71 float* ptr = bottom_top_blob.channel(q);
72
73 int i = 0;
74#if __ARM_NEON
75#if __aarch64__
76 for (; i + 15 < size; i += 16)
77 {
78 float32x4_t _p0 = vld1q_f32(ptr);
79 float32x4_t _p1 = vld1q_f32(ptr + 4);
80 float32x4_t _p2 = vld1q_f32(ptr + 8);
81 float32x4_t _p3 = vld1q_f32(ptr + 12);
82 _p0 = sigmoid_ps(_p0);
83 _p1 = sigmoid_ps(_p1);
84 _p2 = sigmoid_ps(_p2);
85 _p3 = sigmoid_ps(_p3);
86 vst1q_f32(ptr, _p0);
87 vst1q_f32(ptr + 4, _p1);
88 vst1q_f32(ptr + 8, _p2);
89 vst1q_f32(ptr + 12, _p3);
90 ptr += 16;
91 }
92#endif // __aarch64__
93 for (; i + 7 < size; i += 8)
94 {
95 float32x4_t _p0 = vld1q_f32(ptr);
96 float32x4_t _p1 = vld1q_f32(ptr + 4);
97 _p0 = sigmoid_ps(_p0);
98 _p1 = sigmoid_ps(_p1);
99 vst1q_f32(ptr, _p0);
100 vst1q_f32(ptr + 4, _p1);
101 ptr += 8;
102 }
103 for (; i + 3 < size; i += 4)
104 {
105 float32x4_t _p = vld1q_f32(ptr);
106 _p = sigmoid_ps(_p);
107 vst1q_f32(ptr, _p);
108 ptr += 4;
109 }
110#endif // __ARM_NEON
111 for (; i < size; i++)
112 {
113 *ptr = 1.f / (1.f + expf(-*ptr));
114
115 ptr++;
116 }
117 }
118
119 return 0;
120}
121
122#if NCNN_BF16
123int Sigmoid_arm::forward_inplace_bf16s(Mat& bottom_top_blob, const Option& opt) const
124{
125 int w = bottom_top_blob.w;
126 int h = bottom_top_blob.h;
127 int d = bottom_top_blob.d;
128 int channels = bottom_top_blob.c;
129 int elempack = bottom_top_blob.elempack;
130 int size = w * h * d * elempack;
131
132 #pragma omp parallel for num_threads(opt.num_threads)
133 for (int q = 0; q < channels; q++)
134 {
135 unsigned short* ptr = bottom_top_blob.channel(q);
136
137 int i = 0;
138#if __ARM_NEON
139#if __aarch64__
140 for (; i + 15 < size; i += 16)
141 {
142 uint16x8_t _p01 = vld1q_u16(ptr);
143 uint16x8_t _p23 = vld1q_u16(ptr + 8);
144 float32x4_t _p0 = bfloat2float(vget_low_u16(_p01));
145 float32x4_t _p1 = bfloat2float(vget_high_u16(_p01));
146 float32x4_t _p2 = bfloat2float(vget_low_u16(_p23));
147 float32x4_t _p3 = bfloat2float(vget_high_u16(_p23));
148 _p0 = sigmoid_ps(_p0);
149 _p1 = sigmoid_ps(_p1);
150 _p2 = sigmoid_ps(_p2);
151 _p3 = sigmoid_ps(_p3);
152 _p01 = vcombine_u16(float2bfloat(_p0), float2bfloat(_p1));
153 _p23 = vcombine_u16(float2bfloat(_p2), float2bfloat(_p3));
154 vst1q_u16(ptr, _p01);
155 vst1q_u16(ptr + 8, _p23);
156 ptr += 16;
157 }
158#endif // __aarch64__
159 for (; i + 7 < size; i += 8)
160 {
161 uint16x8_t _p = vld1q_u16(ptr);
162 float32x4_t _p0 = bfloat2float(vget_low_u16(_p));
163 float32x4_t _p1 = bfloat2float(vget_high_u16(_p));
164 _p0 = sigmoid_ps(_p0);
165 _p1 = sigmoid_ps(_p1);
166 _p = vcombine_u16(float2bfloat(_p0), float2bfloat(_p1));
167 vst1q_u16(ptr, _p);
168 ptr += 8;
169 }
170 for (; i + 3 < size; i += 4)
171 {
172 float32x4_t _p = bfloat2float(vld1_u16(ptr));
173 _p = sigmoid_ps(_p);
174 vst1_u16(ptr, float2bfloat(_p));
175 ptr += 4;
176 }
177#endif // __ARM_NEON
178 for (; i < size; i++)
179 {
180 float v = bfloat16_to_float32(*ptr);
181 v = 1.f / (1.f + expf(-v));
182 *ptr = float32_to_bfloat16(v);
183
184 ptr++;
185 }
186 }
187
188 return 0;
189}
190#endif // NCNN_BF16
191
192} // namespace ncnn