Elffuss BraidHub

kikocis/ncnn public

master · 7.2 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2021 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 "pass_ncnn.h"
16
17namespace pnnx {
18
19namespace ncnn {
20
21class nn_ConvTranspose2d : public GraphRewriterPass
22{
23public:
24 const char* match_pattern_graph() const
25 {
26 return R"PNNXIR(7767517
273 2
28pnnx.Input input 0 1 input
29nn.ConvTranspose2d op_0 1 1 input out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding groups=1 bias=%bias @weight @bias
30pnnx.Output output 1 0 out
31)PNNXIR";
32 }
33
34 const char* type_str() const
35 {
36 return "Deconvolution";
37 }
38
39 const char* name_str() const
40 {
41 return "deconv";
42 }
43
44 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
45 {
46 op->params["0"] = captured_params.at("out_channels");
47 op->params["1"] = captured_params.at("kernel_size").ai[1];
48 op->params["11"] = captured_params.at("kernel_size").ai[0];
49 op->params["2"] = captured_params.at("dilation").ai[1];
50 op->params["12"] = captured_params.at("dilation").ai[0];
51 op->params["3"] = captured_params.at("stride").ai[1];
52 op->params["13"] = captured_params.at("stride").ai[0];
53 op->params["4"] = captured_params.at("padding").ai[1];
54 op->params["14"] = captured_params.at("padding").ai[0];
55 op->params["18"] = captured_params.at("output_padding").ai[1];
56 op->params["19"] = captured_params.at("output_padding").ai[0];
57 op->params["5"] = captured_params.at("bias").b ? 1 : 0;
58 op->params["6"] = captured_attrs.at("op_0.weight").elemcount();
59
60 // transpose inch-outch-kh-kw to outch-inch-kh-kw
61 const int inch = captured_params.at("in_channels").i;
62 const int outch = captured_params.at("out_channels").i;
63 const int kh = captured_params.at("kernel_size").ai[0];
64 const int kw = captured_params.at("kernel_size").ai[1];
65 std::vector<float> new_weight;
66 {
67 auto w = captured_attrs.at("op_0.weight").get_float32_data();
68
69 new_weight.resize(outch * inch * kh * kw);
70 float* w2 = (float*)new_weight.data();
71 const int maxk = kh * kw;
72
73 // reorder weight from inch-outch to outch-inch
74 for (int i = 0; i < outch; i++)
75 {
76 for (int j = 0; j < inch; j++)
77 {
78 for (int k = 0; k < maxk; k++)
79 {
80 w2[(i * inch + j) * maxk + k] = w[(j * outch + i) * maxk + k];
81 }
82 }
83 }
84 }
85
86 op->attrs["0"] = Attribute();
87 op->attrs["0"].data = {0, 0, 0, 0};
88 op->attrs["1"] = Attribute({outch, inch, kh, kw}, new_weight);
89 if (captured_params.at("bias").b)
90 op->attrs["2"] = captured_attrs.at("op_0.bias");
91 }
92};
93
94class nn_ConvTranspose2d_1 : public GraphRewriterPass
95{
96public:
97 const char* match_pattern_graph() const
98 {
99 return R"PNNXIR(7767517
1003 2
101pnnx.Input input 0 1 input
102nn.ConvTranspose2d op_0 1 1 input out in_channels=%in_channels out_channels=%out_channels kernel_size=%kernel_size stride=%stride padding=%padding dilation=%dilation output_padding=%output_padding groups=%groups bias=%bias @weight @bias
103pnnx.Output output 1 0 out
104)PNNXIR";
105 }
106
107 const char* type_str() const
108 {
109 return "DeconvolutionDepthWise";
110 }
111
112 const char* name_str() const
113 {
114 return "deconvdw";
115 }
116
117 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
118 {
119 op->params["0"] = captured_params.at("out_channels");
120 op->params["1"] = captured_params.at("kernel_size").ai[1];
121 op->params["11"] = captured_params.at("kernel_size").ai[0];
122 op->params["2"] = captured_params.at("dilation").ai[1];
123 op->params["12"] = captured_params.at("dilation").ai[0];
124 op->params["3"] = captured_params.at("stride").ai[1];
125 op->params["13"] = captured_params.at("stride").ai[0];
126 op->params["4"] = captured_params.at("padding").ai[1];
127 op->params["14"] = captured_params.at("padding").ai[0];
128 op->params["18"] = captured_params.at("output_padding").ai[1];
129 op->params["19"] = captured_params.at("output_padding").ai[0];
130 op->params["5"] = captured_params.at("bias").b ? 1 : 0;
131 op->params["6"] = captured_attrs.at("op_0.weight").elemcount();
132 op->params["7"] = captured_params.at("groups");
133
134 // transpose group-inch/group-outch/group-kh-kw to group-outch/group-inch/group-kh-kw
135 const int inch = captured_params.at("in_channels").i;
136 const int outch = captured_params.at("out_channels").i;
137 const int groups = captured_params.at("groups").i;
138 const int kh = captured_params.at("kernel_size").ai[0];
139 const int kw = captured_params.at("kernel_size").ai[1];
140 std::vector<float> new_weight;
141 {
142 auto w = captured_attrs.at("op_0.weight").get_float32_data();
143
144 new_weight.resize(outch / groups * inch * kh * kw);
145 float* w2 = (float*)new_weight.data();
146 const int outch_g = outch / groups;
147 const int inch_g = inch / groups;
148 const int maxk = kh * kw;
149
150 for (int g = 0; g < groups; g++)
151 {
152 // reorder weight from inch-outch to outch-inch
153 float* wg2 = w2 + g * outch_g * inch_g * maxk;
154 const float* wg = (const float*)w.data() + g * inch_g * outch_g * maxk;
155 for (int i = 0; i < outch_g; i++)
156 {
157 for (int j = 0; j < inch_g; j++)
158 {
159 for (int k = 0; k < maxk; k++)
160 {
161 wg2[(i * inch_g + j) * maxk + k] = wg[(j * outch_g + i) * maxk + k];
162 }
163 }
164 }
165 }
166 }
167
168 op->attrs["0"] = Attribute();
169 op->attrs["0"].data = {0, 0, 0, 0};
170 op->attrs["1"] = Attribute({outch / groups, inch, kh, kw}, new_weight);
171 if (captured_params.at("bias").b)
172 op->attrs["2"] = captured_attrs.at("op_0.bias");
173 }
174};
175
176REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_ConvTranspose2d, 20)
177REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_ConvTranspose2d_1, 21)
178
179} // namespace ncnn
180
181} // namespace pnnx