sequence_pad_op.h 2.9 KB
Newer Older
Y
yangyaming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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
16 17

#include <vector>
Y
yangyaming 已提交
18 19 20
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/memory/memcpy.h"
#include "paddle/fluid/operators/math/math_function.h"
21
#include "paddle/fluid/operators/math/sequence_padding.h"
Y
yangyaming 已提交
22 23 24 25 26 27 28 29 30 31 32

namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;
using LoD = framework::LoD;

template <typename DeviceContext, typename T>
class SequencePadOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
33 34
    const auto* x = ctx.Input<LoDTensor>("X");
    auto* out = ctx.Output<LoDTensor>("Out");
35
    auto* len_t = ctx.Output<LoDTensor>("Length");
36
    out->mutable_data<T>(ctx.GetPlace());
Y
yangyaming 已提交
37

38 39
    PADDLE_ENFORCE_EQ(
        x->lod().empty(), false,
40 41
        platform::errors::InvalidArgument("Input(X) Tensor of SequencePadOp "
                                          "does not contain LoD information."));
42

43
    const auto* pad_value = ctx.Input<LoDTensor>("PadValue");
44

45
    int padded_length = ctx.Attr<int>("padded_length");
Y
yangyaming 已提交
46

47
    math::PaddingLoDTensorFunctor<DeviceContext, T>()(
F
fengjiayi 已提交
48
        ctx.template device_context<DeviceContext>(), *x, out, *pad_value,
49
        padded_length, 0, false, math::kBatchLengthWidth);
50 51 52 53 54 55 56 57 58

    LoDTensor seq_len;
    seq_len.Resize(len_t->dims());
    int64_t* len_data = seq_len.mutable_data<int64_t>(platform::CPUPlace());
    for (size_t i = 1; i < x->lod()[0].size(); ++i) {
      len_data[i - 1] = x->lod()[0][i] - x->lod()[0][i - 1];
    }
    framework::TensorCopy(seq_len, ctx.GetPlace(),
                          ctx.template device_context<DeviceContext>(), len_t);
Y
yangyaming 已提交
59 60 61 62 63 64 65
  }
};

template <typename DeviceContext, typename T>
class SequencePadGradOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
66 67 68 69
    auto* d_x = ctx.Output<LoDTensor>(framework::GradVarName("X"));
    if (d_x) {
      const auto* d_out = ctx.Input<LoDTensor>(framework::GradVarName("Out"));
      d_x->mutable_data<T>(ctx.GetPlace());
Y
yangyaming 已提交
70

71
      int padded_length = ctx.Attr<int>("padded_length");
72

73 74 75 76
      math::UnpaddingLoDTensorFunctor<DeviceContext, T>()(
          ctx.template device_context<DeviceContext>(), *d_out, d_x,
          padded_length, 0, false, math::kBatchLengthWidth);
    }
Y
yangyaming 已提交
77 78 79 80 81
  }
};

}  // namespace operators
}  // namespace paddle