pooling.h 8.2 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.
 */
N
nhzlx 已提交
85
#ifdef PADDLE_WITH_CUDA
N
nhzlx 已提交
86 87 88 89 90 91 92 93 94 95
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 已提交
96
#endif
N
nhzlx 已提交
97

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

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

Q
QI JUN 已提交
120
template <typename DeviceContext, class T>
C
chengduoZH 已提交
121
class MaxPool2dGradFunctor {
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,
C
chengduoZH 已提交
129
                  framework::Tensor* input_grad);
130 131
};

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

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

Q
QI JUN 已提交
154
template <typename DeviceContext, class T>
C
chengduoZH 已提交
155
class MaxPool3dGradFunctor {
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,
C
chengduoZH 已提交
163
                  framework::Tensor* input_grad);
164 165
};

C
chengduoZH 已提交
166 167 168
/*
 * \brief Getting max pooling results and corresponding max index, and
 * calculating gradient.
C
chengduoZH 已提交
169
 * In up-sampling-pooling, it is necessary to know max element index.
C
chengduoZH 已提交
170 171 172
 * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
 * NCDHW format.
 */
Q
QI JUN 已提交
173
template <typename DeviceContext, typename T1, typename T2>
C
chengduoZH 已提交
174 175
class MaxPool2dWithIndexFunctor {
 public:
Q
QI JUN 已提交
176
  void operator()(const DeviceContext& context, const framework::Tensor& input,
C
chengduo 已提交
177 178 179
                  const std::vector<int>& ksize,
                  const std::vector<int>& strides,
                  const std::vector<int>& paddings, framework::Tensor* output,
Q
QI JUN 已提交
180
                  framework::Tensor* mask);
C
chengduoZH 已提交
181 182
};

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

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

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

215 216 217
}  // namespace math
}  // namespace operators
}  // namespace paddle