sequence_expand_op.cu 2.7 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 66 67 68 69
namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;

template <typename T>
__global__ sequence_expand_kernel(const T* x_data, T* out_data, size_t* lod,
                                  size_t element_len) {
  int BLOCK_SIZE = 1024;
  __shared__ T shm_lod[BLOCK_SIZE];
  for (int idx = threadIdx.x; idx < BLOCK_SIZE; ++idx) {
    shm_lod[idx] = lod[idx];
  }
  for (int idx = threadIdx.x + blockIdx.x * blockDim.x; idx < lod.size();
       idx += blockDim.x * gridDim.x) {
    int scale = lod[i]
  }
}

template <typename T>
void SequenceExpandFunctor<platform::CPUDeviceContext, T>::operator()(
    const platform::CPUDeviceContext& context, const LoDTensor& x,
    LoDTensor* out) {
  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();

  const int kThreadsPerBlock = 1024;
  int block_cols = kThreadsPerBlock;
  if (out_cols < kThreadsPerBlock) {  // block_cols is aligned by 32.
    block_cols = ((out_cols + 31) >> 5) << 5;
  }
  int block_rows = kThreadsPerBlock / block_cols;
  dim3 block_size = dim3(block_cols, block_rows, 1);

  int max_threads = context.GetMaxPhysicalThreadCount();
  int max_blocks = std::max(max_threads / kThreadsPerBlock, 1);

  int grid_cols =
      std::min((out_cols + block_cols - 1) / block_cols, max_blocks);
  int grid_rows =
      std::min(max_blocks / grid_cols, std::max(out_rows / block_rows, 1));
  dim3 grid_size = dim3(grid_cols, grid_rows, 1);
  sequence_expand_kernel<<<grid_size, block_size, 0, context.stream()>>>(
      x.data<T>(), out->mutable_data<T>(context.GetPlace()),
      out_starts.CUDAData(context.GetPlace()), element_len);
}

}  // namespace operators
}  // namespace paddle

W
wanghaoshuang 已提交
70
namespace ops = paddle::operators;
Q
QI JUN 已提交
71
REGISTER_OP_CUDA_KERNEL(
W
wanghaoshuang 已提交
72 73
    sequence_expand,
    ops::SequenceExpandKernel<paddle::platform::CUDADeviceContext, float>);
Q
QI JUN 已提交
74
REGISTER_OP_CUDA_KERNEL(
W
wanghaoshuang 已提交
75 76
    sequence_expand_grad,
    ops::SequenceExpandGradKernel<paddle::platform::CUDADeviceContext, float>);