sequence_reshape_op.h 3.1 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
//   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
//
// 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 16 17 18 19 20 21 22 23 24 25 26 27 28

#pragma once
#include "paddle/framework/op_registry.h"
#include "paddle/operators/math/math_function.h"

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 35 36

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

    PADDLE_ENFORCE_EQ(in_lod.size(), 1UL,
                      "Only support one level sequence now.");
37 38 39
    PADDLE_ENFORCE_EQ(
        in_dims[0], in_lod[0].back(),
        "Inconsistent size between X.shape[0] and X.lod()[0].back().");
Y
yangyaming 已提交
40 41 42 43

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

Y
yangyaming 已提交
44 45 46 47 48
    if (in_width == out_width) {
      out->set_lod(in->lod());
    } else {
      auto& out_lod = *out->mutable_lod();
      out_lod.resize(1);
Y
yangyaming 已提交
49 50
      out_lod[0].resize(seq_num + 1);
      out_lod[0][0] = 0;
Y
yangyaming 已提交
51 52 53 54 55 56 57 58 59
      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 已提交
60
        out_lod[0][i + 1] = out_lod[0][i] + offset;
Y
yangyaming 已提交
61
      }
Y
yangyaming 已提交
62 63
    }

Y
yangyaming 已提交
64 65
    framework::Copy(*in, context.GetPlace(), out);
    out->Resize({static_cast<int64_t>(out->lod()[0].back()), out_width});
Y
yangyaming 已提交
66 67 68 69 70 71 72 73
  }
};

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 已提交
74
    auto* outg_tensor_ptr =
Y
yangyaming 已提交
75
        context.Input<LoDTensor>(framework::GradVarName("Out"));
Y
yangyaming 已提交
76
    auto* xg_tensor_ptr =
Y
yangyaming 已提交
77 78
        context.Output<LoDTensor>(framework::GradVarName("X"));

Y
yangyaming 已提交
79 80 81
    xg_tensor_ptr->mutable_data<T>(context.GetPlace());
    framework::Copy(*outg_tensor_ptr, context.GetPlace(), xg_tensor_ptr);
    xg_tensor_ptr->Resize(x_tensor_ptr->dims());
Y
yangyaming 已提交
82 83 84 85 86
  }
};

}  // namespace operators
}  // namespace paddle