sequence_enumerate_op.h 3.4 KB
Newer Older
C
chenweihang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
//   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.

#pragma once

#include "paddle/fluid/framework/op_registry.h"

namespace paddle {
namespace operators {

22
template <typename T, typename DeviceContext>
C
chenweihang 已提交
23 24 25
class SequenceEnumerateKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
柠檬味~ 已提交
26 27
    auto* in = context.Input<phi::DenseTensor>("X");
    auto* out = context.Output<phi::DenseTensor>("Out");
C
chenweihang 已提交
28
    int win_size = context.Attr<int>("win_size");
T
tensor-tang 已提交
29
    auto pad_value = static_cast<T>(context.Attr<int>("pad_value"));
C
chenweihang 已提交
30

31
    PADDLE_ENFORCE_EQ(
32 33
        in->lod().empty(),
        false,
34
        platform::errors::InvalidArgument(
柠檬味~ 已提交
35
            "Input(X) phi::DenseTensor of SequenceEnumerateOp does not contain "
36
            "LoD information."));
37

R
risemeup1 已提交
38
    auto in_dims = phi::vectorize<int>(in->dims());
T
tensor-tang 已提交
39
    auto lod0 = in->lod()[0];
C
chenweihang 已提交
40
    PADDLE_ENFORCE_EQ(
41 42
        static_cast<uint64_t>(in_dims[0]),
        lod0.back(),
43 44 45
        platform::errors::InvalidArgument(
            "The actual input data's size mismatched with LoD information."
            "Received input data size is %d (actual) vs %d (loD information).",
46 47
            static_cast<uint64_t>(in_dims[0]),
            lod0.back()));
T
tensor-tang 已提交
48
    PADDLE_ENFORCE_EQ(
49 50
        in_dims.size(),
        2UL,
51 52 53 54
        platform::errors::InvalidArgument(
            "Input(X) of SequenceEnumerate operator's rank should be 2."
            "Received %d instead.",
            in_dims.size()));
55 56
    PADDLE_ENFORCE_EQ(in_dims[1],
                      1,
57 58 59 60
                      platform::errors::InvalidArgument(
                          "Input(X) of SequenceEnumerate operator's 2nd "
                          "dimension should be 1. Received %d instead.",
                          in_dims[1]));
C
chenweihang 已提交
61 62 63

    // Generate enumerate sequence set
    auto in_data = in->data<T>();
64
    out->Resize({in_dims[0], win_size});
T
tensor-tang 已提交
65
    out->set_lod(in->lod());
C
chenweihang 已提交
66
    auto out_data = out->mutable_data<T>(context.GetPlace());
67
    for (size_t i = 0; i < lod0.size() - 1; ++i) {
68
      if (lod0[i] == lod0[i + 1]) continue;
T
tensor-tang 已提交
69 70
      int start = lod0[i];
      int end = lod0[i + 1];
71

T
tensor-tang 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
      int copy_size = win_size < end - start + 1 ? win_size : end - start + 1;
      int mid = end + 1 - copy_size;
      int pad_num = win_size - copy_size;
      copy_size *= sizeof(T);
      for (int idx = start; idx < mid; ++idx) {
        std::memcpy(out_data, in_data + idx, copy_size);
        out_data += win_size;
      }
      for (int idx = mid; idx < end; ++idx) {
        copy_size -= sizeof(T);
        pad_num++;
        std::memcpy(out_data, in_data + idx, copy_size);
        T* pdata = out_data + copy_size / sizeof(T);
        for (int i = 0; i < pad_num; ++i) {
          pdata[i] = pad_value;
87
        }
T
tensor-tang 已提交
88
        out_data += win_size;
C
chenweihang 已提交
89 90 91 92 93 94 95
      }
    }
  }
};

}  // namespace operators
}  // namespace paddle