KernelUtils.cuh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #pragma once
  2. #include <ATen/cuda/Atomic.cuh>
  3. #if !(defined(USE_ROCM) || ((defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 800))))
  4. #include <cuda_bf16.h>
  5. #endif
  6. namespace at {
  7. namespace native {
  8. __device__ __forceinline__ size_t
  9. idx(const size_t nc,
  10. const size_t height,
  11. const size_t width,
  12. const size_t h,
  13. const size_t w) {
  14. return (nc * height + h) * width + w;
  15. }
  16. // for channels-last
  17. __device__ __forceinline__ size_t
  18. idx_cl(
  19. const size_t n, const size_t h, const size_t w, const size_t c,
  20. const size_t height, const size_t width, const size_t channel
  21. ) {
  22. return ((n * height + h) * width + w) * channel + c;
  23. }
  24. template <
  25. typename scalar_t,
  26. typename index_t,
  27. typename std::enable_if<std::is_same<c10::Half, scalar_t>::value>::type* =
  28. nullptr>
  29. __device__ __forceinline__ void fastSpecializedAtomicAdd(
  30. scalar_t* tensor,
  31. index_t index,
  32. const index_t numel,
  33. scalar_t value) {
  34. #if ( \
  35. (defined(USE_ROCM)) || \
  36. (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 700)))
  37. gpuAtomicAddNoReturn(
  38. reinterpret_cast<at::Half*>(tensor) + index,
  39. static_cast<at::Half>(value));
  40. #else
  41. // Accounts for the chance tensor falls on an odd 16 bit alignment (ie, not 32 bit aligned)
  42. __half* target_addr = reinterpret_cast<__half*>(tensor + index);
  43. bool low_byte = (reinterpret_cast<std::uintptr_t>(target_addr) % sizeof(__half2) == 0);
  44. if (low_byte && index < (numel - 1)) {
  45. __half2 value2;
  46. value2.x = value;
  47. value2.y = __int2half_rz(0);
  48. atomicAdd(reinterpret_cast<__half2*>(target_addr), value2);
  49. } else if (!low_byte && index > 0) {
  50. __half2 value2;
  51. value2.x = __int2half_rz(0);
  52. value2.y = value;
  53. atomicAdd(reinterpret_cast<__half2*>(target_addr - 1), value2);
  54. } else {
  55. atomicAdd(
  56. reinterpret_cast<__half*>(tensor) + index, static_cast<__half>(value));
  57. }
  58. #endif
  59. }
  60. template <
  61. typename scalar_t,
  62. typename index_t,
  63. typename std::enable_if<std::is_same<c10::BFloat16, scalar_t>::value>::type* =
  64. nullptr>
  65. __device__ __forceinline__ void fastSpecializedAtomicAdd(
  66. scalar_t* tensor,
  67. index_t index,
  68. const index_t numel,
  69. scalar_t value) {
  70. #if ( \
  71. (defined(USE_ROCM)) || \
  72. (defined(__CUDA_ARCH__) && (__CUDA_ARCH__ < 800)))
  73. gpuAtomicAddNoReturn(
  74. reinterpret_cast<at::BFloat16*>(tensor) + index,
  75. static_cast<at::BFloat16>(value));
  76. #else
  77. // Accounts for the chance tensor falls on an odd 16 bit alignment (ie, not 32 bit aligned)
  78. __nv_bfloat16* target_addr = reinterpret_cast<__nv_bfloat16*>(tensor + index);
  79. bool low_byte = (reinterpret_cast<std::uintptr_t>(target_addr) % sizeof(__nv_bfloat162) == 0);
  80. if (low_byte && index < (numel - 1)) {
  81. __nv_bfloat162 value2;
  82. value2.x = *reinterpret_cast<__nv_bfloat16*>(&value);
  83. value2.y = __int2bfloat16_rz(0);
  84. atomicAdd(reinterpret_cast<__nv_bfloat162*>(target_addr), value2);
  85. } else if (!low_byte && index > 0) {
  86. __nv_bfloat162 value2;
  87. value2.x = __int2bfloat16_rz(0);
  88. value2.y = *reinterpret_cast<__nv_bfloat16*>(&value);
  89. atomicAdd(reinterpret_cast<__nv_bfloat162*>(target_addr - 1), value2);
  90. } else {
  91. atomicAdd(
  92. reinterpret_cast<__nv_bfloat16*>(tensor) + index, *reinterpret_cast<__nv_bfloat16*>(&value));
  93. }
  94. #endif
  95. }
  96. template <
  97. typename scalar_t,
  98. typename index_t,
  99. typename std::enable_if<!std::is_same<c10::Half, scalar_t>::value && !std::is_same<c10::BFloat16, scalar_t>::value >::type* =
  100. nullptr>
  101. __device__ __forceinline__ void fastSpecializedAtomicAdd(
  102. scalar_t* tensor,
  103. index_t index,
  104. const index_t numel,
  105. scalar_t value) {
  106. gpuAtomicAddNoReturn(tensor + index, value);
  107. }
  108. template <class scalar_t, class index_t>
  109. __device__ __forceinline__ void fastAtomicAdd(
  110. scalar_t* tensor,
  111. index_t index,
  112. const index_t numel,
  113. scalar_t value,
  114. bool fast_atomics) {
  115. if (fast_atomics) {
  116. fastSpecializedAtomicAdd(tensor, index, numel, value);
  117. } else {
  118. gpuAtomicAddNoReturn(tensor + index, value);
  119. }
  120. }
  121. } // namespace native
  122. } // namespace at