Pool.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #include <ATen/core/Tensor.h>
  2. #include <ATen/div_rtn.h>
  3. #include <ATen/TensorUtils.h>
  4. #include <ATen/native/DispatchStub.h>
  5. #include <c10/util/irange.h>
  6. #include <utility>
  7. #pragma once
  8. namespace at {
  9. namespace native {
  10. using max_pool2d_fn = void(*)(const Tensor& output, const Tensor& indices, const Tensor& input,
  11. int kW, int kH, int dW, int dH, int padW, int padH, int dilationW, int dilationH);
  12. using max_pool2d_backward_fn = void(*)(const Tensor& grad_input, const Tensor& grad_output, const Tensor& indices);
  13. DECLARE_DISPATCH(max_pool2d_fn, max_pool2d_kernel);
  14. DECLARE_DISPATCH(max_pool2d_backward_fn, max_pool2d_backward_kernel);
  15. // averge pooling has same signature for forward and backward
  16. using avg_pool2d_fn = void(*)(const Tensor& output, const Tensor& input, int64_t kW, int64_t kH,
  17. int64_t dW, int64_t dH, int64_t padW, int64_t padH, bool count_include_pad, c10::optional<int64_t> divisor_override);
  18. using avg_pool2d_backward_fn = void(*)(const Tensor& output, const Tensor& input, int kW, int kH,
  19. int dW, int dH, int padW, int padH, bool count_include_pad, c10::optional<int64_t> divisor_override);
  20. DECLARE_DISPATCH(avg_pool2d_fn, avg_pool2d_kernel);
  21. DECLARE_DISPATCH(avg_pool2d_backward_fn, avg_pool2d_backward_kernel);
  22. namespace {
  23. template <typename dest_t, typename src_t>
  24. static inline dest_t
  25. safe_downcast(src_t v)
  26. {
  27. TORCH_CHECK(std::numeric_limits<dest_t>::min() <= v && v <= std::numeric_limits<dest_t>::max(),
  28. "integer out of range");
  29. return static_cast<dest_t>(v);
  30. }
  31. template<typename T>
  32. static inline T pooling_output_shape_pad_lr(
  33. T inputSize, T kernelSize, T pad_l, T pad_r, T stride, T dilation,
  34. bool ceil_mode) {
  35. T outputSize = div_rtn<T>(
  36. inputSize + pad_l + pad_r - dilation * (kernelSize - 1) - 1 +
  37. (ceil_mode ? stride - 1 : 0), stride) + 1;
  38. if (ceil_mode) {
  39. // ensure that the last pooling starts inside the image
  40. // needed to avoid problems in ceil mode
  41. if ((outputSize - 1) * stride >= inputSize + pad_l) {
  42. --outputSize;
  43. }
  44. }
  45. return outputSize;
  46. }
  47. template<typename T>
  48. static inline T pooling_output_shape(
  49. T inputSize, T kernelSize, T pad, T stride, T dilation, bool ceil_mode) {
  50. TORCH_CHECK(stride != 0, "stride should not be zero");
  51. TORCH_CHECK(pad >= 0,
  52. "pad must be non-negative, but got pad: ", pad);
  53. TORCH_CHECK(pad <= kernelSize / 2,
  54. "pad should be at most half of kernel size, but got pad=",
  55. pad, " and kernel_size=", kernelSize)
  56. return pooling_output_shape_pad_lr(
  57. inputSize, kernelSize, pad, pad, stride, dilation, ceil_mode);
  58. }
  59. template <typename T>
  60. std::pair<T, T> _pooling_same_mode_padding_lr(
  61. T inputSize, T kernelSize, int64_t stride, int64_t dilation) {
  62. // NOTE: with strides, the output shape is ceil(inputSize/stride)
  63. auto total_padding = T(dilation) * (kernelSize - 1);
  64. // Prefer symmetric padding if possible
  65. if (stride > 2 && (total_padding % 2 == 1)) {
  66. // The floor in the output size calculation gives us a little wiggle room
  67. auto wiggle_room = inputSize % stride - 1;
  68. if (wiggle_room > 0) {
  69. total_padding = total_padding - 1;
  70. }
  71. }
  72. auto left = total_padding / 2;
  73. return {left, total_padding - left};
  74. }
  75. inline std::pair<int64_t, int64_t> pooling_same_mode_padding_lr(
  76. int64_t inputSize, int64_t kernelSize, int64_t stride, int64_t dilation) {
  77. return _pooling_same_mode_padding_lr(inputSize, kernelSize, stride, dilation);
  78. }
  79. inline std::pair<c10::SymInt, c10::SymInt> pooling_same_mode_padding_lr(
  80. c10::SymInt inputSize, c10::SymInt kernelSize, int64_t stride, int64_t dilation) {
  81. return _pooling_same_mode_padding_lr(std::move(inputSize), std::move(kernelSize), stride, dilation);
  82. }
  83. // AveragePool2d/DilatedMaxPool2d (forward)
  84. static inline void
  85. pool2d_shape_check(
  86. const Tensor& input,
  87. int kH, int kW, int dH, int dW, int padH, int padW, int dilationH, int dilationW,
  88. int64_t nInputPlane,
  89. int64_t inputHeight, int64_t inputWidth,
  90. int64_t outputHeight, int64_t outputWidth, MemoryFormat memory_format)
  91. {
  92. const int64_t ndim = input.ndimension();
  93. const int64_t nOutputPlane = nInputPlane;
  94. TORCH_CHECK(kW > 0 && kH > 0,
  95. "kernel size should be greater than zero, but got ",
  96. "kH: ", kH, " kW: ", kW);
  97. TORCH_CHECK(dW > 0 && dH > 0,
  98. "stride should be greater than zero, but got "
  99. "dH: ", dH, " dW: ", dW);
  100. TORCH_CHECK(dilationH > 0 && dilationW > 0,
  101. "dilation should be greater than zero, but got ",
  102. "dilationH: ", dilationH, " dilationW: ", dilationW);
  103. bool valid_dims = input.size(1) != 0 && input.size(2) != 0;
  104. if (memory_format == at::MemoryFormat::ChannelsLast){
  105. // Expect tensor in NHWC format and allow 0-dim only for N.
  106. TORCH_CHECK((ndim == 4 && valid_dims && input.size(3) != 0),
  107. "Expected 4D (batch mode) tensor expected for input with channels_last layout"
  108. " with optional 0 dim batch size for input, but got: ", input.sizes());
  109. } else {
  110. TORCH_CHECK((ndim == 3 && input.size(0) != 0 && valid_dims) ||
  111. (ndim == 4 && valid_dims && input.size(3) != 0),
  112. "Expected 3D or 4D (batch mode) tensor with optional 0 dim batch size for input, but got:",
  113. input.sizes());
  114. }
  115. TORCH_CHECK(kW/2 >= padW && kH/2 >= padH,
  116. "pad should be smaller than or equal to half of kernel size, but got ",
  117. "padW = ", padW, ", padH = ", padH, ", kW = ", kW, ", kH = ", kH);
  118. TORCH_CHECK(outputWidth >= 1 && outputHeight >= 1,
  119. "Given input size: (",
  120. nInputPlane, "x", inputHeight, "x", inputWidth, "). ",
  121. "Calculated output size: (",
  122. nOutputPlane, "x", outputHeight, "x", outputWidth, "). ",
  123. "Output size is too small");
  124. }
  125. // DilatedMaxPool2d (backward)
  126. static inline void
  127. max_pool2d_backward_shape_check(
  128. const Tensor& input,
  129. const Tensor& gradOutput,
  130. const Tensor& indices,
  131. int kH, int kW, int dH, int dW, int padH, int padW, int dilationH, int dilationW,
  132. int64_t nInputPlane,
  133. int64_t inputHeight, int64_t inputWidth,
  134. int64_t outputHeight, int64_t outputWidth, MemoryFormat memory_format)
  135. {
  136. pool2d_shape_check(
  137. input,
  138. kH, kW, dH, dW, padH, padW, dilationH, dilationW,
  139. nInputPlane, inputHeight, inputWidth, outputHeight, outputWidth, memory_format);
  140. const int64_t ndim = input.ndimension();
  141. const int64_t nOutputPlane = nInputPlane;
  142. check_dim_size(gradOutput, ndim, ndim-3, nOutputPlane);
  143. check_dim_size(gradOutput, ndim, ndim-2, outputHeight);
  144. check_dim_size(gradOutput, ndim, ndim-1, outputWidth);
  145. check_dim_size(indices, ndim, ndim-3, nOutputPlane);
  146. check_dim_size(indices, ndim, ndim-2, outputHeight);
  147. check_dim_size(indices, ndim, ndim-1, outputWidth);
  148. }
  149. // AveragePool2d (backward)
  150. static inline void
  151. avg_pool2d_backward_shape_check(
  152. const Tensor& input,
  153. const Tensor& gradOutput,
  154. int64_t /*nbatch*/,
  155. int kH, int kW, int dH, int dW, int padH, int padW,
  156. int64_t nInputPlane,
  157. int64_t inputHeight, int64_t inputWidth,
  158. int64_t outputHeight, int64_t outputWidth,
  159. MemoryFormat memory_format)
  160. {
  161. pool2d_shape_check(
  162. input,
  163. kH, kW, dH, dW, padH, padW, 1, 1,
  164. nInputPlane, inputHeight, inputWidth, outputHeight, outputWidth,
  165. memory_format);
  166. const int64_t ndim = input.ndimension();
  167. const int64_t nOutputPlane = nInputPlane;
  168. check_dim_size(gradOutput, ndim, ndim-3, nOutputPlane);
  169. check_dim_size(gradOutput, ndim, ndim-2, outputHeight);
  170. check_dim_size(gradOutput, ndim, ndim-1, outputWidth);
  171. }
  172. // AveragePool3d/DilatedMaxPool3d (forward)
  173. static inline void
  174. pool3d_shape_check(
  175. const Tensor& input,
  176. int64_t nslices,
  177. int kT, int kH, int kW,
  178. int dT, int dH, int dW,
  179. int pT, int pH, int pW,
  180. int dilationT, int dilationH, int dilationW,
  181. int64_t itime, int64_t iheight, int64_t iwidth,
  182. int64_t otime, int64_t oheight, int64_t owidth,
  183. const char *fn_name,
  184. bool check_input_size=false)
  185. {
  186. const int64_t ndim = input.ndimension();
  187. TORCH_CHECK(kT > 0 && kW > 0 && kH > 0,
  188. "kernel size should be greater than zero, but got ",
  189. "kT: ", kT, " kH: ", kH, " kW: ", kW);
  190. TORCH_CHECK(dT > 0 && dW > 0 && dH > 0,
  191. "stride should be greater than zero, but got ",
  192. "dT: ", dT, " dH: ", dH, " dW: ", dW);
  193. TORCH_CHECK(dilationT > 0 && dilationW > 0 && dilationH > 0,
  194. "dilation should be greater than zero, but got ",
  195. "dilationT: ", dilationT, " dilationH: ", dilationH, " dilationW: ", dilationW);
  196. TORCH_CHECK(ndim == 4 || ndim == 5,
  197. fn_name, ": Expected 4D or 5D tensor for input, but got: ", input.sizes());
  198. for (const auto i : c10::irange(ndim)) {
  199. if (ndim == 5 && i == 0) {
  200. // size of batch-dim can be 0.
  201. continue;
  202. }
  203. TORCH_CHECK(
  204. input.size(i) > 0,
  205. fn_name,
  206. ": Expected input's non-batch dimensions to have positive length,"
  207. " but input has a shape of ",
  208. input.sizes(),
  209. " and non-batch dimension ",
  210. input.size(i),
  211. " has length zero!")
  212. }
  213. if (check_input_size) { // AveragePool3d
  214. TORCH_CHECK(itime >= kT && iheight >= kH && iwidth >= kW,
  215. "input image ", "(T: ", itime, " H: ", iheight, " W: ", iwidth, ") smaller than ",
  216. "kernel size ", "(kT: ", kT, " kH: ", kH, " kW: ", kW, ")");
  217. }
  218. TORCH_CHECK(kT/2 >= pT && kW/2 >= pW && kH/2 >= pH,
  219. "pad should be smaller than or equal to half of kernel size, but got "
  220. "kT: ", kT, " kW: ", kW, " kH: ", kH, " padT: ", pT, " padW: ", pW, " padH: ", pH);
  221. TORCH_CHECK(otime >= 1 && owidth >= 1 && oheight >= 1,
  222. "Given input size: (",
  223. nslices,"x", itime, "x", iheight, "x", iwidth, "). ",
  224. "Calculated output size: (",
  225. nslices, "x", otime, "x", oheight, "x", owidth, "). ",
  226. "Output size is too small");
  227. }
  228. static inline void
  229. max_pool3d_backward_shape_check(
  230. const Tensor& input,
  231. const Tensor& gradOutput,
  232. const Tensor& indices,
  233. int64_t nslices,
  234. int kT, int kH, int kW,
  235. int dT, int dH, int dW,
  236. int pT, int pH, int pW,
  237. int dilationT, int dilationH, int dilationW,
  238. int64_t itime, int64_t iheight, int64_t iwidth,
  239. int64_t otime, int64_t oheight, int64_t owidth,
  240. const char* fn_name)
  241. {
  242. const int64_t ndim = input.ndimension();
  243. pool3d_shape_check(
  244. input,
  245. nslices,
  246. kT, kH, kW,
  247. dT, dH, dW,
  248. pT, pH, pW,
  249. dilationT, dilationH, dilationW,
  250. itime, iheight, iwidth,
  251. otime, oheight, owidth, fn_name);
  252. check_dim_size(gradOutput, ndim, ndim-4, nslices);
  253. check_dim_size(gradOutput, ndim, ndim-3, otime);
  254. check_dim_size(gradOutput, ndim, ndim-2, oheight);
  255. check_dim_size(gradOutput, ndim, ndim-1, owidth);
  256. check_dim_size(indices, ndim, ndim-4, nslices);
  257. check_dim_size(indices, ndim, ndim-3, otime);
  258. check_dim_size(indices, ndim, ndim-2, oheight);
  259. check_dim_size(indices, ndim, ndim-1, owidth);
  260. }
  261. static inline void
  262. avg_pool3d_backward_shape_check(
  263. const Tensor& input,
  264. const Tensor& gradOutput,
  265. int64_t nslices,
  266. int kT, int kH, int kW,
  267. int dT, int dH, int dW,
  268. int pT, int pH, int pW,
  269. int64_t itime, int64_t iheight, int64_t iwidth,
  270. int64_t otime, int64_t oheight, int64_t owidth,
  271. const char *fn_name)
  272. {
  273. const int64_t ndim = input.ndimension();
  274. pool3d_shape_check(
  275. input,
  276. nslices,
  277. kT, kH, kW,
  278. dT, dH, dW,
  279. pT, pH, pW,
  280. 1, 1, 1,
  281. itime, iheight, iwidth,
  282. otime, oheight, owidth,
  283. fn_name, true);
  284. check_dim_size(gradOutput, ndim, ndim-4, nslices);
  285. check_dim_size(gradOutput, ndim, ndim-3, otime);
  286. check_dim_size(gradOutput, ndim, ndim-2, oheight);
  287. check_dim_size(gradOutput, ndim, ndim-1, owidth);
  288. }
  289. } // namespace
  290. } // at::native
  291. } // at