sequence_conv_op.h 6.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
C
chengduoZH 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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
16
#include <algorithm>
Y
Yi Wang 已提交
17 18 19
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math/context_project.h"
#include "paddle/fluid/operators/math/math_function.h"
C
chengduoZH 已提交
20 21 22 23 24 25 26

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
using LoDTensor = framework::LoDTensor;

Q
QI JUN 已提交
27
template <typename DeviceContext, typename T>
C
chengduoZH 已提交
28
class SequenceConvKernel : public framework::OpKernel<T> {
C
chengduoZH 已提交
29 30 31 32
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* in = context.Input<LoDTensor>("X");
    auto* out = context.Output<LoDTensor>("Out");
C
chengduoZH 已提交
33
    auto filter = *context.Input<Tensor>("Filter");
34

C
chengduoZH 已提交
35
    out->mutable_data<T>(context.GetPlace());
C
chengduoZH 已提交
36

C
chengduoZH 已提交
37 38 39 40
    int context_start = context.Attr<int>("contextStart");
    int context_length = context.Attr<int>("contextLength");
    int context_stride = context.Attr<int>("contextStride");
    bool padding_trainable = context.Attr<bool>("paddingTrainable");
C
chengduoZH 已提交
41

42 43
    PADDLE_ENFORCE_EQ(
        in->lod().empty(), false,
44 45 46 47 48 49 50 51
        platform::errors::InvalidArgument("Input(X) Tensor of SequenceConvOp "
                                          "does not contain LoD information."));
    PADDLE_ENFORCE_EQ(
        in->lod().size(), 1UL,
        platform::errors::InvalidArgument(
            "Only support input sequence with lod level equal to 1 at "
            "present. But received: lod level %u.",
            in->lod().size()));
C
chengduoZH 已提交
52

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);
60
    auto sequence_width = static_cast<int64_t>(in->dims()[1]);
C
chengduoZH 已提交
61

C
chengduoZH 已提交
62
    framework::DDim col_shape = {in->dims()[0],
C
chengduoZH 已提交
63
                                 context_length * sequence_width};
C
chengduoZH 已提交
64
    Tensor col;
C
chengduoZH 已提交
65 66
    col.mutable_data<T>(col_shape, context.GetPlace());
    // Because if padding_trainable is false, padding data should be zeros.
Q
QI JUN 已提交
67 68
    math::SetConstant<DeviceContext, T> set_zero;
    auto& dev_ctx = context.template device_context<DeviceContext>();
Y
Yu Yang 已提交
69
    auto blas = math::GetBlas<DeviceContext, T>(dev_ctx);
Q
QI JUN 已提交
70 71
    set_zero(dev_ctx, &col, static_cast<T>(0));
    math::ContextProjectFunctor<DeviceContext, T> seq_project_functor;
72

73
    seq_project_functor(dev_ctx, *in, padding_data, padding_trainable,
Q
QI JUN 已提交
74 75
                        context_start, context_length, context_stride, up_pad,
                        down_pad, &col);
76

Y
Yu Yang 已提交
77
    blas.MatMul(col, filter, out);
C
chengduoZH 已提交
78 79 80
  }
};

