Elffuss BraidHub

kikocis/ncnn public

master · 22.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#include "deconvolutiondepthwise_x86.h"
16
17#include "layer_type.h"
18
19#if __SSE2__
20#include <emmintrin.h>
21#if __AVX__
22#include <immintrin.h>
23#endif // __AVX__
24#endif // __SSE2__
25
26#include "x86_activation.h"
27#include "x86_usability.h"
28
29namespace ncnn {
30
31DeconvolutionDepthWise_x86::DeconvolutionDepthWise_x86()
32{
33#if __SSE2__
34 support_packing = true;
35#endif // __SSE2__
36}
37
38int DeconvolutionDepthWise_x86::create_pipeline(const Option& opt)
39{
40 if (dynamic_weight)
41 return 0;
42
43 const int maxk = kernel_w * kernel_h;
44 int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
45
46 // depth-wise
47 if (channels == group && group == num_output)
48 {
49 int elempack = 1;
50#if __SSE2__
51 if (opt.use_packing_layout)
52 {
53#if __AVX512F__
54 elempack = channels % 16 == 0 ? 16 : channels % 8 == 0 ? 8 : channels % 4 == 0 ? 4 : 1;
55#elif __AVX__
56 elempack = channels % 8 == 0 ? 8 : channels % 4 == 0 ? 4 : 1;
57#else
58 elempack = channels % 4 == 0 ? 4 : 1;
59#endif
60 }
61#endif // __SSE2__
62
63 Mat weight_data_transposed(weight_data.w);
64 {
65 float* pt = weight_data_transposed;
66 const float* p = weight_data;
67
68 for (int i = 0; i < (channels / group) * (num_output / group) * group; i++)
69 {
70 for (int k = 0; k < maxk; k++)
71 {
72 pt[maxk - 1 - k] = p[k];
73 }
74
75 p += maxk;
76 pt += maxk;
77 }
78 }
79
80#if __SSE2__
81#if __AVX__
82 // pack16
83#if __AVX512F__
84 if (elempack == 16)
85 {
86 Mat weight_data_r2 = weight_data_transposed.reshape(maxk, group);
87 convert_packing(weight_data_r2, weight_data_tm, 16, opt);
88 }
89#endif // __AVX512F__
90
91 // pack8
92 if (elempack == 8)
93 {
94 Mat weight_data_r2 = weight_data_transposed.reshape(maxk, group);
95 convert_packing(weight_data_r2, weight_data_tm, 8, opt);
96 }
97#endif // __AVX__
98
99 // pack4
100 if (elempack == 4)
101 {
102 Mat weight_data_r2 = weight_data_transposed.reshape(maxk, group);
103 convert_packing(weight_data_r2, weight_data_tm, 4, opt);
104 }
105#endif // __SSE2__
106
107 if (elempack == 1)
108 {
109 weight_data_tm = weight_data_transposed;
110 }
111
112 if (opt.lightmode)
113 weight_data.release();
114
115 return 0;
116 }
117
118 // group convolution
119 create_group_ops(opt);
120
121 if (opt.lightmode)
122 weight_data.release();
123
124 return 0;
125}
126
127int DeconvolutionDepthWise_x86::create_group_ops(const Option& opt)
128{
129 // create Deconvolution op for each group
130 const int maxk = kernel_w * kernel_h;
131 int channels = (weight_data_size / group) / maxk / (num_output / group) * group;
132
133 for (int i = 0; i < (int)group_ops.size(); i++)
134 delete group_ops[i];
135
136 group_ops.clear();
137
138 const int channels_g = channels / group;
139 const int num_output_g = num_output / group;
140
141 group_ops.resize(group);
142
143 for (int g = 0; g < group; g++)
144 {
145 Mat weight_data_g = weight_data.range(maxk * channels_g * num_output_g * g, maxk * channels_g * num_output_g).clone();
146 Mat bias_data_g;
147 if (bias_term)
148 bias_data_g = bias_data.range(num_output_g * g, num_output_g);
149
150 ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::Deconvolution);
151
152 // set param
153 ncnn::ParamDict pd;
154 pd.set(0, num_output_g); // num_output
155 pd.set(1, kernel_w);
156 pd.set(11, kernel_h);
157 pd.set(2, dilation_w);
158 pd.set(12, dilation_h);
159 pd.set(3, stride_w);
160 pd.set(13, stride_h);
161 pd.set(4, 0); // pad_w
162 pd.set(14, 0); // pad_h
163 pd.set(18, output_pad_right);
164 pd.set(19, output_pad_bottom);
165 pd.set(5, bias_term);
166 pd.set(6, maxk * channels_g * num_output_g); // weight_data_size
167 pd.set(9, activation_type);
168 pd.set(10, activation_params);
169
170 op->load_param(pd);
171
172 // set weights
173 if (bias_term)
174 {
175 ncnn::Mat weights[2];
176 weights[0] = weight_data_g;
177 weights[1] = bias_data_g;
178
179 op->load_model(ModelBinFromMatArray(weights));
180 }
181 else
182 {
183 ncnn::Mat weights[1];
184 weights[0] = weight_data_g;
185
186 op->load_model(ModelBinFromMatArray(weights));
187 }
188
189 op->create_pipeline(opt);
190
191 group_ops[g] = op;
192 }
193
194 return 0;
195}
196
197int DeconvolutionDepthWise_x86::destroy_pipeline(const Option& opt)
198{
199 for (int i = 0; i < (int)group_ops.size(); i++)
200 {
201 group_ops[i]->destroy_pipeline(opt);
202 delete group_ops[i];
203 }
204 group_ops.clear();
205
206 return 0;
207}
208
209int DeconvolutionDepthWise_x86::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
210{
211 // convolv with NxN kernel
212 // value = value + bias
213
214 int w = bottom_blob.w;
215 int h = bottom_blob.h;
216 int channels = bottom_blob.c;
217 size_t elemsize = bottom_blob.elemsize;
218 int elempack = bottom_blob.elempack;
219
220 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
221 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
222
223 int outw = (w - 1) * stride_w + kernel_extent_w + output_pad_right;
224 int outh = (h - 1) * stride_h + kernel_extent_h + output_pad_bottom;
225 int out_elempack = 1;
226#if __SSE2__
227 if (opt.use_packing_layout)
228 {
229#if __AVX512F__
230 out_elempack = num_output % 16 == 0 ? 16 : num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1;
231#elif __AVX__
232 out_elempack = num_output % 8 == 0 ? 8 : num_output % 4 == 0 ? 4 : 1;
233#else
234 out_elempack = num_output % 4 == 0 ? 4 : 1;
235#endif
236 }
237#endif // __SSE2__
238 size_t out_elemsize = elemsize / elempack * out_elempack;
239
240 Mat top_blob_bordered;
241 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0 || (output_w > 0 && output_h > 0))
242 {
243 top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.workspace_allocator);
244 }
245 else
246 {
247 top_blob_bordered = top_blob;
248 top_blob_bordered.create(outw, outh, num_output / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
249 }
250 if (top_blob_bordered.empty())
251 return -100;
252
253 const int maxk = kernel_w * kernel_h;
254
255 // depth-wise
256 if (channels * elempack == group && group == num_output)
257 {
258#if __SSE2__
259#if __AVX__
260#if __AVX512F__
261 if (elempack == 16)
262 {
263 {
264 #pragma omp parallel for num_threads(opt.num_threads)
265 for (int g = 0; g < channels; g++)
266 {
267 float* outptr = top_blob_bordered.channel(g);
268 const float* kptr = (const float*)weight_data_tm + maxk * g * 16;
269 const Mat m = bottom_blob.channel(g);
270
271 for (int i = 0; i < outh; i++)
272 {
273 for (int j = 0; j < outw; j++)
274 {
275 __m512 _sum = _mm512_setzero_ps();
276
277 if (bias_term)
278 {
279 _sum = _mm512_loadu_ps((const float*)bias_data + g * 16);
280 }
281
282 for (int y = 0; y < kernel_h; y++)
283 {
284 int sys = (i + y * dilation_h - (kernel_extent_h - 1));
285 if (sys < 0 || sys % stride_h != 0)
286 continue;
287
288 int sy = sys / stride_h;
289 if (sy >= h)
290 continue;
291
292 for (int x = 0; x < kernel_w; x++)
293 {
294 int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
295 if (sxs < 0 || sxs % stride_w != 0)
296 continue;
297
298 int sx = sxs / stride_w;
299 if (sx >= w)
300 continue;
301
302 const float* sptr = m.row(sy) + sx * 16;
303
304 int k = y * kernel_w + x;
305
306 __m512 _val = _mm512_loadu_ps(sptr);
307 __m512 _w = _mm512_loadu_ps(kptr + k * 16);
308 _sum = _mm512_fmadd_ps(_val, _w, _sum);
309 }
310 }
311
312 _sum = activation_avx512(_sum, activation_type, activation_params);
313
314 _mm512_storeu_ps(outptr, _sum);
315 outptr += 16;
316 }
317 }
318 }
319 }
320 }
321#endif // __AVX512F__
322
323 if (elempack == 8)
324 {
325 {
326 #pragma omp parallel for num_threads(opt.num_threads)
327 for (int g = 0; g < channels; g++)
328 {
329 float* outptr = top_blob_bordered.channel(g);
330 const float* kptr = (const float*)weight_data_tm + maxk * g * 8;
331 const Mat m = bottom_blob.channel(g);
332
333 for (int i = 0; i < outh; i++)
334 {
335 for (int j = 0; j < outw; j++)
336 {
337 __m256 _sum = _mm256_setzero_ps();
338
339 if (bias_term)
340 {
341 _sum = _mm256_loadu_ps((const float*)bias_data + g * 8);
342 }
343
344 for (int y = 0; y < kernel_h; y++)
345 {
346 int sys = (i + y * dilation_h - (kernel_extent_h - 1));
347 if (sys < 0 || sys % stride_h != 0)
348 continue;
349
350 int sy = sys / stride_h;
351 if (sy >= h)
352 continue;
353
354 for (int x = 0; x < kernel_w; x++)
355 {
356 int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
357 if (sxs < 0 || sxs % stride_w != 0)
358 continue;
359
360 int sx = sxs / stride_w;
361 if (sx >= w)
362 continue;
363
364 const float* sptr = m.row(sy) + sx * 8;
365
366 int k = y * kernel_w + x;
367
368 __m256 _val = _mm256_loadu_ps(sptr);
369 __m256 _w = _mm256_loadu_ps(kptr + k * 8);
370 _sum = _mm256_comp_fmadd_ps(_val, _w, _sum);
371 }
372 }
373
374 _sum = activation_avx(_sum, activation_type, activation_params);
375
376 _mm256_storeu_ps(outptr, _sum);
377 outptr += 8;
378 }
379 }
380 }
381 }
382 }
383#endif // __AVX__
384
385 if (elempack == 4)
386 {
387 {
388 #pragma omp parallel for num_threads(opt.num_threads)
389 for (int g = 0; g < channels; g++)
390 {
391 float* outptr = top_blob_bordered.channel(g);
392 const float* kptr = (const float*)weight_data_tm + maxk * g * 4;
393 const Mat m = bottom_blob.channel(g);
394
395 for (int i = 0; i < outh; i++)
396 {
397 for (int j = 0; j < outw; j++)
398 {
399 __m128 _sum = _mm_setzero_ps();
400
401 if (bias_term)
402 {
403 _sum = _mm_loadu_ps((const float*)bias_data + g * 4);
404 }
405
406 for (int y = 0; y < kernel_h; y++)
407 {
408 int sys = (i + y * dilation_h - (kernel_extent_h - 1));
409 if (sys < 0 || sys % stride_h != 0)
410 continue;
411
412 int sy = sys / stride_h;
413 if (sy >= h)
414 continue;
415
416 for (int x = 0; x < kernel_w; x++)
417 {
418 int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
419 if (sxs < 0 || sxs % stride_w != 0)
420 continue;
421
422 int sx = sxs / stride_w;
423 if (sx >= w)
424 continue;
425
426 const float* sptr = m.row(sy) + sx * 4;
427
428 int k = y * kernel_w + x;
429
430 __m128 _val = _mm_loadu_ps(sptr);
431 __m128 _w = _mm_loadu_ps(kptr + k * 4);
432 _sum = _mm_comp_fmadd_ps(_val, _w, _sum);
433 }
434 }
435
436 _sum = activation_sse(_sum, activation_type, activation_params);
437
438 _mm_storeu_ps(outptr, _sum);
439 outptr += 4;
440 }
441 }
442 }
443 }
444 }
445#endif // __SSE2__
446
447 if (elempack == 1)
448 {
449 #pragma omp parallel for num_threads(opt.num_threads)
450 for (int g = 0; g < channels; g++)
451 {
452 float* outptr = top_blob_bordered.channel(g);
453 const float* kptr = (const float*)weight_data_tm + maxk * g;
454 const Mat m = bottom_blob.channel(g);
455
456 for (int i = 0; i < outh; i++)
457 {
458 for (int j = 0; j < outw; j++)
459 {
460 float sum = 0.f;
461
462 if (bias_term)
463 {
464 sum = bias_data[g];
465 }
466
467 for (int y = 0; y < kernel_h; y++)
468 {
469 int sys = (i + y * dilation_h - (kernel_extent_h - 1));
470 if (sys < 0 || sys % stride_h != 0)
471 continue;
472
473 int sy = sys / stride_h;
474 if (sy >= h)
475 continue;
476
477 const float* sptr = m.row(sy);
478
479 for (int x = 0; x < kernel_w; x++)
480 {
481 int sxs = (j + x * dilation_w - (kernel_extent_w - 1));
482 if (sxs < 0 || sxs % stride_w != 0)
483 continue;
484
485 int sx = sxs / stride_w;
486 if (sx >= w)
487 continue;
488
489 float val = sptr[sx];
490
491 int k = y * kernel_w + x;
492
493 float w = kptr[k];
494
495 sum += val * w;
496 }
497 }
498
499 sum = activation_ss(sum, activation_type, activation_params);
500
501 outptr[0] = sum;
502 outptr++;
503 }
504 }
505 }
506 }
507 }
508 else
509 {
510 // group deconvolution
511 const int channels_g = channels * elempack / group;
512 const int num_output_g = num_output / group;
513
514 int g_elempack = 1;
515 int out_g_elempack = 1;
516#if __SSE2__
517 if (opt.use_packing_layout)
518 {
519#if __AVX512F__
520 g_elempack = channels_g % 16 == 0 ? 16 : channels_g % 8 == 0 ? 8 : channels_g % 4 == 0 ? 4 : 1;
521 out_g_elempack = num_output_g % 16 == 0 ? 16 : num_output_g % 8 == 0 ? 8 : num_output_g % 4 == 0 ? 4 : 1;
522#elif __AVX__
523 g_elempack = channels_g % 8 == 0 ? 8 : channels_g % 4 == 0 ? 4 : 1;
524 out_g_elempack = num_output_g % 8 == 0 ? 8 : num_output_g % 4 == 0 ? 4 : 1;
525#else
526 g_elempack = channels_g % 4 == 0 ? 4 : 1;
527 out_g_elempack = num_output_g % 4 == 0 ? 4 : 1;
528#endif
529 }
530#endif // __SSE2__
531
532 // unpacking
533 Mat bottom_blob_unpacked = bottom_blob;
534 if (elempack > g_elempack)
535 {
536 Option opt_p = opt;
537 opt_p.blob_allocator = opt.workspace_allocator;
538 convert_packing(bottom_blob, bottom_blob_unpacked, g_elempack, opt_p);
539 if (bottom_blob_unpacked.empty())
540 return -100;
541 }
542
543 Mat top_blob_bordered_unpacked = top_blob_bordered;
544 if (out_g_elempack < out_elempack)
545 {
546 top_blob_bordered_unpacked.create(outw, outh, num_output / out_g_elempack, out_elemsize / out_elempack * out_g_elempack, out_g_elempack, opt.workspace_allocator);
547 if (top_blob_bordered_unpacked.empty())
548 return -100;
549 }
550
551 for (int g = 0; g < group; g++)
552 {
553 const Mat bottom_blob_g = bottom_blob_unpacked.channel_range(channels_g * g / g_elempack, channels_g / g_elempack);
554 Mat top_blob_bordered_g = top_blob_bordered_unpacked.channel_range(num_output_g * g / out_g_elempack, num_output_g / out_g_elempack);
555
556 const ncnn::Layer* op = group_ops[g];
557
558 Option opt_g = opt;
559 opt_g.blob_allocator = top_blob_bordered_unpacked.allocator;
560
561 // forward
562 int ret = op->forward(bottom_blob_g, top_blob_bordered_g, opt_g);
563 if (ret != 0)
564 return ret;
565 }
566
567 // packing
568 if (out_g_elempack < out_elempack)
569 {
570 convert_packing(top_blob_bordered_unpacked, top_blob_bordered, out_elempack, opt);
571 if (top_blob_bordered.empty())
572 return -100;
573 }
574 else
575 {
576 top_blob_bordered = top_blob_bordered_unpacked;
577 }
578 }
579
580 cut_padding(top_blob_bordered, top_blob, opt);
581 if (top_blob.empty())
582 return -100;
583
584 return 0;
585}
586
587int DeconvolutionDepthWise_x86::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const
588{
589 const Mat& bottom_blob = bottom_blobs[0];
590 const Mat& _weight_data = bottom_blobs[1];
591 Mat& top_blob = top_blobs[0];
592
593 const int _num_input = bottom_blob.c * bottom_blob.elempack;
594 const int _kernel_w = _weight_data.w;
595 const int _kernel_h = _weight_data.h;
596 const int _num_output = _weight_data.d * group;
597
598 Mat weight_data_flattened;
599 flatten(_weight_data, weight_data_flattened, opt);
600 if (weight_data_flattened.empty())
601 return -100;
602
603 // weight_data_flattened as pack1
604 weight_data_flattened.w *= weight_data_flattened.elempack;
605 weight_data_flattened.elemsize /= weight_data_flattened.elempack;
606 weight_data_flattened.elempack = 1;
607
608 // transpose group-inch/group-outch/group-kh-kw to group-outch/group-inch/group-kh-kw
609 Mat weight_data_transposed;
610 {
611 weight_data_transposed.create(_kernel_w * _kernel_h * _num_output * _num_input / group, 4u, opt.workspace_allocator);
612 if (weight_data_transposed.empty())
613 return -100;
614
615 const int outch_g = _num_output / group;
616 const int inch_g = _num_input / group;
617 const int maxk = _kernel_h * _kernel_w;
618
619 for (int g = 0; g < group; g++)
620 {
621 // reorder weight from inch-outch to outch-inch
622 float* wg2 = (float*)weight_data_transposed + g * outch_g * inch_g * maxk;
623 const float* wg = (const float*)weight_data_flattened + g * inch_g * outch_g * maxk;
624 for (int i = 0; i < outch_g; i++)
625 {
626 for (int j = 0; j < inch_g; j++)
627 {
628 for (int k = 0; k < maxk; k++)
629 {
630 wg2[(i * inch_g + j) * maxk + k] = wg[(j * outch_g + i) * maxk + k];
631 }
632 }
633 }
634 }
635 }
636
637 Mat bias_data_flattened;
638 if (bias_term)
639 {
640 const Mat& _bias_data = bottom_blobs[2];
641 flatten(_bias_data, bias_data_flattened, opt);
642 if (bias_data_flattened.empty())
643 return -100;
644
645 // bias_data_flattened as pack1
646 bias_data_flattened.w *= bias_data_flattened.elempack;
647 bias_data_flattened.elemsize /= bias_data_flattened.elempack;
648 bias_data_flattened.elempack = 1;
649 }
650
651 ncnn::Layer* op = ncnn::create_layer_cpu(ncnn::LayerType::DeconvolutionDepthWise);
652
653 ncnn::ParamDict pd;
654 pd.set(0, _num_output);
655 pd.set(1, _kernel_w);
656 pd.set(11, _kernel_h);
657 pd.set(2, dilation_w);
658 pd.set(12, dilation_h);
659 pd.set(3, stride_w);
660 pd.set(13, stride_h);
661 pd.set(4, pad_left);
662 pd.set(15, pad_right);
663 pd.set(14, pad_top);
664 pd.set(16, pad_bottom);
665 pd.set(18, output_pad_right);
666 pd.set(19, output_pad_bottom);
667 pd.set(20, output_w);
668 pd.set(21, output_h);
669 pd.set(5, bias_term);
670 pd.set(6, weight_data_transposed.w);
671 pd.set(7, group);
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_transposed;
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} // namespace ncnn