Elffuss BraidHub

kikocis/ncnn public

master · 6.0 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2020 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 "swish_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#include "cpu.h"
24
25namespace ncnn {
26
27Swish_arm::Swish_arm()
28{
29#if __ARM_NEON
30 support_packing = true;
31#if NCNN_ARM82
32 support_fp16_storage = cpu_support_arm_asimdhp();
33#endif
34#endif // __ARM_NEON
35
36#if NCNN_BF16
37 support_bf16_storage = true;
38#endif
39}
40
41int Swish_arm::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
42{
43 int elembits = bottom_top_blob.elembits();
44
45#if NCNN_ARM82
46 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
47 {
48 if (opt.use_fp16_arithmetic)
49 return forward_inplace_fp16sa(bottom_top_blob, opt);
50 else
51 return forward_inplace_fp16s(bottom_top_blob, opt);
52 }
53#endif
54
55#if NCNN_BF16
56 if (opt.use_bf16_storage && elembits == 16)
57 return forward_inplace_bf16s(bottom_top_blob, opt);
58#endif
59
60 int w = bottom_top_blob.w;
61 int h = bottom_top_blob.h;
62 int d = bottom_top_blob.d;
63 int channels = bottom_top_blob.c;
64 int elempack = bottom_top_blob.elempack;
65 int size = w * h * d * elempack;
66
67 #pragma omp parallel for num_threads(opt.num_threads)
68 for (int q = 0; q < channels; q++)
69 {
70 float* ptr = bottom_top_blob.channel(q);
71
72 int i = 0;
73#if __ARM_NEON
74 float32x4_t _one = vdupq_n_f32(1.f);
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 = div_ps(_p0, vaddq_f32(_one, exp_ps(vnegq_f32(_p0))));
83 _p1 = div_ps(_p1, vaddq_f32(_one, exp_ps(vnegq_f32(_p1))));
84 _p2 = div_ps(_p2, vaddq_f32(_one, exp_ps(vnegq_f32(_p2))));
85 _p3 = div_ps(_p3, vaddq_f32(_one, exp_ps(vnegq_f32(_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 = div_ps(_p0, vaddq_f32(_one, exp_ps(vnegq_f32(_p0))));
98 _p1 = div_ps(_p1, vaddq_f32(_one, exp_ps(vnegq_f32(_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 = div_ps(_p, vaddq_f32(_one, exp_ps(vnegq_f32(_p))));
107 vst1q_f32(ptr, _p);
108 ptr += 4;
109 }
110#endif // __ARM_NEON
111 for (; i < size; i++)
112 {
113 *ptr = *ptr / (1.f + expf(-*ptr));
114
115 ptr++;
116 }
117 }
118
119 return 0;
120}
121
122#if NCNN_BF16
123int Swish_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 float32x4_t _one = vdupq_n_f32(1.f);
140#if __aarch64__
141 for (; i + 15 < size; i += 16)
142 {
143 uint16x8_t _p01 = vld1q_u16(ptr);
144 uint16x8_t _p23 = vld1q_u16(ptr + 8);
145 float32x4_t _p0 = bfloat2float(vget_low_u16(_p01));
146 float32x4_t _p1 = bfloat2float(vget_high_u16(_p01));
147 float32x4_t _p2 = bfloat2float(vget_low_u16(_p23));
148 float32x4_t _p3 = bfloat2float(vget_high_u16(_p23));
149 _p0 = div_ps(_p0, vaddq_f32(_one, exp_ps(vnegq_f32(_p0))));
150 _p1 = div_ps(_p1, vaddq_f32(_one, exp_ps(vnegq_f32(_p1))));
151 _p2 = div_ps(_p2, vaddq_f32(_one, exp_ps(vnegq_f32(_p2))));
152 _p3 = div_ps(_p3, vaddq_f32(_one, exp_ps(vnegq_f32(_p3))));
153 _p01 = vcombine_u16(float2bfloat(_p0), float2bfloat(_p1));
154 _p23 = vcombine_u16(float2bfloat(_p2), float2bfloat(_p3));
155 vst1q_u16(ptr, _p01);
156 vst1q_u16(ptr + 8, _p23);
157 ptr += 16;
158 }
159#endif // __aarch64__
160 for (; i + 7 < size; i += 8)
161 {
162 uint16x8_t _p = vld1q_u16(ptr);
163 float32x4_t _p0 = bfloat2float(vget_low_u16(_p));
164 float32x4_t _p1 = bfloat2float(vget_high_u16(_p));
165 _p0 = div_ps(_p0, vaddq_f32(_one, exp_ps(vnegq_f32(_p0))));
166 _p1 = div_ps(_p1, vaddq_f32(_one, exp_ps(vnegq_f32(_p1))));
167 _p = vcombine_u16(float2bfloat(_p0), float2bfloat(_p1));
168 vst1q_u16(ptr, _p);
169 ptr += 8;
170 }
171 for (; i + 3 < size; i += 4)
172 {
173 float32x4_t _p = bfloat2float(vld1_u16(ptr));
174 _p = div_ps(_p, vaddq_f32(_one, exp_ps(vnegq_f32(_p))));
175 vst1_u16(ptr, float2bfloat(_p));
176 ptr += 4;
177 }
178#endif // __ARM_NEON
179 for (; i < size; i++)
180 {
181 float v = bfloat16_to_float32(*ptr);
182 v = v / (1.f + expf(-v));
183 *ptr = float32_to_bfloat16(v);
184
185 ptr++;
186 }
187 }
188
189 return 0;
190}
191#endif // NCNN_BF16
192
193} // namespace ncnn