lstm_cpu_kernel.h 20.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
D
dangqingqing 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#pragma once
16
#include <type_traits>
17

18
#include "paddle/phi/kernels/funcs/activation_functor.h"
F
Feiyu Chan 已提交
19
#include "paddle/phi/kernels/funcs/detail/activation_functions.h"
20
#include "paddle/phi/kernels/funcs/eigen/common.h"
F
Feiyu Chan 已提交
21
#include "paddle/phi/kernels/funcs/lstm_compute.h"
D
dangqingqing 已提交
22

P
peizhilin 已提交
23 24 25 26 27 28
#if defined(_WIN32)
#if defined(__AVX2__) || defined(__AVX__)
inline __m256 operator+=(__m256 a, __m256 b) { return _mm256_add_ps(a, b); }
#endif
#endif

F
Feiyu Chan 已提交
29 30
namespace phi {
namespace funcs {
D
dangqingqing 已提交
31 32
namespace detail {

33
using Array1 = Eigen::DSizes<int64_t, 1>;
F
Feiyu Chan 已提交
34 35
template <typename T,
          int MajorType = Eigen::RowMajor,
36
          typename IndexType = Eigen::DenseIndex>
37
using EigenVector = phi::EigenVector<T, MajorType, IndexType>;
38

39
#if !defined(__NVCC__) && !defined(__HIPCC___)  // @{ Group LSTM CPU
D
dangqingqing 已提交
40 41

template <class T, class Op>
F
Feiyu Chan 已提交
42 43 44 45
void naive_lstm_forward_one_sequence(Op op,
                                     phi::funcs::LstmMetaValue<T> value,
                                     int frame_size,
                                     T cell_clip,
46
                                     ActivationType active_node,
47
                                     ActivationType active_gate,
48 49
                                     ActivationType active_state,
                                     bool old_api_version) {
50 51 52 53 54 55 56 57 58 59 60 61
  T r_value_in;
  T r_value_ig;
  T r_value_fg;
  T r_value_og;
  T r_checkI;
  T r_checkF;
  T r_checkO;
  T r_state;
  T r_prev_state = 0;
  T r_state_atv;
  T r_out;

62 63 64
  T *value_ig = value.gate_value;
  T *value_fg = value.gate_value + frame_size;
  T *value_in = value.gate_value + frame_size * 2;
65
  T *value_og = value.gate_value + frame_size * 3;
66 67 68 69 70
  if (old_api_version) {
    value_in = value.gate_value;
    value_ig = value.gate_value + frame_size;
    value_fg = value.gate_value + frame_size * 2;
  }
71 72 73 74 75 76 77 78 79 80 81 82

  for (int i = 0; i < frame_size; i++) {
    r_value_in = value_in[i];
    r_value_ig = value_ig[i];
    r_value_fg = value_fg[i];
    r_value_og = value_og[i];
    r_checkI = value.check_ig ? value.check_ig[i] : 0;
    r_checkF = value.check_fg ? value.check_fg[i] : 0;
    r_checkO = value.check_og ? value.check_og[i] : 0;

    if (value.prev_state_value) {
      r_prev_state = value.prev_state_value[i];
D
dangqingqing 已提交
83 84
    }

F
Feiyu Chan 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    op(&r_value_in,
       &r_value_ig,
       &r_value_fg,
       &r_value_og,
       &r_prev_state,
       &r_state,
       &r_state_atv,
       &r_out,
       &r_checkI,
       &r_checkF,
       &r_checkO,
       &cell_clip,
       active_node,
       active_gate,
       active_state);
100 101 102 103 104 105 106 107

    value_in[i] = r_value_in;
    value_ig[i] = r_value_ig;
    value_fg[i] = r_value_fg;
    value_og[i] = r_value_og;
    value.state_value[i] = r_state;
    value.state_active_value[i] = r_state_atv;
    value.output_value[i] = r_out;
D
dangqingqing 已提交
108 109 110 111
  }
}

template <class T, class Op>
F
Feiyu Chan 已提交
112 113 114 115 116 117
void naive_lstm_backward_one_sequence(Op op,
                                      phi::funcs::LstmMetaValue<T> value,
                                      phi::funcs::LstmMetaGrad<T> grad,
                                      int frame_size,
                                      T cell_clip,
                                      ActivationType active_node,
118
                                      ActivationType active_gate,
119 120
                                      ActivationType active_state,
                                      bool old_api_version) {
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  T r_value_in;
  T r_value_ig;
  T r_value_fg;
  T r_value_og;
  T r_grad_in;
  T r_grad_ig;
  T r_grad_fg;
  T r_grad_og;
  T r_prev_state = 0;
  T r_prev_state_grad;
  T r_state;
  T r_state_grad;
  T r_state_atv;
  T r_output_grad;
  T r_checkI;
  T r_checkF;
  T r_checkO;
  T r_checkIGrad;
  T r_checkFGrad;
  T r_checkOGrad;

142 143 144
  T *value_ig = value.gate_value;
  T *value_fg = value.gate_value + frame_size;
  T *value_in = value.gate_value + frame_size * 2;
145
  T *value_og = value.gate_value + frame_size * 3;
146 147 148 149 150 151 152 153 154
  if (old_api_version) {
    value_in = value.gate_value;
    value_ig = value.gate_value + frame_size;
    value_fg = value.gate_value + frame_size * 2;
  }

  T *grad_ig = grad.gate_grad;
  T *grad_fg = grad.gate_grad + frame_size;
  T *grad_in = grad.gate_grad + frame_size * 2;
155
  T *grad_og = grad.gate_grad + frame_size * 3;
156 157 158 159 160
  if (old_api_version) {
    grad_in = grad.gate_grad;
    grad_ig = grad.gate_grad + frame_size;
    grad_fg = grad.gate_grad + frame_size * 2;
  }
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

  for (int i = 0; i < frame_size; i++) {
    r_value_in = value_in[i];
    r_value_ig = value_ig[i];
    r_value_fg = value_fg[i];
    r_value_og = value_og[i];
    r_checkI = value.check_ig ? value.check_ig[i] : 0;
    r_checkF = value.check_fg ? value.check_fg[i] : 0;
    r_checkO = value.check_og ? value.check_og[i] : 0;
    r_state = value.state_value[i];
    r_state_atv = value.state_active_value[i];
    r_output_grad = grad.output_grad[i];
    r_state_grad = grad.state_grad[i];
    if (value.prev_state_value) {
      r_prev_state = value.prev_state_value[i];
D
dangqingqing 已提交
176 177
    }

F
Feiyu Chan 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    op(&r_value_in,
       &r_value_ig,
       &r_value_fg,
       &r_value_og,
       &r_grad_in,
       &r_grad_ig,
       &r_grad_fg,
       &r_grad_og,
       &r_prev_state,
       &r_prev_state_grad,
       &r_state,
       &r_state_grad,
       &r_state_atv,
       &r_output_grad,
       &r_checkI,
       &r_checkF,
       &r_checkO,
       &r_checkIGrad,
       &r_checkFGrad,
       &r_checkOGrad,
       &cell_clip,
       active_node,
       active_gate,
       active_state);
202 203 204 205 206 207 208 209 210 211 212

    grad_in[i] = r_grad_in;
    grad_ig[i] = r_grad_ig;
    grad_fg[i] = r_grad_fg;
    grad_og[i] = r_grad_og;
    grad.state_grad[i] = r_state_grad;

    if (grad.prev_state_grad) grad.prev_state_grad[i] = r_prev_state_grad;
    if (value.prev_state_value) {
      if (grad.check_ig_grad) grad.check_ig_grad[i] += r_checkIGrad;
      if (grad.check_fg_grad) grad.check_fg_grad[i] += r_checkFGrad;
D
dangqingqing 已提交
213
    }
214
    if (grad.check_og_grad) grad.check_og_grad[i] += r_checkOGrad;
D
dangqingqing 已提交
215 216 217
  }
}

218
template <class T, class Op>
F
Feiyu Chan 已提交
219 220 221 222
void avx_lstm_forward_one_sequence(Op op,
                                   phi::funcs::LstmMetaValue<T> value,
                                   int frame_size,
                                   T cell_clip,
223
                                   ActivationType active_node,
224
                                   ActivationType active_gate,
225 226
                                   ActivationType active_state,
                                   bool old_api_version) {
D
dangqingqing 已提交
227
#ifdef __AVX__
228 229 230 231 232 233 234 235 236 237 238 239
  __m256 r_value_in;
  __m256 r_value_ig;
  __m256 r_value_fg;
  __m256 r_value_og;
  __m256 r_checkI = _mm256_set1_ps(0.0f);
  __m256 r_checkF = _mm256_set1_ps(0.0f);
  __m256 r_checkO = _mm256_set1_ps(0.0f);
  __m256 r_state;
  __m256 r_prev_state = _mm256_set1_ps(0.0f);
  __m256 r_state_atv;
  __m256 r_out;

240 241 242
  __m256 *value_ig = reinterpret_cast<__m256 *>(value.gate_value);
  __m256 *value_fg = reinterpret_cast<__m256 *>(value.gate_value + frame_size);
  __m256 *value_in =
243 244 245
      reinterpret_cast<__m256 *>(value.gate_value + frame_size * 2);
  __m256 *value_og =
      reinterpret_cast<__m256 *>(value.gate_value + frame_size * 3);
246 247 248 249 250
  if (old_api_version) {
    value_in = reinterpret_cast<__m256 *>(value.gate_value);
    value_ig = reinterpret_cast<__m256 *>(value.gate_value + frame_size);
    value_fg = reinterpret_cast<__m256 *>(value.gate_value + frame_size * 2);
  }
251 252 253 254 255 256 257

  for (int i = 0; i < frame_size / 8; i++) {
    r_value_in = value_in[i];
    r_value_ig = value_ig[i];
    r_value_fg = value_fg[i];
    r_value_og = value_og[i];
    if (value.check_ig) {
258 259 260
      r_checkI = (reinterpret_cast<__m256 *>(value.check_ig))[i];
      r_checkF = (reinterpret_cast<__m256 *>(value.check_fg))[i];
      r_checkO = (reinterpret_cast<__m256 *>(value.check_og))[i];
D
dangqingqing 已提交
261
    }
D
dangqingqing 已提交
262

263
    if (value.prev_state_value) {
264 265
      r_prev_state =
          (reinterpret_cast<__m256 const *>(value.prev_state_value))[i];
D
dangqingqing 已提交
266 267
    }

F
Feiyu Chan 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    op(&r_value_in,
       &r_value_ig,
       &r_value_fg,
       &r_value_og,
       &r_prev_state,
       &r_state,
       &r_state_atv,
       &r_out,
       &r_checkI,
       &r_checkF,
       &r_checkO,
       &cell_clip,
       active_node,
       active_gate,
       active_state);
283 284 285 286 287

    value_in[i] = r_value_in;
    value_ig[i] = r_value_ig;
    value_fg[i] = r_value_fg;
    value_og[i] = r_value_og;
288 289 290
    (reinterpret_cast<__m256 *>(value.state_value))[i] = r_state;
    (reinterpret_cast<__m256 *>(value.state_active_value))[i] = r_state_atv;
    (reinterpret_cast<__m256 *>(value.output_value))[i] = r_out;
D
dangqingqing 已提交
291 292 293 294
  }
#endif
}

295
template <class T, class Op>
F
Feiyu Chan 已提交
296 297 298 299 300 301
void avx_lstm_backward_one_sequence(Op op,
                                    phi::funcs::LstmMetaValue<T> value,
                                    phi::funcs::LstmMetaGrad<T> grad,
                                    int frame_size,
                                    T cell_clip,
                                    ActivationType active_node,
302
                                    ActivationType active_gate,
303 304
                                    ActivationType active_state,
                                    bool old_api_version) {
D
dangqingqing 已提交
305
#ifdef __AVX__
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
  __m256 r_value_in;
  __m256 r_value_ig;
  __m256 r_value_fg;
  __m256 r_value_og;
  __m256 r_grad_in;
  __m256 r_grad_ig;
  __m256 r_grad_fg;
  __m256 r_grad_og;
  __m256 r_prev_state = _mm256_set1_ps(0.0f);
  __m256 r_prev_state_grad;
  __m256 r_state_grad;
  __m256 r_state;
  __m256 r_state_atv;
  __m256 r_output_grad;
  __m256 r_checkI = _mm256_set1_ps(0.0f);
  __m256 r_checkF = _mm256_set1_ps(0.0f);
  __m256 r_checkO = _mm256_set1_ps(0.0f);
  __m256 r_checkIGrad;
  __m256 r_checkFGrad;
  __m256 r_checkOGrad;

327 328 329
  __m256 *value_ig = reinterpret_cast<__m256 *>(value.gate_value);
  __m256 *value_fg = reinterpret_cast<__m256 *>(value.gate_value + frame_size);
  __m256 *value_in =
330 331 332
      reinterpret_cast<__m256 *>(value.gate_value + frame_size * 2);
  __m256 *value_og =
      reinterpret_cast<__m256 *>(value.gate_value + frame_size * 3);
333 334 335 336 337 338 339 340 341
  if (old_api_version) {
    value_in = reinterpret_cast<__m256 *>(value.gate_value);
    value_ig = reinterpret_cast<__m256 *>(value.gate_value + frame_size);
    value_fg = reinterpret_cast<__m256 *>(value.gate_value + frame_size * 2);
  }

  __m256 *grad_ig = reinterpret_cast<__m256 *>(grad.gate_grad);
  __m256 *grad_fg = reinterpret_cast<__m256 *>(grad.gate_grad + frame_size);
  __m256 *grad_in = reinterpret_cast<__m256 *>(grad.gate_grad + frame_size * 2);
342
  __m256 *grad_og = reinterpret_cast<__m256 *>(grad.gate_grad + frame_size * 3);
343 344 345 346 347
  if (old_api_version) {
    grad_in = reinterpret_cast<__m256 *>(grad.gate_grad);
    grad_ig = reinterpret_cast<__m256 *>(grad.gate_grad + frame_size);
    grad_fg = reinterpret_cast<__m256 *>(grad.gate_grad + frame_size * 2);
  }
348 349 350 351 352 353 354

  for (int i = 0; i < frame_size / 8; i++) {
    r_value_in = value_in[i];
    r_value_ig = value_ig[i];
    r_value_fg = value_fg[i];
    r_value_og = value_og[i];
    if (value.check_ig) {
355 356 357
      r_checkI = (reinterpret_cast<__m256 *>(value.check_ig))[i];
      r_checkF = (reinterpret_cast<__m256 *>(value.check_fg))[i];
      r_checkO = (reinterpret_cast<__m256 *>(value.check_og))[i];
D
dangqingqing 已提交
358
    }
359 360 361 362
    r_state = (reinterpret_cast<__m256 *>(value.state_value))[i];
    r_state_atv = (reinterpret_cast<__m256 *>(value.state_active_value))[i];
    r_output_grad = (reinterpret_cast<__m256 *>(grad.output_grad))[i];
    r_state_grad = (reinterpret_cast<__m256 *>(grad.state_grad))[i];
363
    if (value.prev_state_value) {
364 365
      r_prev_state =
          (reinterpret_cast<__m256 const *>(value.prev_state_value))[i];
D
dangqingqing 已提交
366 367
    }

F
Feiyu Chan 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    op(&r_value_in,
       &r_value_ig,
       &r_value_fg,
       &r_value_og,
       &r_grad_in,
       &r_grad_ig,
       &r_grad_fg,
       &r_grad_og,
       &r_prev_state,
       &r_prev_state_grad,
       &r_state,
       &r_state_grad,
       &r_state_atv,
       &r_output_grad,
       &r_checkI,
       &r_checkF,
       &r_checkO,
       &r_checkIGrad,
       &r_checkFGrad,
       &r_checkOGrad,
       &cell_clip,
       active_node,
       active_gate,
       active_state);
392 393 394 395 396

    grad_in[i] = r_grad_in;
    grad_ig[i] = r_grad_ig;
    grad_fg[i] = r_grad_fg;
    grad_og[i] = r_grad_og;
397
    (reinterpret_cast<__m256 *>(grad.state_grad))[i] = r_state_grad;
398 399

    if (grad.prev_state_grad)
400
      (reinterpret_cast<__m256 *>(grad.prev_state_grad))[i] = r_prev_state_grad;
401
    if (value.prev_state_value) {
402 403 404 405
      if (grad.check_ig_grad)
        (reinterpret_cast<__m256 *>(grad.check_ig_grad))[i] += r_checkIGrad;
      if (grad.check_fg_grad)
        (reinterpret_cast<__m256 *>(grad.check_fg_grad))[i] += r_checkFGrad;
D
dangqingqing 已提交
406
    }
407 408
    if (grad.check_og_grad)
      (reinterpret_cast<__m256 *>(grad.check_og_grad))[i] += r_checkOGrad;
D
dangqingqing 已提交
409 410 411 412
  }
#endif
}

413 414 415 416
template <class T, class Context>
void eigen_lstm_forward_one_sequence(const Context &context,
                                     phi::funcs::LstmMetaValue<T> value,
                                     int frame_size) {
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
  auto eigen_value_ig =
      typename EigenVector<T>::Type(value.gate_value, Array1(frame_size));
  auto eigen_value_fg = typename EigenVector<T>::Type(
      value.gate_value + frame_size, Array1(frame_size));
  auto eigen_value_in = typename EigenVector<T>::Type(
      value.gate_value + frame_size * 2, Array1(frame_size));
  auto eigen_value_og = typename EigenVector<T>::Type(
      value.gate_value + frame_size * 3, Array1(frame_size));
  auto eigen_state =
      typename EigenVector<T>::Type(value.state_value, Array1(frame_size));
  auto eigen_state_act = typename EigenVector<T>::Type(value.state_active_value,
                                                       Array1(frame_size));
  auto eigen_output =
      typename EigenVector<T>::Type(value.output_value, Array1(frame_size));

  auto &place = *context.eigen_device();
433 434 435 436
  TanhFunctor<T>()(place, eigen_value_in, eigen_value_in);
  SigmoidFunctor<T>()(place, eigen_value_ig, eigen_value_ig);
  SigmoidFunctor<T>()(place, eigen_value_fg, eigen_value_fg);
  SigmoidFunctor<T>()(place, eigen_value_og, eigen_value_og);
437 438 439 440 441 442 443 444

  eigen_state.device(place) = eigen_value_in * eigen_value_ig;
  if (value.prev_state_value) {
    auto eigen_prev_state = typename EigenVector<T>::ConstType(
        value.prev_state_value, Array1(frame_size));
    eigen_state.device(place) = eigen_state + eigen_prev_state * eigen_value_fg;
  }

445
  TanhFunctor<T>()(place, eigen_state, eigen_state_act);
446 447 448
  eigen_output.device(place) = eigen_value_og * eigen_state_act;
}

449 450 451 452 453
template <class T, class Context>
void eigen_lstm_backward_one_sequence(const Context &context,
                                      phi::funcs::LstmMetaValue<T> value,
                                      phi::funcs::LstmMetaGrad<T> grad,
                                      int frame_size) {
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
  auto eigen_value_ig =
      typename EigenVector<T>::Type(value.gate_value, Array1(frame_size));
  auto eigen_value_fg = typename EigenVector<T>::Type(
      value.gate_value + frame_size, Array1(frame_size));
  auto eigen_value_in = typename EigenVector<T>::Type(
      value.gate_value + frame_size * 2, Array1(frame_size));
  auto eigen_value_og = typename EigenVector<T>::Type(
      value.gate_value + frame_size * 3, Array1(frame_size));
  auto eigen_state_act = typename EigenVector<T>::Type(value.state_active_value,
                                                       Array1(frame_size));

  auto eigen_grad_ig =
      typename EigenVector<T>::Type(grad.gate_grad, Array1(frame_size));
  auto eigen_grad_fg = typename EigenVector<T>::Type(
      grad.gate_grad + frame_size, Array1(frame_size));
  auto eigen_grad_in = typename EigenVector<T>::Type(
      grad.gate_grad + frame_size * 2, Array1(frame_size));
  auto eigen_grad_og = typename EigenVector<T>::Type(
      grad.gate_grad + frame_size * 3, Array1(frame_size));
  auto eigen_grad_output =
      typename EigenVector<T>::Type(grad.output_grad, Array1(frame_size));
  auto eigen_grad_state =
      typename EigenVector<T>::Type(grad.state_grad, Array1(frame_size));

  auto &place = *context.eigen_device();
479 480 481 482 483
  SigmoidGradFunctor<T>()(place,
                          1 /*useless*/,
                          eigen_value_og,
                          eigen_grad_output * eigen_state_act,
                          eigen_grad_og);
484 485 486 487
  eigen_grad_state.device(place) =
      eigen_grad_state +
      eigen_grad_output * eigen_value_og *
          (static_cast<T>(1) - eigen_state_act * eigen_state_act);
488 489 490 491 492 493 494 495 496 497
  TanhGradFunctor<T>()(place,
                       1,
                       eigen_value_in,
                       eigen_grad_state * eigen_value_ig,
                       eigen_grad_in);
  SigmoidGradFunctor<T>()(place,
                          1,
                          eigen_value_ig,
                          eigen_grad_state * eigen_value_in,
                          eigen_grad_ig);
498 499 500
  if (value.prev_state_value) {
    auto eigen_prev_state = typename EigenVector<T>::ConstType(
        value.prev_state_value, Array1(frame_size));
501 502 503 504 505
    SigmoidGradFunctor<T>()(place,
                            1,
                            eigen_value_fg,
                            eigen_grad_state * eigen_prev_state,
                            eigen_grad_fg);
506
  } else {
507
    SigmoidGradFunctor<T>()(place, 1, eigen_value_fg, 0, eigen_grad_fg);
508 509 510 511 512 513 514 515
  }
  if (grad.prev_state_grad) {
    auto eigen_grad_pre_state =
        typename EigenVector<T>::Type(grad.prev_state_grad, Array1(frame_size));
    eigen_grad_pre_state.device(place) = eigen_grad_state * eigen_value_fg;
  }
}

516 517
template <class T, class Op, class Context>
void cpu_lstm_forward(const Context &context,
F
Feiyu Chan 已提交
518 519 520 521 522 523 524 525
                      Op op,
                      phi::funcs::LstmMetaValue<T> value,
                      int frame_size,
                      T cell_clip,
                      ActivationType active_node,
                      ActivationType active_gate,
                      ActivationType active_state,
                      bool old_api_version) {
526 527
  if (!old_api_version) {
    eigen_lstm_forward_one_sequence<T>(context, value, frame_size);
D
dangqingqing 已提交
528
  } else {
529
    if (Op::avx && !(frame_size & (8 - 1)) && (std::is_same<T, float>::value)) {
F
Feiyu Chan 已提交
530 531 532 533 534 535 536
      avx_lstm_forward_one_sequence<T>(op,
                                       value,
                                       frame_size,
                                       cell_clip,
                                       active_node,
                                       active_gate,
                                       active_state,
537 538
                                       old_api_version);
    } else {
F
Feiyu Chan 已提交
539 540 541 542 543 544 545
      naive_lstm_forward_one_sequence<T>(op,
                                         value,
                                         frame_size,
                                         cell_clip,
                                         active_node,
                                         active_gate,
                                         active_state,
546 547
                                         old_api_version);
    }
D
dangqingqing 已提交
548 549 550
  }
}

551 552
template <class T, class Op, class Context>
void cpu_lstm_backward(const Context &context,
F
Feiyu Chan 已提交
553 554 555 556 557 558 559 560
                       Op op,
                       phi::funcs::LstmMetaValue<T> value,
                       phi::funcs::LstmMetaGrad<T> grad,
                       int frame_size,
                       T cell_clip,
                       ActivationType active_node,
                       ActivationType active_gate,
                       ActivationType active_state,
561 562 563
                       bool old_api_version) {
  if (!old_api_version) {
    eigen_lstm_backward_one_sequence<T>(context, value, grad, frame_size);
D
dangqingqing 已提交
564
  } else {
565
    if (Op::avx && !(frame_size & (8 - 1)) && (std::is_same<T, float>::value)) {
F
Feiyu Chan 已提交
566 567 568 569 570 571 572 573
      avx_lstm_backward_one_sequence<T>(op,
                                        value,
                                        grad,
                                        frame_size,
                                        cell_clip,
                                        active_node,
                                        active_gate,
                                        active_state,
574 575
                                        old_api_version);
    } else {
F
Feiyu Chan 已提交
576 577 578 579 580 581 582 583 584
      naive_lstm_backward_one_sequence<T>(op,
                                          value,
                                          grad,
                                          frame_size,
                                          cell_clip,
                                          active_node,
                                          active_gate,
                                          active_state,
                                          old_api_version);
585
    }
D
dangqingqing 已提交
586 587 588
  }
}

589
#endif  // @{ End Group LSTM CPU
D
dangqingqing 已提交
590 591

}  // namespace detail
F
Feiyu Chan 已提交
592 593
}  // namespace funcs
}  // namespace phi