sequence_expand_op.cu 4.5 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 57 58 59 60 61 62 63 64 65
}

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) {
        shm[item_start + tid_z] += doutx_data[item_start * scale + tid_z];
      }
    }
  }
  // synchronize before write to dx
  __syncthreads();
  for (int idx = blockDimx * blockIdx.x + threadIdx.x;
       idx < static_cast<int>(dout_size); idx += blockDim.x * gridDim.x) {
    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 89 90 91 92 93 94 95
template <typename T>
struct SequenceExpandGradFunctor<platform::CUDADeviceContext, T> {
  void operator()(const platform::CUDADeviceContext& ctx, const LoDTensor& x,
                  const LoDTensor& out, const LoDTensor& dout, LoDTensor* dx) {
    auto x_dims = x.dims();
    size_t element_len = framework::product(x_dims) / x_dims[0];
    const T* x_data = x->data<T>();
    T* out_data = out->mutable_data<T>(context.GetPlace());
    auto out_starts = out->lod().back();
D
dzhwinter 已提交
96

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

}  // namespace operators
}  // namespace paddle

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