Elffuss BraidHub

kikocis/ncnn public

master · 6.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_level2.h"
16
17namespace pnnx {
18
19class F_conv1d : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
2516 15
26pnnx.Input input_0 0 1 input
27pnnx.Input input_1 0 1 weight
28pnnx.Input input_2 0 1 bias
29pnnx.Input input_3 0 1 stride
30pnnx.Input input_4 0 1 padding
31pnnx.Input input_5 0 1 dilation
32pnnx.Input input_6 0 1 groups
33prim::Constant op_0 0 1 transposed value=False
34prim::Constant op_1 0 1 output_padding_w value=0
35prim::ListConstruct op_2 1 1 output_padding_w output_padding
36prim::Constant op_3 0 1 benchmark value=*
37prim::Constant op_4 0 1 deterministic value=*
38prim::Constant op_5 0 1 cudnn_enabled value=*
39prim::Constant op_6 0 1 allow_tf32 value=*
40aten::_convolution op_7 13 1 input weight bias stride padding dilation transposed output_padding groups benchmark deterministic cudnn_enabled allow_tf32 out
41pnnx.Output output 1 0 out
42)PNNXIR";
43 }
44
45 const char* type_str() const
46 {
47 return "F.conv1d";
48 }
49};
50
51REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv1d, 10)
52
53class F_convmode : public GraphRewriterPass
54{
55public:
56 const char* match_pattern_graph() const
57 {
58 return R"PNNXIR(7767517
599 8
60pnnx.Input input_0 0 1 input
61pnnx.Input input_1 0 1 weight
62pnnx.Input input_2 0 1 bias
63pnnx.Input input_3 0 1 stride
64pnnx.Input input_4 0 1 padding
65pnnx.Input input_5 0 1 dilation
66pnnx.Input input_6 0 1 groups
67aten::_convolution_mode op_0 7 1 input weight bias stride padding dilation groups out
68pnnx.Output output 1 0 out
69)PNNXIR";
70 }
71
72 const char* type_str() const
73 {
74 return "F.convmode";
75 }
76};
77
78REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_convmode, 10)
79
80class F_conv1d_onnx : public GraphRewriterPass
81{
82public:
83 const char* match_pattern_graph() const
84 {
85 return R"PNNXIR(7767517
865 4
87pnnx.Input input_0 0 1 input
88pnnx.Input input_1 0 1 weight
89pnnx.Input input_2 0 1 bias
90Conv op_0 3 1 input weight bias out %*=%*
91pnnx.Output output 1 0 out
92)PNNXIR";
93 }
94
95 const char* type_str() const
96 {
97 return "F.conv1d";
98 }
99
100 bool match(const std::map<std::string, Parameter>& captured_params) const
101 {
102 if (captured_params.find("op_0.kernel_shape") != captured_params.end())
103 {
104 if (captured_params.at("op_0.kernel_shape").type != 5 || captured_params.at("op_0.kernel_shape").ai.size() != 1)
105 return false;
106 }
107
108 if (captured_params.find("op_0.dilations") != captured_params.end())
109 {
110 if (captured_params.at("op_0.dilations").type != 5 || captured_params.at("op_0.dilations").ai.size() != 1)
111 return false;
112 }
113
114 if (captured_params.find("op_0.strides") != captured_params.end())
115 {
116 if (captured_params.at("op_0.strides").type != 5 || captured_params.at("op_0.strides").ai.size() != 1)
117 return false;
118 }
119
120 if (captured_params.find("op_0.pads") != captured_params.end())
121 {
122 if (captured_params.at("op_0.pads").type != 5 || captured_params.at("op_0.pads").ai.size() != 2)
123 return false;
124
125 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
126 if (pads[0] != pads[1])
127 return false;
128 }
129
130 return true;
131 }
132
133 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
134 {
135 if (captured_params.find("op_0.dilations") != captured_params.end())
136 {
137 op->params["dilation"] = captured_params.at("op_0.dilations");
138 }
139 else
140 {
141 op->params["dilation"] = {1};
142 }
143
144 if (captured_params.find("op_0.strides") != captured_params.end())
145 {
146 op->params["stride"] = captured_params.at("op_0.strides");
147 }
148 else
149 {
150 op->params["stride"] = {1};
151 }
152
153 if (captured_params.find("op_0.pads") != captured_params.end())
154 {
155 const std::vector<int>& pads = captured_params.at("op_0.pads").ai;
156 op->params["padding"] = {pads[0]};
157 }
158 else
159 {
160 op->params["padding"] = {0};
161 }
162
163 if (captured_params.find("op_0.group") != captured_params.end())
164 {
165 op->params["groups"] = captured_params.at("op_0.group");
166 }
167 else
168 {
169 op->params["groups"] = 1;
170 }
171 }
172};
173
174REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv1d_onnx, 10)
175
176class F_conv1d_onnx_1 : public F_conv1d_onnx
177{
178public:
179 const char* match_pattern_graph() const
180 {
181 return R"PNNXIR(7767517
1824 3
183pnnx.Input input_0 0 1 input
184pnnx.Input input_1 0 1 weight
185Conv op_0 2 1 input weight out %*=%*
186pnnx.Output output 1 0 out
187)PNNXIR";
188 }
189
190 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
191 {
192 F_conv1d_onnx::write(op, captured_params);
193
194 op->params["bias"] = Parameter();
195 }
196};
197
198REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_conv1d_onnx_1, 10)
199
200} // namespace pnnx