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) 2019 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 "padding_x86.h"
16
17#include <stdint.h>
18#if __SSE2__
19#include <emmintrin.h>
20#if __AVX__
21#include <immintrin.h>
22#endif // __AVX__
23#endif // __SSE2__
24
25namespace ncnn {
26
27#if __SSE2__
28#include "padding_pack4.h"
29#include "padding_pack8_int8.h"
30#if __AVX__
31#include "padding_pack8.h"
32#if __AVX512F__
33#include "padding_pack16.h"
34#endif // __AVX512F__
35#endif // __AVX__
36#endif // __SSE2__
37
38Padding_x86::Padding_x86()
39{
40#if __SSE2__
41 support_packing = true;
42#endif // __SSE2__
43}
44
45int Padding_x86::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
46{
47 if (top == 0 && bottom == 0 && left == 0 && right == 0 && front == 0 && behind == 0)
48 {
49 top_blob = bottom_blob;
50 return 0;
51 }
52
53 int elembits = bottom_blob.elembits();
54
55 if (elembits == 8)
56 return forward_int8(bottom_blob, top_blob, opt);
57
58 int w = bottom_blob.w;
59 int h = bottom_blob.h;
60 int d = bottom_blob.d;
61 int channels = bottom_blob.c;
62 int dims = bottom_blob.dims;
63 size_t elemsize = bottom_blob.elemsize;
64 int elempack = bottom_blob.elempack;
65
66#if __SSE2__
67#if __AVX__
68#if __AVX512F__
69 if (elempack == 16)
70 {
71 if (dims == 1)
72 {
73 int outw = w * elempack + left + right;
74
75 int out_elempack = outw % 16 == 0 ? 16 : outw % 8 == 0 ? 8 : outw % 4 == 0 ? 4 : 1;
76 size_t out_elemsize = elemsize / elempack * out_elempack;
77
78 if (left % 16 == 0 && out_elempack == 16 && type == 0)
79 {
80 top_blob.create(outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
81 if (top_blob.empty())
82 return -100;
83
84 __m512 pad_value = _mm512_set1_ps(value);
85 padding_constant_pack16_avx512(bottom_blob, top_blob, 0, 0, left / 16, right / 16, pad_value);
86
87 return 0;
88 }
89 }
90
91 if (dims == 2)
92 {
93 int outw = w + left + right;
94 int outh = h * elempack + top + bottom;
95
96 int out_elempack = outh % 16 == 0 ? 16 : outh % 8 == 0 ? 8 : outh % 4 == 0 ? 4 : 1;
97 size_t out_elemsize = elemsize / elempack * out_elempack;
98
99 if (top % 16 == 0 && out_elempack == 16 && type == 0)
100 {
101 top_blob.create(outw, outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
102 if (top_blob.empty())
103 return -100;
104
105 __m512 pad_value = _mm512_set1_ps(value);
106 padding_constant_pack16_avx512(bottom_blob, top_blob, top / 16, bottom / 16, left, right, pad_value);
107
108 return 0;
109 }
110 }
111
112 if (dims == 3)
113 {
114 int outw = w + left + right;
115 int outh = h + top + bottom;
116 int outc = channels * elempack + front + behind;
117
118 int out_elempack = outc % 16 == 0 ? 16 : outc % 8 == 0 ? 8 : outc % 4 == 0 ? 4 : 1;
119 size_t out_elemsize = elemsize / elempack * out_elempack;
120
121 if (front % 16 == 0 && out_elempack == 16 && !(outc != channels * elempack && type != 0))
122 {
123 top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
124 if (top_blob.empty())
125 return -100;
126
127 int front_ = front / elempack;
128 #pragma omp parallel for num_threads(opt.num_threads)
129 for (int q = 0; q < outc / out_elempack; q++)
130 {
131 Mat borderm = top_blob.channel(q);
132
133 __m512 pad_value = per_channel_pad_data_size ? _mm512_loadu_ps((const float*)per_channel_pad_data + q * 16) : _mm512_set1_ps(value);
134 //Channel padding
135 if ((q - front_) < 0 || (q - front_) >= channels)
136 {
137 borderm.fill(pad_value);
138 }
139 else
140 {
141 const Mat m = bottom_blob.channel(q - front_);
142 if (type == 0)
143 padding_constant_pack16_avx512(m, borderm, top, bottom, left, right, pad_value);
144 if (type == 1)
145 padding_replicate_pack16_avx512(m, borderm, top, bottom, left, right);
146 if (type == 2)
147 padding_reflect_pack16_avx512(m, borderm, top, bottom, left, right);
148 }
149 }
150
151 return 0;
152 }
153 }
154
155 if (dims == 4)
156 {
157 int outw = w + left + right;
158 int outh = h + top + bottom;
159 int outd = d + front + behind;
160
161 if (type == 0)
162 {
163 top_blob.create(outw, outh, outd, channels, elemsize, elempack, opt.blob_allocator);
164 if (top_blob.empty())
165 return -100;
166
167 #pragma omp parallel for num_threads(opt.num_threads)
168 for (int q = 0; q < channels; q++)
169 {
170 __m512 pad_value = per_channel_pad_data_size ? _mm512_loadu_ps((const float*)per_channel_pad_data + q * 16) : _mm512_set1_ps(value);
171
172 for (int z = 0; z < outd; z++)
173 {
174 Mat borderm = top_blob.channel(q).depth(z);
175
176 // depth padding
177 if ((z - front) < 0 || (z - front) >= d)
178 {
179 borderm.fill(pad_value);
180 }
181 else
182 {
183 const Mat m = bottom_blob.channel(q).depth(z - front);
184 padding_constant_pack16_avx512(m, borderm, top, bottom, left, right, pad_value);
185 }
186 }
187 }
188
189 return 0;
190 }
191 }
192 }
193#endif // __AVX512F__
194
195 if (elempack == 8)
196 {
197 if (dims == 1)
198 {
199 int outw = w * elempack + left + right;
200
201 int out_elempack = outw % 8 == 0 ? 8 : outw % 4 == 0 ? 4 : 1;
202 size_t out_elemsize = elemsize / elempack * out_elempack;
203
204 if (left % 8 == 0 && out_elempack == 8 && type == 0)
205 {
206 top_blob.create(outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
207 if (top_blob.empty())
208 return -100;
209
210 __m256 pad_value = _mm256_set1_ps(value);
211 padding_constant_pack8_avx(bottom_blob, top_blob, 0, 0, left / 8, right / 8, pad_value);
212
213 return 0;
214 }
215 }
216
217 if (dims == 2)
218 {
219 int outw = w + left + right;
220 int outh = h * elempack + top + bottom;
221
222 int out_elempack = outh % 8 == 0 ? 8 : outh % 4 == 0 ? 4 : 1;
223 size_t out_elemsize = elemsize / elempack * out_elempack;
224
225 if (top % 8 == 0 && out_elempack == 8 && type == 0)
226 {
227 top_blob.create(outw, outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
228 if (top_blob.empty())
229 return -100;
230
231 __m256 pad_value = _mm256_set1_ps(value);
232 padding_constant_pack8_avx(bottom_blob, top_blob, top / 8, bottom / 8, left, right, pad_value);
233
234 return 0;
235 }
236 }
237
238 if (dims == 3)
239 {
240 int outw = w + left + right;
241 int outh = h + top + bottom;
242 int outc = channels * elempack + front + behind;
243
244 int out_elempack = outc % 8 == 0 ? 8 : outc % 4 == 0 ? 4 : 1;
245 size_t out_elemsize = elemsize / elempack * out_elempack;
246
247 if (front % 8 == 0 && out_elempack == 8 && !(outc != channels * elempack && type != 0))
248 {
249 top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
250 if (top_blob.empty())
251 return -100;
252
253 int front_ = front / elempack;
254 #pragma omp parallel for num_threads(opt.num_threads)
255 for (int q = 0; q < outc / out_elempack; q++)
256 {
257 Mat borderm = top_blob.channel(q);
258
259 __m256 pad_value = per_channel_pad_data_size ? _mm256_loadu_ps((const float*)per_channel_pad_data + q * 8) : _mm256_set1_ps(value);
260 //Channel padding
261 if ((q - front_) < 0 || (q - front_) >= channels)
262 {
263 borderm.fill(pad_value);
264 }
265 else
266 {
267 const Mat m = bottom_blob.channel(q - front_);
268 if (type == 0)
269 padding_constant_pack8_avx(m, borderm, top, bottom, left, right, pad_value);
270 if (type == 1)
271 padding_replicate_pack8_avx(m, borderm, top, bottom, left, right);
272 if (type == 2)
273 padding_reflect_pack8_avx(m, borderm, top, bottom, left, right);
274 }
275 }
276
277 return 0;
278 }
279 }
280
281 if (dims == 4)
282 {
283 int outw = w + left + right;
284 int outh = h + top + bottom;
285 int outd = d + front + behind;
286
287 if (type == 0)
288 {
289 top_blob.create(outw, outh, outd, channels, elemsize, elempack, opt.blob_allocator);
290 if (top_blob.empty())
291 return -100;
292
293 #pragma omp parallel for num_threads(opt.num_threads)
294 for (int q = 0; q < channels; q++)
295 {
296 __m256 pad_value = per_channel_pad_data_size ? _mm256_loadu_ps((const float*)per_channel_pad_data + q * 8) : _mm256_set1_ps(value);
297
298 for (int z = 0; z < outd; z++)
299 {
300 Mat borderm = top_blob.channel(q).depth(z);
301
302 // depth padding
303 if ((z - front) < 0 || (z - front) >= d)
304 {
305 borderm.fill(pad_value);
306 }
307 else
308 {
309 const Mat m = bottom_blob.channel(q).depth(z - front);
310 padding_constant_pack8_avx(m, borderm, top, bottom, left, right, pad_value);
311 }
312 }
313 }
314
315 return 0;
316 }
317 }
318 }
319#endif // __AVX__
320
321 if (elempack == 4)
322 {
323 if (dims == 1)
324 {
325 int outw = w * elempack + left + right;
326
327#if __AVX__
328 int out_elempack = outw % 8 == 0 ? 8 : outw % 4 == 0 ? 4 : 1;
329#else
330 int out_elempack = outw % 4 == 0 ? 4 : 1;
331#endif
332 size_t out_elemsize = elemsize / elempack * out_elempack;
333
334 if (left % 4 == 0 && out_elempack == 4 && type == 0)
335 {
336 top_blob.create(outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
337 if (top_blob.empty())
338 return -100;
339
340 __m128 pad_value = _mm_set1_ps(value);
341 padding_constant_pack4_sse(bottom_blob, top_blob, 0, 0, left / 4, right / 4, pad_value);
342
343 return 0;
344 }
345 }
346
347 if (dims == 2)
348 {
349 int outw = w + left + right;
350 int outh = h * elempack + top + bottom;
351
352#if __AVX__
353 int out_elempack = outh % 8 == 0 ? 8 : outh % 4 == 0 ? 4 : 1;
354#else
355 int out_elempack = outh % 4 == 0 ? 4 : 1;
356#endif
357 size_t out_elemsize = elemsize / elempack * out_elempack;
358
359 if (top % 4 == 0 && out_elempack == 4 && type == 0)
360 {
361 top_blob.create(outw, outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
362 if (top_blob.empty())
363 return -100;
364
365 __m128 pad_value = _mm_set1_ps(value);
366 padding_constant_pack4_sse(bottom_blob, top_blob, top / 4, bottom / 4, left, right, pad_value);
367
368 return 0;
369 }
370 }
371
372 if (dims == 3)
373 {
374 int outw = w + left + right;
375 int outh = h + top + bottom;
376 int outc = channels * elempack + front + behind;
377
378#if __AVX__
379 int out_elempack = outc % 8 == 0 ? 8 : outc % 4 == 0 ? 4 : 1;
380#else
381 int out_elempack = outc % 4 == 0 ? 4 : 1;
382#endif
383 size_t out_elemsize = elemsize / elempack * out_elempack;
384
385 if (front % 4 == 0 && out_elempack == 4 && !(outc != channels * elempack && type != 0))
386 {
387 top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
388 if (top_blob.empty())
389 return -100;
390
391 int front_ = front / elempack;
392 #pragma omp parallel for num_threads(opt.num_threads)
393 for (int q = 0; q < outc / out_elempack; q++)
394 {
395 Mat borderm = top_blob.channel(q);
396
397 __m128 pad_value = per_channel_pad_data_size ? _mm_loadu_ps((const float*)per_channel_pad_data + q * 4) : _mm_set1_ps(value);
398 //Channel padding
399 if ((q - front_) < 0 || (q - front_) >= channels)
400 {
401 borderm.fill(pad_value);
402 }
403 else
404 {
405 const Mat m = bottom_blob.channel(q - front_);
406 if (type == 0)
407 padding_constant_pack4_sse(m, borderm, top, bottom, left, right, pad_value);
408 if (type == 1)
409 padding_replicate_pack4_sse(m, borderm, top, bottom, left, right);
410 if (type == 2)
411 padding_reflect_pack4_sse(m, borderm, top, bottom, left, right);
412 }
413 }
414
415 return 0;
416 }
417 }
418
419 if (dims == 4)
420 {
421 int outw = w + left + right;
422 int outh = h + top + bottom;
423 int outd = d + front + behind;
424
425 if (type == 0)
426 {
427 top_blob.create(outw, outh, outd, channels, elemsize, elempack, opt.blob_allocator);
428 if (top_blob.empty())
429 return -100;
430
431 #pragma omp parallel for num_threads(opt.num_threads)
432 for (int q = 0; q < channels; q++)
433 {
434 __m128 pad_value = per_channel_pad_data_size ? _mm_loadu_ps((const float*)per_channel_pad_data + q * 4) : _mm_set1_ps(value);
435
436 for (int z = 0; z < outd; z++)
437 {
438 Mat borderm = top_blob.channel(q).depth(z);
439
440 // depth padding
441 if ((z - front) < 0 || (z - front) >= d)
442 {
443 borderm.fill(pad_value);
444 }
445 else
446 {
447 const Mat m = bottom_blob.channel(q).depth(z - front);
448 padding_constant_pack4_sse(m, borderm, top, bottom, left, right, pad_value);
449 }
450 }
451 }
452
453 return 0;
454 }
455 }
456 }
457#endif // __SSE2__
458
459 Mat bottom_blob_unpacked = bottom_blob;
460 if (elempack != 1)
461 {
462 Option opt_pack1 = opt;
463 opt_pack1.blob_allocator = opt.workspace_allocator;
464
465 convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
466 if (bottom_blob_unpacked.empty())
467 return -100;
468 }
469
470 return Padding::forward(bottom_blob_unpacked, top_blob, opt);
471}
472
473int Padding_x86::forward_int8(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
474{
475 int w = bottom_blob.w;
476 int h = bottom_blob.h;
477 int d = bottom_blob.d;
478 int channels = bottom_blob.c;
479 int dims = bottom_blob.dims;
480 size_t elemsize = bottom_blob.elemsize;
481 int elempack = bottom_blob.elempack;
482
483#if __SSE2__
484 if (elempack == 8)
485 {
486 if (dims == 1)
487 {
488 int outw = w * elempack + left + right;
489
490 int out_elempack = outw % 8 == 0 ? 8 : 1;
491 size_t out_elemsize = elemsize / elempack * out_elempack;
492
493 if (left % 8 == 0 && out_elempack == 8 && type == 0)
494 {
495 top_blob.create(outw / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
496 if (top_blob.empty())
497 return -100;
498
499 int64_t v8 = (int64_t)value;
500 int64_t pad_value = v8 | (v8 << 8) | (v8 << 16) | (v8 << 24) | (v8 << 32) | (v8 << 40) | (v8 << 48) | (v8 << 56);
501 padding_constant_pack8_int8_sse(bottom_blob, top_blob, 0, 0, left / 8, right / 8, pad_value);
502
503 return 0;
504 }
505 }
506
507 if (dims == 2)
508 {
509 int outw = w + left + right;
510 int outh = h * elempack + top + bottom;
511
512 int out_elempack = outh % 8 == 0 ? 8 : 1;
513 size_t out_elemsize = elemsize / elempack * out_elempack;
514
515 if (top % 8 == 0 && out_elempack == 8 && type == 0)
516 {
517 top_blob.create(outw, outh / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
518 if (top_blob.empty())
519 return -100;
520
521 int64_t v8 = (int64_t)value;
522 int64_t pad_value = v8 | (v8 << 8) | (v8 << 16) | (v8 << 24) | (v8 << 32) | (v8 << 40) | (v8 << 48) | (v8 << 56);
523 padding_constant_pack8_int8_sse(bottom_blob, top_blob, top / 8, bottom / 8, left, right, pad_value);
524
525 return 0;
526 }
527 }
528
529 if (dims == 3)
530 {
531 int outw = w + left + right;
532 int outh = h + top + bottom;
533 int outc = channels * elempack + front + behind;
534
535 int out_elempack = outc % 8 == 0 ? 8 : 1;
536 size_t out_elemsize = elemsize / elempack * out_elempack;
537
538 if (front % 8 == 0 && out_elempack == 8 && !(outc != channels * elempack && type != 0))
539 {
540 top_blob.create(outw, outh, outc / out_elempack, out_elemsize, out_elempack, opt.blob_allocator);
541 if (top_blob.empty())
542 return -100;
543
544 int front_ = front / elempack;
545 #pragma omp parallel for num_threads(opt.num_threads)
546 for (int q = 0; q < outc / out_elempack; q++)
547 {
548 Mat borderm = top_blob.channel(q);
549
550 // TODO perchannel
551 // int64_t pad_value = per_channel_pad_data_size ? vld1_s8(per_channel_pad_data + q * 8) : vdup_n_s8((signed char)value);
552 int64_t v8 = (int64_t)value;
553 int64_t pad_value = v8 | (v8 << 8) | (v8 << 16) | (v8 << 24) | (v8 << 32) | (v8 << 40) | (v8 << 48) | (v8 << 56);
554
555 //Channel padding
556 if ((q - front_) < 0 || (q - front_) >= channels)
557 {
558 borderm.fill<int64_t>(pad_value);
559 }
560 else
561 {
562 const Mat m = bottom_blob.channel(q - front_);
563 if (type == 0)
564 padding_constant_pack8_int8_sse(m, borderm, top, bottom, left, right, pad_value);
565 if (type == 1)
566 padding_replicate_pack8_int8_sse(m, borderm, top, bottom, left, right);
567 if (type == 2)
568 padding_reflect_pack8_int8_sse(m, borderm, top, bottom, left, right);
569 }
570 }
571
572 return 0;
573 }
574 }
575
576 if (dims == 4)
577 {
578 int outw = w + left + right;
579 int outh = h + top + bottom;
580 int outd = d + front + behind;
581
582 if (type == 0)
583 {
584 top_blob.create(outw, outh, outd, channels, elemsize, elempack, opt.blob_allocator);
585 if (top_blob.empty())
586 return -100;
587
588 #pragma omp parallel for num_threads(opt.num_threads)
589 for (int q = 0; q < channels; q++)
590 {
591 // TODO perchannel
592 // int64_t pad_value = per_channel_pad_data_size ? vld1_s8(per_channel_pad_data + q * 8) : vdup_n_s8((signed char)value);
593 int64_t v8 = (int64_t)value;
594 int64_t pad_value = v8 | (v8 << 8) | (v8 << 16) | (v8 << 24) | (v8 << 32) | (v8 << 40) | (v8 << 48) | (v8 << 56);
595
596 for (int z = 0; z < outd; z++)
597 {
598 Mat borderm = top_blob.channel(q).depth(z);
599
600 // depth padding
601 if ((z - front) < 0 || (z - front) >= d)
602 {
603 borderm.fill<int64_t>(pad_value);
604 }
605 else
606 {
607 const Mat m = bottom_blob.channel(q).depth(z - front);
608 padding_constant_pack8_int8_sse(m, borderm, top, bottom, left, right, pad_value);
609 }
610 }
611 }
612
613 return 0;
614 }
615 }
616 }
617#endif // __SSE2__
618
619 Mat bottom_blob_unpacked = bottom_blob;
620 if (elempack != 1)
621 {
622 Option opt_pack1 = opt;
623 opt_pack1.blob_allocator = opt.workspace_allocator;
624
625 convert_packing(bottom_blob, bottom_blob_unpacked, 1, opt_pack1);
626 if (bottom_blob_unpacked.empty())
627 return -100;
628 }
629
630 return Padding::forward(bottom_blob_unpacked, top_blob, opt);
631}
632
633} // namespace ncnn