Changesets · Branches · Tags · Compare
| Changeset | 322eb7d75bd072fdc141cbe36804a8a2aecde51af11ae59ba5399886177055a2 |
|---|---|
| Author | nihui <nihuini@tencent.com> · 2024-07-02 23:49 +08:00 |
| Committer | GitHub <noreply@github.com> · 2024-07-02 23:49 +08:00 |
| Parents | 3a0df369be07 |
11 files changed
modified tools/pnnx/src/CMakeLists.txt +4 −0
| @@ -303,6 +303,10 @@ | ||
| 303 | 303 | pass_level2/torch_fft_fftn.cpp |
| 304 | 304 | |
| 305 | 305 | pass_level2/nn_quantized_FloatFunctional.cpp |
| 306 | + | |
| 307 | + pass_level2/nn_GRU.cpp | |
| 308 | + pass_level2/nn_LSTM.cpp | |
| 309 | + pass_level2/nn_RNN.cpp | |
| 306 | 310 | ) |
| 307 | 311 | |
| 308 | 312 | set(pnnx_pass_level3_SRCS |
modified tools/pnnx/src/load_onnx.cpp +8 −72
| @@ -545,85 +545,20 @@ | ||
| 545 | 545 | |
| 546 | 546 | onnx2pnnx::ModelStat oldstat = onnx2pnnx::get_model_stat(model); |
| 547 | 547 | |
| 548 | - fprintf(stderr, "%-34s", "inline_containers ... "); | |
| 548 | + double t0 = 0; | |
| 549 | + double t1 = 0; | |
| 549 | 550 | |
| 550 | - double t0 = get_current_time(); | |
| 551 | + int inlined = 0; | |
| 551 | 552 | |
| 552 | - onnx2pnnx::inline_containers(model); | |
| 553 | - | |
| 554 | - double t1 = get_current_time(); | |
| 555 | - | |
| 556 | - fprintf(stderr, "%8.2fms\n", t1 - t0); | |
| 557 | - | |
| 558 | - fprintf(stderr, "%-34s", "eliminate_noop ... "); | |
| 559 | - | |
| 560 | - t0 = get_current_time(); | |
| 561 | - | |
| 562 | - onnx2pnnx::eliminate_noop(model); | |
| 563 | - | |
| 564 | - t1 = get_current_time(); | |
| 565 | - | |
| 566 | - fprintf(stderr, "%8.2fms\n", t1 - t0); | |
| 567 | - | |
| 568 | - fprintf(stderr, "%-34s", "fold_constants ... "); | |
| 569 | - | |
| 570 | - t0 = get_current_time(); | |
| 571 | - | |
| 572 | - onnx2pnnx::fold_constants(model, input_shapes, input_types, input_shapes2, input_types2); | |
| 573 | - | |
| 574 | - t1 = get_current_time(); | |
| 575 | - | |
| 576 | - fprintf(stderr, "%8.2fms\n", t1 - t0); | |
| 577 | - | |
| 578 | - fprintf(stderr, "%-34s", "canonicalize ... "); | |
| 579 | - | |
| 580 | - t0 = get_current_time(); | |
| 581 | - | |
| 582 | - onnx2pnnx::canonicalize(model); | |
| 583 | - | |
| 584 | - t1 = get_current_time(); | |
| 585 | - | |
| 586 | - fprintf(stderr, "%8.2fms\n", t1 - t0); | |
| 587 | - | |
| 588 | - fprintf(stderr, "%-34s", "shape_inference ... "); | |
| 589 | - | |
| 590 | - t0 = get_current_time(); | |
| 591 | - | |
| 592 | - onnx2pnnx::shape_inference(model, input_shapes, input_types, input_shapes2, input_types2); | |
| 593 | - | |
| 594 | - t1 = get_current_time(); | |
| 595 | - | |
| 596 | - fprintf(stderr, "%8.2fms\n", t1 - t0); | |
| 597 | - | |
| 598 | - fprintf(stderr, "%-34s", "fold_constants_dynamic_shape ... "); | |
| 599 | - | |
| 600 | - t0 = get_current_time(); | |
| 601 | - | |
| 602 | - onnx2pnnx::fold_constants_dynamic_shape(model, input_shapes, input_types); | |
| 603 | - | |
| 604 | - t1 = get_current_time(); | |
| 605 | - | |
| 606 | - fprintf(stderr, "%8.2fms\n", t1 - t0); | |
| 607 | - | |
| 608 | - fprintf(stderr, "%-34s", "inline_if_graph ... "); | |
| 609 | - | |
| 610 | - t0 = get_current_time(); | |
| 611 | - | |
| 612 | - int inlined = onnx2pnnx::inline_if_graph(model); | |
| 613 | - | |
| 614 | - t1 = get_current_time(); | |
| 615 | - | |
| 616 | - fprintf(stderr, "%8.2fms\n", t1 - t0); | |
| 617 | - | |
| 618 | - while (inlined) | |
| 553 | + do | |
| 619 | 554 | { |
| 620 | 555 | fprintf(stderr, "%-34s", "inline_containers ... "); |
| 621 | 556 | |
| 622 | - double t0 = get_current_time(); | |
| 557 | + t0 = get_current_time(); | |
| 623 | 558 | |
| 624 | 559 | onnx2pnnx::inline_containers(model); |
| 625 | 560 | |
| 626 | - double t1 = get_current_time(); | |
| 561 | + t1 = get_current_time(); | |
| 627 | 562 | |
| 628 | 563 | fprintf(stderr, "%8.2fms\n", t1 - t0); |
| 629 | 564 | |
| @@ -686,7 +621,8 @@ | ||
| 686 | 621 | t1 = get_current_time(); |
| 687 | 622 | |
| 688 | 623 | fprintf(stderr, "%8.2fms\n", t1 - t0); |
| 689 | - } | |
| 624 | + | |
| 625 | + } while (inlined); | |
| 690 | 626 | |
| 691 | 627 | fprintf(stderr, "%-34s", "fuse_constant_as_attribute ... "); |
| 692 | 628 |
added tools/pnnx/src/pass_level2/nn_GRU.cpp +599 −0
| @@ -0,0 +1,599 @@ | ||
| 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 |
added tools/pnnx/src/pass_level2/nn_LSTM.cpp +632 −0
| @@ -0,0 +1,632 @@ | ||
| 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 |
added tools/pnnx/src/pass_level2/nn_RNN.cpp +479 −0
| @@ -0,0 +1,479 @@ | ||
| 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 |
modified tools/pnnx/src/pass_onnx/dead_code_elimination.cpp +31 −1
| @@ -145,7 +145,7 @@ | ||
| 145 | 145 | const int graph_value_info_size = graph->value_info_size(); |
| 146 | 146 | for (int k = j; k < graph_value_info_size - 1; k++) |
| 147 | 147 | { |
| 148 | - graph->mutable_node()->SwapElements(k, k + 1); | |
| 148 | + graph->mutable_value_info()->SwapElements(k, k + 1); | |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | // ..... ....... j |
| @@ -155,6 +155,36 @@ | ||
| 155 | 155 | } |
| 156 | 156 | } |
| 157 | 157 | } |
| 158 | + | |
| 159 | + for (int i = 0; i < graph->node_size(); i++) | |
| 160 | + { | |
| 161 | + onnx::NodeProto* node = graph->mutable_node(i); | |
| 162 | + | |
| 163 | + for (int j = 0; j < node->output_size(); j++) | |
| 164 | + { | |
| 165 | + bool has_dead_output = false; | |
| 166 | + for (auto x : dead_outputs) | |
| 167 | + { | |
| 168 | + if (x == node->output(j)) | |
| 169 | + { | |
| 170 | + has_dead_output = true; | |
| 171 | + break; | |
| 172 | + } | |
| 173 | + } | |
| 174 | + | |
| 175 | + if (has_dead_output) | |
| 176 | + { | |
| 177 | + // drop optional unused outputs | |
| 178 | + const int node_output_size = node->output_size(); | |
| 179 | + for (int k = j; k < node_output_size; k++) | |
| 180 | + { | |
| 181 | + node->mutable_output()->RemoveLast(); | |
| 182 | + } | |
| 183 | + | |
| 184 | + break; | |
| 185 | + } | |
| 186 | + } | |
| 187 | + } | |
| 158 | 188 | } |
| 159 | 189 | |
| 160 | 190 | // collect all dead functions |
modified tools/pnnx/src/pass_onnx/eliminate_noop.cpp +12 −6
| @@ -113,16 +113,16 @@ | ||
| 113 | 113 | const onnx::NodeProto& node = graph->node(i); |
| 114 | 114 | const std::string& op_type = node.op_type(); |
| 115 | 115 | |
| 116 | - onnx::ValueInfoProto* input_value = find_value_info_by_name(graph, node.input(0)); | |
| 117 | - onnx::ValueInfoProto* output_value = find_value_info_by_name(graph, node.output(0)); | |
| 118 | - | |
| 119 | - if (!input_value || !output_value) | |
| 120 | - continue; | |
| 121 | - | |
| 122 | 116 | bool noop = false; |
| 123 | 117 | |
| 124 | 118 | if (op_type == "Cast") |
| 125 | 119 | { |
| 120 | + onnx::ValueInfoProto* input_value = find_value_info_by_name(graph, node.input(0)); | |
| 121 | + onnx::ValueInfoProto* output_value = find_value_info_by_name(graph, node.output(0)); | |
| 122 | + | |
| 123 | + if (!input_value || !output_value) | |
| 124 | + continue; | |
| 125 | + | |
| 126 | 126 | if (input_value->type().has_tensor_type() && output_value->type().has_tensor_type()) |
| 127 | 127 | { |
| 128 | 128 | if (input_value->type().tensor_type().elem_type() == output_value->type().tensor_type().elem_type()) |
| @@ -132,6 +132,12 @@ | ||
| 132 | 132 | |
| 133 | 133 | if (op_type == "Reshape") |
| 134 | 134 | { |
| 135 | + onnx::ValueInfoProto* input_value = find_value_info_by_name(graph, node.input(0)); | |
| 136 | + onnx::ValueInfoProto* output_value = find_value_info_by_name(graph, node.output(0)); | |
| 137 | + | |
| 138 | + if (!input_value || !output_value) | |
| 139 | + continue; | |
| 140 | + | |
| 135 | 141 | if (input_value->type().has_tensor_type() && output_value->type().has_tensor_type()) |
| 136 | 142 | { |
| 137 | 143 | const onnx::TensorShapeProto& input_tsp = input_value->type().tensor_type().shape(); |
modified tools/pnnx/tests/onnx/CMakeLists.txt +3 −0
| @@ -48,12 +48,14 @@ | ||
| 48 | 48 | pnnx_onnx_add_test(nn_ConvTranspose2d) |
| 49 | 49 | pnnx_onnx_add_test(nn_ConvTranspose3d) |
| 50 | 50 | pnnx_onnx_add_test(nn_GroupNorm) |
| 51 | +pnnx_onnx_add_test(nn_GRU) | |
| 51 | 52 | pnnx_onnx_add_test(nn_InstanceNorm1d) |
| 52 | 53 | pnnx_onnx_add_test(nn_InstanceNorm2d) |
| 53 | 54 | pnnx_onnx_add_test(nn_InstanceNorm3d) |
| 54 | 55 | pnnx_onnx_add_test(nn_LayerNorm) |
| 55 | 56 | pnnx_onnx_add_test(nn_Linear) |
| 56 | 57 | pnnx_onnx_add_test(nn_LocalResponseNorm) |
| 58 | +pnnx_onnx_add_test(nn_LSTM) | |
| 57 | 59 | pnnx_onnx_add_test(nn_MaxPool1d) |
| 58 | 60 | pnnx_onnx_add_test(nn_MaxPool2d) |
| 59 | 61 | pnnx_onnx_add_test(nn_MaxPool3d) |
| @@ -63,6 +65,7 @@ | ||
| 63 | 65 | pnnx_onnx_add_test(nn_ReplicationPad1d) |
| 64 | 66 | pnnx_onnx_add_test(nn_ReplicationPad2d) |
| 65 | 67 | pnnx_onnx_add_test(nn_ReplicationPad3d) |
| 68 | +pnnx_onnx_add_test(nn_RNN) | |
| 66 | 69 | pnnx_onnx_add_test(nn_Sigmoid) |
| 67 | 70 | pnnx_onnx_add_test(nn_Softmax) |
| 68 | 71 | pnnx_onnx_add_test(nn_Upsample) |
added tools/pnnx/tests/onnx/test_nn_GRU.py +75 −0
| @@ -0,0 +1,75 @@ | ||
| 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 | +import torch | |
| 16 | +import torch.nn as nn | |
| 17 | +import torch.nn.functional as F | |
| 18 | + | |
| 19 | +class Model(nn.Module): | |
| 20 | + def __init__(self): | |
| 21 | + super(Model, self).__init__() | |
| 22 | + | |
| 23 | + self.gru_0_0 = nn.GRU(input_size=32, hidden_size=16) | |
| 24 | + self.gru_0_1 = nn.GRU(input_size=16, hidden_size=16, num_layers=3, bias=False) | |
| 25 | + self.gru_0_2 = nn.GRU(input_size=16, hidden_size=16, num_layers=4, bias=True, bidirectional=True) | |
| 26 | + self.gru_0_3 = nn.GRU(input_size=16, hidden_size=16, num_layers=4, bias=True, bidirectional=True) | |
| 27 | + | |
| 28 | + self.gru_1_0 = nn.GRU(input_size=25, hidden_size=16, batch_first=True) | |
| 29 | + self.gru_1_1 = nn.GRU(input_size=16, hidden_size=16, num_layers=3, bias=False, batch_first=True) | |
| 30 | + self.gru_1_2 = nn.GRU(input_size=16, hidden_size=16, num_layers=4, bias=True, batch_first=True, bidirectional=True) | |
| 31 | + self.gru_1_3 = nn.GRU(input_size=16, hidden_size=16, num_layers=4, bias=True, batch_first=True, bidirectional=True) | |
| 32 | + | |
| 33 | + def forward(self, x, y): | |
| 34 | + x0, h0 = self.gru_0_0(x) | |
| 35 | + x1, _ = self.gru_0_1(x0) | |
| 36 | + x2, h2 = self.gru_0_2(x1) | |
| 37 | + x3, h3 = self.gru_0_3(x1, h2) | |
| 38 | + | |
| 39 | + y0, h4 = self.gru_1_0(y) | |
| 40 | + y1, _ = self.gru_1_1(y0) | |
| 41 | + y2, h6 = self.gru_1_2(y1) | |
| 42 | + y3, h7 = self.gru_1_3(y1, h6) | |
| 43 | + return x2, x3, h0, h2, h3, y2, y3, h4, h6, h7 | |
| 44 | + | |
| 45 | +def test(): | |
| 46 | + net = Model() | |
| 47 | + net.eval() | |
| 48 | + | |
| 49 | + torch.manual_seed(0) | |
| 50 | + x = torch.rand(10, 1, 32) | |
| 51 | + y = torch.rand(1, 12, 25) | |
| 52 | + | |
| 53 | + a = net(x, y) | |
| 54 | + | |
| 55 | + # export onnx | |
| 56 | + torch.onnx.export(net, (x, y), "test_nn_GRU.onnx") | |
| 57 | + | |
| 58 | + # onnx to pnnx | |
| 59 | + import os | |
| 60 | + os.system("../../src/pnnx test_nn_GRU.onnx inputshape=[10,1,32],[1,12,25]") | |
| 61 | + | |
| 62 | + # pnnx inference | |
| 63 | + import test_nn_GRU_pnnx | |
| 64 | + b = test_nn_GRU_pnnx.test_inference() | |
| 65 | + | |
| 66 | + for a0, b0 in zip(a, b): | |
| 67 | + if not torch.equal(a0, b0): | |
| 68 | + return False | |
| 69 | + return True | |
| 70 | + | |
| 71 | +if __name__ == "__main__": | |
| 72 | + if test(): | |
| 73 | + exit(0) | |
| 74 | + else: | |
| 75 | + exit(1) |
added tools/pnnx/tests/onnx/test_nn_LSTM.py +75 −0
| @@ -0,0 +1,75 @@ | ||
| 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 | +import torch | |
| 16 | +import torch.nn as nn | |
| 17 | +import torch.nn.functional as F | |
| 18 | + | |
| 19 | +class Model(nn.Module): | |
| 20 | + def __init__(self): | |
| 21 | + super(Model, self).__init__() | |
| 22 | + | |
| 23 | + self.lstm_0_0 = nn.LSTM(input_size=32, hidden_size=16) | |
| 24 | + self.lstm_0_1 = nn.LSTM(input_size=16, hidden_size=16, num_layers=3, bias=False) | |
| 25 | + self.lstm_0_2 = nn.LSTM(input_size=16, hidden_size=16, num_layers=4, bias=True, bidirectional=True) | |
| 26 | + self.lstm_0_3 = nn.LSTM(input_size=32, hidden_size=16, num_layers=4, bias=True, bidirectional=True) | |
| 27 | + | |
| 28 | + self.lstm_1_0 = nn.LSTM(input_size=25, hidden_size=16, batch_first=True) | |
| 29 | + self.lstm_1_1 = nn.LSTM(input_size=16, hidden_size=16, num_layers=3, bias=False, batch_first=True) | |
| 30 | + self.lstm_1_2 = nn.LSTM(input_size=16, hidden_size=16, num_layers=4, bias=True, batch_first=True, bidirectional=True) | |
| 31 | + self.lstm_1_3 = nn.LSTM(input_size=32, hidden_size=16, num_layers=4, bias=True, batch_first=True, bidirectional=True) | |
| 32 | + | |
| 33 | + def forward(self, x, y): | |
| 34 | + x0, (h0, c0) = self.lstm_0_0(x) | |
| 35 | + x1, _ = self.lstm_0_1(x0) | |
| 36 | + x2, (h2, c2) = self.lstm_0_2(x1) | |
| 37 | + x3, (h3, c3) = self.lstm_0_3(x2, (h2, c2)) | |
| 38 | + | |
| 39 | + y0, (h4, c4) = self.lstm_1_0(y) | |
| 40 | + y1, _ = self.lstm_1_1(y0) | |
| 41 | + y2, (h6, c6) = self.lstm_1_2(y1) | |
| 42 | + y3, (h7, c7) = self.lstm_1_3(y2, (h6, c6)) | |
| 43 | + return x2, x3, h0, h2, h3, c0, c2, c3, y2, y3, h4, h6, h7, c4, c6, c7 | |
| 44 | + | |
| 45 | +def test(): | |
| 46 | + net = Model() | |
| 47 | + net.eval() | |
| 48 | + | |
| 49 | + torch.manual_seed(0) | |
| 50 | + x = torch.rand(10, 1, 32) | |
| 51 | + y = torch.rand(1, 12, 25) | |
| 52 | + | |
| 53 | + a = net(x, y) | |
| 54 | + | |
| 55 | + # export onnx | |
| 56 | + torch.onnx.export(net, (x, y), "test_nn_LSTM.onnx") | |
| 57 | + | |
| 58 | + # onnx to pnnx | |
| 59 | + import os | |
| 60 | + os.system("../../src/pnnx test_nn_LSTM.onnx inputshape=[10,1,32],[1,12,25]") | |
| 61 | + | |
| 62 | + # pnnx inference | |
| 63 | + import test_nn_LSTM_pnnx | |
| 64 | + b = test_nn_LSTM_pnnx.test_inference() | |
| 65 | + | |
| 66 | + for a0, b0 in zip(a, b): | |
| 67 | + if not torch.equal(a0, b0): | |
| 68 | + return False | |
| 69 | + return True | |
| 70 | + | |
| 71 | +if __name__ == "__main__": | |
| 72 | + if test(): | |
| 73 | + exit(0) | |
| 74 | + else: | |
| 75 | + exit(1) |
added tools/pnnx/tests/onnx/test_nn_RNN.py +75 −0
| @@ -0,0 +1,75 @@ | ||
| 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 | +import torch | |
| 16 | +import torch.nn as nn | |
| 17 | +import torch.nn.functional as F | |
| 18 | + | |
| 19 | +class Model(nn.Module): | |
| 20 | + def __init__(self): | |
| 21 | + super(Model, self).__init__() | |
| 22 | + | |
| 23 | + self.rnn_0_0 = nn.RNN(input_size=32, hidden_size=16) | |
| 24 | + self.rnn_0_1 = nn.RNN(input_size=16, hidden_size=16, num_layers=3, nonlinearity='tanh', bias=False) | |
| 25 | + self.rnn_0_2 = nn.RNN(input_size=16, hidden_size=16, num_layers=4, nonlinearity='relu', bias=True, bidirectional=True) | |
| 26 | + self.rnn_0_3 = nn.RNN(input_size=16, hidden_size=16, num_layers=4, nonlinearity='tanh', bias=True, bidirectional=True) | |
| 27 | + | |
| 28 | + self.rnn_1_0 = nn.RNN(input_size=25, hidden_size=16, batch_first=True) | |
| 29 | + self.rnn_1_1 = nn.RNN(input_size=16, hidden_size=16, num_layers=3, nonlinearity='tanh', bias=False, batch_first=True) | |
| 30 | + self.rnn_1_2 = nn.RNN(input_size=16, hidden_size=16, num_layers=4, nonlinearity='relu', bias=True, batch_first=True, bidirectional=True) | |
| 31 | + self.rnn_1_3 = nn.RNN(input_size=16, hidden_size=16, num_layers=4, nonlinearity='tanh', bias=True, batch_first=True, bidirectional=True) | |
| 32 | + | |
| 33 | + def forward(self, x, y): | |
| 34 | + x0, h0 = self.rnn_0_0(x) | |
| 35 | + x1, _ = self.rnn_0_1(x0) | |
| 36 | + x2, h2 = self.rnn_0_2(x1) | |
| 37 | + x3, h3 = self.rnn_0_3(x1, h2) | |
| 38 | + | |
| 39 | + y0, h4 = self.rnn_1_0(y) | |
| 40 | + y1, _ = self.rnn_1_1(y0) | |
| 41 | + y2, h6 = self.rnn_1_2(y1) | |
| 42 | + y3, h7 = self.rnn_1_3(y1, h6) | |
| 43 | + return x2, x3, h0, h2, h3, y2, y3, h4, h6, h7 | |
| 44 | + | |
| 45 | +def test(): | |
| 46 | + net = Model() | |
| 47 | + net.eval() | |
| 48 | + | |
| 49 | + torch.manual_seed(0) | |
| 50 | + x = torch.rand(10, 1, 32) | |
| 51 | + y = torch.rand(1, 12, 25) | |
| 52 | + | |
| 53 | + a = net(x, y) | |
| 54 | + | |
| 55 | + # export onnx | |
| 56 | + torch.onnx.export(net, (x, y), "test_nn_RNN.onnx") | |
| 57 | + | |
| 58 | + # onnx to pnnx | |
| 59 | + import os | |
| 60 | + os.system("../../src/pnnx test_nn_RNN.onnx inputshape=[10,1,32],[1,12,25]") | |
| 61 | + | |
| 62 | + # pnnx inference | |
| 63 | + import test_nn_RNN_pnnx | |
| 64 | + b = test_nn_RNN_pnnx.test_inference() | |
| 65 | + | |
| 66 | + for a0, b0 in zip(a, b): | |
| 67 | + if not torch.equal(a0, b0): | |
| 68 | + return False | |
| 69 | + return True | |
| 70 | + | |
| 71 | +if __name__ == "__main__": | |
| 72 | + if test(): | |
| 73 | + exit(0) | |
| 74 | + else: | |
| 75 | + exit(1) |