| 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 | |
| 18 | namespace pnnx { |
| 19 | |
| 20 | class nn_GRU_onnx : public GraphRewriterPass |
| 21 | { |
| 22 | public: |
| 23 | const char* match_pattern_graph() const |
| 24 | { |
| 25 | return R"PNNXIR(7767517 |
| 26 | 6 5 |
| 27 | pnnx.Input input_0 0 1 input |
| 28 | pnnx.Attribute W 0 1 W @data |
| 29 | pnnx.Attribute R 0 1 R @data |
| 30 | GRU gru 3 1 input W R out %*=%* |
| 31 | Squeeze sqz 1 1 out out1 axes=%axes |
| 32 | pnnx.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 | |
| 218 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx, 10) |
| 219 | |
| 220 | class nn_GRU_onnx_B : public nn_GRU_onnx |
| 221 | { |
| 222 | public: |
| 223 | const char* match_pattern_graph() const |
| 224 | { |
| 225 | return R"PNNXIR(7767517 |
| 226 | 7 6 |
| 227 | pnnx.Input input_0 0 1 input |
| 228 | pnnx.Attribute W 0 1 W @data |
| 229 | pnnx.Attribute R 0 1 R @data |
| 230 | pnnx.Attribute B 0 1 B @data |
| 231 | GRU gru 4 1 input W R B out %*=%* |
| 232 | Squeeze sqz 1 1 out out1 axes=%axes |
| 233 | pnnx.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 | |
| 366 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_B, 10) |
| 367 | |
| 368 | class nn_GRU_onnx_1 : public nn_GRU_onnx |
| 369 | { |
| 370 | public: |
| 371 | const char* match_pattern_graph() const |
| 372 | { |
| 373 | return R"PNNXIR(7767517 |
| 374 | 7 8 |
| 375 | pnnx.Input input_0 0 1 input |
| 376 | pnnx.Input input_1 0 1 initial_h |
| 377 | pnnx.Attribute W 0 1 W @data |
| 378 | pnnx.Attribute R 0 1 R @data |
| 379 | GRU gru 4 2 input W R initial_h out outh %*=%* |
| 380 | Squeeze sqz 1 1 out out1 axes=%axes |
| 381 | pnnx.Output output 2 0 out1 outh |
| 382 | )PNNXIR"; |
| 383 | } |
| 384 | }; |
| 385 | |
| 386 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_1, 10) |
| 387 | |
| 388 | class nn_GRU_onnx_B1 : public nn_GRU_onnx_B |
| 389 | { |
| 390 | public: |
| 391 | const char* match_pattern_graph() const |
| 392 | { |
| 393 | return R"PNNXIR(7767517 |
| 394 | 8 9 |
| 395 | pnnx.Input input_0 0 1 input |
| 396 | pnnx.Input input_1 0 1 initial_h |
| 397 | pnnx.Attribute W 0 1 W @data |
| 398 | pnnx.Attribute R 0 1 R @data |
| 399 | pnnx.Attribute B 0 1 B @data |
| 400 | GRU gru 5 2 input W R B initial_h out outh %*=%* |
| 401 | Squeeze sqz 1 1 out out1 axes=%axes |
| 402 | pnnx.Output output 2 0 out1 outh |
| 403 | )PNNXIR"; |
| 404 | } |
| 405 | }; |
| 406 | |
| 407 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_B1, 10) |
| 408 | |
| 409 | class nn_GRU_onnx_2 : public nn_GRU_onnx |
| 410 | { |
| 411 | public: |
| 412 | const char* match_pattern_graph() const |
| 413 | { |
| 414 | return R"PNNXIR(7767517 |
| 415 | 7 6 |
| 416 | pnnx.Input input_0 0 1 input |
| 417 | pnnx.Input input_1 0 1 initial_h |
| 418 | pnnx.Attribute W 0 1 W @data |
| 419 | pnnx.Attribute R 0 1 R @data |
| 420 | GRU gru 4 1 input W R initial_h out %*=%* |
| 421 | Squeeze sqz 1 1 out out1 axes=%axes |
| 422 | pnnx.Output output 1 0 out1 |
| 423 | )PNNXIR"; |
| 424 | } |
| 425 | }; |
| 426 | |
| 427 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_2, 10) |
| 428 | |
| 429 | class nn_GRU_onnx_B2 : public nn_GRU_onnx_B |
| 430 | { |
| 431 | public: |
| 432 | const char* match_pattern_graph() const |
| 433 | { |
| 434 | return R"PNNXIR(7767517 |
| 435 | 8 7 |
| 436 | pnnx.Input input_0 0 1 input |
| 437 | pnnx.Input input_1 0 1 initial_h |
| 438 | pnnx.Attribute W 0 1 W @data |
| 439 | pnnx.Attribute R 0 1 R @data |
| 440 | pnnx.Attribute B 0 1 B @data |
| 441 | GRU gru 5 1 input W R B initial_h out %*=%* |
| 442 | Squeeze sqz 1 1 out out1 axes=%axes |
| 443 | pnnx.Output output 1 0 out1 |
| 444 | )PNNXIR"; |
| 445 | } |
| 446 | }; |
| 447 | |
| 448 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_B2, 10) |
| 449 | |
| 450 | class nn_GRU_onnx_3 : public nn_GRU_onnx |
| 451 | { |
| 452 | public: |
| 453 | const char* match_pattern_graph() const |
| 454 | { |
| 455 | return R"PNNXIR(7767517 |
| 456 | 7 6 |
| 457 | pnnx.Input input_0 0 1 input |
| 458 | pnnx.Attribute W 0 1 W @data |
| 459 | pnnx.Attribute R 0 1 R @data |
| 460 | GRU gru 3 1 input W R out %*=%* |
| 461 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 462 | Reshape reshape 1 1 out1 out2 %*=%* |
| 463 | pnnx.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 | |
| 479 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_3, 10) |
| 480 | |
| 481 | class nn_GRU_onnx_B3 : public nn_GRU_onnx_B |
| 482 | { |
| 483 | public: |
| 484 | const char* match_pattern_graph() const |
| 485 | { |
| 486 | return R"PNNXIR(7767517 |
| 487 | 8 7 |
| 488 | pnnx.Input input_0 0 1 input |
| 489 | pnnx.Attribute W 0 1 W @data |
| 490 | pnnx.Attribute R 0 1 R @data |
| 491 | pnnx.Attribute B 0 1 B @data |
| 492 | GRU gru 4 1 input W R B out %*=%* |
| 493 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 494 | Reshape reshape 1 1 out1 out2 %*=%* |
| 495 | pnnx.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 | |
| 511 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_B3, 10) |
| 512 | |
| 513 | class nn_GRU_onnx_4 : public nn_GRU_onnx_3 |
| 514 | { |
| 515 | public: |
| 516 | const char* match_pattern_graph() const |
| 517 | { |
| 518 | return R"PNNXIR(7767517 |
| 519 | 8 9 |
| 520 | pnnx.Input input_0 0 1 input |
| 521 | pnnx.Input input_1 0 1 initial_h |
| 522 | pnnx.Attribute W 0 1 W @data |
| 523 | pnnx.Attribute R 0 1 R @data |
| 524 | GRU gru 4 2 input W R initial_h out outh %*=%* |
| 525 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 526 | Reshape reshape 1 1 out1 out2 %*=%* |
| 527 | pnnx.Output output 2 0 out2 outh |
| 528 | )PNNXIR"; |
| 529 | } |
| 530 | }; |
| 531 | |
| 532 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_4, 10) |
| 533 | |
| 534 | class nn_GRU_onnx_B4 : public nn_GRU_onnx_B3 |
| 535 | { |
| 536 | public: |
| 537 | const char* match_pattern_graph() const |
| 538 | { |
| 539 | return R"PNNXIR(7767517 |
| 540 | 9 10 |
| 541 | pnnx.Input input_0 0 1 input |
| 542 | pnnx.Input input_1 0 1 initial_h |
| 543 | pnnx.Attribute W 0 1 W @data |
| 544 | pnnx.Attribute R 0 1 R @data |
| 545 | pnnx.Attribute B 0 1 B @data |
| 546 | GRU gru 5 2 input W R B initial_h out outh %*=%* |
| 547 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 548 | Reshape reshape 1 1 out1 out2 %*=%* |
| 549 | pnnx.Output output 2 0 out2 outh |
| 550 | )PNNXIR"; |
| 551 | } |
| 552 | }; |
| 553 | |
| 554 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_B4, 10) |
| 555 | |
| 556 | class nn_GRU_onnx_5 : public nn_GRU_onnx_3 |
| 557 | { |
| 558 | public: |
| 559 | const char* match_pattern_graph() const |
| 560 | { |
| 561 | return R"PNNXIR(7767517 |
| 562 | 8 7 |
| 563 | pnnx.Input input_0 0 1 input |
| 564 | pnnx.Input input_1 0 1 initial_h |
| 565 | pnnx.Attribute W 0 1 W @data |
| 566 | pnnx.Attribute R 0 1 R @data |
| 567 | GRU gru 4 1 input W R initial_h out %*=%* |
| 568 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 569 | Reshape reshape 1 1 out1 out2 %*=%* |
| 570 | pnnx.Output output 1 0 out2 |
| 571 | )PNNXIR"; |
| 572 | } |
| 573 | }; |
| 574 | |
| 575 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_5, 10) |
| 576 | |
| 577 | class nn_GRU_onnx_B5 : public nn_GRU_onnx_B3 |
| 578 | { |
| 579 | public: |
| 580 | const char* match_pattern_graph() const |
| 581 | { |
| 582 | return R"PNNXIR(7767517 |
| 583 | 9 8 |
| 584 | pnnx.Input input_0 0 1 input |
| 585 | pnnx.Input input_1 0 1 initial_h |
| 586 | pnnx.Attribute W 0 1 W @data |
| 587 | pnnx.Attribute R 0 1 R @data |
| 588 | pnnx.Attribute B 0 1 B @data |
| 589 | GRU gru 5 1 input W R B initial_h out %*=%* |
| 590 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 591 | Reshape reshape 1 1 out1 out2 %*=%* |
| 592 | pnnx.Output output 1 0 out2 |
| 593 | )PNNXIR"; |
| 594 | } |
| 595 | }; |
| 596 | |
| 597 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_GRU_onnx_B5, 10) |
| 598 | |
| 599 | } // namespace pnnx |