sequence_reshape_op.h 3.3 KB
Newer Older
1
//   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13
//
// 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.
Y
yangyaming 已提交
14 15

#pragma once
Y
Yi Wang 已提交
16 17
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/math_function.h"
Y
yangyaming 已提交
18 19 20 21 22 23 24 25 26 27 28

namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;
template <typename DeviceContext, typename T>
class SequenceReshapeKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* in = context.Input<LoDTensor>("X");
    auto* out = context.Output<LoDTensor>("Out");
29
    int out_width = context.Attr<int>("new_dim");
Y
yangyaming 已提交
30 31 32 33 34

    auto in_dims = in->dims();
    int64_t in_width = in_dims[1];
    auto& in_lod = in->lod();

35 36 37
    PADDLE_ENFORCE_EQ(in_lod.empty(), false,
                      "Input(X) Tensor of SequenceReshapeOp does not contain "
                      "LoD information.");
Y
yangyaming 已提交
38 39
    PADDLE_ENFORCE_EQ(in_lod.size(), 1UL,
                      "Only support one level sequence now.");
40
    PADDLE_ENFORCE_EQ(
41
        (uint64_t)in_dims[0], in_lod[0].back(),
42
        "Inconsistent size between X.shape[0] and X.lod()[0].back().");
Y
yangyaming 已提交
43 44 45 46

    auto in_lod_l0 = in_lod[0];
    int seq_num = in_lod_l0.size() - 1;

Y
yangyaming 已提交
47 48 49 50 51
    if (in_width == out_width) {
      out->set_lod(in->lod());
    } else {
      auto& out_lod = *out->mutable_lod();
      out_lod.resize(1);
Y
yangyaming 已提交
52 53
      out_lod[0].resize(seq_num + 1);
      out_lod[0][0] = 0;
Y
yangyaming 已提交
54 55 56 57 58 59 60 61 62
      for (int i = 0; i < seq_num; ++i) {
        size_t seq_len = in_lod_l0[i + 1] - in_lod_l0[i];
        size_t offset = 0;
        offset = (seq_len * in_width) / out_width;
        PADDLE_ENFORCE_EQ(offset * out_width, seq_len * in_width,
                          "Please make sure (sequence_length * dimension) can "
                          "be divided by new_dim with no remainder for each "
                          "sequence. The %dth sequence is invalid.",
                          i + 1);
Y
yangyaming 已提交
63
        out_lod[0][i + 1] = out_lod[0][i] + offset;
Y
yangyaming 已提交
64
      }
Y
yangyaming 已提交
65 66
    }

Y
Yi Wang 已提交
67
    framework::TensorCopy(*in, context.GetPlace(), out);
Y
yangyaming 已提交
68
    out->Resize({static_cast<int64_t>(out->lod()[0].back()), out_width});
Y
yangyaming 已提交
69 70 71 72 73 74 75 76
  }
};

template <typename DeviceContext, typename T>
class SequenceReshapeGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* x_tensor_ptr = context.Input<LoDTensor>("X");
Y
yangyaming 已提交
77
    auto* outg_tensor_ptr =
Y
yangyaming 已提交
78
        context.Input<LoDTensor>(framework::GradVarName("Out"));
Y
yangyaming 已提交
79
    auto* xg_tensor_ptr =
Y
yangyaming 已提交
80 81
        context.Output<LoDTensor>(framework::GradVarName("X"));

Y
yangyaming 已提交
82
    xg_tensor_ptr->mutable_data<T>(context.GetPlace());
Y
Yi Wang 已提交
83
    framework::TensorCopy(*outg_tensor_ptr, context.GetPlace(), xg_tensor_ptr);
Y
yangyaming 已提交
84
    xg_tensor_ptr->Resize(x_tensor_ptr->dims());
Y
yangyaming 已提交
85 86 87 88 89
  }
};

}  // namespace operators
}  // namespace paddle