ConvUtils.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #pragma once
  2. #include <ATen/core/Tensor.h>
  3. #include <ATen/TensorUtils.h>
  4. #include <ATen/detail/CUDAHooksInterface.h>
  5. #include <ATen/native/DispatchStub.h>
  6. #include <c10/util/env.h>
  7. #include <c10/util/irange.h>
  8. #include <utility>
  9. namespace at::native {
  10. using conv_depthwise2d_backward_fn = std::tuple<at::Tensor,at::Tensor>(*)(
  11. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  12. at::IntArrayRef, at::IntArrayRef, std::array<bool, 2>);
  13. DECLARE_DISPATCH(conv_depthwise2d_backward_fn, conv_depthwise2d_backward_stub)
  14. using conv_depthwise3d_backward_fn = std::tuple<at::Tensor,at::Tensor,at::Tensor>(*)(
  15. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  16. at::IntArrayRef, at::IntArrayRef, std::array<bool, 3>);
  17. DECLARE_DISPATCH(conv_depthwise3d_backward_fn, conv_depthwise3d_backward_stub)
  18. using cudnn_convolution_backward_fn = std::tuple<at::Tensor,at::Tensor>(*)(
  19. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  20. at::IntArrayRef, int64_t, bool, bool, bool, std::array<bool,2>);
  21. DECLARE_DISPATCH(cudnn_convolution_backward_fn, cudnn_convolution_backward_stub)
  22. using mps_convolution_backward_fn = std::tuple<at::Tensor,at::Tensor,at::Tensor>(*)(
  23. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  24. at::IntArrayRef, int64_t, std::array<bool,3>);
  25. DECLARE_DISPATCH(mps_convolution_backward_fn, mps_convolution_backward_stub)
  26. using cudnn_convolution_transpose_backward_fn = std::tuple<at::Tensor,at::Tensor>(*)(
  27. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  28. at::IntArrayRef, at::IntArrayRef, int64_t, bool, bool, bool, std::array<bool,2>);
  29. DECLARE_DISPATCH(cudnn_convolution_transpose_backward_fn, cudnn_convolution_transpose_backward_stub)
  30. using miopen_convolution_backward_fn = std::tuple<at::Tensor,at::Tensor,at::Tensor>(*)(
  31. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  32. at::IntArrayRef, int64_t, bool, bool, std::array<bool,3>);
  33. DECLARE_DISPATCH(miopen_convolution_backward_fn, miopen_convolution_backward_stub)
  34. using miopen_convolution_transpose_backward_fn = std::tuple<at::Tensor,at::Tensor,at::Tensor>(*)(
  35. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  36. at::IntArrayRef, at::IntArrayRef, int64_t, bool, bool, std::array<bool,3>);
  37. DECLARE_DISPATCH(miopen_convolution_transpose_backward_fn, miopen_convolution_transpose_backward_stub)
  38. using miopen_depthwise_convolution_backward_fn = std::tuple<at::Tensor,at::Tensor,at::Tensor>(*)(
  39. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  40. at::IntArrayRef, int64_t, bool, bool, std::array<bool,3>);
  41. DECLARE_DISPATCH(miopen_depthwise_convolution_backward_fn, miopen_depthwise_convolution_backward_stub)
  42. using mkldnn_convolution_backward_fn = std::tuple<at::Tensor,at::Tensor,at::Tensor>(*)(
  43. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  44. at::IntArrayRef, int64_t, std::array<bool,3>);
  45. DECLARE_DISPATCH(mkldnn_convolution_backward_fn, mkldnn_convolution_backward_stub)
  46. using mkldnn_convolution_transpose_fn = Tensor(*)(const Tensor&, const Tensor&, const std::optional<Tensor>&,
  47. IntArrayRef, IntArrayRef, IntArrayRef, IntArrayRef, int64_t);
  48. DECLARE_DISPATCH(mkldnn_convolution_transpose_fn, mkldnn_convolution_transpose_stub)
  49. using mkldnn_convolution_transpose_backward_fn = std::tuple<at::Tensor,at::Tensor,at::Tensor>(*)(
  50. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  51. at::IntArrayRef, at::IntArrayRef, int64_t, std::array<bool,3>);
  52. DECLARE_DISPATCH(mkldnn_convolution_transpose_backward_fn, mkldnn_convolution_transpose_backward_stub)
  53. using slow_conv_dilated2d_backward_fn = std::tuple<at::Tensor,at::Tensor,at::Tensor>(*)(
  54. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  55. at::IntArrayRef, at::IntArrayRef, std::array<bool, 3>);
  56. DECLARE_DISPATCH(slow_conv_dilated2d_backward_fn, slow_conv_dilated2d_backward_stub)
  57. using slow_conv_dilated3d_backward_fn = std::tuple<at::Tensor,at::Tensor,at::Tensor>(*)(
  58. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  59. at::IntArrayRef, at::IntArrayRef, std::array<bool, 3>);
  60. DECLARE_DISPATCH(slow_conv_dilated3d_backward_fn, slow_conv_dilated3d_backward_stub)
  61. using slow_conv_transpose2d_backward_fn = std::tuple<at::Tensor,at::Tensor,at::Tensor>(*)(
  62. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  63. at::IntArrayRef, at::IntArrayRef, at::IntArrayRef, std::array<bool,3>);
  64. DECLARE_DISPATCH(slow_conv_transpose2d_backward_fn, slow_conv_transpose2d_backward_stub)
  65. using slow_conv_transpose3d_backward_fn = std::tuple<at::Tensor,at::Tensor,at::Tensor>(*)(
  66. const at::Tensor&, const at::Tensor&, const at::Tensor&, at::IntArrayRef, at::IntArrayRef,
  67. at::IntArrayRef, at::IntArrayRef, at::IntArrayRef, std::array<bool,3>);
  68. DECLARE_DISPATCH(slow_conv_transpose3d_backward_fn, slow_conv_transpose3d_backward_stub)
  69. namespace {
  70. bool is_cudnnv8_heuristic_mode_b() {
  71. static const bool is_cudnnv8_heuristic_mode_b = c10::utils::check_env("TORCH_CUDNN_USE_HEURISTIC_MODE_B") == true;
  72. return is_cudnnv8_heuristic_mode_b;
  73. }
  74. }
  75. inline bool cudnnv8_enabled_check_debug() {
  76. static bool cudnnv8_flag = c10::utils::check_env("TORCH_CUDNN_V8_API_DISABLED") != true;
  77. static bool cudnnv8_debug = c10::utils::check_env("TORCH_CUDNN_V8_API_DEBUG") == true;
  78. static uint8_t cudnnv8_debugcount = 0;
  79. if (cudnnv8_debug == 1 && cudnnv8_debugcount < 10) {
  80. TORCH_WARN("TORCH_CUDNN_V8_DEBUG ON, V8 ON: ", cudnnv8_flag, " TORCH_CUDNN_USE_HEURISTIC_MODE B: ", is_cudnnv8_heuristic_mode_b());
  81. cudnnv8_debugcount++;
  82. }
  83. return cudnnv8_flag == 1;
  84. }
  85. inline bool cudnnv8_use_heur_mode_b() {
  86. return is_cudnnv8_heuristic_mode_b();
  87. }
  88. // Keep in sync with py::enum_ in Module.cpp
  89. enum class ConvBackend {
  90. CudaDepthwise2d,
  91. CudaDepthwise3d,
  92. Cudnn,
  93. CudnnTranspose,
  94. Empty,
  95. Miopen,
  96. MiopenDepthwise,
  97. MiopenTranspose,
  98. Mkldnn,
  99. MkldnnTranspose,
  100. MkldnnEmpty,
  101. NnpackSpatial,
  102. Overrideable,
  103. Slow2d,
  104. Slow3d,
  105. SlowDilated2d,
  106. SlowDilated3d,
  107. SlowTranspose2d,
  108. SlowTranspose3d,
  109. Winograd3x3Depthwise,
  110. Xnnpack2d,
  111. Mps,
  112. MpsTranspose,
  113. };
  114. // Overload for selecting the convolution backend from the full set of convolution inputs.
  115. // This overload is exposed to python for testing, etc.
  116. TORCH_API ConvBackend select_conv_backend(
  117. const Tensor& input, const Tensor& weight, const std::optional<Tensor>& bias_opt,
  118. SymIntArrayRef stride, SymIntArrayRef padding, SymIntArrayRef dilation,
  119. bool transposed, SymIntArrayRef output_padding, c10::SymInt groups, const at::OptionalSymIntArrayRef bias_sizes_opt);
  120. TORCH_API at::MemoryFormat _determine_backend_memory_format(const Tensor& input,
  121. const Tensor& weight,
  122. const ConvBackend backend);
  123. // ---------------------------------------------------------------------
  124. //
  125. // Math
  126. //
  127. // ---------------------------------------------------------------------
  128. constexpr int input_batch_size_dim = 0; // also grad_input
  129. constexpr int input_channels_dim = 1;
  130. constexpr int output_batch_size_dim = 0; // also grad_output
  131. constexpr int output_channels_dim = 1;
  132. constexpr int weight_output_channels_dim = 0;
  133. constexpr int weight_input_channels_dim = 1;
  134. // Often written as 2 + max_dim (extra dims for batch size and channels)
  135. constexpr int max_dim = 3;
  136. // ---------------------------------------------------------------------
  137. //
  138. // Checking
  139. //
  140. // ---------------------------------------------------------------------
  141. // Used on pad, stride and dilation
  142. static void check_args(CheckedFrom c, IntArrayRef args, size_t expected_size, const char* arg_name)
  143. {
  144. TORCH_CHECK(args.size() <= expected_size,
  145. "Too many ", arg_name, " values (", args.size(), ") supplied, expecting ",
  146. expected_size, " (while checking arguments for ", c, ")");
  147. TORCH_CHECK(args.size() >= expected_size,
  148. "Not enough ", arg_name, " values (", args.size(), ") supplied, expecting ",
  149. expected_size, " (while checking arguments for ", c, ")");
  150. auto num_negative_values = std::count_if(args.begin(), args.end(), [](int x){return x < 0;});
  151. if (num_negative_values > 0){
  152. std::stringstream ss;
  153. ss << arg_name << " should be greater than zero but got (";
  154. std::copy(args.begin(), args.end() - 1, std::ostream_iterator<int>(ss,", "));
  155. ss << args.back() << ")" << " (while checking arguments for " << c << ")";
  156. TORCH_CHECK(false, ss.str());
  157. }
  158. }
  159. // NOTE [ Convolution checks ]
  160. //
  161. // NB: For many call sites, it is not strictly necessary to check all of
  162. // these relationships (for example, for forward convolution, we compute
  163. // the size of output ourselves, so we don't actually need to check
  164. // output. However, writing a single function that does everything
  165. // means we get to reuse it for both forwards and all backwards
  166. // variants, even when the set of "real" inputs varies. The magic of
  167. // relational computing!
  168. //
  169. // (There is one downside, which is that it is slightly harder to write
  170. // error messages which are able to distinguish between real inputs
  171. // (which the user can change) and computed inputs (which the user can
  172. // only indirectly affect). It would be an interesting exercise to
  173. // come up with a general framework to handle such situations.)
  174. inline void convolution_shape_check(
  175. CheckedFrom c,
  176. const TensorGeometryArg& input, const TensorGeometryArg& weight, const TensorGeometryArg& output,
  177. IntArrayRef padding, IntArrayRef stride, IntArrayRef dilation, int64_t groups)
  178. {
  179. check_args(c, padding, input->dim() - 2, "padding");
  180. check_args(c, stride, padding.size(), "stride");
  181. check_args(c, dilation, padding.size(), "dilation");
  182. // Input
  183. checkDimRange(c, input, 3, 6 /* exclusive */);
  184. checkSize_symint(c, input, input_channels_dim, weight->size(1) * groups);
  185. // Weight
  186. checkSameDim(c, input, weight);
  187. // TODO: check that output->size() matches output_sizes
  188. // TODO: check that weight matches output->sizes()
  189. checkSameDim(c, input, output);
  190. }
  191. // NB: conv_output_size and conv_input_size are not bijections,
  192. // as conv_output_size loses information; this is why conv_input_size
  193. // takes an extra output_padding argument to resolve the ambiguity.
  194. template <typename T>
  195. inline std::vector<T> _conv_output_size(
  196. ArrayRef<T> input_size, ArrayRef<T> weight_size,
  197. ArrayRef<T> padding, ArrayRef<T> stride, ArrayRef<T> dilation = ArrayRef<T>()
  198. ) {
  199. // ASSERT(input_size.size() > 2)
  200. // ASSERT(input_size.size() == weight_size.size())
  201. bool has_dilation = !dilation.empty();
  202. auto dim = input_size.size();
  203. std::vector<T> output_size(dim);
  204. output_size[0] = input_size[input_batch_size_dim];
  205. output_size[1] = weight_size[weight_output_channels_dim];
  206. for (const auto d : c10::irange(2, dim)) {
  207. auto dilation_ = has_dilation ? dilation[d - 2] : 1;
  208. auto kernel = dilation_ * (weight_size[d] - 1) + 1;
  209. output_size[d] = (input_size[d] + (2 * padding[d - 2]) - kernel) / stride[d - 2] + 1;
  210. }
  211. return output_size;
  212. }
  213. inline std::vector<int64_t> conv_output_size(
  214. IntArrayRef input_size, IntArrayRef weight_size,
  215. IntArrayRef padding, IntArrayRef stride, IntArrayRef dilation = IntArrayRef()
  216. ) {
  217. return _conv_output_size(input_size, weight_size, padding, stride, dilation);
  218. }
  219. inline std::vector<c10::SymInt> conv_output_size(
  220. SymIntArrayRef input_size, SymIntArrayRef weight_size,
  221. SymIntArrayRef padding, SymIntArrayRef stride, SymIntArrayRef dilation = SymIntArrayRef()
  222. ) {
  223. return _conv_output_size(input_size, weight_size, padding, stride, dilation);
  224. }
  225. template <typename T>
  226. std::vector<T> _conv_input_size(
  227. ArrayRef<T> output_size, ArrayRef<T> weight_size,
  228. ArrayRef<T> padding, ArrayRef<T> output_padding, ArrayRef<T> stride, ArrayRef<T> dilation, T groups
  229. ) {
  230. // ASSERT(output_size.size() > 2)
  231. // ASSERT(output_size.size() == weight_size.size())
  232. auto dim = output_size.size();
  233. std::vector<T> input_size(dim);
  234. input_size[0] = output_size[output_batch_size_dim];
  235. input_size[1] = weight_size[weight_input_channels_dim] * groups;
  236. for (const auto d : c10::irange(2, dim)) {
  237. auto kernel = (weight_size[d] - 1) * dilation[d - 2] + 1;
  238. input_size[d] = (output_size[d] - 1) * stride[d - 2] - (padding[d - 2] * 2) +
  239. kernel + output_padding[d - 2];
  240. }
  241. return input_size;
  242. }
  243. inline std::vector<c10::SymInt> conv_input_size(
  244. SymIntArrayRef output_size, SymIntArrayRef weight_size,
  245. SymIntArrayRef padding, SymIntArrayRef output_padding, SymIntArrayRef stride, SymIntArrayRef dilation, c10::SymInt groups
  246. ) {
  247. return _conv_input_size(output_size, weight_size, padding, output_padding, stride, dilation, std::move(groups));
  248. }
  249. inline std::vector<int64_t> conv_input_size(
  250. IntArrayRef output_size, IntArrayRef weight_size,
  251. IntArrayRef padding, IntArrayRef output_padding, IntArrayRef stride, IntArrayRef dilation, int64_t groups
  252. ) {
  253. return _conv_input_size(output_size, weight_size, padding, output_padding, stride, dilation, groups);
  254. }
  255. template <typename T>
  256. std::vector<T> _conv_weight_size(
  257. ArrayRef<T> input_size, ArrayRef<T> output_size,
  258. ArrayRef<T> padding, ArrayRef<T> output_padding, IntArrayRef stride, IntArrayRef dilation, int64_t groups
  259. ) {
  260. auto dim = input_size.size();
  261. std::vector<T> weight_size(dim);
  262. weight_size[0] = output_size[1];
  263. weight_size[1] = input_size[1] / groups;
  264. for (const auto d : c10::irange(2, dim)) {
  265. auto kernel = input_size[d] - (output_size[d] - 1) * stride[d - 2]
  266. + padding[d - 2] * 2 - output_padding[d - 2];
  267. weight_size[d] = (kernel - 1) / dilation[d - 2] + 1;
  268. }
  269. return weight_size;
  270. }
  271. inline std::vector<c10::SymInt> conv_weight_size(
  272. SymIntArrayRef input_size, SymIntArrayRef output_size,
  273. SymIntArrayRef padding, SymIntArrayRef output_padding, IntArrayRef stride, IntArrayRef dilation, int64_t groups
  274. ) {
  275. return _conv_weight_size(input_size, output_size, padding, output_padding, stride, dilation, groups);
  276. }
  277. inline std::vector<int64_t> conv_weight_size(
  278. IntArrayRef input_size, IntArrayRef output_size,
  279. IntArrayRef padding, IntArrayRef output_padding, IntArrayRef stride, IntArrayRef dilation, int64_t groups
  280. ) {
  281. return _conv_weight_size(input_size, output_size, padding, output_padding, stride, dilation, groups);
  282. }
  283. inline Tensor reshape_bias(int64_t dim, const Tensor& bias) {
  284. std::vector<int64_t> shape(dim, 1);
  285. shape[1] = -1;
  286. return bias.reshape(shape);
  287. }
  288. inline at::MemoryFormat cudnn_conv_suggest_memory_format(const at::Tensor& input, const at::Tensor& weight) {
  289. // disable NHWC for float64 input.
  290. if (!at::detail::getCUDAHooks().compiledWithCuDNN() ||
  291. input.scalar_type() == at::kDouble ||
  292. weight.scalar_type() == at::kDouble) {
  293. return at::MemoryFormat::Contiguous;
  294. }
  295. long cudnn_version = at::detail::getCUDAHooks().versionCuDNN();
  296. auto input_memory_format = input.suggest_memory_format();
  297. auto weight_memory_format = weight.suggest_memory_format();
  298. auto weight_ndim = weight.ndimension();
  299. bool can_use_cudnn_channels_last_2d = (cudnn_version >= 7603) && (weight_ndim == 4) && (
  300. (input_memory_format == at::MemoryFormat::ChannelsLast) ||
  301. (weight_memory_format == at::MemoryFormat::ChannelsLast)
  302. );
  303. if (can_use_cudnn_channels_last_2d) {
  304. return at::MemoryFormat::ChannelsLast;
  305. }
  306. bool can_use_cudnn_channels_last_3d = (cudnn_version >= 8005) && (weight_ndim == 5) && (
  307. (input_memory_format == at::MemoryFormat::ChannelsLast3d) ||
  308. (weight_memory_format == at::MemoryFormat::ChannelsLast3d)
  309. );
  310. if (can_use_cudnn_channels_last_3d) {
  311. return at::MemoryFormat::ChannelsLast3d;
  312. }
  313. return at::MemoryFormat::Contiguous;
  314. }
  315. // controls whether emptyCache will be called following cudnn conv benchmarking
  316. TORCH_API void _cudnn_set_conv_benchmark_empty_cache(bool enable);
  317. TORCH_API bool _cudnn_get_conv_benchmark_empty_cache();
  318. inline at::MemoryFormat miopen_conv_suggest_memory_format(const at::Tensor& input, const at::Tensor& weight) {
  319. // disable NHWC for float64 input.
  320. if (!at::detail::getCUDAHooks().compiledWithMIOpen() ||
  321. input.scalar_type() == at::kDouble ||
  322. weight.scalar_type() == at::kDouble) {
  323. return at::MemoryFormat::Contiguous;
  324. }
  325. // TODO: Remove PYTORCH_MIOPEN_SUGGEST_NHWC once ROCm officially supports NHWC in MIOpen
  326. // See https://github.com/pytorch/pytorch/issues/64427.
  327. // non static variable is used to be able to change environment variable in runtime for testing
  328. // enabled by default for ROCm >= 7.0.0 with miopen 3.5
  329. int miopen_version = detail::getCUDAHooks().compiledWithMIOpen() ? detail::getCUDAHooks().versionMIOpen() : 0;
  330. bool is_miopen_3_5 = miopen_version >= 30500; // ROCm 7.0
  331. bool suggest_nhwc = c10::utils::check_env("PYTORCH_MIOPEN_SUGGEST_NHWC").value_or(is_miopen_3_5);
  332. auto input_memory_format = input.suggest_memory_format();
  333. auto weight_memory_format = weight.suggest_memory_format();
  334. auto weight_ndim = weight.ndimension();
  335. bool can_use_miopen_channels_last_2d = suggest_nhwc && (weight_ndim == 4) && (
  336. (input_memory_format == at::MemoryFormat::ChannelsLast) ||
  337. (weight_memory_format == at::MemoryFormat::ChannelsLast)
  338. );
  339. if (can_use_miopen_channels_last_2d) {
  340. return at::MemoryFormat::ChannelsLast;
  341. }
  342. bool can_use_miopen_channels_last_3d = suggest_nhwc && (weight_ndim == 5) && (
  343. (input_memory_format == at::MemoryFormat::ChannelsLast3d) ||
  344. (weight_memory_format == at::MemoryFormat::ChannelsLast3d)
  345. );
  346. if (can_use_miopen_channels_last_3d) {
  347. return at::MemoryFormat::ChannelsLast3d;
  348. }
  349. return at::MemoryFormat::Contiguous;
  350. }
  351. // deprecated, but to remove would be BC-breaking
  352. inline bool miopen_conv_use_channels_last(const at::Tensor& input, const at::Tensor& weight) {
  353. return miopen_conv_suggest_memory_format(input, weight) != at::MemoryFormat::Contiguous;
  354. }
  355. inline bool mkldnn_conv_use_channels_last(const at::Tensor& input, const at::Tensor& weight) {
  356. // disable NHWC for float64 input.
  357. if (input.scalar_type() == at::kDouble ||
  358. weight.scalar_type() == at::kDouble) {
  359. return false;
  360. }
  361. // disable NHWC for MkldnnCPU tensor.
  362. if (input.is_mkldnn() || weight.is_mkldnn()) {
  363. return false;
  364. }
  365. auto input_memory_format = input.suggest_memory_format();
  366. auto weight_memory_format = weight.suggest_memory_format();
  367. bool can_use_mkldnn_channels_last_2d =
  368. (input_memory_format == at::MemoryFormat::ChannelsLast) ||
  369. (weight_memory_format == at::MemoryFormat::ChannelsLast);
  370. bool can_use_mkldnn_channels_last_3d =
  371. (input_memory_format == at::MemoryFormat::ChannelsLast3d) ||
  372. (weight_memory_format == at::MemoryFormat::ChannelsLast3d);
  373. return can_use_mkldnn_channels_last_2d || can_use_mkldnn_channels_last_3d;
  374. }
  375. inline bool thnn_conv_use_channels_last(const at::Tensor& input, const at::Tensor& weight) {
  376. auto input_memory_format = input.suggest_memory_format();
  377. auto weight_memory_format = weight.suggest_memory_format();
  378. bool can_use_thnn_channels_last_2d = input.device().is_cpu() && (
  379. (input_memory_format == at::MemoryFormat::ChannelsLast) || (
  380. weight_memory_format == at::MemoryFormat::ChannelsLast));
  381. return can_use_thnn_channels_last_2d;
  382. }
  383. inline bool xpu_conv_use_channels_last(const at::Tensor& input, const at::Tensor& weight) {
  384. // check layout only for xpu tensor.
  385. if (!input.is_xpu() || !weight.is_xpu()) {
  386. return false;
  387. }
  388. if (!input.defined() || input.is_sparse()) {
  389. // suggest channels_first
  390. return false;
  391. }
  392. auto is_channel_last = [](const at::Tensor& t) {
  393. auto fmt = t.suggest_memory_format();
  394. return fmt == at::MemoryFormat::ChannelsLast || fmt == at::MemoryFormat::ChannelsLast3d;
  395. };
  396. return is_channel_last(input) || is_channel_last(weight);
  397. }
  398. inline bool mps_conv_use_channels_last(const at::Tensor& input, const at::Tensor& weight) {
  399. // check layout only for mps tensor.
  400. if (!input.is_mps() || !weight.is_mps()) {
  401. return false;
  402. }
  403. if (!input.defined() || input.is_sparse()) {
  404. // suggest channels_first
  405. return false;
  406. }
  407. auto fmt = input.suggest_memory_format();
  408. return fmt == at::MemoryFormat::ChannelsLast || fmt == at::MemoryFormat::ChannelsLast3d;
  409. }
  410. } // namespace at::native