sequence_erase_op.cu 4.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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>
W
Wu Yi 已提交
17
#include "paddle/fluid/operators/sequence_ops/sequence_erase_op.h"
D
dzhwinter 已提交
18
#include "paddle/fluid/platform/cuda_primitives.h"
19 20 21 22 23 24 25

namespace paddle {
namespace operators {
using platform::PADDLE_CUDA_NUM_THREADS;
using LoDTensor = framework::LoDTensor;

template <typename T>
26 27 28
__global__ void LabelErasedIdx(const T* in_dat, const int64_t in_len,
                               const int* tokens, const size_t tokens_len,
                               size_t* num_erased) {
29 30
  int index = blockIdx.x * blockDim.x + threadIdx.x;
  if (index < in_len) {
31
    for (size_t i = 0; i < tokens_len; ++i) {
32
      if (in_dat[index] == tokens[i]) {
33 34
        num_erased[index + 1] = 1;
        break;
35 36 37 38 39
      }
    }
  }
}

40 41
__global__ void GetOutLod(const size_t* num_erased, const size_t* in_lod,
                          const size_t lod_len, size_t* out_lod0) {
42 43 44 45 46 47 48
  int index = blockIdx.x * blockDim.x + threadIdx.x;
  if (index < lod_len) {
    out_lod0[index] = in_lod[index] - num_erased[in_lod[index]];
  }
}

template <typename T>
49 50
__global__ void SetOutput(const T* in_dat, const int64_t in_len,
                          const size_t* num_erased, T* out_dat) {
51 52
  int index = blockIdx.x * blockDim.x + threadIdx.x;
  if (index < in_len) {
Y
Yibing Liu 已提交
53
    if (num_erased[index] == num_erased[index + 1]) {
54 55 56 57 58 59 60 61 62 63 64 65 66
      out_dat[index - num_erased[index]] = in_dat[index];
    }
  }
}

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

    auto lod = in->lod();
67
    PADDLE_ENFORCE_EQ(lod[lod.size() - 1].back(), (size_t)in->numel(),
68
                      "The actual size mismatches with the LoD information.");
69
    auto tokens = ctx.Attr<std::vector<int>>("tokens");
70 71
    auto in_len = in->numel();
    auto in_dat = in->data<T>();
72
    // Copy tokens to GPU
73
    thrust::device_vector<int> dev_tokens(tokens.begin(), tokens.end());
74
    int* dev_tokens_ptr = thrust::raw_pointer_cast(dev_tokens.data());
75

76
    // Count number of elements to be erased
77
    thrust::device_vector<size_t> num_erased(in_len + 1, 0);
78
    size_t* num_erased_ptr = thrust::raw_pointer_cast(num_erased.data());
79 80 81
    auto stream = ctx.cuda_device_context().stream();
    LabelErasedIdx<<<(in_len - 1) / PADDLE_CUDA_NUM_THREADS + 1,
                     PADDLE_CUDA_NUM_THREADS, 0, stream>>>(
82
        in_dat, in_len, dev_tokens_ptr, tokens.size(), num_erased_ptr);
83 84 85
    thrust::inclusive_scan(num_erased.begin() + 1, num_erased.end(),
                           num_erased.begin() + 1);

86
    // Copy LoD to GPU
87 88 89
    auto last_lod = lod[lod.size() - 1];
    auto lod_len = last_lod.size();
    const size_t* dev_in_lod_ptr = last_lod.CUDAData(ctx.GetPlace());
90 91 92
    // Calc output LoD
    thrust::device_vector<size_t> dev_out_lod(lod_len);
    size_t* dev_out_lod_ptr = thrust::raw_pointer_cast(dev_out_lod.data());
93 94 95
    GetOutLod<<<(lod_len - 1) / PADDLE_CUDA_NUM_THREADS + 1,
                PADDLE_CUDA_NUM_THREADS, 0, stream>>>(
        num_erased_ptr, dev_in_lod_ptr, lod_len, dev_out_lod_ptr);
96
    // Set LoD for output
97
    std::vector<size_t> out_last_lod(dev_out_lod.begin(), dev_out_lod.end());
98
    framework::LoD out_lod;
99 100 101 102
    for (size_t i = 0; i < lod.size() - 1; ++i) {
      out_lod.push_back(lod[i]);
    }
    out_lod.push_back(out_last_lod);
Y
Yibing Liu 已提交
103
    out->set_lod(out_lod);
104 105

    // Set output
106
    out->Resize({static_cast<int64_t>(out_last_lod.back()), 1});
107 108 109 110 111 112 113 114 115 116 117
    auto out_dat = out->mutable_data<T>(ctx.GetPlace());
    SetOutput<<<(in_len - 1) / PADDLE_CUDA_NUM_THREADS + 1,
                PADDLE_CUDA_NUM_THREADS, 0, stream>>>(in_dat, in_len,
                                                      num_erased_ptr, out_dat);
  }
};

}  // namespace operators
}  // namespace paddle

REGISTER_OP_CUDA_KERNEL(sequence_erase,
118 119
                        paddle::operators::SequenceEraseOpCUDAKernel<int32_t>,
                        paddle::operators::SequenceEraseOpCUDAKernel<int64_t>);