Elffuss BraidHub

kikocis/ncnn public

master · 8.0 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 "pass_ncnn.h"
16
17namespace pnnx {
18
19namespace ncnn {
20
21class nn_ConvTranspose3d : 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.ConvTranspose3d 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 "Deconvolution3D";
37 }
38
39 const char* name_str() const
40 {
41 return "deconv3d";
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[2];
48 op->params["11"] = captured_params.at("kernel_size").ai[1];
49 op->params["21"] = captured_params.at("kernel_size").ai[0];
50 op->params["2"] = captured_params.at("dilation").ai[2];
51 op->params["12"] = captured_params.at("dilation").ai[1];
52 op->params["22"] = captured_params.at("dilation").ai[0];
53 op->params["3"] = captured_params.at("stride").ai[2];
54 op->params["13"] = captured_params.at("stride").ai[1];
55 op->params["23"] = captured_params.at("stride").ai[0];
56 op->params["4"] = captured_params.at("padding").ai[2];
57 op->params["14"] = captured_params.at("padding").ai[1];
58 op->params["24"] = captured_params.at("padding").ai[0];
59 op->params["18"] = captured_params.at("output_padding").ai[2];
60 op->params["19"] = captured_params.at("output_padding").ai[1];
61 op->params["20"] = captured_params.at("output_padding").ai[0];
62 op->params["5"] = captured_params.at("bias").b ? 1 : 0;
63 op->params["6"] = captured_attrs.at("op_0.weight").elemcount();
64
65 // transpose inch-outch-kd-kh-kw to outch-inch-kd-kh-kw
66 const int inch = captured_params.at("in_channels").i;
67 const int outch = captured_params.at("out_channels").i;
68 const int kd = captured_params.at("kernel_size").ai[0];
69 const int kh = captured_params.at("kernel_size").ai[1];
70 const int kw = captured_params.at("kernel_size").ai[2];
71 std::vector<float> new_weight;
72 {
73 auto w = captured_attrs.at("op_0.weight").get_float32_data();
74
75 new_weight.resize(outch * inch * kd * kh * kw);
76 float* w2 = (float*)new_weight.data();
77 const int maxk = kd * kh * kw;
78
79 // reorder weight from inch-outch to outch-inch
80 for (int i = 0; i < outch; i++)
81 {
82 for (int j = 0; j < inch; j++)
83 {
84 for (int k = 0; k < maxk; k++)
85 {
86 w2[(i * inch + j) * maxk + k] = w[(j * outch + i) * maxk + k];
87 }
88 }
89 }
90 }
91
92 op->attrs["0"] = Attribute();
93 op->attrs["0"].data = {0, 0, 0, 0};
94 op->attrs["1"] = Attribute({outch, inch, kd, kh, kw}, new_weight);
95 if (captured_params.at("bias").b)
96 op->attrs["2"] = captured_attrs.at("op_0.bias");
97 }
98};
99
100class nn_ConvTranspose3d_1 : public GraphRewriterPass
101{
102public:
103 const char* match_pattern_graph() const
104 {
105 return R"PNNXIR(7767517
1063 2
107pnnx.Input input 0 1 input
108nn.ConvTranspose3d 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
109pnnx.Output output 1 0 out
110)PNNXIR";
111 }
112
113 const char* type_str() const
114 {
115 return "DeconvolutionDepthWise3D";
116 }
117
118 const char* name_str() const
119 {
120 return "deconvdw3d";
121 }
122
123 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
124 {
125 op->params["0"] = captured_params.at("out_channels");
126 op->params["1"] = captured_params.at("kernel_size").ai[2];
127 op->params["11"] = captured_params.at("kernel_size").ai[1];
128 op->params["21"] = captured_params.at("kernel_size").ai[0];
129 op->params["2"] = captured_params.at("dilation").ai[2];
130 op->params["12"] = captured_params.at("dilation").ai[1];
131 op->params["22"] = captured_params.at("dilation").ai[0];
132 op->params["3"] = captured_params.at("stride").ai[2];
133 op->params["13"] = captured_params.at("stride").ai[1];
134 op->params["23"] = captured_params.at("stride").ai[0];
135 op->params["4"] = captured_params.at("padding").ai[2];
136 op->params["14"] = captured_params.at("padding").ai[1];
137 op->params["24"] = captured_params.at("padding").ai[0];
138 op->params["18"] = captured_params.at("output_padding").ai[2];
139 op->params["19"] = captured_params.at("output_padding").ai[1];
140 op->params["20"] = captured_params.at("output_padding").ai[0];
141 op->params["5"] = captured_params.at("bias").b ? 1 : 0;
142 op->params["6"] = captured_attrs.at("op_0.weight").elemcount();
143 op->params["7"] = captured_params.at("groups");
144
145 // transpose group-inch/group-outch/group-kd-kh-kw to group-outch/group-inch/group-kd-kh-kw
146 const int inch = captured_params.at("in_channels").i;
147 const int outch = captured_params.at("out_channels").i;
148 const int groups = captured_params.at("groups").i;
149 const int kd = captured_params.at("kernel_size").ai[0];
150 const int kh = captured_params.at("kernel_size").ai[1];
151 const int kw = captured_params.at("kernel_size").ai[2];
152 std::vector<float> new_weight;
153 {
154 auto w = captured_attrs.at("op_0.weight").get_float32_data();
155
156 new_weight.resize(outch / groups * inch * kd * kh * kw);
157 float* w2 = (float*)new_weight.data();
158 const int outch_g = outch / groups;
159 const int inch_g = inch / groups;
160 const int maxk = kd * kh * kw;
161
162 for (int g = 0; g < groups; g++)
163 {
164 // reorder weight from inch-outch to outch-inch
165 float* wg2 = w2 + g * outch_g * inch_g * maxk;
166 const float* wg = (const float*)w.data() + g * inch_g * outch_g * maxk;
167 for (int i = 0; i < outch_g; i++)
168 {
169 for (int j = 0; j < inch_g; j++)
170 {
171 for (int k = 0; k < maxk; k++)
172 {
173 wg2[(i * inch_g + j) * maxk + k] = wg[(j * outch_g + i) * maxk + k];
174 }
175 }
176 }
177 }
178 }
179
180 op->attrs["0"] = Attribute();
181 op->attrs["0"].data = {0, 0, 0, 0};
182 op->attrs["1"] = Attribute({outch / groups, inch, kd, kh, kw}, new_weight);
183 if (captured_params.at("bias").b)
184 op->attrs["2"] = captured_attrs.at("op_0.bias");
185 }
186};
187
188REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_ConvTranspose3d, 20)
189REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_ConvTranspose3d_1, 21)
190
191} // namespace ncnn
192
193} // namespace pnnx