Elffuss BraidHub

kikocis/ncnn public

master · 27.4 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#if NCNN_RUNTIME_CPU && NCNN_ARM82FP16FML && __aarch64__ && !__ARM_FEATURE_FP16_FML
16void innerproduct_pack4_fp16s_neon_asimdfhm(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data_fp16, const Mat& bias_data, int activation_type, const Mat& activation_params, const Option& opt);
17void innerproduct_fp16s_neon_asimdfhm(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data_fp16, const Mat& bias_data, int activation_type, const Mat& activation_params, const Option& opt);
18void innerproduct_transform_kernel_fp16s_neon_asimdfhm(const Mat& weight_data, Mat& weight_data_tm, int num_input, int num_output, const Option& opt);
19#endif
20
21#if NCNN_RUNTIME_CPU && NCNN_ARM82 && __aarch64__ && !__ARM_FEATURE_FP16_VECTOR_ARITHMETIC && !__ARM_FEATURE_FP16_FML
22void innerproduct_pack4_fp16s_neon_asimdhp(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data_fp16, const Mat& bias_data, int activation_type, const Mat& activation_params, const Option& opt);
23void innerproduct_fp16s_neon_asimdhp(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data_fp16, const Mat& bias_data, int activation_type, const Mat& activation_params, const Option& opt);
24void innerproduct_transform_kernel_fp16s_neon_asimdhp(const Mat& weight_data, Mat& weight_data_tm, int num_input, int num_output, const Option& opt);
25#endif
26
27static void innerproduct_pack4_fp16s_neon(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data_fp16, const Mat& bias_data, int activation_type, const Mat& activation_params, const Option& opt)
28{
29#if NCNN_RUNTIME_CPU && NCNN_ARM82FP16FML && __aarch64__ && !__ARM_FEATURE_FP16_FML
30 if (ncnn::cpu_support_arm_asimdfhm())
31 {
32 innerproduct_pack4_fp16s_neon_asimdfhm(bottom_blob, top_blob, weight_data_fp16, bias_data, activation_type, activation_params, opt);
33 return;
34 }
35#endif
36
37#if NCNN_RUNTIME_CPU && NCNN_ARM82 && __aarch64__ && !__ARM_FEATURE_FP16_VECTOR_ARITHMETIC && !__ARM_FEATURE_FP16_FML
38 if (ncnn::cpu_support_arm_asimdhp())
39 {
40 innerproduct_pack4_fp16s_neon_asimdhp(bottom_blob, top_blob, weight_data_fp16, bias_data, activation_type, activation_params, opt);
41 return;
42 }
43#endif
44
45 const int num_input = bottom_blob.w * bottom_blob.elempack;
46 const int num_output = top_blob.w;
47
48 const float* bias_data_ptr = bias_data;
49
50 #pragma omp parallel for num_threads(opt.num_threads)
51 for (int p = 0; p < num_output; p++)
52 {
53 float32x4_t _sum0 = vdupq_n_f32(0.f);
54
55 if (bias_data_ptr)
56 {
57 _sum0 = vld1q_f32(bias_data_ptr + p * 4);
58 }
59
60 float32x4_t _sum1 = vdupq_n_f32(0.f);
61 float32x4_t _sum2 = vdupq_n_f32(0.f);
62 float32x4_t _sum3 = vdupq_n_f32(0.f);
63
64#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
65 const __fp16* sptr = bottom_blob;
66 const __fp16* kptr = weight_data_fp16.row<const __fp16>(p);
67#else
68 const float* sptr = bottom_blob;
69 const unsigned short* kptr = weight_data_fp16.row<const unsigned short>(p);
70#endif
71
72 int i = 0;
73#if NCNN_GNU_INLINE_ASM
74 for (; i + 7 < num_input; i += 8)
75 {
76#if __aarch64__
77#if __ARM_FEATURE_FP16_FML
78 asm volatile(
79 "prfm pldl1keep, [%0, #128] \n"
80 "ld1 {v0.8h}, [%0], #16 \n"
81 "prfm pldl1keep, [%1, #512] \n"
82 "ld1 {v2.8h, v3.8h, v4.8h, v5.8h}, [%1], #64 \n"
83 "fmlal %2.4s, v2.4h, v0.h[0] \n"
84 "fmlal2 %3.4s, v2.4h, v0.h[1] \n"
85 "fmlal %4.4s, v3.4h, v0.h[2] \n"
86 "fmlal2 %5.4s, v3.4h, v0.h[3] \n"
87 "fmlal %2.4s, v4.4h, v0.h[4] \n"
88 "fmlal2 %3.4s, v4.4h, v0.h[5] \n"
89 "fmlal %4.4s, v5.4h, v0.h[6] \n"
90 "fmlal2 %5.4s, v5.4h, v0.h[7] \n"
91 : "=r"(sptr), // %0
92 "=r"(kptr), // %1
93 "=w"(_sum0), // %2
94 "=w"(_sum1), // %3
95 "=w"(_sum2), // %4
96 "=w"(_sum3) // %5
97 : "0"(sptr),
98 "1"(kptr),
99 "2"(_sum0),
100 "3"(_sum1),
101 "4"(_sum2),
102 "5"(_sum3)
103 : "cc", "memory", "v0", "v2", "v3", "v4", "v5");
104#elif __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
105 asm volatile(
106 "prfm pldl1keep, [%0, #128] \n"
107 "ld1 {v1.8h}, [%0], #16 \n"
108 "prfm pldl1keep, [%1, #512] \n"
109 "ld1 {v6.8h, v7.8h, v8.8h, v9.8h}, [%1], #64 \n"
110 "fcvtl v0.4s, v1.4h \n"
111 "fcvtl2 v1.4s, v1.8h \n"
112 "fcvtl v2.4s, v6.4h \n"
113 "fcvtl2 v3.4s, v6.8h \n"
114 "fcvtl v4.4s, v7.4h \n"
115 "fcvtl2 v5.4s, v7.8h \n"
116 "fcvtl v6.4s, v8.4h \n"
117 "fcvtl2 v7.4s, v8.8h \n"
118 "fcvtl v8.4s, v9.4h \n"
119 "fcvtl2 v9.4s, v9.8h \n"
120 "fmla %2.4s, v2.4s, v0.s[0] \n"
121 "fmla %3.4s, v3.4s, v0.s[1] \n"
122 "fmla %4.4s, v4.4s, v0.s[2] \n"
123 "fmla %5.4s, v5.4s, v0.s[3] \n"
124 "fmla %2.4s, v6.4s, v1.s[0] \n"
125 "fmla %3.4s, v7.4s, v1.s[1] \n"
126 "fmla %4.4s, v8.4s, v1.s[2] \n"
127 "fmla %5.4s, v9.4s, v1.s[3] \n"
128 : "=r"(sptr), // %0
129 "=r"(kptr), // %1
130 "=w"(_sum0), // %2
131 "=w"(_sum1), // %3
132 "=w"(_sum2), // %4
133 "=w"(_sum3) // %5
134 : "0"(sptr),
135 "1"(kptr),
136 "2"(_sum0),
137 "3"(_sum1),
138 "4"(_sum2),
139 "5"(_sum3)
140 : "cc", "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9");
141#else // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
142 asm volatile(
143 "prfm pldl1keep, [%0, #256] \n"
144 "ld1 {v0.4s, v1.4s}, [%0], #32 \n"
145 "prfm pldl1keep, [%1, #512] \n"
146 "ld1 {v6.8h, v7.8h, v8.8h, v9.8h}, [%1], #64 \n"
147 "fcvtl v2.4s, v6.4h \n"
148 "fcvtl2 v3.4s, v6.8h \n"
149 "fcvtl v4.4s, v7.4h \n"
150 "fcvtl2 v5.4s, v7.8h \n"
151 "fcvtl v6.4s, v8.4h \n"
152 "fcvtl2 v7.4s, v8.8h \n"
153 "fcvtl v8.4s, v9.4h \n"
154 "fcvtl2 v9.4s, v9.8h \n"
155 "fmla %2.4s, v2.4s, v0.s[0] \n"
156 "fmla %3.4s, v3.4s, v0.s[1] \n"
157 "fmla %4.4s, v4.4s, v0.s[2] \n"
158 "fmla %5.4s, v5.4s, v0.s[3] \n"
159 "fmla %2.4s, v6.4s, v1.s[0] \n"
160 "fmla %3.4s, v7.4s, v1.s[1] \n"
161 "fmla %4.4s, v8.4s, v1.s[2] \n"
162 "fmla %5.4s, v9.4s, v1.s[3] \n"
163 : "=r"(sptr), // %0
164 "=r"(kptr), // %1
165 "=w"(_sum0), // %2
166 "=w"(_sum1), // %3
167 "=w"(_sum2), // %4
168 "=w"(_sum3) // %5
169 : "0"(sptr),
170 "1"(kptr),
171 "2"(_sum0),
172 "3"(_sum1),
173 "4"(_sum2),
174 "5"(_sum3)
175 : "cc", "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9");
176#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
177#else // __aarch64__
178 asm volatile(
179 "pld [%0, #256] \n"
180 "vld1.f32 {d0-d3}, [%0 :128]! \n"
181 "pld [%1, #512] \n"
182 "vldm %1!, {d12-d19} \n"
183 "vcvt.f32.f16 q2, d12 \n"
184 "vcvt.f32.f16 q3, d13 \n"
185 "vcvt.f32.f16 q4, d14 \n"
186 "vcvt.f32.f16 q5, d15 \n"
187 "vcvt.f32.f16 q6, d16 \n"
188 "vcvt.f32.f16 q7, d17 \n"
189 "vcvt.f32.f16 q8, d18 \n"
190 "vcvt.f32.f16 q9, d19 \n"
191 "vmla.f32 %q2, q2, d0[0] \n"
192 "vmla.f32 %q3, q3, d0[1] \n"
193 "vmla.f32 %q4, q4, d1[0] \n"
194 "vmla.f32 %q5, q5, d1[1] \n"
195 "vmla.f32 %q2, q6, d2[0] \n"
196 "vmla.f32 %q3, q7, d2[1] \n"
197 "vmla.f32 %q4, q8, d3[0] \n"
198 "vmla.f32 %q5, q9, d3[1] \n"
199 : "=r"(sptr), // %0
200 "=r"(kptr), // %1
201 "=w"(_sum0), // %2
202 "=w"(_sum1), // %3
203 "=w"(_sum2), // %4
204 "=w"(_sum3) // %5
205 : "0"(sptr),
206 "1"(kptr),
207 "2"(_sum0),
208 "3"(_sum1),
209 "4"(_sum2),
210 "5"(_sum3)
211 : "cc", "memory", "q0", "q1", "q2", "q3", "q4", "q5", "q6", "q7", "q8", "q9");
212#endif // __aarch64__
213 }
214#endif // NCNN_GNU_INLINE_ASM
215 for (; i + 3 < num_input; i += 4)
216 {
217#if __ARM_FEATURE_FP16_FML
218 float16x4_t _val = vld1_f16(sptr);
219 float16x8_t _w01 = vld1q_f16(kptr);
220 float16x8_t _w23 = vld1q_f16(kptr + 8);
221
222 _sum0 = vfmlalq_lane_low_f16(_sum0, _w01, _val, 0);
223 _sum1 = vfmlalq_lane_high_f16(_sum1, _w01, _val, 1);
224 _sum2 = vfmlalq_lane_low_f16(_sum2, _w23, _val, 2);
225 _sum3 = vfmlalq_lane_high_f16(_sum3, _w23, _val, 3);
226#else // __ARM_FEATURE_FP16_FML
227#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
228 float32x4_t _val = vcvt_f32_f16(vld1_f16(sptr));
229 float16x8_t _w01 = vld1q_f16(kptr);
230 float16x8_t _w23 = vld1q_f16(kptr + 8);
231 float32x4_t _w0 = vcvt_f32_f16(vget_low_f16(_w01));
232 float32x4_t _w1 = vcvt_f32_f16(vget_high_f16(_w01));
233 float32x4_t _w2 = vcvt_f32_f16(vget_low_f16(_w23));
234 float32x4_t _w3 = vcvt_f32_f16(vget_high_f16(_w23));
235#else
236 float32x4_t _val = vld1q_f32(sptr);
237 uint16x8_t _w01 = vld1q_u16(kptr);
238 uint16x8_t _w23 = vld1q_u16(kptr + 8);
239 float32x4_t _w0 = vcvt_f32_f16((float16x4_t)(vget_low_u16(_w01)));
240 float32x4_t _w1 = vcvt_f32_f16((float16x4_t)(vget_high_u16(_w01)));
241 float32x4_t _w2 = vcvt_f32_f16((float16x4_t)(vget_low_u16(_w23)));
242 float32x4_t _w3 = vcvt_f32_f16((float16x4_t)(vget_high_u16(_w23)));
243#endif
244
245#if __aarch64__
246 _sum0 = vfmaq_laneq_f32(_sum0, _w0, _val, 0);
247 _sum1 = vfmaq_laneq_f32(_sum1, _w1, _val, 1);
248 _sum2 = vfmaq_laneq_f32(_sum2, _w2, _val, 2);
249 _sum3 = vfmaq_laneq_f32(_sum3, _w3, _val, 3);
250#else
251 _sum0 = vmlaq_lane_f32(_sum0, _w0, vget_low_f32(_val), 0);
252 _sum1 = vmlaq_lane_f32(_sum1, _w1, vget_low_f32(_val), 1);
253 _sum2 = vmlaq_lane_f32(_sum2, _w2, vget_high_f32(_val), 0);
254 _sum3 = vmlaq_lane_f32(_sum3, _w3, vget_high_f32(_val), 1);
255#endif
256#endif // __ARM_FEATURE_FP16_FML
257
258 sptr += 4;
259 kptr += 16;
260 }
261 for (; i < num_input; i++)
262 {
263 float32x4_t _val = vdupq_n_f32((float)sptr[0]);
264#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
265 float32x4_t _w = vcvt_f32_f16(vld1_f16(kptr));
266#else
267 float32x4_t _w = vcvt_f32_f16((float16x4_t)(vld1_u16(kptr)));
268#endif
269 _sum0 = vfmaq_f32(_sum0, _val, _w);
270
271 sptr += 1;
272 kptr += 4;
273 }
274
275 _sum0 = vaddq_f32(_sum0, _sum1);
276 _sum2 = vaddq_f32(_sum2, _sum3);
277 _sum0 = vaddq_f32(_sum0, _sum2);
278
279 _sum0 = activation_ps(_sum0, activation_type, activation_params);
280
281#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
282 __fp16* outptr = (__fp16*)top_blob;
283 vst1_f16(outptr + p * 4, vcvt_f16_f32(_sum0));
284#else
285 float* outptr = top_blob;
286 vst1q_f32(outptr + p * 4, _sum0);
287#endif
288 }
289}
290
291static void innerproduct_fp16s_neon(const Mat& bottom_blob, Mat& top_blob, const Mat& weight_data_fp16, const Mat& bias_data, int activation_type, const Mat& activation_params, const Option& opt)
292{
293#if NCNN_RUNTIME_CPU && NCNN_ARM82FP16FML && __aarch64__ && !__ARM_FEATURE_FP16_FML
294 if (ncnn::cpu_support_arm_asimdfhm())
295 {
296 innerproduct_fp16s_neon_asimdfhm(bottom_blob, top_blob, weight_data_fp16, bias_data, activation_type, activation_params, opt);
297 return;
298 }
299#endif
300
301#if NCNN_RUNTIME_CPU && NCNN_ARM82 && __aarch64__ && !__ARM_FEATURE_FP16_VECTOR_ARITHMETIC && !__ARM_FEATURE_FP16_FML
302 if (ncnn::cpu_support_arm_asimdhp())
303 {
304 innerproduct_fp16s_neon_asimdhp(bottom_blob, top_blob, weight_data_fp16, bias_data, activation_type, activation_params, opt);
305 return;
306 }
307#endif
308
309 const int num_input = bottom_blob.w * bottom_blob.elempack;
310 const int num_output = top_blob.w;
311
312 const float* bias_data_ptr = bias_data;
313
314 int nn_num_output = num_output >> 2;
315 int remain_num_output_start = nn_num_output << 2;
316
317 #pragma omp parallel for num_threads(opt.num_threads)
318 for (int pp = 0; pp < nn_num_output; pp++)
319 {
320 int p = pp * 4;
321
322 float sums[4] = {0.0f};
323 if (bias_data_ptr)
324 {
325 sums[0] = bias_data_ptr[p];
326 sums[1] = bias_data_ptr[p + 1];
327 sums[2] = bias_data_ptr[p + 2];
328 sums[3] = bias_data_ptr[p + 3];
329 }
330
331#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
332 const __fp16* sptr = bottom_blob;
333 const __fp16* kptr0 = weight_data_fp16.row<const __fp16>(p);
334 const __fp16* kptr1 = weight_data_fp16.row<const __fp16>(p + 1);
335 const __fp16* kptr2 = weight_data_fp16.row<const __fp16>(p + 2);
336 const __fp16* kptr3 = weight_data_fp16.row<const __fp16>(p + 3);
337#else
338 const float* sptr = bottom_blob;
339 const unsigned short* kptr0 = weight_data_fp16.row<const unsigned short>(p);
340 const unsigned short* kptr1 = weight_data_fp16.row<const unsigned short>(p + 1);
341 const unsigned short* kptr2 = weight_data_fp16.row<const unsigned short>(p + 2);
342 const unsigned short* kptr3 = weight_data_fp16.row<const unsigned short>(p + 3);
343#endif
344
345 int i = 0;
346
347 float32x4_t _sum0 = vdupq_n_f32(0.f);
348 float32x4_t _sum1 = vdupq_n_f32(0.f);
349 float32x4_t _sum2 = vdupq_n_f32(0.f);
350 float32x4_t _sum3 = vdupq_n_f32(0.f);
351 for (; i + 3 < num_input; i += 4)
352 {
353#if __ARM_FEATURE_FP16_FML
354 float16x4_t _val = vld1_f16(sptr);
355 float16x4_t _w0 = vld1_f16(kptr0);
356 float16x4_t _w1 = vld1_f16(kptr1);
357 float16x4_t _w2 = vld1_f16(kptr2);
358 float16x4_t _w3 = vld1_f16(kptr3);
359 float16x8_t _w01 = vcombine_f16(_w0, _w1);
360 float16x8_t _w23 = vcombine_f16(_w2, _w3);
361 float16x8_t _valval = vcombine_f16(_val, _val);
362
363 _sum0 = vfmlalq_low_f16(_sum0, _w01, _valval);
364 _sum1 = vfmlalq_high_f16(_sum1, _w01, _valval);
365 _sum2 = vfmlalq_low_f16(_sum2, _w23, _valval);
366 _sum3 = vfmlalq_high_f16(_sum3, _w23, _valval);
367#else // __ARM_FEATURE_FP16_FML
368#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
369 float32x4_t _val = vcvt_f32_f16(vld1_f16(sptr));
370 float32x4_t _w0 = vcvt_f32_f16(vld1_f16(kptr0));
371 float32x4_t _w1 = vcvt_f32_f16(vld1_f16(kptr1));
372 float32x4_t _w2 = vcvt_f32_f16(vld1_f16(kptr2));
373 float32x4_t _w3 = vcvt_f32_f16(vld1_f16(kptr3));
374#else
375 float32x4_t _val = vld1q_f32(sptr);
376 float32x4_t _w0 = vcvt_f32_f16((float16x4_t)(vld1_u16(kptr0)));
377 float32x4_t _w1 = vcvt_f32_f16((float16x4_t)(vld1_u16(kptr1)));
378 float32x4_t _w2 = vcvt_f32_f16((float16x4_t)(vld1_u16(kptr2)));
379 float32x4_t _w3 = vcvt_f32_f16((float16x4_t)(vld1_u16(kptr3)));
380#endif
381
382 _sum0 = vfmaq_f32(_sum0, _val, _w0);
383 _sum1 = vfmaq_f32(_sum1, _val, _w1);
384 _sum2 = vfmaq_f32(_sum2, _val, _w2);
385 _sum3 = vfmaq_f32(_sum3, _val, _w3);
386#endif // __ARM_FEATURE_FP16_FML
387
388 sptr += 4;
389 kptr0 += 4;
390 kptr1 += 4;
391 kptr2 += 4;
392 kptr3 += 4;
393 }
394
395#if __aarch64__
396 sums[0] += vaddvq_f32(_sum0);
397 sums[1] += vaddvq_f32(_sum1);
398 sums[2] += vaddvq_f32(_sum2);
399 sums[3] += vaddvq_f32(_sum3);
400#else
401 float32x2_t _sum0ss = vadd_f32(vget_low_f32(_sum0), vget_high_f32(_sum0));
402 float32x2_t _sum1ss = vadd_f32(vget_low_f32(_sum1), vget_high_f32(_sum1));
403 float32x2_t _sum2ss = vadd_f32(vget_low_f32(_sum2), vget_high_f32(_sum2));
404 float32x2_t _sum3ss = vadd_f32(vget_low_f32(_sum3), vget_high_f32(_sum3));
405 float32x2_t _sum01ss = vpadd_f32(_sum0ss, _sum1ss);
406 float32x2_t _sum23ss = vpadd_f32(_sum2ss, _sum3ss);
407 sums[0] += vget_lane_f32(_sum01ss, 0);
408 sums[1] += vget_lane_f32(_sum01ss, 1);
409 sums[2] += vget_lane_f32(_sum23ss, 0);
410 sums[3] += vget_lane_f32(_sum23ss, 1);
411#endif
412
413 for (; i < num_input; i++)
414 {
415#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
416 sums[0] += (float)(*sptr) * (float)(*kptr0);
417 sums[1] += (float)(*sptr) * (float)(*kptr1);
418 sums[2] += (float)(*sptr) * (float)(*kptr2);
419 sums[3] += (float)(*sptr) * (float)(*kptr3);
420#else
421 sums[0] += *sptr * float16_to_float32(*kptr0);
422 sums[1] += *sptr * float16_to_float32(*kptr1);
423 sums[2] += *sptr * float16_to_float32(*kptr2);
424 sums[3] += *sptr * float16_to_float32(*kptr3);
425#endif
426
427 sptr++;
428 kptr0++;
429 kptr1++;
430 kptr2++;
431 kptr3++;
432 }
433
434 float32x4_t _sum = vld1q_f32(sums);
435
436 _sum = activation_ps(_sum, activation_type, activation_params);
437
438#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
439 __fp16* outptr = (__fp16*)top_blob;
440 vst1_f16(outptr + p, vcvt_f16_f32(_sum));
441#else
442 float* outptr = top_blob;
443 vst1q_f32(outptr + p, _sum);
444#endif
445 }
446
447 #pragma omp parallel for num_threads(opt.num_threads)
448 for (int p = remain_num_output_start; p < num_output; p++)
449 {
450 float sum = 0.f;
451
452 if (bias_data_ptr)
453 sum = bias_data_ptr[p];
454
455#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
456 const __fp16* sptr = bottom_blob;
457 const __fp16* kptr = weight_data_fp16.row<const __fp16>(p);
458#else
459 const float* sptr = bottom_blob;
460 const unsigned short* kptr = weight_data_fp16.row<const unsigned short>(p);
461#endif
462
463 int i = 0;
464
465 float32x4_t _sum = vdupq_n_f32(0.f);
466 for (; i + 3 < num_input; i += 4)
467 {
468#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
469 float32x4_t _val = vcvt_f32_f16(vld1_f16(sptr));
470 float32x4_t _w = vcvt_f32_f16(vld1_f16(kptr));
471#else
472 float32x4_t _val = vld1q_f32(sptr);
473 float32x4_t _w = vcvt_f32_f16((float16x4_t)(vld1_u16(kptr)));
474#endif
475 _sum = vfmaq_f32(_sum, _val, _w);
476
477 sptr += 4;
478 kptr += 4;
479 }
480 for (; i < num_input; i++)
481 {
482#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
483 sum += (float)(*sptr) * (float)(*kptr);
484#else
485 sum += *sptr * float16_to_float32(*kptr);
486#endif
487 sptr++;
488 kptr++;
489 }
490
491#if __aarch64__
492 sum += vaddvq_f32(_sum);
493#else
494 float32x2_t _sumss = vadd_f32(vget_low_f32(_sum), vget_high_f32(_sum));
495 _sumss = vpadd_f32(_sumss, _sumss);
496 sum += vget_lane_f32(_sumss, 0);
497#endif // __aarch64__
498
499 sum = activation_ss(sum, activation_type, activation_params);
500
501#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
502 __fp16* outptr = (__fp16*)top_blob;
503 outptr[p] = (__fp16)sum;
504#else
505 float* outptr = top_blob;
506 outptr[p] = sum;
507#endif
508 }
509}
510
511static void innerproduct_transform_kernel_fp16s_neon(const Mat& weight_data, Mat& weight_data_tm, int num_input, int num_output, const Option& opt)
512{
513#if NCNN_RUNTIME_CPU && NCNN_ARM82FP16FML && __aarch64__ && !__ARM_FEATURE_FP16_FML
514 if (ncnn::cpu_support_arm_asimdfhm())
515 {
516 innerproduct_transform_kernel_fp16s_neon_asimdfhm(weight_data, weight_data_tm, num_input, num_output, opt);
517 return;
518 }
519#endif
520
521#if NCNN_RUNTIME_CPU && NCNN_ARM82 && __aarch64__ && !__ARM_FEATURE_FP16_VECTOR_ARITHMETIC && !__ARM_FEATURE_FP16_FML
522 if (ncnn::cpu_support_arm_asimdhp())
523 {
524 innerproduct_transform_kernel_fp16s_neon_asimdhp(weight_data, weight_data_tm, num_input, num_output, opt);
525 return;
526 }
527#endif
528
529 int out_elempack = 1;
530 if (opt.use_packing_layout)
531 {
532#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
533 out_elempack = opt.use_fp16_arithmetic && num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1;
534#else
535 out_elempack = num_output % 4 == 0 ? 4 : 1;
536#endif
537 }
538
539 // src = inch-outch
540 // dst = pb-inch-outch/pb
541#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
542 if (out_elempack == 8)
543 {
544 Mat weight_data_r2 = weight_data.reshape(num_input, num_output);
545
546 weight_data_tm.create(num_input, num_output / 8, (size_t)16u, 8);
547
548 for (int q = 0; q + 7 < num_output; q += 8)
549 {
550 unsigned short* g0 = weight_data_tm.row<unsigned short>(q / 8);
551
552 const float* k0 = weight_data_r2.row(q);
553 const float* k1 = weight_data_r2.row(q + 1);
554 const float* k2 = weight_data_r2.row(q + 2);
555 const float* k3 = weight_data_r2.row(q + 3);
556 const float* k4 = weight_data_r2.row(q + 4);
557 const float* k5 = weight_data_r2.row(q + 5);
558 const float* k6 = weight_data_r2.row(q + 6);
559 const float* k7 = weight_data_r2.row(q + 7);
560
561 int p = 0;
562#if NCNN_GNU_INLINE_ASM
563 for (; p + 7 < num_input; p += 8)
564 {
565 // transpose 8x8
566 asm volatile(
567 "ld1 {v0.4s, v1.4s}, [%0], #32 \n"
568 "ld1 {v2.4s, v3.4s}, [%1], #32 \n"
569 "ld1 {v4.4s, v5.4s}, [%2], #32 \n"
570 "ld1 {v6.4s, v7.4s}, [%3], #32 \n"
571 "ld1 {v8.4s, v9.4s}, [%4], #32 \n"
572 "ld1 {v10.4s, v11.4s}, [%5], #32 \n"
573 "ld1 {v12.4s, v13.4s}, [%6], #32 \n"
574 "ld1 {v14.4s, v15.4s}, [%7], #32 \n"
575
576 "fcvtn v0.4h, v0.4s \n"
577 "fcvtn2 v0.8h, v1.4s \n"
578 "fcvtn v1.4h, v2.4s \n"
579 "fcvtn2 v1.8h, v3.4s \n"
580 "fcvtn v2.4h, v4.4s \n"
581 "fcvtn2 v2.8h, v5.4s \n"
582 "fcvtn v3.4h, v6.4s \n"
583 "fcvtn2 v3.8h, v7.4s \n"
584 "fcvtn v4.4h, v8.4s \n"
585 "fcvtn2 v4.8h, v9.4s \n"
586 "fcvtn v5.4h, v10.4s \n"
587 "fcvtn2 v5.8h, v11.4s \n"
588 "fcvtn v6.4h, v12.4s \n"
589 "fcvtn2 v6.8h, v13.4s \n"
590 "fcvtn v7.4h, v14.4s \n"
591 "fcvtn2 v7.8h, v15.4s \n"
592
593 "zip1 v16.8h, v0.8h, v4.8h \n"
594 "zip2 v20.8h, v0.8h, v4.8h \n"
595 "zip1 v17.8h, v1.8h, v5.8h \n"
596 "zip2 v21.8h, v1.8h, v5.8h \n"
597 "zip1 v18.8h, v2.8h, v6.8h \n"
598 "zip2 v22.8h, v2.8h, v6.8h \n"
599 "zip1 v19.8h, v3.8h, v7.8h \n"
600 "zip2 v23.8h, v3.8h, v7.8h \n"
601
602 "st4 {v16.8h, v17.8h, v18.8h, v19.8h}, [%8], #64 \n"
603 "st4 {v20.8h, v21.8h, v22.8h, v23.8h}, [%8], #64 \n"
604 : "=r"(k0), // %0
605 "=r"(k1), // %1
606 "=r"(k2), // %2
607 "=r"(k3), // %3
608 "=r"(k4), // %4
609 "=r"(k5), // %5
610 "=r"(k6), // %6
611 "=r"(k7), // %7
612 "=r"(g0) // %8
613 : "0"(k0),
614 "1"(k1),
615 "2"(k2),
616 "3"(k3),
617 "4"(k4),
618 "5"(k5),
619 "6"(k6),
620 "7"(k7),
621 "8"(g0)
622 : "memory", "v0", "v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10", "v11", "v12", "v13", "v14", "v15", "v16", "v17", "v18", "v19", "v20", "v21", "v22", "v23");
623 }
624#endif // NCNN_GNU_INLINE_ASM
625 for (; p < num_input; p++)
626 {
627 g0[0] = float32_to_float16(*k0++);
628 g0[1] = float32_to_float16(*k1++);
629 g0[2] = float32_to_float16(*k2++);
630 g0[3] = float32_to_float16(*k3++);
631 g0[4] = float32_to_float16(*k4++);
632 g0[5] = float32_to_float16(*k5++);
633 g0[6] = float32_to_float16(*k6++);
634 g0[7] = float32_to_float16(*k7++);
635 g0 += 8;
636 }
637 }
638 }
639#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
640
641 if (out_elempack == 4)
642 {
643 Mat weight_data_r2 = weight_data.reshape(num_input, num_output);
644
645 weight_data_tm.create(num_input, num_output / 4, (size_t)8u, 4);
646
647 for (int q = 0; q + 3 < num_output; q += 4)
648 {
649 unsigned short* g0 = weight_data_tm.row<unsigned short>(q / 4);
650
651 const float* k0 = weight_data_r2.row(q);
652 const float* k1 = weight_data_r2.row(q + 1);
653 const float* k2 = weight_data_r2.row(q + 2);
654 const float* k3 = weight_data_r2.row(q + 3);
655
656 int p = 0;
657 for (; p + 3 < num_input; p += 4)
658 {
659 // transpose 4x4
660 uint16x4x4_t _p;
661 _p.val[0] = (uint16x4_t)(vcvt_f16_f32(vld1q_f32(k0)));
662 _p.val[1] = (uint16x4_t)(vcvt_f16_f32(vld1q_f32(k1)));
663 _p.val[2] = (uint16x4_t)(vcvt_f16_f32(vld1q_f32(k2)));
664 _p.val[3] = (uint16x4_t)(vcvt_f16_f32(vld1q_f32(k3)));
665 vst4_u16(g0, _p);
666
667 k0 += 4;
668 k1 += 4;
669 k2 += 4;
670 k3 += 4;
671 g0 += 16;
672 }
673 for (; p < num_input; p++)
674 {
675 g0[0] = float32_to_float16(*k0++);
676 g0[1] = float32_to_float16(*k1++);
677 g0[2] = float32_to_float16(*k2++);
678 g0[3] = float32_to_float16(*k3++);
679 g0 += 4;
680 }
681 }
682 }
683
684 if (out_elempack == 1)
685 {
686 Mat weight_data_r2 = weight_data.reshape(num_input, num_output);
687 ncnn::cast_float32_to_float16(weight_data_r2, weight_data_tm, opt);
688 }
689}