| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2017 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 "lstm.h" |
| 16 | |
| 17 | namespace ncnn { |
| 18 | |
| 19 | LSTM::LSTM() |
| 20 | { |
| 21 | one_blob_only = false; |
| 22 | support_inplace = false; |
| 23 | } |
| 24 | |
| 25 | int LSTM::load_param(const ParamDict& pd) |
| 26 | { |
| 27 | num_output = pd.get(0, 0); |
| 28 | weight_data_size = pd.get(1, 0); |
| 29 | direction = pd.get(2, 0); |
| 30 | hidden_size = pd.get(3, num_output); |
| 31 | int8_scale_term = pd.get(8, 0); |
| 32 | |
| 33 | if (int8_scale_term) |
| 34 | { |
| 35 | #if !NCNN_INT8 |
| 36 | NCNN_LOGE("please build ncnn with NCNN_INT8 enabled for int8 inference"); |
| 37 | return -1; |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | int LSTM::load_model(const ModelBin& mb) |
| 45 | { |
| 46 | int num_directions = direction == 2 ? 2 : 1; |
| 47 | |
| 48 | int size = weight_data_size / num_directions / hidden_size / 4; |
| 49 | |
| 50 | // raw weight data |
| 51 | weight_xc_data = mb.load(size, hidden_size * 4, num_directions, 0); |
| 52 | if (weight_xc_data.empty()) |
| 53 | return -100; |
| 54 | |
| 55 | bias_c_data = mb.load(hidden_size, 4, num_directions, 0); |
| 56 | if (bias_c_data.empty()) |
| 57 | return -100; |
| 58 | |
| 59 | weight_hc_data = mb.load(num_output, hidden_size * 4, num_directions, 0); |
| 60 | if (weight_hc_data.empty()) |
| 61 | return -100; |
| 62 | |
| 63 | if (num_output != hidden_size) |
| 64 | { |
| 65 | weight_hr_data = mb.load(hidden_size, num_output, num_directions, 0); |
| 66 | if (weight_hr_data.empty()) |
| 67 | return -100; |
| 68 | } |
| 69 | |
| 70 | #if NCNN_INT8 |
| 71 | if (int8_scale_term) |
| 72 | { |
| 73 | weight_xc_data_int8_scales = mb.load(hidden_size * 4, num_directions, 1); |
| 74 | weight_hc_data_int8_scales = mb.load(hidden_size * 4, num_directions, 1); |
| 75 | } |
| 76 | #endif // NCNN_INT8 |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int lstm(const Mat& bottom_blob, Mat& top_blob, int reverse, const Mat& weight_xc, const Mat& bias_c, const Mat& weight_hc, const Mat& weight_hr, Mat& hidden_state, Mat& cell_state, const Option& opt) |
| 82 | { |
| 83 | int size = bottom_blob.w; |
| 84 | int T = bottom_blob.h; |
| 85 | |
| 86 | int num_output = top_blob.w; |
| 87 | int hidden_size = cell_state.w; |
| 88 | |
| 89 | // 4 x hidden_size |
| 90 | Mat gates(4, hidden_size, 4u, opt.workspace_allocator); |
| 91 | if (gates.empty()) |
| 92 | return -100; |
| 93 | |
| 94 | Mat tmp_hidden_state; |
| 95 | if (num_output != hidden_size) |
| 96 | { |
| 97 | tmp_hidden_state.create(hidden_size, 4u, opt.workspace_allocator); |
| 98 | if (tmp_hidden_state.empty()) |
| 99 | return -100; |
| 100 | } |
| 101 | |
| 102 | // unroll |
| 103 | for (int t = 0; t < T; t++) |
| 104 | { |
| 105 | // clip hidden by continuation indicator |
| 106 | // h_cont_{t-1} = cont_t * h_{t-1} |
| 107 | // h_cont_{t-1} = h_{t-1} if cont_t == 1 |
| 108 | // 0 otherwise |
| 109 | // calculate hidden |
| 110 | // gate_input_t := W_hc * h_conted_{t-1} + W_xc * x_t + b_c |
| 111 | |
| 112 | int ti = reverse ? T - 1 - t : t; |
| 113 | |
| 114 | const float* x = bottom_blob.row(ti); |
| 115 | #pragma omp parallel for num_threads(opt.num_threads) |
| 116 | for (int q = 0; q < hidden_size; q++) |
| 117 | { |
| 118 | const float* bias_c_I = bias_c.row(0); |
| 119 | const float* bias_c_F = bias_c.row(1); |
| 120 | const float* bias_c_O = bias_c.row(2); |
| 121 | const float* bias_c_G = bias_c.row(3); |
| 122 | |
| 123 | float* gates_data = gates.row(q); |
| 124 | |
| 125 | // gate I F O G |
| 126 | const float* weight_xc_I = weight_xc.row(hidden_size * 0 + q); |
| 127 | const float* weight_xc_F = weight_xc.row(hidden_size * 1 + q); |
| 128 | const float* weight_xc_O = weight_xc.row(hidden_size * 2 + q); |
| 129 | const float* weight_xc_G = weight_xc.row(hidden_size * 3 + q); |
| 130 | |
| 131 | const float* weight_hc_I = weight_hc.row(hidden_size * 0 + q); |
| 132 | const float* weight_hc_F = weight_hc.row(hidden_size * 1 + q); |
| 133 | const float* weight_hc_O = weight_hc.row(hidden_size * 2 + q); |
| 134 | const float* weight_hc_G = weight_hc.row(hidden_size * 3 + q); |
| 135 | |
| 136 | float I = bias_c_I[q]; |
| 137 | float F = bias_c_F[q]; |
| 138 | float O = bias_c_O[q]; |
| 139 | float G = bias_c_G[q]; |
| 140 | |
| 141 | for (int i = 0; i < size; i++) |
| 142 | { |
| 143 | float xi = x[i]; |
| 144 | |
| 145 | I += weight_xc_I[i] * xi; |
| 146 | F += weight_xc_F[i] * xi; |
| 147 | O += weight_xc_O[i] * xi; |
| 148 | G += weight_xc_G[i] * xi; |
| 149 | } |
| 150 | |
| 151 | for (int i = 0; i < num_output; i++) |
| 152 | { |
| 153 | float h_cont = hidden_state[i]; |
| 154 | |
| 155 | I += weight_hc_I[i] * h_cont; |
| 156 | F += weight_hc_F[i] * h_cont; |
| 157 | O += weight_hc_O[i] * h_cont; |
| 158 | G += weight_hc_G[i] * h_cont; |
| 159 | } |
| 160 | |
| 161 | gates_data[0] = I; |
| 162 | gates_data[1] = F; |
| 163 | gates_data[2] = O; |
| 164 | gates_data[3] = G; |
| 165 | } |
| 166 | |
| 167 | // lstm unit |
| 168 | // sigmoid(I) |
| 169 | // sigmoid(F) |
| 170 | // sigmoid(O) |
| 171 | // tanh(G) |
| 172 | // c_t := f_t .* c_{t-1} + i_t .* g_t |
| 173 | // h_t := o_t .* tanh[c_t] |
| 174 | float* output_data = top_blob.row(ti); |
| 175 | #pragma omp parallel for num_threads(opt.num_threads) |
| 176 | for (int q = 0; q < hidden_size; q++) |
| 177 | { |
| 178 | const float* gates_data = gates.row(q); |
| 179 | |
| 180 | float I = gates_data[0]; |
| 181 | float F = gates_data[1]; |
| 182 | float O = gates_data[2]; |
| 183 | float G = gates_data[3]; |
| 184 | |
| 185 | I = 1.f / (1.f + expf(-I)); |
| 186 | F = 1.f / (1.f + expf(-F)); |
| 187 | O = 1.f / (1.f + expf(-O)); |
| 188 | G = tanhf(G); |
| 189 | |
| 190 | float cell2 = F * cell_state[q] + I * G; |
| 191 | float H = O * tanhf(cell2); |
| 192 | cell_state[q] = cell2; |
| 193 | |
| 194 | if (num_output == hidden_size) |
| 195 | { |
| 196 | hidden_state[q] = H; |
| 197 | output_data[q] = H; |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | tmp_hidden_state[q] = H; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if (num_output != hidden_size) |
| 206 | { |
| 207 | #pragma omp parallel for num_threads(opt.num_threads) |
| 208 | for (int q = 0; q < num_output; q++) |
| 209 | { |
| 210 | const float* hr = weight_hr.row(q); |
| 211 | |
| 212 | float H = 0; |
| 213 | for (int i = 0; i < hidden_size; i++) |
| 214 | { |
| 215 | H += tmp_hidden_state[i] * hr[i]; |
| 216 | } |
| 217 | |
| 218 | hidden_state[q] = H; |
| 219 | output_data[q] = H; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | #if NCNN_INT8 |
| 228 | static int lstm_int8(const Mat& bottom_blob, Mat& top_blob, int reverse, const Mat& weight_xc_int8, const float* weight_xc_int8_scales, const Mat& bias_c, const Mat& weight_hc_int8, const float* weight_hc_int8_scales, const Mat& weight_hr, Mat& hidden_state, Mat& cell_state, const Option& opt) |
| 229 | { |
| 230 | int size = bottom_blob.w; |
| 231 | int T = bottom_blob.h; |
| 232 | |
| 233 | int num_output = top_blob.w; |
| 234 | int hidden_size = cell_state.w; |
| 235 | |
| 236 | // 4 x hidden_size |
| 237 | Mat gates(4, hidden_size, 4u, opt.workspace_allocator); |
| 238 | if (gates.empty()) |
| 239 | return -100; |
| 240 | |
| 241 | Mat tmp_hidden_state; |
| 242 | if (num_output != hidden_size) |
| 243 | { |
| 244 | tmp_hidden_state.create(hidden_size, 4u, opt.workspace_allocator); |
| 245 | if (tmp_hidden_state.empty()) |
| 246 | return -100; |
| 247 | } |
| 248 | |
| 249 | // dynamic quantize bottom_blob |
| 250 | Mat bottom_blob_int8(size, T, (size_t)1u, 1, opt.workspace_allocator); |
| 251 | Mat bottom_blob_int8_scales(T, (size_t)4u, 1, opt.workspace_allocator); |
| 252 | { |
| 253 | for (int t = 0; t < T; t++) |
| 254 | { |
| 255 | const float* x = bottom_blob.row(t); |
| 256 | |
| 257 | float absmax = 0.f; |
| 258 | for (int i = 0; i < size; i++) |
| 259 | { |
| 260 | absmax = std::max(absmax, (float)fabs(x[i])); |
| 261 | } |
| 262 | |
| 263 | bottom_blob_int8_scales[t] = 127.f / absmax; |
| 264 | } |
| 265 | |
| 266 | Option opt_quant = opt; |
| 267 | opt_quant.blob_allocator = opt.workspace_allocator; |
| 268 | opt_quant.use_packing_layout = false; |
| 269 | quantize_to_int8(bottom_blob, bottom_blob_int8, bottom_blob_int8_scales, opt_quant); |
| 270 | } |
| 271 | |
| 272 | Mat hidden_state_int8(num_output, (size_t)1u, 1, opt.workspace_allocator); |
| 273 | Mat hidden_state_int8_scales(1, (size_t)4u, 1, opt.workspace_allocator); |
| 274 | |
| 275 | // unroll |
| 276 | for (int t = 0; t < T; t++) |
| 277 | { |
| 278 | // clip hidden by continuation indicator |
| 279 | // h_cont_{t-1} = cont_t * h_{t-1} |
| 280 | // h_cont_{t-1} = h_{t-1} if cont_t == 1 |
| 281 | // 0 otherwise |
| 282 | // calculate hidden |
| 283 | // gate_input_t := W_hc * h_conted_{t-1} + W_xc * x_t + b_c |
| 284 | |
| 285 | int ti = reverse ? T - 1 - t : t; |
| 286 | |
| 287 | // dynamic quantize hidden_state |
| 288 | { |
| 289 | float absmax = 0.f; |
| 290 | for (int i = 0; i < num_output; i++) |
| 291 | { |
| 292 | absmax = std::max(absmax, (float)fabs(hidden_state[i])); |
| 293 | } |
| 294 | |
| 295 | if (absmax == 0.f) |
| 296 | { |
| 297 | hidden_state_int8_scales[0] = 1.f; |
| 298 | hidden_state_int8.fill<signed char>(0); |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | hidden_state_int8_scales[0] = 127.f / absmax; |
| 303 | |
| 304 | Option opt_quant = opt; |
| 305 | opt_quant.blob_allocator = opt.workspace_allocator; |
| 306 | opt_quant.use_packing_layout = false; |
| 307 | quantize_to_int8(hidden_state, hidden_state_int8, hidden_state_int8_scales, opt_quant); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | const signed char* x = bottom_blob_int8.row<const signed char>(ti); |
| 312 | const signed char* hs = hidden_state_int8; |
| 313 | const float descale_x = 1.f / bottom_blob_int8_scales[ti]; |
| 314 | const float descale_h = 1.f / hidden_state_int8_scales[0]; |
| 315 | #pragma omp parallel for num_threads(opt.num_threads) |
| 316 | for (int q = 0; q < hidden_size; q++) |
| 317 | { |
| 318 | const float* bias_c_I = bias_c.row(0); |
| 319 | const float* bias_c_F = bias_c.row(1); |
| 320 | const float* bias_c_O = bias_c.row(2); |
| 321 | const float* bias_c_G = bias_c.row(3); |
| 322 | |
| 323 | float* gates_data = gates.row(q); |
| 324 | |
| 325 | // gate I F O G |
| 326 | const signed char* weight_xc_int8_I = weight_xc_int8.row<const signed char>(hidden_size * 0 + q); |
| 327 | const signed char* weight_xc_int8_F = weight_xc_int8.row<const signed char>(hidden_size * 1 + q); |
| 328 | const signed char* weight_xc_int8_O = weight_xc_int8.row<const signed char>(hidden_size * 2 + q); |
| 329 | const signed char* weight_xc_int8_G = weight_xc_int8.row<const signed char>(hidden_size * 3 + q); |
| 330 | |
| 331 | const signed char* weight_hc_int8_I = weight_hc_int8.row<const signed char>(hidden_size * 0 + q); |
| 332 | const signed char* weight_hc_int8_F = weight_hc_int8.row<const signed char>(hidden_size * 1 + q); |
| 333 | const signed char* weight_hc_int8_O = weight_hc_int8.row<const signed char>(hidden_size * 2 + q); |
| 334 | const signed char* weight_hc_int8_G = weight_hc_int8.row<const signed char>(hidden_size * 3 + q); |
| 335 | |
| 336 | const float descale_xc_I = 1.f / weight_xc_int8_scales[hidden_size * 0 + q]; |
| 337 | const float descale_xc_F = 1.f / weight_xc_int8_scales[hidden_size * 1 + q]; |
| 338 | const float descale_xc_O = 1.f / weight_xc_int8_scales[hidden_size * 2 + q]; |
| 339 | const float descale_xc_G = 1.f / weight_xc_int8_scales[hidden_size * 3 + q]; |
| 340 | const float descale_hc_I = 1.f / weight_hc_int8_scales[hidden_size * 0 + q]; |
| 341 | const float descale_hc_F = 1.f / weight_hc_int8_scales[hidden_size * 1 + q]; |
| 342 | const float descale_hc_O = 1.f / weight_hc_int8_scales[hidden_size * 2 + q]; |
| 343 | const float descale_hc_G = 1.f / weight_hc_int8_scales[hidden_size * 3 + q]; |
| 344 | |
| 345 | int Ix = 0; |
| 346 | int Fx = 0; |
| 347 | int Ox = 0; |
| 348 | int Gx = 0; |
| 349 | for (int i = 0; i < size; i++) |
| 350 | { |
| 351 | signed char xi = x[i]; |
| 352 | |
| 353 | Ix += weight_xc_int8_I[i] * xi; |
| 354 | Fx += weight_xc_int8_F[i] * xi; |
| 355 | Ox += weight_xc_int8_O[i] * xi; |
| 356 | Gx += weight_xc_int8_G[i] * xi; |
| 357 | } |
| 358 | |
| 359 | int Ih = 0; |
| 360 | int Fh = 0; |
| 361 | int Oh = 0; |
| 362 | int Gh = 0; |
| 363 | for (int i = 0; i < num_output; i++) |
| 364 | { |
| 365 | signed char h_cont = hs[i]; |
| 366 | |
| 367 | Ih += weight_hc_int8_I[i] * h_cont; |
| 368 | Fh += weight_hc_int8_F[i] * h_cont; |
| 369 | Oh += weight_hc_int8_O[i] * h_cont; |
| 370 | Gh += weight_hc_int8_G[i] * h_cont; |
| 371 | } |
| 372 | |
| 373 | float I = bias_c_I[q] + Ix * (descale_x * descale_xc_I) + Ih * (descale_h * descale_hc_I); |
| 374 | float F = bias_c_F[q] + Fx * (descale_x * descale_xc_F) + Fh * (descale_h * descale_hc_F); |
| 375 | float O = bias_c_O[q] + Ox * (descale_x * descale_xc_O) + Oh * (descale_h * descale_hc_O); |
| 376 | float G = bias_c_G[q] + Gx * (descale_x * descale_xc_G) + Gh * (descale_h * descale_hc_G); |
| 377 | |
| 378 | gates_data[0] = I; |
| 379 | gates_data[1] = F; |
| 380 | gates_data[2] = O; |
| 381 | gates_data[3] = G; |
| 382 | } |
| 383 | |
| 384 | // lstm unit |
| 385 | // sigmoid(I) |
| 386 | // sigmoid(F) |
| 387 | // sigmoid(O) |
| 388 | // tanh(G) |
| 389 | // c_t := f_t .* c_{t-1} + i_t .* g_t |
| 390 | // h_t := o_t .* tanh[c_t] |
| 391 | float* output_data = top_blob.row(ti); |
| 392 | #pragma omp parallel for num_threads(opt.num_threads) |
| 393 | for (int q = 0; q < hidden_size; q++) |
| 394 | { |
| 395 | const float* gates_data = gates.row(q); |
| 396 | |
| 397 | float I = gates_data[0]; |
| 398 | float F = gates_data[1]; |
| 399 | float O = gates_data[2]; |
| 400 | float G = gates_data[3]; |
| 401 | |
| 402 | I = 1.f / (1.f + expf(-I)); |
| 403 | F = 1.f / (1.f + expf(-F)); |
| 404 | O = 1.f / (1.f + expf(-O)); |
| 405 | G = tanhf(G); |
| 406 | |
| 407 | float cell2 = F * cell_state[q] + I * G; |
| 408 | float H = O * tanhf(cell2); |
| 409 | cell_state[q] = cell2; |
| 410 | |
| 411 | if (num_output == hidden_size) |
| 412 | { |
| 413 | hidden_state[q] = H; |
| 414 | output_data[q] = H; |
| 415 | } |
| 416 | else |
| 417 | { |
| 418 | tmp_hidden_state[q] = H; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | if (num_output != hidden_size) |
| 423 | { |
| 424 | #pragma omp parallel for num_threads(opt.num_threads) |
| 425 | for (int q = 0; q < num_output; q++) |
| 426 | { |
| 427 | const float* hr = weight_hr.row(q); |
| 428 | |
| 429 | float H = 0; |
| 430 | for (int i = 0; i < hidden_size; i++) |
| 431 | { |
| 432 | H += tmp_hidden_state[i] * hr[i]; |
| 433 | } |
| 434 | |
| 435 | hidden_state[q] = H; |
| 436 | output_data[q] = H; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | #endif // NCNN_INT8 |
| 444 | |
| 445 | int LSTM::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const |
| 446 | { |
| 447 | int T = bottom_blob.h; |
| 448 | |
| 449 | int num_directions = direction == 2 ? 2 : 1; |
| 450 | |
| 451 | // initial hidden state |
| 452 | Mat hidden(num_output, 4u, opt.workspace_allocator); |
| 453 | if (hidden.empty()) |
| 454 | return -100; |
| 455 | hidden.fill(0.f); |
| 456 | |
| 457 | Mat cell(hidden_size, 4u, opt.workspace_allocator); |
| 458 | if (cell.empty()) |
| 459 | return -100; |
| 460 | cell.fill(0.f); |
| 461 | |
| 462 | top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator); |
| 463 | if (top_blob.empty()) |
| 464 | return -100; |
| 465 | |
| 466 | // Uni directional |
| 467 | if (direction == 0 || direction == 1) |
| 468 | { |
| 469 | #if NCNN_INT8 |
| 470 | if (int8_scale_term) |
| 471 | { |
| 472 | int ret = lstm_int8(bottom_blob, top_blob, direction, weight_xc_data.channel(0), weight_xc_data_int8_scales.row(0), bias_c_data.channel(0), weight_hc_data.channel(0), weight_hc_data_int8_scales.row(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt); |
| 473 | if (ret != 0) |
| 474 | return ret; |
| 475 | } |
| 476 | else |
| 477 | #endif |
| 478 | { |
| 479 | int ret = lstm(bottom_blob, top_blob, direction, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt); |
| 480 | if (ret != 0) |
| 481 | return ret; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | if (direction == 2) |
| 486 | { |
| 487 | Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator); |
| 488 | if (top_blob_forward.empty()) |
| 489 | return -100; |
| 490 | |
| 491 | Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator); |
| 492 | if (top_blob_reverse.empty()) |
| 493 | return -100; |
| 494 | |
| 495 | #if NCNN_INT8 |
| 496 | if (int8_scale_term) |
| 497 | { |
| 498 | int ret = lstm_int8(bottom_blob, top_blob_forward, 0, weight_xc_data.channel(0), weight_xc_data_int8_scales.row(0), bias_c_data.channel(0), weight_hc_data.channel(0), weight_hc_data_int8_scales.row(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt); |
| 499 | if (ret != 0) |
| 500 | return ret; |
| 501 | } |
| 502 | else |
| 503 | #endif |
| 504 | { |
| 505 | int ret = lstm(bottom_blob, top_blob_forward, 0, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt); |
| 506 | if (ret != 0) |
| 507 | return ret; |
| 508 | } |
| 509 | |
| 510 | hidden.fill(0.0f); |
| 511 | cell.fill(0.0f); |
| 512 | |
| 513 | #if NCNN_INT8 |
| 514 | if (int8_scale_term) |
| 515 | { |
| 516 | int ret = lstm_int8(bottom_blob, top_blob_reverse, 1, weight_xc_data.channel(1), weight_xc_data_int8_scales.row(1), bias_c_data.channel(1), weight_hc_data.channel(1), weight_hc_data_int8_scales.row(1), num_output == hidden_size ? Mat() : weight_hr_data.channel(1), hidden, cell, opt); |
| 517 | if (ret != 0) |
| 518 | return ret; |
| 519 | } |
| 520 | else |
| 521 | #endif |
| 522 | { |
| 523 | int ret = lstm(bottom_blob, top_blob_reverse, 1, weight_xc_data.channel(1), bias_c_data.channel(1), weight_hc_data.channel(1), num_output == hidden_size ? Mat() : weight_hr_data.channel(1), hidden, cell, opt); |
| 524 | if (ret != 0) |
| 525 | return ret; |
| 526 | } |
| 527 | |
| 528 | // concat w |
| 529 | for (int i = 0; i < T; i++) |
| 530 | { |
| 531 | const float* pf = top_blob_forward.row(i); |
| 532 | const float* pr = top_blob_reverse.row(i); |
| 533 | float* ptr = top_blob.row(i); |
| 534 | |
| 535 | memcpy(ptr, pf, num_output * sizeof(float)); |
| 536 | memcpy(ptr + num_output, pr, num_output * sizeof(float)); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | int LSTM::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const |
| 544 | { |
| 545 | const Mat& bottom_blob = bottom_blobs[0]; |
| 546 | int T = bottom_blob.h; |
| 547 | int num_directions = direction == 2 ? 2 : 1; |
| 548 | |
| 549 | Mat hidden; |
| 550 | Mat cell; |
| 551 | Allocator* hidden_cell_allocator = top_blobs.size() == 3 ? opt.blob_allocator : opt.workspace_allocator; |
| 552 | if (bottom_blobs.size() == 3) |
| 553 | { |
| 554 | hidden = bottom_blobs[1].clone(hidden_cell_allocator); |
| 555 | cell = bottom_blobs[2].clone(hidden_cell_allocator); |
| 556 | } |
| 557 | else |
| 558 | { |
| 559 | hidden.create(num_output, num_directions, 4u, hidden_cell_allocator); |
| 560 | if (hidden.empty()) |
| 561 | return -100; |
| 562 | hidden.fill(0.f); |
| 563 | |
| 564 | cell.create(hidden_size, num_directions, 4u, hidden_cell_allocator); |
| 565 | if (cell.empty()) |
| 566 | return -100; |
| 567 | cell.fill(0.f); |
| 568 | } |
| 569 | |
| 570 | Mat& top_blob = top_blobs[0]; |
| 571 | top_blob.create(num_output * num_directions, T, 4u, opt.blob_allocator); |
| 572 | if (top_blob.empty()) |
| 573 | return -100; |
| 574 | |
| 575 | // Uni directional |
| 576 | if (direction == 0 || direction == 1) |
| 577 | { |
| 578 | #if NCNN_INT8 |
| 579 | if (int8_scale_term) |
| 580 | { |
| 581 | int ret = lstm_int8(bottom_blob, top_blob, direction, weight_xc_data.channel(0), weight_xc_data_int8_scales.row(0), bias_c_data.channel(0), weight_hc_data.channel(0), weight_hc_data_int8_scales.row(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt); |
| 582 | if (ret != 0) |
| 583 | return ret; |
| 584 | } |
| 585 | else |
| 586 | #endif |
| 587 | { |
| 588 | int ret = lstm(bottom_blob, top_blob, direction, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden, cell, opt); |
| 589 | if (ret != 0) |
| 590 | return ret; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | if (direction == 2) |
| 595 | { |
| 596 | Mat top_blob_forward(num_output, T, 4u, opt.workspace_allocator); |
| 597 | if (top_blob_forward.empty()) |
| 598 | return -100; |
| 599 | |
| 600 | Mat top_blob_reverse(num_output, T, 4u, opt.workspace_allocator); |
| 601 | if (top_blob_reverse.empty()) |
| 602 | return -100; |
| 603 | |
| 604 | Mat hidden0 = hidden.row_range(0, 1); |
| 605 | Mat cell0 = cell.row_range(0, 1); |
| 606 | #if NCNN_INT8 |
| 607 | if (int8_scale_term) |
| 608 | { |
| 609 | int ret = lstm_int8(bottom_blob, top_blob_forward, 0, weight_xc_data.channel(0), weight_xc_data_int8_scales.row(0), bias_c_data.channel(0), weight_hc_data.channel(0), weight_hc_data_int8_scales.row(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden0, cell0, opt); |
| 610 | if (ret != 0) |
| 611 | return ret; |
| 612 | } |
| 613 | else |
| 614 | #endif |
| 615 | { |
| 616 | int ret = lstm(bottom_blob, top_blob_forward, 0, weight_xc_data.channel(0), bias_c_data.channel(0), weight_hc_data.channel(0), num_output == hidden_size ? Mat() : weight_hr_data.channel(0), hidden0, cell0, opt); |
| 617 | if (ret != 0) |
| 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | Mat hidden1 = hidden.row_range(1, 1); |
| 622 | Mat cell1 = cell.row_range(1, 1); |
| 623 | #if NCNN_INT8 |
| 624 | if (int8_scale_term) |
| 625 | { |
| 626 | int ret = lstm_int8(bottom_blob, top_blob_reverse, 1, weight_xc_data.channel(1), weight_xc_data_int8_scales.row(1), bias_c_data.channel(1), weight_hc_data.channel(1), weight_hc_data_int8_scales.row(1), num_output == hidden_size ? Mat() : weight_hr_data.channel(1), hidden1, cell1, opt); |
| 627 | if (ret != 0) |
| 628 | return ret; |
| 629 | } |
| 630 | else |
| 631 | #endif |
| 632 | { |
| 633 | int ret = lstm(bottom_blob, top_blob_reverse, 1, weight_xc_data.channel(1), bias_c_data.channel(1), weight_hc_data.channel(1), num_output == hidden_size ? Mat() : weight_hr_data.channel(1), hidden1, cell1, opt); |
| 634 | if (ret != 0) |
| 635 | return ret; |
| 636 | } |
| 637 | |
| 638 | // concat w |
| 639 | for (int i = 0; i < T; i++) |
| 640 | { |
| 641 | const float* pf = top_blob_forward.row(i); |
| 642 | const float* pr = top_blob_reverse.row(i); |
| 643 | float* ptr = top_blob.row(i); |
| 644 | |
| 645 | memcpy(ptr, pf, num_output * sizeof(float)); |
| 646 | memcpy(ptr + num_output, pr, num_output * sizeof(float)); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | if (top_blobs.size() == 3) |
| 651 | { |
| 652 | top_blobs[1] = hidden; |
| 653 | top_blobs[2] = cell; |
| 654 | } |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | } // namespace ncnn |