sequence_enumerate_op.cu 3.4 KB
Newer Older
C
chenweihang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
17

W
Wu Yi 已提交
18
#include "paddle/fluid/operators/sequence_ops/sequence_enumerate_op.h"
19
#include "paddle/phi/backends/gpu/gpu_primitives.h"
C
chenweihang 已提交
20 21 22

namespace paddle {
namespace operators {
23
using phi::PADDLE_CUDA_NUM_THREADS;
24
using LoDTensor = phi::DenseTensor;
C
chenweihang 已提交
25 26

template <typename T>
27 28 29 30 31 32
__global__ void CalcOutPut(const T* in_data,
                           const size_t* in_lod,
                           const size_t lod_len,
                           const int64_t win_size,
                           const int64_t pad_value,
                           T* out_data) {
C
chenweihang 已提交
33
  int index = blockIdx.x * blockDim.x + threadIdx.x;
34 35 36 37 38 39 40 41 42
  if (index < in_lod[lod_len - 1]) {
    int end_idx = 0;
    // Get LoD interval of index
    for (int i = 1; i < lod_len; ++i) {
      if (index < in_lod[i]) {
        end_idx = in_lod[i];
        break;
      }
    }
C
chenweihang 已提交
43 44 45
    for (size_t i = 0; i < win_size; ++i) {
      int word_pos = index + i;
      out_data[index * win_size + i] =
46
          word_pos < end_idx ? in_data[word_pos] : pad_value;
C
chenweihang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    }
  }
}

template <typename T>
class SequenceEnumerateOpCUDAKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* in = context.Input<LoDTensor>("X");
    auto* out = context.Output<LoDTensor>("Out");
    int win_size = context.Attr<int>("win_size");
    int pad_value = context.Attr<int>("pad_value");

    auto in_dims = in->dims();
    auto in_lod = in->lod();

    PADDLE_ENFORCE_EQ(
64 65
        static_cast<uint64_t>(in_dims[0]),
        in_lod[0].back(),
66 67 68
        platform::errors::InvalidArgument(
            "The actual input data's size mismatched with LoD information."
            "Received input data size is %d (actual) vs %d (loD information).",
69 70
            static_cast<uint64_t>(in_dims[0]),
            in_lod[0].back()));
C
chenweihang 已提交
71 72 73

    /* Generate enumerate sequence set */
    auto stream = context.cuda_device_context().stream();
74
    auto lod0 = in_lod[0];
C
chenweihang 已提交
75 76
    auto in_len = in->numel();
    auto in_data = in->data<T>();
77
    out->Resize({in_dims[0], win_size});
C
chenweihang 已提交
78
    auto out_data = out->mutable_data<T>(context.GetPlace());
79
    // Copy LoD to GPU
80 81
    paddle::framework::MixVector<size_t> mixv_lod0(&lod0);
    const size_t* dev_in_lod_ptr = mixv_lod0.CUDAData(context.GetPlace());
C
chenweihang 已提交
82 83
    // Calc output tensor
    CalcOutPut<<<(in_len - 1) / PADDLE_CUDA_NUM_THREADS + 1,
84 85 86
                 PADDLE_CUDA_NUM_THREADS,
                 0,
                 stream>>>(
87
        in_data, dev_in_lod_ptr, lod0.size(), win_size, pad_value, out_data);
88
    out->set_lod(in->lod());
C
chenweihang 已提交
89 90 91 92 93 94 95 96 97 98
  }
};

}  // namespace operators
}  // namespace paddle

REGISTER_OP_CUDA_KERNEL(
    sequence_enumerate,
    paddle::operators::SequenceEnumerateOpCUDAKernel<int32_t>,
    paddle::operators::SequenceEnumerateOpCUDAKernel<int64_t>);