Elffuss BraidHub

kikocis/ncnn public

master · 4.8 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 "mish_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
27Mish_arm::Mish_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 Mish_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 size = w * h * d;
65 int elempack = bottom_top_blob.elempack;
66
67#if __ARM_NEON
68 if (elempack == 4)
69 {
70 #pragma omp parallel for num_threads(opt.num_threads)
71 for (int q = 0; q < channels; q++)
72 {
73 float* ptr = bottom_top_blob.channel(q);
74
75 for (int i = 0; i < size; i++)
76 {
77 float32x4_t _p = vld1q_f32(ptr);
78 _p = vmulq_f32(_p, tanh_ps(log_ps(vaddq_f32(exp_ps(_p), vdupq_n_f32(1.f)))));
79 vst1q_f32(ptr, _p);
80 ptr += 4;
81 }
82 }
83
84 return 0;
85 }
86#endif // __ARM_NEON
87
88 #pragma omp parallel for num_threads(opt.num_threads)
89 for (int q = 0; q < channels; q++)
90 {
91 float* ptr = bottom_top_blob.channel(q);
92
93#if __ARM_NEON
94 int nn = size >> 2;
95 int remain = size - (nn << 2);
96#else
97 int remain = size;
98#endif // __ARM_NEON
99
100#if __ARM_NEON
101 for (; nn > 0; nn--)
102 {
103 float32x4_t _p = vld1q_f32(ptr);
104 _p = vmulq_f32(_p, tanh_ps(log_ps(vaddq_f32(exp_ps(_p), vdupq_n_f32(1.f)))));
105 vst1q_f32(ptr, _p);
106 ptr += 4;
107 }
108#endif // __ARM_NEON
109 for (; remain > 0; remain--)
110 {
111 *ptr = *ptr * tanhf(logf(expf(*ptr) + 1.f));
112 ptr++;
113 }
114 }
115
116 return 0;
117}
118
119#if NCNN_BF16
120int Mish_arm::forward_inplace_bf16s(Mat& bottom_top_blob, const Option& opt) const
121{
122 int w = bottom_top_blob.w;
123 int h = bottom_top_blob.h;
124 int d = bottom_top_blob.d;
125 int channels = bottom_top_blob.c;
126 int size = w * h * d;
127 int elempack = bottom_top_blob.elempack;
128
129#if __ARM_NEON
130 if (elempack == 4)
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 for (int i = 0; i < size; i++)
138 {
139 float32x4_t _p = bfloat2float(vld1_u16(ptr));
140 _p = vmulq_f32(_p, tanh_ps(log_ps(vaddq_f32(exp_ps(_p), vdupq_n_f32(1.f)))));
141 vst1_u16(ptr, float2bfloat(_p));
142 ptr += 4;
143 }
144 }
145
146 return 0;
147 }
148#endif // __ARM_NEON
149
150 #pragma omp parallel for num_threads(opt.num_threads)
151 for (int q = 0; q < channels; q++)
152 {
153 unsigned short* ptr = bottom_top_blob.channel(q);
154
155#if __ARM_NEON
156 int nn = size >> 2;
157 int remain = size - (nn << 2);
158#else
159 int remain = size;
160#endif // __ARM_NEON
161
162#if __ARM_NEON
163 for (; nn > 0; nn--)
164 {
165 float32x4_t _p = bfloat2float(vld1_u16(ptr));
166 _p = vmulq_f32(_p, tanh_ps(log_ps(vaddq_f32(exp_ps(_p), vdupq_n_f32(1.f)))));
167 vst1_u16(ptr, float2bfloat(_p));
168 ptr += 4;
169 }
170#endif // __ARM_NEON
171 for (; remain > 0; remain--)
172 {
173 float v = bfloat16_to_float32(*ptr);
174 v = v * tanhf(logf(expf(v) + 1.f));
175 *ptr = float32_to_bfloat16(v);
176 ptr++;
177 }
178 }
179
180 return 0;
181}
182#endif // NCNN_BF16
183
184} // namespace ncnn