Elffuss BraidHub

kikocis/ncnn public

master · 10.6 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
17#if _MSC_VER
18#define _USE_MATH_DEFINES
19#include <math.h>
20#endif
21
22namespace pnnx {
23
24class F_gelu : public GraphRewriterPass
25{
26public:
27 const char* match_pattern_graph() const
28 {
29 return R"PNNXIR(7767517
303 2
31pnnx.Input input 0 1 input
32aten::gelu op_0 1 1 input out
33pnnx.Output output 1 0 out
34)PNNXIR";
35 }
36
37 const char* type_str() const
38 {
39 return "F.gelu";
40 }
41};
42
43REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu, 10)
44
45class F_gelu_1 : public GraphRewriterPass
46{
47public:
48 const char* match_pattern_graph() const
49 {
50 return R"PNNXIR(7767517
514 3
52pnnx.Input input_0 0 1 input
53prim::Constant op_0 0 1 approximate value=%approximate
54aten::gelu op_1 2 1 input approximate out
55pnnx.Output output 1 0 out
56)PNNXIR";
57 }
58
59 const char* type_str() const
60 {
61 return "F.gelu";
62 }
63
64 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
65 {
66 const std::string& approximate = captured_params.at("approximate").s;
67 if (approximate != "none")
68 op->params["approximate"] = approximate;
69 }
70};
71
72REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_1, 10)
73
74class F_gelu_1_1 : public GraphRewriterPass
75{
76public:
77 const char* match_pattern_graph() const
78 {
79 return R"PNNXIR(7767517
803 2
81pnnx.Input input 0 1 input
82aten::gelu_approximate_none op_0 1 1 input out
83pnnx.Output output 1 0 out
84)PNNXIR";
85 }
86
87 const char* type_str() const
88 {
89 return "F.gelu";
90 }
91};
92
93REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_1_1, 10)
94
95class F_gelu_2 : public GraphRewriterPass
96{
97public:
98 // x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0)))
99 const char* match_pattern_graph() const
100 {
101 return R"PNNXIR(7767517
10211 10
103pnnx.Input input_0 0 1 input
104prim::Constant op_0 0 1 12 value=%0p5
105aten::mul op_1 2 1 input 12 13
106prim::Constant op_2 0 1 15 value=%sqrt2
107aten::div op_3 2 1 input 15 16
108aten::erf op_4 1 1 16 17
109prim::Constant op_5 0 1 20 value=%1
110prim::Constant op_6 0 1 21 value=1
111aten::add op_7 3 1 17 20 21 22
112aten::mul op_8 2 1 13 22 out
113pnnx.Output output 1 0 out
114)PNNXIR";
115 }
116
117 bool match(const std::map<std::string, Parameter>& captured_params) const
118 {
119 if (captured_params.at("0p5").f != 0.5f)
120 return false;
121
122 if (fabs(captured_params.at("sqrt2").f - sqrt(2.f)) > 0.0001f)
123 return false;
124
125 if ((captured_params.at("1").type == 2 && captured_params.at("1").i != 1) || (captured_params.at("1").type == 3 && captured_params.at("1").f != 1.f))
126 return false;
127
128 return true;
129 }
130
131 const char* type_str() const
132 {
133 return "F.gelu";
134 }
135
136 void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const
137 {
138 }
139};
140
141REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_2, 9)
142
143class F_gelu_3 : public GraphRewriterPass
144{
145public:
146 // 0.5 * x * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi) * (x + 0.044715 * torch.pow(x, 3.0))))
147 const char* match_pattern_graph() const
148 {
149 return R"PNNXIR(7767517
15017 16
151pnnx.Input input_0 0 1 input
152prim::Constant op_0 0 1 60 value=%0p5
153aten::mul op_1 2 1 input 60 26
154prim::Constant op_2 0 1 28 value=%3
155aten::pow op_3 2 1 input 28 29
156prim::Constant op_4 0 1 30 value=%0p044715
157aten::mul op_5 2 1 29 30 31
158prim::Constant op_6 0 1 61 value=1
159aten::add op_7 3 1 input 31 61 35
160prim::Constant op_8 0 1 36 value=%sqrt2dpi
161aten::mul op_9 2 1 35 36 37
162aten::tanh op_10 1 1 37 39
163prim::Constant op_11 0 1 62 value=%1
164prim::Constant op_12 0 1 63 value=%1_1
165aten::add op_13 3 1 39 62 63 42
166aten::mul op_14 2 1 26 42 out
167pnnx.Output output 1 0 out
168)PNNXIR";
169 }
170
171 bool match(const std::map<std::string, Parameter>& captured_params) const
172 {
173 if (captured_params.at("0p5").f != 0.5f)
174 return false;
175
176 if (fabs(captured_params.at("0p044715").f - 0.044715f) > 0.0001f)
177 return false;
178
179 if (fabs(captured_params.at("sqrt2dpi").f - sqrt(2.f / M_PI)) > 0.0001f)
180 return false;
181
182 if ((captured_params.at("1").type == 2 && captured_params.at("1").i != 1) || (captured_params.at("1").type == 3 && captured_params.at("1").f != 1.f))
183 return false;
184
185 if ((captured_params.at("3").type == 2 && captured_params.at("3").i != 3) || (captured_params.at("3").type == 3 && captured_params.at("3").f != 3.f))
186 return false;
187
188 if ((captured_params.at("1_1").type == 2 && captured_params.at("1_1").i != 1) || (captured_params.at("1_1").type == 3 && captured_params.at("1_1").f != 1.f))
189 return false;
190
191 return true;
192 }
193
194 const char* type_str() const
195 {
196 return "F.gelu";
197 }
198
199 void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const
200 {
201 }
202};
203
204REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_3, 9)
205
206class F_gelu_4 : public GraphRewriterPass
207{
208public:
209 // (x * 0.5) * (tanh((x + (torch.pow(x, 3.0) * 0.044715)) * 7.978846e-01) + 1)
210 const char* match_pattern_graph() const
211 {
212 return R"PNNXIR(7767517
21315 14
214pnnx.Input input 0 1 input
215prim::Constant op_0 0 1 zp5 value=0.5
216aten::mul op_1 2 1 input zp5 pnnx_90
217prim::Constant op_2 0 1 three value=%3
218aten::pow op_3 2 1 input three pnnx_91
219prim::Constant op_4 0 1 zp044715 value=%0p044715
220aten::mul op_5 2 1 pnnx_91 zp044715 pnnx_92
221aten::add op_6 2 1 input pnnx_92 pnnx_93
222prim::Constant op_7 0 1 sqrt2dpi value=%sqrt2dpi
223aten::mul op_8 2 1 pnnx_93 sqrt2dpi pnnx_94
224aten::tanh op_9 1 1 pnnx_94 pnnx_95
225prim::Constant op_10 0 1 one value=%1
226aten::add op_11 2 1 pnnx_95 one pnnx_96
227aten::mul op_12 2 1 pnnx_90 pnnx_96 out
228pnnx.Output output 1 0 out
229)PNNXIR";
230 }
231
232 bool match(const std::map<std::string, Parameter>& captured_params) const
233 {
234 if (fabs(captured_params.at("0p044715").f - 0.044715f) > 0.0001f)
235 return false;
236
237 if (fabs(captured_params.at("sqrt2dpi").f - sqrt(2.f / M_PI)) > 0.0001f)
238 return false;
239
240 if ((captured_params.at("1").type == 2 && captured_params.at("1").i != 1) || (captured_params.at("1").type == 3 && captured_params.at("1").f != 1.f))
241 return false;
242
243 if ((captured_params.at("3").type == 2 && captured_params.at("3").i != 3) || (captured_params.at("3").type == 3 && captured_params.at("3").f != 3.f))
244 return false;
245
246 return true;
247 }
248
249 const char* type_str() const
250 {
251 return "F.gelu";
252 }
253
254 void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const
255 {
256 }
257};
258
259REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_4, 9)
260
261class F_gelu_onnx : public GraphRewriterPass
262{
263public:
264 // x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0)))
265 const char* match_pattern_graph() const
266 {
267 return R"PNNXIR(7767517
26810 9
269pnnx.Input input 0 1 input
270prim::Constant op_0 0 1 sqrt2 value=%sqrt2
271aten::div op_1 2 1 input sqrt2 16
272aten::erf op_2 1 1 16 17
273prim::Constant op_3 0 1 one value=%1
274aten::add op_4 2 1 17 one 22
275aten::mul op_5 2 1 input 22 25
276prim::Constant op_6 0 1 0p5 value=%0p5
277aten::mul op_7 2 1 25 0p5 out
278pnnx.Output output 1 0 out
279)PNNXIR";
280 }
281
282 bool match(const std::map<std::string, Parameter>& captured_params) const
283 {
284 if (captured_params.at("0p5").f != 0.5f)
285 return false;
286
287 if (fabs(captured_params.at("sqrt2").f - sqrt(2.f)) > 0.0001f)
288 return false;
289
290 if (captured_params.at("1").f != 1.f)
291 return false;
292
293 return true;
294 }
295
296 const char* type_str() const
297 {
298 return "F.gelu";
299 }
300
301 void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const
302 {
303 }
304};
305
306REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_onnx, 9)
307
308class F_gelu_onnx_1 : public F_gelu_onnx
309{
310public:
311 // (x * 0.5) * (1.0 + torch.erf(x / math.sqrt(2.0)))
312 const char* match_pattern_graph() const
313 {
314 return R"PNNXIR(7767517
31510 9
316pnnx.Input input 0 1 input
317prim::Constant op_0 0 1 0p5 value=%0p5
318aten::mul op_1 2 1 input 0p5 15
319prim::Constant op_2 0 1 sqrt2 value=%sqrt2
320aten::div op_3 2 1 input sqrt2 16
321aten::erf op_4 1 1 16 17
322prim::Constant op_5 0 1 one value=%1
323aten::add op_6 2 1 17 one 22
324aten::mul op_7 2 1 15 22 out
325pnnx.Output output 1 0 out
326)PNNXIR";
327 }
328};
329
330REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_onnx_1, 9)
331
332class F_gelu_onnx_2 : public GraphRewriterPass
333{
334public:
335 const char* match_pattern_graph() const
336 {
337 return R"PNNXIR(7767517
3383 2
339pnnx.Input input 0 1 input
340Gelu op_0 1 1 input out approximate=%approximate
341pnnx.Output output 1 0 out
342)PNNXIR";
343 }
344
345 const char* type_str() const
346 {
347 return "F.gelu";
348 }
349};
350
351REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_onnx_2, 10)
352
353} // namespace pnnx