| 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 | #include <string.h> |
| 17 | |
| 18 | namespace pnnx { |
| 19 | |
| 20 | namespace ncnn { |
| 21 | |
| 22 | class nn_LSTM : public GraphRewriterPass |
| 23 | { |
| 24 | public: |
| 25 | const char* match_pattern_graph() const |
| 26 | { |
| 27 | return R"PNNXIR(7767517 |
| 28 | 3 4 |
| 29 | pnnx.Input input 0 1 input |
| 30 | nn.LSTM op_0 1 3 input out out_hidden out_cell input_size=%input_size hidden_size=%hidden_size num_layers=1 bias=%bias batch_first=%batch_first bidirectional=%bidirectional proj_size=%proj_size @weight_ih_l0 @weight_hh_l0 @bias_ih_l0 @bias_hh_l0 @weight_hr_l0 @weight_ih_l0_reverse @weight_hh_l0_reverse @bias_ih_l0_reverse @bias_hh_l0_reverse @weight_hr_l0_reverse |
| 31 | pnnx.Output output 3 0 out out_hidden out_cell |
| 32 | )PNNXIR"; |
| 33 | } |
| 34 | |
| 35 | const char* type_str() const |
| 36 | { |
| 37 | return "LSTM"; |
| 38 | } |
| 39 | |
| 40 | const char* name_str() const |
| 41 | { |
| 42 | return "lstm"; |
| 43 | } |
| 44 | |
| 45 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& captured_attrs) const |
| 46 | { |
| 47 | const bool bidirectional = captured_params.at("bidirectional").b; |
| 48 | const int num_directions = bidirectional ? 2 : 1; |
| 49 | const int hidden_size = captured_params.at("hidden_size").i; |
| 50 | const int input_size = captured_params.at("input_size").i; |
| 51 | |
| 52 | int proj_size = captured_params.at("proj_size").i; |
| 53 | if (proj_size == 0) |
| 54 | proj_size = hidden_size; |
| 55 | |
| 56 | int weight_data_size = num_directions * hidden_size * input_size * 4; |
| 57 | |
| 58 | op->params["0"] = proj_size; |
| 59 | op->params["1"] = weight_data_size; |
| 60 | op->params["2"] = bidirectional ? 2 : 0; |
| 61 | op->params["3"] = hidden_size; |
| 62 | |
| 63 | op->attrs["0"] = Attribute(); |
| 64 | op->attrs["0"].data = {0, 0, 0, 0}; |
| 65 | |
| 66 | // reorder IFGO-hidden-input_size to IFOG-hidden-input_size |
| 67 | { |
| 68 | std::vector<float> new_weight_ih; |
| 69 | { |
| 70 | const int weight_data_size_g = hidden_size * input_size; |
| 71 | |
| 72 | auto weight_ih = captured_attrs.at("op_0.weight_ih_l0").get_float32_data(); |
| 73 | const float* iptr = (const float*)weight_ih.data(); |
| 74 | const float* fptr = (const float*)weight_ih.data() + weight_data_size_g; |
| 75 | const float* gptr = (const float*)weight_ih.data() + weight_data_size_g * 2; |
| 76 | const float* optr = (const float*)weight_ih.data() + weight_data_size_g * 3; |
| 77 | |
| 78 | new_weight_ih.resize(4 * hidden_size * input_size); |
| 79 | float* weight = (float*)new_weight_ih.data(); |
| 80 | float* w_iptr = weight; |
| 81 | float* w_fptr = weight + weight_data_size_g; |
| 82 | float* w_optr = weight + weight_data_size_g * 2; |
| 83 | float* w_gptr = weight + weight_data_size_g * 3; |
| 84 | memcpy(w_iptr, iptr, weight_data_size_g * sizeof(float)); |
| 85 | memcpy(w_fptr, fptr, weight_data_size_g * sizeof(float)); |
| 86 | memcpy(w_optr, optr, weight_data_size_g * sizeof(float)); |
| 87 | memcpy(w_gptr, gptr, weight_data_size_g * sizeof(float)); |
| 88 | } |
| 89 | |
| 90 | if (bidirectional) |
| 91 | { |
| 92 | std::vector<float> new_weight_ih_reverse; |
| 93 | { |
| 94 | const int weight_data_size_g = hidden_size * input_size; |
| 95 | |
| 96 | auto weight_ih = captured_attrs.at("op_0.weight_ih_l0_reverse").get_float32_data(); |
| 97 | const float* iptr = (const float*)weight_ih.data(); |
| 98 | const float* fptr = (const float*)weight_ih.data() + weight_data_size_g; |
| 99 | const float* gptr = (const float*)weight_ih.data() + weight_data_size_g * 2; |
| 100 | const float* optr = (const float*)weight_ih.data() + weight_data_size_g * 3; |
| 101 | |
| 102 | new_weight_ih_reverse.resize(4 * hidden_size * input_size); |
| 103 | float* weight = (float*)new_weight_ih_reverse.data(); |
| 104 | float* w_iptr = weight; |
| 105 | float* w_fptr = weight + weight_data_size_g; |
| 106 | float* w_optr = weight + weight_data_size_g * 2; |
| 107 | float* w_gptr = weight + weight_data_size_g * 3; |
| 108 | memcpy(w_iptr, iptr, weight_data_size_g * sizeof(float)); |
| 109 | memcpy(w_fptr, fptr, weight_data_size_g * sizeof(float)); |
| 110 | memcpy(w_optr, optr, weight_data_size_g * sizeof(float)); |
| 111 | memcpy(w_gptr, gptr, weight_data_size_g * sizeof(float)); |
| 112 | } |
| 113 | op->attrs["1"] = Attribute({4, hidden_size, input_size}, new_weight_ih) + Attribute({4, hidden_size, input_size}, new_weight_ih_reverse); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | op->attrs["1"] = Attribute({4, hidden_size, input_size}, new_weight_ih); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | op->attrs["2"] = Attribute(); |
| 122 | op->attrs["2"].data = {0, 0, 0, 0}; |
| 123 | if (captured_params.at("bias").b) |
| 124 | { |
| 125 | // reduce bias_ih and bias_hh |
| 126 | // reorder IFGO-hidden to IFOG-hidden |
| 127 | std::vector<float> new_bias; |
| 128 | { |
| 129 | auto bias_ih = captured_attrs.at("op_0.bias_ih_l0").get_float32_data(); |
| 130 | auto bias_hh = captured_attrs.at("op_0.bias_hh_l0").get_float32_data(); |
| 131 | const float* bias_ih_iptr = (const float*)bias_ih.data(); |
| 132 | const float* bias_ih_fptr = (const float*)bias_ih.data() + hidden_size; |
| 133 | const float* bias_ih_gptr = (const float*)bias_ih.data() + hidden_size * 2; |
| 134 | const float* bias_ih_optr = (const float*)bias_ih.data() + hidden_size * 3; |
| 135 | const float* bias_hh_iptr = (const float*)bias_hh.data(); |
| 136 | const float* bias_hh_fptr = (const float*)bias_hh.data() + hidden_size; |
| 137 | const float* bias_hh_gptr = (const float*)bias_hh.data() + hidden_size * 2; |
| 138 | const float* bias_hh_optr = (const float*)bias_hh.data() + hidden_size * 3; |
| 139 | |
| 140 | new_bias.resize(4 * hidden_size); |
| 141 | float* bias = (float*)new_bias.data(); |
| 142 | float* b_iptr = bias; |
| 143 | float* b_fptr = bias + hidden_size; |
| 144 | float* b_optr = bias + hidden_size * 2; |
| 145 | float* b_gptr = bias + hidden_size * 3; |
| 146 | for (int i = 0; i < hidden_size; i++) |
| 147 | { |
| 148 | b_iptr[i] = bias_ih_iptr[i] + bias_hh_iptr[i]; |
| 149 | } |
| 150 | for (int i = 0; i < hidden_size; i++) |
| 151 | { |
| 152 | b_fptr[i] = bias_ih_fptr[i] + bias_hh_fptr[i]; |
| 153 | } |
| 154 | for (int i = 0; i < hidden_size; i++) |
| 155 | { |
| 156 | b_optr[i] = bias_ih_optr[i] + bias_hh_optr[i]; |
| 157 | } |
| 158 | for (int i = 0; i < hidden_size; i++) |
| 159 | { |
| 160 | b_gptr[i] = bias_ih_gptr[i] + bias_hh_gptr[i]; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | if (bidirectional) |
| 165 | { |
| 166 | std::vector<float> new_bias_reverse; |
| 167 | { |
| 168 | auto bias_ih = captured_attrs.at("op_0.bias_ih_l0_reverse").get_float32_data(); |
| 169 | auto bias_hh = captured_attrs.at("op_0.bias_hh_l0_reverse").get_float32_data(); |
| 170 | const float* bias_ih_iptr = (const float*)bias_ih.data(); |
| 171 | const float* bias_ih_fptr = (const float*)bias_ih.data() + hidden_size; |
| 172 | const float* bias_ih_gptr = (const float*)bias_ih.data() + hidden_size * 2; |
| 173 | const float* bias_ih_optr = (const float*)bias_ih.data() + hidden_size * 3; |
| 174 | const float* bias_hh_iptr = (const float*)bias_hh.data(); |
| 175 | const float* bias_hh_fptr = (const float*)bias_hh.data() + hidden_size; |
| 176 | const float* bias_hh_gptr = (const float*)bias_hh.data() + hidden_size * 2; |
| 177 | const float* bias_hh_optr = (const float*)bias_hh.data() + hidden_size * 3; |
| 178 | |
| 179 | new_bias_reverse.resize(4 * hidden_size); |
| 180 | float* bias = (float*)new_bias_reverse.data(); |
| 181 | float* b_iptr = bias; |
| 182 | float* b_fptr = bias + hidden_size; |
| 183 | float* b_optr = bias + hidden_size * 2; |
| 184 | float* b_gptr = bias + hidden_size * 3; |
| 185 | for (int i = 0; i < hidden_size; i++) |
| 186 | { |
| 187 | b_iptr[i] = bias_ih_iptr[i] + bias_hh_iptr[i]; |
| 188 | } |
| 189 | for (int i = 0; i < hidden_size; i++) |
| 190 | { |
| 191 | b_fptr[i] = bias_ih_fptr[i] + bias_hh_fptr[i]; |
| 192 | } |
| 193 | for (int i = 0; i < hidden_size; i++) |
| 194 | { |
| 195 | b_optr[i] = bias_ih_optr[i] + bias_hh_optr[i]; |
| 196 | } |
| 197 | for (int i = 0; i < hidden_size; i++) |
| 198 | { |
| 199 | b_gptr[i] = bias_ih_gptr[i] + bias_hh_gptr[i]; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | op->attrs["3"] = Attribute({4, hidden_size}, new_bias) + Attribute({4, hidden_size}, new_bias_reverse); |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | op->attrs["3"] = Attribute({4, hidden_size}, new_bias); |
| 208 | } |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | std::vector<float> bias(4 * hidden_size, 0.f); |
| 213 | |
| 214 | if (bidirectional) |
| 215 | op->attrs["3"] = Attribute({4, hidden_size}, bias) + Attribute({4, hidden_size}, bias); |
| 216 | else |
| 217 | op->attrs["3"] = Attribute({4, hidden_size}, bias); |
| 218 | } |
| 219 | |
| 220 | op->attrs["4"] = Attribute(); |
| 221 | op->attrs["4"].data = {0, 0, 0, 0}; |
| 222 | |
| 223 | // reorder IFGO-hidden-proj to IFOG-hidden-proj |
| 224 | { |
| 225 | std::vector<float> new_weight_hh; |
| 226 | { |
| 227 | const int weight_data_size_g = hidden_size * proj_size; |
| 228 | |
| 229 | auto weight_hh = captured_attrs.at("op_0.weight_hh_l0").get_float32_data(); |
| 230 | const float* iptr = (const float*)weight_hh.data(); |
| 231 | const float* fptr = (const float*)weight_hh.data() + weight_data_size_g; |
| 232 | const float* gptr = (const float*)weight_hh.data() + weight_data_size_g * 2; |
| 233 | const float* optr = (const float*)weight_hh.data() + weight_data_size_g * 3; |
| 234 | |
| 235 | new_weight_hh.resize(4 * hidden_size * proj_size); |
| 236 | float* weight = (float*)new_weight_hh.data(); |
| 237 | float* w_iptr = weight; |
| 238 | float* w_fptr = weight + weight_data_size_g; |
| 239 | float* w_optr = weight + weight_data_size_g * 2; |
| 240 | float* w_gptr = weight + weight_data_size_g * 3; |
| 241 | memcpy(w_iptr, iptr, weight_data_size_g * sizeof(float)); |
| 242 | memcpy(w_fptr, fptr, weight_data_size_g * sizeof(float)); |
| 243 | memcpy(w_optr, optr, weight_data_size_g * sizeof(float)); |
| 244 | memcpy(w_gptr, gptr, weight_data_size_g * sizeof(float)); |
| 245 | } |
| 246 | |
| 247 | if (bidirectional) |
| 248 | { |
| 249 | std::vector<float> new_weight_hh_reverse; |
| 250 | { |
| 251 | const int weight_data_size_g = hidden_size * proj_size; |
| 252 | |
| 253 | auto weight_hh = captured_attrs.at("op_0.weight_hh_l0_reverse").get_float32_data(); |
| 254 | const float* iptr = (const float*)weight_hh.data(); |
| 255 | const float* fptr = (const float*)weight_hh.data() + weight_data_size_g; |
| 256 | const float* gptr = (const float*)weight_hh.data() + weight_data_size_g * 2; |
| 257 | const float* optr = (const float*)weight_hh.data() + weight_data_size_g * 3; |
| 258 | |
| 259 | new_weight_hh_reverse.resize(4 * hidden_size * proj_size); |
| 260 | float* weight = (float*)new_weight_hh_reverse.data(); |
| 261 | float* w_iptr = weight; |
| 262 | float* w_fptr = weight + weight_data_size_g; |
| 263 | float* w_optr = weight + weight_data_size_g * 2; |
| 264 | float* w_gptr = weight + weight_data_size_g * 3; |
| 265 | memcpy(w_iptr, iptr, weight_data_size_g * sizeof(float)); |
| 266 | memcpy(w_fptr, fptr, weight_data_size_g * sizeof(float)); |
| 267 | memcpy(w_optr, optr, weight_data_size_g * sizeof(float)); |
| 268 | memcpy(w_gptr, gptr, weight_data_size_g * sizeof(float)); |
| 269 | } |
| 270 | op->attrs["5"] = Attribute({4, hidden_size, proj_size}, new_weight_hh) + Attribute({4, hidden_size, proj_size}, new_weight_hh_reverse); |
| 271 | } |
| 272 | else |
| 273 | { |
| 274 | op->attrs["5"] = Attribute({4, hidden_size, proj_size}, new_weight_hh); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (proj_size != hidden_size) |
| 279 | { |
| 280 | op->attrs["6"] = Attribute(); |
| 281 | op->attrs["6"].data = {0, 0, 0, 0}; |
| 282 | |
| 283 | if (bidirectional) |
| 284 | { |
| 285 | op->attrs["7"] = captured_attrs.at("op_0.weight_hr_l0") + captured_attrs.at("op_0.weight_hr_l0_reverse"); |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | op->attrs["7"] = captured_attrs.at("op_0.weight_hr_l0"); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | }; |
| 294 | |
| 295 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_LSTM, 20) |
| 296 | |
| 297 | class nn_LSTM_1 : public nn_LSTM |
| 298 | { |
| 299 | public: |
| 300 | const char* match_pattern_graph() const |
| 301 | { |
| 302 | return R"PNNXIR(7767517 |
| 303 | 5 6 |
| 304 | pnnx.Input input 0 1 input |
| 305 | pnnx.Input in_hidden 0 1 in_hidden |
| 306 | pnnx.Input in_hidden 0 1 in_cell |
| 307 | nn.LSTM op_0 3 3 input in_hidden in_cell out out_hidden out_cell input_size=%input_size hidden_size=%hidden_size num_layers=1 bias=%bias batch_first=%batch_first bidirectional=%bidirectional proj_size=%proj_size @weight_ih_l0 @weight_hh_l0 @bias_ih_l0 @bias_hh_l0 @weight_hr_l0 @weight_ih_l0_reverse @weight_hh_l0_reverse @bias_ih_l0_reverse @bias_hh_l0_reverse @weight_hr_l0_reverse |
| 308 | pnnx.Output output 3 0 out out_hidden out_cell |
| 309 | )PNNXIR"; |
| 310 | } |
| 311 | }; |
| 312 | |
| 313 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_LSTM_1, 20) |
| 314 | |
| 315 | class nn_LSTM_2 : public nn_LSTM |
| 316 | { |
| 317 | public: |
| 318 | const char* match_pattern_graph() const |
| 319 | { |
| 320 | return R"PNNXIR(7767517 |
| 321 | 3 2 |
| 322 | pnnx.Input input 0 1 input |
| 323 | nn.LSTM op_0 1 1 input out input_size=%input_size hidden_size=%hidden_size num_layers=1 bias=%bias batch_first=%batch_first bidirectional=%bidirectional proj_size=%proj_size @weight_ih_l0 @weight_hh_l0 @bias_ih_l0 @bias_hh_l0 @weight_hr_l0 @weight_ih_l0_reverse @weight_hh_l0_reverse @bias_ih_l0_reverse @bias_hh_l0_reverse @weight_hr_l0_reverse |
| 324 | pnnx.Output output 1 0 out |
| 325 | )PNNXIR"; |
| 326 | } |
| 327 | }; |
| 328 | |
| 329 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_LSTM_2, 20) |
| 330 | |
| 331 | class nn_LSTM_3 : public nn_LSTM |
| 332 | { |
| 333 | public: |
| 334 | const char* match_pattern_graph() const |
| 335 | { |
| 336 | return R"PNNXIR(7767517 |
| 337 | 5 4 |
| 338 | pnnx.Input input 0 1 input |
| 339 | pnnx.Input in_hidden 0 1 in_hidden |
| 340 | pnnx.Input in_hidden 0 1 in_cell |
| 341 | nn.LSTM op_0 3 1 input in_hidden in_cell out input_size=%input_size hidden_size=%hidden_size num_layers=1 bias=%bias batch_first=%batch_first bidirectional=%bidirectional proj_size=%proj_size @weight_ih_l0 @weight_hh_l0 @bias_ih_l0 @bias_hh_l0 @weight_hr_l0 @weight_ih_l0_reverse @weight_hh_l0_reverse @bias_ih_l0_reverse @bias_hh_l0_reverse @weight_hr_l0_reverse |
| 342 | pnnx.Output output 1 0 out |
| 343 | )PNNXIR"; |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(nn_LSTM_3, 20) |
| 348 | |
| 349 | } // namespace ncnn |
| 350 | |
| 351 | } // namespace pnnx |