Elffuss BraidHub

kikocis/ncnn public

master · 15.9 KiBRawPermalink
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 "pass_level2.h"
16
17namespace pnnx {
18
19class nn_RNN_onnx : public GraphRewriterPass
20{
21public:
22 const char* match_pattern_graph() const
23 {
24 return R"PNNXIR(7767517
256 5
26pnnx.Input input_0 0 1 input
27pnnx.Attribute W 0 1 W @data
28pnnx.Attribute R 0 1 R @data
29RNN rnn 3 1 input W R out %*=%*
30Squeeze sqz 1 1 out out1 axes=%axes
31pnnx.Output output 1 0 out1
32)PNNXIR";
33 }
34
35 const char* type_str() const
36 {
37 return "nn.RNN";
38 }
39
40 const char* name_str() const
41 {
42 return "rnn";
43 }
44
45 bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
46 {
47 if (captured_params.find("rnn.hidden_size") == captured_params.end())
48 return false;
49
50 const int hidden_size = captured_params.at("rnn.hidden_size").i;
51
52 std::string direction = "forward";
53 if (captured_params.find("rnn.direction") != captured_params.end())
54 {
55 direction = captured_params.at("rnn.direction").s;
56 }
57
58 if (direction != "forward" && direction != "bidirectional")
59 return false;
60
61 const int num_directions = direction == "bidirectional" ? 2 : 1;
62
63 if (captured_params.find("rnn.activations") != captured_params.end())
64 {
65 const std::vector<std::string>& acts = captured_params.at("rnn.activations").as;
66
67 if (num_directions == 1)
68 {
69 if (acts != std::vector<std::string>{"Tanh"} && acts != std::vector<std::string>{"Relu"})
70 return false;
71 }
72 else // if (num_directions == 2)
73 {
74 if (acts != std::vector<std::string>{"Tanh", "Tanh"} && acts != std::vector<std::string>{"Relu", "Relu"})
75 return false;
76 }
77 }
78
79 if (captured_params.find("axes") != captured_params.end())
80 {
81 if (captured_params.at("axes").type == 2 && captured_params.at("axes").i != 1)
82 return false;
83
84 if (captured_params.at("axes").type == 5 && captured_params.at("axes").ai != std::vector<int>{1})
85 return false;
86 }
87
88 const auto& W = captured_attrs.at("W.data");
89 const auto& R = captured_attrs.at("R.data");
90
91 if (W.shape.size() != 3 || W.shape[0] != num_directions || W.shape[1] != hidden_size)
92 return false;
93
94 if (R.shape.size() != 3 || R.shape[0] != num_directions || R.shape[1] != hidden_size || R.shape[2] != hidden_size)
95 return false;
96
97 return true;
98 }
99
100 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
101 {
102 std::string direction = "forward";
103 if (captured_params.find("rnn.direction") != captured_params.end())
104 {
105 direction = captured_params.at("rnn.direction").s;
106 }
107
108 std::string act = "Tanh";
109 if (captured_params.find("rnn.activations") != captured_params.end())
110 {
111 act = captured_params.at("rnn.activations").as[0];
112 }
113
114 const auto& W = captured_attrs.at("W.data");
115 const auto& R = captured_attrs.at("R.data");
116
117 bool batch_first = false;
118 if (captured_params.find("rnn.layout") != captured_params.end())
119 {
120 const int layout = captured_params.at("rnn.layout").i;
121 batch_first = layout == 1;
122 }
123
124 const int hidden_size = captured_params.at("rnn.hidden_size").i;
125
126 const int input_size = W.shape[2];
127
128 op->params["input_size"] = input_size;
129 op->params["hidden_size"] = hidden_size;
130 op->params["num_layers"] = 1;
131 op->params["nonlinearity"] = act == "Relu" ? "relu" : "tanh";
132 op->params["bias"] = false;
133 op->params["batch_first"] = batch_first;
134 op->params["bidirectional"] = direction == "bidirectional" ? true : false;
135
136 // split W R
137 auto W_data = W.get_float32_data();
138 auto R_data = R.get_float32_data();
139
140 if (direction == "bidirectional")
141 {
142 op->attrs["weight_ih_l0"] = Attribute({hidden_size, input_size}, std::vector<float>(&W_data[0], &W_data[hidden_size * input_size]));
143 op->attrs["weight_hh_l0"] = Attribute({hidden_size, hidden_size}, std::vector<float>(&R_data[0], &R_data[hidden_size * hidden_size]));
144
145 op->attrs["weight_ih_l0_reverse"] = Attribute({hidden_size, input_size}, std::vector<float>(&W_data[hidden_size * input_size], &W_data[hidden_size * input_size * 2]));
146 op->attrs["weight_hh_l0_reverse"] = Attribute({hidden_size, hidden_size}, std::vector<float>(&R_data[hidden_size * hidden_size], &R_data[hidden_size * hidden_size * 2]));
147 }
148 else
149 {
150 op->attrs["weight_ih_l0"] = Attribute({hidden_size, input_size}, W_data);
151 op->attrs["weight_hh_l0"] = Attribute({hidden_size, hidden_size}, R_data);
152 }
153 }
154};
155
156REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx, 10)
157
158class nn_RNN_onnx_B : public nn_RNN_onnx
159{
160public:
161 const char* match_pattern_graph() const
162 {
163 return R"PNNXIR(7767517
1647 6
165pnnx.Input input_0 0 1 input
166pnnx.Attribute W 0 1 W @data
167pnnx.Attribute R 0 1 R @data
168pnnx.Attribute B 0 1 B @data
169RNN rnn 4 1 input W R B out %*=%*
170Squeeze sqz 1 1 out out1 axes=%axes
171pnnx.Output output 1 0 out1
172)PNNXIR";
173 }
174
175 bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
176 {
177 if (!nn_RNN_onnx::match(captured_params, captured_attrs))
178 return false;
179
180 const int hidden_size = captured_params.at("rnn.hidden_size").i;
181
182 std::string direction = "forward";
183 if (captured_params.find("rnn.direction") != captured_params.end())
184 {
185 direction = captured_params.at("rnn.direction").s;
186 }
187
188 const int num_directions = direction == "bidirectional" ? 2 : 1;
189
190 const auto& B = captured_attrs.at("B.data");
191
192 if (B.shape.size() != 2 || B.shape[0] != num_directions || B.shape[1] != 2 * hidden_size)
193 return false;
194
195 return true;
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 nn_RNN_onnx::write(op, captured_params, captured_attrs);
201
202 const auto& B = captured_attrs.at("B.data");
203
204 bool has_bias = false;
205 for (auto b : B.get_float32_data())
206 {
207 if (b != 0.f)
208 {
209 has_bias = true;
210 break;
211 }
212 }
213
214 op->params["bias"] = has_bias;
215
216 if (has_bias)
217 {
218 // split B
219 auto B_data = B.get_float32_data();
220
221 const int hidden_size = captured_params.at("rnn.hidden_size").i;
222
223 std::string direction = "forward";
224 if (captured_params.find("rnn.direction") != captured_params.end())
225 {
226 direction = captured_params.at("rnn.direction").s;
227 }
228
229 if (direction == "bidirectional")
230 {
231 op->attrs["bias_ih_l0"] = Attribute({hidden_size}, std::vector<float>(&B_data[0], &B_data[hidden_size]));
232 op->attrs["bias_hh_l0"] = Attribute({hidden_size}, std::vector<float>(&B_data[hidden_size], &B_data[hidden_size * 2]));
233
234 op->attrs["bias_ih_l0_reverse"] = Attribute({hidden_size}, std::vector<float>(&B_data[hidden_size * 2], &B_data[hidden_size * 3]));
235 op->attrs["bias_hh_l0_reverse"] = Attribute({hidden_size}, std::vector<float>(&B_data[hidden_size * 3], &B_data[hidden_size * 4]));
236 }
237 else
238 {
239 op->attrs["bias_ih_l0"] = Attribute({hidden_size}, std::vector<float>(&B_data[0], &B_data[hidden_size]));
240 op->attrs["bias_hh_l0"] = Attribute({hidden_size}, std::vector<float>(&B_data[hidden_size], &B_data[hidden_size * 2]));
241 }
242 }
243 }
244};
245
246REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_B, 10)
247
248class nn_RNN_onnx_1 : public nn_RNN_onnx
249{
250public:
251 const char* match_pattern_graph() const
252 {
253 return R"PNNXIR(7767517
2547 7
255pnnx.Input input_0 0 1 input
256pnnx.Input input_1 0 1 initial_h
257pnnx.Attribute W 0 1 W @data
258pnnx.Attribute R 0 1 R @data
259RNN rnn 4 2 input W R initial_h out outh %*=%*
260Squeeze sqz 1 1 out out1 axes=%axes
261pnnx.Output output 2 0 out1 outh
262)PNNXIR";
263 }
264};
265
266REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_1, 10)
267
268class nn_RNN_onnx_B1 : public nn_RNN_onnx_B
269{
270public:
271 const char* match_pattern_graph() const
272 {
273 return R"PNNXIR(7767517
2748 8
275pnnx.Input input_0 0 1 input
276pnnx.Input input_1 0 1 initial_h
277pnnx.Attribute W 0 1 W @data
278pnnx.Attribute R 0 1 R @data
279pnnx.Attribute B 0 1 B @data
280RNN rnn 5 2 input W R B initial_h out outh %*=%*
281Squeeze sqz 1 1 out out1 axes=%axes
282pnnx.Output output 2 0 out1 outh
283)PNNXIR";
284 }
285};
286
287REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_B1, 10)
288
289class nn_RNN_onnx_2 : public nn_RNN_onnx
290{
291public:
292 const char* match_pattern_graph() const
293 {
294 return R"PNNXIR(7767517
2957 6
296pnnx.Input input_0 0 1 input
297pnnx.Input input_1 0 1 initial_h
298pnnx.Attribute W 0 1 W @data
299pnnx.Attribute R 0 1 R @data
300RNN rnn 4 1 input W R initial_h out %*=%*
301Squeeze sqz 1 1 out out1 axes=%axes
302pnnx.Output output 1 0 out1
303)PNNXIR";
304 }
305};
306
307REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_2, 10)
308
309class nn_RNN_onnx_B2 : public nn_RNN_onnx_B
310{
311public:
312 const char* match_pattern_graph() const
313 {
314 return R"PNNXIR(7767517
3158 7
316pnnx.Input input_0 0 1 input
317pnnx.Input input_1 0 1 initial_h
318pnnx.Attribute W 0 1 W @data
319pnnx.Attribute R 0 1 R @data
320pnnx.Attribute B 0 1 B @data
321RNN rnn 5 1 input W R B initial_h out %*=%*
322Squeeze sqz 1 1 out out1 axes=%axes
323pnnx.Output output 1 0 out1
324)PNNXIR";
325 }
326};
327
328REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_B2, 10)
329
330class nn_RNN_onnx_3 : public nn_RNN_onnx
331{
332public:
333 const char* match_pattern_graph() const
334 {
335 return R"PNNXIR(7767517
3367 6
337pnnx.Input input_0 0 1 input
338pnnx.Attribute W 0 1 W @data
339pnnx.Attribute R 0 1 R @data
340RNN rnn 3 1 input W R out %*=%*
341Transpose transpose 1 1 out out1 perm=(0,2,1,3)
342Reshape reshape 1 1 out1 out2 %*=%*
343pnnx.Output output 1 0 out2
344)PNNXIR";
345 }
346
347 bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
348 {
349 if (!nn_RNN_onnx::match(captured_params, captured_attrs))
350 return false;
351
352 if (captured_params.at("reshape.shape").ai != std::vector<int>{0, 0, -1})
353 return false;
354
355 return true;
356 }
357};
358
359REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_3, 10)
360
361class nn_RNN_onnx_B3 : public nn_RNN_onnx_B
362{
363public:
364 const char* match_pattern_graph() const
365 {
366 return R"PNNXIR(7767517
3678 7
368pnnx.Input input_0 0 1 input
369pnnx.Attribute W 0 1 W @data
370pnnx.Attribute R 0 1 R @data
371pnnx.Attribute B 0 1 B @data
372RNN rnn 4 1 input W R B out %*=%*
373Transpose transpose 1 1 out out1 perm=(0,2,1,3)
374Reshape reshape 1 1 out1 out2 %*=%*
375pnnx.Output output 1 0 out2
376)PNNXIR";
377 }
378
379 bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
380 {
381 if (!nn_RNN_onnx_B::match(captured_params, captured_attrs))
382 return false;
383
384 if (captured_params.at("reshape.shape").ai != std::vector<int>{0, 0, -1})
385 return false;
386
387 return true;
388 }
389};
390
391REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_B3, 10)
392
393class nn_RNN_onnx_4 : public nn_RNN_onnx_3
394{
395public:
396 const char* match_pattern_graph() const
397 {
398 return R"PNNXIR(7767517
3998 8
400pnnx.Input input_0 0 1 input
401pnnx.Input input_1 0 1 initial_h
402pnnx.Attribute W 0 1 W @data
403pnnx.Attribute R 0 1 R @data
404RNN rnn 4 2 input W R initial_h out outh %*=%*
405Transpose transpose 1 1 out out1 perm=(0,2,1,3)
406Reshape reshape 1 1 out1 out2 %*=%*
407pnnx.Output output 2 0 out2 outh
408)PNNXIR";
409 }
410};
411
412REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_4, 10)
413
414class nn_RNN_onnx_B4 : public nn_RNN_onnx_B3
415{
416public:
417 const char* match_pattern_graph() const
418 {
419 return R"PNNXIR(7767517
4209 9
421pnnx.Input input_0 0 1 input
422pnnx.Input input_1 0 1 initial_h
423pnnx.Attribute W 0 1 W @data
424pnnx.Attribute R 0 1 R @data
425pnnx.Attribute B 0 1 B @data
426RNN rnn 5 2 input W R B initial_h out outh %*=%*
427Transpose transpose 1 1 out out1 perm=(0,2,1,3)
428Reshape reshape 1 1 out1 out2 %*=%*
429pnnx.Output output 2 0 out2 outh
430)PNNXIR";
431 }
432};
433
434REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_B4, 10)
435
436class nn_RNN_onnx_5 : public nn_RNN_onnx_3
437{
438public:
439 const char* match_pattern_graph() const
440 {
441 return R"PNNXIR(7767517
4428 7
443pnnx.Input input_0 0 1 input
444pnnx.Input input_1 0 1 initial_h
445pnnx.Attribute W 0 1 W @data
446pnnx.Attribute R 0 1 R @data
447RNN rnn 4 1 input W R initial_h out %*=%*
448Transpose transpose 1 1 out out1 perm=(0,2,1,3)
449Reshape reshape 1 1 out1 out2 %*=%*
450pnnx.Output output 1 0 out2
451)PNNXIR";
452 }
453};
454
455REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_5, 10)
456
457class nn_RNN_onnx_B5 : public nn_RNN_onnx_B3
458{
459public:
460 const char* match_pattern_graph() const
461 {
462 return R"PNNXIR(7767517
4639 8
464pnnx.Input input_0 0 1 input
465pnnx.Input input_1 0 1 initial_h
466pnnx.Attribute W 0 1 W @data
467pnnx.Attribute R 0 1 R @data
468pnnx.Attribute B 0 1 B @data
469RNN rnn 5 1 input W R B initial_h out %*=%*
470Transpose transpose 1 1 out out1 perm=(0,2,1,3)
471Reshape reshape 1 1 out1 out2 %*=%*
472pnnx.Output output 1 0 out2
473)PNNXIR";
474 }
475};
476
477REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_B5, 10)
478
479} // namespace pnnx