|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include <thrust/device_vector.h> |
| 16 | +#include <thrust/host_vector.h> |
| 17 | +#include "paddle/operators/sequence_erase_op.h" |
| 18 | +#include "paddle/platform/cuda_helper.h" |
| 19 | + |
| 20 | +namespace paddle { |
| 21 | +namespace operators { |
| 22 | +using platform::PADDLE_CUDA_NUM_THREADS; |
| 23 | +using LoDTensor = framework::LoDTensor; |
| 24 | + |
| 25 | +template <typename T> |
| 26 | +__global__ void LabelErasedIdx(const T* in_dat, const int in_len, |
| 27 | + const T* tokens, const int tokens_len, |
| 28 | + int* num_erased) { |
| 29 | + int index = blockIdx.x * blockDim.x + threadIdx.x; |
| 30 | + if (index < in_len) { |
| 31 | + int erased = 0; |
| 32 | + for (int i = 0; i < tokens_len; ++i) { |
| 33 | + if (in_dat[index] == tokens[i]) { |
| 34 | + erased = 1; |
| 35 | + } |
| 36 | + } |
| 37 | + num_erased[index + 1] = erased; |
| 38 | + if (index == 0) { |
| 39 | + num_erased[0] = 0; |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +template <typename T> |
| 45 | +__global__ void GetOutLod(const T* num_erased, const int* in_lod, |
| 46 | + const int lod_len, int* out_lod0) { |
| 47 | + int index = blockIdx.x * blockDim.x + threadIdx.x; |
| 48 | + if (index < lod_len) { |
| 49 | + out_lod0[index] = in_lod[index] - num_erased[in_lod[index]]; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +template <typename T> |
| 54 | +__global__ void SetOutput(const T* in_dat, const int in_len, |
| 55 | + const int* num_erased, T* out_dat) { |
| 56 | + int index = blockIdx.x * blockDim.x + threadIdx.x; |
| 57 | + if (index < in_len) { |
| 58 | + if (in_dat[index] != in_dat[index + 1]) { |
| 59 | + out_dat[index - num_erased[index]] = in_dat[index]; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +template <typename T> |
| 65 | +class SequenceEraseOpCUDAKernel : public framework::OpKernel<T> { |
| 66 | + public: |
| 67 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 68 | + auto* in = ctx.Input<LoDTensor>("X"); |
| 69 | + auto* out = ctx.Output<LoDTensor>("Out"); |
| 70 | + |
| 71 | + auto lod = in->lod(); |
| 72 | + PADDLE_ENFORCE_EQ(lod.size(), 1UL, "Only support one level sequence now."); |
| 73 | + PADDLE_ENFORCE_EQ(lod[0].back(), (size_t)in->numel(), |
| 74 | + "The actual size mismatches with the LoD information."); |
| 75 | + auto tokens = ctx.Attr<std::vector<T>>("tokens"); |
| 76 | + auto tokens_len = tokens.size(); |
| 77 | + auto in_len = in->numel(); |
| 78 | + auto in_dat = in->data<T>(); |
| 79 | + auto lod0 = lod[0]; |
| 80 | + |
| 81 | + thrust::host_vector<T> host_tokens(tokens_len); |
| 82 | + for (size_t i = 0; i < tokens.size(); ++i) { |
| 83 | + host_tokens[i] = tokens[i]; |
| 84 | + } |
| 85 | + thrust::device_vector<T> dev_tokens = host_tokens; |
| 86 | + thrust::device_vector<int> num_erased(in_len + 1); |
| 87 | + |
| 88 | + T* dev_tokens_ptr = thrust::raw_pointer_cast(dev_tokens.data()); |
| 89 | + int* num_erased_ptr = thrust::raw_pointer_cast(num_erased.data()); |
| 90 | + |
| 91 | + auto stream = ctx.cuda_device_context().stream(); |
| 92 | + LabelErasedIdx<<<(in_len - 1) / PADDLE_CUDA_NUM_THREADS + 1, |
| 93 | + PADDLE_CUDA_NUM_THREADS, 0, stream>>>( |
| 94 | + in_dat, in_len, dev_tokens_ptr, tokens_len, num_erased_ptr); |
| 95 | + thrust::inclusive_scan(num_erased.begin() + 1, num_erased.end(), |
| 96 | + num_erased.begin() + 1); |
| 97 | + |
| 98 | + // Calc LoD |
| 99 | + auto lod_len = lod0.size(); |
| 100 | + thrust::host_vector<int> host_lod(lod_len); |
| 101 | + for (size_t i = 0; i < lod_len; ++i) { |
| 102 | + host_lod[i] = lod0[i]; |
| 103 | + } |
| 104 | + thrust::device_vector<int> dev_in_lod = host_lod; |
| 105 | + thrust::device_vector<int> dev_out_lod(lod_len); |
| 106 | + int* dev_in_lod_ptr = thrust::raw_pointer_cast(dev_in_lod.data()); |
| 107 | + int* dev_out_lod_ptr = thrust::raw_pointer_cast(dev_out_lod.data()); |
| 108 | + GetOutLod<<<(lod_len - 1) / PADDLE_CUDA_NUM_THREADS + 1, |
| 109 | + PADDLE_CUDA_NUM_THREADS, 0, stream>>>( |
| 110 | + num_erased_ptr, dev_in_lod_ptr, lod_len, dev_out_lod_ptr); |
| 111 | + thrust::host_vector<int> host_out_lod = dev_out_lod; |
| 112 | + std::vector<int> out_lod0(lod_len, 0); |
| 113 | + for (size_t i = 0; i < lod_len; i++) { |
| 114 | + out_lod0[i] = host_out_lod[i]; |
| 115 | + } |
| 116 | + framework::LoD out_lod; |
| 117 | + out_lod.push_back(out_lod0); |
| 118 | + out->set_lod(out_lod); |
| 119 | + |
| 120 | + // Set output |
| 121 | + out->Resize({out_lod0.back(), 1}); |
| 122 | + auto out_dat = out->mutable_data<T>(ctx.GetPlace()); |
| 123 | + SetOutput<<<(in_len - 1) / PADDLE_CUDA_NUM_THREADS + 1, |
| 124 | + PADDLE_CUDA_NUM_THREADS, 0, stream>>>(in_dat, in_len, |
| 125 | + num_erased_ptr, out_dat); |
| 126 | + } |
| 127 | +}; |
| 128 | + |
| 129 | +} // namespace operators |
| 130 | +} // namespace paddle |
| 131 | + |
| 132 | +REGISTER_OP_CUDA_KERNEL(sequence_erase, |
| 133 | + paddle::operators::SequenceEraseOpCUDAKernel<int32_t>); |
0 commit comments