Elffuss BraidHub

kikocis/ncnn public

master · 21.2 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#include <string.h>
17
18namespace pnnx {
19
20class nn_GRU_onnx : public GraphRewriterPass
21{
22public:
23 const char* match_pattern_graph() const
24 {
25 return R"PNNXIR(7767517
266 5
27pnnx.Input input_0 0 1 input
28pnnx.Attribute W 0 1 W @data
29pnnx.Attribute R 0 1 R @data
30GRU gru 3 1 input W R out %*=%*
31Squeeze sqz 1 1 out out1 axes=%axes
32pnnx.Output output 1 0 out1
33)PNNXIR";
34 }
35
36 const char* type_str() const
37 {
38 return "nn.GRU";
39 }
40
41 const char* name_str() const
42 {
43 return "gru";
44 }
45
46 bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
47 {
48 if (captured_params.find("gru.hidden_size") == captured_params.end())
49 return false;
50
51 const int hidden_size = captured_params.at("gru.hidden_size").i;
52
53 std::string direction = "forward";
54 if (captured_params.find("gru.direction") != captured_params.end())
55 {
56 direction = captured_params.at("gru.direction").s;
57 }
58
59 if (direction != "forward" && direction != "bidirectional")
60 return false;
61
62 const int num_directions = direction == "bidirectional" ? 2 : 1;
63
64 if (captured_params.find("gru.activations") != captured_params.end())
65 {
66 const std::vector<std::string>& acts = captured_params.at("gru.activations").as;
67
68 if (num_directions == 1)
69 {
70 if (acts != std::vector<std::string>{"Sigmoid", "Tanh"})
71 return false;
72 }
73 else // if (num_directions == 2)
74 {
75 if (acts != std::vector<std::string>{"Sigmoid", "Tanh", "Sigmoid", "Tanh"})
76 return false;
77 }
78 }
79
80 if (captured_params.find("axes") != captured_params.end())
81 {
82 if (captured_params.at("axes").type == 2 && captured_params.at("axes").i != 1)
83 return false;
84
85 if (captured_params.at("axes").type == 5 && captured_params.at("axes").ai != std::vector<int>{1})
86 return false;
87 }
88
89 const auto& W = captured_attrs.at("W.data");
90 const auto& R = captured_attrs.at("R.data");
91
92 if (W.shape.size() != 3 || W.shape[0] != num_directions || W.shape[1] != 3 * hidden_size)
93 return false;
94
95 if (R.shape.size() != 3 || R.shape[0] != num_directions || R.shape[1] != 3 * hidden_size || R.shape[2] != hidden_size)
96 return false;
97
98 return true;
99 }
100
101 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
102 {
103 std::string direction = "forward";
104 if (captured_params.find("gru.direction") != captured_params.end())
105 {
106 direction = captured_params.at("gru.direction").s;
107 }
108
109 const auto& W = captured_attrs.at("W.data");
110 const auto& R = captured_attrs.at("R.data");
111
112 bool batch_first = false;
113 if (captured_params.find("gru.layout") != captured_params.end())
114 {
115 const int layout = captured_params.at("gru.layout").i;
116 batch_first = layout == 1;
117 }
118
119 const int hidden_size = captured_params.at("gru.hidden_size").i;
120
121 const int input_size = W.shape[2];
122
123 op->params["input_size"] = input_size;
124 op->params["hidden_size"] = hidden_size;
125 op->params["num_layers"] = 1;
126 op->params["bias"] = false;
127 op->params["batch_first"] = batch_first;
128 op->params["bidirectional"] = direction == "bidirectional" ? true : false;
129
130 // split W R and reorder URN to RUN
131 auto W_data = W.get_float32_data();
132 auto R_data = R.get_float32_data();
133
134 std::vector<float> W2(3 * hidden_size * input_size);
135 {
136 const int weight_data_size_g = hidden_size * input_size;
137
138 const float* uptr = (const float*)W_data.data();
139 const float* rptr = (const float*)W_data.data() + weight_data_size_g;
140 const float* nptr = (const float*)W_data.data() + weight_data_size_g * 2;
141
142 float* w_rptr = (float*)W2.data();
143 float* w_uptr = (float*)W2.data() + weight_data_size_g;
144 float* w_nptr = (float*)W2.data() + weight_data_size_g * 2;
145
146 memcpy(w_rptr, rptr, weight_data_size_g * sizeof(float));
147 memcpy(w_uptr, uptr, weight_data_size_g * sizeof(float));
148 memcpy(w_nptr, nptr, weight_data_size_g * sizeof(float));
149 }
150
151 std::vector<float> R2(3 * hidden_size * hidden_size);
152 {
153 const int weight_data_size_g = hidden_size * hidden_size;
154
155 const float* uptr = (const float*)R_data.data();
156 const float* rptr = (const float*)R_data.data() + weight_data_size_g;
157 const float* nptr = (const float*)R_data.data() + weight_data_size_g * 2;
158
159 float* w_rptr = (float*)R2.data();
160 float* w_uptr = (float*)R2.data() + weight_data_size_g;
161 float* w_nptr = (float*)R2.data() + weight_data_size_g * 2;
162
163 memcpy(w_rptr, rptr, weight_data_size_g * sizeof(float));
164 memcpy(w_uptr, uptr, weight_data_size_g * sizeof(float));
165 memcpy(w_nptr, nptr, weight_data_size_g * sizeof(float));
166 }
167
168 if (direction == "bidirectional")
169 {
170 op->attrs["weight_ih_l0"] = Attribute({3 * hidden_size, input_size}, W2);
171 op->attrs["weight_hh_l0"] = Attribute({3 * hidden_size, hidden_size}, R2);
172
173 std::vector<float> W2R(3 * hidden_size * input_size);
174 {
175 const int weight_data_size_g = hidden_size * input_size;
176
177 const float* uptr = (const float*)W_data.data() + weight_data_size_g * 3;
178 const float* rptr = (const float*)W_data.data() + weight_data_size_g * 4;
179 const float* nptr = (const float*)W_data.data() + weight_data_size_g * 5;
180
181 float* w_rptr = (float*)W2R.data();
182 float* w_uptr = (float*)W2R.data() + weight_data_size_g;
183 float* w_nptr = (float*)W2R.data() + weight_data_size_g * 2;
184
185 memcpy(w_rptr, rptr, weight_data_size_g * sizeof(float));
186 memcpy(w_uptr, uptr, weight_data_size_g * sizeof(float));
187 memcpy(w_nptr, nptr, weight_data_size_g * sizeof(float));
188 }
189
190 std::vector<float> R2R(3 * hidden_size * hidden_size);
191 {
192 const int weight_data_size_g = hidden_size * hidden_size;
193
194 const float* uptr = (const float*)R_data.data() + weight_data_size_g * 3;
195 const float* rptr = (const float*)R_data.data() + weight_data_size_g * 4;
196 const float* nptr = (const float*)R_data.data() + weight_data_size_g * 5;
197
198 float* w_rptr = (float*)R2R.data();
199 float* w_uptr = (float*)R2R.data() + weight_data_size_g;
200 float* w_nptr = (float*)R2R.data() + weight_data_size_g * 2;
201
202 memcpy(w_rptr, rptr, weight_data_size_g * sizeof(float));
203 memcpy(w_uptr, uptr, weight_data_size_g * sizeof(float));
204 memcpy(w_nptr, nptr, weight_data_size_g * sizeof(float));
205 }
206
207 op->attrs["weight_ih_l0_reverse"] = Attribute({3 * hidden_size, input_size}, W2R);
208 op->attrs["weight_hh_l0_reverse"] = Attribute({3 * hidden_size, hidden_size}, R2R);
209 }
210 else
211 {
212 op->attrs["weight_ih_l0"] = Attribute({3 * hidden_size, input_size}, W2);
213 op->attrs["weight_hh_l0"] = Attribute({3 * hidden_size, hidden_size}, R2);
214 }
215 }
216};
217
218REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx, 10)
219
220class nn_GRU_onnx_B : public nn_GRU_onnx
221{
222public:
223 const char* match_pattern_graph() const
224 {
225 return R"PNNXIR(7767517
2267 6
227pnnx.Input input_0 0 1 input
228pnnx.Attribute W 0 1 W @data
229pnnx.Attribute R 0 1 R @data
230pnnx.Attribute B 0 1 B @data
231GRU gru 4 1 input W R B out %*=%*
232Squeeze sqz 1 1 out out1 axes=%axes
233pnnx.Output output 1 0 out1
234)PNNXIR";
235 }
236
237 bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
238 {
239 if (!nn_GRU_onnx::match(captured_params, captured_attrs))
240 return false;
241
242 const int hidden_size = captured_params.at("gru.hidden_size").i;
243
244 std::string direction = "forward";
245 if (captured_params.find("gru.direction") != captured_params.end())
246 {
247 direction = captured_params.at("gru.direction").s;
248 }
249
250 const int num_directions = direction == "bidirectional" ? 2 : 1;
251
252 const auto& B = captured_attrs.at("B.data");
253
254 if (B.shape.size() != 2 || B.shape[0] != num_directions || B.shape[1] != 6 * hidden_size)
255 return false;
256
257 return true;
258 }
259
260 void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
261 {
262 nn_GRU_onnx::write(op, captured_params, captured_attrs);
263
264 const auto& B = captured_attrs.at("B.data");
265
266 bool has_bias = false;
267 for (auto b : B.get_float32_data())
268 {
269 if (b != 0.f)
270 {
271 has_bias = true;
272 break;
273 }
274 }
275
276 op->params["bias"] = has_bias;
277
278 if (has_bias)
279 {
280 const int hidden_size = captured_params.at("gru.hidden_size").i;
281
282 // split B and reorder URN to RUN
283 auto B_data = B.get_float32_data();
284
285 std::vector<float> B2(3 * hidden_size);
286 std::vector<float> B3(3 * hidden_size);
287 {
288 const float* uptr = (const float*)B_data.data();
289 const float* rptr = (const float*)B_data.data() + hidden_size;
290 const float* nptr = (const float*)B_data.data() + hidden_size * 2;
291
292 float* w_rptr = (float*)B2.data();
293 float* w_uptr = (float*)B2.data() + hidden_size;
294 float* w_nptr = (float*)B2.data() + hidden_size * 2;
295
296 memcpy(w_rptr, rptr, hidden_size * sizeof(float));
297 memcpy(w_uptr, uptr, hidden_size * sizeof(float));
298 memcpy(w_nptr, nptr, hidden_size * sizeof(float));
299 }
300 {
301 const float* uptr = (const float*)B_data.data() + hidden_size * 3;
302 const float* rptr = (const float*)B_data.data() + hidden_size * 4;
303 const float* nptr = (const float*)B_data.data() + hidden_size * 5;
304
305 float* w_rptr = (float*)B3.data();
306 float* w_uptr = (float*)B3.data() + hidden_size;
307 float* w_nptr = (float*)B3.data() + hidden_size * 2;
308
309 memcpy(w_rptr, rptr, hidden_size * sizeof(float));
310 memcpy(w_uptr, uptr, hidden_size * sizeof(float));
311 memcpy(w_nptr, nptr, hidden_size * sizeof(float));
312 }
313
314 std::string direction = "forward";
315 if (captured_params.find("gru.direction") != captured_params.end())
316 {
317 direction = captured_params.at("gru.direction").s;
318 }
319
320 if (direction == "bidirectional")
321 {
322 op->attrs["bias_ih_l0"] = Attribute({3 * hidden_size}, B2);
323 op->attrs["bias_hh_l0"] = Attribute({3 * hidden_size}, B3);
324
325 std::vector<float> B2R(3 * hidden_size);
326 std::vector<float> B3R(3 * hidden_size);
327 {
328 const float* uptr = (const float*)B_data.data() + hidden_size * 6;
329 const float* rptr = (const float*)B_data.data() + hidden_size * 7;
330 const float* nptr = (const float*)B_data.data() + hidden_size * 8;
331
332 float* w_rptr = (float*)B2R.data();
333 float* w_uptr = (float*)B2R.data() + hidden_size;
334 float* w_nptr = (float*)B2R.data() + hidden_size * 2;
335
336 memcpy(w_rptr, rptr, hidden_size * sizeof(float));
337 memcpy(w_uptr, uptr, hidden_size * sizeof(float));
338 memcpy(w_nptr, nptr, hidden_size * sizeof(float));
339 }
340 {
341 const float* uptr = (const float*)B_data.data() + hidden_size * 9;
342 const float* rptr = (const float*)B_data.data() + hidden_size * 10;
343 const float* nptr = (const float*)B_data.data() + hidden_size * 11;
344
345 float* w_rptr = (float*)B3R.data();
346 float* w_uptr = (float*)B3R.data() + hidden_size;
347 float* w_nptr = (float*)B3R.data() + hidden_size * 2;
348
349 memcpy(w_rptr, rptr, hidden_size * sizeof(float));
350 memcpy(w_uptr, uptr, hidden_size * sizeof(float));
351 memcpy(w_nptr, nptr, hidden_size * sizeof(float));
352 }
353
354 op->attrs["bias_ih_l0_reverse"] = Attribute({3 * hidden_size}, B2R);
355 op->attrs["bias_hh_l0_reverse"] = Attribute({3 * hidden_size}, B3R);
356 }
357 else
358 {
359 op->attrs["bias_ih_l0"] = Attribute({3 * hidden_size}, B2);
360 op->attrs["bias_hh_l0"] = Attribute({3 * hidden_size}, B3);
361 }
362 }
363 }
364};
365
366REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_B, 10)
367
368class nn_GRU_onnx_1 : public nn_GRU_onnx
369{
370public:
371 const char* match_pattern_graph() const
372 {
373 return R"PNNXIR(7767517
3747 8
375pnnx.Input input_0 0 1 input
376pnnx.Input input_1 0 1 initial_h
377pnnx.Attribute W 0 1 W @data
378pnnx.Attribute R 0 1 R @data
379GRU gru 4 2 input W R initial_h out outh %*=%*
380Squeeze sqz 1 1 out out1 axes=%axes
381pnnx.Output output 2 0 out1 outh
382)PNNXIR";
383 }
384};
385
386REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_1, 10)
387
388class nn_GRU_onnx_B1 : public nn_GRU_onnx_B
389{
390public:
391 const char* match_pattern_graph() const
392 {
393 return R"PNNXIR(7767517
3948 9
395pnnx.Input input_0 0 1 input
396pnnx.Input input_1 0 1 initial_h
397pnnx.Attribute W 0 1 W @data
398pnnx.Attribute R 0 1 R @data
399pnnx.Attribute B 0 1 B @data
400GRU gru 5 2 input W R B initial_h out outh %*=%*
401Squeeze sqz 1 1 out out1 axes=%axes
402pnnx.Output output 2 0 out1 outh
403)PNNXIR";
404 }
405};
406
407REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_B1, 10)
408
409class nn_GRU_onnx_2 : public nn_GRU_onnx
410{
411public:
412 const char* match_pattern_graph() const
413 {
414 return R"PNNXIR(7767517
4157 6
416pnnx.Input input_0 0 1 input
417pnnx.Input input_1 0 1 initial_h
418pnnx.Attribute W 0 1 W @data
419pnnx.Attribute R 0 1 R @data
420GRU gru 4 1 input W R initial_h out %*=%*
421Squeeze sqz 1 1 out out1 axes=%axes
422pnnx.Output output 1 0 out1
423)PNNXIR";
424 }
425};
426
427REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_2, 10)
428
429class nn_GRU_onnx_B2 : public nn_GRU_onnx_B
430{
431public:
432 const char* match_pattern_graph() const
433 {
434 return R"PNNXIR(7767517
4358 7
436pnnx.Input input_0 0 1 input
437pnnx.Input input_1 0 1 initial_h
438pnnx.Attribute W 0 1 W @data
439pnnx.Attribute R 0 1 R @data
440pnnx.Attribute B 0 1 B @data
441GRU gru 5 1 input W R B initial_h out %*=%*
442Squeeze sqz 1 1 out out1 axes=%axes
443pnnx.Output output 1 0 out1
444)PNNXIR";
445 }
446};
447
448REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_B2, 10)
449
450class nn_GRU_onnx_3 : public nn_GRU_onnx
451{
452public:
453 const char* match_pattern_graph() const
454 {
455 return R"PNNXIR(7767517
4567 6
457pnnx.Input input_0 0 1 input
458pnnx.Attribute W 0 1 W @data
459pnnx.Attribute R 0 1 R @data
460GRU gru 3 1 input W R out %*=%*
461Transpose transpose 1 1 out out1 perm=(0,2,1,3)
462Reshape reshape 1 1 out1 out2 %*=%*
463pnnx.Output output 1 0 out2
464)PNNXIR";
465 }
466
467 bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
468 {
469 if (!nn_GRU_onnx::match(captured_params, captured_attrs))
470 return false;
471
472 if (captured_params.at("reshape.shape").ai != std::vector<int>{0, 0, -1})
473 return false;
474
475 return true;
476 }
477};
478
479REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_3, 10)
480
481class nn_GRU_onnx_B3 : public nn_GRU_onnx_B
482{
483public:
484 const char* match_pattern_graph() const
485 {
486 return R"PNNXIR(7767517
4878 7
488pnnx.Input input_0 0 1 input
489pnnx.Attribute W 0 1 W @data
490pnnx.Attribute R 0 1 R @data
491pnnx.Attribute B 0 1 B @data
492GRU gru 4 1 input W R B out %*=%*
493Transpose transpose 1 1 out out1 perm=(0,2,1,3)
494Reshape reshape 1 1 out1 out2 %*=%*
495pnnx.Output output 1 0 out2
496)PNNXIR";
497 }
498
499 bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const
500 {
501 if (!nn_GRU_onnx_B::match(captured_params, captured_attrs))
502 return false;
503
504 if (captured_params.at("reshape.shape").ai != std::vector<int>{0, 0, -1})
505 return false;
506
507 return true;
508 }
509};
510
511REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_B3, 10)
512
513class nn_GRU_onnx_4 : public nn_GRU_onnx_3
514{
515public:
516 const char* match_pattern_graph() const
517 {
518 return R"PNNXIR(7767517
5198 9
520pnnx.Input input_0 0 1 input
521pnnx.Input input_1 0 1 initial_h
522pnnx.Attribute W 0 1 W @data
523pnnx.Attribute R 0 1 R @data
524GRU gru 4 2 input W R initial_h out outh %*=%*
525Transpose transpose 1 1 out out1 perm=(0,2,1,3)
526Reshape reshape 1 1 out1 out2 %*=%*
527pnnx.Output output 2 0 out2 outh
528)PNNXIR";
529 }
530};
531
532REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_4, 10)
533
534class nn_GRU_onnx_B4 : public nn_GRU_onnx_B3
535{
536public:
537 const char* match_pattern_graph() const
538 {
539 return R"PNNXIR(7767517
5409 10
541pnnx.Input input_0 0 1 input
542pnnx.Input input_1 0 1 initial_h
543pnnx.Attribute W 0 1 W @data
544pnnx.Attribute R 0 1 R @data
545pnnx.Attribute B 0 1 B @data
546GRU gru 5 2 input W R B initial_h out outh %*=%*
547Transpose transpose 1 1 out out1 perm=(0,2,1,3)
548Reshape reshape 1 1 out1 out2 %*=%*
549pnnx.Output output 2 0 out2 outh
550)PNNXIR";
551 }
552};
553
554REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_B4, 10)
555
556class nn_GRU_onnx_5 : public nn_GRU_onnx_3
557{
558public:
559 const char* match_pattern_graph() const
560 {
561 return R"PNNXIR(7767517
5628 7
563pnnx.Input input_0 0 1 input
564pnnx.Input input_1 0 1 initial_h
565pnnx.Attribute W 0 1 W @data
566pnnx.Attribute R 0 1 R @data
567GRU gru 4 1 input W R initial_h out %*=%*
568Transpose transpose 1 1 out out1 perm=(0,2,1,3)
569Reshape reshape 1 1 out1 out2 %*=%*
570pnnx.Output output 1 0 out2
571)PNNXIR";
572 }
573};
574
575REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_5, 10)
576
577class nn_GRU_onnx_B5 : public nn_GRU_onnx_B3
578{
579public:
580 const char* match_pattern_graph() const
581 {
582 return R"PNNXIR(7767517
5839 8
584pnnx.Input input_0 0 1 input
585pnnx.Input input_1 0 1 initial_h
586pnnx.Attribute W 0 1 W @data
587pnnx.Attribute R 0 1 R @data
588pnnx.Attribute B 0 1 B @data
589GRU gru 5 1 input W R B initial_h out %*=%*
590Transpose transpose 1 1 out out1 perm=(0,2,1,3)
591Reshape reshape 1 1 out1 out2 %*=%*
592pnnx.Output output 1 0 out2
593)PNNXIR";
594 }
595};
596
597REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_B5, 10)
598
599} // namespace pnnx