Elffuss BraidHub

kikocis/ncnn public

master · 7.7 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_ncnn.h"
16
17namespace pnnx {
18
19namespace ncnn {
20
21class nn_Linear_0 : public GraphRewriterPass
22{
23public:
24 const char* match_pattern_graph() const
25 {
26 return R"PNNXIR(7767517
273 2
28pnnx.Input input 0 1 input #input=(1,%m,%in_features)f32
29nn.Linear op_0 1 1 input out in_features=%in_features out_features=%out_features bias=%bias
30pnnx.Output output 1 0 out
31)PNNXIR";
32 }
33
34 const char* type_str() const
35 {
36 return "Gemm";
37 }
38
39 const char* name_str() const
40 {
41 return "gemm";
42 }
43
44 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
45 {
46 int m = captured_params.at("m").i;
47 if (m == -1)
48 m = 0;
49
50 op->params["2"] = 0;
51 op->params["3"] = 1;
52 op->params["4"] = 0;
53 op->params["5"] = 1;
54 op->params["6"] = 1;
55 op->params["7"] = m;
56 op->params["8"] = captured_params.at("out_features");
57 op->params["9"] = captured_params.at("in_features");
58 op->params["10"] = captured_params.at("bias").b ? 4 : -1;
59
60 op->attrs["0"] = Attribute();
61 op->attrs["0"].data = {0, 0, 0, 0};
62 op->attrs["1"] = captured_attrs.at("op_0.weight");
63 if (captured_params.at("bias").b)
64 {
65 op->attrs["2"] = Attribute();
66 op->attrs["2"].data = {0, 0, 0, 0};
67 op->attrs["3"] = captured_attrs.at("op_0.bias");
68 }
69 }
70};
71
72REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Linear_0, 19)
73
74class nn_Linear_01 : public nn_Linear_0
75{
76public:
77 const char* match_pattern_graph() const
78 {
79 return R"PNNXIR(7767517
803 2
81pnnx.Input input 0 1 input #input=(%m,%in_features)f32
82nn.Linear op_0 1 1 input out in_features=%in_features out_features=%out_features bias=%bias
83pnnx.Output output 1 0 out
84)PNNXIR";
85 }
86
87 bool match(const std::map<std::string, Parameter>& captured_params) const
88 {
89 const int m = captured_params.at("m").i;
90
91 if (m == 1)
92 return false;
93
94 return true;
95 }
96};
97
98REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Linear_01, 19)
99
100class nn_Linear_10 : public GraphRewriterPass
101{
102public:
103 const char* match_pattern_graph() const
104 {
105 return R"PNNXIR(7767517
1064 3
107pnnx.Input input_0 0 1 input #input=(1,%m,%in_features)f32
108pnnx.Input input_1 0 1 bias
109nn.Linear op_0 2 1 input bias out in_features=%in_features out_features=%out_features bias=False
110pnnx.Output output 1 0 out
111)PNNXIR";
112 }
113
114 const char* type_str() const
115 {
116 return "Gemm";
117 }
118
119 const char* name_str() const
120 {
121 return "gemm";
122 }
123
124 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
125 {
126 int m = captured_params.at("m").i;
127 if (m == -1)
128 m = 0;
129
130 op->params["2"] = 0;
131 op->params["3"] = 1;
132 op->params["4"] = 0;
133 op->params["5"] = 1;
134 op->params["6"] = 0;
135 op->params["7"] = m;
136 op->params["8"] = captured_params.at("out_features");
137 op->params["9"] = captured_params.at("in_features");
138 op->params["10"] = 4;
139
140 op->attrs["0"] = Attribute();
141 op->attrs["0"].data = {0, 0, 0, 0};
142 op->attrs["1"] = captured_attrs.at("op_0.weight");
143 }
144};
145
146REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Linear_10, 19)
147
148class nn_Linear_11 : public nn_Linear_10
149{
150public:
151 const char* match_pattern_graph() const
152 {
153 return R"PNNXIR(7767517
1544 3
155pnnx.Input input_0 0 1 input #input=(%m,%in_features)f32
156pnnx.Input input_1 0 1 bias
157nn.Linear op_0 2 1 input bias out in_features=%in_features out_features=%out_features bias=False
158pnnx.Output output 1 0 out
159)PNNXIR";
160 }
161
162 bool match(const std::map<std::string, Parameter>& captured_params) const
163 {
164 const int m = captured_params.at("m").i;
165
166 if (m == 1)
167 return false;
168
169 return true;
170 }
171};
172
173REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Linear_11, 19)
174
175class nn_Linear : public GraphRewriterPass
176{
177public:
178 const char* match_pattern_graph() const
179 {
180 return R"PNNXIR(7767517
1813 2
182pnnx.Input input 0 1 input
183nn.Linear op_0 1 1 input out in_features=%in_features out_features=%out_features bias=%bias
184pnnx.Output output 1 0 out
185)PNNXIR";
186 }
187
188 const char* type_str() const
189 {
190 return "InnerProduct";
191 }
192
193 const char* name_str() const
194 {
195 return "linear";
196 }
197
198 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
199 {
200 op->params["0"] = captured_params.at("out_features");
201 op->params["1"] = captured_params.at("bias").b ? 1 : 0;
202 op->params["2"] = captured_attrs.at("op_0.weight").elemcount();
203
204 op->attrs["0"] = Attribute();
205 op->attrs["0"].data = {0, 0, 0, 0};
206 op->attrs["1"] = captured_attrs.at("op_0.weight");
207 if (captured_params.at("bias").b)
208 op->attrs["2"] = captured_attrs.at("op_0.bias");
209 }
210};
211
212REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Linear, 20)
213
214class nn_Linear_1 : public GraphRewriterPass
215{
216public:
217 const char* match_pattern_graph() const
218 {
219 return R"PNNXIR(7767517
2204 3
221pnnx.Input input_0 0 1 input
222pnnx.Input input_1 0 1 bias
223nn.Linear op_0 2 1 input bias out in_features=%in_features out_features=%out_features bias=False
224pnnx.Output output 1 0 out
225)PNNXIR";
226 }
227
228 const char* replace_pattern_graph() const
229 {
230 return R"PNNXIR(7767517
2315 4
232pnnx.Input input_0 0 1 input
233pnnx.Input input_1 0 1 bias
234InnerProduct linear 1 1 input a
235BinaryOp bias 2 1 a bias out 0=0
236pnnx.Output output 1 0 out
237)PNNXIR";
238 }
239
240 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
241 {
242 GraphRewriterPass::write(ops, captured_params, captured_attrs);
243
244 const int batch_index = ops.at("linear")->inputs[0]->params["__batch_index"].i;
245
246 ops.at("linear")->params["0"] = captured_params.at("out_features");
247 ops.at("linear")->params["1"] = 0;
248 ops.at("linear")->params["2"] = captured_attrs.at("op_0.weight").elemcount();
249
250 ops.at("linear")->attrs["0"] = Attribute();
251 ops.at("linear")->attrs["0"].data = {0, 0, 0, 0};
252 ops.at("linear")->attrs["1"] = captured_attrs.at("op_0.weight");
253
254 ops.at("linear")->outputs[0]->params["__batch_index"] = batch_index;
255 }
256};
257
258REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_Linear_1, 20)
259
260} // namespace ncnn
261
262} // namespace pnnx