Elffuss BraidHub

kikocis/ncnn public

master · 3.5 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 "fold.h"
16
17namespace ncnn {
18
19Fold::Fold()
20{
21 one_blob_only = true;
22}
23
24int Fold::load_param(const ParamDict& pd)
25{
26 kernel_w = pd.get(1, 0);
27 kernel_h = pd.get(11, kernel_w);
28 dilation_w = pd.get(2, 1);
29 dilation_h = pd.get(12, dilation_w);
30 stride_w = pd.get(3, 1);
31 stride_h = pd.get(13, stride_w);
32 pad_left = pd.get(4, 0);
33 pad_right = pd.get(15, pad_left);
34 pad_top = pd.get(14, pad_left);
35 pad_bottom = pd.get(16, pad_top);
36 output_w = pd.get(20, 0);
37 output_h = pd.get(21, output_w);
38
39 return 0;
40}
41
42int Fold::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
43{
44 const int max_channels = bottom_blob.h;
45 size_t elemsize = bottom_blob.elemsize;
46
47 const int kernel_extent_w = dilation_w * (kernel_w - 1) + 1;
48 const int kernel_extent_h = dilation_h * (kernel_h - 1) + 1;
49
50 const int outw = output_w + pad_left + pad_right;
51 const int outh = output_h + pad_top + pad_bottom;
52
53 const int inw = (outw - kernel_extent_w) / stride_w + 1;
54 const int inh = (outh - kernel_extent_h) / stride_h + 1;
55
56 // assert inw * inh == size
57
58 const int maxk = kernel_w * kernel_h;
59 const int channels = max_channels / maxk;
60
61 Mat top_blob_bordered;
62 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0)
63 {
64 top_blob_bordered.create(outw, outh, channels, elemsize, opt.workspace_allocator);
65 }
66 else
67 {
68 top_blob_bordered = top_blob;
69 top_blob_bordered.create(outw, outh, channels, elemsize, opt.blob_allocator);
70 }
71 if (top_blob_bordered.empty())
72 return -100;
73
74 // col2im
75 const int gap = outw * stride_h - inw * stride_w;
76
77 #pragma omp parallel for num_threads(opt.num_threads)
78 for (int p = 0; p < channels; p++)
79 {
80 const float* sptr = bottom_blob.row(p * maxk);
81 Mat outm = top_blob_bordered.channel(p);
82
83 outm.fill(0.f);
84
85 for (int u = 0; u < kernel_h; u++)
86 {
87 for (int v = 0; v < kernel_w; v++)
88 {
89 float* ptr = outm.row(dilation_h * u) + dilation_w * v;
90
91 for (int i = 0; i < inh; i++)
92 {
93 for (int j = 0; j < inw; j++)
94 {
95 ptr[0] += sptr[0];
96
97 ptr += stride_w;
98 sptr += 1;
99 }
100
101 ptr += gap;
102 }
103 }
104 }
105 }
106
107 if (pad_left > 0 || pad_right > 0 || pad_top > 0 || pad_bottom > 0)
108 {
109 Option opt_b = opt;
110 opt_b.use_packing_layout = false;
111 copy_cut_border(top_blob_bordered, top_blob, pad_top, pad_bottom, pad_left, pad_right, opt_b);
112 if (top_blob.empty())
113 return -100;
114 }
115 else
116 {
117 top_blob = top_blob_bordered;
118 }
119
120 return 0;
121}
122
123} // namespace ncnn