| 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_LSTM_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 | LSTM lstm 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.LSTM"; |
| 39 | } |
| 40 | |
| 41 | const char* name_str() const |
| 42 | { |
| 43 | return "lstm"; |
| 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("lstm.hidden_size") == captured_params.end()) |
| 49 | return false; |
| 50 | |
| 51 | const int hidden_size = captured_params.at("lstm.hidden_size").i; |
| 52 | |
| 53 | std::string direction = "forward"; |
| 54 | if (captured_params.find("lstm.direction") != captured_params.end()) |
| 55 | { |
| 56 | direction = captured_params.at("lstm.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("lstm.activations") != captured_params.end()) |
| 65 | { |
| 66 | const std::vector<std::string>& acts = captured_params.at("lstm.activations").as; |
| 67 | |
| 68 | if (num_directions == 1) |
| 69 | { |
| 70 | if (acts != std::vector<std::string>{"Sigmoid", "Tanh", "Tanh"}) |
| 71 | return false; |
| 72 | } |
| 73 | else // if (num_directions == 2) |
| 74 | { |
| 75 | if (acts != std::vector<std::string>{"Sigmoid", "Tanh", "Tanh", "Sigmoid", "Tanh", "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] != 4 * hidden_size) |
| 93 | return false; |
| 94 | |
| 95 | if (R.shape.size() != 3 || R.shape[0] != num_directions || R.shape[1] != 4 * 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("lstm.direction") != captured_params.end()) |
| 105 | { |
| 106 | direction = captured_params.at("lstm.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("lstm.layout") != captured_params.end()) |
| 114 | { |
| 115 | const int layout = captured_params.at("lstm.layout").i; |
| 116 | batch_first = layout == 1; |
| 117 | } |
| 118 | |
| 119 | const int hidden_size = captured_params.at("lstm.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 | op->params["proj_size"] = 0; |
| 130 | |
| 131 | // split W R and reorder IOFG to IFGO |
| 132 | auto W_data = W.get_float32_data(); |
| 133 | auto R_data = R.get_float32_data(); |
| 134 | |
| 135 | std::vector<float> W2(4 * hidden_size * input_size); |
| 136 | { |
| 137 | const int weight_data_size_g = hidden_size * input_size; |
| 138 | |
| 139 | const float* iptr = (const float*)W_data.data(); |
| 140 | const float* optr = (const float*)W_data.data() + weight_data_size_g; |
| 141 | const float* fptr = (const float*)W_data.data() + weight_data_size_g * 2; |
| 142 | const float* gptr = (const float*)W_data.data() + weight_data_size_g * 3; |
| 143 | |
| 144 | float* w_iptr = (float*)W2.data(); |
| 145 | float* w_fptr = (float*)W2.data() + weight_data_size_g; |
| 146 | float* w_gptr = (float*)W2.data() + weight_data_size_g * 2; |
| 147 | float* w_optr = (float*)W2.data() + weight_data_size_g * 3; |
| 148 | |
| 149 | memcpy(w_iptr, iptr, weight_data_size_g * sizeof(float)); |
| 150 | memcpy(w_fptr, fptr, weight_data_size_g * sizeof(float)); |
| 151 | memcpy(w_gptr, gptr, weight_data_size_g * sizeof(float)); |
| 152 | memcpy(w_optr, optr, weight_data_size_g * sizeof(float)); |
| 153 | } |
| 154 | |
| 155 | std::vector<float> R2(4 * hidden_size * hidden_size); |
| 156 | { |
| 157 | const int weight_data_size_g = hidden_size * hidden_size; |
| 158 | |
| 159 | const float* iptr = (const float*)R_data.data(); |
| 160 | const float* optr = (const float*)R_data.data() + weight_data_size_g; |
| 161 | const float* fptr = (const float*)R_data.data() + weight_data_size_g * 2; |
| 162 | const float* gptr = (const float*)R_data.data() + weight_data_size_g * 3; |
| 163 | |
| 164 | float* w_iptr = (float*)R2.data(); |
| 165 | float* w_fptr = (float*)R2.data() + weight_data_size_g; |
| 166 | float* w_gptr = (float*)R2.data() + weight_data_size_g * 2; |
| 167 | float* w_optr = (float*)R2.data() + weight_data_size_g * 3; |
| 168 | |
| 169 | memcpy(w_iptr, iptr, weight_data_size_g * sizeof(float)); |
| 170 | memcpy(w_fptr, fptr, weight_data_size_g * sizeof(float)); |
| 171 | memcpy(w_gptr, gptr, weight_data_size_g * sizeof(float)); |
| 172 | memcpy(w_optr, optr, weight_data_size_g * sizeof(float)); |
| 173 | } |
| 174 | |
| 175 | if (direction == "bidirectional") |
| 176 | { |
| 177 | op->attrs["weight_ih_l0"] = Attribute({4 * hidden_size, input_size}, W2); |
| 178 | op->attrs["weight_hh_l0"] = Attribute({4 * hidden_size, hidden_size}, R2); |
| 179 | |
| 180 | std::vector<float> W2R(4 * hidden_size * input_size); |
| 181 | { |
| 182 | const int weight_data_size_g = hidden_size * input_size; |
| 183 | |
| 184 | const float* iptr = (const float*)W_data.data() + weight_data_size_g * 4; |
| 185 | const float* optr = (const float*)W_data.data() + weight_data_size_g * 5; |
| 186 | const float* fptr = (const float*)W_data.data() + weight_data_size_g * 6; |
| 187 | const float* gptr = (const float*)W_data.data() + weight_data_size_g * 7; |
| 188 | |
| 189 | float* w_iptr = (float*)W2R.data(); |
| 190 | float* w_fptr = (float*)W2R.data() + weight_data_size_g; |
| 191 | float* w_gptr = (float*)W2R.data() + weight_data_size_g * 2; |
| 192 | float* w_optr = (float*)W2R.data() + weight_data_size_g * 3; |
| 193 | |
| 194 | memcpy(w_iptr, iptr, weight_data_size_g * sizeof(float)); |
| 195 | memcpy(w_fptr, fptr, weight_data_size_g * sizeof(float)); |
| 196 | memcpy(w_gptr, gptr, weight_data_size_g * sizeof(float)); |
| 197 | memcpy(w_optr, optr, weight_data_size_g * sizeof(float)); |
| 198 | } |
| 199 | |
| 200 | std::vector<float> R2R(4 * hidden_size * hidden_size); |
| 201 | { |
| 202 | const int weight_data_size_g = hidden_size * hidden_size; |
| 203 | |
| 204 | const float* iptr = (const float*)R_data.data() + weight_data_size_g * 4; |
| 205 | const float* optr = (const float*)R_data.data() + weight_data_size_g * 5; |
| 206 | const float* fptr = (const float*)R_data.data() + weight_data_size_g * 6; |
| 207 | const float* gptr = (const float*)R_data.data() + weight_data_size_g * 7; |
| 208 | |
| 209 | float* w_iptr = (float*)R2R.data(); |
| 210 | float* w_fptr = (float*)R2R.data() + weight_data_size_g; |
| 211 | float* w_gptr = (float*)R2R.data() + weight_data_size_g * 2; |
| 212 | float* w_optr = (float*)R2R.data() + weight_data_size_g * 3; |
| 213 | |
| 214 | memcpy(w_iptr, iptr, weight_data_size_g * sizeof(float)); |
| 215 | memcpy(w_fptr, fptr, weight_data_size_g * sizeof(float)); |
| 216 | memcpy(w_gptr, gptr, weight_data_size_g * sizeof(float)); |
| 217 | memcpy(w_optr, optr, weight_data_size_g * sizeof(float)); |
| 218 | } |
| 219 | |
| 220 | op->attrs["weight_ih_l0_reverse"] = Attribute({4 * hidden_size, input_size}, W2R); |
| 221 | op->attrs["weight_hh_l0_reverse"] = Attribute({4 * hidden_size, hidden_size}, R2R); |
| 222 | } |
| 223 | else |
| 224 | { |
| 225 | op->attrs["weight_ih_l0"] = Attribute({4 * hidden_size, input_size}, W2); |
| 226 | op->attrs["weight_hh_l0"] = Attribute({4 * hidden_size, hidden_size}, R2); |
| 227 | } |
| 228 | } |
| 229 | }; |
| 230 | |
| 231 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_LSTM_onnx, 10) |
| 232 | |
| 233 | class nn_LSTM_onnx_B : public nn_LSTM_onnx |
| 234 | { |
| 235 | public: |
| 236 | const char* match_pattern_graph() const |
| 237 | { |
| 238 | return R"PNNXIR(7767517 |
| 239 | 7 6 |
| 240 | pnnx.Input input_0 0 1 input |
| 241 | pnnx.Attribute W 0 1 W @data |
| 242 | pnnx.Attribute R 0 1 R @data |
| 243 | pnnx.Attribute B 0 1 B @data |
| 244 | LSTM lstm 4 1 input W R B out %*=%* |
| 245 | Squeeze sqz 1 1 out out1 axes=%axes |
| 246 | pnnx.Output output 1 0 out1 |
| 247 | )PNNXIR"; |
| 248 | } |
| 249 | |
| 250 | bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 251 | { |
| 252 | if (!nn_LSTM_onnx::match(captured_params, captured_attrs)) |
| 253 | return false; |
| 254 | |
| 255 | const int hidden_size = captured_params.at("lstm.hidden_size").i; |
| 256 | |
| 257 | std::string direction = "forward"; |
| 258 | if (captured_params.find("lstm.direction") != captured_params.end()) |
| 259 | { |
| 260 | direction = captured_params.at("lstm.direction").s; |
| 261 | } |
| 262 | |
| 263 | const int num_directions = direction == "bidirectional" ? 2 : 1; |
| 264 | |
| 265 | const auto& B = captured_attrs.at("B.data"); |
| 266 | |
| 267 | if (B.shape.size() != 2 || B.shape[0] != num_directions || B.shape[1] != 8 * hidden_size) |
| 268 | return false; |
| 269 | |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 274 | { |
| 275 | nn_LSTM_onnx::write(op, captured_params, captured_attrs); |
| 276 | |
| 277 | const auto& B = captured_attrs.at("B.data"); |
| 278 | |
| 279 | bool has_bias = false; |
| 280 | for (auto b : B.get_float32_data()) |
| 281 | { |
| 282 | if (b != 0.f) |
| 283 | { |
| 284 | has_bias = true; |
| 285 | break; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | op->params["bias"] = has_bias; |
| 290 | |
| 291 | if (has_bias) |
| 292 | { |
| 293 | const int hidden_size = captured_params.at("lstm.hidden_size").i; |
| 294 | |
| 295 | // split B and reorder IOFG to IFGO |
| 296 | auto B_data = B.get_float32_data(); |
| 297 | |
| 298 | std::vector<float> B2(4 * hidden_size); |
| 299 | std::vector<float> B3(4 * hidden_size); |
| 300 | { |
| 301 | const float* iptr = (const float*)B_data.data(); |
| 302 | const float* optr = (const float*)B_data.data() + hidden_size; |
| 303 | const float* fptr = (const float*)B_data.data() + hidden_size * 2; |
| 304 | const float* gptr = (const float*)B_data.data() + hidden_size * 3; |
| 305 | |
| 306 | float* w_iptr = (float*)B2.data(); |
| 307 | float* w_fptr = (float*)B2.data() + hidden_size; |
| 308 | float* w_gptr = (float*)B2.data() + hidden_size * 2; |
| 309 | float* w_optr = (float*)B2.data() + hidden_size * 3; |
| 310 | |
| 311 | memcpy(w_iptr, iptr, hidden_size * sizeof(float)); |
| 312 | memcpy(w_fptr, fptr, hidden_size * sizeof(float)); |
| 313 | memcpy(w_gptr, gptr, hidden_size * sizeof(float)); |
| 314 | memcpy(w_optr, optr, hidden_size * sizeof(float)); |
| 315 | } |
| 316 | { |
| 317 | const float* iptr = (const float*)B_data.data() + hidden_size * 4; |
| 318 | const float* optr = (const float*)B_data.data() + hidden_size * 5; |
| 319 | const float* fptr = (const float*)B_data.data() + hidden_size * 6; |
| 320 | const float* gptr = (const float*)B_data.data() + hidden_size * 7; |
| 321 | |
| 322 | float* w_iptr = (float*)B3.data(); |
| 323 | float* w_fptr = (float*)B3.data() + hidden_size; |
| 324 | float* w_gptr = (float*)B3.data() + hidden_size * 2; |
| 325 | float* w_optr = (float*)B3.data() + hidden_size * 3; |
| 326 | |
| 327 | memcpy(w_iptr, iptr, hidden_size * sizeof(float)); |
| 328 | memcpy(w_fptr, fptr, hidden_size * sizeof(float)); |
| 329 | memcpy(w_gptr, gptr, hidden_size * sizeof(float)); |
| 330 | memcpy(w_optr, optr, hidden_size * sizeof(float)); |
| 331 | } |
| 332 | |
| 333 | std::string direction = "forward"; |
| 334 | if (captured_params.find("lstm.direction") != captured_params.end()) |
| 335 | { |
| 336 | direction = captured_params.at("lstm.direction").s; |
| 337 | } |
| 338 | |
| 339 | if (direction == "bidirectional") |
| 340 | { |
| 341 | op->attrs["bias_ih_l0"] = Attribute({4 * hidden_size}, B2); |
| 342 | op->attrs["bias_hh_l0"] = Attribute({4 * hidden_size}, B3); |
| 343 | |
| 344 | std::vector<float> B2R(4 * hidden_size); |
| 345 | std::vector<float> B3R(4 * hidden_size); |
| 346 | { |
| 347 | const float* iptr = (const float*)B_data.data() + hidden_size * 8; |
| 348 | const float* optr = (const float*)B_data.data() + hidden_size * 9; |
| 349 | const float* fptr = (const float*)B_data.data() + hidden_size * 10; |
| 350 | const float* gptr = (const float*)B_data.data() + hidden_size * 11; |
| 351 | |
| 352 | float* w_iptr = (float*)B2R.data(); |
| 353 | float* w_fptr = (float*)B2R.data() + hidden_size; |
| 354 | float* w_gptr = (float*)B2R.data() + hidden_size * 2; |
| 355 | float* w_optr = (float*)B2R.data() + hidden_size * 3; |
| 356 | |
| 357 | memcpy(w_iptr, iptr, hidden_size * sizeof(float)); |
| 358 | memcpy(w_fptr, fptr, hidden_size * sizeof(float)); |
| 359 | memcpy(w_gptr, gptr, hidden_size * sizeof(float)); |
| 360 | memcpy(w_optr, optr, hidden_size * sizeof(float)); |
| 361 | } |
| 362 | { |
| 363 | const float* iptr = (const float*)B_data.data() + hidden_size * 12; |
| 364 | const float* optr = (const float*)B_data.data() + hidden_size * 13; |
| 365 | const float* fptr = (const float*)B_data.data() + hidden_size * 14; |
| 366 | const float* gptr = (const float*)B_data.data() + hidden_size * 15; |
| 367 | |
| 368 | float* w_iptr = (float*)B3R.data(); |
| 369 | float* w_fptr = (float*)B3R.data() + hidden_size; |
| 370 | float* w_gptr = (float*)B3R.data() + hidden_size * 2; |
| 371 | float* w_optr = (float*)B3R.data() + hidden_size * 3; |
| 372 | |
| 373 | memcpy(w_iptr, iptr, hidden_size * sizeof(float)); |
| 374 | memcpy(w_fptr, fptr, hidden_size * sizeof(float)); |
| 375 | memcpy(w_gptr, gptr, hidden_size * sizeof(float)); |
| 376 | memcpy(w_optr, optr, hidden_size * sizeof(float)); |
| 377 | } |
| 378 | |
| 379 | op->attrs["bias_ih_l0_reverse"] = Attribute({4 * hidden_size}, B2R); |
| 380 | op->attrs["bias_hh_l0_reverse"] = Attribute({4 * hidden_size}, B3R); |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | op->attrs["bias_ih_l0"] = Attribute({4 * hidden_size}, B2); |
| 385 | op->attrs["bias_hh_l0"] = Attribute({4 * hidden_size}, B3); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | }; |
| 390 | |
| 391 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_LSTM_onnx_B, 10) |
| 392 | |
| 393 | class nn_LSTM_onnx_1 : public nn_LSTM_onnx |
| 394 | { |
| 395 | public: |
| 396 | const char* match_pattern_graph() const |
| 397 | { |
| 398 | return R"PNNXIR(7767517 |
| 399 | 8 9 |
| 400 | pnnx.Input input_0 0 1 input |
| 401 | pnnx.Input input_1 0 1 initial_h |
| 402 | pnnx.Input input_2 0 1 initial_c |
| 403 | pnnx.Attribute W 0 1 W @data |
| 404 | pnnx.Attribute R 0 1 R @data |
| 405 | LSTM lstm 5 3 input W R initial_h initial_c out outh outc %*=%* |
| 406 | Squeeze sqz 1 1 out out1 axes=%axes |
| 407 | pnnx.Output output 3 0 out1 outh outc |
| 408 | )PNNXIR"; |
| 409 | } |
| 410 | }; |
| 411 | |
| 412 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_LSTM_onnx_1, 10) |
| 413 | |
| 414 | class nn_LSTM_onnx_B1 : public nn_LSTM_onnx_B |
| 415 | { |
| 416 | public: |
| 417 | const char* match_pattern_graph() const |
| 418 | { |
| 419 | return R"PNNXIR(7767517 |
| 420 | 9 10 |
| 421 | pnnx.Input input_0 0 1 input |
| 422 | pnnx.Input input_1 0 1 initial_h |
| 423 | pnnx.Input input_2 0 1 initial_c |
| 424 | pnnx.Attribute W 0 1 W @data |
| 425 | pnnx.Attribute R 0 1 R @data |
| 426 | pnnx.Attribute B 0 1 B @data |
| 427 | LSTM lstm 6 3 input W R B initial_h initial_c out outh outc %*=%* |
| 428 | Squeeze sqz 1 1 out out1 axes=%axes |
| 429 | pnnx.Output output 3 0 out1 outh outc |
| 430 | )PNNXIR"; |
| 431 | } |
| 432 | }; |
| 433 | |
| 434 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_LSTM_onnx_B1, 10) |
| 435 | |
| 436 | class nn_LSTM_onnx_2 : public nn_LSTM_onnx |
| 437 | { |
| 438 | public: |
| 439 | const char* match_pattern_graph() const |
| 440 | { |
| 441 | return R"PNNXIR(7767517 |
| 442 | 8 7 |
| 443 | pnnx.Input input_0 0 1 input |
| 444 | pnnx.Input input_1 0 1 initial_h |
| 445 | pnnx.Input input_2 0 1 initial_c |
| 446 | pnnx.Attribute W 0 1 W @data |
| 447 | pnnx.Attribute R 0 1 R @data |
| 448 | LSTM lstm 5 1 input W R initial_h initial_c out %*=%* |
| 449 | Squeeze sqz 1 1 out out1 axes=%axes |
| 450 | pnnx.Output output 1 0 out1 |
| 451 | )PNNXIR"; |
| 452 | } |
| 453 | }; |
| 454 | |
| 455 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_LSTM_onnx_2, 10) |
| 456 | |
| 457 | class nn_LSTM_onnx_B2 : public nn_LSTM_onnx_B |
| 458 | { |
| 459 | public: |
| 460 | const char* match_pattern_graph() const |
| 461 | { |
| 462 | return R"PNNXIR(7767517 |
| 463 | 9 8 |
| 464 | pnnx.Input input_0 0 1 input |
| 465 | pnnx.Input input_1 0 1 initial_h |
| 466 | pnnx.Input input_2 0 1 initial_c |
| 467 | pnnx.Attribute W 0 1 W @data |
| 468 | pnnx.Attribute R 0 1 R @data |
| 469 | pnnx.Attribute B 0 1 B @data |
| 470 | LSTM lstm 6 1 input W R B initial_h initial_c out %*=%* |
| 471 | Squeeze sqz 1 1 out out1 axes=%axes |
| 472 | pnnx.Output output 1 0 out1 |
| 473 | )PNNXIR"; |
| 474 | } |
| 475 | }; |
| 476 | |
| 477 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_LSTM_onnx_B2, 10) |
| 478 | |
| 479 | class nn_LSTM_onnx_3 : public nn_LSTM_onnx |
| 480 | { |
| 481 | public: |
| 482 | const char* match_pattern_graph() const |
| 483 | { |
| 484 | return R"PNNXIR(7767517 |
| 485 | 7 6 |
| 486 | pnnx.Input input_0 0 1 input |
| 487 | pnnx.Attribute W 0 1 W @data |
| 488 | pnnx.Attribute R 0 1 R @data |
| 489 | LSTM lstm 3 1 input W R out %*=%* |
| 490 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 491 | Reshape reshape 1 1 out1 out2 %*=%* |
| 492 | pnnx.Output output 1 0 out2 |
| 493 | )PNNXIR"; |
| 494 | } |
| 495 | |
| 496 | bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 497 | { |
| 498 | if (!nn_LSTM_onnx::match(captured_params, captured_attrs)) |
| 499 | return false; |
| 500 | |
| 501 | if (captured_params.at("reshape.shape").ai != std::vector<int>{0, 0, -1}) |
| 502 | return false; |
| 503 | |
| 504 | return true; |
| 505 | } |
| 506 | }; |
| 507 | |
| 508 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_LSTM_onnx_3, 10) |
| 509 | |
| 510 | class nn_LSTM_onnx_B3 : public nn_LSTM_onnx_B |
| 511 | { |
| 512 | public: |
| 513 | const char* match_pattern_graph() const |
| 514 | { |
| 515 | return R"PNNXIR(7767517 |
| 516 | 8 7 |
| 517 | pnnx.Input input_0 0 1 input |
| 518 | pnnx.Attribute W 0 1 W @data |
| 519 | pnnx.Attribute R 0 1 R @data |
| 520 | pnnx.Attribute B 0 1 B @data |
| 521 | LSTM lstm 4 1 input W R B out %*=%* |
| 522 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 523 | Reshape reshape 1 1 out1 out2 %*=%* |
| 524 | pnnx.Output output 1 0 out2 |
| 525 | )PNNXIR"; |
| 526 | } |
| 527 | |
| 528 | bool match(const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 529 | { |
| 530 | if (!nn_LSTM_onnx_B::match(captured_params, captured_attrs)) |
| 531 | return false; |
| 532 | |
| 533 | if (captured_params.at("reshape.shape").ai != std::vector<int>{0, 0, -1}) |
| 534 | return false; |
| 535 | |
| 536 | return true; |
| 537 | } |
| 538 | }; |
| 539 | |
| 540 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_LSTM_onnx_B3, 10) |
| 541 | |
| 542 | class nn_LSTM_onnx_4 : public nn_LSTM_onnx_3 |
| 543 | { |
| 544 | public: |
| 545 | const char* match_pattern_graph() const |
| 546 | { |
| 547 | return R"PNNXIR(7767517 |
| 548 | 9 10 |
| 549 | pnnx.Input input_0 0 1 input |
| 550 | pnnx.Input input_1 0 1 initial_h |
| 551 | pnnx.Input input_2 0 1 initial_c |
| 552 | pnnx.Attribute W 0 1 W @data |
| 553 | pnnx.Attribute R 0 1 R @data |
| 554 | LSTM lstm 5 3 input W R initial_h initial_c out outh outc %*=%* |
| 555 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 556 | Reshape reshape 1 1 out1 out2 %*=%* |
| 557 | pnnx.Output output 3 0 out2 outh outc |
| 558 | )PNNXIR"; |
| 559 | } |
| 560 | }; |
| 561 | |
| 562 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_LSTM_onnx_4, 10) |
| 563 | |
| 564 | class nn_LSTM_onnx_B4 : public nn_LSTM_onnx_B3 |
| 565 | { |
| 566 | public: |
| 567 | const char* match_pattern_graph() const |
| 568 | { |
| 569 | return R"PNNXIR(7767517 |
| 570 | 10 11 |
| 571 | pnnx.Input input_0 0 1 input |
| 572 | pnnx.Input input_1 0 1 initial_h |
| 573 | pnnx.Input input_2 0 1 initial_c |
| 574 | pnnx.Attribute W 0 1 W @data |
| 575 | pnnx.Attribute R 0 1 R @data |
| 576 | pnnx.Attribute B 0 1 B @data |
| 577 | LSTM lstm 6 3 input W R B initial_h initial_c out outh outc %*=%* |
| 578 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 579 | Reshape reshape 1 1 out1 out2 %*=%* |
| 580 | pnnx.Output output 3 0 out2 outh outc |
| 581 | )PNNXIR"; |
| 582 | } |
| 583 | }; |
| 584 | |
| 585 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_LSTM_onnx_B4, 10) |
| 586 | |
| 587 | class nn_LSTM_onnx_5 : public nn_LSTM_onnx_3 |
| 588 | { |
| 589 | public: |
| 590 | const char* match_pattern_graph() const |
| 591 | { |
| 592 | return R"PNNXIR(7767517 |
| 593 | 9 8 |
| 594 | pnnx.Input input_0 0 1 input |
| 595 | pnnx.Input input_1 0 1 initial_h |
| 596 | pnnx.Input input_2 0 1 initial_c |
| 597 | pnnx.Attribute W 0 1 W @data |
| 598 | pnnx.Attribute R 0 1 R @data |
| 599 | LSTM lstm 5 1 input W R initial_h initial_c out %*=%* |
| 600 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 601 | Reshape reshape 1 1 out1 out2 %*=%* |
| 602 | pnnx.Output output 1 0 out2 |
| 603 | )PNNXIR"; |
| 604 | } |
| 605 | }; |
| 606 | |
| 607 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_LSTM_onnx_5, 10) |
| 608 | |
| 609 | class nn_LSTM_onnx_B5 : public nn_LSTM_onnx_B3 |
| 610 | { |
| 611 | public: |
| 612 | const char* match_pattern_graph() const |
| 613 | { |
| 614 | return R"PNNXIR(7767517 |
| 615 | 10 9 |
| 616 | pnnx.Input input_0 0 1 input |
| 617 | pnnx.Input input_1 0 1 initial_h |
| 618 | pnnx.Input input_2 0 1 initial_c |
| 619 | pnnx.Attribute W 0 1 W @data |
| 620 | pnnx.Attribute R 0 1 R @data |
| 621 | pnnx.Attribute B 0 1 B @data |
| 622 | LSTM lstm 6 1 input W R B initial_h initial_c out %*=%* |
| 623 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 624 | Reshape reshape 1 1 out1 out2 %*=%* |
| 625 | pnnx.Output output 1 0 out2 |
| 626 | )PNNXIR"; |
| 627 | } |
| 628 | }; |
| 629 | |
| 630 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_LSTM_onnx_B5, 10) |
| 631 | |
| 632 | } // namespace pnnx |