gru_cpu_kernel.h 26.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
G
guosheng 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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
#include <type_traits>
Y
Yi Wang 已提交
17 18
#include "paddle/fluid/operators/math/detail/activation_functions.h"
#include "paddle/fluid/operators/math/gru_compute.h"
G
guosheng 已提交
19 20 21 22 23 24 25 26 27

namespace paddle {
namespace operators {
namespace math {
namespace detail {

#ifndef __NVCC__

template <class OpResetOutput, typename T>
28 29 30 31
void hl_naive_gru_forward_reset_output(
    OpResetOutput op_reset_output, T *gate_value, T *reset_output_value,
    const T *prev_output_value, int frame_size, ActivationType active_gate,
    bool old_version = true, const T *reset_bias = nullptr) {
G
guosheng 已提交
32 33 34 35
  T r_value_update_gate;
  T r_value_reset_gate;
  T r_value_reset_output;
  T r_prev_out = 0;
36 37 38 39 40 41 42 43 44 45
  T r_reset_bias = 0;
  T *update_gate = nullptr;
  T *reset_gate = nullptr;
  if (old_version) {
    update_gate = gate_value;
    reset_gate = gate_value + frame_size;
  } else {
    reset_gate = gate_value;
    update_gate = gate_value + frame_size;
  }
G
guosheng 已提交
46 47 48
  for (int i = 0; i < frame_size; i++) {
    r_value_update_gate = update_gate[i];
    r_value_reset_gate = reset_gate[i];
49 50 51 52
    if (!old_version) {
      r_value_reset_output = reset_output_value[i];
      r_reset_bias = reset_bias[i];
    }
G
guosheng 已提交
53 54
    if (prev_output_value) {
      r_prev_out = prev_output_value[i];
G
guosheng 已提交
55 56
    }

57
    op_reset_output(&r_value_update_gate, &r_value_reset_gate, &r_prev_out,
58 59
                    &r_value_reset_output, active_gate, &r_reset_bias,
                    old_version);
G
guosheng 已提交
60

G
guosheng 已提交
61 62 63
    update_gate[i] = r_value_update_gate;
    reset_gate[i] = r_value_reset_gate;
    reset_output_value[i] = r_value_reset_output;
G
guosheng 已提交
64 65 66 67
  }
}

template <class OpFinalOutput, typename T>
68 69 70 71
void hl_naive_gru_forward_final_output(
    OpFinalOutput op_final_output, T *gate_value, const T *prev_output_value,
    T *output_value, int frame_size, ActivationType active_node,
    bool origin_mode, bool old_version = true) {
G
guosheng 已提交
72 73 74 75
  T r_value_update_gate;
  T r_value_frame_state;
  T r_prev_out = 0;
  T r_output;
76 77 78 79 80 81
  T *update_gate;
  if (old_version) {
    update_gate = gate_value;
  } else {
    update_gate = gate_value + frame_size;
  }
G
guosheng 已提交
82 83 84 85 86 87 88
  T *frame_state = gate_value + frame_size * 2;

  for (int i = 0; i < frame_size; i++) {
    r_value_update_gate = update_gate[i];
    r_value_frame_state = frame_state[i];
    if (prev_output_value) {
      r_prev_out = prev_output_value[i];
G
guosheng 已提交
89 90
    }

91
    op_final_output(&r_value_update_gate, &r_value_frame_state, &r_prev_out,
Q
Qiao Longfei 已提交
92
                    &r_output, active_node, origin_mode);
G
guosheng 已提交
93

G
guosheng 已提交
94 95
    frame_state[i] = r_value_frame_state;
    output_value[i] = r_output;
G
guosheng 已提交
96 97 98 99
  }
}

template <class OpResetOutput, typename T>
G
guosheng 已提交
100 101
void hl_avx_gru_forward_reset_output(OpResetOutput op_reset_output,
                                     T *gate_value, T *reset_output_value,
102 103 104 105
                                     const T *prev_output_value, int frame_size,
                                     ActivationType active_gate,
                                     bool old_version = true,
                                     const T *reset_bias = nullptr) {
G
guosheng 已提交
106
#ifdef __AVX__
107 108
  __m256 r_value_update_gate, r_value_update_gate_last = _mm256_set1_ps(0.0f);
  __m256 r_value_reset_gate, r_value_reset_gate_last = _mm256_set1_ps(0.0f);
G
guosheng 已提交
109
  __m256 r_value_reset_output;
110 111
  __m256 r_prev_out = _mm256_set1_ps(0.0f),
         r_prev_out_last = _mm256_set1_ps(0.0f);
112 113 114 115 116 117 118 119 120 121
  __m256 r_reset_bias = _mm256_set1_ps(0.0f);
  T *update_gate;
  T *reset_gate;
  if (old_version) {
    update_gate = gate_value;
    reset_gate = gate_value + frame_size;
  } else {
    reset_gate = gate_value;
    update_gate = gate_value + frame_size;
  }
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  int block = 8;
  const int n = frame_size;
  const int rest = n % block;
  const int end = n - rest;
  int i = 0;

  if (rest > 0) {
    i = n - block;
    r_value_update_gate_last =
        _mm256_loadu_ps((const float *)(update_gate + i));
    r_value_reset_gate_last = _mm256_loadu_ps((const float *)(reset_gate + i));
    if (prev_output_value) {
      r_prev_out_last = _mm256_loadu_ps((const float *)(prev_output_value + i));
    }
  }
G
guosheng 已提交
137

138 139 140
  for (i = 0; i < end; i += block) {
    r_value_update_gate = _mm256_loadu_ps((const float *)(update_gate + i));
    r_value_reset_gate = _mm256_loadu_ps((const float *)(reset_gate + i));
G
guosheng 已提交
141
    if (prev_output_value) {
142
      r_prev_out = _mm256_loadu_ps((const float *)(prev_output_value + i));
G
guosheng 已提交
143
    }
144 145 146 147 148
    if (!old_version) {
      r_reset_bias = _mm256_loadu_ps((const float *)(reset_bias + i));
      r_value_reset_output =
          _mm256_loadu_ps((const float *)(reset_output_value + i));
    }
G
guosheng 已提交
149

150
    op_reset_output(&r_value_update_gate, &r_value_reset_gate, &r_prev_out,
151 152
                    &r_value_reset_output, active_gate, &r_reset_bias,
                    old_version);
G
guosheng 已提交
153

154 155 156 157 158 159 160 161 162 163 164 165
    _mm256_storeu_ps(reinterpret_cast<float *>(update_gate + i),
                     r_value_update_gate);
    _mm256_storeu_ps(reinterpret_cast<float *>(reset_gate + i),
                     r_value_reset_gate);
    _mm256_storeu_ps(reinterpret_cast<float *>(reset_output_value + i),
                     r_value_reset_output);
  }

  if (rest > 0) {
    i = n - block;

    op_reset_output(&r_value_update_gate_last, &r_value_reset_gate_last,
166 167
                    &r_prev_out_last, &r_value_reset_output, active_gate,
                    &r_reset_bias, old_version);
168 169 170 171 172 173 174

    _mm256_storeu_ps(reinterpret_cast<float *>(update_gate + i),
                     r_value_update_gate_last);
    _mm256_storeu_ps(reinterpret_cast<float *>(reset_gate + i),
                     r_value_reset_gate_last);
    _mm256_storeu_ps(reinterpret_cast<float *>(reset_output_value + i),
                     r_value_reset_output);
G
guosheng 已提交
175 176 177 178 179
  }
#endif
}

template <class OpFinalOutput, typename T>
G
guosheng 已提交
180
void hl_avx_gru_forward_final_output(OpFinalOutput op_final_output,
181
                                     T *gate_value, const T *prev_output_value,
G
guosheng 已提交
182
                                     T *output_value, int frame_size,
Q
Qiao Longfei 已提交
183
                                     ActivationType active_node,
184 185
                                     bool origin_mode,
                                     bool old_version = true) {
G
guosheng 已提交
186
#ifdef __AVX__
187 188 189 190
  __m256 r_value_update_gate, r_value_update_gate_last = _mm256_set1_ps(0.0f);
  __m256 r_value_frame_state, r_value_frame_state_last = _mm256_set1_ps(0.0f);
  __m256 r_prev_out = _mm256_set1_ps(0.0f),
         r_prev_out_last = _mm256_set1_ps(0.0f);
G
guosheng 已提交
191
  __m256 r_output;
192 193 194 195 196 197 198
  T *update_gate;
  if (old_version) {
    update_gate = gate_value;
  } else {
    update_gate = gate_value + frame_size;
  }

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  T *frame_state = gate_value + frame_size * 2;
  int block = 8;
  const int n = frame_size;
  const int rest = n % block;
  const int end = n - rest;
  int i = 0;

  if (rest > 0) {
    i = n - block;
    r_value_update_gate_last =
        _mm256_loadu_ps((const float *)(update_gate + i));
    r_value_frame_state_last =
        _mm256_loadu_ps((const float *)(frame_state + i));
    if (prev_output_value) {
      r_prev_out_last = _mm256_loadu_ps((const float *)(prev_output_value + i));
    }
  }
G
guosheng 已提交
216

217 218 219
  for (i = 0; i < end; i += block) {
    r_value_update_gate = _mm256_loadu_ps((const float *)(update_gate + i));
    r_value_frame_state = _mm256_loadu_ps((const float *)(frame_state + i));
G
guosheng 已提交
220
    if (prev_output_value) {
221
      r_prev_out = _mm256_loadu_ps((const float *)(prev_output_value + i));
G
guosheng 已提交
222 223
    }

224
    op_final_output(&r_value_update_gate, &r_value_frame_state, &r_prev_out,
Q
Qiao Longfei 已提交
225
                    &r_output, active_node, origin_mode);
G
guosheng 已提交
226

227 228 229 230 231 232 233 234
    _mm256_storeu_ps(reinterpret_cast<float *>(frame_state + i),
                     r_value_frame_state);
    _mm256_storeu_ps(reinterpret_cast<float *>(output_value + i), r_output);
  }

  if (rest > 0) {
    i = n - block;
    op_final_output(&r_value_update_gate_last, &r_value_frame_state_last,
Q
Qiao Longfei 已提交
235
                    &r_prev_out_last, &r_output, active_node, origin_mode);
236 237 238 239

    _mm256_storeu_ps(reinterpret_cast<float *>(frame_state + i),
                     r_value_frame_state_last);
    _mm256_storeu_ps(reinterpret_cast<float *>(output_value + i), r_output);
G
guosheng 已提交
240
  }
241

G
guosheng 已提交
242 243 244 245
#endif
}

template <class OpResetOutput, typename T>
G
guosheng 已提交
246
inline void forward_reset_output(OpResetOutput op_reset_output,
247
                                 GRUMetaValue<T> value, int frame_size,
248 249
                                 int batch_size, ActivationType active_gate,
                                 bool old_version = true) {
G
guosheng 已提交
250
  for (int b = 0; b < batch_size; b++) {
251 252
    if (OpResetOutput::avx && (frame_size > static_cast<int>(8 - 1)) &&
        (sizeof(T) == 4)) {
G
guosheng 已提交
253
      hl_avx_gru_forward_reset_output(
G
guosheng 已提交
254
          op_reset_output, value.gate_value, value.reset_output_value,
255 256
          value.prev_out_value, frame_size, active_gate, old_version,
          value.reset_bias);
G
guosheng 已提交
257 258
    } else {
      hl_naive_gru_forward_reset_output(
G
guosheng 已提交
259
          op_reset_output, value.gate_value, value.reset_output_value,
260 261
          value.prev_out_value, frame_size, active_gate, old_version,
          value.reset_bias);
G
guosheng 已提交
262
    }
G
guosheng 已提交
263 264 265 266
    value.gate_value += frame_size * 3;
    value.reset_output_value += frame_size;
    if (value.prev_out_value) {
      value.prev_out_value += frame_size;
G
guosheng 已提交
267 268 269 270 271
    }
  }
}

template <class OpFinalOutput, typename T>
G
guosheng 已提交
272
inline void forward_final_output(OpFinalOutput op_final_output,
273
                                 GRUMetaValue<T> value, int frame_size,
Q
Qiao Longfei 已提交
274
                                 int batch_size, ActivationType active_node,
275
                                 bool origin_mode, bool old_version = true) {
G
guosheng 已提交
276
  for (int b = 0; b < batch_size; b++) {
277 278
    if (OpFinalOutput::avx && (frame_size > static_cast<int>(8 - 1)) &&
        (sizeof(T) == 4)) {
G
guosheng 已提交
279 280
      hl_avx_gru_forward_final_output(op_final_output, value.gate_value,
                                      value.prev_out_value, value.output_value,
281 282
                                      frame_size, active_node, origin_mode,
                                      old_version);
G
guosheng 已提交
283
    } else {
284 285 286 287
      hl_naive_gru_forward_final_output(op_final_output, value.gate_value,
                                        value.prev_out_value,
                                        value.output_value, frame_size,
                                        active_node, origin_mode, old_version);
G
guosheng 已提交
288 289
    }

G
guosheng 已提交
290 291 292 293
    value.gate_value += frame_size * 3;
    value.output_value += frame_size;
    if (value.prev_out_value) {
      value.prev_out_value += frame_size;
G
guosheng 已提交
294 295 296 297 298
    }
  }
}

template <class OpStateGrad, typename T>
G
guosheng 已提交
299
void hl_naive_gru_backward_state_grad(OpStateGrad op_state_grad, T *gate_value,
300
                                      T *gate_grad, const T *prev_out_value,
G
guosheng 已提交
301 302
                                      T *prev_out_grad, T *output_grad,
                                      int frame_size,
Q
Qiao Longfei 已提交
303 304
                                      ActivationType active_node,
                                      bool origin_mode) {
G
guosheng 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
  T r_update_gate_value;
  T r_update_gate_grad;
  T r_frame_state_value;
  T r_frame_state_grad;
  T r_out_grad;
  T r_prev_out_value = 0;
  T r_prev_out_grad = 0;
  T *update_gate_value = gate_value;
  T *update_gate_grad = gate_grad;
  T *frame_state_value = gate_value + frame_size * 2;
  T *frame_state_grad = gate_grad + frame_size * 2;

  for (int i = 0; i < frame_size; i++) {
    r_update_gate_value = update_gate_value[i];
    r_frame_state_value = frame_state_value[i];
    r_out_grad = output_grad[i];
    if (prev_out_value) {
      r_prev_out_value = prev_out_value[i];
G
guosheng 已提交
323
    }
G
guosheng 已提交
324 325
    if (prev_out_grad) {
      r_prev_out_grad = prev_out_grad[i];
G
guosheng 已提交
326 327
    }

328 329
    op_state_grad(&r_update_gate_value, &r_update_gate_grad,
                  &r_frame_state_value, &r_frame_state_grad, &r_prev_out_value,
Q
Qiao Longfei 已提交
330
                  &r_prev_out_grad, &r_out_grad, active_node, origin_mode);
G
guosheng 已提交
331

G
guosheng 已提交
332 333 334 335
    update_gate_grad[i] = r_update_gate_grad;
    frame_state_grad[i] = r_frame_state_grad;
    if (prev_out_grad) {
      prev_out_grad[i] = r_prev_out_grad;
G
guosheng 已提交
336 337 338 339 340
    }
  }
}

template <class OpResetGrad, typename T>
G
guosheng 已提交
341
void hl_naive_gru_backward_reset_grad(OpResetGrad op_reset_grad, T *gate_value,
342
                                      T *gate_grad, const T *prev_out_value,
G
guosheng 已提交
343 344
                                      T *prev_out_grad, T *reset_output_grad,
                                      int frame_size,
Q
Qiao Longfei 已提交
345
                                      ActivationType active_gate) {
G
guosheng 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  T r_update_gate_value;
  T r_update_gate_grad;
  T r_reset_gate_value;
  T r_reset_gate_grad;
  T r_reset_output_grad = 0;
  T r_prev_out_value = 0;
  T r_prev_out_grad = 0;
  T *update_gate_value = gate_value;
  T *update_gate_grad = gate_grad;
  T *reset_gate_value = gate_value + frame_size;
  T *reset_gate_grad = gate_grad + frame_size;

  for (int i = 0; i < frame_size; i++) {
    r_update_gate_value = update_gate_value[i];
    r_update_gate_grad = update_gate_grad[i];
    r_reset_gate_value = reset_gate_value[i];

    if (prev_out_value && prev_out_grad) {
      r_reset_output_grad = reset_output_grad[i];
G
guosheng 已提交
365
    }
G
guosheng 已提交
366 367
    if (prev_out_value) {
      r_prev_out_value = prev_out_value[i];
G
guosheng 已提交
368
    }
G
guosheng 已提交
369 370
    if (prev_out_grad) {
      r_prev_out_grad = prev_out_grad[i];
G
guosheng 已提交
371 372
    }

373 374
    op_reset_grad(&r_update_gate_value, &r_update_gate_grad,
                  &r_reset_gate_value, &r_reset_gate_grad, &r_prev_out_value,
Q
Qiao Longfei 已提交
375
                  &r_prev_out_grad, &r_reset_output_grad, active_gate);
G
guosheng 已提交
376

G
guosheng 已提交
377 378 379 380
    update_gate_grad[i] = r_update_gate_grad;
    reset_gate_grad[i] = r_reset_gate_grad;
    if (prev_out_grad) {
      prev_out_grad[i] = r_prev_out_grad;
G
guosheng 已提交
381 382 383 384 385
    }
  }
}

template <class OpStateGrad, typename T>
G
guosheng 已提交
386
void hl_avx_gru_backward_state_grad(OpStateGrad op_state_grad, T *gate_value,
387
                                    T *gate_grad, const T *prev_out_value,
G
guosheng 已提交
388
                                    T *prev_out_grad, T *output_grad,
Q
Qiao Longfei 已提交
389 390
                                    int frame_size, ActivationType active_node,
                                    bool origin_mode) {
G
guosheng 已提交
391
#ifdef __AVX__
G
guosheng 已提交
392 393 394 395 396 397 398
  __m256 r_update_gate_value;
  __m256 r_update_gate_grad;
  __m256 r_frame_state_value;
  __m256 r_frame_state_grad;
  __m256 r_out_grad;
  __m256 r_prev_out_value = _mm256_set1_ps(0.0f);
  __m256 r_prev_out_grad = _mm256_set1_ps(0.0f);
399 400 401 402 403 404
  __m256 *update_gate_value = reinterpret_cast<__m256 *>(gate_value);
  __m256 *update_gate_grad = reinterpret_cast<__m256 *>(gate_grad);
  __m256 *frame_state_value =
      reinterpret_cast<__m256 *>(gate_value + frame_size * 2);
  __m256 *frame_state_grad =
      reinterpret_cast<__m256 *>(gate_grad + frame_size * 2);
G
guosheng 已提交
405 406 407 408

  for (int i = 0; i < frame_size / 8; i++) {
    r_update_gate_value = update_gate_value[i];
    r_frame_state_value = frame_state_value[i];
409
    r_out_grad = (reinterpret_cast<__m256 *>(output_grad))[i];
G
guosheng 已提交
410
    if (prev_out_value) {
411
      r_prev_out_value = (reinterpret_cast<const __m256 *>(prev_out_value))[i];
G
guosheng 已提交
412
    }
G
guosheng 已提交
413
    if (prev_out_grad) {
414
      r_prev_out_grad = (reinterpret_cast<__m256 *>(prev_out_grad))[i];
G
guosheng 已提交
415 416
    }

417 418
    op_state_grad(&r_update_gate_value, &r_update_gate_grad,
                  &r_frame_state_value, &r_frame_state_grad, &r_prev_out_value,
Q
Qiao Longfei 已提交
419
                  &r_prev_out_grad, &r_out_grad, active_node, origin_mode);
G
guosheng 已提交
420

G
guosheng 已提交
421 422 423
    update_gate_grad[i] = r_update_gate_grad;
    frame_state_grad[i] = r_frame_state_grad;
    if (prev_out_grad) {
424
      (reinterpret_cast<__m256 *>(prev_out_grad))[i] = r_prev_out_grad;
G
guosheng 已提交
425 426 427 428 429 430
    }
  }
#endif
}

template <class OpResetGrad, typename T>
G
guosheng 已提交
431
void hl_avx_gru_backward_reset_grad(OpResetGrad op_reset_grad, T *gate_value,
432
                                    T *gate_grad, const T *prev_out_value,
G
guosheng 已提交
433
                                    T *prev_out_grad, T *reset_output_grad,
Q
Qiao Longfei 已提交
434 435
                                    int frame_size,
                                    ActivationType active_gate) {
G
guosheng 已提交
436
#ifdef __AVX__
G
guosheng 已提交
437 438 439 440 441 442 443
  __m256 r_update_gate_value;
  __m256 r_update_gate_grad;
  __m256 r_reset_gate_value;
  __m256 r_reset_gate_grad;
  __m256 r_reset_output_grad = _mm256_set1_ps(0.0f);
  __m256 r_prev_out_value = _mm256_set1_ps(0.0f);
  __m256 r_prev_out_grad = _mm256_set1_ps(0.0f);
444 445 446 447 448
  __m256 *update_gate_value = reinterpret_cast<__m256 *>(gate_value);
  __m256 *update_gate_grad = reinterpret_cast<__m256 *>(gate_grad);
  __m256 *reset_gate_value =
      reinterpret_cast<__m256 *>(gate_value + frame_size);
  __m256 *reset_gate_grad = reinterpret_cast<__m256 *>(gate_grad + frame_size);
G
guosheng 已提交
449 450 451 452 453 454 455

  for (int i = 0; i < frame_size / 8; i++) {
    r_update_gate_value = update_gate_value[i];
    r_update_gate_grad = update_gate_grad[i];
    r_reset_gate_value = reset_gate_value[i];

    if (prev_out_value && prev_out_grad) {
456
      r_reset_output_grad = (reinterpret_cast<__m256 *>(reset_output_grad))[i];
G
guosheng 已提交
457
    }
G
guosheng 已提交
458
    if (prev_out_value) {
459
      r_prev_out_value = (reinterpret_cast<const __m256 *>(prev_out_value))[i];
G
guosheng 已提交
460
    }
G
guosheng 已提交
461
    if (prev_out_grad) {
462
      r_prev_out_grad = (reinterpret_cast<__m256 *>(prev_out_grad))[i];
G
guosheng 已提交
463 464
    }

465 466
    op_reset_grad(&r_update_gate_value, &r_update_gate_grad,
                  &r_reset_gate_value, &r_reset_gate_grad, &r_prev_out_value,
Q
Qiao Longfei 已提交
467
                  &r_prev_out_grad, &r_reset_output_grad, active_gate);
G
guosheng 已提交
468

G
guosheng 已提交
469 470 471
    update_gate_grad[i] = r_update_gate_grad;
    reset_gate_grad[i] = r_reset_gate_grad;
    if (prev_out_grad) {
472
      (reinterpret_cast<__m256 *>(prev_out_grad))[i] = r_prev_out_grad;
G
guosheng 已提交
473 474 475 476 477
    }
  }
#endif
}

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
template <class OpGruGrad, typename T>
inline void hl_naive_gru_backward(OpGruGrad op_gru_grad, T *gate_value,
                                  T *gate_grad, const T *prev_out_value,
                                  T *prev_out_grad, T *reset_output_value,
                                  T *reset_output_grad, T *output_grad,
                                  int frame_size, ActivationType active_node,
                                  ActivationType active_gate) {
  T r_value_reset_gate;
  T r_grad_reset_gate;
  T r_value_update_gate;
  T r_grad_update_gate;
  T r_value_frame_state;
  T r_grad_frame_state;
  T r_value_prev_out = 0;
  T r_grad_prev_out = 0;
  T r_grad_output;
  T r_value_reset_output;
  T r_grad_reset_output = 0;
  T *reset_gate_value = gate_value;
  T *reset_gate_grad = gate_grad;
  T *update_gate_value = gate_value + frame_size;
  T *update_gate_grad = gate_grad + frame_size;
  T *frame_state_value = gate_value + 2 * frame_size;
  T *frame_state_grad = gate_grad + 2 * frame_size;

  for (int i = 0; i < frame_size; ++i) {
    r_value_reset_gate = reset_gate_value[i];
    r_grad_reset_gate = reset_gate_grad[i];
    r_value_update_gate = update_gate_value[i];
    r_grad_update_gate = update_gate_grad[i];
    r_value_frame_state = frame_state_value[i];
    r_grad_frame_state = frame_state_grad[i];
    if (prev_out_value) {
      r_value_prev_out = prev_out_value[i];
    }
    if (prev_out_grad) {
      r_grad_prev_out = prev_out_grad[i];
    }
    r_grad_output = output_grad[i];
    r_value_reset_output = reset_output_value[i];
    if (prev_out_value && prev_out_grad) {
      r_grad_reset_output = reset_output_grad[i];
    }

    op_gru_grad(&r_value_reset_gate, &r_grad_reset_gate, &r_value_update_gate,
                &r_grad_update_gate, &r_value_frame_state, &r_grad_frame_state,
                &r_value_prev_out, &r_grad_prev_out, &r_grad_output,
                &r_value_reset_output, &r_grad_reset_output, active_node,
                active_gate);

    reset_gate_grad[i] = r_grad_reset_gate;
    update_gate_grad[i] = r_grad_update_gate;
    frame_state_grad[i] = r_grad_frame_state;
    if (prev_out_grad) {
      prev_out_grad[i] = r_grad_prev_out;
    }
    if (prev_out_value && prev_out_grad) {
      reset_output_grad[i] = r_grad_reset_output;
    }
  }
}

template <class OpGruGrad, typename T>
inline void hl_avx_gru_backward(OpGruGrad op_gru_grad, T *gate_value,
                                T *gate_grad, const T *prev_out_value,
                                T *prev_out_grad, T *reset_output_value,
                                T *reset_output_grad, T *output_grad,
                                int frame_size, ActivationType active_node,
                                ActivationType active_gate) {
#ifdef __AVX__
  __m256 r_value_reset_gate;
  __m256 r_grad_reset_gate;
  __m256 r_value_update_gate;
  __m256 r_grad_update_gate;
  __m256 r_value_frame_state;
  __m256 r_grad_frame_state;
  __m256 r_value_prev_out = _mm256_set1_ps(0.0f);
  __m256 r_grad_prev_out = _mm256_set1_ps(0.0f);
  __m256 r_grad_output;
  __m256 r_value_reset_output;
  __m256 r_grad_reset_output = _mm256_set1_ps(0.0f);
  __m256 *reset_gate_value = reinterpret_cast<__m256 *>(gate_value);
  __m256 *reset_gate_grad = reinterpret_cast<__m256 *>(gate_grad);
  __m256 *update_gate_value =
      reinterpret_cast<__m256 *>(gate_value + frame_size);
  __m256 *update_gate_grad = reinterpret_cast<__m256 *>(gate_grad + frame_size);
  __m256 *frame_state_value =
      reinterpret_cast<__m256 *>(gate_value + 2 * frame_size);
  __m256 *frame_state_grad =
      reinterpret_cast<__m256 *>(gate_grad + 2 * frame_size);

  for (int i = 0; i < frame_size / 8; ++i) {
    r_value_reset_gate = reset_gate_value[i];
    r_grad_reset_gate = reset_gate_grad[i];
    r_value_update_gate = update_gate_value[i];
    r_grad_update_gate = update_gate_grad[i];
    r_value_frame_state = frame_state_value[i];
    r_grad_frame_state = frame_state_grad[i];
    if (prev_out_value) {
      r_value_prev_out = (reinterpret_cast<const __m256 *>(prev_out_value))[i];
    }
    if (prev_out_grad) {
      r_grad_prev_out = (reinterpret_cast<__m256 *>(prev_out_grad))[i];
    }
    r_grad_output = (reinterpret_cast<__m256 *>(output_grad))[i];
    r_value_reset_output = (reinterpret_cast<__m256 *>(reset_output_value))[i];
    if (prev_out_value && prev_out_grad) {
      r_grad_reset_output = (reinterpret_cast<__m256 *>(reset_output_grad))[i];
    }

    op_gru_grad(&r_value_reset_gate, &r_grad_reset_gate, &r_value_update_gate,
                &r_grad_update_gate, &r_value_frame_state, &r_grad_frame_state,
                &r_value_prev_out, &r_grad_prev_out, &r_grad_output,
                &r_value_reset_output, &r_grad_reset_output, active_node,
                active_gate);

    reset_gate_grad[i] = r_grad_reset_gate;
    update_gate_grad[i] = r_grad_update_gate;
    frame_state_grad[i] = r_grad_frame_state;
    if (prev_out_grad) {
      (reinterpret_cast<__m256 *>(prev_out_grad))[i] = r_grad_prev_out;
    }
    if (prev_out_value && prev_out_grad) {
      (reinterpret_cast<__m256 *>(reset_output_grad))[i] = r_grad_reset_output;
    }
  }
#endif
}

G
guosheng 已提交
607
template <class OpStateGrad, typename T>
G
guosheng 已提交
608
inline void backward_state_grad(OpStateGrad op_state_grad,
609
                                GRUMetaValue<T> value, GRUMetaGrad<T> grad,
G
guosheng 已提交
610
                                int frame_size, int batch_size,
Q
Qiao Longfei 已提交
611
                                ActivationType active_node, bool origin_mode) {
G
guosheng 已提交
612 613
  for (int b = 0; b < batch_size; b++) {
    if (OpStateGrad::avx && !(frame_size & (8 - 1)) && (sizeof(T) == 4)) {
Q
Qiao Longfei 已提交
614 615 616 617
      hl_avx_gru_backward_state_grad(op_state_grad, value.gate_value,
                                     grad.gate_grad, value.prev_out_value,
                                     grad.prev_out_grad, grad.output_grad,
                                     frame_size, active_node, origin_mode);
G
guosheng 已提交
618
    } else {
Q
Qiao Longfei 已提交
619 620 621 622
      hl_naive_gru_backward_state_grad(op_state_grad, value.gate_value,
                                       grad.gate_grad, value.prev_out_value,
                                       grad.prev_out_grad, grad.output_grad,
                                       frame_size, active_node, origin_mode);
G
guosheng 已提交
623 624
    }

G
guosheng 已提交
625 626 627
    value.gate_value += frame_size * 3;
    if (value.prev_out_value) {
      value.prev_out_value += frame_size;
G
guosheng 已提交
628 629
    }

G
guosheng 已提交
630 631 632 633
    grad.gate_grad += frame_size * 3;
    grad.output_grad += frame_size;
    if (grad.prev_out_grad) {
      grad.prev_out_grad += frame_size;
G
guosheng 已提交
634 635 636 637 638
    }
  }
}

template <class OpResetGrad, typename T>
G
guosheng 已提交
639
inline void backward_reset_grad(OpResetGrad op_reset_grad,
640
                                GRUMetaValue<T> value, GRUMetaGrad<T> grad,
G
guosheng 已提交
641
                                int frame_size, int batch_size,
Q
Qiao Longfei 已提交
642
                                ActivationType active_gate) {
G
guosheng 已提交
643 644
  for (int b = 0; b < batch_size; b++) {
    if (OpResetGrad::avx && !(frame_size & (8 - 1)) && (sizeof(T) == 4)) {
Q
Qiao Longfei 已提交
645 646 647
      hl_avx_gru_backward_reset_grad(
          op_reset_grad, value.gate_value, grad.gate_grad, value.prev_out_value,
          grad.prev_out_grad, grad.reset_output_grad, frame_size, active_gate);
G
guosheng 已提交
648 649
    } else {
      hl_naive_gru_backward_reset_grad(
G
guosheng 已提交
650
          op_reset_grad, value.gate_value, grad.gate_grad, value.prev_out_value,
Q
Qiao Longfei 已提交
651
          grad.prev_out_grad, grad.reset_output_grad, frame_size, active_gate);
G
guosheng 已提交
652 653
    }

G
guosheng 已提交
654 655 656
    value.gate_value += frame_size * 3;
    if (value.prev_out_value) {
      value.prev_out_value += frame_size;
G
guosheng 已提交
657 658
    }

G
guosheng 已提交
659 660 661 662
    grad.gate_grad += frame_size * 3;
    grad.reset_output_grad += frame_size;
    if (grad.prev_out_grad) {
      grad.prev_out_grad += frame_size;
G
guosheng 已提交
663 664 665 666
    }
  }
}

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
template <class OpGruGrad, typename T>
inline void cpu_gru_backward(OpGruGrad op_gru_grad, GRUMetaValue<T> value,
                             GRUMetaGrad<T> grad, int frame_size,
                             int batch_size, ActivationType active_node,
                             ActivationType active_gate) {
  for (int b = 0; b < batch_size; ++b) {
    if (OpGruGrad::avx && !(frame_size & (8 - 1)) && (sizeof(T) == 4)) {
      hl_avx_gru_backward(
          op_gru_grad, value.gate_value, grad.gate_grad, value.prev_out_value,
          grad.prev_out_grad, value.reset_output_value, grad.reset_output_grad,
          grad.output_grad, frame_size, active_node, active_gate);
    } else {
      hl_naive_gru_backward(
          op_gru_grad, value.gate_value, grad.gate_grad, value.prev_out_value,
          grad.prev_out_grad, value.reset_output_value, grad.reset_output_grad,
          grad.output_grad, frame_size, active_node, active_gate);
    }

    value.gate_value += frame_size * 3;
    value.reset_output_value += frame_size;
    if (value.prev_out_value) {
      value.prev_out_value += frame_size;
    }

    grad.gate_grad += frame_size * 3;
    grad.output_grad += frame_size;
    grad.reset_output_grad += frame_size;
    if (grad.prev_out_grad) {
      grad.prev_out_grad += frame_size;
    }
  }
}

G
guosheng 已提交
700 701 702 703 704 705
#endif

}  // namespace detail
}  // namespace math
}  // namespace operators
}  // namespace paddle