pooling.h 7.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
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
C
chengduo 已提交
16
#include <vector>
Y
Yi Wang 已提交
17 18 19 20
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/hostdevice.h"
D
dzhwinter 已提交
21
#include "paddle/fluid/platform/macros.h"
22 23 24 25 26

namespace paddle {
namespace operators {
namespace math {

C
chengduoZH 已提交
27 28
/*
 * \brief Extracting simple operations from pooling.
C
chengduoZH 已提交
29 30
 *        Both MaxPool and AvgPool need "initial", "compute" and "finalize"
 * operation.
C
chengduoZH 已提交
31 32 33
 *        MaxPool initializes temp variable to the negative maximum to find the
 * maximum value in the pooling field.
 *        AvgPool initializes temp variable to the zero to accumulate all values
C
chengduoZH 已提交
34
 * in pool pooling, and finally takes the average.
C
chengduoZH 已提交
35 36
 *        MaxPoolGrad and AvgPoolGrad are gradient operations respectively.
 */
37
template <class T>
38
class MaxPool {
39
 public:
C
chengduoZH 已提交
40
  DEVICE inline T initial() { return static_cast<T>(-FLT_MAX); }
C
chengduo 已提交
41 42
  DEVICE inline void compute(const T& x, T* y) { *y = *y > x ? *y : x; }
  DEVICE inline void finalize(const T& pool_field, T* y) {}
C
chengduoZH 已提交
43 44 45
};

template <class T>
46
class AvgPool {
C
chengduoZH 已提交
47 48
 public:
  DEVICE inline T initial() { return static_cast<T>(0); }
C
chengduo 已提交
49 50
  DEVICE inline void compute(const T& x, T* y) { *y += x; }
  DEVICE inline void finalize(const T& pool_field, T* y) { *y /= pool_field; }
C
chengduoZH 已提交
51
};
C
chengduoZH 已提交
52

C
chengduoZH 已提交
53
template <class T>
54
class MaxPoolGrad {
C
chengduoZH 已提交
55
 public:
C
chengduo 已提交
56 57 58
  DEVICE inline void compute(const T& x, const T& y, const T& dy, T scale,
                             T* dx) {
    *dx += dy * (x == y);
59 60 61 62
  }
};

template <class T>
63
class AvgPoolGrad {
64
 public:
C
chengduo 已提交
65 66 67
  DEVICE inline void compute(const T& x, const T& y, const T& dy, T scale,
                             T* dx) {
    *dx += (scale * dy);
68 69 70
  }
};

C
chengduoZH 已提交
71 72 73
/*
 * \brief Getting pooling results, and calculating gradient.
 *
C
chengduoZH 已提交
74 75 76 77
 * In pool2d, all tensors are in NCHW format. Where N is batch size, C is the
 * number of channels, H and W is the height and width of feature.
 * In pool3d, all tensors are in NCDHW format. Where N is batch size, C is the
 * number of channels, D, H and W is the depth, height and width of feature.
C
chengduoZH 已提交
78 79
 *
 * In max pooling, it is possible that the pooling region has multiple maximum
C
chengduoZH 已提交
80 81
 * elements. In this case, we should compute the gradient of the first maximum
 * element.
C
chengduoZH 已提交
82 83 84
 * This is different from average pooling. So we rewrite the max_pool_grad:
 * MaxPool2dGradFunctor, MaxPool3dGradFunctor.
 */
Q
QI JUN 已提交
85
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
86
class Pool2dFunctor {
87
 public:
Q
QI JUN 已提交
88
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
89 90 91
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
92
                  bool exclusive, framework::Tensor* output);
93 94
};

Q
QI JUN 已提交
95
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
96
class Pool2dGradFunctor {
97
 public:
Q
QI JUN 已提交
98
  void operator()(const DeviceContext& context, const framework::Tensor& input,
99
                  const framework::Tensor& output,
C
chengduo 已提交
100 101 102 103
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
104
                  bool exclusive, framework::Tensor* input_grad);
105 106
};

Q
QI JUN 已提交
107
template <typename DeviceContext, class T>
C
chengduoZH 已提交
108
class MaxPool2dGradFunctor {
109
 public:
Q
QI JUN 已提交
110
  void operator()(const DeviceContext& context, const framework::Tensor& input,
111
                  const framework::Tensor& output,
C
chengduo 已提交
112 113 114 115
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
116
                  framework::Tensor* input_grad);
117 118
};

Q
QI JUN 已提交
119
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
120
class Pool3dFunctor {
121
 public:
Q
QI JUN 已提交
122
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
123 124 125
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
126
                  bool exclusive, framework::Tensor* output);
127 128
};

Q
QI JUN 已提交
129
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
130
class Pool3dGradFunctor {
131
 public:
Q
QI JUN 已提交
132
  void operator()(const DeviceContext& context, const framework::Tensor& input,
133
                  const framework::Tensor& output,
C
chengduo 已提交
134 135 136 137
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
138
                  bool exclusive, framework::Tensor* input_grad);
139 140
};

Q
QI JUN 已提交
141
template <typename DeviceContext, class T>
C
chengduoZH 已提交
142
class MaxPool3dGradFunctor {
143
 public:
Q
QI JUN 已提交
144
  void operator()(const DeviceContext& context, const framework::Tensor& input,
145
                  const framework::Tensor& output,
C
chengduo 已提交
146 147 148 149
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
150
                  framework::Tensor* input_grad);
151 152
};

C
chengduoZH 已提交
153 154 155
/*
 * \brief Getting max pooling results and corresponding max index, and
 * calculating gradient.
C
chengduoZH 已提交
156
 * In up-sampling-pooling, it is necessary to know max element index.
C
chengduoZH 已提交
157 158 159
 * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
 * NCDHW format.
 */
Q
QI JUN 已提交
160
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
161 162
class MaxPool2dWithIndexFunctor {
 public:
Q
QI JUN 已提交
163
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
164 165 166
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, framework::Tensor* output,
Q
QI JUN 已提交
167
                  framework::Tensor* mask);
C
chengduoZH 已提交
168 169
};

Q
QI JUN 已提交
170
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
171 172
class MaxPool2dWithIndexGradFunctor {
 public:
Q
QI JUN 已提交
173
  void operator()(const DeviceContext& context,
C
chengduoZH 已提交
174
                  const framework::Tensor& output_grad,
C
chengduo 已提交
175 176 177
                  const framework::Tensor& mask, const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
178
                  framework::Tensor* input_grad);
C
chengduoZH 已提交
179 180
};

Q
QI JUN 已提交
181
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
182 183
class MaxPool3dWithIndexFunctor {
 public:
Q
QI JUN 已提交
184
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
185 186 187
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, framework::Tensor* output,
Q
QI JUN 已提交
188
                  framework::Tensor* mask);
C
chengduoZH 已提交
189 190
};

Q
QI JUN 已提交
191
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
192 193
class MaxPool3dWithIndexGradFunctor {
 public:
Q
QI JUN 已提交
194
  void operator()(const DeviceContext& context,
C
chengduoZH 已提交
195
                  const framework::Tensor& output_grad,
C
chengduo 已提交
196 197 198
                  const framework::Tensor& mask, const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
199
                  framework::Tensor* input_grad);
C
chengduoZH 已提交
200
};
C
chengduoZH 已提交
201

202 203 204
}  // namespace math
}  // namespace operators
}  // namespace paddle