block_expand_op.h 4.8 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 {

G
gongweibao 已提交
26 27 28 29
inline void get_blockexpand_output_shape(int img_height, int img_width,
                                         int block_height, int block_width,
                                         int stride_height, int stride_width,
                                         int padding_height, int padding_width,
G
gongweibao 已提交
30 31 32
                                         int& outputHeight, int& outputWidth) {
  outputHeight =
      1 +
G
gongweibao 已提交
33 34
      (img_height + 2 * padding_height - block_height + stride_height - 1) /
          stride_height;
G
gongweibao 已提交
35

G
gongweibao 已提交
36 37 38 39
  outputWidth =
      1 +
      (img_width + 2 * padding_width - block_width + stride_width - 1) /
          stride_width;
G
gongweibao 已提交
40 41
}

G
gongweibao 已提交
42 43 44
template <typename Place, typename T>
class BlockExpandKernel : public framework::OpKernel<T> {
 public:
G
gongweibao 已提交
45
  void Compute(const framework::ExecutionContext& ctx) const override {
G
gongweibao 已提交
46
    using namespace framework;
G
gongweibao 已提交
47
    const Tensor* in = ctx.Input<Tensor>("X");
G
gongweibao 已提交
48 49 50 51 52 53
    Tensor* out = ctx.Output<Tensor>("Out");
    out->mutable_data<T>(ctx.GetPlace());

    auto in_dim = in->dims();
    int N = in_dim[0];
    int C = in_dim[1];
G
gongweibao 已提交
54 55
    int img_height = in_dim[2];
    int img_width = in_dim[3];
G
gongweibao 已提交
56

G
gongweibao 已提交
57 58 59 60 61 62
    int block_height = ctx.Attr<int>("blockHeight");
    int block_width = ctx.Attr<int>("blockWidth");
    int stride_height = ctx.Attr<int>("strideHeight");
    int stride_width = ctx.Attr<int>("strideWidth");
    int padding_height = ctx.Attr<int>("paddingHeight");
    int padding_width = ctx.Attr<int>("paddingWidth");
G
gongweibao 已提交
63 64 65 66

    int outputHeight = 0;
    int outputWidth = 0;

G
gongweibao 已提交
67 68 69
    get_blockexpand_output_shape(
        img_height, img_width, block_height, block_width, stride_height,
        stride_width, padding_height, padding_width, outputHeight, outputWidth);
G
gongweibao 已提交
70

G
gongweibao 已提交
71 72 73
    std::vector<int> stride({stride_height, stride_width});
    std::vector<int> padding({padding_height, padding_width});

G
gongweibao 已提交
74
    for (int i = 0; i < N; i++) {
G
gongweibao 已提交
75 76
      Tensor src = in->Slice(i, i + 1).Resize({C, img_height, img_width});
      Tensor dst = out->Slice(i, i + 1).Resize(
G
add gpu  
gongweibao 已提交
77
          {outputHeight, outputWidth, C, block_height, block_width});
G
gongweibao 已提交
78

G
add gpu  
gongweibao 已提交
79
      math::Im2ColFunctor<math::ColFormat::kOCF, Place, T> f;
G
gongweibao 已提交
80
      f(ctx.device_context(), src, stride, padding, &dst);
G
gongweibao 已提交
81 82 83 84 85 86 87 88
    }
  }
};

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

    auto x_v = framework::EigenVector<T>::Flatten(*d_x);
    x_v.device(ctx.GetEigenDevice<Place>()) = x_v.constant(0.0);
G
add gpu  
gongweibao 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

    auto in_dim = in->dims();
    int N = in_dim[0];
    int C = in_dim[1];
    int img_height = in_dim[2];
    int img_width = in_dim[3];

    int block_height = ctx.Attr<int>("blockHeight");
    int block_width = ctx.Attr<int>("blockWidth");
    int stride_height = ctx.Attr<int>("strideHeight");
    int stride_width = ctx.Attr<int>("strideWidth");
    int padding_height = ctx.Attr<int>("paddingHeight");
    int padding_width = ctx.Attr<int>("paddingWidth");

    int outputHeight = 0;
    int outputWidth = 0;

    get_blockexpand_output_shape(
        img_height, img_width, block_height, block_width, stride_height,
        stride_width, padding_height, padding_width, outputHeight, outputWidth);

G
gongweibao 已提交
118 119 120 121
    std::vector<int> stride({stride_height, stride_width});
    std::vector<int> padding({padding_height, padding_width});
    // std::vector<int> stride({stride_height, stride_width});

G
add gpu  
gongweibao 已提交
122
    for (int i = 0; i < N; i++) {
G
gongweibao 已提交
123 124
      Tensor dst = d_x->Slice(i, i + 1).Resize({C, img_height, img_width});
      Tensor src = d_out->Slice(i, i + 1).Resize(
G
add gpu  
gongweibao 已提交
125
          {outputHeight, outputWidth, C, block_height, block_width});
G
gongweibao 已提交
126
      math::Col2ImFunctor<math::ColFormat::kOCF, Place, T> f;
G
gongweibao 已提交
127
      f(ctx.device_context(), dst, stride, padding, &src);
G
add gpu  
gongweibao 已提交
128
    }
G
gongweibao 已提交
129 130 131 132 133
  }
};

}  // namespace operators
}  // namespace paddle