Elffuss BraidHub

kikocis/ncnn public

master · 7.4 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 "fuse_convolution1d_activation.h"
16
17#include "pass_level2.h"
18
19#include <float.h>
20
21namespace pnnx {
22
23namespace ncnn {
24
25class fuse_convolution1d_relu_pass : public GraphRewriterPass
26{
27public:
28 const char* match_pattern_graph() const
29 {
30 return R"PNNXIR(7767517
314 3
32pnnx.Input input 0 1 input
33Convolution1D op_0 1 1 input a %*=%*
34ReLU op_1 1 1 a out %*=%*
35pnnx.Output output 1 0 out
36)PNNXIR";
37 }
38
39 const char* type_str() const
40 {
41 return "Convolution1D";
42 }
43
44 const char* name_str() const
45 {
46 return "conv1drelu";
47 }
48
49 bool match_captured_params(const std::map<std::string, Parameter>& captured_params) const
50 {
51 return captured_params.find("op_0.9") == captured_params.end();
52 }
53
54 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
55 {
56 for (const auto& p : captured_params)
57 {
58 const std::string& pkey = p.first;
59 const Parameter& pp = p.second;
60
61 if (pkey.substr(0, 5) == "op_0.")
62 op->params[pkey.substr(5)] = pp;
63 }
64
65 for (const auto& a : captured_attrs)
66 {
67 const std::string& akey = a.first;
68 const Attribute& ap = a.second;
69
70 if (akey.substr(0, 5) == "op_0.")
71 op->attrs[akey.substr(5)] = ap;
72 }
73
74 float slope = 0.f;
75 if (captured_params.find("op_1.0") != captured_params.end())
76 {
77 slope = captured_params.at("op_1.0").f;
78 }
79
80 if (slope == 0.f)
81 {
82 op->params["9"] = 1;
83 }
84 else
85 {
86 op->params["9"] = 2;
87 op->params["10"] = Parameter{slope};
88 }
89 }
90};
91
92class fuse_convolution1d_clip_pass : public GraphRewriterPass
93{
94public:
95 const char* match_pattern_graph() const
96 {
97 return R"PNNXIR(7767517
984 3
99pnnx.Input input 0 1 input
100Convolution1D op_0 1 1 input a %*=%*
101Clip op_1 1 1 a out %*=%*
102pnnx.Output output 1 0 out
103)PNNXIR";
104 }
105
106 const char* type_str() const
107 {
108 return "Convolution1D";
109 }
110
111 const char* name_str() const
112 {
113 return "conv1dclip";
114 }
115
116 bool match_captured_params(const std::map<std::string, Parameter>& captured_params) const
117 {
118 return captured_params.find("op_0.9") == captured_params.end();
119 }
120
121 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
122 {
123 for (const auto& p : captured_params)
124 {
125 const std::string& pkey = p.first;
126 const Parameter& pp = p.second;
127
128 if (pkey.substr(0, 5) == "op_0.")
129 op->params[pkey.substr(5)] = pp;
130 }
131
132 for (const auto& a : captured_attrs)
133 {
134 const std::string& akey = a.first;
135 const Attribute& ap = a.second;
136
137 if (akey.substr(0, 5) == "op_0.")
138 op->attrs[akey.substr(5)] = ap;
139 }
140
141 float min = -FLT_MAX;
142 float max = FLT_MAX;
143 if (captured_params.find("op_1.0") != captured_params.end())
144 {
145 min = captured_params.at("op_1.0").f;
146 }
147 if (captured_params.find("op_1.1") != captured_params.end())
148 {
149 max = captured_params.at("op_1.1").f;
150 }
151
152 op->params["9"] = 3;
153 op->params["10"] = Parameter{min, max};
154 }
155};
156
157class fuse_convolution1d_sigmoid_pass : public GraphRewriterPass
158{
159public:
160 const char* match_pattern_graph() const
161 {
162 return R"PNNXIR(7767517
1634 3
164pnnx.Input input 0 1 input
165Convolution1D op_0 1 1 input a %*=%*
166Sigmoid op_1 1 1 a out
167pnnx.Output output 1 0 out
168)PNNXIR";
169 }
170
171 const char* type_str() const
172 {
173 return "Convolution1D";
174 }
175
176 const char* name_str() const
177 {
178 return "conv1dsigmoid";
179 }
180
181 bool match_captured_params(const std::map<std::string, Parameter>& captured_params) const
182 {
183 return captured_params.find("op_0.9") == captured_params.end();
184 }
185
186 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
187 {
188 for (const auto& p : captured_params)
189 {
190 const std::string& pkey = p.first;
191 const Parameter& pp = p.second;
192
193 if (pkey.substr(0, 5) == "op_0.")
194 op->params[pkey.substr(5)] = pp;
195 }
196
197 for (const auto& a : captured_attrs)
198 {
199 const std::string& akey = a.first;
200 const Attribute& ap = a.second;
201
202 if (akey.substr(0, 5) == "op_0.")
203 op->attrs[akey.substr(5)] = ap;
204 }
205
206 op->params["9"] = 4;
207 }
208};
209
210class fuse_convolution1d_mish_pass : public GraphRewriterPass
211{
212public:
213 const char* match_pattern_graph() const
214 {
215 return R"PNNXIR(7767517
2164 3
217pnnx.Input input 0 1 input
218Convolution1D op_0 1 1 input a %*=%*
219Mish op_1 1 1 a out
220pnnx.Output output 1 0 out
221)PNNXIR";
222 }
223
224 const char* type_str() const
225 {
226 return "Convolution1D";
227 }
228
229 const char* name_str() const
230 {
231 return "conv1dmish";
232 }
233
234 bool match_captured_params(const std::map<std::string, Parameter>& captured_params) const
235 {
236 return captured_params.find("op_0.9") == captured_params.end();
237 }
238
239 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
240 {
241 for (const auto& p : captured_params)
242 {
243 const std::string& pkey = p.first;
244 const Parameter& pp = p.second;
245
246 if (pkey.substr(0, 5) == "op_0.")
247 op->params[pkey.substr(5)] = pp;
248 }
249
250 for (const auto& a : captured_attrs)
251 {
252 const std::string& akey = a.first;
253 const Attribute& ap = a.second;
254
255 if (akey.substr(0, 5) == "op_0.")
256 op->attrs[akey.substr(5)] = ap;
257 }
258
259 op->params["9"] = 5;
260 }
261};
262
263void fuse_convolution1d_activation(Graph& graph)
264{
265 fuse_convolution1d_relu_pass a;
266 fuse_convolution1d_clip_pass b;
267 fuse_convolution1d_sigmoid_pass c;
268 fuse_convolution1d_mish_pass d;
269 int opindex = 0;
270
271 pnnx_graph_rewrite(graph, &a, opindex);
272 pnnx_graph_rewrite(graph, &b, opindex);
273 pnnx_graph_rewrite(graph, &c, opindex);
274 pnnx_graph_rewrite(graph, &d, opindex);
275}
276
277} // namespace ncnn
278
279} // namespace pnnx