Elffuss BraidHub

kikocis/ncnn public

d7a5f98c0f60 · 36.2 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 "reduction.h"
16
17#include <float.h>
18#include <limits.h>
19
20namespace ncnn {
21
22Reduction::Reduction()
23{
24 one_blob_only = true;
25 support_inplace = false;
26}
27
28int Reduction::load_param(const ParamDict& pd)
29{
30 operation = pd.get(0, 0);
31 reduce_all = pd.get(1, 1);
32 coeff = pd.get(2, 1.f);
33 axes = pd.get(3, Mat());
34 keepdims = pd.get(4, 0);
35
36 // the original reduction handle axes as blob with batch dimension
37 // ask user to regenerate param instead of producing wrong result
38 int fixbug0 = pd.get(5, 0);
39 if (fixbug0 == 0 && !axes.empty())
40 {
41 NCNN_LOGE("param is too old, please regenerate!");
42 return -1;
43 }
44
45 return 0;
46}
47
48template<typename Op, typename Op2>
49static int reduction_op(const Mat& a, Mat& b, float v0, bool reduce_w, bool reduce_h, bool reduce_d, bool reduce_c, int keepdims, const Option& opt)
50{
51 Op op;
52 Op2 op2;
53
54 size_t elemsize = a.elemsize;
55 int dims = a.dims;
56
57 if (dims == 1)
58 {
59 int w = a.w;
60 b.create(1, elemsize, opt.blob_allocator);
61 const float* ptr = a;
62
63 float sum = v0;
64 for (int i = 0; i < w; i++)
65 {
66 sum = op(sum, ptr[i]);
67 }
68 b[0] = sum;
69
70 return 0;
71 }
72
73 if (dims == 2)
74 {
75 int w = a.w;
76 int h = a.h;
77
78 if (reduce_w && reduce_h)
79 {
80 // w h -> X X
81 if (keepdims)
82 b.create(1, 1, elemsize, opt.blob_allocator);
83 else
84 b.create(1, elemsize, opt.blob_allocator);
85
86 Mat sums(h, elemsize, opt.workspace_allocator);
87 if (sums.empty())
88 return -100;
89
90 #pragma omp parallel for num_threads(opt.num_threads)
91 for (int i = 0; i < h; i++)
92 {
93 const float* ptr = a.row(i);
94
95 float sum = v0;
96 for (int j = 0; j < w; j++)
97 {
98 sum = op(sum, ptr[j]);
99 }
100 sums[i] = sum;
101 }
102
103 float sum = v0;
104 for (int i = 0; i < h; i++)
105 {
106 sum = op2(sum, sums[i]);
107 }
108 b[0] = sum;
109
110 return 0;
111 }
112
113 if (reduce_w && !reduce_h)
114 {
115 // w h -> X h
116 if (keepdims)
117 b.create(1, h, elemsize, opt.blob_allocator);
118 else
119 b.create(h, elemsize, opt.blob_allocator);
120
121 #pragma omp parallel for num_threads(opt.num_threads)
122 for (int i = 0; i < h; i++)
123 {
124 const float* ptr = a.row(i);
125
126 float sum = v0;
127 for (int j = 0; j < w; j++)
128 {
129 sum = op(sum, ptr[j]);
130 }
131 b[i] = sum;
132 }
133 return 0;
134 }
135
136 if (!reduce_w && reduce_h)
137 {
138 // w h -> w X
139 if (keepdims)
140 b.create(w, 1, elemsize, opt.blob_allocator);
141 else
142 b.create(w, elemsize, opt.blob_allocator);
143 b.fill(v0);
144
145 for (int i = 0; i < h; i++)
146 {
147 const float* ptr = a.row(i);
148 for (int j = 0; j < w; j++)
149 {
150 b[j] = op(b[j], ptr[j]);
151 }
152 }
153 return 0;
154 }
155 }
156
157 if (dims == 3)
158 {
159 int w = a.w;
160 int h = a.h;
161 int channels = a.c;
162 int size = w * h;
163
164 if (reduce_w && reduce_h && reduce_c)
165 {
166 // w h c -> X X X
167 if (keepdims)
168 b.create(1, 1, 1, elemsize, opt.blob_allocator);
169 else
170 b.create(1, elemsize, opt.blob_allocator);
171 Mat sums(channels, elemsize, opt.workspace_allocator);
172 if (sums.empty())
173 return -100;
174
175 #pragma omp parallel for num_threads(opt.num_threads)
176 for (int q = 0; q < channels; q++)
177 {
178 const float* ptr = a.channel(q);
179
180 float sum = v0;
181 for (int i = 0; i < size; i++)
182 {
183 sum = op(sum, ptr[i]);
184 }
185 sums[q] = sum;
186 }
187
188 float sum = v0;
189 for (int i = 0; i < channels; i++)
190 {
191 sum = op2(sum, sums[i]);
192 }
193 b[0] = sum;
194
195 return 0;
196 }
197
198 if (reduce_w && reduce_h && !reduce_c)
199 {
200 // w h c -> X X c
201 if (keepdims)
202 b.create(1, 1, channels, elemsize, opt.blob_allocator);
203 else
204 b.create(channels, elemsize, opt.blob_allocator);
205
206 #pragma omp parallel for num_threads(opt.num_threads)
207 for (int q = 0; q < channels; q++)
208 {
209 const float* ptr = a.channel(q);
210
211 float sum = v0;
212 for (int i = 0; i < size; i++)
213 {
214 sum = op(sum, ptr[i]);
215 }
216
217 if (keepdims)
218 b.channel(q)[0] = sum;
219 else
220 b[q] = sum;
221 }
222
223 return 0;
224 }
225
226 if (reduce_w && !reduce_h && reduce_c)
227 {
228 // w h c -> X h X
229 if (keepdims)
230 b.create(1, h, 1, elemsize, opt.blob_allocator);
231 else
232 b.create(h, elemsize, opt.blob_allocator);
233 Mat mins(1, h, channels, elemsize, opt.workspace_allocator);
234 if (mins.empty())
235 return -100;
236
237 mins.fill(v0);
238
239 #pragma omp parallel for num_threads(opt.num_threads)
240 for (int q = 0; q < channels; q++)
241 {
242 const float* ptr = a.channel(q);
243 float* mins_ptr = mins.channel(q);
244
245 for (int i = 0; i < h; i++)
246 {
247 float sum = v0;
248 for (int j = 0; j < w; j++)
249 {
250 sum = op(sum, ptr[j]);
251 }
252 mins_ptr[i] = sum;
253 ptr += w;
254 }
255 }
256
257 b.fill(v0);
258
259 for (int q = 0; q < channels; q++)
260 {
261 const float* mins_ptr = mins.channel(q);
262 for (int i = 0; i < h; i++)
263 {
264 b[i] = op2(b[i], mins_ptr[i]);
265 }
266 }
267
268 return 0;
269 }
270
271 if (!reduce_w && reduce_h && reduce_c)
272 {
273 // w h c -> w X X
274 if (keepdims)
275 b.create(w, 1, 1, elemsize, opt.blob_allocator);
276 else
277 b.create(w, elemsize, opt.blob_allocator);
278
279 Mat mins(w, 1, channels, elemsize, opt.workspace_allocator);
280 if (mins.empty())
281 return -100;
282
283 mins.fill(v0);
284
285 #pragma omp parallel for num_threads(opt.num_threads)
286 for (int q = 0; q < channels; q++)
287 {
288 const float* ptr = a.channel(q);
289 float* mins_ptr = mins.channel(q);
290
291 for (int i = 0; i < h; i++)
292 {
293 for (int j = 0; j < w; j++)
294 {
295 mins_ptr[j] = op(mins_ptr[j], ptr[j]);
296 }
297 ptr += w;
298 }
299 }
300
301 b.fill(v0);
302
303 for (int q = 0; q < channels; q++)
304 {
305 const float* mins_ptr = mins.channel(q);
306 for (int j = 0; j < w; j++)
307 {
308 b[j] = op2(b[j], mins_ptr[j]);
309 }
310 }
311
312 return 0;
313 }
314
315 if (reduce_w && !reduce_h && !reduce_c)
316 {
317 // w h c -> X h c
318 if (keepdims)
319 b.create(1, h, channels, elemsize, opt.blob_allocator);
320 else
321 b.create(h, channels, elemsize, opt.blob_allocator);
322
323 #pragma omp parallel for num_threads(opt.num_threads)
324 for (int q = 0; q < channels; q++)
325 {
326 const float* ptr = a.channel(q);
327 float* outptr = keepdims ? b.channel(q) : b.row(q);
328
329 for (int i = 0; i < h; i++)
330 {
331 float sum = v0;
332 for (int j = 0; j < w; j++)
333 {
334 sum = op(sum, ptr[j]);
335 }
336 outptr[i] = sum;
337 ptr += w;
338 }
339 }
340
341 return 0;
342 }
343
344 if (!reduce_w && !reduce_h && reduce_c)
345 {
346 // w h c -> w h X
347 if (keepdims)
348 b.create(w, h, 1, elemsize, opt.blob_allocator);
349 else
350 b.create(w, h, elemsize, opt.blob_allocator);
351
352 b.fill(v0);
353
354 for (int q = 0; q < channels; q++)
355 {
356 const float* ptr = a.channel(q);
357
358 for (int i = 0; i < size; i++)
359 {
360 b[i] = op(b[i], ptr[i]);
361 }
362 }
363
364 return 0;
365 }
366
367 if (!reduce_w && reduce_h && !reduce_c)
368 {
369 // w h c -> w X c
370 if (keepdims)
371 b.create(w, 1, channels, elemsize, opt.blob_allocator);
372 else
373 b.create(w, channels, elemsize, opt.blob_allocator);
374
375 b.fill(v0);
376
377 #pragma omp parallel for num_threads(opt.num_threads)
378 for (int q = 0; q < channels; q++)
379 {
380 const float* ptr = a.channel(q);
381 float* outptr = keepdims ? b.channel(q) : b.row(q);
382
383 for (int i = 0; i < h; i++)
384 {
385 for (int j = 0; j < w; j++)
386 {
387 outptr[j] = op(outptr[j], ptr[j]);
388 }
389 ptr += w;
390 }
391 }
392 return 0;
393 }
394 }
395
396 if (dims == 4)
397 {
398 int w = a.w;
399 int h = a.h;
400 int d = a.d;
401 int channels = a.c;
402 int size = w * h * d;
403
404 if (reduce_w && reduce_h && reduce_d && reduce_c)
405 {
406 // w h d c -> X X X X
407 if (keepdims)
408 b.create(1, 1, 1, 1, elemsize, opt.blob_allocator);
409 else
410 b.create(1, elemsize, opt.blob_allocator);
411 Mat sums(channels, elemsize, opt.workspace_allocator);
412 if (sums.empty())
413 return -100;
414
415 #pragma omp parallel for num_threads(opt.num_threads)
416 for (int q = 0; q < channels; q++)
417 {
418 const float* ptr = a.channel(q);
419
420 float sum = v0;
421 for (int i = 0; i < size; i++)
422 {
423 sum = op(sum, ptr[i]);
424 }
425 sums[q] = sum;
426 }
427
428 float sum = v0;
429 for (int i = 0; i < channels; i++)
430 {
431 sum = op2(sum, sums[i]);
432 }
433 b[0] = sum;
434
435 return 0;
436 }
437
438 if (reduce_w && reduce_h && reduce_d && !reduce_c)
439 {
440 // w h d c -> X X X c
441 if (keepdims)
442 b.create(1, 1, 1, channels, elemsize, opt.blob_allocator);
443 else
444 b.create(channels, elemsize, opt.blob_allocator);
445
446 #pragma omp parallel for num_threads(opt.num_threads)
447 for (int q = 0; q < channels; q++)
448 {
449 const float* ptr = a.channel(q);
450
451 float sum = v0;
452 for (int i = 0; i < size; i++)
453 {
454 sum = op(sum, ptr[i]);
455 }
456 if (keepdims)
457 b.channel(q)[0] = sum;
458 else
459 b[q] = sum;
460 }
461
462 return 0;
463 }
464
465 if (reduce_w && reduce_h && !reduce_d && reduce_c)
466 {
467 // w h d c -> X X d X
468 if (keepdims)
469 b.create(1, 1, d, 1, elemsize, opt.blob_allocator);
470 else
471 b.create(d, elemsize, opt.blob_allocator);
472 Mat mins(1, d, channels, elemsize, opt.workspace_allocator);
473 if (mins.empty())
474 return -100;
475
476 mins.fill(v0);
477
478 #pragma omp parallel for num_threads(opt.num_threads)
479 for (int q = 0; q < channels; q++)
480 {
481 const float* ptr = a.channel(q);
482 float* mins_ptr = mins.channel(q);
483
484 for (int i = 0; i < d; i++)
485 {
486 float sum = v0;
487 for (int j = 0; j < w * h; j++)
488 {
489 sum = op(sum, ptr[j]);
490 }
491 mins_ptr[i] = sum;
492 ptr += w * h;
493 }
494 }
495
496 b.fill(v0);
497
498 for (int q = 0; q < channels; q++)
499 {
500 const float* mins_ptr = mins.channel(q);
501 for (int i = 0; i < d; i++)
502 {
503 b[i] = op2(b[i], mins_ptr[i]);
504 }
505 }
506
507 return 0;
508 }
509
510 if (reduce_w && !reduce_h && reduce_d && reduce_c)
511 {
512 // w h d c -> X h X X
513 if (keepdims)
514 b.create(1, h, 1, 1, elemsize, opt.blob_allocator);
515 else
516 b.create(h, elemsize, opt.blob_allocator);
517 Mat mins(1, h, channels, elemsize, opt.workspace_allocator);
518 if (mins.empty())
519 return -100;
520
521 mins.fill(v0);
522
523 #pragma omp parallel for num_threads(opt.num_threads)
524 for (int q = 0; q < channels; q++)
525 {
526 const float* ptr = a.channel(q);
527 float* mins_ptr = mins.channel(q);
528
529 for (int i = 0; i < d; i++)
530 {
531 for (int j = 0; j < h; j++)
532 {
533 for (int k = 0; k < w; k++)
534 {
535 mins_ptr[j] = op(mins_ptr[j], ptr[k]);
536 }
537 ptr += w;
538 }
539 }
540 }
541
542 b.fill(v0);
543
544 for (int q = 0; q < channels; q++)
545 {
546 const float* mins_ptr = mins.channel(q);
547 for (int i = 0; i < h; i++)
548 {
549 b[i] = op2(b[i], mins_ptr[i]);
550 }
551 }
552
553 return 0;
554 }
555
556 if (!reduce_w && reduce_h && reduce_d && reduce_c)
557 {
558 // w h d c -> w X X X
559 if (keepdims)
560 b.create(w, 1, 1, 1, elemsize, opt.blob_allocator);
561 else
562 b.create(w, elemsize, opt.blob_allocator);
563 Mat mins(w, 1, channels, elemsize, opt.workspace_allocator);
564 if (mins.empty())
565 return -100;
566
567 mins.fill(v0);
568
569 #pragma omp parallel for num_threads(opt.num_threads)
570 for (int q = 0; q < channels; q++)
571 {
572 const float* ptr = a.channel(q);
573 float* mins_ptr = mins.channel(q);
574
575 for (int i = 0; i < d; i++)
576 {
577 for (int j = 0; j < h; j++)
578 {
579 for (int k = 0; k < w; k++)
580 {
581 mins_ptr[k] = op(mins_ptr[k], ptr[k]);
582 }
583 ptr += w;
584 }
585 }
586 }
587
588 b.fill(v0);
589
590 for (int q = 0; q < channels; q++)
591 {
592 const float* mins_ptr = mins.channel(q);
593 for (int i = 0; i < w; i++)
594 {
595 b[i] = op2(b[i], mins_ptr[i]);
596 }
597 }
598
599 return 0;
600 }
601
602 if (reduce_w && reduce_h && !reduce_d && !reduce_c)
603 {
604 // w h d c -> X X d c
605 if (keepdims)
606 b.create(1, 1, d, channels, elemsize, opt.blob_allocator);
607 else
608 b.create(d, channels, elemsize, opt.blob_allocator);
609
610 #pragma omp parallel for num_threads(opt.num_threads)
611 for (int q = 0; q < channels; q++)
612 {
613 const float* ptr = a.channel(q);
614 float* outptr = keepdims ? b.channel(q) : b.row(q);
615
616 for (int i = 0; i < d; i++)
617 {
618 float sum = v0;
619 for (int j = 0; j < w * h; j++)
620 {
621 sum = op(sum, ptr[j]);
622 }
623 outptr[i] = sum;
624 ptr += w * h;
625 }
626 }
627
628 return 0;
629 }
630
631 if (reduce_w && !reduce_h && !reduce_d && reduce_c)
632 {
633 // w h d c -> X h d X
634 if (keepdims)
635 b.create(1, h, d, 1, elemsize, opt.blob_allocator);
636 else
637 b.create(h, d, elemsize, opt.blob_allocator);
638
639 Mat mins(h, d, channels, elemsize, opt.workspace_allocator);
640 if (mins.empty())
641 return -100;
642
643 mins.fill(v0);
644
645 #pragma omp parallel for num_threads(opt.num_threads)
646 for (int q = 0; q < channels; q++)
647 {
648 const float* ptr = a.channel(q);
649 Mat minsm = mins.channel(q);
650
651 for (int i = 0; i < d; i++)
652 {
653 float* mins_ptr = minsm.row(i);
654 for (int j = 0; j < h; j++)
655 {
656 for (int k = 0; k < w; k++)
657 {
658 mins_ptr[j] = op(mins_ptr[j], ptr[k]);
659 }
660 ptr += w;
661 }
662 }
663 }
664
665 b.fill(v0);
666
667 for (int q = 0; q < channels; q++)
668 {
669 const Mat minsm = mins.channel(q);
670 for (int i = 0; i < d; i++)
671 {
672 const float* mins_ptr = minsm.row(i);
673 float* bptr = keepdims ? b.depth(i) : b.row(i);
674 for (int j = 0; j < h; j++)
675 {
676 bptr[j] = op2(bptr[j], mins_ptr[j]);
677 }
678 }
679 }
680
681 return 0;
682 }
683
684 if (!reduce_w && !reduce_h && reduce_d && reduce_c)
685 {
686 // w h d c -> w h X X
687 if (keepdims)
688 b.create(w, h, 1, 1, elemsize, opt.blob_allocator);
689 else
690 b.create(w, h, elemsize, opt.blob_allocator);
691
692 Mat mins(w, h, channels, elemsize, opt.workspace_allocator);
693 if (mins.empty())
694 return -100;
695
696 mins.fill(v0);
697
698 #pragma omp parallel for num_threads(opt.num_threads)
699 for (int q = 0; q < channels; q++)
700 {
701 const float* ptr = a.channel(q);
702 Mat minsm = mins.channel(q);
703
704 for (int i = 0; i < d; i++)
705 {
706 for (int j = 0; j < h; j++)
707 {
708 float* mins_ptr = minsm.row(j);
709 for (int k = 0; k < w; k++)
710 {
711 mins_ptr[k] = op(mins_ptr[k], ptr[k]);
712 }
713 ptr += w;
714 }
715 }
716 }
717
718 b.fill(v0);
719
720 for (int q = 0; q < channels; q++)
721 {
722 const Mat minsm = mins.channel(q);
723 for (int i = 0; i < h; i++)
724 {
725 const float* mins_ptr = minsm.row(i);
726 float* bptr = b.row(i);
727 for (int j = 0; j < w; j++)
728 {
729 bptr[j] = op2(bptr[j], mins_ptr[j]);
730 }
731 }
732 }
733
734 return 0;
735 }
736
737 if (reduce_w && !reduce_h && reduce_d && !reduce_c)
738 {
739 // w h d c -> X h X c
740 if (keepdims)
741 b.create(1, h, 1, channels, elemsize, opt.blob_allocator);
742 else
743 b.create(h, channels, elemsize, opt.blob_allocator);
744
745 #pragma omp parallel for num_threads(opt.num_threads)
746 for (int q = 0; q < channels; q++)
747 {
748 const float* ptr = a.channel(q);
749 float* outptr = keepdims ? b.channel(q) : b.row(q);
750 for (int i = 0; i < h; i++)
751 {
752 outptr[i] = v0;
753 }
754
755 for (int i = 0; i < d; i++)
756 {
757 for (int j = 0; j < h; j++)
758 {
759 for (int k = 0; k < w; k++)
760 {
761 outptr[j] = op(outptr[j], ptr[k]);
762 }
763 ptr += w;
764 }
765 }
766 }
767
768 return 0;
769 }
770
771 if (!reduce_w && reduce_h && !reduce_d && reduce_c)
772 {
773 // w h d c -> w X d X
774 if (keepdims)
775 b.create(w, 1, d, 1, elemsize, opt.blob_allocator);
776 else
777 b.create(w, d, elemsize, opt.blob_allocator);
778
779 Mat mins(w, d, channels, elemsize, opt.workspace_allocator);
780 if (mins.empty())
781 return -100;
782
783 mins.fill(v0);
784
785 #pragma omp parallel for num_threads(opt.num_threads)
786 for (int q = 0; q < channels; q++)
787 {
788 const float* ptr = a.channel(q);
789 Mat minsm = mins.channel(q);
790
791 for (int i = 0; i < d; i++)
792 {
793 float* mins_ptr = minsm.row(i);
794 for (int j = 0; j < h; j++)
795 {
796 for (int k = 0; k < w; k++)
797 {
798 mins_ptr[k] = op(mins_ptr[k], ptr[k]);
799 }
800 ptr += w;
801 }
802 }
803 }
804
805 b.fill(v0);
806
807 for (int q = 0; q < channels; q++)
808 {
809 const Mat minsm = mins.channel(q);
810 for (int i = 0; i < d; i++)
811 {
812 const float* mins_ptr = minsm.row(i);
813 float* bptr = b.row(i);
814 for (int j = 0; j < w; j++)
815 {
816 bptr[j] = op2(bptr[j], mins_ptr[j]);
817 }
818 }
819 }
820
821 return 0;
822 }
823
824 if (!reduce_w && reduce_h && reduce_d && !reduce_c)
825 {
826 // w h d c -> w X X c
827 if (keepdims)
828 b.create(w, 1, 1, channels, elemsize, opt.blob_allocator);
829 else
830 b.create(w, channels, elemsize, opt.blob_allocator);
831
832 #pragma omp parallel for num_threads(opt.num_threads)
833 for (int q = 0; q < channels; q++)
834 {
835 const float* ptr = a.channel(q);
836 float* outptr = keepdims ? b.channel(q) : b.row(q);
837 for (int i = 0; i < w; i++)
838 {
839 outptr[i] = v0;
840 }
841
842 for (int i = 0; i < d; i++)
843 {
844 for (int j = 0; j < h; j++)
845 {
846 for (int k = 0; k < w; k++)
847 {
848 outptr[k] = op(outptr[k], ptr[k]);
849 }
850 ptr += w;
851 }
852 }
853 }
854
855 return 0;
856 }
857
858 if (reduce_w && !reduce_h && !reduce_d && !reduce_c)
859 {
860 // w h d c -> X h d c
861 if (keepdims)
862 b.create(1, h, d, channels, elemsize, opt.blob_allocator);
863 else
864 b.create(h, d, channels, elemsize, opt.blob_allocator);
865
866 #pragma omp parallel for num_threads(opt.num_threads)
867 for (int q = 0; q < channels; q++)
868 {
869 const float* ptr = a.channel(q);
870 float* outptr = b.channel(q);
871
872 for (int i = 0; i < d * h; i++)
873 {
874 float sum = v0;
875 for (int j = 0; j < w; j++)
876 {
877 sum = op(sum, ptr[j]);
878 }
879 outptr[i] = sum;
880 ptr += w;
881 }
882 }
883
884 return 0;
885 }
886
887 if (!reduce_w && !reduce_h && !reduce_d && reduce_c)
888 {
889 // w h d c -> w h d X
890 if (keepdims)
891 b.create(w, h, d, 1, elemsize, opt.blob_allocator);
892 else
893 b.create(w, h, d, elemsize, opt.blob_allocator);
894
895 b.fill(v0);
896
897 for (int q = 0; q < channels; q++)
898 {
899 const float* ptr = a.channel(q);
900
901 for (int i = 0; i < d; i++)
902 {
903 Mat outm = keepdims ? b.depth(i) : b.channel(i);
904 for (int j = 0; j < h; j++)
905 {
906 float* outptr = outm.row(j);
907 for (int k = 0; k < w; k++)
908 {
909 outptr[k] = op(outptr[k], ptr[k]);
910 }
911 ptr += w;
912 }
913 }
914 }
915
916 return 0;
917 }
918
919 if (!reduce_w && reduce_h && !reduce_d && !reduce_c)
920 {
921 // w h d c -> w X d c
922 if (keepdims)
923 b.create(w, 1, d, channels, elemsize, opt.blob_allocator);
924 else
925 b.create(w, d, channels, elemsize, opt.blob_allocator);
926
927 #pragma omp parallel for num_threads(opt.num_threads)
928 for (int q = 0; q < channels; q++)
929 {
930 const float* ptr = a.channel(q);
931 Mat outm = b.channel(q);
932
933 outm.fill(v0);
934
935 for (int i = 0; i < d; i++)
936 {
937 float* outptr = outm.row(i);
938 for (int j = 0; j < h; j++)
939 {
940 for (int k = 0; k < w; k++)
941 {
942 outptr[k] = op(outptr[k], ptr[k]);
943 }
944 ptr += w;
945 }
946 }
947 }
948
949 return 0;
950 }
951
952 if (!reduce_w && !reduce_h && reduce_d && !reduce_c)
953 {
954 // w h d c -> w h X c
955 if (keepdims)
956 b.create(w, h, 1, channels, elemsize, opt.blob_allocator);
957 else
958 b.create(w, h, channels, elemsize, opt.blob_allocator);
959
960 #pragma omp parallel for num_threads(opt.num_threads)
961 for (int q = 0; q < channels; q++)
962 {
963 const float* ptr = a.channel(q);
964 Mat outm = b.channel(q);
965
966 outm.fill(v0);
967
968 for (int i = 0; i < d; i++)
969 {
970 for (int j = 0; j < h; j++)
971 {
972 float* outptr = outm.row(j);
973 for (int k = 0; k < w; k++)
974 {
975 outptr[k] = op(outptr[k], ptr[k]);
976 }
977 ptr += w;
978 }
979 }
980 }
981
982 return 0;
983 }
984 }
985
986 return 0;
987}
988
989template<typename MathOp>
990static int reduction_post_process(Mat& a, float coeff, const Option& opt)
991{
992 MathOp mathop;
993
994 int dims = a.dims;
995 if (dims == 1)
996 {
997 int w = a.w;
998
999 #pragma omp parallel for num_threads(opt.num_threads)
1000 for (int i = 0; i < w; i++)
1001 a[i] = mathop(a[i]) * coeff;
1002 }
1003 else if (dims == 2)
1004 {
1005 int size = a.w * a.h;
1006
1007 #pragma omp parallel for num_threads(opt.num_threads)
1008 for (int i = 0; i < size; i++)
1009 a[i] = mathop(a[i]) * coeff;
1010 }
1011 else if (dims == 3 || dims == 4)
1012 {
1013 int c = a.c;
1014 int size = a.w * a.h * a.d;
1015 if (c == 1)
1016 {
1017 #pragma omp parallel for num_threads(opt.num_threads)
1018 for (int i = 0; i < size; i++)
1019 a[i] = mathop(a[i]) * coeff;
1020 }
1021 else
1022 {
1023 #pragma omp parallel for num_threads(opt.num_threads)
1024 for (int q = 0; q < c; q++)
1025 {
1026 float* outptr = a.channel(q);
1027 for (int i = 0; i < size; i++)
1028 outptr[i] = mathop(outptr[i]) * coeff;
1029 }
1030 }
1031 }
1032
1033 return 0;
1034}
1035
1036template<typename Op, typename Op2, typename Op3>
1037static int reduction(const Mat& a, Mat& b, float v0, bool reduce_w, bool reduce_h, bool reduce_d, bool reduce_c, bool post_process, float coeff, int keepdims, const Option& opt)
1038{
1039 int ret = reduction_op<Op, Op2>(a, b, v0, reduce_w, reduce_h, reduce_d, reduce_c, keepdims, opt);
1040 if (ret != 0)
1041 return -100;
1042
1043 if (post_process || fabsf(coeff - 1.f) > FLT_EPSILON)
1044 {
1045 ret = reduction_post_process<Op3>(b, coeff, opt);
1046 if (ret != 0)
1047 return -100;
1048 }
1049
1050 return 0;
1051}
1052
1053template<typename T>
1054struct post_process_identity
1055{
1056 T operator()(const T& x) const
1057 {
1058 return x;
1059 }
1060};
1061
1062template<typename T>
1063struct post_process_sqrt
1064{
1065 T operator()(const T& x) const
1066 {
1067 // math optimization will probably generate rsqrt
1068 // that produce -inf on sse with subnormal input
1069 // flush subnormal input to zero as a workaround
1070 // TODO explicit use simd sqrt like unaryop --- nihui
1071 return static_cast<T>(sqrtf(x < FLT_MIN ? 0.f : x));
1072 }
1073};
1074
1075template<typename T>
1076struct post_process_log
1077{
1078 T operator()(const T& x) const
1079 {
1080 return static_cast<T>(logf(x));
1081 }
1082};
1083
1084template<typename T>
1085struct reduction_op_add
1086{
1087 T operator()(const T& x, const T& y) const
1088 {
1089 return x + y;
1090 }
1091};
1092
1093template<typename T>
1094struct reduction_op_mul
1095{
1096 T operator()(const T& x, const T& y) const
1097 {
1098 return x * y;
1099 }
1100};
1101
1102template<typename T>
1103struct reduction_op_asum
1104{
1105 T operator()(const T& x, const T& y) const
1106 {
1107 return static_cast<T>(x + fabsf(y));
1108 }
1109};
1110
1111template<typename T>
1112struct reduction_op_sumsq
1113{
1114 T operator()(const T& x, const T& y) const
1115 {
1116 return x + y * y;
1117 }
1118};
1119
1120template<typename T>
1121struct reduction_op_sumsexp
1122{
1123 T operator()(const T& x, const T& y) const
1124 {
1125 return static_cast<T>(x + expf(y));
1126 }
1127};
1128
1129template<typename T>
1130struct reduction_op_max
1131{
1132 T operator()(const T& x, const T& y) const
1133 {
1134 return std::max(x, y);
1135 }
1136};
1137
1138template<typename T>
1139struct reduction_op_min
1140{
1141 T operator()(const T& x, const T& y) const
1142 {
1143 return std::min(x, y);
1144 }
1145};
1146
1147int Reduction::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
1148{
1149 int dims = bottom_blob.dims;
1150 int axes_flag[4] = {0};
1151 bool reduce_w = false;
1152 bool reduce_h = false;
1153 bool reduce_d = false;
1154 bool reduce_c = false;
1155
1156 if (reduce_all)
1157 {
1158 reduce_w = true;
1159 reduce_h = true;
1160 reduce_d = true;
1161 reduce_c = true;
1162 }
1163 else
1164 {
1165 const int* axes_ptr = axes;
1166 int reduced_axes_num = axes.w;
1167
1168 for (int i = 0; i < reduced_axes_num; i++)
1169 {
1170 int axis = axes_ptr[i];
1171 // handle negative axis
1172 if (axis < 0)
1173 axis += dims;
1174 axes_flag[axis] = 1;
1175 }
1176
1177 if (dims == 1)
1178 {
1179 reduce_w = true;
1180 }
1181 else if (dims == 2)
1182 {
1183 if (axes_flag[0] == 1) reduce_h = true;
1184 if (axes_flag[1] == 1) reduce_w = true;
1185 }
1186 else if (dims == 3)
1187 {
1188 if (axes_flag[0] == 1) reduce_c = true;
1189 if (axes_flag[1] == 1) reduce_h = true;
1190 if (axes_flag[2] == 1) reduce_w = true;
1191 }
1192 else if (dims == 4)
1193 {
1194 if (axes_flag[0] == 1) reduce_c = true;
1195 if (axes_flag[1] == 1) reduce_d = true;
1196 if (axes_flag[2] == 1) reduce_h = true;
1197 if (axes_flag[3] == 1) reduce_w = true;
1198 }
1199 }
1200
1201 if (operation == ReductionOp_SUM)
1202 return reduction<reduction_op_add<float>, reduction_op_add<float>, post_process_identity<float> >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_d, reduce_c, false, coeff, keepdims, opt);
1203
1204 if (operation == ReductionOp_ASUM)
1205 return reduction<reduction_op_asum<float>, reduction_op_add<float>, post_process_identity<float> >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_d, reduce_c, false, coeff, keepdims, opt);
1206
1207 if (operation == ReductionOp_SUMSQ)
1208 return reduction<reduction_op_sumsq<float>, reduction_op_add<float>, post_process_identity<float> >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_d, reduce_c, false, coeff, keepdims, opt);
1209
1210 if (operation == ReductionOp_MEAN)
1211 {
1212 int scale = 1;
1213 int dims = bottom_blob.dims;
1214 if (dims == 1)
1215 {
1216 scale = bottom_blob.w;
1217 }
1218 else if (dims == 2)
1219 {
1220 if (reduce_w) scale *= bottom_blob.w;
1221 if (reduce_h) scale *= bottom_blob.h;
1222 }
1223 else if (dims == 3)
1224 {
1225 if (reduce_w) scale *= bottom_blob.w;
1226 if (reduce_h) scale *= bottom_blob.h;
1227 if (reduce_c) scale *= bottom_blob.c;
1228 }
1229 else if (dims == 4)
1230 {
1231 if (reduce_w) scale *= bottom_blob.w;
1232 if (reduce_h) scale *= bottom_blob.h;
1233 if (reduce_d) scale *= bottom_blob.d;
1234 if (reduce_c) scale *= bottom_blob.c;
1235 }
1236
1237 float coeff_mean = coeff / scale;
1238 return reduction<reduction_op_add<float>, reduction_op_add<float>, post_process_identity<float> >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_d, reduce_c, true, coeff_mean, keepdims, opt);
1239 }
1240
1241 if (operation == ReductionOp_MAX)
1242 return reduction<reduction_op_max<float>, reduction_op_max<float>, post_process_identity<float> >(bottom_blob, top_blob, -FLT_MAX, reduce_w, reduce_h, reduce_d, reduce_c, false, coeff, keepdims, opt);
1243
1244 if (operation == ReductionOp_MIN)
1245 return reduction<reduction_op_min<float>, reduction_op_min<float>, post_process_identity<float> >(bottom_blob, top_blob, FLT_MAX, reduce_w, reduce_h, reduce_d, reduce_c, false, coeff, keepdims, opt);
1246
1247 if (operation == ReductionOp_PROD)
1248 return reduction<reduction_op_mul<float>, reduction_op_mul<float>, post_process_identity<float> >(bottom_blob, top_blob, 1.f, reduce_w, reduce_h, reduce_d, reduce_c, false, coeff, keepdims, opt);
1249
1250 if (operation == ReductionOp_L1)
1251 return reduction<reduction_op_asum<float>, reduction_op_add<float>, post_process_identity<float> >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_d, reduce_c, false, 1.f, keepdims, opt);
1252
1253 if (operation == ReductionOp_L2)
1254 return reduction<reduction_op_sumsq<float>, reduction_op_add<float>, post_process_sqrt<float> >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_d, reduce_c, true, 1.f, keepdims, opt);
1255
1256 if (operation == ReductionOp_LogSum)
1257 return reduction<reduction_op_add<float>, reduction_op_add<float>, post_process_log<float> >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_d, reduce_c, true, 1.f, keepdims, opt);
1258
1259 if (operation == ReductionOp_LogSumExp)
1260 return reduction<reduction_op_sumsexp<float>, reduction_op_add<float>, post_process_log<float> >(bottom_blob, top_blob, 0.f, reduce_w, reduce_h, reduce_d, reduce_c, true, 1.f, keepdims, opt);
1261
1262 return 0;
1263}
1264
1265} // namespace ncnn