sequence_expand_op.cu 4.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
W
wanghaoshuang 已提交
2

L
Luo Tao 已提交
3 4 5
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
W
wanghaoshuang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
W
wanghaoshuang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
W
wanghaoshuang 已提交
14 15

#define EIGEN_USE_GPU
Y
Yi Wang 已提交
16
#include "paddle/fluid/operators/sequence_expand_op.h"
W
wanghaoshuang 已提交
17

D
dzhwinter 已提交
18 19 20 21 22 23
namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;

template <typename T>
D
dzhwinter 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
__global__ void sequence_expand_kernel(const T* x_data, T* out_data,
                                       const size_t* lod, size_t lod_size,
                                       size_t element_len) {
  int tid_x = blockIdx.x * blockDim.x + threadIdx.x;
  for (; tid_x < static_cast<int>(lod_size - 1);
       tid_x += blockDim.x * gridDim.x) {
    int scale = lod[tid_x + 1] - lod[tid_x];
    int tid_y = blockIdx.y * blockDim.y + threadIdx.y;
    for (; tid_y < scale; tid_y += blockDim.y * gridDim.y) {
      int tid_z = blockIdx.z * blockDim.z + threadIdx.z;
      int item_start = tid_x / element_len;
      for (; tid_z < element_len; tid_z += blockDim.z * gridDim.z) {
        out_data[item_start * scale + tid_z] = x_data[item_start + tid_z];
      }
    }
D
dzhwinter 已提交
39
  }
D
dzhwinter 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
}

template <typename T>
__global__ void sequence_expand_grad_kernel(const T* dout_data, T* dx_data,
                                            const size_t* lod, size_t lod_size,
                                            size_t element_len,
                                            size_t dout_size) {
  extern __shared__ T shm[];
  int tid_x = blockIdx.x * blockDim.x + threadIdx.x;
  for (; tid_x < static_cast<int>(lod_size - 1);
       tid_x += blockDim.x * gridDim.x) {
    int scale = lod[tid_x + 1] - lod[tid_x];
    int tid_y = blockIdx.y * blockDim.y + threadIdx.y;
    for (; tid_y < scale; tid_y += blockDim.y * gridDim.y) {
      int tid_z = blockIdx.z * blockDim.z + threadIdx.z;
      int item_start = tid_x / element_len;
      for (; tid_z < element_len; tid_z += blockDim.z * gridDim.z) {
D
dzhwinter 已提交
57
        shm[item_start + tid_z] += dout_data[item_start * scale + tid_z];
D
dzhwinter 已提交
58 59 60 61 62
      }
    }
  }
  // synchronize before write to dx
  __syncthreads();
D
dzhwinter 已提交
63
  for (int idx = blockDim.x * blockIdx.x + threadIdx.x;
D
dzhwinter 已提交
64
       idx < static_cast<int>(dout_size); idx += blockDim.x * gridDim.x) {
D
dzhwinter 已提交
65
    dx_data[idx] = shm[idx];
D
dzhwinter 已提交
66 67 68 69
  }
}

template <typename T>
D
dzhwinter 已提交
70 71 72 73 74 75 76
struct SequenceExpandFunctor<platform::CUDADeviceContext, T> {
  void operator()(const platform::CUDADeviceContext& context,
                  const LoDTensor& x, LoDTensor* out) {
    auto x_dims = x.dims();
    size_t element_len = framework::product(x_dims) / x_dims[0];
    T* out_data = out->mutable_data<T>(context.GetPlace());
    auto out_starts = out->lod().back();
D
dzhwinter 已提交
77

D
dzhwinter 已提交
78 79 80 81 82 83
    dim3 block_size(16, 32, element_len);
    dim3 grid_size(10, 10);
    sequence_expand_kernel<<<grid_size, block_size, 0, context.stream()>>>(
        x.data<T>(), out->mutable_data<T>(context.GetPlace()),
        out_starts.CUDAData(context.GetPlace()), out_starts.size(),
        element_len);
D
dzhwinter 已提交
84
  }
D
dzhwinter 已提交
85
};
D
dzhwinter 已提交
86

D
dzhwinter 已提交
87 88
template <typename T>
struct SequenceExpandGradFunctor<platform::CUDADeviceContext, T> {
D
dzhwinter 已提交
89 90 91
  void operator()(const platform::CUDADeviceContext& context,
                  const LoDTensor& x, const LoDTensor& out,
                  const LoDTensor& dout, LoDTensor* dx) {
D
dzhwinter 已提交
92 93
    auto x_dims = x.dims();
    size_t element_len = framework::product(x_dims) / x_dims[0];
D
dzhwinter 已提交
94
    auto out_starts = out.lod().back();
D
dzhwinter 已提交
95

D
dzhwinter 已提交
96 97 98
    dim3 block_size(16, 32, element_len);
    dim3 grid_size(10, 10);
    size_t out_size = framework::product(dx->dims());
D
dzhwinter 已提交
99 100
    sequence_expand_grad_kernel<<<grid_size, block_size, out_size * sizeof(T),
                                  context.stream()>>>(
D
dzhwinter 已提交
101 102 103 104 105
        dout.data<T>(), dx->mutable_data<T>(context.GetPlace()),
        out_starts.CUDAData(context.GetPlace()), out_starts.size(), element_len,
        out_size);
  }
};
D
dzhwinter 已提交
106 107 108 109

}  // namespace operators
}  // namespace paddle

W
wanghaoshuang 已提交
110
namespace ops = paddle::operators;
Q
QI JUN 已提交
111
REGISTER_OP_CUDA_KERNEL(
W
wanghaoshuang 已提交
112 113
    sequence_expand,
    ops::SequenceExpandKernel<paddle::platform::CUDADeviceContext, float>);
Q
QI JUN 已提交
114
REGISTER_OP_CUDA_KERNEL(
W
wanghaoshuang 已提交
115 116
    sequence_expand_grad,
    ops::SequenceExpandGradKernel<paddle::platform::CUDADeviceContext, float>);