Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 23.4 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 "permute.h"
16
17namespace ncnn {
18
19Permute::Permute()
20{
21 one_blob_only = true;
22 support_inplace = false;
23}
24
25int Permute::load_param(const ParamDict& pd)
26{
27 order_type = pd.get(0, 0);
28
29 return 0;
30}
31
32int Permute::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
33{
34 int w = bottom_blob.w;
35 int h = bottom_blob.h;
36 int d = bottom_blob.d;
37 int channels = bottom_blob.c;
38 size_t elemsize = bottom_blob.elemsize;
39
40 int dims = bottom_blob.dims;
41
42 if (dims == 1)
43 {
44 top_blob = bottom_blob;
45 return 0;
46 }
47
48 if (dims == 2)
49 {
50 // order_type
51 // 0 = w h
52 // 1 = h w
53
54 if (order_type == 0)
55 {
56 top_blob = bottom_blob;
57 }
58 if (order_type == 1)
59 {
60 top_blob.create(h, w, elemsize, opt.blob_allocator);
61 if (top_blob.empty())
62 return -100;
63
64 float* outptr = top_blob;
65
66 for (int i = 0; i < w; i++)
67 {
68 for (int j = 0; j < h; j++)
69 {
70 *outptr++ = bottom_blob.row(j)[i];
71 }
72 }
73 }
74 }
75
76 if (dims == 3)
77 {
78 // order_type
79 // 0 = w h c
80 // 1 = h w c
81 // 2 = w c h
82 // 3 = c w h
83 // 4 = h c w
84 // 5 = c h w
85
86 if (order_type == 0)
87 {
88 top_blob = bottom_blob;
89 }
90 if (order_type == 1)
91 {
92 top_blob.create(h, w, channels, elemsize, opt.blob_allocator);
93 if (top_blob.empty())
94 return -100;
95
96 #pragma omp parallel for num_threads(opt.num_threads)
97 for (int q = 0; q < channels; q++)
98 {
99 const Mat m = bottom_blob.channel(q);
100 float* outptr = top_blob.channel(q);
101
102 for (int i = 0; i < w; i++)
103 {
104 for (int j = 0; j < h; j++)
105 {
106 *outptr++ = m.row(j)[i];
107 }
108 }
109 }
110 }
111 if (order_type == 2)
112 {
113 top_blob.create(w, channels, h, elemsize, opt.blob_allocator);
114 if (top_blob.empty())
115 return -100;
116
117 #pragma omp parallel for num_threads(opt.num_threads)
118 for (int q = 0; q < h; q++)
119 {
120 float* outptr = top_blob.channel(q);
121
122 for (int i = 0; i < channels; i++)
123 {
124 const float* ptr = bottom_blob.channel(i).row(q);
125
126 for (int j = 0; j < w; j++)
127 {
128 *outptr++ = ptr[j];
129 }
130 }
131 }
132 }
133 if (order_type == 3)
134 {
135 top_blob.create(channels, w, h, elemsize, opt.blob_allocator);
136 if (top_blob.empty())
137 return -100;
138
139 #pragma omp parallel for num_threads(opt.num_threads)
140 for (int q = 0; q < h; q++)
141 {
142 float* outptr = top_blob.channel(q);
143
144 for (int i = 0; i < w; i++)
145 {
146 for (int j = 0; j < channels; j++)
147 {
148 *outptr++ = bottom_blob.channel(j).row(q)[i];
149 }
150 }
151 }
152 }
153 if (order_type == 4)
154 {
155 top_blob.create(h, channels, w, elemsize, opt.blob_allocator);
156 if (top_blob.empty())
157 return -100;
158
159 #pragma omp parallel for num_threads(opt.num_threads)
160 for (int q = 0; q < w; q++)
161 {
162 float* outptr = top_blob.channel(q);
163
164 for (int i = 0; i < channels; i++)
165 {
166 const Mat m = bottom_blob.channel(i);
167
168 for (int j = 0; j < h; j++)
169 {
170 *outptr++ = m.row(j)[q];
171 }
172 }
173 }
174 }
175 if (order_type == 5)
176 {
177 top_blob.create(channels, h, w, elemsize, opt.blob_allocator);
178 if (top_blob.empty())
179 return -100;
180
181 #pragma omp parallel for num_threads(opt.num_threads)
182 for (int q = 0; q < w; q++)
183 {
184 float* outptr = top_blob.channel(q);
185
186 for (int i = 0; i < h; i++)
187 {
188 for (int j = 0; j < channels; j++)
189 {
190 *outptr++ = bottom_blob.channel(j).row(i)[q];
191 }
192 }
193 }
194 }
195 }
196
197 if (dims == 4)
198 {
199 // order_type
200 // 0 = w h d c
201 // 1 = h w d c
202 // 2 = w d h c
203 // 3 = d w h c
204 // 4 = h d w c
205 // 5 = d h w c
206 // 6 = w h c d
207 // 7 = h w c d
208 // 8 = w c h d
209 // 9 = c w h d
210 //10 = h c w d
211 //11 = c h w d
212 //12 = w d c h
213 //13 = d w c h
214 //14 = w c d h
215 //15 = c w d h
216 //16 = d c w h
217 //17 = c d w h
218 //18 = h d c w
219 //19 = d h c w
220 //20 = h c d w
221 //21 = c h d w
222 //22 = d c h w
223 //23 = c d h w
224
225 if (order_type == 0)
226 {
227 top_blob = bottom_blob;
228 }
229 if (order_type == 1)
230 {
231 top_blob.create(h, w, d, channels, elemsize, opt.blob_allocator);
232 if (top_blob.empty())
233 return -100;
234
235 #pragma omp parallel for num_threads(opt.num_threads)
236 for (int q = 0; q < channels; q++)
237 {
238 float* outptr = top_blob.channel(q);
239
240 for (int z = 0; z < d; z++)
241 {
242 const Mat m = bottom_blob.channel(q).depth(z);
243
244 for (int i = 0; i < w; i++)
245 {
246 for (int j = 0; j < h; j++)
247 {
248 *outptr++ = m.row(j)[i];
249 }
250 }
251 }
252 }
253 }
254 if (order_type == 2)
255 {
256 top_blob.create(w, d, h, channels, elemsize, opt.blob_allocator);
257 if (top_blob.empty())
258 return -100;
259
260 #pragma omp parallel for num_threads(opt.num_threads)
261 for (int q = 0; q < channels; q++)
262 {
263 float* outptr = top_blob.channel(q);
264
265 for (int z = 0; z < h; z++)
266 {
267 for (int i = 0; i < d; i++)
268 {
269 const float* ptr = bottom_blob.channel(q).depth(i).row(z);
270
271 for (int j = 0; j < w; j++)
272 {
273 *outptr++ = ptr[j];
274 }
275 }
276 }
277 }
278 }
279 if (order_type == 3)
280 {
281 top_blob.create(d, w, h, channels, elemsize, opt.blob_allocator);
282 if (top_blob.empty())
283 return -100;
284
285 #pragma omp parallel for num_threads(opt.num_threads)
286 for (int q = 0; q < channels; q++)
287 {
288 const Mat m = bottom_blob.channel(q);
289 float* outptr = top_blob.channel(q);
290
291 for (int z = 0; z < h; z++)
292 {
293 for (int i = 0; i < w; i++)
294 {
295 for (int j = 0; j < d; j++)
296 {
297 *outptr++ = m.depth(j).row(z)[i];
298 }
299 }
300 }
301 }
302 }
303 if (order_type == 4)
304 {
305 top_blob.create(h, d, w, channels, elemsize, opt.blob_allocator);
306 if (top_blob.empty())
307 return -100;
308
309 #pragma omp parallel for num_threads(opt.num_threads)
310 for (int q = 0; q < channels; q++)
311 {
312 float* outptr = top_blob.channel(q);
313
314 for (int z = 0; z < w; z++)
315 {
316 for (int i = 0; i < d; i++)
317 {
318 const Mat m = bottom_blob.channel(q).depth(i);
319
320 for (int j = 0; j < h; j++)
321 {
322 *outptr++ = m.row(j)[z];
323 }
324 }
325 }
326 }
327 }
328 if (order_type == 5)
329 {
330 top_blob.create(d, h, w, channels, elemsize, opt.blob_allocator);
331 if (top_blob.empty())
332 return -100;
333
334 #pragma omp parallel for num_threads(opt.num_threads)
335 for (int q = 0; q < channels; q++)
336 {
337 const Mat m = bottom_blob.channel(q);
338 float* outptr = top_blob.channel(q);
339
340 for (int z = 0; z < w; z++)
341 {
342 for (int i = 0; i < h; i++)
343 {
344 for (int j = 0; j < d; j++)
345 {
346 *outptr++ = m.depth(j).row(i)[z];
347 }
348 }
349 }
350 }
351 }
352 if (order_type == 6)
353 {
354 top_blob.create(w, h, channels, d, elemsize, opt.blob_allocator);
355 if (top_blob.empty())
356 return -100;
357
358 #pragma omp parallel for num_threads(opt.num_threads)
359 for (int q = 0; q < d; q++)
360 {
361 float* outptr = top_blob.channel(q);
362
363 for (int z = 0; z < channels; z++)
364 {
365 for (int i = 0; i < h; i++)
366 {
367 const float* ptr = bottom_blob.channel(z).depth(q).row(i);
368
369 for (int j = 0; j < w; j++)
370 {
371 *outptr++ = ptr[j];
372 }
373 }
374 }
375 }
376 }
377 if (order_type == 7)
378 {
379 top_blob.create(h, w, channels, d, elemsize, opt.blob_allocator);
380 if (top_blob.empty())
381 return -100;
382
383 #pragma omp parallel for num_threads(opt.num_threads)
384 for (int q = 0; q < d; q++)
385 {
386 float* outptr = top_blob.channel(q);
387
388 for (int z = 0; z < channels; z++)
389 {
390 const Mat m = bottom_blob.channel(z).depth(q);
391
392 for (int i = 0; i < w; i++)
393 {
394 for (int j = 0; j < h; j++)
395 {
396 *outptr++ = m.row(j)[i];
397 }
398 }
399 }
400 }
401 }
402 if (order_type == 8)
403 {
404 top_blob.create(w, channels, h, d, elemsize, opt.blob_allocator);
405 if (top_blob.empty())
406 return -100;
407
408 #pragma omp parallel for num_threads(opt.num_threads)
409 for (int q = 0; q < d; q++)
410 {
411 float* outptr = top_blob.channel(q);
412
413 for (int z = 0; z < h; z++)
414 {
415 for (int i = 0; i < channels; i++)
416 {
417 const float* ptr = bottom_blob.channel(i).depth(q).row(z);
418
419 for (int j = 0; j < w; j++)
420 {
421 *outptr++ = ptr[j];
422 }
423 }
424 }
425 }
426 }
427 if (order_type == 9)
428 {
429 top_blob.create(channels, w, h, d, elemsize, opt.blob_allocator);
430 if (top_blob.empty())
431 return -100;
432
433 #pragma omp parallel for num_threads(opt.num_threads)
434 for (int q = 0; q < d; q++)
435 {
436 float* outptr = top_blob.channel(q);
437
438 for (int z = 0; z < h; z++)
439 {
440 for (int i = 0; i < w; i++)
441 {
442 for (int j = 0; j < channels; j++)
443 {
444 *outptr++ = bottom_blob.channel(j).depth(q).row(z)[i];
445 }
446 }
447 }
448 }
449 }
450 if (order_type == 10)
451 {
452 top_blob.create(h, channels, w, d, elemsize, opt.blob_allocator);
453 if (top_blob.empty())
454 return -100;
455
456 #pragma omp parallel for num_threads(opt.num_threads)
457 for (int q = 0; q < d; q++)
458 {
459 float* outptr = top_blob.channel(q);
460
461 for (int z = 0; z < w; z++)
462 {
463 for (int i = 0; i < channels; i++)
464 {
465 const Mat m = bottom_blob.channel(i).depth(q);
466
467 for (int j = 0; j < h; j++)
468 {
469 *outptr++ = m.row(j)[z];
470 }
471 }
472 }
473 }
474 }
475 if (order_type == 11)
476 {
477 top_blob.create(channels, h, w, d, elemsize, opt.blob_allocator);
478 if (top_blob.empty())
479 return -100;
480
481 #pragma omp parallel for num_threads(opt.num_threads)
482 for (int q = 0; q < d; q++)
483 {
484 float* outptr = top_blob.channel(q);
485
486 for (int z = 0; z < w; z++)
487 {
488 for (int i = 0; i < h; i++)
489 {
490 for (int j = 0; j < channels; j++)
491 {
492 *outptr++ = bottom_blob.channel(j).depth(q).row(i)[z];
493 }
494 }
495 }
496 }
497 }
498 if (order_type == 12)
499 {
500 top_blob.create(w, d, channels, h, elemsize, opt.blob_allocator);
501 if (top_blob.empty())
502 return -100;
503
504 #pragma omp parallel for num_threads(opt.num_threads)
505 for (int q = 0; q < h; q++)
506 {
507 float* outptr = top_blob.channel(q);
508
509 for (int z = 0; z < channels; z++)
510 {
511 for (int i = 0; i < d; i++)
512 {
513 const float* ptr = bottom_blob.channel(z).depth(i).row(q);
514
515 for (int j = 0; j < w; j++)
516 {
517 *outptr++ = ptr[j];
518 }
519 }
520 }
521 }
522 }
523 if (order_type == 13)
524 {
525 top_blob.create(d, w, channels, h, elemsize, opt.blob_allocator);
526 if (top_blob.empty())
527 return -100;
528
529 #pragma omp parallel for num_threads(opt.num_threads)
530 for (int q = 0; q < h; q++)
531 {
532 float* outptr = top_blob.channel(q);
533
534 for (int z = 0; z < channels; z++)
535 {
536 const Mat m = bottom_blob.channel(z);
537
538 for (int i = 0; i < w; i++)
539 {
540 for (int j = 0; j < d; j++)
541 {
542 *outptr++ = m.depth(j).row(q)[i];
543 }
544 }
545 }
546 }
547 }
548 if (order_type == 14)
549 {
550 top_blob.create(w, channels, d, h, elemsize, opt.blob_allocator);
551 if (top_blob.empty())
552 return -100;
553
554 #pragma omp parallel for num_threads(opt.num_threads)
555 for (int q = 0; q < h; q++)
556 {
557 float* outptr = top_blob.channel(q);
558
559 for (int z = 0; z < d; z++)
560 {
561 for (int i = 0; i < channels; i++)
562 {
563 const float* ptr = bottom_blob.channel(i).depth(z).row(q);
564
565 for (int j = 0; j < w; j++)
566 {
567 *outptr++ = ptr[j];
568 }
569 }
570 }
571 }
572 }
573 if (order_type == 15)
574 {
575 top_blob.create(channels, w, d, h, elemsize, opt.blob_allocator);
576 if (top_blob.empty())
577 return -100;
578
579 #pragma omp parallel for num_threads(opt.num_threads)
580 for (int q = 0; q < h; q++)
581 {
582 float* outptr = top_blob.channel(q);
583
584 for (int z = 0; z < d; z++)
585 {
586 for (int i = 0; i < w; i++)
587 {
588 for (int j = 0; j < channels; j++)
589 {
590 *outptr++ = bottom_blob.channel(j).depth(z).row(q)[i];
591 }
592 }
593 }
594 }
595 }
596 if (order_type == 16)
597 {
598 top_blob.create(d, channels, w, h, elemsize, opt.blob_allocator);
599 if (top_blob.empty())
600 return -100;
601
602 #pragma omp parallel for num_threads(opt.num_threads)
603 for (int q = 0; q < h; q++)
604 {
605 float* outptr = top_blob.channel(q);
606
607 for (int z = 0; z < w; z++)
608 {
609 for (int i = 0; i < channels; i++)
610 {
611 const Mat m = bottom_blob.channel(i);
612
613 for (int j = 0; j < d; j++)
614 {
615 *outptr++ = m.depth(j).row(q)[z];
616 }
617 }
618 }
619 }
620 }
621 if (order_type == 17)
622 {
623 top_blob.create(channels, d, w, h, elemsize, opt.blob_allocator);
624 if (top_blob.empty())
625 return -100;
626
627 #pragma omp parallel for num_threads(opt.num_threads)
628 for (int q = 0; q < h; q++)
629 {
630 float* outptr = top_blob.channel(q);
631
632 for (int z = 0; z < w; z++)
633 {
634 for (int i = 0; i < d; i++)
635 {
636 for (int j = 0; j < channels; j++)
637 {
638 *outptr++ = bottom_blob.channel(j).depth(i).row(q)[z];
639 }
640 }
641 }
642 }
643 }
644 if (order_type == 18)
645 {
646 top_blob.create(h, d, channels, w, elemsize, opt.blob_allocator);
647 if (top_blob.empty())
648 return -100;
649
650 #pragma omp parallel for num_threads(opt.num_threads)
651 for (int q = 0; q < w; q++)
652 {
653 float* outptr = top_blob.channel(q);
654
655 for (int z = 0; z < channels; z++)
656 {
657 for (int i = 0; i < d; i++)
658 {
659 const Mat m = bottom_blob.channel(z).depth(i);
660
661 for (int j = 0; j < h; j++)
662 {
663 *outptr++ = m.row(j)[q];
664 }
665 }
666 }
667 }
668 }
669 if (order_type == 19)
670 {
671 top_blob.create(d, h, channels, w, elemsize, opt.blob_allocator);
672 if (top_blob.empty())
673 return -100;
674
675 #pragma omp parallel for num_threads(opt.num_threads)
676 for (int q = 0; q < w; q++)
677 {
678 float* outptr = top_blob.channel(q);
679
680 for (int z = 0; z < channels; z++)
681 {
682 for (int i = 0; i < h; i++)
683 {
684 const Mat m = bottom_blob.channel(z);
685
686 for (int j = 0; j < d; j++)
687 {
688 *outptr++ = m.depth(j).row(i)[q];
689 }
690 }
691 }
692 }
693 }
694 if (order_type == 20)
695 {
696 top_blob.create(h, channels, d, w, elemsize, opt.blob_allocator);
697 if (top_blob.empty())
698 return -100;
699
700 #pragma omp parallel for num_threads(opt.num_threads)
701 for (int q = 0; q < w; q++)
702 {
703 float* outptr = top_blob.channel(q);
704
705 for (int z = 0; z < d; z++)
706 {
707 for (int i = 0; i < channels; i++)
708 {
709 const Mat m = bottom_blob.channel(i).depth(z);
710
711 for (int j = 0; j < h; j++)
712 {
713 *outptr++ = m.row(j)[q];
714 }
715 }
716 }
717 }
718 }
719 if (order_type == 21)
720 {
721 top_blob.create(channels, h, d, w, elemsize, opt.blob_allocator);
722 if (top_blob.empty())
723 return -100;
724
725 #pragma omp parallel for num_threads(opt.num_threads)
726 for (int q = 0; q < w; q++)
727 {
728 float* outptr = top_blob.channel(q);
729
730 for (int z = 0; z < d; z++)
731 {
732 for (int i = 0; i < h; i++)
733 {
734 for (int j = 0; j < channels; j++)
735 {
736 *outptr++ = bottom_blob.channel(j).depth(z).row(i)[q];
737 }
738 }
739 }
740 }
741 }
742 if (order_type == 22)
743 {
744 top_blob.create(d, channels, h, w, elemsize, opt.blob_allocator);
745 if (top_blob.empty())
746 return -100;
747
748 #pragma omp parallel for num_threads(opt.num_threads)
749 for (int q = 0; q < w; q++)
750 {
751 float* outptr = top_blob.channel(q);
752
753 for (int z = 0; z < h; z++)
754 {
755 for (int i = 0; i < channels; i++)
756 {
757 const Mat m = bottom_blob.channel(i);
758
759 for (int j = 0; j < d; j++)
760 {
761 *outptr++ = m.depth(j).row(z)[q];
762 }
763 }
764 }
765 }
766 }
767 if (order_type == 23)
768 {
769 top_blob.create(channels, d, h, w, elemsize, opt.blob_allocator);
770 if (top_blob.empty())
771 return -100;
772
773 #pragma omp parallel for num_threads(opt.num_threads)
774 for (int q = 0; q < w; q++)
775 {
776 float* outptr = top_blob.channel(q);
777
778 for (int z = 0; z < h; z++)
779 {
780 for (int i = 0; i < d; i++)
781 {
782 for (int j = 0; j < channels; j++)
783 {
784 *outptr++ = bottom_blob.channel(j).depth(i).row(z)[q];
785 }
786 }
787 }
788 }
789 }
790 }
791
792 return 0;
793}
794
795} // namespace ncnn