sequence_expand_op.h 4.7 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"
W
wanghaoshuang 已提交
19
#include "unsupported/Eigen/CXX11/Tensor"
W
wanghaoshuang 已提交
20 21 22 23 24 25

namespace paddle {
namespace operators {

using LoDTensor = framework::LoDTensor;

Q
QI JUN 已提交
26
template <typename DeviceContext, typename T>
W
wanghaoshuang 已提交
27
class SequenceExpandKernel : public framework::OpKernel<T> {
W
wanghaoshuang 已提交
28 29 30
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* x = context.Input<LoDTensor>("X");
W
wanghaoshuang 已提交
31
    auto* y = context.Input<LoDTensor>("Y");
Y
yangyaming 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    auto* out = context.Output<LoDTensor>("Out");
    int ref_level = context.Attr<int>("ref_level");

    auto& x_lod = x->lod();
    auto& y_lod = y->lod();

    PADDLE_ENFORCE_GE(ref_level, 0,
                      "Value of attribute `ref_level` should be greater or "
                      "equal to 0.");

    PADDLE_ENFORCE_LT(ref_level, y_lod.size(),
                      "Value of attribute `ref_level` should be smaller than "
                      "level number of Y's lod.");

    if (y_lod[ref_level].size() < 1) {
      framework::TensorCopy(*x, context.GetPlace(), out);
      return;
    }
W
wanghaoshuang 已提交
50

Y
yangyaming 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    if (x_lod.size() == 0) {
      int out_start = 0;
      for (size_t i = 1; i < y_lod[ref_level].size(); ++i) {
        int repeat_num = y_lod[ref_level][i] - y_lod[ref_level][i - 1];
        auto x_sub_tensor = x->Slice(i - 1, i);
        for (size_t j = 0; j < repeat_num; ++j) {
          auto out_sub_tensor = out->Slice(out_start, out_start + 1);
          framework::TensorCopy(x_sub_tensor, context.GetPlace(),
                                &out_sub_tensor);
          out_start++;
        }
      }
    } else {
      auto& out_lod = *out->mutable_lod();
      out_lod.resize(1);
      out_lod[0].resize(1);
      out_lod[0][0] = 0;
      int out_idx = 0;
      for (size_t i = 1; i < y_lod[ref_level].size(); ++i) {
        int repeat_num = y_lod[ref_level][i] - y_lod[ref_level][i - 1];
        int x_seq_len = x_lod[0][i] - x_lod[0][i - 1];
        auto x_sub_tensor = x->Slice(x_lod[0][i], x_lod[0][i - 1]);
        for (size_t j = 0; j < repeat_num; ++j) {
          auto out_sub_tensor =
              out->Slice(out_lod[0][out_idx], out_lod[0][out_idx] + x_seq_len);
          framework::TensorCopy(x_sub_tensor, context.GetPlace(),
                                &out_sub_tensor);
          out_lod[0].push_back(out_lod[0][out_idx] + x_seq_len);
          out_idx++;
        }
      }
W
wanghaoshuang 已提交
82
    }
W
wanghaoshuang 已提交
83 84 85
  }
};

86 87 88 89 90 91 92 93 94 95 96 97
/*
 *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
 *
 * */
Q
QI JUN 已提交
98
template <typename DeviceContext, typename T>
W
wanghaoshuang 已提交
99
class SequenceExpandGradKernel : public framework::OpKernel<T> {
W
wanghaoshuang 已提交
100 101
 public:
  void Compute(const framework::ExecutionContext& context) const override {
W
wanghaoshuang 已提交
102 103 104
    auto* d_out = context.Input<LoDTensor>(framework::GradVarName("Out"));
    auto* x = context.Input<LoDTensor>("X");
    auto* out = context.Input<LoDTensor>("Out");
W
wanghaoshuang 已提交
105
    auto* d_x = context.Output<LoDTensor>(framework::GradVarName("X"));
W
wanghaoshuang 已提交
106
    auto out_last_level = out->lod().back();
W
wanghaoshuang 已提交
107 108 109
    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());
110
    size_t element_len = d_out->numel() / d_out->dims()[0];
W
wanghaoshuang 已提交
111 112 113 114 115 116 117
    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));
Q
QI JUN 已提交
118 119 120
      auto place =
          context.template device_context<DeviceContext>().eigen_device();
      d_x_t.device(*place) = d_out_t.sum(Eigen::array<int, 1>({{0}}));
W
wanghaoshuang 已提交
121 122
      d_out_data += (repeat * element_len);
      d_x_data += element_len;
W
wanghaoshuang 已提交
123
    }
W
wanghaoshuang 已提交
124 125 126 127 128
  }
};

}  // namespace operators
}  // namespace paddle