im2sequence_op.h 5.1 KB
Newer Older
G
gongweibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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

17
#include "paddle/framework/data_layout.h"
G
gongweibao 已提交
18 19
#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
G
gongweibao 已提交
20
#include "paddle/operators/math/im2col.h"
21
#include "paddle/operators/math/math_function.h"
G
gongweibao 已提交
22 23 24 25

namespace paddle {
namespace operators {

W
wanghaoshuang 已提交
26 27 28
using Tensor = framework::Tensor;
using LoDTensor = framework::LoDTensor;

W
wanghaoshuang 已提交
29 30 31 32 33
inline int OutputSize(int input_size, int filter_size, int padding_0,
                      int padding_1, int stride) {
  const int output_size =
      (input_size + padding_0 + padding_1 - filter_size) / stride + 1;
  return output_size;
G
gongweibao 已提交
34 35
}

W
wanghaoshuang 已提交
36
template <typename DeviceContext, typename T>
37
class Im2SequenceKernel : public framework::OpKernel<T> {
G
gongweibao 已提交
38
 public:
G
gongweibao 已提交
39
  void Compute(const framework::ExecutionContext& ctx) const override {
G
gongweibao 已提交
40
    const Tensor* in = ctx.Input<Tensor>("X");
W
wanghaoshuang 已提交
41
    LoDTensor* out = ctx.Output<LoDTensor>("Out");
G
gongweibao 已提交
42
    out->mutable_data<T>(ctx.GetPlace());
43 44 45 46
    // TODO(wanghaoshuang): Add layout checker after 'set_layout'
    // being available for python API
    // PADDLE_ENFORCE_EQ(in->layout(), framework::DataLayout::kNCHW,
    //                  "Input(X) layout must be NCHW");
G
gongweibao 已提交
47
    auto in_dim = in->dims();
W
wanghaoshuang 已提交
48 49
    int batch_size = in_dim[0];
    int img_channels = in_dim[1];
G
gongweibao 已提交
50 51
    int img_height = in_dim[2];
    int img_width = in_dim[3];
W
wanghaoshuang 已提交
52

W
wanghaoshuang 已提交
53 54 55 56 57
    auto kernels = ctx.Attr<std::vector<int>>("kernels");
    auto strides = ctx.Attr<std::vector<int>>("strides");
    auto paddings = ctx.Attr<std::vector<int>>("paddings");
    int output_height = OutputSize(img_height, kernels[0], paddings[0],
                                   paddings[2], strides[0]);
W
wanghaoshuang 已提交
58
    int output_width =
W
wanghaoshuang 已提交
59
        OutputSize(img_width, kernels[1], paddings[1], paddings[3], strides[1]);
W
wanghaoshuang 已提交
60 61 62 63 64 65 66 67

    const std::vector<int> dilations({1, 1});

    auto out_dims = out->dims();
    out->Resize({batch_size, out->numel() / batch_size});
    for (int i = 0; i < batch_size; i++) {
      const Tensor src =
          in->Slice(i, i + 1).Resize({img_channels, img_height, img_width});
W
wanghaoshuang 已提交
68 69
      Tensor dst = out->Slice(i, i + 1).Resize(
          {output_height, output_width, img_channels, kernels[0], kernels[1]});
G
gongweibao 已提交
70

W
wanghaoshuang 已提交
71 72 73
      math::Im2ColFunctor<math::ColFormat::kOCF, DeviceContext, T> f;
      auto& dev_ctx = ctx.template device_context<DeviceContext>();
      f(dev_ctx, src, dilations, strides, paddings, &dst);
G
gongweibao 已提交
74
    }
W
wanghaoshuang 已提交
75 76 77 78 79
    out->Resize(out_dims);

    // set lod information
    // TODO(wanghaoshuang): Move this to InferShape
    framework::LoD lod(1);
80
    lod[0].reserve(batch_size + 1);
W
wanghaoshuang 已提交
81
    for (int i = 0, offset = 0; i < batch_size + 1; ++i) {
W
wanghaoshuang 已提交
82
      lod[0].push_back(offset);
W
wanghaoshuang 已提交
83 84 85
      offset += output_height * output_width;
    }
    out->set_lod(lod);
G
gongweibao 已提交
86 87 88
  }
};

W
wanghaoshuang 已提交
89
template <typename DeviceContext, typename T>
90
class Im2SequenceGradKernel : public framework::OpKernel<T> {
G
gongweibao 已提交
91 92
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
G
add gpu  
gongweibao 已提交
93
    auto* in = ctx.Input<Tensor>("X");
W
wanghaoshuang 已提交
94 95
    Tensor* d_out =
        const_cast<Tensor*>(ctx.Input<Tensor>(framework::GradVarName("Out")));
W
wanghaoshuang 已提交
96
    auto* d_x = ctx.Output<Tensor>(framework::GradVarName("X"));
G
gongweibao 已提交
97 98 99
    d_x->mutable_data<T>(ctx.GetPlace());

    auto x_v = framework::EigenVector<T>::Flatten(*d_x);
W
wanghaoshuang 已提交
100 101
    auto& place = *ctx.template device_context<DeviceContext>().eigen_device();
    x_v.device(place) = x_v.constant(0.0);
G
add gpu  
gongweibao 已提交
102 103

    auto in_dim = in->dims();
W
wanghaoshuang 已提交
104 105
    int batch_size = in_dim[0];
    int img_channels = in_dim[1];
G
add gpu  
gongweibao 已提交
106 107 108
    int img_height = in_dim[2];
    int img_width = in_dim[3];

W
wanghaoshuang 已提交
109 110 111
    auto kernels = ctx.Attr<std::vector<int>>("kernels");
    auto strides = ctx.Attr<std::vector<int>>("strides");
    auto paddings = ctx.Attr<std::vector<int>>("paddings");
W
wanghaoshuang 已提交
112 113
    int output_height = OutputSize(img_height, kernels[0], paddings[0],
                                   paddings[2], strides[0]);
W
wanghaoshuang 已提交
114
    int output_width =
W
wanghaoshuang 已提交
115
        OutputSize(img_width, kernels[1], paddings[1], paddings[3], strides[1]);
W
wanghaoshuang 已提交
116 117 118 119 120 121 122 123 124

    const std::vector<int> dilations({1, 1});

    auto d_out_dims = d_out->dims();
    d_out->Resize({batch_size, d_out->numel() / batch_size});
    for (int i = 0; i < batch_size; i++) {
      Tensor dst =
          d_x->Slice(i, i + 1).Resize({img_channels, img_height, img_width});
      const Tensor src = d_out->Slice(i, i + 1).Resize(
W
wanghaoshuang 已提交
125
          {output_height, output_width, img_channels, kernels[0], kernels[1]});
W
wanghaoshuang 已提交
126 127 128
      math::Col2ImFunctor<math::ColFormat::kOCF, DeviceContext, T> f;
      auto& dev_ctx = ctx.template device_context<DeviceContext>();
      f(dev_ctx, src, dilations, strides, paddings, &dst);
G
add gpu  
gongweibao 已提交
129
    }
W
wanghaoshuang 已提交
130
    d_out->Resize(d_out_dims);
G
gongweibao 已提交
131 132 133 134 135
  }
};

}  // namespace operators
}  // namespace paddle