sequence_concat_op.h 4.5 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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.
Y
Yancey1989 已提交
14 15

#pragma once
C
chengduoZH 已提交
16

17
#include <vector>
Y
Yi Wang 已提交
18
#include "paddle/fluid/framework/op_registry.h"
C
chengduoZH 已提交
19 20
#include "paddle/fluid/operators/detail/safe_ref.h"
#include "paddle/fluid/operators/math/concat.h"
Y
Yancey1989 已提交
21 22 23 24

namespace paddle {
namespace operators {

C
chengduoZH 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38
namespace detail {
template <typename Container>
inline framework::LoD ConcatLoD(const Container &xs,
                                std::vector<framework::Tensor> *xs_in_order) {
  std::vector<size_t> result;
  result.resize(xs[0].get().lod()[0].size());

  for (size_t i = 1; i < result.size(); ++i) {
    size_t sum = 0;
    for (size_t j = 0; j < xs.size(); ++j) {
      auto &x_lod = xs[j].get().lod()[0];
      const framework::Tensor &tensor = xs[j].get();
      xs_in_order->emplace_back(tensor.Slice(x_lod[i - 1], x_lod[i]));
      sum += x_lod[i];
39
    }
C
chengduoZH 已提交
40
    result[i] = sum;
41
  }
C
chengduoZH 已提交
42 43 44
  framework::LoD lod;
  lod.emplace_back(result);
  return lod;
Y
Yancey1989 已提交
45
}
C
chengduoZH 已提交
46
}  // namespace detail
Y
Yancey1989 已提交
47

Q
QI JUN 已提交
48
template <typename DeviceContext, typename T>
C
chengduoZH 已提交
49
class SeqConcatKernel : public framework::OpKernel<T> {
Y
Yancey1989 已提交
50
 public:
C
chengduoZH 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64
  void Compute(const framework::ExecutionContext &context) const override {
    auto xs = detail::VectorRef(context.MultiInput<framework::LoDTensor>("X"),
                                "Cannot find multiple input X");
    auto &out = detail::Ref(context.Output<framework::LoDTensor>("Out"),
                            "Cannot find output");

    size_t lod_size = 0;
    for (auto &x : xs) {
      if (lod_size == 0) {
        lod_size = x.get().lod()[0].size();
      } else {
        PADDLE_ENFORCE_EQ(
            lod_size, x.get().lod()[0].size(),
            "The number of sequence must be same between each input");
Y
Yancey1989 已提交
65 66
      }
    }
C
chengduoZH 已提交
67 68 69 70 71 72 73 74
    PADDLE_ENFORCE_NE(lod_size, 0, "Each input must have sequence information");

    std::vector<framework::Tensor> x_in_order;
    out.set_lod(detail::ConcatLoD(xs, &x_in_order));
    out.mutable_data<T>(context.GetPlace());
    math::ConcatFunctor<DeviceContext, T> functor;
    functor(context.template device_context<DeviceContext>(), x_in_order, 0,
            &out);
Y
Yancey1989 已提交
75 76 77
  }
};

Q
QI JUN 已提交
78
template <typename DeviceContext, typename T>
C
chengduoZH 已提交
79
class SeqConcatGradKernel : public framework::OpKernel<T> {
Y
Yancey1989 已提交
80
 public:
C
chengduoZH 已提交
81 82 83 84 85 86 87 88 89 90
  void Compute(const framework::ExecutionContext &context) const override {
    auto xs = context.MultiInput<framework::LoDTensor>("X");
    auto dxs =
        context.MultiOutput<framework::LoDTensor>(framework::GradVarName("X"));
    PADDLE_ENFORCE_EQ(xs.size(), dxs.size());
    for (size_t i = 0; i < dxs.size(); ++i) {
      if (dxs[i] != nullptr) {
        dxs[i]->set_lod(xs[i]->lod());
        dxs[i]->mutable_data<T>(context.GetPlace());
      }
Y
Yancey1989 已提交
91
    }
C
chengduoZH 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
    std::vector<framework::Tensor> sliced_x;
    std::vector<boost::variant<boost::blank, framework::Tensor>> sliced_dx;

    for (size_t i = 1; i < xs[0]->lod()[0].size(); ++i) {
      for (size_t j = 0; j < xs.size(); ++j) {
        const framework::LoDTensor *x = xs[j];
        framework::LoDTensor *dx = dxs[j];
        auto &x_lod = x->lod()[0];
        sliced_x.emplace_back(x->Slice(x_lod[i - 1], x_lod[i]));
        if (dx != nullptr) {
          sliced_dx.emplace_back(dx->Slice(x_lod[i - 1], x_lod[i]));
        } else {
          sliced_dx.emplace_back(boost::blank());
        }
      }
107
    }
Y
Yancey1989 已提交
108

C
chengduoZH 已提交
109 110 111 112 113 114
    math::ConcatGradFunctor<DeviceContext, T> functor;
    std::vector<const framework::Tensor *> sliced_x_ptr;
    std::vector<framework::Tensor *> sliced_dx_ptr;
    for (auto &x : sliced_x) {
      sliced_x_ptr.emplace_back(&x);
    }
Y
Yancey1989 已提交
115

C
chengduoZH 已提交
116 117 118 119 120
    for (auto &dx : sliced_dx) {
      try {
        sliced_dx_ptr.emplace_back(&boost::get<framework::Tensor>(dx));
      } catch (boost::bad_get &) {
        sliced_dx_ptr.emplace_back(nullptr);
Y
Yancey1989 已提交
121 122
      }
    }
C
chengduoZH 已提交
123 124 125 126 127
    functor(context.template device_context<DeviceContext>(),
            detail::Ref(
                context.Input<framework::Tensor>(framework::GradVarName("Out")),
                "Sequence Concat OG must be set"),
            sliced_x_ptr, 0, &sliced_dx_ptr);
Y
Yancey1989 已提交
128 129 130 131 132
  }
};

}  // namespace operators
}  // namespace paddle