sequence_erase_op.h 2.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yibing Liu 已提交
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. */

#pragma once

17
#include <vector>
Y
Yi Wang 已提交
18
#include "paddle/fluid/framework/op_registry.h"
Y
Yibing Liu 已提交
19 20 21 22 23 24 25 26

namespace paddle {
namespace operators {

template <typename DeviceContext, typename T>
class SequenceEraseKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
27 28
    auto* in = ctx.Input<framework::LoDTensor>("X");
    auto* out = ctx.Output<framework::LoDTensor>("Out");
Y
Yibing Liu 已提交
29 30

    auto lod = in->lod();
31 32 33
    PADDLE_ENFORCE_EQ(
        lod.empty(), false,
        "Input(X) Tensor of SequenceEraseOp does not contain LoD information.");
34
    PADDLE_ENFORCE_EQ(lod[lod.size() - 1].back(), (size_t)in->numel(),
35
                      "The actual size mismatches with the LoD information.");
Y
Yibing Liu 已提交
36 37 38
    auto tokens = ctx.Attr<std::vector<int>>("tokens");
    auto in_len = in->numel();
    auto in_dat = in->data<T>();
39
    auto last_lod = lod[lod.size() - 1];
40

Y
Yibing Liu 已提交
41
    std::vector<size_t> num_erased(in_len + 1, 0);
42 43
    std::vector<size_t> out_last_lod(1, 0);
    for (size_t i = 0; i < last_lod.size() - 1; ++i) {
44
      size_t num_out = 0;
45
      for (auto j = last_lod[i] + 1; j <= last_lod[i + 1]; ++j) {
46 47 48 49 50 51 52
        num_erased[j] = num_erased[j - 1];
        if (std::find(tokens.begin(), tokens.end(), in_dat[j - 1]) !=
            tokens.end()) {
          num_erased[j] += 1;
        } else {
          num_out += 1;
        }
Y
Yibing Liu 已提交
53
      }
54
      out_last_lod.push_back(out_last_lod.back() + num_out);
Y
Yibing Liu 已提交
55 56 57 58 59 60
    }

    auto out_len = in_len - num_erased[in_len];
    out->Resize({static_cast<int64_t>(out_len), 1});
    auto out_dat = out->mutable_data<T>(ctx.GetPlace());

Y
Yibing Liu 已提交
61
    for (int64_t i = 0; i < in_len; ++i) {
Y
Yibing Liu 已提交
62 63 64 65 66
      if (num_erased[i] == num_erased[i + 1]) {
        out_dat[i - num_erased[i]] = in_dat[i];
      }
    }
    framework::LoD out_lod;
67 68 69 70
    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 已提交
71 72 73 74 75 76
    out->set_lod(out_lod);
  }
};

}  // namespace operators
}  // namespace paddle