sequence_expand_op.cu 6.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
D
dzhwinter 已提交
16 17
#include <stdio.h>
#include <algorithm>
Y
Yi Wang 已提交
18
#include "paddle/fluid/operators/sequence_expand_op.h"
D
dzhwinter 已提交
19
#include "paddle/fluid/platform/cuda_helper.h"
W
wanghaoshuang 已提交
20

D
dzhwinter 已提交
21 22 23 24 25 26
namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;

template <typename T>
D
dzhwinter 已提交
27
__global__ void sequence_expand_kernel(const T* x_data, T* out_data,
D
dzhwinter 已提交
28 29 30 31 32 33 34 35 36 37 38 39
                                       const size_t* lod,
                                       const size_t* out_offset,
                                       size_t lod_size, size_t element_len,
                                       size_t x_size) {
  int bid_x = blockIdx.x;
  if (bid_x > lod_size) return;
  int repeats = lod[bid_x];
  int offset = out_offset[bid_x];
  for (int tid_y = threadIdx.y; tid_y < repeats; tid_y += blockDim.y) {
    for (int tid_x = threadIdx.x; tid_x < element_len; tid_x += blockDim.x) {
      out_data[(offset + tid_y) * element_len + tid_x] =
          x_data[bid_x * element_len + tid_x];
D
dzhwinter 已提交
40
    }
D
dzhwinter 已提交
41
  }
D
dzhwinter 已提交
42 43 44 45
}

template <typename T>
__global__ void sequence_expand_grad_kernel(const T* dout_data, T* dx_data,
D
dzhwinter 已提交
46 47 48 49 50 51 52 53 54 55 56
                                            const size_t* lod,
                                            const size_t* out_offset,
                                            size_t lod_size, size_t element_len,
                                            size_t dout_size, size_t dx_size) {
  // reduce visit memory time.
  // dout_shm = [0 - dout_size-1], dx_shm = [dout_size-1, dout_size + dx_size-1]
  if (blockIdx.x == 0 && blockIdx.y == 0 && threadIdx.x == 0 &&
      threadIdx.y == 0) {
    printf("lod_size=%ld, element_size=%ld, dout_size=%ld, dx_size=%ld\n",
           lod_size, element_len, dout_size, dx_size);
  }
D
dzhwinter 已提交
57
  extern __shared__ T shm[];
D
dzhwinter 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  T* dout_shm = shm;
  T* dx_shm = &shm[dout_size];

  // int idx = threadIdx.x + blockIdx.x * blockDim.x;
  for (int idx = 0; idx < dout_size; ++idx) {
    if (idx < dx_size) {
      dx_shm[idx] = 0.0;
    }
    if (idx < dout_size) {
      dout_shm[idx] = dout_data[idx];
    }
  }

  int bid_x = blockIdx.x;
  if (bid_x > lod_size) return;
  int repeats = lod[bid_x];
  int offset = out_offset[bid_x];
  if (threadIdx.x == 0) {
    printf("repeats=%d, offset=%ld\n", repeats, offset);
  }
  for (int tid_y = threadIdx.y; tid_y < repeats; tid_y += blockDim.y) {
    for (int tid_x = threadIdx.x; tid_x < element_len; tid_x += blockDim.x) {
      T val = dout_shm[(offset + tid_y) * element_len + tid_x];
      platform::CudaAtomicAdd(&dx_shm[bid_x * element_len + tid_x], val);
      int dx_idx = bid_x * element_len + tid_x;
      int dout_idx = (offset + tid_y) * element_len + tid_x;
      printf("dx_idx=%d, dout_idx=%d, dx_data=%f, dout_data=%f, val=%f \n",
             dx_idx, dout_idx, dx_shm[dx_idx], dout_shm[dout_idx], val);
D
dzhwinter 已提交
86 87 88
    }
  }
  __syncthreads();
D
dzhwinter 已提交
89 90 91 92
  // copy shared memory back to dx
  for (int idx = threadIdx.x + blockIdx.x * blockDim.x; idx < dx_size;
       idx += blockDim.x * gridDim.x) {
    dx_data[idx] = dx_shm[idx];
D
dzhwinter 已提交
93 94 95 96
  }
}

