sequence_conv_op.h 7.4 KB
Newer Older
C
chengduoZH 已提交
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/eigen.h"
#include "paddle/framework/op_registry.h"
C
chengduoZH 已提交
18
#include "paddle/operators/math/math_function.h"
C
chengduoZH 已提交
19
#include "paddle/operators/math/sequence_project.h"
C
chengduoZH 已提交
20 21 22 23 24 25

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using LoDTensor = framework::LoDTensor;
C
chengduoZH 已提交
26 27 28
// template <typename T, int MajorType = Eigen::RowMajor,
//          typename IndexType = Eigen::DenseIndex>
// using EigenVector = framework::EigenVector<T, MajorType, IndexType>;
C
chengduoZH 已提交
29 30 31 32 33
template <typename T, int MajorType = Eigen::RowMajor,
          typename IndexType = Eigen::DenseIndex>
using EigenMatrix = framework::EigenMatrix<T, MajorType, IndexType>;

template <typename Place, typename T>
C
chengduoZH 已提交
34
class SequenceConvKernel : public framework::OpKernel<T> {
C
chengduoZH 已提交
35 36 37 38
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* in = context.Input<LoDTensor>("X");
    auto* out = context.Output<LoDTensor>("Out");
C
chengduoZH 已提交
39
    auto filter = *context.Input<LoDTensor>("Filter");
40

C
chengduoZH 已提交
41
    out->mutable_data<T>(context.GetPlace());
C
chengduoZH 已提交
42
    //  out->set_lod(in->lod());
C
chengduoZH 已提交
43 44 45 46

    int context_start = context.Attr<int>("context_start");
    int context_length = context.Attr<int>("context_length");
    int context_stride = context.Attr<int>("context_stride");
C
chengduoZH 已提交
47
    bool padding_trainable = context.Attr<bool>("padding_trainable");
C
chengduoZH 已提交
48 49 50 51 52 53 54 55 56 57 58 59

    // InferShape by in_lod
    PADDLE_ENFORCE_EQ(in->lod().size(), 1UL,
                      "Only support one level sequence now.");

    const LoDTensor* padding_data = nullptr;
    if (padding_trainable) {
      padding_data = context.Input<LoDTensor>("PaddingData");
    }

    int up_pad = std::max(0, -context_start);
    int down_pad = std::max(0, context_start + context_length - 1);
C
chengduoZH 已提交
60
    int sequence_width;
C
chengduoZH 已提交
61
    sequence_width = static_cast<int>(in->dims()[1]);
C
chengduoZH 已提交
62

C
chengduoZH 已提交
63 64 65 66 67 68 69 70 71
    // use col_shape in the im2col calculation
    framework::DDim col_shape = {in->dims()[0],
                                 sequence_width * context_length};
    LoDTensor col;
    col.mutable_data<T>(col_shape, context.GetPlace());
    // Because if padding_trainable is false, padding data should be zeros.
    auto temp = framework::EigenVector<T>::Flatten(col);
    temp.device(context.GetEigenDevice<Place>()) =
        temp.constant(static_cast<T>(0));
72

C
chengduoZH 已提交
73 74
    paddle::operators::math::SequenceProjectFunctor<Place, T>
        seq_project_functor;
C
chengduoZH 已提交
75 76
    LoDTensor* input = const_cast<LoDTensor*>(in);
    LoDTensor* pad_data = const_cast<LoDTensor*>(padding_data);
77

C
chengduoZH 已提交
78
    seq_project_functor(context.device_context(), *input, *pad_data, col,
C
chengduoZH 已提交
79
                        padding_trainable, context_start, context_length,
C
chengduoZH 已提交
80
                        context_stride, up_pad, down_pad, false, false, false);
81

C
chengduoZH 已提交
82 83 84
    filter.Resize(framework::make_ddim({context_length * sequence_width, 1}));
    math::matmul<Place, T>(context.device_context(), col, false, filter, false,
                           T(1.0), out, T(0.0));
C
chengduoZH 已提交
85 86 87 88
  }
};

template <typename Place, typename T>
C
chengduoZH 已提交
89
class SequenceConvGradKernel : public framework::OpKernel<T> {
C
chengduoZH 已提交
90 91 92 93
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* out_g = context.Input<LoDTensor>(framework::GradVarName("Out"));
    auto* in_g = context.Output<LoDTensor>(framework::GradVarName("X"));
C
chengduoZH 已提交
94 95
    auto* filter_g =
        context.Output<LoDTensor>(framework::GradVarName("Filter"));
C
chengduoZH 已提交
96 97
    auto* padding_data_g =
        context.Output<LoDTensor>(framework::GradVarName("PaddingData"));
98
    auto* in = context.Input<LoDTensor>("X");
C
chengduoZH 已提交
99 100
    auto* filter = context.Input<LoDTensor>("Filter");

C
chengduoZH 已提交
101 102
    int context_start = context.Attr<int>("context_start");
    int context_length = context.Attr<int>("context_length");
103
    int context_stride = context.Attr<int>("context_stride");
C
chengduoZH 已提交
104
    bool padding_trainable = context.Attr<bool>("padding_trainable");
C
chengduoZH 已提交
105 106

