GridSamplerUtils.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. // See NOTE: [Tensor vs. TensorBase]
  3. // https://github.com/pytorch/pytorch/pull/66979
  4. #include <ATen/core/TensorBase.h>
  5. #include <ATen/native/TensorProperties.h>
  6. #include <ATen/native/CanUse32BitIndexMath.h>
  7. namespace at::native {
  8. namespace detail {
  9. enum class GridSamplerInterpolation {Bilinear, Nearest, Bicubic};
  10. enum class GridSamplerPadding {Zeros, Border, Reflection};
  11. } // namespace detail
  12. using detail::GridSamplerInterpolation;
  13. using detail::GridSamplerPadding;
  14. // See NOTE [ grid_sampler Native Functions ].
  15. inline void check_grid_sampler_common(
  16. const TensorBase& input,
  17. const TensorBase& grid
  18. ) {
  19. auto input_opt = input.options();
  20. auto grid_opt = grid.options();
  21. TORCH_CHECK(
  22. input.defined(),
  23. "grid_sampler(): expected input to not be undefined");
  24. TORCH_CHECK(
  25. grid.defined(),
  26. "grid_sampler(): expected grid to not be undefined");
  27. TORCH_CHECK(
  28. input_opt.device() == grid_opt.device(),
  29. "grid_sampler(): expected input and grid to be on same device, but input "
  30. "is on ", input_opt.device(), " and grid is on ", grid_opt.device());
  31. TORCH_CHECK(
  32. input_opt.layout() == kStrided && grid_opt.layout() == kStrided,
  33. "grid_sampler(): expected input and grid to have torch.strided layout, but "
  34. "input has ", input_opt.layout(), " and grid has ", grid_opt.layout());
  35. TORCH_CHECK(
  36. input.size(0) == grid.size(0),
  37. "grid_sampler(): expected grid and input to have same batch size, but got "
  38. "input with sizes ", input.sizes(), " and grid with sizes ", grid.sizes());
  39. TORCH_CHECK(
  40. grid.size(-1) == input.dim() - 2,
  41. "grid_sampler(): expected grid to have size ", input.dim() - 2, " in last "
  42. "dimension, but got grid with sizes ", grid.sizes());
  43. for (const auto i : c10::irange(2, input.dim())) {
  44. TORCH_CHECK(input.size(i) > 0,
  45. "grid_sampler(): expected input to have non-empty spatial dimensions, "
  46. "but input has sizes ", input.sizes(), " with dimension ", i, " being "
  47. "empty");
  48. }
  49. }
  50. // See NOTE [ grid_sampler Native Functions ].
  51. inline void check_grid_sampler_2d(
  52. const TensorBase& input,
  53. const TensorBase& grid
  54. ) {
  55. TORCH_CHECK(
  56. input.dim() == 4 && input.dim() == grid.dim(),
  57. "grid_sampler(): expected 4D input and grid with same number of "
  58. "dimensions, but got input with sizes ", input.sizes(),
  59. " and grid with sizes ", grid.sizes());
  60. }
  61. // See NOTE [ grid_sampler Native Functions ].
  62. inline void check_grid_sampler_3d(
  63. const TensorBase& input,
  64. const TensorBase& grid,
  65. int64_t interpolation_mode
  66. ) {
  67. TORCH_CHECK(
  68. input.dim() == 5 && input.dim() == grid.dim(),
  69. "grid_sampler(): expected 5D input and grid with same number of "
  70. "dimensions, but got input with sizes ", input.sizes(),
  71. " and grid with sizes ", grid.sizes());
  72. TORCH_CHECK(
  73. !(input.dim() == 5 &&
  74. static_cast<GridSamplerInterpolation>(interpolation_mode) ==
  75. GridSamplerInterpolation::Bicubic),
  76. "grid_sampler(): bicubic interpolation only supports 4D input");
  77. }
  78. // See NOTE [ grid_sampler Native Functions ].
  79. // cudnn does not support inputs larger than 1024.
  80. inline bool cond_cudnn_grid_sampler(
  81. const TensorBase& input,
  82. const TensorBase& grid
  83. ) {
  84. return (
  85. at::native::cudnn_is_acceptable(input) &&
  86. at::native::cudnn_is_acceptable(grid) &&
  87. at::native::canUse32BitIndexMath(input) &&
  88. at::native::canUse32BitIndexMath(grid) &&
  89. input.dim() == 4 &&
  90. input.sym_size(1) <= 1024);
  91. }
  92. } // namespace at::native