Elffuss BraidHub

kikocis/ncnn public

master · 4.5 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_prelu : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
254 3
26pnnx.Input input_0 0 1 input
27pnnx.Input input_1 0 1 weight
28aten::prelu op_0 2 1 input weight out
29pnnx.Output output 1 0 out
30)PNNXIR";
31 }
32
33 const char* type_str() const
34 {
35 return "F.prelu";
36 }
37};
38
39REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_prelu, 10)
40
41class F_prelu_onnx : public GraphRewriterPass
42{
43public:
44 const char* match_pattern_graph() const
45 {
46 // clang-format off
47 // *INDENT-OFF*
48
49 return R"PNNXIR(7767517
504 3
51pnnx.Input input_0 0 1 input
52pnnx.Input input_1 0 1 weight #weight=(?)f32
53PRelu op_0 2 1 input weight out
54pnnx.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
67REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_prelu_onnx, 10)
68
69class F_prelu_onnx_1 : public GraphRewriterPass
70{
71public:
72 const char* match_pattern_graph() const
73 {
74 return R"PNNXIR(7767517
754 3
76pnnx.Input input 0 1 input
77pnnx.Attribute slope 0 1 weight @data
78PRelu op_0 2 1 input weight out
79pnnx.Output output 1 0 out
80)PNNXIR";
81 }
82
83 const char* replace_pattern_graph() const
84 {
85 return R"PNNXIR(7767517
864 3
87pnnx.Input input 0 1 input
88pnnx.Attribute slope 0 1 weight @data=%slope.data
89F.prelu prelu 2 1 input weight out
90pnnx.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
112REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_prelu_onnx_1, 10)
113
114class F_prelu_onnx_2 : public GraphRewriterPass
115{
116public:
117 const char* match_pattern_graph() const
118 {
119 // clang-format off
120 // *INDENT-OFF*
121
122 return R"PNNXIR(7767517
1235 4
124pnnx.Input input_0 0 1 input
125pnnx.Input input_1 0 1 weight #weight=(?)f32
126Unsqueeze uqz 1 1 weight w2 axes=%axes
127PRelu op_0 2 1 input w2 out
128pnnx.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
167REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_prelu_onnx_2, 10)
168
169} // namespace pnnx