| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 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_ncnn.h" |
| 16 | |
| 17 | namespace pnnx { |
| 18 | |
| 19 | namespace ncnn { |
| 20 | |
| 21 | class nn_RNN : public GraphRewriterPass |
| 22 | { |
| 23 | public: |
| 24 | const char* match_pattern_graph() const |
| 25 | { |
| 26 | return R"PNNXIR(7767517 |
| 27 | 3 3 |
| 28 | pnnx.Input input 0 1 input |
| 29 | nn.RNN op_0 1 2 input out out_hidden input_size=%input_size hidden_size=%hidden_size num_layers=1 nonlinearity=%nonlinearity bias=%bias batch_first=%batch_first bidirectional=%bidirectional @weight_ih_l0 @weight_hh_l0 @bias_ih_l0 @bias_hh_l0 @weight_ih_l0_reverse @weight_hh_l0_reverse @bias_ih_l0_reverse @bias_hh_l0_reverse |
| 30 | pnnx.Output output 2 0 out out_hidden |
| 31 | )PNNXIR"; |
| 32 | } |
| 33 | |
| 34 | const char* type_str() const |
| 35 | { |
| 36 | return "RNN"; |
| 37 | } |
| 38 | |
| 39 | const char* name_str() const |
| 40 | { |
| 41 | return "rnn"; |
| 42 | } |
| 43 | |
| 44 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 45 | { |
| 46 | const std::string nonlinearity = captured_params.at("nonlinearity").s; |
| 47 | |
| 48 | if (nonlinearity != "tanh") |
| 49 | { |
| 50 | fprintf(stderr, "RNN nonlinearity=%s not supported\n", nonlinearity.c_str()); |
| 51 | } |
| 52 | |
| 53 | const bool bidirectional = captured_params.at("bidirectional").b; |
| 54 | const int num_directions = bidirectional ? 2 : 1; |
| 55 | const int num_output = captured_params.at("hidden_size").i; |
| 56 | const int input_size = captured_params.at("input_size").i; |
| 57 | |
| 58 | int weight_data_size = num_directions * num_output * input_size; |
| 59 | |
| 60 | op->params["0"] = num_output; |
| 61 | op->params["1"] = weight_data_size; |
| 62 | op->params["2"] = bidirectional ? 2 : 0; |
| 63 | |
| 64 | op->attrs["0"] = Attribute(); |
| 65 | op->attrs["0"].data = {0, 0, 0, 0}; |
| 66 | if (bidirectional) |
| 67 | op->attrs["1"] = captured_attrs.at("op_0.weight_ih_l0") + captured_attrs.at("op_0.weight_ih_l0_reverse"); |
| 68 | else |
| 69 | op->attrs["1"] = captured_attrs.at("op_0.weight_ih_l0"); |
| 70 | |
| 71 | op->attrs["2"] = Attribute(); |
| 72 | op->attrs["2"].data = {0, 0, 0, 0}; |
| 73 | if (captured_params.at("bias").b) |
| 74 | { |
| 75 | // reduce bias_ih and bias_hh |
| 76 | std::vector<float> new_bias; |
| 77 | { |
| 78 | auto bias_ih = captured_attrs.at("op_0.bias_ih_l0").get_float32_data(); |
| 79 | auto bias_hh = captured_attrs.at("op_0.bias_hh_l0").get_float32_data(); |
| 80 | |
| 81 | new_bias.resize(num_output); |
| 82 | float* bias = (float*)new_bias.data(); |
| 83 | for (int i = 0; i < num_output; i++) |
| 84 | { |
| 85 | bias[i] = bias_ih[i] + bias_hh[i]; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (bidirectional) |
| 90 | { |
| 91 | std::vector<float> new_bias_reverse; |
| 92 | { |
| 93 | auto bias_ih = captured_attrs.at("op_0.bias_ih_l0_reverse").get_float32_data(); |
| 94 | auto bias_hh = captured_attrs.at("op_0.bias_hh_l0_reverse").get_float32_data(); |
| 95 | |
| 96 | new_bias_reverse.resize(num_output); |
| 97 | float* bias = (float*)new_bias_reverse.data(); |
| 98 | for (int i = 0; i < num_output; i++) |
| 99 | { |
| 100 | bias[i] = bias_ih[i] + bias_hh[i]; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | op->attrs["3"] = Attribute({num_output}, new_bias) + Attribute({num_output}, new_bias_reverse); |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | op->attrs["3"] = Attribute({num_output}, new_bias); |
| 109 | } |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | std::vector<float> bias(num_output, 0.f); |
| 114 | |
| 115 | if (bidirectional) |
| 116 | op->attrs["3"] = Attribute({num_output}, bias) + Attribute({num_output}, bias); |
| 117 | else |
| 118 | op->attrs["3"] = Attribute({num_output}, bias); |
| 119 | } |
| 120 | |
| 121 | op->attrs["4"] = Attribute(); |
| 122 | op->attrs["4"].data = {0, 0, 0, 0}; |
| 123 | if (bidirectional) |
| 124 | op->attrs["5"] = captured_attrs.at("op_0.weight_hh_l0") + captured_attrs.at("op_0.weight_hh_l0_reverse"); |
| 125 | else |
| 126 | op->attrs["5"] = captured_attrs.at("op_0.weight_hh_l0"); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_RNN, 20) |
| 131 | |
| 132 | class nn_RNN_1 : public nn_RNN |
| 133 | { |
| 134 | public: |
| 135 | const char* match_pattern_graph() const |
| 136 | { |
| 137 | return R"PNNXIR(7767517 |
| 138 | 4 4 |
| 139 | pnnx.Input input 0 1 input |
| 140 | pnnx.Input in_hidden 0 1 in_hidden |
| 141 | nn.RNN op_0 2 2 input in_hidden out out_hidden input_size=%input_size hidden_size=%hidden_size num_layers=1 nonlinearity=%nonlinearity bias=%bias batch_first=%batch_first bidirectional=%bidirectional @weight_ih_l0 @weight_hh_l0 @bias_ih_l0 @bias_hh_l0 @weight_ih_l0_reverse @weight_hh_l0_reverse @bias_ih_l0_reverse @bias_hh_l0_reverse |
| 142 | pnnx.Output output 2 0 out out_hidden |
| 143 | )PNNXIR"; |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_RNN_1, 20) |
| 148 | |
| 149 | class nn_RNN_2 : public nn_RNN |
| 150 | { |
| 151 | public: |
| 152 | const char* match_pattern_graph() const |
| 153 | { |
| 154 | return R"PNNXIR(7767517 |
| 155 | 3 2 |
| 156 | pnnx.Input input 0 1 input |
| 157 | nn.RNN op_0 1 1 input out input_size=%input_size hidden_size=%hidden_size num_layers=1 nonlinearity=%nonlinearity bias=%bias batch_first=%batch_first bidirectional=%bidirectional @weight_ih_l0 @weight_hh_l0 @bias_ih_l0 @bias_hh_l0 @weight_ih_l0_reverse @weight_hh_l0_reverse @bias_ih_l0_reverse @bias_hh_l0_reverse |
| 158 | pnnx.Output output 1 0 out |
| 159 | )PNNXIR"; |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_RNN_2, 20) |
| 164 | |
| 165 | class nn_RNN_3 : public nn_RNN |
| 166 | { |
| 167 | public: |
| 168 | const char* match_pattern_graph() const |
| 169 | { |
| 170 | return R"PNNXIR(7767517 |
| 171 | 4 3 |
| 172 | pnnx.Input input 0 1 input |
| 173 | pnnx.Input in_hidden 0 1 in_hidden |
| 174 | nn.RNN op_0 2 1 input in_hidden out input_size=%input_size hidden_size=%hidden_size num_layers=1 nonlinearity=%nonlinearity bias=%bias batch_first=%batch_first bidirectional=%bidirectional @weight_ih_l0 @weight_hh_l0 @bias_ih_l0 @bias_hh_l0 @weight_ih_l0_reverse @weight_hh_l0_reverse @bias_ih_l0_reverse @bias_hh_l0_reverse |
| 175 | pnnx.Output output 1 0 out |
| 176 | )PNNXIR"; |
| 177 | } |
| 178 | }; |
| 179 | |
| 180 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_RNN_3, 20) |
| 181 | |
| 182 | } // namespace ncnn |
| 183 | |
| 184 | } // namespace pnnx |