Elffuss BraidHub

kikocis/ncnn public

master · 26.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 "deconvolutiondepthwise_arm.h"
16
17#include "layer_type.h"
18
19#if __ARM_NEON
20#include <arm_neon.h>
21#endif // __ARM_NEON
22
23#include "arm_activation.h"
24#include "arm_usability.h"
25
26#include "cpu.h"
27
28namespace ncnn {
29
30DeconvolutionDepthWise_arm::DeconvolutionDepthWise_arm()
31{
32#if __ARM_NEON
33 support_packing = true;
34#if NCNN_ARM82
35 support_fp16_storage = cpu_support_arm_asimdhp();
36#endif
37#endif // __ARM_NEON
38
39#if NCNN_BF16
40 support_bf16_storage = true;
41#endif
42}
43
44int DeconvolutionDepthWise_arm::create_pipeline(const Option& opt)
45{
46 if (dynamic_weight)
47 return 0;
48
49#if NCNN_ARM82
50 if (support_fp16_storage && opt.use_fp16_storage)
51 {
52 return create_pipeline_fp16s(opt);
53 }
54#endif
55
56 // create Deconvolution op for each group
57 const int maxk = kernel_w * kernel_h;
58 int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
59
60 // depth-wise
61 if (channels == group && group == num_output)
62 {
63 int elempack = 1;
64#if __ARM_NEON
65 if (opt.use_packing_layout)
66 {
67 elempack = channels % 4 == 0 ? 4 : 1;
68 }
69#endif
70
71 Mat weight_data_transposed(weight_data.w);
72 {
73 float* pt = weight_data_transposed;
74 const float* p = weight_data;
75
76 for (int i = 0; i < (channels / group) * (num_output / group) * group; i++)
77 {
78 for (int k = 0; k < maxk; k++)
79 {
80 pt[maxk - 1 - k] = p[k];
81 }
82
83 p += maxk;
84 pt += maxk;
85 }
86 }
87
88#if NCNN_BF16
89 if (opt.use_bf16_storage)
90 {
91#if __ARM_NEON
92 if (elempack == 4)
93 {
94 Mat weight_data_r2 = weight_data_transposed.reshape(maxk, group);
95 Mat weight_data_r2_packed;
96 convert_packing(weight_data_r2, weight_data_r2_packed, 4, opt);
97
98 ncnn::cast_float32_to_bfloat16(weight_data_r2_packed, weight_data_tm, opt);
99 }
100#endif // __ARM_NEON
101
102 if (elempack == 1)
103 {
104 ncnn::cast_float32_to_bfloat16(weight_data_transposed, weight_data_tm, opt);
105 }
106
107 if (opt.lightmode)
108 weight_data.release();
109
110 return 0;
111 }
112#endif // NCNN_BF16
113
114#if __ARM_NEON
115 // pack4
116 if (elempack == 4)
117 {
118 Mat weight_data_r2 = weight_data_transposed.reshape(maxk, group);
119 convert_packing(weight_data_r2, weight_data_tm, 4, opt);
120 }
121#endif // __ARM_NEON
122
123 // pack1
124 if (elempack == 1)
125 {
126 weight_data_tm = weight_data_transposed;
127 }
128 }
129 else
130 {
131 // group deconvolution
132 for (int i = 0; i < (int)group_ops.size(); i++)
133 delete group_ops[i];
134
135 group_ops.clear();
136
137 const int channels_g = channels / group;
138 const int num_output_g = num_output / group;
139
140 group_ops.resize(group);
141
142 for (int g = 0; g < group; g++)
143 {
144 Mat weight_data_g = weight_data.range(maxk * channels_g * num_output_g * g, maxk * channels_g * num_output_g).clone();
145 Mat bias_data_g;
146 if (bias_term)
147 bias_data_g = bias_data.range(num_output_g * g, num_output_g);
148
149 ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Deconvolution);
150
151 // set param
152 ncnn::ParamDict pd;
153 pd.set(0, num_output_g); // num_output
154 pd.set(1, kernel_w);
155 pd.set(11, kernel_h);
156 pd.set(2, dilation_w);
157 pd.set(12, dilation_h);
158 pd.set(3, stride_w);
159 pd.set(13, stride_h);
160 pd.set(4, 0); // pad_w
161 pd.set(14, 0); // pad_h
162 pd.set(18, output_pad_right);
163 pd.set(19, output_pad_bottom);
164 pd.set(5, bias_term);
165 pd.set(6, maxk * channels_g * num_output_g); // weight_data_size
166 pd.set(9, activation_type);
167 pd.set(10, activation_params);
168
169 op->load_param(pd);
170
171 // set weights
172 if (bias_term)
173 {
174 ncnn::Mat weights[2];
175 weights[0] = weight_data_g;
176 weights[1] = bias_data_g;
177
178 op->load_model(ModelBinFromMatArray(weights));
179 }
180 else
181 {
182 ncnn::Mat weights[1];
183 weights[0] = weight_data_g;
184
185 op->load_model(ModelBinFromMatArray(weights));
186 }
187
188 op->create_pipeline(opt);
189
190 group_ops[g] = op;
191 }
192 }
193
194 if (opt.lightmode)
195 weight_data.release();
196
197 return 0;
198}
199
200int DeconvolutionDepthWise_arm::destroy_pipeline(const Option& opt)
201{
202 for (int i = 0; i < (int)group_ops.size(); i++)
203 {
204 group_ops[i]->destroy_pipeline(opt);
205 delete group_ops[i];
206 }
207 group_ops.clear();
208
209 return 0;
210}
211
212int DeconvolutionDepthWise_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
213{
214 int elembits = bottom_blob.elembits();
215
216#if NCNN_ARM82
217 if (support_fp16_storage && opt.use_fp16_storage && elembits == 16)
218 {
219 if (opt.use_fp16_arithmetic)
220 return forward_fp16sa(bottom_blob, top_blob, opt);
221 else
222 return forward_fp16s(bottom_blob, top_blob, opt);
223 }
224#endif
225
226#if NCNN_BF16
227 if (opt.use_bf16_storage && elembits == 16)
228 return forward_bf16s(bottom_blob, top_blob, opt);
229#endif
230
231 // convolv with NxN kernel
232 // value = value + bias
233
234 int w = bottom_blob.w;
235 int h = bottom_blob.h;
236 int channels = bottom_blob.c;
237 size_t elemsize = bottom_blob.elemsize;
238 int elempack = bottom_blob.elempack;
239
240 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
241 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
242
243 int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right;
244 int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom;
245 int out_elempack = 1;
246#if __ARM_NEON
247 if (opt.use_packing_layout)
248 {
249 out_elempack = num_output % 4 == 0 ? 4 : 1;
250 }
251#endif
252 size_t out_elemsize = elemsize / elempack * out_elempack;
253
254 Mat top_blob_bordered;
255 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0))
256 {
257 top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator);
258 }
259 else
260 {
261 top_blob_bordered = top_blob;
262 top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
263 }
264 if (top_blob_bordered.empty())
265 return -100;
266
267 const int maxk = kernel_w * kernel_h;
268
269 // depth-wise
270 if (channels * elempack == group && group == num_output)
271 {
272#if __ARM_NEON
273 if (elempack == 4)
274 {
275 #pragma omp parallel for num_threads(opt.num_threads)
276 for (int g = 0; g < channels; g++)
277 {
278 float* outptr = top_blob_bordered.channel(g);
279 const float* kptr = (const float*)weight_data_tm + maxk * g * 4;
280 const Mat m = bottom_blob.channel(g);
281
282 for (int i = 0; i < outh; i++)
283 {
284 for (int j = 0; j < outw; j++)
285 {
286 float32x4_t _sum = vdupq_n_f32(0.f);
287
288 if (bias_term)
289 {
290 _sum = vld1q_f32((const float*)bias_data + g * 4);
291 }
292
293 for (int y = 0; y < kernel_h; y++)
294 {
295 int sys = (i + y * dilation_h - (kernel_extent_h - 1));
296 if (sys < 0 || sys % stride_h != 0)
297 continue;
298
299 int sy = sys / stride_h;
300 if (sy >= h)
301 continue;
302
303 for (int x = 0; x < kernel_w; x++)
304 {
305 int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
306 if (sxs < 0 || sxs % stride_w != 0)
307 continue;
308
309 int sx = sxs / stride_w;
310 if (sx >= w)
311 continue;
312
313 const float* sptr = m.row(sy) + sx * 4;
314
315 float32x4_t _val = vld1q_f32(sptr);
316
317 int k = y * kernel_w + x;
318
319 float32x4_t _w = vld1q_f32(kptr + k * 4);
320
321 _sum = vmlaq_f32(_sum, _val, _w);
322 }
323 }
324
325 _sum = activation_ps(_sum, activation_type, activation_params);
326
327 vst1q_f32(outptr + j * 4, _sum);
328 }
329
330 outptr += outw * 4;
331 }
332 }
333 }
334#endif // __ARM_NEON
335
336 if (elempack == 1)
337 {
338 #pragma omp parallel for num_threads(opt.num_threads)
339 for (int g = 0; g < channels; g++)
340 {
341 float* outptr = top_blob_bordered.channel(g);
342 const float* kptr = (const float*)weight_data_tm + maxk * g;
343 const Mat m = bottom_blob.channel(g);
344
345 for (int i = 0; i < outh; i++)
346 {
347 for (int j = 0; j < outw; j++)
348 {
349 float sum = 0.f;
350
351 if (bias_term)
352 {
353 sum = bias_data[g];
354 }
355
356 for (int y = 0; y < kernel_h; y++)
357 {
358 int sys = (i + y * dilation_h - (kernel_extent_h - 1));
359 if (sys < 0 || sys % stride_h != 0)
360 continue;
361
362 int sy = sys / stride_h;
363 if (sy >= h)
364 continue;
365
366 const float* sptr = m.row(sy);
367
368 for (int x = 0; x < kernel_w; x++)
369 {
370 int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
371 if (sxs < 0 || sxs % stride_w != 0)
372 continue;
373
374 int sx = sxs / stride_w;
375 if (sx >= w)
376 continue;
377
378 float val = sptr[sx];
379
380 int k = y * kernel_w + x;
381
382 float w = kptr[k];
383
384 sum += val * w;
385 }
386 }
387
388 sum = activation_ss(sum, activation_type, activation_params);
389
390 outptr[j] = sum;
391 }
392
393 outptr += outw;
394 }
395 }
396 }
397 }
398 else
399 {
400 // group deconvolution
401 const int channels_g = channels * elempack / group;
402 const int num_output_g = num_output / group;
403
404 int g_elempack = 1;
405 int out_g_elempack = 1;
406#if __ARM_NEON
407 if (opt.use_packing_layout)
408 {
409 g_elempack = channels_g % 4 == 0 ? 4 : 1;
410 out_g_elempack = num_output_g % 4 == 0 ? 4 : 1;
411 }
412#endif
413
414 // unpacking
415 Mat bottom_blob_unpacked = bottom_blob;
416 if (elempack == 4 && g_elempack == 1)
417 {
418 Option opt_p = opt;
419 opt_p.blob_allocator = opt.workspace_allocator;
420 convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_p);
421 if (bottom_blob_unpacked.empty())
422 return -100;
423 }
424
425 Mat top_blob_bordered_unpacked = top_blob_bordered;
426 if (out_g_elempack == 1 && out_elempack == 4)
427 {
428 top_blob_bordered_unpacked.create(outw, outh, num_output, out_elemsize / out_elempack, 1, opt.workspace_allocator);
429 if (top_blob_bordered_unpacked.empty())
430 return -100;
431 }
432
433 for (int g = 0; g < group; g++)
434 {
435 const Mat bottom_blob_g = bottom_blob_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack);
436 Mat top_blob_bordered_g = top_blob_bordered_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack);
437
438 const ncnn::Layer* op = group_ops[g];
439
440 Option opt_g = opt;
441 opt_g.blob_allocator = top_blob_bordered_unpacked.allocator;
442
443 // forward
444 int ret = op->forward(bottom_blob_g, top_blob_bordered_g, opt_g);
445 if (ret != 0)
446 return ret;
447 }
448
449 // packing
450 if (out_g_elempack == 1 && out_elempack == 4)
451 {
452 convert_packing(top_blob_bordered_unpacked, top_blob_bordered, 4, opt);
453 if (top_blob_bordered.empty())
454 return -100;
455 }
456 else
457 {
458 top_blob_bordered = top_blob_bordered_unpacked;
459 }
460 }
461
462 cut_padding(top_blob_bordered, top_blob, opt);
463 if (top_blob.empty())
464 return -100;
465
466 return 0;
467}
468
469int DeconvolutionDepthWise_arm::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
470{
471 const Mat& bottom_blob = bottom_blobs[0];
472 const Mat& _weight_data = bottom_blobs[1];
473 Mat& top_blob = top_blobs[0];
474
475 const int _num_input = bottom_blob.c * bottom_blob.elempack;
476 const int _kernel_w = _weight_data.w;
477 const int _kernel_h = _weight_data.h;
478 const int _num_output = _weight_data.d * group;
479
480 Mat weight_data_flattened;
481 flatten(_weight_data, weight_data_flattened, opt);
482 if (weight_data_flattened.empty())
483 return -100;
484
485#if NCNN_ARM82
486 if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && weight_data_flattened.elembits() == 16)
487 {
488 Mat weight_data_flattened_fp32;
489 cast_float16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
490 weight_data_flattened = weight_data_flattened_fp32;
491 }
492#endif // NCNN_ARM82
493#if NCNN_BF16
494 if (opt.use_bf16_storage && weight_data_flattened.elembits() == 16)
495 {
496 Mat weight_data_flattened_fp32;
497 cast_bfloat16_to_float32(weight_data_flattened, weight_data_flattened_fp32, opt);
498 weight_data_flattened = weight_data_flattened_fp32;
499 }
500#endif // NCNN_BF16
501
502 // weight_data_flattened as pack1
503 weight_data_flattened.w *= weight_data_flattened.elempack;
504 weight_data_flattened.elemsize /= weight_data_flattened.elempack;
505 weight_data_flattened.elempack = 1;
506
507 // transpose group-inch/group-outch/group-kh-kw to group-outch/group-inch/group-kh-kw
508 Mat weight_data_transposed;
509 {
510 weight_data_transposed.create(_kernel_w * _kernel_h * _num_output * _num_input / group, 4u, opt.workspace_allocator);
511 if (weight_data_transposed.empty())
512 return -100;
513
514 const int outch_g = _num_output / group;
515 const int inch_g = _num_input / group;
516 const int maxk = _kernel_h * _kernel_w;
517
518 for (int g = 0; g < group; g++)
519 {
520 // reorder weight from inch-outch to outch-inch
521 float* wg2 = (float*)weight_data_transposed + g * outch_g * inch_g * maxk;
522 const float* wg = (const float*)weight_data_flattened + g * inch_g * outch_g * maxk;
523 for (int i = 0; i < outch_g; i++)
524 {
525 for (int j = 0; j < inch_g; j++)
526 {
527 for (int k = 0; k < maxk; k++)
528 {
529 wg2[(i * inch_g + j) * maxk + k] = wg[(j * outch_g + i) * maxk + k];
530 }
531 }
532 }
533 }
534 }
535
536 Mat bias_data_flattened;
537 if (bias_term)
538 {
539 const Mat& _bias_data = bottom_blobs[2];
540 flatten(_bias_data, bias_data_flattened, opt);
541 if (bias_data_flattened.empty())
542 return -100;
543
544#if NCNN_ARM82
545 if (opt.use_fp16_storage && cpu_support_arm_asimdhp() && bias_data_flattened.elembits() == 16)
546 {
547 Mat bias_data_flattened_fp32;
548 cast_float16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
549 bias_data_flattened = bias_data_flattened_fp32;
550 }
551#endif // NCNN_ARM82
552#if NCNN_BF16
553 if (opt.use_bf16_storage && bias_data_flattened.elembits() == 16)
554 {
555 Mat bias_data_flattened_fp32;
556 cast_bfloat16_to_float32(bias_data_flattened, bias_data_flattened_fp32, opt);
557 bias_data_flattened = bias_data_flattened_fp32;
558 }
559#endif // NCNN_BF16
560
561 // bias_data_flattened as pack1
562 bias_data_flattened.w *= bias_data_flattened.elempack;
563 bias_data_flattened.elemsize /= bias_data_flattened.elempack;
564 bias_data_flattened.elempack = 1;
565 }
566
567 ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::DeconvolutionDepthWise);
568
569 ncnn::ParamDict pd;
570 pd.set(0, _num_output);
571 pd.set(1, _kernel_w);
572 pd.set(11, _kernel_h);
573 pd.set(2, dilation_w);
574 pd.set(12, dilation_h);
575 pd.set(3, stride_w);
576 pd.set(13, stride_h);
577 pd.set(4, pad_left);
578 pd.set(15, pad_right);
579 pd.set(14, pad_top);
580 pd.set(16, pad_bottom);
581 pd.set(18, output_pad_right);
582 pd.set(19, output_pad_bottom);
583 pd.set(20, output_w);
584 pd.set(21, output_h);
585 pd.set(5, bias_term);
586 pd.set(6, weight_data_transposed.w);
587 pd.set(7, group);
588 pd.set(9, activation_type);
589 pd.set(10, activation_params);
590
591 op->load_param(pd);
592
593 ncnn::Mat weights[2];
594 weights[0] = weight_data_transposed;
595 weights[1] = bias_data_flattened;
596
597 op->load_model(ncnn::ModelBinFromMatArray(weights));
598
599 op->create_pipeline(opt);
600
601 op->forward(bottom_blob, top_blob, opt);
602
603 op->destroy_pipeline(opt);
604
605 delete op;
606
607 return 0;
608}
609
610#if NCNN_BF16
611int DeconvolutionDepthWise_arm::forward_bf16s(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
612{
613 int w = bottom_blob.w;
614 int h = bottom_blob.h;
615 int channels = bottom_blob.c;
616 size_t elemsize = bottom_blob.elemsize;
617 int elempack = bottom_blob.elempack;
618
619 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
620 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
621
622 int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right;
623 int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom;
624 int out_elempack = 1;
625#if __ARM_NEON
626 if (opt.use_packing_layout)
627 {
628 out_elempack = num_output % 4 == 0 ? 4 : 1;
629 }
630#endif
631 size_t out_elemsize = elemsize / elempack * out_elempack;
632
633 Mat top_blob_bordered;
634 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0))
635 {
636 top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator);
637 }
638 else
639 {
640 top_blob_bordered = top_blob;
641 top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
642 }
643 if (top_blob_bordered.empty())
644 return -100;
645
646 const int maxk = kernel_w * kernel_h;
647
648 // depth-wise
649 if (channels * elempack == group && group == num_output)
650 {
651#if __ARM_NEON
652 if (elempack == 4)
653 {
654 #pragma omp parallel for num_threads(opt.num_threads)
655 for (int g = 0; g < channels; g++)
656 {
657 unsigned short* outptr = top_blob_bordered.channel(g);
658 const unsigned short* kptr = (const unsigned short*)weight_data_tm + maxk * g * 4;
659 const Mat m = bottom_blob.channel(g);
660
661 for (int i = 0; i < outh; i++)
662 {
663 for (int j = 0; j < outw; j++)
664 {
665 float32x4_t _sum = vdupq_n_f32(0.f);
666
667 if (bias_term)
668 {
669 _sum = vld1q_f32((const float*)bias_data + g * 4);
670 }
671
672 for (int y = 0; y < kernel_h; y++)
673 {
674 int sys = (i + y * dilation_h - (kernel_extent_h - 1));
675 if (sys < 0 || sys % stride_h != 0)
676 continue;
677
678 int sy = sys / stride_h;
679 if (sy >= h)
680 continue;
681
682 for (int x = 0; x < kernel_w; x++)
683 {
684 int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
685 if (sxs < 0 || sxs % stride_w != 0)
686 continue;
687
688 int sx = sxs / stride_w;
689 if (sx >= w)
690 continue;
691
692 const unsigned short* sptr = m.row<const unsigned short>(sy) + sx * 4;
693
694 float32x4_t _val = bfloat2float(vld1_u16(sptr));
695
696 int k = y * kernel_w + x;
697
698 float32x4_t _w = bfloat2float(vld1_u16(kptr + k * 4));
699
700 _sum = vmlaq_f32(_sum, _val, _w);
701 }
702 }
703
704 _sum = activation_ps(_sum, activation_type, activation_params);
705
706 vst1_u16(outptr + j * 4, float2bfloat(_sum));
707 }
708
709 outptr += outw * 4;
710 }
711 }
712 }
713#endif // __ARM_NEON
714
715 if (elempack == 1)
716 {
717 #pragma omp parallel for num_threads(opt.num_threads)
718 for (int g = 0; g < channels; g++)
719 {
720 unsigned short* outptr = top_blob_bordered.channel(g);
721 const unsigned short* kptr = (const unsigned short*)weight_data_tm + maxk * g;
722 const Mat m = bottom_blob.channel(g);
723
724 for (int i = 0; i < outh; i++)
725 {
726 for (int j = 0; j < outw; j++)
727 {
728 float sum = 0.f;
729
730 if (bias_term)
731 {
732 sum = bias_data[g];
733 }
734
735 for (int y = 0; y < kernel_h; y++)
736 {
737 int sys = (i + y * dilation_h - (kernel_extent_h - 1));
738 if (sys < 0 || sys % stride_h != 0)
739 continue;
740
741 int sy = sys / stride_h;
742 if (sy >= h)
743 continue;
744
745 const unsigned short* sptr = m.row<const unsigned short>(sy);
746
747 for (int x = 0; x < kernel_w; x++)
748 {
749 int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
750 if (sxs < 0 || sxs % stride_w != 0)
751 continue;
752
753 int sx = sxs / stride_w;
754 if (sx >= w)
755 continue;
756
757 float val = bfloat16_to_float32(sptr[sx]);
758
759 int k = y * kernel_w + x;
760
761 float w = bfloat16_to_float32(kptr[k]);
762
763 sum += val * w;
764 }
765 }
766
767 sum = activation_ss(sum, activation_type, activation_params);
768
769 outptr[j] = float32_to_bfloat16(sum);
770 }
771
772 outptr += outw;
773 }
774 }
775 }
776 }
777 else
778 {
779 // group deconvolution
780 const int channels_g = channels * elempack / group;
781 const int num_output_g = num_output / group;
782
783 int g_elempack = 1;
784 int out_g_elempack = 1;
785#if __ARM_NEON
786 if (opt.use_packing_layout)
787 {
788 g_elempack = channels_g % 4 == 0 ? 4 : 1;
789 out_g_elempack = num_output_g % 4 == 0 ? 4 : 1;
790 }
791#endif
792
793 // unpacking
794 Mat bottom_blob_unpacked = bottom_blob;
795 if (elempack == 4 && g_elempack == 1)
796 {
797 Option opt_p = opt;
798 opt_p.blob_allocator = opt.workspace_allocator;
799 convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_p);
800 if (bottom_blob_unpacked.empty())
801 return -100;
802 }
803
804 Mat top_blob_bordered_unpacked = top_blob_bordered;
805 if (out_g_elempack == 1 && out_elempack == 4)
806 {
807 top_blob_bordered_unpacked.create(outw, outh, num_output, out_elemsize / out_elempack, 1, opt.workspace_allocator);
808 if (top_blob_bordered_unpacked.empty())
809 return -100;
810 }
811
812 for (int g = 0; g < group; g++)
813 {
814 const Mat bottom_blob_g = bottom_blob_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack);
815 Mat top_blob_bordered_g = top_blob_bordered_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack);
816
817 const ncnn::Layer* op = group_ops[g];
818
819 Option opt_g = opt;
820 opt_g.blob_allocator = top_blob_bordered_unpacked.allocator;
821
822 // forward
823 int ret = op->forward(bottom_blob_g, top_blob_bordered_g, opt_g);
824 if (ret != 0)
825 return ret;
826 }
827
828 // packing
829 if (out_g_elempack == 1 && out_elempack == 4)
830 {
831 convert_packing(top_blob_bordered_unpacked, top_blob_bordered, 4, opt);
832 if (top_blob_bordered.empty())
833 return -100;
834 }
835 else
836 {
837 top_blob_bordered = top_blob_bordered_unpacked;
838 }
839 }
840
841 cut_padding(top_blob_bordered, top_blob, opt);
842 if (top_blob.empty())
843 return -100;
844
845 return 0;
846}
847#endif // NCNN_BF16
848
849} // namespace ncnn