    // InferShape by in_lod
107
    PADDLE_ENFORCE_EQ(in->lod().size(), 1UL,
C
chengduoZH 已提交
108
                      "Only support one level sequence now.");
109
    auto lod_g_level_0 = in->lod()[0];
C
chengduoZH 已提交
110

C
chengduoZH 已提交
111 112
    int up_pad = std::max(0, -context_start);
    int down_pad = std::max(0, context_start + context_length - 1);
C
chengduoZH 已提交
113
    int sequence_width = static_cast<int>(in->dims()[1]);
C
chengduoZH 已提交
114

C
chengduoZH 已提交
115 116 117 118 119 120 121 122 123 124 125
    // use col_shape in the im2col calculation
    framework::DDim col_shape = {in->dims()[0],
                                 sequence_width * context_length};
    LoDTensor col;

    if (in_g || filter_g || (padding_trainable && padding_data_g)) {
      col.mutable_data<T>(col_shape, context.GetPlace());
      // Because if padding_trainable is false, padding data should be zeros.
      auto temp = framework::EigenVector<T>::Flatten(col);
      temp.device(context.GetEigenDevice<Place>()) =
          temp.constant(static_cast<T>(0));
C
chengduoZH 已提交
126

C
chengduoZH 已提交
127 128 129
      math::matmul<Place, T>(context.device_context(), *out_g, false, *filter,
                             true, T(1.0), &col, T(1.0));
    }
C
chengduoZH 已提交
130 131
    paddle::operators::math::SequenceProjectFunctor<Place, T>
        seq_project_functor;
C
chengduoZH 已提交
132

C
chengduoZH 已提交
133 134
    if (in_g) {
      in_g->mutable_data<T>(context.GetPlace());
C
chengduoZH 已提交
135
      in_g->set_lod(in->lod());
C
chengduoZH 已提交
136

C
chengduoZH 已提交
137 138
      math::SetConstant<Place, T> functor;
      functor(context.device_context(), in_g, 0);
139

C
chengduoZH 已提交
140 141 142
      seq_project_functor(context.device_context(), *in_g, *padding_data_g, col,
                          padding_trainable, context_start, context_length,
                          context_stride, up_pad, down_pad, true, true, false);
C
chengduoZH 已提交
143 144 145 146
    }

    if (padding_trainable && padding_data_g) {
      padding_data_g->mutable_data<T>(context.GetPlace());
C
chengduoZH 已提交
147

C
chengduoZH 已提交
148 149 150
      math::SetConstant<Place, T> functor;
      functor(context.device_context(), padding_data_g, 0);

C
chengduoZH 已提交
151 152 153 154
      LoDTensor* input = const_cast<LoDTensor*>(in);
      seq_project_functor(context.device_context(), *input, *padding_data_g,
                          col, padding_trainable, context_start, context_length,
                          context_stride, up_pad, down_pad, true, false, true);
C
chengduoZH 已提交
155
    }
C
chengduoZH 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

    if (filter_g) {
      filter_g->mutable_data<T>(context.GetPlace());

      math::SetConstant<Place, T> functor;
      functor(context.device_context(), filter_g, 0);

      Tensor filter_grad_ = *filter_g;
      Tensor out_grad_ = *out_g;

      const LoDTensor* padding_data = nullptr;
      if (padding_trainable) {
        padding_data = context.Input<LoDTensor>("PaddingData");
      }

      sequence_width = static_cast<int>(in->dims()[1]);

C
chengduoZH 已提交
173 174
      LoDTensor* input = const_cast<LoDTensor*>(in);
      LoDTensor* pad_data = const_cast<LoDTensor*>(padding_data);
C
chengduoZH 已提交
175

C
chengduoZH 已提交
176
      seq_project_functor(context.device_context(), *input, *pad_data, col,
C
chengduoZH 已提交
177
                          padding_trainable, context_start, context_length,
C
chengduoZH 已提交
178 179
                          context_stride, up_pad, down_pad, false, false,
                          false);
C
chengduoZH 已提交
180 181 182 183 184 185 186

      filter_grad_.Resize(
          framework::make_ddim({context_length * sequence_width, 1}));

      math::matmul<Place, T>(context.device_context(), col, true, out_grad_,
                             false, T(1.0), &filter_grad_, T(1.0));
    }
C
chengduoZH 已提交
187 188 189 190 191
  }
};

}  // namespace operators
}  // namespace paddle