pool_op.h 7.1 KB
Newer Older
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/framework/eigen.h"
#include "paddle/framework/op_registry.h"
#include "paddle/operators/math/math_function.h"
#include "paddle/operators/math/pooling.h"
21
#include "paddle/platform/dynload/cudnn.h"
22 23 24 25 26

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
27 28 29 30 31 32

class PoolOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override;
33 34 35 36

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override;
37 38 39 40 41 42 43
};

class PoolOpGrad : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override;
44 45 46 47

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override;
48 49 50 51
};

class Pool2dOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
52
  Pool2dOpMaker(OpProto* proto, OpAttrChecker* op_checker);
53 54 55 56
};

class Pool3dOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
57
  Pool3dOpMaker(OpProto* proto, OpAttrChecker* op_checker);
58
};
59

Q
QI JUN 已提交
60
template <typename DeviceContext, typename T>
C
chengduoZH 已提交
61
class PoolKernel : public framework::OpKernel<T> {
62 63
 public:
  void Compute(const framework::ExecutionContext& context) const override {
C
chengduoZH 已提交
64
    const Tensor* in_x = context.Input<Tensor>("X");
65
    Tensor* out = context.Output<Tensor>("Out");
66

C
chengduoZH 已提交
67
    std::string pooling_type = context.Attr<std::string>("pooling_type");
68 69 70
    std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
    std::vector<int> strides = context.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");
C
chengduoZH 已提交
71
    if (context.Attr<bool>("global_pooling")) {
72
      for (size_t i = 0; i < ksize.size(); ++i) {
C
fix bug  
chengduoZH 已提交
73
        paddings[i] = 0;
C
chengduoZH 已提交
74
        ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
75 76
      }
    }
Q
QI JUN 已提交
77
    auto& dev_ctx = context.template device_context<DeviceContext>();
78 79 80
    switch (ksize.size()) {
      case 2: {
        if (pooling_type == "max") {
C
chengduoZH 已提交
81
          paddle::operators::math::Pool2dFunctor<
Q
QI JUN 已提交
82
              DeviceContext, paddle::operators::math::MaxPool<T>, T>
83
              pool2d_forward;
84
          paddle::operators::math::MaxPool<T> pool_process;
Q
QI JUN 已提交
85 86
          pool2d_forward(dev_ctx, *in_x, ksize, strides, paddings, pool_process,
                         out);
87

C
chengduoZH 已提交
88
        } else if (pooling_type == "avg") {
C
chengduoZH 已提交
89
          paddle::operators::math::Pool2dFunctor<
Q
QI JUN 已提交
90
              DeviceContext, paddle::operators::math::AvgPool<T>, T>
91
              pool2d_forward;
92
          paddle::operators::math::AvgPool<T> pool_process;
Q
QI JUN 已提交
93 94
          pool2d_forward(dev_ctx, *in_x, ksize, strides, paddings, pool_process,
                         out);
95 96 97 98
        }
      } break;
      case 3: {
        if (pooling_type == "max") {
C
chengduoZH 已提交
99
          paddle::operators::math::Pool3dFunctor<
Q
QI JUN 已提交
100
              DeviceContext, paddle::operators::math::MaxPool<T>, T>
101
              pool3d_forward;
102
          paddle::operators::math::MaxPool<T> pool_process;
Q
QI JUN 已提交
103 104
          pool3d_forward(dev_ctx, *in_x, ksize, strides, paddings, pool_process,
                         out);
C
chengduoZH 已提交
105
        } else if (pooling_type == "avg") {
C
chengduoZH 已提交
106
          paddle::operators::math::Pool3dFunctor<
Q
QI JUN 已提交
107
              DeviceContext, paddle::operators::math::AvgPool<T>, T>
108
              pool3d_forward;
109
          paddle::operators::math::AvgPool<T> pool_process;
Q
QI JUN 已提交
110 111
          pool3d_forward(dev_ctx, *in_x, ksize, strides, paddings, pool_process,
                         out);
112 113
        }
      } break;
C
fix bug  
chengduoZH 已提交
114
      default: { PADDLE_THROW("Pool op only supports 2D and 3D input."); }
115 116 117 118
    }
  }
};

