swin_window_process.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <torch/torch.h>
  17. #include <torch/extension.h>
  18. at::Tensor roll_and_window_partition_forward_cuda(
  19. at::Tensor & input,
  20. //at::Tensor & output,
  21. const int B,
  22. const int H,
  23. const int W,
  24. const int C,
  25. const int shift_size,
  26. const int window_size);
  27. at::Tensor roll_and_window_partition_backward_cuda(
  28. at::Tensor & grad_in,
  29. //at::Tensor & grad_out,
  30. const int B,
  31. const int H,
  32. const int W,
  33. const int C,
  34. const int shift_size,
  35. const int window_size);
  36. at::Tensor window_merge_and_roll_forward_cuda(
  37. at::Tensor & input,
  38. //at::Tensor & output,
  39. const int B,
  40. const int H,
  41. const int W,
  42. const int C,
  43. const int shift_size,
  44. const int window_size);
  45. at::Tensor window_merge_and_roll_backward_cuda(
  46. at::Tensor & grad_in,
  47. //at::Tensor & grad_out,
  48. const int B,
  49. const int H,
  50. const int W,
  51. const int C,
  52. const int shift_size,
  53. const int window_size);
  54. #define CHECK_CUDA(x) AT_ASSERTM(x.type().is_cuda(), #x " must be a CUDA tensor")
  55. #define CHECK_CONTIGUOUS(x) AT_ASSERTM(x.is_contiguous(), #x " must be contiguous")
  56. #define CHECK_INPUT(x) CHECK_CUDA(x); CHECK_CONTIGUOUS(x)
  57. at::Tensor roll_and_window_partition_forward(
  58. at::Tensor & input,
  59. //at::Tensor & output,
  60. const int B,
  61. const int H,
  62. const int W,
  63. const int C,
  64. const int shift_size,
  65. const int window_size){
  66. CHECK_INPUT(input);
  67. return roll_and_window_partition_forward_cuda(input, B, H, W, C, shift_size, window_size);
  68. }
  69. at::Tensor roll_and_window_partition_backward(
  70. at::Tensor & grad_in,
  71. //at::Tensor & grad_out,
  72. const int B,
  73. const int H,
  74. const int W,
  75. const int C,
  76. const int shift_size,
  77. const int window_size){
  78. CHECK_INPUT(grad_in);
  79. return roll_and_window_partition_backward_cuda(grad_in, B, H, W, C, shift_size, window_size);
  80. }
  81. at::Tensor window_merge_and_roll_forward(
  82. at::Tensor & input,
  83. //at::Tensor & output,
  84. const int B,
  85. const int H,
  86. const int W,
  87. const int C,
  88. const int shift_size,
  89. const int window_size){
  90. CHECK_INPUT(input);
  91. return window_merge_and_roll_forward_cuda(input, B, H, W, C, shift_size, window_size);
  92. }
  93. at::Tensor window_merge_and_roll_backward(
  94. at::Tensor & grad_in,
  95. //at::Tensor & grad_out,
  96. const int B,
  97. const int H,
  98. const int W,
  99. const int C,
  100. const int shift_size,
  101. const int window_size){
  102. CHECK_INPUT(grad_in);
  103. return window_merge_and_roll_backward_cuda(grad_in, B, H, W, C, shift_size, window_size);
  104. }
  105. PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  106. m.def("roll_and_window_partition_forward", &roll_and_window_partition_forward, "torch.roll and window_partition.");
  107. m.def("roll_and_window_partition_backward", &roll_and_window_partition_backward, "torch.roll and window_partition.");
  108. m.def("window_merge_and_roll_forward", &window_merge_and_roll_forward, "window merge and torch.roll.");
  109. m.def("window_merge_and_roll_backward", &window_merge_and_roll_backward, "window merge and torch.roll.");
  110. }