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<Tensor>("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

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

C
chengduoZH 已提交
53
    const Tensor* padding_data = nullptr;
C
chengduoZH 已提交
54
    if (padding_trainable) {
C
chengduoZH 已提交
55
      padding_data = context.Input<Tensor>("PaddingData");
C
chengduoZH 已提交
56 57 58 59
    }

    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
    // use col_shape in the im2col calculation
    framework::DDim col_shape = {in->dims()[0],
                                 sequence_width * context_length};
C
chengduoZH 已提交
66
    Tensor col;
C
chengduoZH 已提交
67 68 69 70 71
    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
    LoDTensor* input = const_cast<LoDTensor*>(in);
C
chengduoZH 已提交
76
    Tensor* pad_data = const_cast<Tensor*>(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
    auto* filter_g = context.Output<Tensor>(framework::GradVarName("Filter"));
C
chengduoZH 已提交
95
    auto* padding_data_g =
C
chengduoZH 已提交
96
        context.Output<Tensor>(framework::GradVarName("PaddingData"));
97
    auto* in = context.Input<LoDTensor>("X");
C
chengduoZH 已提交
98
    auto* filter = context.Input<Tensor>("Filter");
C
chengduoZH 已提交
99

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

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

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

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

    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 已提交
125

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

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

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

C
chengduoZH 已提交
139 140 141
      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 已提交
142 143 144 145
    }

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

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

C
chengduoZH 已提交
150 151 152 153
      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 已提交
154
    }
C
chengduoZH 已提交
155 156 157 158 159 160 161 162

    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;
C
chengduoZH 已提交
163
      LoDTensor out_grad_ = *out_g;
C
chengduoZH 已提交
164

C
chengduoZH 已提交
165
      const Tensor* padding_data = nullptr;
C
chengduoZH 已提交
166
      if (padding_trainable) {
C
chengduoZH 已提交
167
        padding_data = context.Input<Tensor>("PaddingData");
C
chengduoZH 已提交
168 169 170 171
      }

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

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

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

      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 已提交
186 187 188 189 190
  }
};

}  // namespace operators
}  // namespace paddle