Elffuss BraidHub

kikocis/ncnn public

master · 10.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_linear : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
255 4
26pnnx.Input input_0 0 1 input
27pnnx.Input input_1 0 1 weight
28pnnx.Input input_2 0 1 bias
29aten::linear op_0 3 1 input weight bias out
30pnnx.Output output 1 0 out
31)PNNXIR";
32 }
33
34 const char* type_str() const
35 {
36 return "F.linear";
37 }
38};
39
40REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear, 10)
41
42class F_linear_1 : public GraphRewriterPass
43{
44public:
45 const char* match_pattern_graph() const
46 {
47 return R"PNNXIR(7767517
488 7
49pnnx.Input input_0 0 1 input
50pnnx.Input input_1 0 1 weight
51pnnx.Input input_2 0 1 bias
52aten::t op_0 1 1 weight 9
53aten::matmul op_1 2 1 input 9 a
54prim::Constant op_2 0 1 19 value=1
55aten::add op_3 3 1 a bias 19 out
56pnnx.Output output 1 0 out
57)PNNXIR";
58 }
59
60 const char* type_str() const
61 {
62 return "F.linear";
63 }
64};
65
66REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_1, 9)
67
68class F_linear_2 : public GraphRewriterPass
69{
70public:
71 const char* match_pattern_graph() const
72 {
73 return R"PNNXIR(7767517
745 4
75pnnx.Input input_0 0 1 input
76pnnx.Input input_1 0 1 weight
77aten::t op_0 1 1 weight 9
78aten::matmul op_1 2 1 input 9 out
79pnnx.Output output 1 0 out
80)PNNXIR";
81 }
82
83 const char* type_str() const
84 {
85 return "F.linear";
86 }
87
88 void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const
89 {
90 op->params["bias"] = Parameter();
91 }
92};
93
94REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_2, 10)
95
96class F_linear_3 : public GraphRewriterPass
97{
98public:
99 const char* match_pattern_graph() const
100 {
101 return R"PNNXIR(7767517
1028 7
103pnnx.Input input_0 0 1 input
104pnnx.Input input_1 0 1 weight
105pnnx.Input input_2 0 1 bias
106aten::t op_0 1 1 weight 14
107prim::Constant op_1 0 1 15 value=1
108prim::Constant op_2 0 1 30 value=1
109aten::addmm op_3 5 1 bias input 14 15 30 out
110pnnx.Output output 1 0 out
111)PNNXIR";
112 }
113
114 const char* type_str() const
115 {
116 return "F.linear";
117 }
118};
119
120REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_3, 10)
121
122class F_linear_onnx : public GraphRewriterPass
123{
124public:
125 const char* match_pattern_graph() const
126 {
127 return R"PNNXIR(7767517
1285 4
129pnnx.Input input_0 0 1 input
130pnnx.Input input_1 0 1 weight
131pnnx.Input input_2 0 1 bias
132Gemm gemm 3 1 input weight bias out %*=%*
133pnnx.Output output 1 0 out
134)PNNXIR";
135 }
136
137 const char* type_str() const
138 {
139 return "F.linear";
140 }
141
142 bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const
143 {
144 if (captured_params.find("gemm.alpha") != captured_params.end())
145 {
146 if (captured_params.at("gemm.alpha").type != 3 || captured_params.at("gemm.alpha").f != 1.f)
147 return false;
148 }
149
150 if (captured_params.find("gemm.beta") != captured_params.end())
151 {
152 if (captured_params.at("gemm.beta").type != 3 || captured_params.at("gemm.beta").f != 1.f)
153 return false;
154 }
155
156 if (captured_params.find("gemm.transA") != captured_params.end())
157 {
158 if (captured_params.at("gemm.transA").type != 2 || captured_params.at("gemm.transA").i != 0)
159 return false;
160 }
161
162 if (captured_params.find("gemm.transB") == captured_params.end())
163 return false;
164
165 if (captured_params.at("gemm.transB").type != 2 || captured_params.at("gemm.transB").i != 1)
166 return false;
167
168 return true;
169 }
170
171 void write(Operator* op, const std::map<std::string, Parameter>& /*captured_params*/) const
172 {
173 }
174};
175
176REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_onnx, 10)
177
178class F_linear_onnx_1 : public GraphRewriterPass
179{
180public:
181 const char* match_pattern_graph() const
182 {
183 return R"PNNXIR(7767517
1845 4
185pnnx.Input input_0 0 1 input
186pnnx.Input input_1 0 1 bias
187pnnx.Attribute weight 0 1 weight @data=(%in_features,%out_features)f32
188Gemm gemm 3 1 input weight bias out %*=%*
189pnnx.Output output 1 0 out
190)PNNXIR";
191 }
192
193 const char* replace_pattern_graph() const
194 {
195 return R"PNNXIR(7767517
1965 4
197pnnx.Input input_0 0 1 input
198pnnx.Input input_1 0 1 bias
199pnnx.Attribute weight 0 1 weight
200F.linear linear 3 1 input weight bias out $weight=weight
201pnnx.Output output 1 0 out
202)PNNXIR";
203 }
204
205 bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const
206 {
207 if (captured_params.find("gemm.alpha") != captured_params.end())
208 {
209 if (captured_params.at("gemm.alpha").type != 3 || captured_params.at("gemm.alpha").f != 1.f)
210 return false;
211 }
212
213 if (captured_params.find("gemm.beta") != captured_params.end())
214 {
215 if (captured_params.at("gemm.beta").type != 3 || captured_params.at("gemm.beta").f != 1.f)
216 return false;
217 }
218
219 if (captured_params.find("gemm.transA") != captured_params.end())
220 {
221 if (captured_params.at("gemm.transA").type != 2 || captured_params.at("gemm.transA").i != 0)
222 return false;
223 }
224
225 if (captured_params.find("gemm.transB") != captured_params.end())
226 {
227 if (captured_params.at("gemm.transB").type != 2 || captured_params.at("gemm.transB").i != 0)
228 return false;
229 }
230
231 return true;
232 }
233
234 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
235 {
236 const int in_features = captured_params.at("in_features").i;
237 const int out_features = captured_params.at("out_features").i;
238
239 auto weight = captured_attrs.at("weight.data").get_float32_data();
240 std::vector<float> transposed_weight(in_features * out_features);
241 for (int i = 0; i < out_features; i++)
242 {
243 float* wptr = (float*)transposed_weight.data() + in_features * i;
244 for (int j = 0; j < in_features; j++)
245 {
246 wptr[j] = ((const float*)weight.data())[out_features * j + i];
247 }
248 }
249
250 Operator* op_weight = ops.at("weight");
251 op_weight->attrs["data"] = Attribute({out_features, in_features}, transposed_weight);
252 }
253};
254
255REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_onnx_1, 9)
256
257class F_linear_onnx_2 : public F_linear_onnx_1
258{
259public:
260 const char* match_pattern_graph() const
261 {
262 return R"PNNXIR(7767517
2634 3
264pnnx.Input input_0 0 1 input
265pnnx.Attribute weight 0 1 weight @data=(%in_features,%out_features)f32
266MatMul matmul 2 1 input weight out
267pnnx.Output output 1 0 out
268)PNNXIR";
269 }
270
271 const char* replace_pattern_graph() const
272 {
273 return R"PNNXIR(7767517
2744 3
275pnnx.Input input_0 0 1 input
276pnnx.Attribute weight 0 1 weight
277F.linear linear 2 1 input weight out bias=None $weight=weight
278pnnx.Output output 1 0 out
279)PNNXIR";
280 }
281};
282
283REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_onnx_2, 9)
284
285class F_linear_onnx_3 : public F_linear_onnx_1
286{
287public:
288 const char* match_pattern_graph() const
289 {
290 return R"PNNXIR(7767517
2916 5
292pnnx.Input input_0 0 1 input
293pnnx.Attribute weight 0 1 weight @data=(%in_features,%out_features)f32
294pnnx.Attribute bias 0 1 bias @data=(%out_features)f32
295MatMul matmul 2 1 input weight mm
296aten::add add 2 1 mm bias out
297pnnx.Output output 1 0 out
298)PNNXIR";
299 }
300
301 const char* replace_pattern_graph() const
302 {
303 return R"PNNXIR(7767517
3045 4
305pnnx.Input input_0 0 1 input
306pnnx.Attribute weight 0 1 weight
307pnnx.Attribute bias 0 1 bias
308F.linear linear 3 1 input weight bias out $weight=weight $bias=bias
309pnnx.Output output 1 0 out
310)PNNXIR";
311 }
312
313 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
314 {
315 F_linear_onnx_1::write(ops, captured_params, captured_attrs);
316
317 Operator* op_bias = ops.at("bias");
318 op_bias->attrs["data"] = captured_attrs.at("bias.data");
319 }
320};
321
322REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_onnx_3, 8)
323
324class F_linear_onnx_4 : public F_linear_onnx_3
325{
326public:
327 const char* match_pattern_graph() const
328 {
329 return R"PNNXIR(7767517
3306 5
331pnnx.Input input_0 0 1 input
332pnnx.Attribute weight 0 1 weight @data=(%in_features,%out_features)f32
333pnnx.Attribute bias 0 1 bias @data=(%out_features)f32
334MatMul matmul 2 1 input weight mm
335aten::add add 2 1 bias mm out
336pnnx.Output output 1 0 out
337)PNNXIR";
338 }
339};
340
341REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_linear_onnx_4, 8)
342
343} // namespace pnnx