Elffuss BraidHub

kikocis/ncnn public

master · 4.8 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2024 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_level2.h"
16
17namespace pnnx {
18
19class F_conv_transpose2d_onnx : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
255 4
26pnnx.Input input_0 0 1 input
27pnnx.Input input_1 0 1 weight
28pnnx.Input input_2 0 1 bias
29ConvTranspose op_0 3 1 input weight bias out %*=%*
30pnnx.Output output 1 0 out
31)PNNXIR";
32 }
33
34 const char* type_str() const
35 {
36 return "F.conv_transpose2d";
37 }
38
39 bool match(const std::map<std::string, Parameter>& captured_params) const
40 {
41 if (captured_params.find("op_0.kernel_shape") != captured_params.end())
42 {
43 if (captured_params.at("op_0.kernel_shape").type != 5 || captured_params.at("op_0.kernel_shape").ai.size() != 2)
44 return false;
45 }
46
47 if (captured_params.find("op_0.dilations") != captured_params.end())
48 {
49 if (captured_params.at("op_0.dilations").type != 5 || captured_params.at("op_0.dilations").ai.size() != 2)
50 return false;
51 }
52
53 if (captured_params.find("op_0.strides") != captured_params.end())
54 {
55 if (captured_params.at("op_0.strides").type != 5 || captured_params.at("op_0.strides").ai.size() != 2)
56 return false;
57 }
58
59 if (captured_params.find("op_0.output_padding") != captured_params.end())
60 {
61 if (captured_params.at("op_0.output_padding").type != 5 || captured_params.at("op_0.output_padding").ai.size() != 2)
62 return false;
63 }
64
65 if (captured_params.find("op_0.pads") != captured_params.end())
66 {
67 if (captured_params.at("op_0.pads").type != 5 || captured_params.at("op_0.pads").ai.size() != 4)
68 return false;
69
70 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
71 if (pads[0] != pads[2] || pads[1] != pads[3])
72 return false;
73 }
74
75 return true;
76 }
77
78 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
79 {
80 if (captured_params.find("op_0.dilations") != captured_params.end())
81 {
82 op->params["dilation"] = captured_params.at("op_0.dilations");
83 }
84 else
85 {
86 op->params["dilation"] = {1, 1};
87 }
88
89 if (captured_params.find("op_0.strides") != captured_params.end())
90 {
91 op->params["stride"] = captured_params.at("op_0.strides");
92 }
93 else
94 {
95 op->params["stride"] = {1, 1};
96 }
97
98 if (captured_params.find("op_0.output_padding") != captured_params.end())
99 {
100 op->params["output_padding"] = captured_params.at("op_0.output_padding");
101 }
102 else
103 {
104 op->params["output_padding"] = {0, 0};
105 }
106
107 if (captured_params.find("op_0.pads") != captured_params.end())
108 {
109 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
110 op->params["padding"] = {pads[0], pads[1]};
111 }
112 else
113 {
114 op->params["padding"] = {0, 0};
115 }
116
117 if (captured_params.find("op_0.group") != captured_params.end())
118 {
119 op->params["groups"] = captured_params.at("op_0.group");
120 }
121 else
122 {
123 op->params["groups"] = 1;
124 }
125 }
126};
127
128REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv_transpose2d_onnx, 10)
129
130class F_conv_transpose2d_onnx_1 : public F_conv_transpose2d_onnx
131{
132public:
133 const char* match_pattern_graph() const
134 {
135 return R"PNNXIR(7767517
1364 3
137pnnx.Input input_0 0 1 input
138pnnx.Input input_1 0 1 weight
139ConvTranspose op_0 2 1 input weight out %*=%*
140pnnx.Output output 1 0 out
141)PNNXIR";
142 }
143
144 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
145 {
146 F_conv_transpose2d_onnx::write(op, captured_params);
147
148 op->params["bias"] = Parameter();
149 }
150};
151
152REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv_transpose2d_onnx_1, 10)
153
154} // namespace pnnx