| 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 | |
| 17 | namespace pnnx { |
| 18 | |
| 19 | class nn_RNN_onnx : public GraphRewriterPass |
| 20 | { |
| 21 | public: |
| 22 | const char* match_pattern_graph() const |
| 23 | { |
| 24 | return R"PNNXIR(7767517 |
| 25 | 6 5 |
| 26 | pnnx.Input input_0 0 1 input |
| 27 | pnnx.Attribute W 0 1 W @data |
| 28 | pnnx.Attribute R 0 1 R @data |
| 29 | RNN rnn 3 1 input W R out %*=%* |
| 30 | Squeeze sqz 1 1 out out1 axes=%axes |
| 31 | pnnx.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 | |
| 156 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx, 10) |
| 157 | |
| 158 | class nn_RNN_onnx_B : public nn_RNN_onnx |
| 159 | { |
| 160 | public: |
| 161 | const char* match_pattern_graph() const |
| 162 | { |
| 163 | return R"PNNXIR(7767517 |
| 164 | 7 6 |
| 165 | pnnx.Input input_0 0 1 input |
| 166 | pnnx.Attribute W 0 1 W @data |
| 167 | pnnx.Attribute R 0 1 R @data |
| 168 | pnnx.Attribute B 0 1 B @data |
| 169 | RNN rnn 4 1 input W R B out %*=%* |
| 170 | Squeeze sqz 1 1 out out1 axes=%axes |
| 171 | pnnx.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 | |
| 246 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_B, 10) |
| 247 | |
| 248 | class nn_RNN_onnx_1 : public nn_RNN_onnx |
| 249 | { |
| 250 | public: |
| 251 | const char* match_pattern_graph() const |
| 252 | { |
| 253 | return R"PNNXIR(7767517 |
| 254 | 7 7 |
| 255 | pnnx.Input input_0 0 1 input |
| 256 | pnnx.Input input_1 0 1 initial_h |
| 257 | pnnx.Attribute W 0 1 W @data |
| 258 | pnnx.Attribute R 0 1 R @data |
| 259 | RNN rnn 4 2 input W R initial_h out outh %*=%* |
| 260 | Squeeze sqz 1 1 out out1 axes=%axes |
| 261 | pnnx.Output output 2 0 out1 outh |
| 262 | )PNNXIR"; |
| 263 | } |
| 264 | }; |
| 265 | |
| 266 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_1, 10) |
| 267 | |
| 268 | class nn_RNN_onnx_B1 : public nn_RNN_onnx_B |
| 269 | { |
| 270 | public: |
| 271 | const char* match_pattern_graph() const |
| 272 | { |
| 273 | return R"PNNXIR(7767517 |
| 274 | 8 8 |
| 275 | pnnx.Input input_0 0 1 input |
| 276 | pnnx.Input input_1 0 1 initial_h |
| 277 | pnnx.Attribute W 0 1 W @data |
| 278 | pnnx.Attribute R 0 1 R @data |
| 279 | pnnx.Attribute B 0 1 B @data |
| 280 | RNN rnn 5 2 input W R B initial_h out outh %*=%* |
| 281 | Squeeze sqz 1 1 out out1 axes=%axes |
| 282 | pnnx.Output output 2 0 out1 outh |
| 283 | )PNNXIR"; |
| 284 | } |
| 285 | }; |
| 286 | |
| 287 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_B1, 10) |
| 288 | |
| 289 | class nn_RNN_onnx_2 : public nn_RNN_onnx |
| 290 | { |
| 291 | public: |
| 292 | const char* match_pattern_graph() const |
| 293 | { |
| 294 | return R"PNNXIR(7767517 |
| 295 | 7 6 |
| 296 | pnnx.Input input_0 0 1 input |
| 297 | pnnx.Input input_1 0 1 initial_h |
| 298 | pnnx.Attribute W 0 1 W @data |
| 299 | pnnx.Attribute R 0 1 R @data |
| 300 | RNN rnn 4 1 input W R initial_h out %*=%* |
| 301 | Squeeze sqz 1 1 out out1 axes=%axes |
| 302 | pnnx.Output output 1 0 out1 |
| 303 | )PNNXIR"; |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_2, 10) |
| 308 | |
| 309 | class nn_RNN_onnx_B2 : public nn_RNN_onnx_B |
| 310 | { |
| 311 | public: |
| 312 | const char* match_pattern_graph() const |
| 313 | { |
| 314 | return R"PNNXIR(7767517 |
| 315 | 8 7 |
| 316 | pnnx.Input input_0 0 1 input |
| 317 | pnnx.Input input_1 0 1 initial_h |
| 318 | pnnx.Attribute W 0 1 W @data |
| 319 | pnnx.Attribute R 0 1 R @data |
| 320 | pnnx.Attribute B 0 1 B @data |
| 321 | RNN rnn 5 1 input W R B initial_h out %*=%* |
| 322 | Squeeze sqz 1 1 out out1 axes=%axes |
| 323 | pnnx.Output output 1 0 out1 |
| 324 | )PNNXIR"; |
| 325 | } |
| 326 | }; |
| 327 | |
| 328 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_B2, 10) |
| 329 | |
| 330 | class nn_RNN_onnx_3 : public nn_RNN_onnx |
| 331 | { |
| 332 | public: |
| 333 | const char* match_pattern_graph() const |
| 334 | { |
| 335 | return R"PNNXIR(7767517 |
| 336 | 7 6 |
| 337 | pnnx.Input input_0 0 1 input |
| 338 | pnnx.Attribute W 0 1 W @data |
| 339 | pnnx.Attribute R 0 1 R @data |
| 340 | RNN rnn 3 1 input W R out %*=%* |
| 341 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 342 | Reshape reshape 1 1 out1 out2 %*=%* |
| 343 | pnnx.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 | |
| 359 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_3, 10) |
| 360 | |
| 361 | class nn_RNN_onnx_B3 : public nn_RNN_onnx_B |
| 362 | { |
| 363 | public: |
| 364 | const char* match_pattern_graph() const |
| 365 | { |
| 366 | return R"PNNXIR(7767517 |
| 367 | 8 7 |
| 368 | pnnx.Input input_0 0 1 input |
| 369 | pnnx.Attribute W 0 1 W @data |
| 370 | pnnx.Attribute R 0 1 R @data |
| 371 | pnnx.Attribute B 0 1 B @data |
| 372 | RNN rnn 4 1 input W R B out %*=%* |
| 373 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 374 | Reshape reshape 1 1 out1 out2 %*=%* |
| 375 | pnnx.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 | |
| 391 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_B3, 10) |
| 392 | |
| 393 | class nn_RNN_onnx_4 : public nn_RNN_onnx_3 |
| 394 | { |
| 395 | public: |
| 396 | const char* match_pattern_graph() const |
| 397 | { |
| 398 | return R"PNNXIR(7767517 |
| 399 | 8 8 |
| 400 | pnnx.Input input_0 0 1 input |
| 401 | pnnx.Input input_1 0 1 initial_h |
| 402 | pnnx.Attribute W 0 1 W @data |
| 403 | pnnx.Attribute R 0 1 R @data |
| 404 | RNN rnn 4 2 input W R initial_h out outh %*=%* |
| 405 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 406 | Reshape reshape 1 1 out1 out2 %*=%* |
| 407 | pnnx.Output output 2 0 out2 outh |
| 408 | )PNNXIR"; |
| 409 | } |
| 410 | }; |
| 411 | |
| 412 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_4, 10) |
| 413 | |
| 414 | class nn_RNN_onnx_B4 : public nn_RNN_onnx_B3 |
| 415 | { |
| 416 | public: |
| 417 | const char* match_pattern_graph() const |
| 418 | { |
| 419 | return R"PNNXIR(7767517 |
| 420 | 9 9 |
| 421 | pnnx.Input input_0 0 1 input |
| 422 | pnnx.Input input_1 0 1 initial_h |
| 423 | pnnx.Attribute W 0 1 W @data |
| 424 | pnnx.Attribute R 0 1 R @data |
| 425 | pnnx.Attribute B 0 1 B @data |
| 426 | RNN rnn 5 2 input W R B initial_h out outh %*=%* |
| 427 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 428 | Reshape reshape 1 1 out1 out2 %*=%* |
| 429 | pnnx.Output output 2 0 out2 outh |
| 430 | )PNNXIR"; |
| 431 | } |
| 432 | }; |
| 433 | |
| 434 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_B4, 10) |
| 435 | |
| 436 | class nn_RNN_onnx_5 : public nn_RNN_onnx_3 |
| 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.Attribute W 0 1 W @data |
| 446 | pnnx.Attribute R 0 1 R @data |
| 447 | RNN rnn 4 1 input W R initial_h out %*=%* |
| 448 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 449 | Reshape reshape 1 1 out1 out2 %*=%* |
| 450 | pnnx.Output output 1 0 out2 |
| 451 | )PNNXIR"; |
| 452 | } |
| 453 | }; |
| 454 | |
| 455 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_5, 10) |
| 456 | |
| 457 | class nn_RNN_onnx_B5 : public nn_RNN_onnx_B3 |
| 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.Attribute W 0 1 W @data |
| 467 | pnnx.Attribute R 0 1 R @data |
| 468 | pnnx.Attribute B 0 1 B @data |
| 469 | RNN rnn 5 1 input W R B initial_h out %*=%* |
| 470 | Transpose transpose 1 1 out out1 perm=(0,2,1,3) |
| 471 | Reshape reshape 1 1 out1 out2 %*=%* |
| 472 | pnnx.Output output 1 0 out2 |
| 473 | )PNNXIR"; |
| 474 | } |
| 475 | }; |
| 476 | |
| 477 | REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(nn_RNN_onnx_B5, 10) |
| 478 | |
| 479 | } // namespace pnnx |