Elffuss BraidHub

kikocis/ncnn public

master · 6.3 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_ConvTranspose1d : 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.ConvTranspose1d 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 "Deconvolution1D";
37 }
38
39 const char* name_str() const
40 {
41 return "deconv1d";
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[0];
48 op->params["2"] = captured_params.at("dilation").ai[0];
49 op->params["3"] = captured_params.at("stride").ai[0];
50 op->params["4"] = captured_params.at("padding").ai[0];
51 op->params["18"] = captured_params.at("output_padding").ai[0];
52 op->params["5"] = captured_params.at("bias").b ? 1 : 0;
53 op->params["6"] = captured_attrs.at("op_0.weight").elemcount();
54
55 // transpose inch-outch-kw to outch-inch-kw
56 const int inch = captured_params.at("in_channels").i;
57 const int outch = captured_params.at("out_channels").i;
58 const int kw = captured_params.at("kernel_size").ai[0];
59 std::vector<float> new_weight;
60 {
61 auto w = captured_attrs.at("op_0.weight").get_float32_data();
62
63 new_weight.resize(outch * inch * kw);
64 float* w2 = (float*)new_weight.data();
65
66 // reorder weight from inch-outch to outch-inch
67 for (int i = 0; i < outch; i++)
68 {
69 for (int j = 0; j < inch; j++)
70 {
71 for (int k = 0; k < kw; k++)
72 {
73 w2[(i * inch + j) * kw + k] = w[(j * outch + i) * kw + k];
74 }
75 }
76 }
77 }
78
79 op->attrs["0"] = Attribute();
80 op->attrs["0"].data = {0, 0, 0, 0};
81 op->attrs["1"] = Attribute({outch, inch, kw}, new_weight);
82 if (captured_params.at("bias").b)
83 op->attrs["2"] = captured_attrs.at("op_0.bias");
84 }
85};
86
87class nn_ConvTranspose1d_1 : public GraphRewriterPass
88{
89public:
90 const char* match_pattern_graph() const
91 {
92 return R"PNNXIR(7767517
933 2
94pnnx.Input input 0 1 input
95nn.ConvTranspose1d 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
96pnnx.Output output 1 0 out
97)PNNXIR";
98 }
99
100 const char* type_str() const
101 {
102 return "DeconvolutionDepthWise1D";
103 }
104
105 const char* name_str() const
106 {
107 return "deconvdw1d";
108 }
109
110 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
111 {
112 op->params["0"] = captured_params.at("out_channels");
113 op->params["1"] = captured_params.at("kernel_size").ai[0];
114 op->params["2"] = captured_params.at("dilation").ai[0];
115 op->params["3"] = captured_params.at("stride").ai[0];
116 op->params["4"] = captured_params.at("padding").ai[0];
117 op->params["18"] = captured_params.at("output_padding").ai[0];
118 op->params["5"] = captured_params.at("bias").b ? 1 : 0;
119 op->params["6"] = captured_attrs.at("op_0.weight").elemcount();
120 op->params["7"] = captured_params.at("groups");
121
122 // transpose group-inch/group-outch/group-kw to group-outch/group-inch/group-kw
123 const int inch = captured_params.at("in_channels").i;
124 const int outch = captured_params.at("out_channels").i;
125 const int groups = captured_params.at("groups").i;
126 const int kw = captured_params.at("kernel_size").ai[0];
127 std::vector<float> new_weight;
128 {
129 auto w = captured_attrs.at("op_0.weight").get_float32_data();
130
131 new_weight.resize(outch / groups * inch * kw);
132 float* w2 = (float*)new_weight.data();
133 const int outch_g = outch / groups;
134 const int inch_g = inch / groups;
135
136 for (int g = 0; g < groups; g++)
137 {
138 // reorder weight from inch-outch to outch-inch
139 float* wg2 = w2 + g * outch_g * inch_g * kw;
140 const float* wg = (const float*)w.data() + g * inch_g * outch_g * kw;
141 for (int i = 0; i < outch_g; i++)
142 {
143 for (int j = 0; j < inch_g; j++)
144 {
145 for (int k = 0; k < kw; k++)
146 {
147 wg2[(i * inch_g + j) * kw + k] = wg[(j * outch_g + i) * kw + k];
148 }
149 }
150 }
151 }
152 }
153
154 op->attrs["0"] = Attribute();
155 op->attrs["0"].data = {0, 0, 0, 0};
156 op->attrs["1"] = Attribute({outch / groups, inch, kw}, new_weight);
157 if (captured_params.at("bias").b)
158 op->attrs["2"] = captured_attrs.at("op_0.bias");
159 }
160};
161
162REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_ConvTranspose1d, 20)
163REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_ConvTranspose1d_1, 21)
164
165} // namespace ncnn
166
167} // namespace pnnx