Q
QI JUN 已提交
81
template <typename DeviceContext, typename T>
C
chengduoZH 已提交
82
class SequenceConvGradKernel : public framework::OpKernel<T> {
C
chengduoZH 已提交
83 84 85
 public:
  void Compute(const framework::ExecutionContext& context) const override {
    auto* in_g = context.Output<LoDTensor>(framework::GradVarName("X"));
C
chengduoZH 已提交
86
    auto* out_g = context.Input<LoDTensor>(framework::GradVarName("Out"));
C
chengduoZH 已提交
87
    auto* filter_g = context.Output<Tensor>(framework::GradVarName("Filter"));
C
chengduoZH 已提交
88
    auto* padding_data_g =
C
chengduoZH 已提交
89
        context.Output<Tensor>(framework::GradVarName("PaddingData"));
90
    auto* in = context.Input<LoDTensor>("X");
C
chengduoZH 已提交
91
    auto* filter = context.Input<Tensor>("Filter");
C
chengduoZH 已提交
92

C
chengduoZH 已提交
93 94 95 96
    int context_start = context.Attr<int>("contextStart");
    int context_length = context.Attr<int>("contextLength");
    int context_stride = context.Attr<int>("contextStride");
    bool padding_trainable = context.Attr<bool>("paddingTrainable");
C
chengduoZH 已提交
97

98 99 100 101 102 103
    PADDLE_ENFORCE_EQ(
        in->lod().size(), 1UL,
        platform::errors::InvalidArgument(
            "Only support input sequence with lod level equal to 1 at "
            "present. But received: lod level %u.",
            in->lod().size()));
104
    auto lod_g_level_0 = in->lod()[0];
C
chengduoZH 已提交
105

C
chengduoZH 已提交
106 107
    int up_pad = std::max(0, -context_start);
    int down_pad = std::max(0, context_start + context_length - 1);
108
    auto sequence_width = static_cast<int64_t>(in->dims()[1]);
C
chengduoZH 已提交
109

Q
QI JUN 已提交
110 111
    math::SetConstant<DeviceContext, T> set_zero;
    auto& dev_ctx = context.template device_context<DeviceContext>();
Y
Yu Yang 已提交
112
    auto blas = math::GetBlas<DeviceContext, T>(dev_ctx);
C
chengduoZH 已提交
113 114 115
    // use col_shape in the im2col calculation
    framework::DDim col_shape = {in->dims()[0],
                                 sequence_width * context_length};
C
chengduoZH 已提交
116
    Tensor col;
C
chengduoZH 已提交
117 118 119 120

    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.
Q
QI JUN 已提交
121
      set_zero(dev_ctx, &col, static_cast<T>(0));
Y
Yu Yang 已提交
122
      blas.MatMul(*out_g, false, *filter, true, &col);
C
chengduoZH 已提交
123
    }
Q
QI JUN 已提交
124 125
    math::ContextProjectFunctor<DeviceContext, T> seq_project_functor;
    math::ContextProjectGradFunctor<DeviceContext, T> seq_project_grad_functor;
C
chengduoZH 已提交
126

C
chengduoZH 已提交
127 128
    if (in_g) {
      in_g->mutable_data<T>(context.GetPlace());
C
chengduoZH 已提交
129
      in_g->set_lod(in->lod());
Q
QI JUN 已提交
130
      set_zero(dev_ctx, in_g, static_cast<T>(0));
131

Q
QI JUN 已提交
132 133 134
      seq_project_grad_functor(dev_ctx, *in_g, padding_trainable, context_start,
                               context_length, context_stride, up_pad, down_pad,
                               false, true, padding_data_g, &col);
C
chengduoZH 已提交
135 136 137 138
    }

    if (padding_trainable && padding_data_g) {
      padding_data_g->mutable_data<T>(context.GetPlace());
Q
QI JUN 已提交
139
      set_zero(dev_ctx, padding_data_g, static_cast<T>(0));
C
chengduoZH 已提交
140

C
chengduoZH 已提交
141
      LoDTensor* input = const_cast<LoDTensor*>(in);
Q
QI JUN 已提交
142 143 144
      seq_project_grad_functor(
          dev_ctx, *input, padding_trainable, context_start, context_length,
          context_stride, up_pad, down_pad, true, false, padding_data_g, &col);
C
chengduoZH 已提交
145
    }
C
chengduoZH 已提交
146 147 148

    if (filter_g) {
      filter_g->mutable_data<T>(context.GetPlace());
Q
QI JUN 已提交
149
      set_zero(dev_ctx, filter_g, static_cast<T>(0));
C
chengduoZH 已提交
150

C
chengduoZH 已提交
151 152
      Tensor filter_grad = *filter_g;
      LoDTensor out_grad = *out_g;
C
chengduoZH 已提交
153

C
chengduoZH 已提交
154
      const Tensor* padding_data = nullptr;
C
chengduoZH 已提交
155
      if (padding_trainable) {
C
chengduoZH 已提交
156
        padding_data = context.Input<Tensor>("PaddingData");
C
chengduoZH 已提交
157 158
      }

159
      seq_project_functor(dev_ctx, *in, padding_data, padding_trainable,
Q
QI JUN 已提交
160 161
                          context_start, context_length, context_stride, up_pad,
                          down_pad, &col);
C
chengduoZH 已提交
162

Y
Yu Yang 已提交
163
      blas.MatMul(col, true, out_grad, false, &filter_grad);
C
chengduoZH 已提交
164
    }
C
chengduoZH 已提交
165 166 167 168 169
  }
};

}  // namespace operators
}  // namespace paddle