block_expand_op.h 5.5 KB
Newer Older
G
gongweibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* 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/operators/math/math_function.h"

#include "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
G
gongweibao 已提交
21
#include "paddle/operators/math/im2col.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
inline int get_output_size(int img_size, int block_size, int stride,
                           int padding) {
  return (1 + (img_size + 2 * padding - block_size + stride - 1) / stride);
G
gongweibao 已提交
32 33
}

W
wanghaoshuang 已提交
34
template <typename DeviceContext, typename T>
G
gongweibao 已提交
35 36
class BlockExpandKernel : public framework::OpKernel<T> {
 public:
G
gongweibao 已提交
37
  void Compute(const framework::ExecutionContext& ctx) const override {
G
gongweibao 已提交
38
    const Tensor* in = ctx.Input<Tensor>("X");
W
wanghaoshuang 已提交
39
    LoDTensor* out = ctx.Output<LoDTensor>("Out");
G
gongweibao 已提交
40 41 42
    out->mutable_data<T>(ctx.GetPlace());

    auto in_dim = in->dims();
W
wanghaoshuang 已提交
43 44
    int batch_size = in_dim[0];
    int img_channels = in_dim[1];
G
gongweibao 已提交
45 46
    int img_height = in_dim[2];
    int img_width = in_dim[3];
W
wanghaoshuang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    int block_height = ctx.Attr<int>("block_height");
    int block_width = ctx.Attr<int>("block_width");
    int stride_height = ctx.Attr<int>("stride_height");
    int stride_width = ctx.Attr<int>("stride_width");
    int padding_height = ctx.Attr<int>("padding_height");
    int padding_width = ctx.Attr<int>("padding_width");

    int output_height = get_output_size(img_height, block_height, stride_height,
                                        padding_height);
    int output_width =
        get_output_size(img_width, block_width, stride_width, padding_width);

    const std::vector<int> dilations({1, 1});
    const std::vector<int> strides(
        {stride_height, stride_width, stride_height, stride_width});
    const std::vector<int> paddings(
        {padding_height, padding_width, padding_height, padding_width});

    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});
      Tensor dst = out->Slice(i, i + 1).Resize({output_height, output_width,
                                                img_channels, block_height,
                                                block_width});
G
gongweibao 已提交
73

W
wanghaoshuang 已提交
74 75 76
      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 已提交
77
    }
W
wanghaoshuang 已提交
78 79 80 81 82 83 84 85 86 87
    out->Resize(out_dims);

    // set lod information
    // TODO(wanghaoshuang): Move this to InferShape
    framework::LoD lod(1);
    for (int i = 0, offset = 0; i < batch_size + 1; ++i) {
      lod[0].push_back(offset);
      offset += output_height * output_width;
    }
    out->set_lod(lod);
G
gongweibao 已提交
88 89 90
  }
};

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

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

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

W
wanghaoshuang 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    int block_height = ctx.Attr<int>("block_height");
    int block_width = ctx.Attr<int>("block_width");
    int stride_height = ctx.Attr<int>("stride_height");
    int stride_width = ctx.Attr<int>("stride_width");
    int padding_height = ctx.Attr<int>("padding_height");
    int padding_width = ctx.Attr<int>("padding_width");
    int output_height = get_output_size(img_height, block_height, stride_height,
                                        padding_height);
    int output_width =
        get_output_size(img_width, block_width, stride_width, padding_width);

    const std::vector<int> dilations({1, 1});
    const std::vector<int> strides(
        {stride_height, stride_width, stride_height, stride_width});
    const std::vector<int> paddings(
        {padding_height, padding_width, padding_height, padding_width});

    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(
          {output_height, output_width, img_channels, block_height,
           block_width});
W
wanghaoshuang 已提交
136 137 138
      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 已提交
139
    }
W
wanghaoshuang 已提交
140
    d_out->Resize(d_out_dims);
G
gongweibao 已提交
141 142 143 144 145
  }
};

}  // namespace operators
}  // namespace paddle