lstm_gpu_kernel.h 9.2 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 17
#include <type_traits>

Y
Yi Wang 已提交
18 19
#include "paddle/fluid/operators/math/detail/activation_functions.h"
#include "paddle/fluid/operators/math/lstm_compute.h"
D
dzhwinter 已提交
20
#include "paddle/fluid/platform/cuda_primitives.h"
Y
Yi Wang 已提交
21
#include "paddle/fluid/platform/device_context.h"
D
dangqingqing 已提交
22 23 24 25 26 27 28

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

/*
29 30
 * threads(frame_per_block, batch_per_block)
 * grid(frame_blocks, batch_blocks)
D
dangqingqing 已提交
31
 */
32 33
template <class T, class Op, bool is_batch>
__global__ void KeLstmForward(Op op, LstmMetaValue<T> value, int frame_size,
34 35
                              int batch_size, T cell_clip,
                              ActivationType active_node,
36 37
                              ActivationType active_gate,
                              ActivationType active_state) {
38 39 40 41 42 43 44 45 46 47 48
  const int frame_idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (frame_idx >= frame_size) return;

  int batch_idx = 0;
  if (is_batch) {
    batch_idx = blockIdx.y * blockDim.y + threadIdx.y;
    if (batch_idx >= batch_size) return;
    value.gate_value += batch_idx * frame_size * 4;
    value.output_value += batch_idx * frame_size;
    value.state_value += batch_idx * frame_size;
    value.state_active_value += batch_idx * frame_size;
D
dangqingqing 已提交
49 50
  }

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  T r_state;
  T r_prev_state = 0;
  T r_state_atv;
  T r_out;
  T r_value_in;
  T r_value_ig;
  T r_value_fg;
  T r_value_og;

  T r_checkI = value.check_ig ? value.check_ig[frame_idx] : 0;
  T r_checkF = value.check_fg ? value.check_fg[frame_idx] : 0;
  T r_checkO = value.check_og ? value.check_og[frame_idx] : 0;

  r_value_in = value.gate_value[frame_idx];
  r_value_ig = value.gate_value[frame_idx + frame_size];
  r_value_fg = value.gate_value[frame_idx + frame_size * 2];
  r_value_og = value.gate_value[frame_idx + frame_size * 3];

  if (value.prev_state_value) {
    if (is_batch) value.prev_state_value += batch_idx * frame_size;
    r_prev_state = value.prev_state_value[frame_idx];
D
dangqingqing 已提交
72 73
  }

74 75
  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,
76
     &cell_clip, active_node, active_gate, active_state);
D
dangqingqing 已提交
77

78 79 80 81
  value.gate_value[frame_idx] = r_value_in;
  value.gate_value[frame_idx + frame_size] = r_value_ig;
  value.gate_value[frame_idx + frame_size * 2] = r_value_fg;
  value.gate_value[frame_idx + frame_size * 3] = r_value_og;
D
dangqingqing 已提交
82

83 84 85
  value.state_value[frame_idx] = r_state;
  value.state_active_value[frame_idx] = r_state_atv;
  value.output_value[frame_idx] = r_out;
D
dangqingqing 已提交
86 87 88
}

/*
89 90
 * threads(frame_per_block, batch_per_block)
 * grid(frame_blocks, batch_blocks)
D
dangqingqing 已提交
91
 */
92
template <class T, class Op, bool is_batch>
93
__global__ void KeLstmBackward(Op op, LstmMetaValue<T> value,
94
                               LstmMetaGrad<T> grad, int frame_size,
95 96
                               int batch_size, T cell_clip,
                               ActivationType active_node,
97 98
                               ActivationType active_gate,
                               ActivationType active_state) {
99 100 101 102 103 104 105 106 107 108 109 110 111
  const int frame_idx = blockIdx.x * blockDim.x + threadIdx.x;
  if (frame_idx >= frame_size) return;

  int batch_idx = 0;
  if (is_batch) {
    batch_idx = blockIdx.y * blockDim.y + threadIdx.y;
    if (batch_idx >= batch_size) return;
    value.gate_value += batch_idx * frame_size * 4;
    value.state_value += batch_idx * frame_size;
    value.state_active_value += batch_idx * frame_size;
    grad.gate_grad += batch_idx * frame_size * 4;
    grad.state_grad += batch_idx * frame_size;
    grad.output_grad += batch_idx * frame_size;
D
dangqingqing 已提交
112 113
  }

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
  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 = value.check_ig ? value.check_ig[frame_idx] : 0;
  T r_checkF = value.check_fg ? value.check_fg[frame_idx] : 0;
  T r_checkO = value.check_og ? value.check_og[frame_idx] : 0;

  T r_checkIGrad;
  T r_checkFGrad;
  T r_checkOGrad;

  r_value_in = value.gate_value[frame_idx];
  r_value_ig = value.gate_value[frame_idx + frame_size];
  r_value_fg = value.gate_value[frame_idx + frame_size * 2];
  r_value_og = value.gate_value[frame_idx + frame_size * 3];
  r_state = value.state_value[frame_idx];
  r_state_atv = value.state_active_value[frame_idx];
  r_output_grad = grad.output_grad[frame_idx];
  r_state_grad = grad.state_grad[frame_idx];

  if (value.prev_state_value) {
    if (is_batch) value.prev_state_value += batch_idx * frame_size;
    r_prev_state = value.prev_state_value[frame_idx];
D
dangqingqing 已提交
148 149
  }

150 151 152
  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,
153 154
     &r_checkO, &r_checkIGrad, &r_checkFGrad, &r_checkOGrad, &cell_clip,
     active_node, active_gate, active_state);
