sequence_expand_op.h 5.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
W
wanghaoshuang 已提交
2

L
Luo Tao 已提交
3 4 5
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
W
wanghaoshuang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
W
wanghaoshuang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
W
wanghaoshuang 已提交
14 15 16

#pragma once

Y
Yi Wang 已提交
17 18
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/memory/memcpy.h"
D
dzhwinter 已提交
19
#include "paddle/fluid/platform/device_context.h"
W
wanghaoshuang 已提交
20 21 22 23 24 25

namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;

D
dzhwinter 已提交
26 27 28 29 30
template <typename DeviceContext, typename T>
struct SequenceExpandFunctor {
  void operator()(const DeviceContext& ctx, const LoDTensor& x, LoDTensor* out);
};

D
dzhwinter 已提交
31 32 33 34 35
template <typename DeviceContext, typename T>
struct SequenceExpandGradFunctor {
  void operator()(const DeviceContext& ctx, const LoDTensor& x,
                  const LoDTensor& out, const LoDTensor& dout, LoDTensor* dx);
};
D
dzhwinter 已提交
36 37

template <typename T>
D
dzhwinter 已提交
38 39 40 41 42 43 44 45
struct SequenceExpandFunctor<platform::CPUDeviceContext, T> {
  void operator()(const platform::CPUDeviceContext& context, const LoDTensor& x,
                  LoDTensor* out) {
    auto x_dims = x.dims();
    size_t element_len = framework::product(x_dims) / x_dims[0];
    const T* x_data = x->data<T>();
    T* out_data = out->mutable_data<T>(context.GetPlace());
    auto out_starts = out->lod().back();
D
dzhwinter 已提交
46

D
dzhwinter 已提交
47 48 49 50 51 52 53 54 55 56 57 58
    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);
      Eigen::array<int, 2> cast({{scale, 1}});
      out_t.device(*context.eigen_device()) = x_t.broadcast(cast);
      x_data += element_len;
      out_data += element_len * scale;
    }
D
dzhwinter 已提交
59
  }
D
dzhwinter 已提交
60
};
D
dzhwinter 已提交
61

Q
QI JUN 已提交
62
template <typename DeviceContext, typename T>
W
wanghaoshuang 已提交
63
class SequenceExpandKernel : public framework::OpKernel<T> {
W
wanghaoshuang 已提交
64 65 66 67
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* x = context.Input<LoDTensor>("X");
    auto* out = context.Output<LoDTensor>("Out");
W
wanghaoshuang 已提交
68
    auto x_dims = x->dims();
W
wanghaoshuang 已提交
69
    auto* y = context.Input<LoDTensor>("Y");
Q
Qiao Longfei 已提交
70
    PADDLE_ENFORCE(!y->lod().empty(), "y should have lod");
71 72
    PADDLE_ENFORCE_EQ(static_cast<size_t>(x_dims[0]),
                      y->lod().back().size() - 1,
W
wanghaoshuang 已提交
73 74 75
                      "The size of last lod level in Input(Y)"
                      "must be equal to dims[0] of Input(X).");
    out->set_lod(y->lod());
D
dzhwinter 已提交
76 77
    SequenceExpandFunctor<DeviceContext, T> functor;
    functor(context.template device_context<DeviceContext>(), *x, out);
W
wanghaoshuang 已提交
78 79 80
  }
};

81 82 83 84 85 86 87 88 89 90 91 92
/*
 *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
 *
 * */
D
dzhwinter 已提交
93 94 95 96 97 98
template <typename T>
struct SequenceExpandGradFunctor<platform::CPUDeviceContext, T> {
  void operator()(const platform::CPUDeviceContext& ctx, const LoDTensor& x,
                  const LoDTensor& out, const LoDTensor& dout, LoDTensor* dx) {
    auto out_last_level = out.lod().back();
    const T* d_out_data = d_out.data<T>();
W
wanghaoshuang 已提交
99
    T* d_x_data = d_x->mutable_data<T>(context.GetPlace());
D
dzhwinter 已提交
100
    size_t element_len = d_out.numel() / d_out.dims()[0];
W
wanghaoshuang 已提交
101 102 103 104 105 106 107
    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));
D
dzhwinter 已提交
108 109
      d_x_t.device(*context.eigen_device()) =
          d_out_t.sum(Eigen::array<int, 1>({{0}}));
W
wanghaoshuang 已提交
110 111
      d_out_data += (repeat * element_len);
      d_x_data += element_len;
W
wanghaoshuang 已提交
112
    }
W
wanghaoshuang 已提交
113 114 115
  }
};

D
dzhwinter 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129
template <typename DeviceContext, typename T>
class SequenceExpandGradKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* d_out = context.Input<LoDTensor>(framework::GradVarName("Out"));
    auto* x = context.Input<LoDTensor>("X");
    auto* out = context.Input<LoDTensor>("Out");
    auto* d_x = context.Output<LoDTensor>(framework::GradVarName("X"));
    d_x->set_lod(x->lod());
    SequenceExpandGradFunctor(context.template device_context(), *x, *out,
                              d_out, d_x);
  }
};

W
wanghaoshuang 已提交
130 131
}  // namespace operators
}  // namespace paddle