Q
QI JUN 已提交
119
template <typename DeviceContext, typename T>
C
chengduoZH 已提交
120
class PoolGradKernel : public framework::OpKernel<T> {
121 122
 public:
  void Compute(const framework::ExecutionContext& context) const override {
C
chengduoZH 已提交
123
    const Tensor* in_x = context.Input<Tensor>("X");
124 125 126
    const Tensor* out = context.Input<Tensor>("Out");
    const Tensor* out_grad =
        context.Input<Tensor>(framework::GradVarName("Out"));
C
chengduoZH 已提交
127
    Tensor* in_x_grad = context.Output<Tensor>(framework::GradVarName("X"));
128

C
chengduoZH 已提交
129
    std::string pooling_type = context.Attr<std::string>("pooling_type");
130 131 132 133
    std::vector<int> ksize = context.Attr<std::vector<int>>("ksize");
    std::vector<int> strides = context.Attr<std::vector<int>>("strides");
    std::vector<int> paddings = context.Attr<std::vector<int>>("paddings");

C
chengduoZH 已提交
134
    if (context.Attr<bool>("global_pooling")) {
C
fix bug  
chengduoZH 已提交
135 136
      for (size_t i = 0; i < ksize.size(); ++i) {
        paddings[i] = 0;
C
chengduoZH 已提交
137
        ksize[i] = static_cast<int>(in_x->dims()[i + 2]);
C
fix bug  
chengduoZH 已提交
138
      }
139
    }
Q
QI JUN 已提交
140
    auto& dev_ctx = context.template device_context<DeviceContext>();
C
chengduoZH 已提交
141 142 143
    if (in_x_grad) {
      in_x_grad->mutable_data<T>(context.GetPlace());
      auto temp = framework::EigenVector<T>::Flatten(*in_x_grad);
Q
QI JUN 已提交
144 145
      temp.device(
          *context.template device_context<DeviceContext>().eigen_device()) =
146 147 148 149 150
          temp.constant(static_cast<T>(0));

      switch (ksize.size()) {
        case 2: {
          if (pooling_type == "max") {
Q
QI JUN 已提交
151
            paddle::operators::math::MaxPool2dGradFunctor<DeviceContext, T>
152
                pool2d_backward;
Q
QI JUN 已提交
153 154
            pool2d_backward(dev_ctx, *in_x, *out, *out_grad, ksize, strides,
                            paddings, in_x_grad);
C
chengduoZH 已提交
155
          } else if (pooling_type == "avg") {
C
chengduoZH 已提交
156
            paddle::operators::math::Pool2dGradFunctor<
Q
QI JUN 已提交
157
                DeviceContext, paddle::operators::math::AvgPoolGrad<T>, T>
158
                pool2d_backward;
159
            paddle::operators::math::AvgPoolGrad<T> pool_process;
Q
QI JUN 已提交
160 161
            pool2d_backward(dev_ctx, *in_x, *out, *out_grad, ksize, strides,
                            paddings, pool_process, in_x_grad);
162 163 164 165
          }
        } break;
        case 3: {
          if (pooling_type == "max") {
Q
QI JUN 已提交
166
            paddle::operators::math::MaxPool3dGradFunctor<DeviceContext, T>
167
                pool3d_backward;
Q
QI JUN 已提交
168 169
            pool3d_backward(dev_ctx, *in_x, *out, *out_grad, ksize, strides,
                            paddings, in_x_grad);
C
chengduoZH 已提交
170
          } else if (pooling_type == "avg") {
C
chengduoZH 已提交
171
            paddle::operators::math::Pool3dGradFunctor<
Q
QI JUN 已提交
172
                DeviceContext, paddle::operators::math::AvgPoolGrad<T>, T>
173
                pool3d_backward;
174
            paddle::operators::math::AvgPoolGrad<T> pool_process;
Q
QI JUN 已提交
175 176
            pool3d_backward(dev_ctx, *in_x, *out, *out_grad, ksize, strides,
                            paddings, pool_process, in_x_grad);
177 178
          }
        } break;
C
fix bug  
chengduoZH 已提交
179
        default: { PADDLE_THROW("Pool op only supports 2D and 3D input."); }
180 181 182 183 184 185 186
      }
    }
  }
};

}  // namespace operators
}  // namespace paddle