pooling.h 8.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
  }
};

D
dengkaipeng 已提交
71 72 73 74 75 76 77 78 79 80 81 82
/* 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 已提交
83 84 85
/*
 * \brief Getting pooling results, and calculating gradient.
 *
C
chengduoZH 已提交
86 87 88 89
 * 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 已提交
90 91
 *
 * In max pooling, it is possible that the pooling region has multiple maximum
C
chengduoZH 已提交
92 93
 * elements. In this case, we should compute the gradient of the first maximum
 * element.
C
chengduoZH 已提交
94 95 96
 * This is different from average pooling. So we rewrite the max_pool_grad:
 * MaxPool2dGradFunctor, MaxPool3dGradFunctor.
 */
N
nhzlx 已提交
97
#ifdef PADDLE_WITH_CUDA
N
nhzlx 已提交
98 99 100 101 102 103 104 105 106 107
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 已提交
108
#endif
N
nhzlx 已提交
109

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

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

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

Q
QI JUN 已提交
144
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
145
class Pool3dFunctor {
146
 public:
Q
QI JUN 已提交
147
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
148 149 150
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
151
                  bool exclusive, bool adaptive, framework::Tensor* output);
152 153
};

Q
QI JUN 已提交
154
template <typename DeviceContext, typename PoolProcess, typename T>
C
chengduoZH 已提交
155
class Pool3dGradFunctor {
156
 public:
Q
QI JUN 已提交
157
  void operator()(const DeviceContext& context, const framework::Tensor& input,
158
                  const framework::Tensor& output,
C
chengduo 已提交
159 160 161 162
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, PoolProcess pool_compute,
163
                  bool exclusive, bool adaptive, framework::Tensor* input_grad);
164 165
};

Q
QI JUN 已提交
166
template <typename DeviceContext, class T>
C
chengduoZH 已提交
167
class MaxPool3dGradFunctor {
168
 public:
Q
QI JUN 已提交
169
  void operator()(const DeviceContext& context, const framework::Tensor& input,
170
                  const framework::Tensor& output,
C
chengduo 已提交
171 172 173 174
                  const framework::Tensor& output_grad,
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings,
C
chengduoZH 已提交
175
                  framework::Tensor* input_grad);
176 177
};

C
chengduoZH 已提交
178 179 180
/*
 * \brief Getting max pooling results and corresponding max index, and
 * calculating gradient.
C
chengduoZH 已提交
181
 * In up-sampling-pooling, it is necessary to know max element index.
C
chengduoZH 已提交
182 183 184
 * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
 * NCDHW format.
 */
Q
QI JUN 已提交
185
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
186 187
class MaxPool2dWithIndexFunctor {
 public:
Q
QI JUN 已提交
188
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
189 190
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
191 192
                  const std::vector<int>& paddings, bool adaptive,
                  framework::Tensor* output, framework::Tensor* mask);
C
chengduoZH 已提交
193 194
};

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

Q
QI JUN 已提交
206
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
207 208
class MaxPool3dWithIndexFunctor {
 public:
Q
QI JUN 已提交
209
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
210 211
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
212 213
                  const std::vector<int>& paddings, bool adaptive,
                  framework::Tensor* output, framework::Tensor* mask);
C
chengduoZH 已提交
214 215
};

Q
QI JUN 已提交
216
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
217 218
class MaxPool3dWithIndexGradFunctor {
 public:
Q
QI JUN 已提交
219
  void operator()(const DeviceContext& context,
C
chengduoZH 已提交
220
                  const framework::Tensor& output_grad,
C
chengduo 已提交
221 222
                  const framework::Tensor& mask, const std::vector<int>& ksize,
                  const std::vector<int>& strides,
223
                  const std::vector<int>& paddings, bool adaptive,
C
chengduoZH 已提交
224
                  framework::Tensor* input_grad);
C
chengduoZH 已提交
225
};
C
chengduoZH 已提交
226

227 228 229
}  // namespace math
}  // namespace operators
}  // namespace paddle