seq_expand_op.h 3.8 KB
Newer Older
W
wanghaoshuang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2016 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. */

#pragma once

#include "paddle/framework/op_registry.h"
W
wanghaoshuang 已提交
18
#include "paddle/memory/memcpy.h"
W
wanghaoshuang 已提交
19
#include "unsupported/Eigen/CXX11/Tensor"
W
wanghaoshuang 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32

namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;

template <typename Place, typename T>
class SeqExpandKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* x = context.Input<LoDTensor>("X");
    auto* out = context.Output<LoDTensor>("Out");
    const T* x_data = x->data<T>();
W
wanghaoshuang 已提交
33
    auto x_dims = x->dims();
W
wanghaoshuang 已提交
34 35 36 37 38 39
    auto* y = context.Input<LoDTensor>("Y");
    PADDLE_ENFORCE_EQ(x_dims[0], y->lod().back().size() - 1,
                      "The size of last lod level in Input(Y)"
                      "must be equal to dims[0] of Input(X).");
    out->set_lod(y->lod());
    auto place = context.GetEigenDevice<Place>();
W
wanghaoshuang 已提交
40 41
    size_t element_len = framework::product(x_dims) / x_dims[0];
    T* out_data = out->mutable_data<T>(context.GetPlace());
W
wanghaoshuang 已提交
42 43 44 45 46 47 48 49 50
    auto out_starts = out->lod().back();

    for (size_t i = 0; i < out_starts.size() - 1; i++) {
      int scale = out_starts[i + 1] - out_starts[i];
      Eigen::TensorMap<
          Eigen::Tensor<const T, 2, Eigen::RowMajor, Eigen::DenseIndex>>
          x_t(x_data, 1, element_len);
      Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor, Eigen::DenseIndex>>
          out_t(out_data, scale, element_len);
Y
Yu Yang 已提交
51
      Eigen::array<int, 2> cast({{scale, 1}});
W
wanghaoshuang 已提交
52 53 54
      out_t.device(place) = x_t.broadcast(cast);
      x_data += element_len;
      out_data += element_len * scale;
W
wanghaoshuang 已提交
55
    }
W
wanghaoshuang 已提交
56 57 58
  }
};

59 60 61 62 63 64 65 66 67 68 69 70
/*
 *Given Grad(Out)
 *
 *    Grad(Out).lod = [[0,                            2],
 *                     [0,              3,            6]]
 *    Grad(Out).data = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]
 * Then
 *    Grad(X).data = [(0.1 + 0.2 + 0.3), (0.4 + 0.5 + 0.6)]
 *                 = [0.6, 1.5]
 *    Grad(X).lod = Input(X).lod
 *
 * */
W
wanghaoshuang 已提交
71 72 73 74
template <typename Place, typename T>
class SeqExpandGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
W
wanghaoshuang 已提交
75 76 77
    auto* d_out = context.Input<LoDTensor>(framework::GradVarName("Out"));
    auto* x = context.Input<LoDTensor>("X");
    auto* out = context.Input<LoDTensor>("Out");
W
wanghaoshuang 已提交
78
    auto* d_x = context.Output<LoDTensor>(framework::GradVarName("X"));
W
wanghaoshuang 已提交
79
    auto out_last_level = out->lod().back();
W
wanghaoshuang 已提交
80 81 82
    d_x->set_lod(x->lod());
    const T* d_out_data = d_out->data<T>();
    T* d_x_data = d_x->mutable_data<T>(context.GetPlace());
83
    size_t element_len = d_out->numel() / d_out->dims()[0];
W
wanghaoshuang 已提交
84 85 86 87 88 89 90
    for (size_t i = 0; i < out_last_level.size() - 1; ++i) {
      size_t repeat = out_last_level[i + 1] - out_last_level[i];
      Eigen::TensorMap<
          Eigen::Tensor<const T, 2, Eigen::RowMajor, Eigen::DenseIndex>>
      d_out_t(d_out_data, static_cast<int>(repeat), element_len);
      Eigen::TensorMap<Eigen::Tensor<T, 1, Eigen::RowMajor, Eigen::DenseIndex>>
      d_x_t(d_x_data, static_cast<int>(element_len));
W
wanghaoshuang 已提交
91
      auto place = context.GetEigenDevice<Place>();
92
      d_x_t.device(place) = d_out_t.sum(Eigen::array<int, 1>({{0}}));
W
wanghaoshuang 已提交
93 94
      d_out_data += (repeat * element_len);
      d_x_data += element_len;
W
wanghaoshuang 已提交
95
    }
W
wanghaoshuang 已提交
96 97 98 99 100
  }
};

}  // namespace operators
}  // namespace paddle