Elffuss BraidHub

kikocis/ncnn public

master · 54.6 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 "convolutiondepthwise_arm.h"
16
17#include "cpu.h"
18#include "layer_type.h"
19
20#if __ARM_NEON
21#include <arm_neon.h>
22#endif // __ARM_NEON
23
24#include "arm_activation.h"
25#include "arm_usability.h"
26
27namespace ncnn {
28
29#if NCNN_GNU_INLINE_ASM
30#include "convolutiondepthwise_3x3.h"
31#include "convolutiondepthwise_5x5.h"
32
33#if NCNN_INT8
34#include "convolutiondepthwise_3x3_int8.h"
35#endif // NCNN_INT8
36
37#if __ARM_NEON
38#include "convolutiondepthwise_3x3_pack4.h"
39#include "convolutiondepthwise_5x5_pack4.h"
40
41#if NCNN_BF16
42#include "convolutiondepthwise_3x3_pack4_bf16s.h"
43#include "convolutiondepthwise_5x5_pack4_bf16s.h"
44#endif // NCNN_BF16
45
46#if NCNN_INT8
47#include "convolutiondepthwise_3x3_pack8_int8.h"
48#endif // NCNN_INT8
49#endif // __ARM_NEON
50#endif // NCNN_GNU_INLINE_ASM
51
52ConvolutionDepthWise_arm::ConvolutionDepthWise_arm()
53{
54#if __ARM_NEON
55 support_packing = true;
56#if NCNN_ARM82
57 support_fp16_storage = cpu_support_arm_asimdhp();
58#endif
59#endif // __ARM_NEON
60
61#if NCNN_BF16
62 support_bf16_storage = true;
63#endif
64
65 activation = 0;
66}
67
68int ConvolutionDepthWise_arm::create_pipeline(const Option& opt)
69{
70 if (dynamic_weight)
71 return 0;
72
73 activation = create_activation_layer(activation_type, activation_params, opt);
74
75#if NCNN_INT8
76 if (opt.use_int8_inference && weight_data.elemsize == (size_t)1u)
77 {
78 return create_pipeline_int8_arm(opt);
79 }
80#endif
81
82#if NCNN_ARM82
83 if (support_fp16_storage && opt.use_fp16_storage)
84 {
85 return create_pipeline_fp16s(opt);
86 }
87#endif
88
89 const int maxk = kernel_w * kernel_h;
90 int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
91
92 // depth-wise
93 if (channels == group && group == num_output)
94 {
95 int elempack = 1;
96#if __ARM_NEON
97 if (opt.use_packing_layout)
98 {
99 elempack = channels % 4 == 0 ? 4 : 1;
100 }
101#endif // __ARM_NEON
102
103#if NCNN_BF16
104 if (opt.use_bf16_storage)
105 {
106#if __ARM_NEON
107 if (elempack == 4)
108 {
109 Mat weight_data_r2 = weight_data.reshape(maxk, group);
110 Mat weight_data_r2_packed;
111 convert_packing(weight_data_r2, weight_data_r2_packed, 4, opt);
112
113 ncnn::cast_float32_to_bfloat16(weight_data_r2_packed, weight_data_tm, opt);
114 }
115#endif // __ARM_NEON
116
117 if (elempack == 1)
118 {
119 ncnn::cast_float32_to_bfloat16(weight_data, weight_data_tm, opt);
120 }
121
122 if (opt.lightmode)
123 weight_data.release();
124
125 return 0;
126 }
127#endif // NCNN_BF16
128
129#if __ARM_NEON
130 // pack4
131 if (elempack == 4)
132 {
133 Mat weight_data_r2 = weight_data.reshape(maxk, group);
134 convert_packing(weight_data_r2, weight_data_tm, 4, opt);
135 }
136#endif // __ARM_NEON
137
138 if (elempack == 1)
139 {
140#if NCNN_GNU_INLINE_ASM
141 if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
142 {
143 weight_data_tm = weight_data;
144 }
145 else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
146 {
147 weight_data_tm = weight_data;
148 }
149 else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
150 {
151 weight_data_tm = weight_data;
152 }
153 else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
154 {
155 weight_data_tm = weight_data;
156 }
157 else
158#endif // NCNN_GNU_INLINE_ASM
159 {
160 // group convolution
161 create_group_ops(opt);
162 }
163 }
164
165 if (opt.lightmode)
166 weight_data.release();
167
168 return 0;
169 }
170
171 // group convolution
172 create_group_ops(opt);
173
174 if (opt.lightmode)
175 weight_data.release();
176
177 return 0;
178}
179
180int ConvolutionDepthWise_arm::create_group_ops(const Option& opt)
181{
182 // create Convolution op for each group
183 const int maxk = kernel_w * kernel_h;
184 int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
185
186 for (int i = 0; i < (int)group_ops.size(); i++)
187 delete group_ops[i];
188
189 group_ops.clear();
190
191 const int channels_g = channels / group;
192 const int num_output_g = num_output / group;
193
194 group_ops.resize(group);
195
196 for (int g = 0; g < group; g++)
197 {
198 Mat weight_data_g = weight_data.range(maxk * channels_g * num_output_g * g, maxk * channels_g * num_output_g).clone();
199 Mat bias_data_g;
200 if (bias_term)
201 bias_data_g = bias_data.range(num_output_g * g, num_output_g);
202
203 ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Convolution);
204
205 // set param
206 ncnn::ParamDict pd;
207 pd.set(0, num_output_g); // num_output
208 pd.set(1, kernel_w);
209 pd.set(11, kernel_h);
210 pd.set(2, dilation_w);
211 pd.set(12, dilation_h);
212 pd.set(3, stride_w);
213 pd.set(13, stride_h);
214 pd.set(4, 0); // pad_w
215 pd.set(14, 0); // pad_h
216 pd.set(5, bias_term);
217 pd.set(6, maxk * channels_g * num_output_g); // weight_data_size
218 pd.set(8, int8_scale_term);
219 pd.set(9, activation_type);
220 pd.set(10, activation_params);
221
222 op->load_param(pd);
223
224 // set weights
225 if (bias_term)
226 {
227 ncnn::Mat weights[5];
228 weights[0] = weight_data_g;
229 weights[1] = bias_data_g;
230
231#if NCNN_INT8
232 if (int8_scale_term)
233 {
234 Mat weight_data_int8_scales_g(num_output_g);
235 weight_data_int8_scales_g.fill(weight_data_int8_scales[g]);
236 weights[2] = weight_data_int8_scales_g;
237 weights[3] = bottom_blob_int8_scales.range(g, 1);
238 }
239 if (int8_scale_term > 100)
240 {
241 weights[4] = top_blob_int8_scales.range(g, 1);
242 }
243#endif
244
245 op->load_model(ModelBinFromMatArray(weights));
246 }
247 else
248 {
249 ncnn::Mat weights[4];
250 weights[0] = weight_data_g;
251
252#if NCNN_INT8
253 if (int8_scale_term)
254 {
255 Mat weight_data_int8_scales_g(num_output_g);
256 weight_data_int8_scales_g.fill(weight_data_int8_scales[g]);
257 weights[1] = weight_data_int8_scales_g;
258 weights[2] = bottom_blob_int8_scales.range(g, 1);
259 }
260 if (int8_scale_term > 100)
261 {
262 weights[3] = top_blob_int8_scales.range(g, 1);
263 }
264#endif
265
266 op->load_model(ModelBinFromMatArray(weights));
267 }
268
269 op->create_pipeline(opt);
270
271 group_ops[g] = op;
272 }
273
274 return 0;
275}
276
277int ConvolutionDepthWise_arm::destroy_pipeline(const Option& opt)
278{
279 if (activation)
280 {
281 activation->destroy_pipeline(opt);
282 delete activation;
283 activation = 0;
284 }
285
286 for (int i = 0; i < (int)group_ops.size(); i++)
287 {
288 group_ops[i]->destroy_pipeline(opt);
289 delete group_ops[i];
290 }
291 group_ops.clear();
292
293 return 0;
294}
295
296int ConvolutionDepthWise_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
297{
298#if NCNN_INT8
299 if (opt.use_int8_inference && int8_scale_term)
300 {
301 return forward_int8_arm(bottom_blob, top_blob, opt);
302 }
303#endif
304
305 int elembits = bottom_blob.elembits();
306
307#if NCNN_ARM82
308 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
309 {
310 if (opt.use_fp16_arithmetic)
311 return forward_fp16sa(bottom_blob, top_blob, opt);
312 else
313 return forward_fp16s(bottom_blob, top_blob, opt);
314 }
315#endif
316
317#if NCNN_BF16
318 if (opt.use_bf16_storage && elembits == 16)
319 return forward_bf16s(bottom_blob, top_blob, opt);
320#endif
321
322 int w = bottom_blob.w;
323 int h = bottom_blob.h;
324 int channels = bottom_blob.c;
325 size_t elemsize = bottom_blob.elemsize;
326 int elempack = bottom_blob.elempack;
327
328 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
329 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
330
331 Mat bottom_blob_bordered;
332 make_padding(bottom_blob, bottom_blob_bordered, opt);
333 if (bottom_blob_bordered.empty())
334 return -100;
335
336 w = bottom_blob_bordered.w;
337 h = bottom_blob_bordered.h;
338
339 int outw = (w - kernel_extent_w) / stride_w + 1;
340 int outh = (h - kernel_extent_h) / stride_h + 1;
341 int out_elempack = 1;
342#if __ARM_NEON
343 if (opt.use_packing_layout)
344 {
345 out_elempack = num_output % 4 == 0 ? 4 : 1;
346 }
347#endif // __ARM_NEON
348 size_t out_elemsize = elemsize / elempack * out_elempack;
349
350 top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
351 if (top_blob.empty())
352 return -100;
353
354 // depth-wise
355 if (channels * elempack == group && group == num_output)
356 {
357#if __ARM_NEON
358 if (elempack == 4)
359 {
360#if NCNN_GNU_INLINE_ASM
361 if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
362 {
363 convdw3x3s1_pack4_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
364
365 if (activation)
366 {
367 activation->forward_inplace(top_blob, opt);
368 }
369
370 return 0;
371 }
372 else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
373 {
374 convdw3x3s2_pack4_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
375
376 if (activation)
377 {
378 activation->forward_inplace(top_blob, opt);
379 }
380
381 return 0;
382 }
383 else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
384 {
385 convdw5x5s1_pack4_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
386
387 if (activation)
388 {
389 activation->forward_inplace(top_blob, opt);
390 }
391
392 return 0;
393 }
394 else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
395 {
396 convdw5x5s2_pack4_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
397
398 if (activation)
399 {
400 activation->forward_inplace(top_blob, opt);
401 }
402
403 return 0;
404 }
405 else
406#endif // NCNN_GNU_INLINE_ASM
407 {
408 const int maxk = kernel_w * kernel_h;
409
410 // kernel offsets
411 std::vector<int> _space_ofs(maxk);
412 int* space_ofs = &_space_ofs[0];
413 {
414 int p1 = 0;
415 int p2 = 0;
416 int gap = w * dilation_h - kernel_w * dilation_w;
417 for (int i = 0; i < kernel_h; i++)
418 {
419 for (int j = 0; j < kernel_w; j++)
420 {
421 space_ofs[p1] = p2;
422 p1++;
423 p2 += dilation_w;
424 }
425 p2 += gap;
426 }
427 }
428
429 #pragma omp parallel for num_threads(opt.num_threads)
430 for (int g = 0; g < channels; g++)
431 {
432 float* outptr = top_blob.channel(g);
433 const float* kptr = (const float*)weight_data_tm + maxk * g * 4;
434 const Mat m = bottom_blob_bordered.channel(g);
435
436 for (int i = 0; i < outh; i++)
437 {
438 for (int j = 0; j < outw; j++)
439 {
440 float32x4_t _sum = vdupq_n_f32(0.f);
441
442 if (bias_term)
443 {
444 _sum = vld1q_f32(((const float*)bias_data) + g * 4);
445 }
446
447 const float* sptr = m.row(i * stride_h) + j * stride_w * 4;
448
449 for (int k = 0; k < maxk; k++)
450 {
451 float32x4_t _val = vld1q_f32(sptr + space_ofs[k] * 4);
452 float32x4_t _w = vld1q_f32(kptr + k * 4);
453 _sum = vmlaq_f32(_sum, _val, _w);
454 }
455
456 _sum = activation_ps(_sum, activation_type, activation_params);
457
458 vst1q_f32(outptr + j * 4, _sum);
459 }
460
461 outptr += outw * 4;
462 }
463 }
464
465 return 0;
466 }
467 }
468#endif // __ARM_NEON
469
470 if (elempack == 1)
471 {
472#if NCNN_GNU_INLINE_ASM
473 if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
474 {
475 convdw3x3s1_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
476
477 if (activation)
478 {
479 activation->forward_inplace(top_blob, opt);
480 }
481
482 return 0;
483 }
484 else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
485 {
486 convdw3x3s2_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
487
488 if (activation)
489 {
490 activation->forward_inplace(top_blob, opt);
491 }
492
493 return 0;
494 }
495 else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
496 {
497 convdw5x5s1_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
498
499 if (activation)
500 {
501 activation->forward_inplace(top_blob, opt);
502 }
503
504 return 0;
505 }
506 else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
507 {
508 convdw5x5s2_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
509
510 if (activation)
511 {
512 activation->forward_inplace(top_blob, opt);
513 }
514
515 return 0;
516 }
517#endif // NCNN_GNU_INLINE_ASM
518 }
519 }
520
521 // group convolution
522 const int channels_g = channels * elempack / group;
523 const int num_output_g = num_output / group;
524
525 int g_elempack = 1;
526 int out_g_elempack = 1;
527#if __ARM_NEON
528 if (opt.use_packing_layout)
529 {
530 g_elempack = channels_g % 4 == 0 ? 4 : 1;
531 out_g_elempack = num_output_g % 4 == 0 ? 4 : 1;
532 }
533#endif
534
535 // unpacking
536 Mat bottom_blob_bordered_unpacked = bottom_blob_bordered;
537 if (elempack == 4 && g_elempack == 1)
538 {
539 Option opt_p = opt;
540 opt_p.blob_allocator = opt.workspace_allocator;
541 convert_packing(bottom_blob_bordered, bottom_blob_bordered_unpacked, 1, opt_p);
542 if (bottom_blob_bordered_unpacked.empty())
543 return -100;
544 }
545
546 Mat top_blob_unpacked = top_blob;
547 if (out_g_elempack == 1 && out_elempack == 4)
548 {
549 top_blob_unpacked.create(outw, outh, num_output, out_elemsize / out_elempack, 1, opt.workspace_allocator);
550 if (top_blob_unpacked.empty())
551 return -100;
552 }
553
554 for (int g = 0; g < group; g++)
555 {
556 const Mat bottom_blob_bordered_g = bottom_blob_bordered_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack);
557 Mat top_blob_g = top_blob_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack);
558
559 const ncnn::Layer* op = group_ops[g];
560
561 Option opt_g = opt;
562 opt_g.blob_allocator = top_blob_unpacked.allocator;
563
564 // forward
565 int ret = op->forward(bottom_blob_bordered_g, top_blob_g, opt_g);
566 if (ret != 0)
567 return ret;
568 }
569
570 // packing
571 if (out_g_elempack == 1 && out_elempack == 4)
572 {
573 convert_packing(top_blob_unpacked, top_blob, 4, opt);
574 if (top_blob.empty())
575 return -100;
576 }
577 else
578 {
579 top_blob = top_blob_unpacked;
580 }
581
582 return 0;
583}
584
585int ConvolutionDepthWise_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
586{
587 const Mat& bottom_blob = bottom_blobs[0];
588 const Mat& _weight_data = bottom_blobs[1];
589 Mat& top_blob = top_blobs[0];
590
591 const int _kernel_w = _weight_data.w;
592 const int _kernel_h = _weight_data.h;
593 const int _num_output = _weight_data.c * _weight_data.elempack;
594
595 Mat weight_data_flattened;
596 flatten(_weight_data, weight_data_flattened, opt);
597 if (weight_data_flattened.empty())
598 return -100;
599
600#if NCNN_ARM82
601 if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && weight_data_flattened.elembits() == 16)
602 {
603 Mat weight_data_flattened_fp32;
604 cast_float16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
605 weight_data_flattened = weight_data_flattened_fp32;
606 }
607#endif // NCNN_ARM82
608#if NCNN_BF16
609 if (opt.use_bf16_storage && weight_data_flattened.elembits() == 16)
610 {
611 Mat weight_data_flattened_fp32;
612 cast_bfloat16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
613 weight_data_flattened = weight_data_flattened_fp32;
614 }
615#endif // NCNN_BF16
616
617 // weight_data_flattened as pack1
618 weight_data_flattened.w *= weight_data_flattened.elempack;
619 weight_data_flattened.elemsize /= weight_data_flattened.elempack;
620 weight_data_flattened.elempack = 1;
621
622 Mat bias_data_flattened;
623 if (bias_term)
624 {
625 const Mat& _bias_data = bottom_blobs[2];
626 flatten(_bias_data, bias_data_flattened, opt);
627 if (bias_data_flattened.empty())
628 return -100;
629
630#if NCNN_ARM82
631 if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && bias_data_flattened.elembits() == 16)
632 {
633 Mat bias_data_flattened_fp32;
634 cast_float16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
635 bias_data_flattened = bias_data_flattened_fp32;
636 }
637#endif // NCNN_ARM82
638#if NCNN_BF16
639 if (opt.use_bf16_storage && bias_data_flattened.elembits() == 16)
640 {
641 Mat bias_data_flattened_fp32;
642 cast_bfloat16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
643 bias_data_flattened = bias_data_flattened_fp32;
644 }
645#endif // NCNN_BF16
646
647 // bias_data_flattened as pack1
648 bias_data_flattened.w *= bias_data_flattened.elempack;
649 bias_data_flattened.elemsize /= bias_data_flattened.elempack;
650 bias_data_flattened.elempack = 1;
651 }
652
653 ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::ConvolutionDepthWise);
654
655 ncnn::ParamDict pd;
656 pd.set(0, _num_output);
657 pd.set(1, _kernel_w);
658 pd.set(11, _kernel_h);
659 pd.set(2, dilation_w);
660 pd.set(12, dilation_h);
661 pd.set(3, stride_w);
662 pd.set(13, stride_h);
663 pd.set(4, pad_left);
664 pd.set(15, pad_right);
665 pd.set(14, pad_top);
666 pd.set(16, pad_bottom);
667 pd.set(18, pad_value);
668 pd.set(5, bias_term);
669 pd.set(6, weight_data_flattened.w);
670 pd.set(7, group);
671 pd.set(8, int8_scale_term);
672 pd.set(9, activation_type);
673 pd.set(10, activation_params);
674
675 op->load_param(pd);
676
677 ncnn::Mat weights[2];
678 weights[0] = weight_data_flattened;
679 weights[1] = bias_data_flattened;
680
681 op->load_model(ncnn::ModelBinFromMatArray(weights));
682
683 op->create_pipeline(opt);
684
685 op->forward(bottom_blob, top_blob, opt);
686
687 op->destroy_pipeline(opt);
688
689 delete op;
690
691 return 0;
692}
693
694#if NCNN_BF16
695int ConvolutionDepthWise_arm::forward_bf16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
696{
697 int w = bottom_blob.w;
698 int h = bottom_blob.h;
699 int channels = bottom_blob.c;
700 size_t elemsize = bottom_blob.elemsize;
701 int elempack = bottom_blob.elempack;
702
703 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
704 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
705
706 Mat bottom_blob_bordered;
707 make_padding(bottom_blob, bottom_blob_bordered, opt);
708 if (bottom_blob_bordered.empty())
709 return -100;
710
711 w = bottom_blob_bordered.w;
712 h = bottom_blob_bordered.h;
713
714 int outw = (w - kernel_extent_w) / stride_w + 1;
715 int outh = (h - kernel_extent_h) / stride_h + 1;
716 int out_elempack = 1;
717#if __ARM_NEON
718 if (opt.use_packing_layout)
719 {
720 out_elempack = num_output % 4 == 0 ? 4 : 1;
721 }
722#endif // __ARM_NEON
723 size_t out_elemsize = elemsize / elempack * out_elempack;
724
725 top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
726 if (top_blob.empty())
727 return -100;
728
729 // depth-wise
730 if (channels * elempack == group && group == num_output)
731 {
732#if __ARM_NEON
733 if (elempack == 4)
734 {
735#if NCNN_GNU_INLINE_ASM
736 if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
737 {
738 convdw3x3s1_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
739
740 if (activation)
741 {
742 activation->forward_inplace(top_blob, opt);
743 }
744 }
745 else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
746 {
747 convdw3x3s2_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
748
749 if (activation)
750 {
751 activation->forward_inplace(top_blob, opt);
752 }
753 }
754 else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
755 {
756 convdw5x5s1_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
757
758 if (activation)
759 {
760 activation->forward_inplace(top_blob, opt);
761 }
762 }
763 else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
764 {
765 convdw5x5s2_pack4_bf16s_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
766
767 if (activation)
768 {
769 activation->forward_inplace(top_blob, opt);
770 }
771 }
772 else
773#endif // NCNN_GNU_INLINE_ASM
774 {
775 const int maxk = kernel_w * kernel_h;
776
777 // kernel offsets
778 std::vector<int> _space_ofs(maxk);
779 int* space_ofs = &_space_ofs[0];
780 {
781 int p1 = 0;
782 int p2 = 0;
783 int gap = w * dilation_h - kernel_w * dilation_w;
784 for (int i = 0; i < kernel_h; i++)
785 {
786 for (int j = 0; j < kernel_w; j++)
787 {
788 space_ofs[p1] = p2;
789 p1++;
790 p2 += dilation_w;
791 }
792 p2 += gap;
793 }
794 }
795
796 #pragma omp parallel for num_threads(opt.num_threads)
797 for (int g = 0; g < channels; g++)
798 {
799 unsigned short* outptr = top_blob.channel(g);
800 const unsigned short* kptr = (const unsigned short*)weight_data_tm + maxk * g * 4;
801 const Mat m = bottom_blob_bordered.channel(g);
802
803 for (int i = 0; i < outh; i++)
804 {
805 for (int j = 0; j < outw; j++)
806 {
807 float32x4_t _sum = vdupq_n_f32(0.f);
808
809 if (bias_term)
810 {
811 _sum = vld1q_f32(((const float*)bias_data) + g * 4);
812 }
813
814 const unsigned short* sptr = m.row<const unsigned short>(i * stride_h) + j * stride_w * 4;
815
816 for (int k = 0; k < maxk; k++)
817 {
818 float32x4_t _val = bfloat2float(vld1_u16(sptr + space_ofs[k] * 4));
819 float32x4_t _w = bfloat2float(vld1_u16(kptr + k * 4));
820 _sum = vmlaq_f32(_sum, _val, _w);
821 }
822
823 _sum = activation_ps(_sum, activation_type, activation_params);
824
825 vst1_u16(outptr + j * 4, float2bfloat(_sum));
826 }
827
828 outptr += outw * 4;
829 }
830 }
831 }
832
833 return 0;
834 }
835#endif // __ARM_NEON
836
837 if (elempack == 1)
838 {
839#if NCNN_GNU_INLINE_ASM
840 // if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
841 // {
842 // convdw3x3s1_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
843 //
844 // if (activation)
845 // {
846 // activation->forward_inplace(top_blob, opt);
847 // }
848 //
849 // return 0;
850 // }
851 // else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
852 // {
853 // convdw3x3s2_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
854 //
855 // if (activation)
856 // {
857 // activation->forward_inplace(top_blob, opt);
858 // }
859 //
860 // return 0;
861 // }
862 // else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1)
863 // {
864 // convdw5x5s1_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
865 //
866 // if (activation)
867 // {
868 // activation->forward_inplace(top_blob, opt);
869 // }
870 //
871 // return 0;
872 // }
873 // else if (kernel_w == 5 && kernel_h == 5 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2)
874 // {
875 // convdw5x5s2_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, opt);
876 //
877 // if (activation)
878 // {
879 // activation->forward_inplace(top_blob, opt);
880 // }
881 //
882 // return 0;
883 // }
884 // else
885#endif // NCNN_GNU_INLINE_ASM
886 {
887 const int maxk = kernel_w * kernel_h;
888
889 // kernel offsets
890 std::vector<int> _space_ofs(maxk);
891 int* space_ofs = &_space_ofs[0];
892 {
893 int p1 = 0;
894 int p2 = 0;
895 int gap = w * dilation_h - kernel_w * dilation_w;
896 for (int i = 0; i < kernel_h; i++)
897 {
898 for (int j = 0; j < kernel_w; j++)
899 {
900 space_ofs[p1] = p2;
901 p1++;
902 p2 += dilation_w;
903 }
904 p2 += gap;
905 }
906 }
907
908 #pragma omp parallel for num_threads(opt.num_threads)
909 for (int g = 0; g < group; g++)
910 {
911 unsigned short* outptr = top_blob.channel(g);
912 const unsigned short* kptr = (const unsigned short*)weight_data_tm + maxk * g;
913 const Mat m = bottom_blob_bordered.channel(g);
914
915 for (int i = 0; i < outh; i++)
916 {
917 for (int j = 0; j < outw; j++)
918 {
919 float sum = 0.f;
920
921 if (bias_term)
922 sum = bias_data[g];
923
924 const unsigned short* sptr = m.row<const unsigned short>(i * stride_h) + j * stride_w;
925
926 for (int k = 0; k < maxk; k++)
927 {
928 float val = bfloat16_to_float32(sptr[space_ofs[k]]);
929 float w = bfloat16_to_float32(kptr[k]);
930 sum += val * w;
931 }
932
933 sum = activation_ss(sum, activation_type, activation_params);
934
935 outptr[j] = float32_to_bfloat16(sum);
936 }
937
938 outptr += outw;
939 }
940 }
941 }
942 }
943
944 return 0;
945 }
946
947 // group convolution
948 const int channels_g = channels * elempack / group;
949 const int num_output_g = num_output / group;
950
951 int g_elempack = 1;
952 int out_g_elempack = 1;
953#if __ARM_NEON
954 if (opt.use_packing_layout)
955 {
956 g_elempack = channels_g % 4 == 0 ? 4 : 1;
957 out_g_elempack = num_output_g % 4 == 0 ? 4 : 1;
958 }
959#endif
960
961 // unpacking
962 Mat bottom_blob_bordered_unpacked = bottom_blob_bordered;
963 if (elempack == 4 && g_elempack == 1)
964 {
965 Option opt_p = opt;
966 opt_p.blob_allocator = opt.workspace_allocator;
967 convert_packing(bottom_blob_bordered, bottom_blob_bordered_unpacked, 1, opt_p);
968 if (bottom_blob_bordered_unpacked.empty())
969 return -100;
970 }
971
972 Mat top_blob_unpacked = top_blob;
973 if (out_g_elempack == 1 && out_elempack == 4)
974 {
975 top_blob_unpacked.create(outw, outh, num_output, out_elemsize / out_elempack, 1, opt.workspace_allocator);
976 if (top_blob_unpacked.empty())
977 return -100;
978 }
979
980 for (int g = 0; g < group; g++)
981 {
982 const Mat bottom_blob_bordered_g = bottom_blob_bordered_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack);
983 Mat top_blob_g = top_blob_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack);
984
985 const ncnn::Layer* op = group_ops[g];
986
987 Option opt_g = opt;
988 opt_g.blob_allocator = top_blob_unpacked.allocator;
989
990 // forward
991 int ret = op->forward(bottom_blob_bordered_g, top_blob_g, opt_g);
992 if (ret != 0)
993 return ret;
994 }
995
996 // packing
997 if (out_g_elempack == 1 && out_elempack == 4)
998 {
999 convert_packing(top_blob_unpacked, top_blob, 4, opt);
1000 if (top_blob.empty())
1001 return -100;
1002 }
1003 else
1004 {
1005 top_blob = top_blob_unpacked;
1006 }
1007
1008 return 0;
1009}
1010#endif // NCNN_BF16
1011
1012#if NCNN_INT8
1013int ConvolutionDepthWise_arm::create_pipeline_int8_arm(const Option& opt)
1014{
1015 const int maxk = kernel_w * kernel_h;
1016 int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
1017
1018 // depth-wise
1019 if (channels == group && group == num_output)
1020 {
1021 int elempack = 1;
1022#if __ARM_NEON
1023 if (opt.use_packing_layout)
1024 {
1025 elempack = channels % 8 == 0 ? 8 : 1;
1026 }
1027#endif // __ARM_NEON
1028
1029 if (elempack == 8)
1030 {
1031 Mat weight_data_r2 = weight_data.reshape(maxk, group);
1032 convert_packing(weight_data_r2, weight_data_tm, 8, opt);
1033 }
1034
1035 if (elempack == 1)
1036 {
1037 weight_data_tm = weight_data;
1038 }
1039
1040 if (opt.lightmode)
1041 weight_data.release();
1042
1043 return 0;
1044 }
1045
1046 // group convolution
1047 create_group_ops(opt);
1048
1049 if (opt.lightmode)
1050 weight_data.release();
1051
1052 return 0;
1053}
1054
1055int ConvolutionDepthWise_arm::forward_int8_arm(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
1056{
1057 int w = bottom_blob.w;
1058 int h = bottom_blob.h;
1059 int channels = bottom_blob.c;
1060 int elempack = bottom_blob.elempack;
1061
1062 int elembits = bottom_blob.elembits();
1063
1064 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
1065 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
1066
1067 Mat bottom_blob_int8 = bottom_blob;
1068 if (elembits != 8)
1069 {
1070 const int channels_g = channels * elempack / group;
1071
1072 Mat scales(channels * elempack);
1073 {
1074 float* ps = scales;
1075 for (int g = 0; g < group; g++)
1076 {
1077 float scale = bottom_blob_int8_scales[g];
1078 for (int q = 0; q < channels_g; q++)
1079 {
1080 *ps++ = scale;
1081 }
1082 }
1083 }
1084
1085 Option opt_q = opt;
1086 opt_q.blob_allocator = opt.workspace_allocator;
1087 quantize_to_int8(bottom_blob, bottom_blob_int8, scales, opt_q);
1088 if (bottom_blob_int8.empty())
1089 return -100;
1090 }
1091
1092 Mat bottom_blob_bordered;
1093 make_padding(bottom_blob_int8, bottom_blob_bordered, opt);
1094 if (bottom_blob_bordered.empty())
1095 return -100;
1096
1097 w = bottom_blob_bordered.w;
1098 h = bottom_blob_bordered.h;
1099 channels = bottom_blob_bordered.c;
1100 elempack = bottom_blob_bordered.elempack;
1101
1102 int outw = (w - kernel_extent_w) / stride_w + 1;
1103 int outh = (h - kernel_extent_h) / stride_h + 1;
1104
1105 // depth-wise
1106 if (channels * elempack == group && group == num_output)
1107 {
1108 int out_elempack = 1;
1109#if __ARM_NEON
1110 if (opt.use_packing_layout)
1111 {
1112 out_elempack = num_output % 8 == 0 ? 8 : 1;
1113 }
1114#endif // __ARM_NEON
1115 bool use_int8_requantize = int8_scale_term > 100;
1116 size_t out_elemsize = use_int8_requantize ? 1u * out_elempack : 4u * out_elempack;
1117#if NCNN_ARM82
1118 if (support_fp16_storage && opt.use_fp16_storage)
1119 {
1120 out_elemsize = use_int8_requantize ? 1u * out_elempack : 2u * out_elempack;
1121 }
1122#endif
1123 if (opt.use_bf16_storage)
1124 out_elemsize = use_int8_requantize ? 1u * out_elempack : 2u * out_elempack;
1125
1126 top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
1127 if (top_blob.empty())
1128 return -100;
1129
1130 // TODO use fp16 / bf16
1131 out_elemsize = use_int8_requantize ? 1u * out_elempack : 4u * out_elempack;
1132 top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
1133 if (top_blob.empty())
1134 return -100;
1135
1136#if __ARM_NEON
1137 if (elempack == 8)
1138 {
1139#if NCNN_GNU_INLINE_ASM
1140 if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1 && (activation_type == 0 || activation_type == 1))
1141 {
1142 Mat top_blob_int32;
1143 top_blob_int32.create(outw, outh, num_output / out_elempack, (size_t)4u * out_elempack, out_elempack, opt.workspace_allocator);
1144 if (top_blob_int32.empty())
1145 return -100;
1146
1147 convdw3x3s1_pack8_int8_neon(bottom_blob_bordered, top_blob_int32, weight_data_tm, opt);
1148
1149 Mat scale_in_data(group);
1150 for (int g = 0; g < group; g++)
1151 {
1152 // dequantize
1153 float scale_in;
1154 if (weight_data_int8_scales[g] == 0)
1155 scale_in = 0;
1156 else
1157 scale_in = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]);
1158
1159 scale_in_data[g] = scale_in;
1160 }
1161
1162 if (use_int8_requantize)
1163 {
1164 requantize_from_int32_to_int8(top_blob_int32, top_blob, scale_in_data, top_blob_int8_scales, bias_data, activation_type, activation_params, opt);
1165 }
1166 else
1167 {
1168 dequantize_from_int32(top_blob_int32, top_blob, scale_in_data, bias_data, opt);
1169
1170 if (activation)
1171 {
1172 activation->forward_inplace(top_blob, opt);
1173 }
1174 }
1175 }
1176 else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (activation_type == 0 || activation_type == 1))
1177 {
1178 Mat top_blob_int32;
1179 top_blob_int32.create(outw, outh, num_output / out_elempack, (size_t)4u * out_elempack, out_elempack, opt.workspace_allocator);
1180 if (top_blob_int32.empty())
1181 return -100;
1182
1183 convdw3x3s2_pack8_int8_neon(bottom_blob_bordered, top_blob_int32, weight_data_tm, opt);
1184
1185 Mat scale_in_data(group);
1186 for (int g = 0; g < group; g++)
1187 {
1188 // dequantize
1189 float scale_in;
1190 if (weight_data_int8_scales[g] == 0)
1191 scale_in = 0;
1192 else
1193 scale_in = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]);
1194
1195 scale_in_data[g] = scale_in;
1196 }
1197
1198 if (use_int8_requantize)
1199 {
1200 requantize_from_int32_to_int8(top_blob_int32, top_blob, scale_in_data, top_blob_int8_scales, bias_data, activation_type, activation_params, opt);
1201 }
1202 else
1203 {
1204 dequantize_from_int32(top_blob_int32, top_blob, scale_in_data, bias_data, opt);
1205
1206 if (activation)
1207 {
1208 activation->forward_inplace(top_blob, opt);
1209 }
1210 }
1211 }
1212 else
1213#endif // NCNN_GNU_INLINE_ASM
1214 {
1215 const int maxk = kernel_w * kernel_h;
1216
1217 // kernel offsets
1218 std::vector<int> _space_ofs(maxk);
1219 int* space_ofs = &_space_ofs[0];
1220 {
1221 int p1 = 0;
1222 int p2 = 0;
1223 int gap = w * dilation_h - kernel_w * dilation_w;
1224 for (int i = 0; i < kernel_h; i++)
1225 {
1226 for (int j = 0; j < kernel_w; j++)
1227 {
1228 space_ofs[p1] = p2;
1229 p1++;
1230 p2 += dilation_w;
1231 }
1232 p2 += gap;
1233 }
1234 }
1235
1236 #pragma omp parallel for num_threads(opt.num_threads)
1237 for (int g = 0; g < channels; g++)
1238 {
1239 signed char* outptr_s8 = top_blob.channel(g);
1240 float* outptr_f32 = top_blob.channel(g);
1241 const signed char* kptr = (const signed char*)weight_data_tm + maxk * g * 8;
1242 const Mat m = bottom_blob_bordered.channel(g);
1243
1244 for (int i = 0; i < outh; i++)
1245 {
1246 for (int j = 0; j < outw; j++)
1247 {
1248 int32x4_t _sum0 = vdupq_n_s32(0);
1249 int32x4_t _sum1 = vdupq_n_s32(0);
1250
1251 const signed char* sptr = m.row<const signed char>(i * stride_h) + j * stride_w * 8;
1252
1253 for (int k = 0; k < maxk; k++)
1254 {
1255 int8x8_t _val = vld1_s8(sptr + space_ofs[k] * 8);
1256 int8x8_t _w = vld1_s8(kptr + k * 8);
1257 int16x8_t _s0 = vmull_s8(_val, _w);
1258 _sum0 = vaddw_s16(_sum0, vget_low_s16(_s0));
1259 _sum1 = vaddw_s16(_sum1, vget_high_s16(_s0));
1260 }
1261
1262 float32x4_t _scale_in0;
1263 float32x4_t _scale_in1;
1264 {
1265 float32x4_t _bottom_blob_int8_scales0 = vld1q_f32((const float*)bottom_blob_int8_scales + g * 8);
1266 float32x4_t _bottom_blob_int8_scales1 = vld1q_f32((const float*)bottom_blob_int8_scales + g * 8 + 4);
1267 float32x4_t _weight_data_int8_scales0 = vld1q_f32((const float*)weight_data_int8_scales + g * 8);
1268 float32x4_t _weight_data_int8_scales1 = vld1q_f32((const float*)weight_data_int8_scales + g * 8 + 4);
1269 _scale_in0 = div_ps(vdupq_n_f32(1.f), vmulq_f32(_bottom_blob_int8_scales0, _weight_data_int8_scales0));
1270 _scale_in1 = div_ps(vdupq_n_f32(1.f), vmulq_f32(_bottom_blob_int8_scales1, _weight_data_int8_scales1));
1271
1272 uint32x4_t _m0 = vmvnq_u32(vceqq_f32(_weight_data_int8_scales0, vdupq_n_f32(0.f)));
1273 uint32x4_t _m1 = vmvnq_u32(vceqq_f32(_weight_data_int8_scales1, vdupq_n_f32(0.f)));
1274 _scale_in0 = vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(_scale_in0), _m0));
1275 _scale_in1 = vreinterpretq_f32_u32(vandq_u32(vreinterpretq_u32_f32(_scale_in1), _m1));
1276 }
1277
1278 float32x4_t _sumfp32_0 = vmulq_f32(vcvtq_f32_s32(_sum0), _scale_in0);
1279 float32x4_t _sumfp32_1 = vmulq_f32(vcvtq_f32_s32(_sum1), _scale_in1);
1280
1281 if (bias_term)
1282 {
1283 float32x4_t _bias0 = vld1q_f32((const float*)bias_data + g * 8);
1284 float32x4_t _bias1 = vld1q_f32((const float*)bias_data + g * 8 + 4);
1285 _sumfp32_0 = vaddq_f32(_sumfp32_0, _bias0);
1286 _sumfp32_1 = vaddq_f32(_sumfp32_1, _bias1);
1287 }
1288
1289 _sumfp32_0 = activation_ps(_sumfp32_0, activation_type, activation_params);
1290 _sumfp32_1 = activation_ps(_sumfp32_1, activation_type, activation_params);
1291
1292 if (use_int8_requantize)
1293 {
1294 // requantize
1295 float32x4_t _scale_out0 = vld1q_f32((const float*)top_blob_int8_scales + g * 8);
1296 float32x4_t _scale_out1 = vld1q_f32((const float*)top_blob_int8_scales + g * 8 + 4);
1297 int8x8_t _sum8 = float2int8(vmulq_f32(_sumfp32_0, _scale_out0), vmulq_f32(_sumfp32_1, _scale_out1));
1298 vst1_s8(outptr_s8, _sum8);
1299 outptr_s8 += 8;
1300 }
1301 else
1302 {
1303 // dequantize
1304 vst1q_f32(outptr_f32, _sumfp32_0);
1305 vst1q_f32(outptr_f32 + 4, _sumfp32_1);
1306 outptr_f32 += 8;
1307 }
1308 }
1309 }
1310 }
1311 }
1312 }
1313#endif // __ARM_NEON
1314
1315 if (elempack == 1)
1316 {
1317#if NCNN_GNU_INLINE_ASM
1318 if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 1 && stride_h == 1 && (activation_type == 0 || activation_type == 1))
1319 {
1320 if (use_int8_requantize)
1321 {
1322 std::vector<float> requantize_scales;
1323 for (int g = 0; g < group; g++)
1324 {
1325 float scale_in;
1326 if (weight_data_int8_scales[g] == 0)
1327 scale_in = 0;
1328 else
1329 scale_in = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]);
1330
1331 float scale_out = top_blob_int8_scales[g];
1332
1333 requantize_scales.push_back(scale_in);
1334 requantize_scales.push_back(scale_out);
1335 }
1336
1337 convdw3x3s1_int8_requant_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, requantize_scales, opt);
1338 }
1339 else
1340 {
1341 Mat top_blob_int32;
1342 top_blob_int32.create(outw, outh, num_output, (size_t)4u, opt.workspace_allocator);
1343 if (top_blob_int32.empty())
1344 return -100;
1345
1346 convdw3x3s1_int8_neon(bottom_blob_bordered, top_blob_int32, weight_data_tm, opt);
1347 // convdw3x3s1_int8_dequant_neon(bottom_blob_bordered, top_blob_int32, weight_data_tm, bias_data, dequantize_scales, opt);
1348
1349 Mat scale_data(group);
1350 for (int g = 0; g < group; g++)
1351 {
1352 // dequantize
1353 float scale_in;
1354 if (weight_data_int8_scales[g] == 0)
1355 scale_in = 0;
1356 else
1357 scale_in = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]);
1358
1359 scale_data[g] = scale_in;
1360 }
1361
1362 dequantize_from_int32(top_blob_int32, top_blob, scale_data, bias_data, opt);
1363 }
1364
1365 if (activation)
1366 {
1367 activation->forward_inplace(top_blob, opt);
1368 }
1369 }
1370 else if (kernel_w == 3 && kernel_h == 3 && dilation_w == 1 && dilation_h == 1 && stride_w == 2 && stride_h == 2 && (activation_type == 0 || activation_type == 1))
1371 {
1372 if (use_int8_requantize)
1373 {
1374 std::vector<float> requantize_scales;
1375 for (int g = 0; g < group; g++)
1376 {
1377 float scale_in;
1378 if (weight_data_int8_scales[g] == 0)
1379 scale_in = 0;
1380 else
1381 scale_in = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]);
1382
1383 float scale_out = top_blob_int8_scales[g];
1384
1385 requantize_scales.push_back(scale_in);
1386 requantize_scales.push_back(scale_out);
1387 }
1388
1389 convdw3x3s2_int8_requant_neon(bottom_blob_bordered, top_blob, weight_data_tm, bias_data, requantize_scales, opt);
1390 }
1391 else
1392 {
1393 Mat top_blob_int32;
1394 top_blob_int32.create(outw, outh, num_output, (size_t)4u, opt.workspace_allocator);
1395 if (top_blob_int32.empty())
1396 return -100;
1397
1398 convdw3x3s2_int8_neon(bottom_blob_bordered, top_blob_int32, weight_data_tm, opt);
1399 // convdw3x3s2_int8_dequant_neon(bottom_blob_bordered, top_blob_int32, weight_data_tm, bias_data, dequantize_scales, opt);
1400
1401 Mat scale_data(group);
1402 for (int g = 0; g < group; g++)
1403 {
1404 // dequantize
1405 float scale_in;
1406 if (weight_data_int8_scales[g] == 0)
1407 scale_in = 0;
1408 else
1409 scale_in = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]);
1410
1411 scale_data[g] = scale_in;
1412 }
1413
1414 dequantize_from_int32(top_blob_int32, top_blob, scale_data, bias_data, opt);
1415 }
1416
1417 if (activation)
1418 {
1419 activation->forward_inplace(top_blob, opt);
1420 }
1421 }
1422 else
1423#endif // NCNN_GNU_INLINE_ASM
1424 {
1425 const int maxk = kernel_w * kernel_h;
1426
1427 // kernel offsets
1428 std::vector<int> _space_ofs(maxk);
1429 int* space_ofs = &_space_ofs[0];
1430 {
1431 int p1 = 0;
1432 int p2 = 0;
1433 int gap = w * dilation_h - kernel_w * dilation_w;
1434 for (int i = 0; i < kernel_h; i++)
1435 {
1436 for (int j = 0; j < kernel_w; j++)
1437 {
1438 space_ofs[p1] = p2;
1439 p1++;
1440 p2 += dilation_w;
1441 }
1442 p2 += gap;
1443 }
1444 }
1445
1446 #pragma omp parallel for num_threads(opt.num_threads)
1447 for (int g = 0; g < group; g++)
1448 {
1449 signed char* outptr_s8 = top_blob.channel(g);
1450 float* outptr_f32 = top_blob.channel(g);
1451 const signed char* kptr = (const signed char*)weight_data_tm + maxk * g;
1452 const Mat m = bottom_blob_bordered.channel(g);
1453
1454 for (int i = 0; i < outh; i++)
1455 {
1456 for (int j = 0; j < outw; j++)
1457 {
1458 int sum = 0;
1459
1460 const signed char* sptr = m.row<const signed char>(i * stride_h) + j * stride_w;
1461
1462 for (int k = 0; k < maxk; k++)
1463 {
1464 signed char val = sptr[space_ofs[k]];
1465 signed char w = kptr[k];
1466 sum += val * w;
1467 }
1468
1469 float scale_in;
1470 if (weight_data_int8_scales[g] == 0)
1471 scale_in = 0;
1472 else
1473 scale_in = 1.f / (bottom_blob_int8_scales[g] * weight_data_int8_scales[g]);
1474
1475 float sumfp32 = sum * scale_in;
1476
1477 if (bias_term)
1478 sumfp32 += bias_data[g];
1479
1480 sumfp32 = activation_ss(sumfp32, activation_type, activation_params);
1481
1482 if (use_int8_requantize)
1483 {
1484 // requantize
1485 float scale_out = top_blob_int8_scales[g];
1486 signed char sums8 = float2int8(sumfp32 * scale_out);
1487 outptr_s8[0] = sums8;
1488 outptr_s8 += 1;
1489 }
1490 else
1491 {
1492 // dequantize
1493 outptr_f32[0] = sumfp32;
1494 outptr_f32 += 1;
1495 }
1496 }
1497 }
1498 }
1499 }
1500 }
1501
1502 return 0;
1503 }
1504
1505 bool use_int8_requantize = int8_scale_term > 100;
1506 int out_elempack = 1;
1507#if __ARM_NEON
1508 if (opt.use_packing_layout)
1509 {
1510 if (use_int8_requantize)
1511 out_elempack = num_output % 8 == 0 ? 8 : 1;
1512 else
1513 out_elempack = num_output % 4 == 0 ? 4 : 1;
1514 }
1515#endif // __ARM_NEON
1516 size_t out_elemsize = use_int8_requantize ? 1u * out_elempack : 4u * out_elempack;
1517#if NCNN_ARM82
1518 if (support_fp16_storage && opt.use_fp16_storage)
1519 {
1520 out_elemsize = use_int8_requantize ? 1u * out_elempack : 2u * out_elempack;
1521 }
1522#endif
1523 if (opt.use_bf16_storage)
1524 out_elemsize = use_int8_requantize ? 1u * out_elempack : 2u * out_elempack;
1525
1526 top_blob.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
1527 if (top_blob.empty())
1528 return -100;
1529
1530 // group convolution
1531 const int channels_g = channels * elempack / group;
1532 const int num_output_g = num_output / group;
1533
1534 int g_elempack = 1;
1535 int out_g_elempack = 1;
1536#if __ARM_NEON
1537 if (opt.use_packing_layout)
1538 {
1539 g_elempack = channels_g % 8 == 0 ? 8 : 1;
1540 if (use_int8_requantize)
1541 out_g_elempack = num_output_g % 8 == 0 ? 8 : 1;
1542 else
1543 out_g_elempack = num_output_g % 4 == 0 ? 4 : 1;
1544 }
1545#endif // __ARM_NEON
1546
1547 // unpacking
1548 Mat bottom_blob_bordered_unpacked = bottom_blob_bordered;
1549 if (elempack > g_elempack)
1550 {
1551 Option opt_p = opt;
1552 opt_p.blob_allocator = opt.workspace_allocator;
1553 convert_packing(bottom_blob_bordered, bottom_blob_bordered_unpacked, g_elempack, opt_p);
1554 if (bottom_blob_bordered_unpacked.empty())
1555 return -100;
1556 }
1557
1558 Mat top_blob_unpacked = top_blob;
1559 if (out_g_elempack < out_elempack)
1560 {
1561 top_blob_unpacked.create(outw, outh, num_output / out_g_elempack, out_elemsize / out_elempack * out_g_elempack, out_g_elempack, opt.workspace_allocator);
1562 if (top_blob_unpacked.empty())
1563 return -100;
1564 }
1565
1566 for (int g = 0; g < group; g++)
1567 {
1568 const Mat bottom_blob_bordered_g = bottom_blob_bordered_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack);
1569 Mat top_blob_g = top_blob_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack);
1570
1571 const ncnn::Layer* op = group_ops[g];
1572
1573 Option opt_g = opt;
1574 opt_g.blob_allocator = top_blob_unpacked.allocator;
1575
1576 // forward
1577 int ret = op->forward(bottom_blob_bordered_g, top_blob_g, opt_g);
1578 if (ret != 0)
1579 return ret;
1580 }
1581
1582 // packing
1583 if (out_g_elempack < out_elempack)
1584 {
1585 convert_packing(top_blob_unpacked, top_blob, out_elempack, opt);
1586 if (top_blob.empty())
1587 return -100;
1588 }
1589 else
1590 {
1591 top_blob = top_blob_unpacked;
1592 }
1593
1594 return 0;
1595}
1596#endif // NCNN_INT8
1597
1598} // namespace ncnn