Pool.h 13 KB

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