pooling.h 11.6 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
16
#include <string>
C
chengduo 已提交
17
#include <vector>
Y
Yi Wang 已提交
18 19 20 21
#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 已提交
22
#include "paddle/fluid/platform/macros.h"
23 24 25 26 27

namespace paddle {
namespace operators {
namespace math {

C
chengduoZH 已提交
28 29
/*
 * \brief Extracting simple operations from pooling.
C
chengduoZH 已提交
30 31
 *        Both MaxPool and AvgPool need "initial", "compute" and "finalize"
 * operation.
C
chengduoZH 已提交
32 33 34
 *        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 已提交
35
 * in pool pooling, and finally takes the average.
C
chengduoZH 已提交
36 37
 *        MaxPoolGrad and AvgPoolGrad are gradient operations respectively.
 */
38
template <class T>
39
class MaxPool {
40
 public:
C
chengduoZH 已提交
41
  DEVICE inline T initial() { return static_cast<T>(-FLT_MAX); }
C
chengduo 已提交
42 43
  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 已提交
44 45 46
};

template <class T>
47
class AvgPool {
C
chengduoZH 已提交
48 49
 public:
  DEVICE inline T initial() { return static_cast<T>(0); }
C
chengduo 已提交
50 51
  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 已提交
52
};
C
chengduoZH 已提交
53

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

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

D
dengkaipeng 已提交
72 73 74 75 76 77 78 79 80 81 82 83
/* used for adaptive pool to calculate start and end index of each divided grid
 */
HOSTDEVICE inline int AdaptStartIndex(int ph, int input_size, int output_size) {
  return static_cast<int>(
      floor(static_cast<double>(ph * input_size) / output_size));
}

HOSTDEVICE inline int AdaptEndIndex(int ph, int input_size, int output_size) {
  return static_cast<int>(
      ceil(static_cast<double>((ph + 1) * input_size) / output_size));
}

C
chengduoZH 已提交
84 85 86
/*
 * \brief Getting pooling results, and calculating gradient.
 *
87 88 89 90 91
 * In pool2d, all Tensors are in NCHW or NHWC 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 or NDHWC 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 已提交
92 93
 *
 * In max pooling, it is possible that the pooling region has multiple maximum
C
chengduoZH 已提交
94 95
 * elements. In this case, we should compute the gradient of the first maximum
 * element.
C
chengduoZH 已提交
96 97 98
 * This is different from average pooling. So we rewrite the max_pool_grad:
 * MaxPool2dGradFunctor, MaxPool3dGradFunctor.
 */
N
nhzlx 已提交
99
#ifdef PADDLE_WITH_CUDA
N
nhzlx 已提交
100 101 102 103 104 105 106 107 108 109
template <typename PoolProcess, typename T>
class Pool2dDirectCUDAFunctor {
 public:
  void operator()(const T* input, const std::vector<int>& input_shape,
                  const std::vector<int>& output_shape,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
                  bool exclusive, T* output, cudaStream_t stream);
};
N
nhzlx 已提交
110
#endif
N
nhzlx 已提交
111

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

