Elffuss BraidHub

kikocis/ncnn public

pnnx convert onnx prelu gelu elu leakyrelu relu6 celu hardshrink hardsigmoid hardswish clip (#5572)

Changesetb9a5dfbe2a9cd5714a1a8fbc5058caacf804f44026ae6ba00fbcf3004a678e85
Authornihui <nihuini@tencent.com> · 2024-07-09 11:26 +08:00
CommitterGitHub <noreply@github.com> · 2024-07-09 11:26 +08:00
Parentsba1d2642f155

Browse the files at this changeset

35 files changed

modified tools/pnnx/src/CMakeLists.txt +1 −0
@@ -382,6 +382,7 @@
382382 pass_level5/fuse_static_instancenorm.cpp
383383 pass_level5/fuse_static_layernorm.cpp
384384 pass_level5/fuse_static_linear.cpp
385+ pass_level5/fuse_static_prelu.cpp
385386 pass_level5/normalize_einsum_equation.cpp
386387 pass_level5/unroll_rnn_op.cpp
387388 )
modified tools/pnnx/src/pass_level2/F_celu.cpp +21 −0
@@ -38,4 +38,25 @@
3838
3939 REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_celu, 10)
4040
41+class F_celu_onnx : public GraphRewriterPass
42+{
43+public:
44+ const char* match_pattern_graph() const
45+ {
46+ return R"PNNXIR(7767517
47+3 2
48+pnnx.Input input 0 1 input
49+Celu op_0 1 1 input out alpha=%alpha
50+pnnx.Output output 1 0 out
51+)PNNXIR";
52+ }
53+
54+ const char* type_str() const
55+ {
56+ return "F.celu";
57+ }
58+};
59+
60+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_celu_onnx, 10)
61+
4162 } // namespace pnnx
modified tools/pnnx/src/pass_level2/F_elu.cpp +21 −0
@@ -40,4 +40,25 @@
4040
4141 REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_elu, 10)
4242
43+class F_elu_onnx : public GraphRewriterPass
44+{
45+public:
46+ const char* match_pattern_graph() const
47+ {
48+ return R"PNNXIR(7767517
49+3 2
50+pnnx.Input input 0 1 input
51+Elu op_0 1 1 input out alpha=%alpha
52+pnnx.Output output 1 0 out
53+)PNNXIR";
54+ }
55+
56+ const char* type_str() const
57+ {
58+ return "F.elu";
59+ }
60+};
61+
62+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_elu_onnx, 10)
63+
4364 } // namespace pnnx
modified tools/pnnx/src/pass_level2/F_gelu.cpp +45 −0
@@ -305,4 +305,49 @@
305305
306306 REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_onnx, 9)
307307
308+class F_gelu_onnx_1 : public F_gelu_onnx
309+{
310+public:
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
315+10 9
316+pnnx.Input input 0 1 input
317+prim::Constant op_0 0 1 0p5 value=%0p5
318+aten::mul op_1 2 1 input 0p5 15
319+prim::Constant op_2 0 1 sqrt2 value=%sqrt2
320+aten::div op_3 2 1 input sqrt2 16
321+aten::erf op_4 1 1 16 17
322+prim::Constant op_5 0 1 one value=%1
323+aten::add op_6 2 1 17 one 22
324+aten::mul op_7 2 1 15 22 out
325+pnnx.Output output 1 0 out
326+)PNNXIR";
327+ }
328+};
329+
330+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_onnx_1, 9)
331+
332+class F_gelu_onnx_2 : public GraphRewriterPass
333+{
334+public:
335+ const char* match_pattern_graph() const
336+ {
337+ return R"PNNXIR(7767517
338+3 2
339+pnnx.Input input 0 1 input
340+Gelu op_0 1 1 input out approximate=%approximate
341+pnnx.Output output 1 0 out
342+)PNNXIR";
343+ }
344+
345+ const char* type_str() const
346+ {
347+ return "F.gelu";
348+ }
349+};
350+
351+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_gelu_onnx_2, 10)
352+
308353 } // namespace pnnx
modified tools/pnnx/src/pass_level2/F_hardshrink.cpp +52 −0
@@ -38,4 +38,56 @@
3838
3939 REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardshrink, 10)
4040
41+static bool NearlyEqual(float a, float b, float epsilon)
42+{
43+ if (a == b)
44+ return true;
45+
46+ float diff = (float)fabs(a - b);
47+ if (diff <= epsilon)
48+ return true;
49+
50+ // relative error
51+ return diff < epsilon * std::max(fabs(a), fabs(b));
52+}
53+
54+class F_hardshrink_1 : public GraphRewriterPass
55+{
56+public:
57+ const char* match_pattern_graph() const
58+ {
59+ return R"PNNXIR(7767517
60+9 8
61+pnnx.Input input 0 1 input
62+prim::Constant op_0 0 1 a value=%lambd
63+aten::gt op_1 2 1 input a aa
64+prim::Constant op_2 0 1 b value=%lambd2
65+aten::lt op_3 2 1 input b bb
66+aten::__or__ op_4 2 1 aa bb ab
67+prim::Constant op_5 0 1 zero value=0
68+aten::where op_6 3 1 ab input zero out
69+pnnx.Output output 1 0 out
70+)PNNXIR";
71+ }
72+
73+ const char* type_str() const
74+ {
75+ return "F.hardshrink";
76+ }
77+
78+ bool match(const std::map<std::string, Parameter>& captured_params) const
79+ {
80+ float lambd = captured_params.at("lambd").f;
81+ float lambd2 = captured_params.at("lambd2").f;
82+ return NearlyEqual(lambd, -lambd2, 0.001);
83+ }
84+
85+ void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
86+ {
87+ op->params["lambd"] = captured_params.at("lambd");
88+ }
89+};
90+
91+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardshrink_1, 10)
92+
4193 } // namespace pnnx
modified tools/pnnx/src/pass_level2/F_hardsigmoid.cpp +22 −0
@@ -65,6 +65,28 @@
6565
6666 REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardsigmoid_2, 9)
6767
68+class F_hardsigmoid_2_1 : public F_hardsigmoid_2
69+{
70+public:
71+ const char* match_pattern_graph() const
72+ {
73+ return R"PNNXIR(7767517
74+9 8
75+pnnx.Input input 0 1 input
76+prim::Constant op_0 0 1 410 value=3
77+aten::add op_1 2 1 input 410 a
78+prim::Constant op_2 0 1 413 value=0
79+prim::Constant op_3 0 1 414 value=6
80+aten::clamp op_4 3 1 a 413 414 b
81+prim::Constant op_5 0 1 409 value=6
82+aten::div op_6 2 1 b 409 out
83+pnnx.Output output 1 0 out
84+)PNNXIR";
85+ }
86+};
87+
88+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardsigmoid_2_1, 9)
89+
6890 class F_hardsigmoid_3 : public GraphRewriterPass
6991 {
7092 public:
modified tools/pnnx/src/pass_level2/F_hardswish.cpp +68 −0
@@ -146,6 +146,29 @@
146146
147147 REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_4, 8)
148148
149+class F_hardswish_4_1 : public F_hardswish_4
150+{
151+public:
152+ const char* match_pattern_graph() const
153+ {
154+ return R"PNNXIR(7767517
155+10 9
156+pnnx.Input input 0 1 input
157+prim::Constant op_0 0 1 25 value=3
158+aten::add op_2 2 1 input 25 a
159+prim::Constant op_3 0 1 48 value=0
160+prim::Constant op_4 0 1 49 value=6
161+aten::clamp op_5 3 1 a 48 49 b
162+prim::Constant op_6 0 1 50 value=6
163+aten::div op_7 2 1 b 50 c
164+aten::mul op_8 2 1 c input out
165+pnnx.Output output 1 0 out
166+)PNNXIR";
167+ }
168+};
169+
170+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_4_1, 8)
171+
149172 class F_hardswish_5 : public GraphRewriterPass
150173 {
151174 public:
@@ -249,4 +272,49 @@
249272
250273 REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_onnx, 10)
251274
275+static bool NearlyEqual(float a, float b, float epsilon)
276+{
277+ if (a == b)
278+ return true;
279+
280+ float diff = (float)fabs(a - b);
281+ if (diff <= epsilon)
282+ return true;
283+
284+ // relative error
285+ return diff < epsilon * std::max(fabs(a), fabs(b));
286+}
287+
288+class F_hardswish_onnx_1 : public GraphRewriterPass
289+{
290+public:
291+ const char* match_pattern_graph() const
292+ {
293+ return R"PNNXIR(7767517
294+4 3
295+pnnx.Input input 0 1 input
296+HardSigmoid op_0 1 1 input h alpha=%alpha
297+aten::mul op_1 2 1 input h out
298+pnnx.Output output 1 0 out
299+)PNNXIR";
300+ }
301+
302+ const char* type_str() const
303+ {
304+ return "F.hardswish";
305+ }
306+
307+ bool match(const std::map<std::string, Parameter>& captured_params) const
308+ {
309+ float alpha = captured_params.at("alpha").f;
310+ return NearlyEqual(alpha, 1.f / 6, 0.001);
311+ }
312+
313+ void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const
314+ {
315+ }
316+};
317+
318+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_hardswish_onnx_1, 9)
319+
252320 } // namespace pnnx
modified tools/pnnx/src/pass_level2/F_leaky_relu.cpp +21 −0
@@ -38,4 +38,25 @@
3838
3939 REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_leaky_relu, 10)
4040
41+class F_leaky_relu_onnx : public GraphRewriterPass
42+{
43+public:
44+ const char* match_pattern_graph() const
45+ {
46+ return R"PNNXIR(7767517
47+3 2
48+pnnx.Input input 0 1 input
49+LeakyRelu op_0 1 1 input out alpha=%negative_slope
50+pnnx.Output output 1 0 out
51+)PNNXIR";
52+ }
53+
54+ const char* type_str() const
55+ {
56+ return "F.leaky_relu";
57+ }
58+};
59+
60+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_leaky_relu_onnx, 10)
61+
4162 } // namespace pnnx
modified tools/pnnx/src/pass_level2/F_prelu.cpp +128 −0
@@ -38,4 +38,132 @@
3838
3939 REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_prelu, 10)
4040
41+class F_prelu_onnx : public GraphRewriterPass
42+{
43+public:
44+ const char* match_pattern_graph() const
45+ {
46+ // clang-format off
47+ // *INDENT-OFF*
48+
49+ return R"PNNXIR(7767517
50+4 3
51+pnnx.Input input_0 0 1 input
52+pnnx.Input input_1 0 1 weight #weight=(?)f32
53+PRelu op_0 2 1 input weight out
54+pnnx.Output output 1 0 out
55+)PNNXIR";
56+
57+ // *INDENT-ON*
58+ // clang-format on
59+ }
60+
61+ const char* type_str() const
62+ {
63+ return "F.prelu";
64+ }
65+};
66+
67+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_prelu_onnx, 10)
68+
69+class F_prelu_onnx_1 : public GraphRewriterPass
70+{
71+public:
72+ const char* match_pattern_graph() const
73+ {
74+ return R"PNNXIR(7767517
75+4 3
76+pnnx.Input input 0 1 input
77+pnnx.Attribute slope 0 1 weight @data
78+PRelu op_0 2 1 input weight out
79+pnnx.Output output 1 0 out
80+)PNNXIR";
81+ }
82+
83+ const char* replace_pattern_graph() const
84+ {
85+ return R"PNNXIR(7767517
86+4 3
87+pnnx.Input input 0 1 input
88+pnnx.Attribute slope 0 1 weight @data=%slope.data
89+F.prelu prelu 2 1 input weight out
90+pnnx.Output output 1 0 out
91+)PNNXIR";
92+ }
93+
94+ void write(const std::map<std::string, Operator*>& ops, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
95+ {
96+ GraphRewriterPass::write(ops, captured_params, captured_attrs);
97+
98+ Operator* op_slope = ops.at("slope");
99+
100+ // hack slope shape
101+ int num_slope = op_slope->attrs["data"].shape[0];
102+ op_slope->attrs["data"].shape = {num_slope};
103+
104+ op_slope->outputs[0]->shape = {num_slope};
105+ op_slope->outputs[0]->type = op_slope->attrs["data"].type;
106+
107+ Operator* op_prelu = ops.at("prelu");
108+ op_prelu->inputnames = {"input", "weight"};
109+ }
110+};
111+
112+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_prelu_onnx_1, 10)
113+
114+class F_prelu_onnx_2 : public GraphRewriterPass
115+{
116+public:
117+ const char* match_pattern_graph() const
118+ {
119+ // clang-format off
120+ // *INDENT-OFF*
121+
122+ return R"PNNXIR(7767517
123+5 4
124+pnnx.Input input_0 0 1 input
125+pnnx.Input input_1 0 1 weight #weight=(?)f32
126+Unsqueeze uqz 1 1 weight w2 axes=%axes
127+PRelu op_0 2 1 input w2 out
128+pnnx.Output output 1 0 out
129+)PNNXIR";
130+
131+ // *INDENT-ON*
132+ // clang-format on
133+ }
134+
135+ const char* type_str() const
136+ {
137+ return "F.prelu";
138+ }
139+
140+ bool match(const std::map<std::string, Parameter>& captured_params) const
141+ {
142+ if (captured_params.at("axes").type == 5)
143+ {
144+ // 1 2 ... N
145+ const std::vector<int>& axes = captured_params.at("axes").ai;
146+ for (int i = 0; i < (int)axes.size(); i++)
147+ {
148+ if (axes[i] != i + 1)
149+ return false;
150+ }
151+ }
152+ else
153+ {
154+ int axes = captured_params.at("axes").i;
155+ if (axes != 1)
156+ return false;
157+ }
158+
159+ return true;
160+ }
161+
162+ void write(Operator* /*op*/, const std::map<std::string, Parameter>& /*captured_params*/) const
163+ {
164+ }
165+};
166+
167+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_prelu_onnx_2, 10)
168+
41169 } // namespace pnnx
modified tools/pnnx/src/pass_level2/torch_clamp.cpp +27 −0
@@ -60,4 +60,31 @@
6060
6161 REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_clamp_onnx, 20)
6262
63+class torch_clamp_onnx_1 : public GraphRewriterPass
64+{
65+public:
66+ const char* match_pattern_graph() const
67+ {
68+ return R"PNNXIR(7767517
69+3 2
70+pnnx.Input input 0 1 input
71+aten::clamp op_0 1 1 input out max=%max
72+pnnx.Output output 1 0 out
73+)PNNXIR";
74+ }
75+
76+ const char* type_str() const
77+ {
78+ return "torch.clamp";
79+ }
80+
81+ void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
82+ {
83+ op->params["min"] = 0.f;
84+ op->params["max"] = captured_params.at("max");
85+ }
86+};
87+
88+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(torch_clamp_onnx_1, 20)
89+
6390 } // namespace pnnx
modified tools/pnnx/src/pass_level5.cpp +2 −0
@@ -59,6 +59,7 @@
5959 #include "pass_level5/fuse_static_instancenorm.h"
6060 #include "pass_level5/fuse_static_layernorm.h"
6161 #include "pass_level5/fuse_static_linear.h"
62+#include "pass_level5/fuse_static_prelu.h"
6263 #include "pass_level5/normalize_einsum_equation.h"
6364 #include "pass_level4/dead_code_elimination.h"
6465 #include "pass_level4/canonicalize.h"
@@ -106,6 +107,7 @@
106107 fuse_static_convtranspose(g);
107108 fuse_static_linear(g);
108109 fuse_static_embedding(g);
110+ fuse_static_prelu(g);
109111
110112 fuse_conv1d_batchnorm1d(g);
111113 fuse_conv2d_batchnorm2d(g);
added tools/pnnx/src/pass_level5/fuse_static_prelu.cpp +57 −0
@@ -0,0 +1,57 @@
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 "fuse_static_prelu.h"
16+
17+#include "pass_level2.h"
18+
19+#include <math.h>
20+#include <string.h>
21+
22+namespace pnnx {
23+
24+class fuse_static_Fprelu_pass : public GraphRewriterPass
25+{
26+public:
27+ const char* match_pattern_graph() const
28+ {
29+ return R"PNNXIR(7767517
30+4 3
31+pnnx.Input input 0 1 input
32+pnnx.Attribute op_weight 0 1 weight @data=(%num_parameters)f32
33+F.prelu op_0 2 1 input weight out
34+pnnx.Output output 1 0 out
35+)PNNXIR";
36+ }
37+
38+ const char* replace_pattern_graph() const
39+ {
40+ return R"PNNXIR(7767517
41+3 2
42+pnnx.Input input 0 1 input
43+nn.PReLU prelu 1 1 input out num_parameters=%num_parameters @weight=%op_weight.data
44+pnnx.Output output 1 0 out
45+)PNNXIR";
46+ }
47+};
48+
49+void fuse_static_prelu(Graph& graph)
50+{
51+ fuse_static_Fprelu_pass a;
52+ int opindex = 0;
53+
54+ pnnx_graph_rewrite(graph, &a, opindex);
55+}
56+
57+} // namespace pnnx
added tools/pnnx/src/pass_level5/fuse_static_prelu.h +21 −0
@@ -0,0 +1,21 @@
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 "ir.h"
16+
17+namespace pnnx {
18+
19+void fuse_static_prelu(Graph& graph);
20+
21+} // namespace pnnx
modified tools/pnnx/src/pass_onnx.cpp +16 −0
@@ -812,12 +812,28 @@
812812
813813 bool is_attr_weight = false;
814814 {
815+ if (sim_op_type == "BatchNormalization" && (j == 1 || j == 2 || j == 3 || j == 4))
816+ is_attr_weight = true;
815817 if (sim_op_type == "Conv" && (j == 1 || j == 2))
816818 is_attr_weight = true;
817819 if (sim_op_type == "ConvTranspose" && (j == 1 || j == 2))
818820 is_attr_weight = true;
821+ if (sim_op_type == "Gather" && j == 0)
822+ is_attr_weight = true;
823+ if (sim_op_type == "GroupNormalization" && (j == 1 || j == 2))
824+ is_attr_weight = true;
825+ if (sim_op_type == "GRU" && (j == 1 || j == 2 || j == 3 || j == 5))
826+ is_attr_weight = true;
819827 if (sim_op_type == "InstanceNormalization" && (j == 1 || j == 2))
820828 is_attr_weight = true;
829+ if (sim_op_type == "LayerNormalization" && (j == 1 || j == 2))
830+ is_attr_weight = true;
831+ if (sim_op_type == "LSTM" && (j == 1 || j == 2 || j == 3 || j == 5 || j == 6))
832+ is_attr_weight = true;
833+ if (sim_op_type == "PRelu" && j == 1)
834+ is_attr_weight = true;
835+ if (sim_op_type == "RNN" && (j == 1 || j == 2 || j == 3 || j == 5))
836+ is_attr_weight = true;
821837 }
822838
823839 int64_t numel = 1;
modified tools/pnnx/tests/onnx/CMakeLists.txt +14 −0
@@ -9,23 +9,30 @@
99 pnnx_onnx_add_test(F_avg_pool2d)
1010 pnnx_onnx_add_test(F_avg_pool3d)
1111 pnnx_onnx_add_test(F_batch_norm)
12+pnnx_onnx_add_test(F_celu)
1213 pnnx_onnx_add_test(F_conv_transpose1d)
1314 pnnx_onnx_add_test(F_conv_transpose2d)
1415 pnnx_onnx_add_test(F_conv_transpose3d)
1516 pnnx_onnx_add_test(F_conv1d)
1617 pnnx_onnx_add_test(F_conv2d)
1718 pnnx_onnx_add_test(F_conv3d)
19+pnnx_onnx_add_test(F_elu)
20+pnnx_onnx_add_test(F_gelu)
1821 # pnnx_onnx_add_test(F_group_norm)
1922 # pnnx_onnx_add_test(F_instance_norm)
2023 pnnx_onnx_add_test(F_interpolate)
2124 pnnx_onnx_add_test(F_layer_norm)
25+pnnx_onnx_add_test(F_leaky_relu)
2226 pnnx_onnx_add_test(F_linear)
2327 pnnx_onnx_add_test(F_local_response_norm)
2428 pnnx_onnx_add_test(F_max_pool1d)
2529 pnnx_onnx_add_test(F_max_pool2d)
2630 pnnx_onnx_add_test(F_max_pool3d)
2731 pnnx_onnx_add_test(F_pad)
32+pnnx_onnx_add_test(F_prelu)
2833 pnnx_onnx_add_test(F_relu)
34+pnnx_onnx_add_test(F_relu6)
35+# pnnx_onnx_add_test(F_scaled_dot_product_attention)
2936 pnnx_onnx_add_test(F_sigmoid)
3037 pnnx_onnx_add_test(F_softmax)
3138 pnnx_onnx_add_test(F_upsample_bilinear)
@@ -38,6 +45,7 @@
3845 pnnx_onnx_add_test(nn_BatchNorm1d)
3946 pnnx_onnx_add_test(nn_BatchNorm2d)
4047 pnnx_onnx_add_test(nn_BatchNorm3d)
48+pnnx_onnx_add_test(nn_CELU)
4149 pnnx_onnx_add_test(nn_ConstantPad1d)
4250 pnnx_onnx_add_test(nn_ConstantPad2d)
4351 pnnx_onnx_add_test(nn_ConstantPad3d)
@@ -47,21 +55,27 @@
4755 pnnx_onnx_add_test(nn_ConvTranspose1d)
4856 pnnx_onnx_add_test(nn_ConvTranspose2d)
4957 pnnx_onnx_add_test(nn_ConvTranspose3d)
58+pnnx_onnx_add_test(nn_ELU)
59+pnnx_onnx_add_test(nn_GELU)
5060 pnnx_onnx_add_test(nn_GroupNorm)
5161 pnnx_onnx_add_test(nn_GRU)
5262 pnnx_onnx_add_test(nn_InstanceNorm1d)
5363 pnnx_onnx_add_test(nn_InstanceNorm2d)
5464 pnnx_onnx_add_test(nn_InstanceNorm3d)
5565 pnnx_onnx_add_test(nn_LayerNorm)
66+pnnx_onnx_add_test(nn_LeakyReLU)
5667 pnnx_onnx_add_test(nn_Linear)
5768 pnnx_onnx_add_test(nn_LocalResponseNorm)
5869 pnnx_onnx_add_test(nn_LSTM)
5970 pnnx_onnx_add_test(nn_MaxPool1d)
6071 pnnx_onnx_add_test(nn_MaxPool2d)
6172 pnnx_onnx_add_test(nn_MaxPool3d)
73+# pnnx_onnx_add_test(nn_MultiheadAttention)
74+pnnx_onnx_add_test(nn_PReLU)
6275 pnnx_onnx_add_test(nn_ReflectionPad1d)
6376 pnnx_onnx_add_test(nn_ReflectionPad2d)
6477 pnnx_onnx_add_test(nn_ReLU)
78+pnnx_onnx_add_test(nn_ReLU6)
6579 pnnx_onnx_add_test(nn_ReplicationPad1d)
6680 pnnx_onnx_add_test(nn_ReplicationPad2d)
6781 pnnx_onnx_add_test(nn_ReplicationPad3d)
added tools/pnnx/tests/onnx/test_F_celu.py +70 −0
@@ -0,0 +1,70 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+from packaging import version
19+
20+class Model(nn.Module):
21+ def __init__(self):
22+ super(Model, self).__init__()
23+
24+ def forward(self, x, y, z, w):
25+ x = x * 2 - 1
26+ y = y * 2 - 1
27+ z = z * 2 - 1
28+ w = w * 2 - 1
29+ x = F.celu(x)
30+ y = F.celu(y, 0.8)
31+ z = F.celu(z, 0.5)
32+ w = F.celu(w, 2)
33+ return x, y, z, w
34+
35+def test():
36+ if version.parse(torch.__version__) < version.parse('1.12'):
37+ return True
38+
39+ net = Model()
40+ net.eval()
41+
42+ torch.manual_seed(0)
43+ x = torch.rand(1, 16)
44+ y = torch.rand(12, 2, 16)
45+ z = torch.rand(1, 3, 12, 16)
46+ w = torch.rand(1, 5, 7, 9, 11)
47+
48+ a = net(x, y, z, w)
49+
50+ # export onnx
51+ torch.onnx.export(net, (x, y, z, w), "test_F_celu.onnx")
52+
53+ # onnx to pnnx
54+ import os
55+ os.system("../../src/pnnx test_F_celu.onnx inputshape=[1,16],[12,2,16],[1,3,12,16],[1,5,7,9,11]")
56+
57+ # pnnx inference
58+ import test_F_celu_pnnx
59+ b = test_F_celu_pnnx.test_inference()
60+
61+ for a0, b0 in zip(a, b):
62+ if not torch.equal(a0, b0):
63+ return False
64+ return True
65+
66+if __name__ == "__main__":
67+ if test():
68+ exit(0)
69+ else:
70+ exit(1)
added tools/pnnx/tests/onnx/test_F_elu.py +66 −0
@@ -0,0 +1,66 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ def forward(self, x, y, z, w):
24+ x = x * 2 - 1
25+ y = y * 2 - 1
26+ z = z * 2 - 1
27+ w = w * 2 - 1
28+ x = F.elu(x)
29+ y = F.elu(y, 1.2)
30+ z = F.elu(z, -0.6)
31+ w = F.elu(w, 0)
32+ return x, y, z, w
33+
34+def test():
35+ net = Model()
36+ net.eval()
37+
38+ torch.manual_seed(0)
39+ x = torch.rand(1, 16)
40+ y = torch.rand(12, 2, 16)
41+ z = torch.rand(1, 3, 12, 16)
42+ w = torch.rand(1, 5, 7, 9, 11)
43+
44+ a = net(x, y, z, w)
45+
46+ # export onnx
47+ torch.onnx.export(net, (x, y, z, w), "test_F_elu.onnx")
48+
49+ # onnx to pnnx
50+ import os
51+ os.system("../../src/pnnx test_F_elu.onnx inputshape=[1,16],[12,2,16],[1,3,12,16],[1,5,7,9,11]")
52+
53+ # pnnx inference
54+ import test_F_elu_pnnx
55+ b = test_F_elu_pnnx.test_inference()
56+
57+ for a0, b0 in zip(a, b):
58+ if not torch.equal(a0, b0):
59+ return False
60+ return True
61+
62+if __name__ == "__main__":
63+ if test():
64+ exit(0)
65+ else:
66+ exit(1)
added tools/pnnx/tests/onnx/test_F_gelu.py +73 −0
@@ -0,0 +1,73 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+import math
19+
20+def gelu_forward_0(x):
21+ return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0)))
22+
23+def gelu_forward_1(x):
24+ return 0.5 * x * (1.0 + torch.tanh(math.sqrt(2.0 / math.pi) * (x + 0.044715 * torch.pow(x, 3.0))))
25+
26+class Model(nn.Module):
27+ def __init__(self):
28+ super(Model, self).__init__()
29+
30+ def forward(self, x, y, z, w):
31+ x = x * 2 - 1
32+ y = y * 2 - 1
33+ z = z * 2 - 1
34+ w = w * 2 - 1
35+ x = F.gelu(x)
36+ y = F.gelu(y)
37+ z = gelu_forward_0(z)
38+ w = gelu_forward_1(w)
39+ return x, y, z, w
40+
41+def test():
42+ net = Model()
43+ net.eval()
44+
45+ torch.manual_seed(0)
46+ x = torch.rand(1, 16)
47+ y = torch.rand(12, 2, 16)
48+ z = torch.rand(1, 3, 12, 16)
49+ w = torch.rand(1, 5, 7, 9, 11)
50+
51+ a = net(x, y, z, w)
52+
53+ # export onnx
54+ torch.onnx.export(net, (x, y, z, w), "test_F_gelu.onnx")
55+
56+ # onnx to pnnx
57+ import os
58+ os.system("../../src/pnnx test_F_gelu.onnx inputshape=[1,16],[12,2,16],[1,3,12,16],[1,5,7,9,11]")
59+
60+ # pnnx inference
61+ import test_F_gelu_pnnx
62+ b = test_F_gelu_pnnx.test_inference()
63+
64+ for a0, b0 in zip(a, b):
65+ if not torch.allclose(a0, b0, 1e-3, 1e-3):
66+ return False
67+ return True
68+
69+if __name__ == "__main__":
70+ if test():
71+ exit(0)
72+ else:
73+ exit(1)
added tools/pnnx/tests/onnx/test_F_hardshrink.py +66 −0
@@ -0,0 +1,66 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ def forward(self, x, y, z, w):
24+ x = x * 2 - 1
25+ y = y * 2 - 1
26+ z = z * 2 - 1
27+ w = w * 2 - 1
28+ x = F.hardshrink(x)
29+ y = F.hardshrink(y, 0.1)
30+ z = F.hardshrink(z, 0.22)
31+ w = F.hardshrink(w, 0)
32+ return x, y, z, w
33+
34+def test():
35+ net = Model()
36+ net.eval()
37+
38+ torch.manual_seed(0)
39+ x = torch.rand(1, 16)
40+ y = torch.rand(12, 2, 16)
41+ z = torch.rand(1, 3, 12, 16)
42+ w = torch.rand(1, 5, 7, 9, 11)
43+
44+ a = net(x, y, z, w)
45+
46+ # export onnx
47+ torch.onnx.export(net, (x, y, z, w), "test_F_hardshrink.onnx")
48+
49+ # onnx to pnnx
50+ import os
51+ os.system("../../src/pnnx test_F_hardshrink.onnx inputshape=[1,16],[12,2,16],[1,3,12,16],[1,5,7,9,11]")
52+
53+ # pnnx inference
54+ import test_F_hardshrink_pnnx
55+ b = test_F_hardshrink_pnnx.test_inference()
56+
57+ for a0, b0 in zip(a, b):
58+ if not torch.equal(a0, b0):
59+ return False
60+ return True
61+
62+if __name__ == "__main__":
63+ if test():
64+ exit(0)
65+ else:
66+ exit(1)
added tools/pnnx/tests/onnx/test_F_hardsigmoid.py +83 −0
@@ -0,0 +1,83 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+def hardsigmoid_forward_0(x):
20+ return F.relu6(x + 3., True) / 6.
21+
22+def hardsigmoid_forward_1(x):
23+ return x.add_(3.).clamp_(0., 6.).div_(6.)
24+
25+class h_sigmoid(nn.Module):
26+ def __init__(self, inplace=True):
27+ super(h_sigmoid, self).__init__()
28+ self.relu = nn.ReLU6(inplace=inplace)
29+
30+ def forward(self, x):
31+ return self.relu(x + 3) / 6
32+
33+class Model(nn.Module):
34+ def __init__(self):
35+ super(Model, self).__init__()
36+
37+ self.h_sigmoid = h_sigmoid();
38+
39+ def forward(self, x, y, z, w):
40+ x = x * 2 - 1
41+ y = y * 2 - 1
42+ z = z * 2 - 1
43+ w = w * 2 - 1
44+ x = F.hardsigmoid(x)
45+ y = F.hardsigmoid(y)
46+ z = self.h_sigmoid(z)
47+ w = hardsigmoid_forward_0(w)
48+ w = hardsigmoid_forward_1(w)
49+ return x, y, z, w
50+
51+def test():
52+ net = Model()
53+ net.eval()
54+
55+ torch.manual_seed(0)
56+ x = torch.rand(1, 16)
57+ y = torch.rand(12, 2, 16)
58+ z = torch.rand(1, 3, 12, 16)
59+ w = torch.rand(1, 5, 7, 9, 11)
60+
61+ a = net(x, y, z, w)
62+
63+ # export onnx
64+ torch.onnx.export(net, (x, y, z, w), "test_F_hardsigmoid.onnx")
65+
66+ # onnx to pnnx
67+ import os
68+ os.system("../../src/pnnx test_F_hardsigmoid.onnx inputshape=[1,16],[12,2,16],[1,3,12,16],[1,5,7,9,11]")
69+
70+ # pnnx inference
71+ import test_F_hardsigmoid_pnnx
72+ b = test_F_hardsigmoid_pnnx.test_inference()
73+
74+ for a0, b0 in zip(a, b):
75+ if not torch.allclose(a0, b0, 1e-4, 1e-4):
76+ return False
77+ return True
78+
79+if __name__ == "__main__":
80+ if test():
81+ exit(0)
82+ else:
83+ exit(1)
added tools/pnnx/tests/onnx/test_F_hardswish.py +80 −0
@@ -0,0 +1,80 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+def hardswish_forward_0(x):
20+ return x * F.hardsigmoid(x)
21+
22+def hardswish_forward_1(x):
23+ return x * F.hardtanh(x + 3, 0., 6.) / 6.
24+
25+def hardswish_forward_2(x):
26+ out = F.relu6(x + 3., True) / 6.
27+ return out * x
28+
29+def hardswish_forward_3(x):
30+ return x * F.relu6(x + 3, inplace=True) / 6
31+
32+class Model(nn.Module):
33+ def __init__(self):
34+ super(Model, self).__init__()
35+
36+ def forward(self, x, y, z, w):
37+ x = x * 2 - 1
38+ y = y * 2 - 1
39+ z = z * 2 - 1
40+ w = w * 2 - 1
41+ x = F.hardswish(x)
42+ y = hardswish_forward_0(y)
43+ z = hardswish_forward_1(z)
44+ w = hardswish_forward_2(w)
45+ w = hardswish_forward_3(w)
46+ return x, y, z, w
47+
48+def test():
49+ net = Model()
50+ net.eval()
51+
52+ torch.manual_seed(0)
53+ x = torch.rand(1, 16)
54+ y = torch.rand(12, 2, 16)
55+ z = torch.rand(1, 3, 12, 16)
56+ w = torch.rand(1, 5, 7, 9, 11)
57+
58+ a = net(x, y, z, w)
59+
60+ # export onnx
61+ torch.onnx.export(net, (x, y, z, w), "test_F_hardswish.onnx")
62+
63+ # onnx to pnnx
64+ import os
65+ os.system("../../src/pnnx test_F_hardswish.onnx inputshape=[1,16],[12,2,16],[1,3,12,16],[1,5,7,9,11]")
66+
67+ # pnnx inference
68+ import test_F_hardswish_pnnx
69+ b = test_F_hardswish_pnnx.test_inference()
70+
71+ for a0, b0 in zip(a, b):
72+ if not torch.allclose(a0, b0, 1e-4, 1e-4):
73+ return False
74+ return True
75+
76+if __name__ == "__main__":
77+ if test():
78+ exit(0)
79+ else:
80+ exit(1)
added tools/pnnx/tests/onnx/test_F_hardtanh.py +66 −0
@@ -0,0 +1,66 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ def forward(self, x, y, z, w):
24+ x = x * 2 - 1
25+ y = y * 2 - 1
26+ z = z * 2 - 1
27+ w = w * 2 - 1
28+ x = F.hardtanh(x)
29+ y = F.hardtanh(y, -1, 1)
30+ z = F.hardtanh(z, -0.1, 0.1)
31+ w = F.hardtanh(w, 0.1, 0.3)
32+ return x, y, z, w
33+
34+def test():
35+ net = Model()
36+ net.eval()
37+
38+ torch.manual_seed(0)
39+ x = torch.rand(1, 16)
40+ y = torch.rand(12, 2, 16)
41+ z = torch.rand(1, 3, 12, 16)
42+ w = torch.rand(1, 5, 7, 9, 11)
43+
44+ a = net(x, y, z, w)
45+
46+ # export onnx
47+ torch.onnx.export(net, (x, y, z, w), "test_F_hardtanh.onnx")
48+
49+ # onnx to pnnx
50+ import os
51+ os.system("../../src/pnnx test_F_hardtanh.onnx inputshape=[1,16],[12,2,16],[1,3,12,16],[1,5,7,9,11]")
52+
53+ # pnnx inference
54+ import test_F_hardtanh_pnnx
55+ b = test_F_hardtanh_pnnx.test_inference()
56+
57+ for a0, b0 in zip(a, b):
58+ if not torch.equal(a0, b0):
59+ return False
60+ return True
61+
62+if __name__ == "__main__":
63+ if test():
64+ exit(0)
65+ else:
66+ exit(1)
added tools/pnnx/tests/onnx/test_F_leaky_relu.py +66 −0
@@ -0,0 +1,66 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ def forward(self, x, y, z, w):
24+ x = x * 2 - 1
25+ y = y * 2 - 1
26+ z = z * 2 - 1
27+ w = w * 2 - 1
28+ x = F.leaky_relu(x)
29+ y = F.leaky_relu(y, 0.1)
30+ z = F.leaky_relu(z, -0.22)
31+ w = F.leaky_relu(w, 0)
32+ return x, y, z, w
33+
34+def test():
35+ net = Model()
36+ net.eval()
37+
38+ torch.manual_seed(0)
39+ x = torch.rand(1, 16)
40+ y = torch.rand(12, 2, 16)
41+ z = torch.rand(1, 3, 12, 16)
42+ w = torch.rand(1, 5, 7, 9, 11)
43+
44+ a = net(x, y, z, w)
45+
46+ # export onnx
47+ torch.onnx.export(net, (x, y, z, w), "test_F_leaky_relu.onnx")
48+
49+ # onnx to pnnx
50+ import os
51+ os.system("../../src/pnnx test_F_leaky_relu.onnx inputshape=[1,16],[12,2,16],[1,3,12,16],[1,5,7,9,11]")
52+
53+ # pnnx inference
54+ import test_F_leaky_relu_pnnx
55+ b = test_F_leaky_relu_pnnx.test_inference()
56+
57+ for a0, b0 in zip(a, b):
58+ if not torch.equal(a0, b0):
59+ return False
60+ return True
61+
62+if __name__ == "__main__":
63+ if test():
64+ exit(0)
65+ else:
66+ exit(1)
added tools/pnnx/tests/onnx/test_F_prelu.py +79 −0
@@ -0,0 +1,79 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ self.w4 = nn.Parameter(torch.rand(16))
24+ self.w5 = nn.Parameter(torch.rand(2))
25+ self.w6 = nn.Parameter(torch.rand(3))
26+ self.w7 = nn.Parameter(torch.rand(1))
27+
28+ def forward(self, x, y, z, w, w0, w1, w2, w3):
29+ x = x * 2 - 1
30+ y = y * 2 - 1
31+ z = z * 2 - 1
32+ w = w * 2 - 1
33+ x = F.prelu(x, w0)
34+ x = F.prelu(x, self.w4)
35+ y = F.prelu(y, w1)
36+ y = F.prelu(y, self.w5)
37+ z = F.prelu(z, w2)
38+ z = F.prelu(z, self.w6)
39+ w = F.prelu(w, w3)
40+ w = F.prelu(w, self.w7)
41+ return x, y, z, w
42+
43+def test():
44+ net = Model()
45+ net.eval()
46+
47+ torch.manual_seed(0)
48+ x = torch.rand(1, 16)
49+ y = torch.rand(12, 2, 16)
50+ z = torch.rand(1, 3, 12, 16)
51+ w = torch.rand(1, 5, 7, 9, 11)
52+ w0 = torch.rand(16)
53+ w1 = torch.rand(2)
54+ w2 = torch.rand(3)
55+ w3 = torch.rand(1)
56+
57+ a = net(x, y, z, w, w0, w1, w2, w3)
58+
59+ # export onnx
60+ torch.onnx.export(net, (x, y, z, w, w0, w1, w2, w3), "test_F_prelu.onnx")
61+
62+ # onnx to pnnx
63+ import os
64+ os.system("../../src/pnnx test_F_prelu.onnx inputshape=[1,16],[12,2,16],[1,3,12,16],[1,5,7,9,11],[16],[2],[3],[1]")
65+
66+ # pnnx inference
67+ import test_F_prelu_pnnx
68+ b = test_F_prelu_pnnx.test_inference()
69+
70+ for a0, b0 in zip(a, b):
71+ if not torch.equal(a0, b0):
72+ return False
73+ return True
74+
75+if __name__ == "__main__":
76+ if test():
77+ exit(0)
78+ else:
79+ exit(1)
added tools/pnnx/tests/onnx/test_F_relu6.py +71 −0
@@ -0,0 +1,71 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+from packaging import version
19+
20+class Model(nn.Module):
21+ def __init__(self):
22+ super(Model, self).__init__()
23+
24+ def forward(self, x, y, z, w):
25+ x = x * 2 - 1
26+ y = y * 2 - 1
27+ z = z * 2 - 1
28+ w = w * 2 - 1
29+ x = F.relu6(x)
30+ y = F.relu6(y)
31+ z = F.relu6(z)
32+ w = F.relu6(w)
33+ return x, y, z, w
34+
35+def test():
36+ # torch-1.9 failed to export onnx for relu6
37+ if version.parse(torch.__version__) >= version.parse('1.9') and version.parse(torch.__version__) < version.parse('1.10'):
38+ return True
39+
40+ net = Model()
41+ net.eval()
42+
43+ torch.manual_seed(0)
44+ x = torch.rand(1, 16)
45+ y = torch.rand(12, 2, 16)
46+ z = torch.rand(1, 3, 12, 16)
47+ w = torch.rand(1, 5, 7, 9, 11)
48+
49+ a = net(x, y, z, w)
50+
51+ # export onnx
52+ torch.onnx.export(net, (x, y, z, w), "test_F_relu6.onnx")
53+
54+ # onnx to pnnx
55+ import os
56+ os.system("../../src/pnnx test_F_relu6.onnx inputshape=[1,16],[12,2,16],[1,3,12,16],[1,5,7,9,11]")
57+
58+ # pnnx inference
59+ import test_F_relu6_pnnx
60+ b = test_F_relu6_pnnx.test_inference()
61+
62+ for a0, b0 in zip(a, b):
63+ if not torch.equal(a0, b0):
64+ return False
65+ return True
66+
67+if __name__ == "__main__":
68+ if test():
69+ exit(0)
70+ else:
71+ exit(1)
added tools/pnnx/tests/onnx/test_nn_CELU.py +73 −0
@@ -0,0 +1,73 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+from packaging import version
19+
20+class Model(nn.Module):
21+ def __init__(self):
22+ super(Model, self).__init__()
23+
24+ self.act_0 = nn.CELU()
25+ self.act_1 = nn.CELU(alpha=2.0)
26+
27+ def forward(self, x, y, z, w):
28+ x = x * 2 - 1
29+ y = y * 2 - 1
30+ z = z * 2 - 1
31+ w = w * 2 - 1
32+ x = self.act_0(x)
33+ y = self.act_0(y)
34+ z = self.act_1(z)
35+ w = self.act_1(w)
36+ return x, y, z, w
37+
38+def test():
39+ if version.parse(torch.__version__) < version.parse('1.12'):
40+ return True
41+
42+ net = Model()
43+ net.eval()
44+
45+ torch.manual_seed(0)
46+ x = torch.rand(1, 12)
47+ y = torch.rand(1, 12, 64)
48+ z = torch.rand(1, 12, 24, 64)
49+ w = torch.rand(1, 12, 24, 32, 64)
50+
51+ a = net(x, y, z, w)
52+
53+ # export onnx
54+ torch.onnx.export(net, (x, y, z, w), "test_nn_CELU.onnx")
55+
56+ # onnx to pnnx
57+ import os
58+ os.system("../../src/pnnx test_nn_CELU.onnx inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]")
59+
60+ # pnnx inference
61+ import test_nn_CELU_pnnx
62+ b = test_nn_CELU_pnnx.test_inference()
63+
64+ for a0, b0 in zip(a, b):
65+ if not torch.equal(a0, b0):
66+ return False
67+ return True
68+
69+if __name__ == "__main__":
70+ if test():
71+ exit(0)
72+ else:
73+ exit(1)
added tools/pnnx/tests/onnx/test_nn_ELU.py +69 −0
@@ -0,0 +1,69 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ self.act_0 = nn.ELU()
24+ self.act_1 = nn.ELU(alpha=1.3)
25+
26+ def forward(self, x, y, z, w):
27+ x = x * 2 - 1
28+ y = y * 2 - 1
29+ z = z * 2 - 1
30+ w = w * 2 - 1
31+ x = self.act_0(x)
32+ y = self.act_0(y)
33+ z = self.act_1(z)
34+ w = self.act_1(w)
35+ return x, y, z, w
36+
37+def test():
38+ net = Model()
39+ net.eval()
40+
41+ torch.manual_seed(0)
42+ x = torch.rand(1, 12)
43+ y = torch.rand(1, 12, 64)
44+ z = torch.rand(1, 12, 24, 64)
45+ w = torch.rand(1, 12, 24, 32, 64)
46+
47+ a = net(x, y, z, w)
48+
49+ # export onnx
50+ torch.onnx.export(net, (x, y, z, w), "test_nn_ELU.onnx")
51+
52+ # onnx to pnnx
53+ import os
54+ os.system("../../src/pnnx test_nn_ELU.onnx inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]")
55+
56+ # pnnx inference
57+ import test_nn_ELU_pnnx
58+ b = test_nn_ELU_pnnx.test_inference()
59+
60+ for a0, b0 in zip(a, b):
61+ if not torch.equal(a0, b0):
62+ return False
63+ return True
64+
65+if __name__ == "__main__":
66+ if test():
67+ exit(0)
68+ else:
69+ exit(1)
added tools/pnnx/tests/onnx/test_nn_GELU.py +68 −0
@@ -0,0 +1,68 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ self.act_0 = nn.GELU()
24+
25+ def forward(self, x, y, z, w):
26+ x = x * 2 - 1
27+ y = y * 2 - 1
28+ z = z * 2 - 1
29+ w = w * 2 - 1
30+ x = self.act_0(x)
31+ y = self.act_0(y)
32+ z = self.act_0(z)
33+ w = self.act_0(w)
34+ return x, y, z, w
35+
36+def test():
37+ net = Model()
38+ net.eval()
39+
40+ torch.manual_seed(0)
41+ x = torch.rand(1, 12)
42+ y = torch.rand(1, 12, 64)
43+ z = torch.rand(1, 12, 24, 64)
44+ w = torch.rand(1, 12, 24, 32, 64)
45+
46+ a = net(x, y, z, w)
47+
48+ # export onnx
49+ torch.onnx.export(net, (x, y, z, w), "test_nn_GELU.onnx")
50+
51+ # onnx to pnnx
52+ import os
53+ os.system("../../src/pnnx test_nn_GELU.onnx inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]")
54+
55+ # pnnx inference
56+ import test_nn_GELU_pnnx
57+ b = test_nn_GELU_pnnx.test_inference()
58+
59+ for a0, b0 in zip(a, b):
60+ if not torch.allclose(a0, b0, 1e-3, 1e-3):
61+ return False
62+ return True
63+
64+if __name__ == "__main__":
65+ if test():
66+ exit(0)
67+ else:
68+ exit(1)
added tools/pnnx/tests/onnx/test_nn_Hardshrink.py +69 −0
@@ -0,0 +1,69 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ self.act_0 = nn.Hardshrink()
24+ self.act_1 = nn.Hardshrink(lambd=0.3)
25+
26+ def forward(self, x, y, z, w):
27+ x = x * 2 - 1
28+ y = y * 2 - 1
29+ z = z * 2 - 1
30+ w = w * 2 - 1
31+ x = self.act_0(x)
32+ y = self.act_0(y)
33+ z = self.act_1(z)
34+ w = self.act_1(w)
35+ return x, y, z, w
36+
37+def test():
38+ net = Model()
39+ net.eval()
40+
41+ torch.manual_seed(0)
42+ x = torch.rand(1, 12)
43+ y = torch.rand(1, 12, 64)
44+ z = torch.rand(1, 12, 24, 64)
45+ w = torch.rand(1, 12, 24, 32, 64)
46+
47+ a = net(x, y, z, w)
48+
49+ # export onnx
50+ torch.onnx.export(net, (x, y, z, w), "test_nn_Hardshrink.onnx")
51+
52+ # onnx to pnnx
53+ import os
54+ os.system("../../src/pnnx test_nn_Hardshrink.onnx inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]")
55+
56+ # pnnx inference
57+ import test_nn_Hardshrink_pnnx
58+ b = test_nn_Hardshrink_pnnx.test_inference()
59+
60+ for a0, b0 in zip(a, b):
61+ if not torch.equal(a0, b0):
62+ return False
63+ return True
64+
65+if __name__ == "__main__":
66+ if test():
67+ exit(0)
68+ else:
69+ exit(1)
added tools/pnnx/tests/onnx/test_nn_Hardsigmoid.py +68 −0
@@ -0,0 +1,68 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ self.act_0 = nn.Hardsigmoid()
24+
25+ def forward(self, x, y, z, w):
26+ x = x * 2 - 1
27+ y = y * 2 - 1
28+ z = z * 2 - 1
29+ w = w * 2 - 1
30+ x = self.act_0(x)
31+ y = self.act_0(y)
32+ z = self.act_0(z)
33+ w = self.act_0(w)
34+ return x, y, z, w
35+
36+def test():
37+ net = Model()
38+ net.eval()
39+
40+ torch.manual_seed(0)
41+ x = torch.rand(1, 12)
42+ y = torch.rand(1, 12, 64)
43+ z = torch.rand(1, 12, 24, 64)
44+ w = torch.rand(1, 12, 24, 32, 64)
45+
46+ a = net(x, y, z, w)
47+
48+ # export onnx
49+ torch.onnx.export(net, (x, y, z, w), "test_nn_Hardsigmoid.onnx")
50+
51+ # onnx to pnnx
52+ import os
53+ os.system("../../src/pnnx test_nn_Hardsigmoid.onnx inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]")
54+
55+ # pnnx inference
56+ import test_nn_Hardsigmoid_pnnx
57+ b = test_nn_Hardsigmoid_pnnx.test_inference()
58+
59+ for a0, b0 in zip(a, b):
60+ if not torch.equal(a0, b0):
61+ return False
62+ return True
63+
64+if __name__ == "__main__":
65+ if test():
66+ exit(0)
67+ else:
68+ exit(1)
added tools/pnnx/tests/onnx/test_nn_Hardswish.py +68 −0
@@ -0,0 +1,68 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ self.act_0 = nn.Hardswish()
24+
25+ def forward(self, x, y, z, w):
26+ x = x * 2 - 1
27+ y = y * 2 - 1
28+ z = z * 2 - 1
29+ w = w * 2 - 1
30+ x = self.act_0(x)
31+ y = self.act_0(y)
32+ z = self.act_0(z)
33+ w = self.act_0(w)
34+ return x, y, z, w
35+
36+def test():
37+ net = Model()
38+ net.eval()
39+
40+ torch.manual_seed(0)
41+ x = torch.rand(1, 12)
42+ y = torch.rand(1, 12, 64)
43+ z = torch.rand(1, 12, 24, 64)
44+ w = torch.rand(1, 12, 24, 32, 64)
45+
46+ a = net(x, y, z, w)
47+
48+ # export onnx
49+ torch.onnx.export(net, (x, y, z, w), "test_nn_Hardswish.onnx")
50+
51+ # onnx to pnnx
52+ import os
53+ os.system("../../src/pnnx test_nn_Hardswish.onnx inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]")
54+
55+ # pnnx inference
56+ import test_nn_Hardswish_pnnx
57+ b = test_nn_Hardswish_pnnx.test_inference()
58+
59+ for a0, b0 in zip(a, b):
60+ if not torch.equal(a0, b0):
61+ return False
62+ return True
63+
64+if __name__ == "__main__":
65+ if test():
66+ exit(0)
67+ else:
68+ exit(1)
added tools/pnnx/tests/onnx/test_nn_Hardtanh.py +69 −0
@@ -0,0 +1,69 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ self.act_0 = nn.Hardtanh()
24+ self.act_1 = nn.Hardtanh(-0.2, 0.2)
25+
26+ def forward(self, x, y, z, w):
27+ x = x * 2 - 1
28+ y = y * 2 - 1
29+ z = z * 2 - 1
30+ w = w * 2 - 1
31+ x = self.act_0(x)
32+ y = self.act_0(y)
33+ z = self.act_1(z)
34+ w = self.act_1(w)
35+ return x, y, z, w
36+
37+def test():
38+ net = Model()
39+ net.eval()
40+
41+ torch.manual_seed(0)
42+ x = torch.rand(1, 12)
43+ y = torch.rand(1, 12, 64)
44+ z = torch.rand(1, 12, 24, 64)
45+ w = torch.rand(1, 12, 24, 32, 64)
46+
47+ a = net(x, y, z, w)
48+
49+ # export onnx
50+ torch.onnx.export(net, (x, y, z, w), "test_nn_Hardtanh.onnx")
51+
52+ # onnx to pnnx
53+ import os
54+ os.system("../../src/pnnx test_nn_Hardtanh.onnx inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]")
55+
56+ # pnnx inference
57+ import test_nn_Hardtanh_pnnx
58+ b = test_nn_Hardtanh_pnnx.test_inference()
59+
60+ for a0, b0 in zip(a, b):
61+ if not torch.equal(a0, b0):
62+ return False
63+ return True
64+
65+if __name__ == "__main__":
66+ if test():
67+ exit(0)
68+ else:
69+ exit(1)
added tools/pnnx/tests/onnx/test_nn_LeakyReLU.py +69 −0
@@ -0,0 +1,69 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ self.act_0 = nn.LeakyReLU()
24+ self.act_1 = nn.LeakyReLU(negative_slope=-0.24)
25+
26+ def forward(self, x, y, z, w):
27+ x = x * 2 - 1
28+ y = y * 2 - 1
29+ z = z * 2 - 1
30+ w = w * 2 - 1
31+ x = self.act_0(x)
32+ y = self.act_0(y)
33+ z = self.act_1(z)
34+ w = self.act_1(w)
35+ return x, y, z, w
36+
37+def test():
38+ net = Model()
39+ net.eval()
40+
41+ torch.manual_seed(0)
42+ x = torch.rand(1, 12)
43+ y = torch.rand(1, 12, 64)
44+ z = torch.rand(1, 12, 24, 64)
45+ w = torch.rand(1, 12, 24, 32, 64)
46+
47+ a = net(x, y, z, w)
48+
49+ # export onnx
50+ torch.onnx.export(net, (x, y, z, w), "test_nn_LeakyReLU.onnx")
51+
52+ # onnx to pnnx
53+ import os
54+ os.system("../../src/pnnx test_nn_LeakyReLU.onnx inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]")
55+
56+ # pnnx inference
57+ import test_nn_LeakyReLU_pnnx
58+ b = test_nn_LeakyReLU_pnnx.test_inference()
59+
60+ for a0, b0 in zip(a, b):
61+ if not torch.equal(a0, b0):
62+ return False
63+ return True
64+
65+if __name__ == "__main__":
66+ if test():
67+ exit(0)
68+ else:
69+ exit(1)
added tools/pnnx/tests/onnx/test_nn_PReLU.py +77 −0
@@ -0,0 +1,77 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ self.prelu_0 = nn.PReLU(num_parameters=12)
24+ self.prelu_1 = nn.PReLU(num_parameters=1, init=0.12)
25+
26+ def forward(self, x, y, z, w):
27+ x = x * 2 - 1
28+ y = y * 2 - 1
29+ z = z * 2 - 1
30+ w = w * 2 - 1
31+
32+ x = self.prelu_0(x)
33+ x = self.prelu_1(x)
34+
35+ y = self.prelu_0(y)
36+ y = self.prelu_1(y)
37+
38+ z = self.prelu_0(z)
39+ z = self.prelu_1(z)
40+
41+ w = self.prelu_0(w)
42+ w = self.prelu_1(w)
43+ return x, y, z, w
44+
45+def test():
46+ net = Model()
47+ net.eval()
48+
49+ torch.manual_seed(0)
50+ x = torch.rand(1, 12)
51+ y = torch.rand(1, 12, 64)
52+ z = torch.rand(1, 12, 24, 64)
53+ w = torch.rand(1, 12, 24, 32, 64)
54+
55+ a = net(x, y, z, w)
56+
57+ # export onnx
58+ torch.onnx.export(net, (x, y, z, w), "test_nn_PReLU.onnx")
59+
60+ # onnx to pnnx
61+ import os
62+ os.system("../../src/pnnx test_nn_PReLU.onnx inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]")
63+
64+ # pnnx inference
65+ import test_nn_PReLU_pnnx
66+ b = test_nn_PReLU_pnnx.test_inference()
67+
68+ for a0, b0 in zip(a, b):
69+ if not torch.equal(a0, b0):
70+ return False
71+ return True
72+
73+if __name__ == "__main__":
74+ if test():
75+ exit(0)
76+ else:
77+ exit(1)
added tools/pnnx/tests/onnx/test_nn_ReLU6.py +68 −0
@@ -0,0 +1,68 @@
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+import torch
16+import torch.nn as nn
17+import torch.nn.functional as F
18+
19+class Model(nn.Module):
20+ def __init__(self):
21+ super(Model, self).__init__()
22+
23+ self.act_0 = nn.ReLU6()
24+
25+ def forward(self, x, y, z, w):
26+ x = x * 2 - 1
27+ y = y * 2 - 1
28+ z = z * 2 - 1
29+ w = w * 2 - 1
30+ x = self.act_0(x)
31+ y = self.act_0(y)
32+ z = self.act_0(z)
33+ w = self.act_0(w)
34+ return x, y, z, w
35+
36+def test():
37+ net = Model()
38+ net.eval()
39+
40+ torch.manual_seed(0)
41+ x = torch.rand(1, 12)
42+ y = torch.rand(1, 12, 64)
43+ z = torch.rand(1, 12, 24, 64)
44+ w = torch.rand(1, 12, 24, 32, 64)
45+
46+ a = net(x, y, z, w)
47+
48+ # export onnx
49+ torch.onnx.export(net, (x, y, z, w), "test_nn_ReLU6.onnx")
50+
51+ # onnx to pnnx
52+ import os
53+ os.system("../../src/pnnx test_nn_ReLU6.onnx inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]")
54+
55+ # pnnx inference
56+ import test_nn_ReLU6_pnnx
57+ b = test_nn_ReLU6_pnnx.test_inference()
58+
59+ for a0, b0 in zip(a, b):
60+ if not torch.equal(a0, b0):
61+ return False
62+ return True
63+
64+if __name__ == "__main__":
65+ if test():
66+ exit(0)
67+ else:
68+ exit(1)