pooling.h 7.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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/tensor.h"
#include "paddle/platform/device_context.h"
C
chengduoZH 已提交
19
#include "paddle/platform/hostdevice.h"
20 21 22 23 24

namespace paddle {
namespace operators {
namespace math {

C
chengduoZH 已提交
25 26
#define FLT_MAX \
  __FLT_MAX__  // It might need to be placed in another file, but I'm still
C
chengduoZH 已提交
27
               // wondering where to put it.
C
chengduoZH 已提交
28 29 30

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

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

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

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

C
chengduoZH 已提交
73 74 75
/*
 * \brief Getting pooling results, and calculating gradient.
 *
C
chengduoZH 已提交
76 77 78 79
 * 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 已提交
80 81
 *
 * In max pooling, it is possible that the pooling region has multiple maximum
C
chengduoZH 已提交
82 83
 * elements. In this case, we should compute the gradient of the first maximum
 * element.
C
chengduoZH 已提交
84 85 86
 * This is different from average pooling. So we rewrite the max_pool_grad:
 * MaxPool2dGradFunctor, MaxPool3dGradFunctor.
 */
87
template <typename Place, typename PoolProcess, typename T>
C
chengduoZH 已提交
88
class Pool2dFunctor {
89
 public:
90 91
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& output,
92
                  std::vector<int>& ksize, std::vector<int>& strides,
C
chengduoZH 已提交
93
                  std::vector<int>& paddings, PoolProcess pool_compute);
94 95 96
};

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

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

117
template <typename Place, typename PoolProcess, typename T>
C
chengduoZH 已提交
118
class Pool3dFunctor {
119
 public:
120 121
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& output,
122
                  std::vector<int>& ksize, std::vector<int>& strides,
C
chengduoZH 已提交
123
                  std::vector<int>& paddings, PoolProcess pool_compute);
124 125 126
};

template <typename Place, typename PoolProcess, typename T>
C
chengduoZH 已提交
127
class Pool3dGradFunctor {
128
 public:
129 130
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& input_grad,
131 132 133
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings,
C
chengduoZH 已提交
134
                  PoolProcess pool_compute);
135 136
};

137
template <typename Place, class T>
C
chengduoZH 已提交
138
class MaxPool3dGradFunctor {
139 140 141 142 143 144 145 146
 public:
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& input_grad,
                  const framework::Tensor& output,
                  const framework::Tensor& output_grad, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings);
};

C
chengduoZH 已提交
147 148 149
/*
 * \brief Getting max pooling results and corresponding max index, and
 * calculating gradient.
C
chengduoZH 已提交
150
 * In up-sampling-pooling, it is necessary to know max element index.
C
chengduoZH 已提交
151 152 153
 * In pool2d, all tensors are in NCHW format. In pool3d, all tensors are in
 * NCDHW format.
 */
C
chengduoZH 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
template <typename Place, typename T>
class MaxPool2dWithIndexFunctor {
 public:
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& output,
                  framework::Tensor& mask, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings);
};

template <typename Place, typename T>
class MaxPool2dWithIndexGradFunctor {
 public:
  void operator()(const platform::DeviceContext& context,
                  framework::Tensor& input_grad,
                  const framework::Tensor& output_grad,
                  const framework::Tensor& mask, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings);
};

template <typename Place, typename T>
class MaxPool3dWithIndexFunctor {
 public:
  void operator()(const platform::DeviceContext& context,
                  const framework::Tensor& input, framework::Tensor& output,
                  framework::Tensor& mask, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings);
};

template <typename Place, typename T>
class MaxPool3dWithIndexGradFunctor {
 public:
  void operator()(const platform::DeviceContext& context,
                  framework::Tensor& input_grad,
                  const framework::Tensor& output_grad,
                  const framework::Tensor& mask, std::vector<int>& ksize,
                  std::vector<int>& strides, std::vector<int>& paddings);
};
C
chengduoZH 已提交
191

192 193 194
}  // namespace math
}  // namespace operators
}  // namespace paddle