  // overload operator() to support argument data_format
  void operator()(const DeviceContext& context, const framework::Tensor& input,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
                  const std::string data_format, PoolProcess pool_compute,
                  bool exclusive, bool adaptive, framework::Tensor* output);
128 129
};

Q
QI JUN 已提交
130
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
131
class Pool2dGradFunctor {
132
 public:
Q
QI JUN 已提交
133
  void operator()(const DeviceContext& context, const framework::Tensor& input,
134
                  const framework::Tensor& output,
C
chengduo 已提交
135 136 137 138
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
139
                  bool exclusive, bool adaptive, framework::Tensor* input_grad);
140 141 142 143 144 145 146 147 148
  // overload operator() to support argument data_format
  void operator()(const DeviceContext& context, const framework::Tensor& input,
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
                  const std::string data_format, PoolProcess pool_compute,
                  bool exclusive, bool adaptive, framework::Tensor* input_grad);
149 150
};

Q
QI JUN 已提交
151
template <typename DeviceContext, class T>
C
chengduoZH 已提交
152
class MaxPool2dGradFunctor {
153
 public:
Q
QI JUN 已提交
154
  void operator()(const DeviceContext& context, const framework::Tensor& input,
155
                  const framework::Tensor& output,
C
chengduo 已提交
156 157 158 159
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
160
                  framework::Tensor* input_grad);
161 162 163 164 165 166 167 168
  // overload operator() to support argument data_format
  void operator()(const DeviceContext& context, const framework::Tensor& input,
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
                  const std::string data_format, framework::Tensor* input_grad);
169 170
};

Q
QI JUN 已提交
171
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
172
class Pool3dFunctor {
173
 public:
Q
QI JUN 已提交
174
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
175 176 177
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
178
                  bool exclusive, bool adaptive, framework::Tensor* output);
179 180 181 182 183 184 185
  // overload operator() to support argument data_format
  void operator()(const DeviceContext& context, const framework::Tensor& input,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
                  const std::string data_format, PoolProcess pool_compute,
                  bool exclusive, bool adaptive, framework::Tensor* output);
186 187
};

Q
QI JUN 已提交
188
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
189
class Pool3dGradFunctor {
190
 public:
Q
QI JUN 已提交
191
  void operator()(const DeviceContext& context, const framework::Tensor& input,
192
                  const framework::Tensor& output,
C
chengduo 已提交
193 194 195 196
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
197
                  bool exclusive, bool adaptive, framework::Tensor* input_grad);
198 199 200 201 202 203 204 205 206
  // overload operator() to support argument data_format
  void operator()(const DeviceContext& context, const framework::Tensor& input,
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
                  const std::string data_format, PoolProcess pool_compute,
                  bool exclusive, bool adaptive, framework::Tensor* input_grad);
207 208
};

Q
QI JUN 已提交
209
template <typename DeviceContext, class T>
C
chengduoZH 已提交
210
class MaxPool3dGradFunctor {
211
 public:
Q
QI JUN 已提交
212
  void operator()(const DeviceContext& context, const framework::Tensor& input,
213
                  const framework::Tensor& output,
C
chengduo 已提交
214 215 216 217
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
218
                  framework::Tensor* input_grad);
219 220 221 222 223 224 225 226
  // overload operator() to support argument data_format
  void operator()(const DeviceContext& context, const framework::Tensor& input,
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
                  const std::string data_format, framework::Tensor* input_grad);
227 228
};

C
chengduoZH 已提交
229 230 231
/*
 * \brief Getting max pooling results and corresponding max index, and
 * calculating gradient.
C
chengduoZH 已提交
232
 * In up-sampling-pooling, it is necessary to know max element index.
C
chengduoZH 已提交
233 234 235
 * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
 * NCDHW format.
 */
Q
QI JUN 已提交
236
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
237 238
class MaxPool2dWithIndexFunctor {
 public:
Q
QI JUN 已提交
239
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
240 241
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
242 243
                  const std::vector<int>& paddings, bool adaptive,
                  framework::Tensor* output, framework::Tensor* mask);
C
chengduoZH 已提交
244 245
};

Q
QI JUN 已提交
246
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
247 248
class MaxPool2dWithIndexGradFunctor {
 public:
Q
QI JUN 已提交
249
  void operator()(const DeviceContext& context,
C
chengduoZH 已提交
250
                  const framework::Tensor& output_grad,
C
chengduo 已提交
251 252
                  const framework::Tensor& mask, const std::vector<int>& ksize,
                  const std::vector<int>& strides,
253
                  const std::vector<int>& paddings, bool adaptive,
C
chengduoZH 已提交
254
                  framework::Tensor* input_grad);
C
chengduoZH 已提交
255 256
};

Q
QI JUN 已提交
257
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
258 259
class MaxPool3dWithIndexFunctor {
 public:
Q
QI JUN 已提交
260
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
261 262
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
263 264
                  const std::vector<int>& paddings, bool adaptive,
                  framework::Tensor* output, framework::Tensor* mask);
C
chengduoZH 已提交
265 266
};

Q
QI JUN 已提交
267
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
268 269
class MaxPool3dWithIndexGradFunctor {
 public:
Q
QI JUN 已提交
270
  void operator()(const DeviceContext& context,
C
chengduoZH 已提交
271
                  const framework::Tensor& output_grad,
C
chengduo 已提交
272 273
                  const framework::Tensor& mask, const std::vector<int>& ksize,
                  const std::vector<int>& strides,
274
                  const std::vector<int>& paddings, bool adaptive,
C
chengduoZH 已提交
275
                  framework::Tensor* input_grad);
C
chengduoZH 已提交
276
};
C
chengduoZH 已提交
277

278 279 280
}  // namespace math
}  // namespace operators
}  // namespace paddle