template <typename T>
D
dzhwinter 已提交
97 98 99 100 101
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];
D
dzhwinter 已提交
102 103 104 105 106
    auto lod = out->lod().back();
    framework::Vector<size_t> out_lod;
    for (size_t i = 0; i < lod.size() - 1; ++i) {
      out_lod.push_back(lod[i + 1] - lod[i]);
    }
D
dzhwinter 已提交
107

D
dzhwinter 已提交
108 109 110 111
    int thread_x = std::max(static_cast<int>(element_len), 32);
    int block_x = static_cast<int>(out_lod.size());
    dim3 block_size(thread_x, 1024 / thread_x);
    dim3 grid_size(block_x, 1);
D
dzhwinter 已提交
112 113
    sequence_expand_kernel<<<grid_size, block_size, 0, context.stream()>>>(
        x.data<T>(), out->mutable_data<T>(context.GetPlace()),
D
dzhwinter 已提交
114 115
        out_lod.CUDAData(context.GetPlace()), lod.CUDAData(context.GetPlace()),
        out_lod.size(), element_len, framework::product(x_dims));
D
dzhwinter 已提交
116
  }
D
dzhwinter 已提交
117
};
D
dzhwinter 已提交
118

D
dzhwinter 已提交
119 120
template <typename T>
struct SequenceExpandGradFunctor<platform::CUDADeviceContext, T> {
D
dzhwinter 已提交
121 122 123
  void operator()(const platform::CUDADeviceContext& context,
                  const LoDTensor& x, const LoDTensor& out,
                  const LoDTensor& dout, LoDTensor* dx) {
D
dzhwinter 已提交
124 125
    auto x_dims = x.dims();
    size_t element_len = framework::product(x_dims) / x_dims[0];
D
dzhwinter 已提交
126 127 128 129 130 131 132
    auto lod = out.lod().back();
    framework::Vector<size_t> out_lod;
    for (size_t i = 0; i < lod.size() - 1; ++i) {
      out_lod.push_back(lod[i + 1] - lod[i]);
    }
    size_t dout_size = framework::product(dout.dims());
    size_t dx_size = framework::product(dx->dims());
D
dzhwinter 已提交
133

D
dzhwinter 已提交
134 135 136 137 138 139
    int thread_x = std::max(static_cast<int>(element_len), 32);
    dim3 block_size(thread_x, 1024 / thread_x);
    int block_x = static_cast<int>(out_lod.size());
    dim3 grid_size(block_x, 1);
    sequence_expand_grad_kernel<<<grid_size, block_size,
                                  (dout_size + dx_size) * sizeof(T),
D
dzhwinter 已提交
140
                                  context.stream()>>>(
D
dzhwinter 已提交
141
        dout.data<T>(), dx->mutable_data<T>(context.GetPlace()),
D
dzhwinter 已提交
142 143
        out_lod.CUDAData(context.GetPlace()), lod.CUDAData(context.GetPlace()),
        out_lod.size(), element_len, dout_size, dx_size);
D
dzhwinter 已提交
144 145
  }
};
D
dzhwinter 已提交
146 147 148 149

}  // namespace operators
}  // namespace paddle

W
wanghaoshuang 已提交
150
namespace ops = paddle::operators;
Q
QI JUN 已提交
151
REGISTER_OP_CUDA_KERNEL(
W
wanghaoshuang 已提交
152
    sequence_expand,
Y
yangyaming 已提交
153 154 155 156
    ops::SequenceExpandKernel<paddle::platform::CUDADeviceContext, float>,
    ops::SequenceExpandKernel<paddle::platform::CUDADeviceContext, double>,
    ops::SequenceExpandKernel<paddle::platform::CUDADeviceContext, int>,
    ops::SequenceExpandKernel<paddle::platform::CUDADeviceContext, int64_t>);
Q
QI JUN 已提交
157
REGISTER_OP_CUDA_KERNEL(
W
wanghaoshuang 已提交
158
    sequence_expand_grad,
Y
yangyaming 已提交
159 160 161 162 163
    ops::SequenceExpandGradKernel<paddle::platform::CUDADeviceContext, float>,
    ops::SequenceExpandGradKernel<paddle::platform::CUDADeviceContext, double>,
    ops::SequenceExpandGradKernel<paddle::platform::CUDADeviceContext, int>,
    ops::SequenceExpandGradKernel<paddle::platform::CUDADeviceContext,
                                  int64_t>);