155 156 157 158 159 160 161 162 163

  grad.gate_grad[frame_idx] = r_grad_in;
  grad.gate_grad[frame_idx + frame_size] = r_grad_ig;
  grad.gate_grad[frame_idx + frame_size * 2] = r_grad_fg;
  grad.gate_grad[frame_idx + frame_size * 3] = r_grad_og;
  grad.state_grad[frame_idx] = r_state_grad;
  if (grad.prev_state_grad) {
    if (is_batch) grad.prev_state_grad += batch_idx * frame_size;
    grad.prev_state_grad[frame_idx] = r_prev_state_grad;
D
dangqingqing 已提交
164 165
  }

166 167 168 169 170 171 172 173
  if (is_batch) {
    if (value.prev_state_value) {
      if (grad.check_ig_grad)
        paddle::platform::CudaAtomicAdd(grad.check_ig_grad + frame_idx,
                                        r_checkIGrad);
      if (grad.check_fg_grad)
        paddle::platform::CudaAtomicAdd(grad.check_fg_grad + frame_idx,
                                        r_checkFGrad);
D
dangqingqing 已提交
174
    }
175 176 177
    if (grad.check_og_grad)
      paddle::platform::CudaAtomicAdd(grad.check_og_grad + frame_idx,
                                      r_checkOGrad);
D
dangqingqing 已提交
178
  } else {
179 180 181
    if (value.prev_state_value) {
      if (grad.check_ig_grad) grad.check_ig_grad[frame_idx] += r_checkIGrad;
      if (grad.check_fg_grad) grad.check_fg_grad[frame_idx] += r_checkFGrad;
D
dangqingqing 已提交
182
    }
183
    if (grad.check_og_grad) grad.check_og_grad[frame_idx] += r_checkOGrad;
D
dangqingqing 已提交
184 185 186 187
  }
}

template <class T, class Op>
188
void gpu_lstm_forward(const platform::DeviceContext& context, Op op,
189
                      LstmMetaValue<T> value, int frame_size, int batch_size,
190 191
                      T cell_clip, ActivationType active_node,
                      ActivationType active_gate, ActivationType active_state) {
D
dangqingqing 已提交
192 193
  dim3 threads;
  dim3 grid;
194 195 196 197 198
  if (batch_size == 1) {
    int frame_per_block = frame_size <= 1024 ? frame_size : 1024;
    int frame_blocks = (frame_size + 1024 - 1) / 1024;
    threads = dim3(frame_per_block, 1);
    grid = dim3(frame_blocks, 1);
D
dangqingqing 已提交
199
  } else {
200 201 202
    /* frame_per_block = 32 batch_per_block = 16 */
    threads = dim3(32, 16);
    grid = dim3((frame_size + 32 - 1) / 32, (batch_size + 16 - 1) / 16);
D
dangqingqing 已提交
203 204
  }

205 206
  auto stream =
      reinterpret_cast<const platform::CUDADeviceContext&>(context).stream();
207
  if (batch_size == 1) {
D
dangqingqing 已提交
208
    KeLstmForward<T, Op,
209
                  /* is_batch= */ false><<<grid, threads, 0, stream>>>(
210
        op, value, frame_size, batch_size, cell_clip, active_node, active_gate,
211
        active_state);
D
dangqingqing 已提交
212 213
  } else {
    KeLstmForward<T, Op,
214
                  /* is_batch= */ true><<<grid, threads, 0, stream>>>(
215
        op, value, frame_size, batch_size, cell_clip, active_node, active_gate,
216
        active_state);
D
dangqingqing 已提交
217 218 219 220
  }
}

template <class T, class Op>
221 222
void gpu_lstm_backward(const platform::DeviceContext& context, Op op,
                       LstmMetaValue<T> value, LstmMetaGrad<T> grad,
223
                       int frame_size, int batch_size, T cell_clip,
D
dangqingqing 已提交
224
                       ActivationType active_node, ActivationType active_gate,
225
                       ActivationType active_state) {
D
dangqingqing 已提交
226 227
  dim3 threads;
  dim3 grid;
228 229 230 231 232
  if (batch_size == 1) {
    int frame_per_block = frame_size <= 1024 ? frame_size : 1024;
    int frame_blocks = (frame_size + 1024 - 1) / 1024;
    threads = dim3(frame_per_block, 1);
    grid = dim3(frame_blocks, 1);
D
dangqingqing 已提交
233
  } else {
234
    /* frame_per_block = 32 batch_per_block = 16 */
235
    threads = dim3(32, 16);
236
    grid = dim3((frame_size + 32 - 1) / 32, (batch_size + 16 - 1) / 16);
D
dangqingqing 已提交
237 238
  }

239 240
  auto stream =
      reinterpret_cast<const platform::CUDADeviceContext&>(context).stream();
241
  if (batch_size == 1) {
D
dangqingqing 已提交
242
    KeLstmBackward<T, Op,
243
                   /* is_batch= */ false><<<grid, threads, 0, stream>>>(
244 245
        op, value, grad, frame_size, batch_size, cell_clip, active_node,
        active_gate, active_state);
D
dangqingqing 已提交
246 247
  } else {
    KeLstmBackward<T, Op,
248
                   /* is_batch= */ true><<<grid, threads, 0, stream>>>(
249 250
        op, value, grad, frame_size, batch_size, cell_clip, active_node,
        active_gate, active_state);
D
dangqingqing 已提交
251 252 253 254 255 256 257
  }
}

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