NumericUtils.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #pragma once
  2. #ifdef __HIPCC__
  3. #include <hip/hip_runtime.h>
  4. #endif
  5. #include <c10/macros/Macros.h>
  6. #include <c10/util/BFloat16.h>
  7. #include <c10/util/Float8_e4m3fn.h>
  8. #include <c10/util/Float8_e4m3fnuz.h>
  9. #include <c10/util/Float8_e5m2.h>
  10. #include <c10/util/Float8_e5m2fnuz.h>
  11. #include <c10/util/Half.h>
  12. #include <c10/util/complex.h>
  13. #include <cmath>
  14. #include <type_traits>
  15. namespace at {
  16. // std::isnan isn't performant to use on integral types; it will
  17. // (uselessly) convert to floating point and then do the test.
  18. // This function is.
  19. template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
  20. inline C10_HOST_DEVICE bool _isnan(T /*val*/) {
  21. return false;
  22. }
  23. template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
  24. inline C10_HOST_DEVICE bool _isnan(T val) {
  25. #if defined(__CUDACC__) || defined(__HIPCC__)
  26. return ::isnan(val);
  27. #else
  28. return std::isnan(val);
  29. #endif
  30. }
  31. template <typename T, std::enable_if_t<c10::is_complex<T>::value, int> = 0>
  32. inline C10_HOST_DEVICE bool _isnan(T val) {
  33. return std::isnan(val.real()) || std::isnan(val.imag());
  34. }
  35. template <typename T, std::enable_if_t<std::is_same_v<T, at::Half>, int> = 0>
  36. inline C10_HOST_DEVICE bool _isnan(T val) {
  37. return at::_isnan(static_cast<float>(val));
  38. }
  39. template <
  40. typename T,
  41. std::enable_if_t<std::is_same_v<T, at::BFloat16>, int> = 0>
  42. inline C10_HOST_DEVICE bool _isnan(at::BFloat16 val) {
  43. return at::_isnan(static_cast<float>(val));
  44. }
  45. inline C10_HOST_DEVICE bool _isnan(at::BFloat16 val) {
  46. return at::_isnan(static_cast<float>(val));
  47. }
  48. template <
  49. typename T,
  50. std::enable_if_t<std::is_same_v<T, at::Float8_e5m2>, int> = 0>
  51. inline C10_HOST_DEVICE bool _isnan(T val) {
  52. return val.isnan();
  53. }
  54. template <
  55. typename T,
  56. std::enable_if_t<std::is_same_v<T, at::Float8_e4m3fn>, int> = 0>
  57. inline C10_HOST_DEVICE bool _isnan(T val) {
  58. return val.isnan();
  59. }
  60. template <
  61. typename T,
  62. std::enable_if_t<std::is_same_v<T, at::Float8_e5m2fnuz>, int> = 0>
  63. inline C10_HOST_DEVICE bool _isnan(T val) {
  64. return val.isnan();
  65. }
  66. template <
  67. typename T,
  68. std::enable_if_t<std::is_same_v<T, at::Float8_e4m3fnuz>, int> = 0>
  69. inline C10_HOST_DEVICE bool _isnan(T val) {
  70. return val.isnan();
  71. }
  72. // std::isinf isn't performant to use on integral types; it will
  73. // (uselessly) convert to floating point and then do the test.
  74. // This function is.
  75. template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
  76. inline C10_HOST_DEVICE bool _isinf(T /*val*/) {
  77. return false;
  78. }
  79. template <typename T, std::enable_if_t<std::is_floating_point_v<T>, int> = 0>
  80. inline C10_HOST_DEVICE bool _isinf(T val) {
  81. #if defined(__CUDACC__) || defined(__HIPCC__)
  82. return ::isinf(val);
  83. #else
  84. return std::isinf(val);
  85. #endif
  86. }
  87. inline C10_HOST_DEVICE bool _isinf(at::Half val) {
  88. return at::_isinf(static_cast<float>(val));
  89. }
  90. inline C10_HOST_DEVICE bool _isinf(at::BFloat16 val) {
  91. return at::_isinf(static_cast<float>(val));
  92. }
  93. inline C10_HOST_DEVICE bool _isinf(at::Float8_e5m2 val) {
  94. return val.isinf();
  95. }
  96. inline C10_HOST_DEVICE bool _isinf(at::Float8_e4m3fn val) {
  97. return false;
  98. }
  99. inline C10_HOST_DEVICE bool _isinf(at::Float8_e5m2fnuz val) {
  100. return false;
  101. }
  102. inline C10_HOST_DEVICE bool _isinf(at::Float8_e4m3fnuz val) {
  103. return false;
  104. }
  105. template <typename T>
  106. C10_HOST_DEVICE inline T exp(T x) {
  107. static_assert(
  108. !std::is_same_v<T, double>,
  109. "this template must be used with float or less precise type");
  110. #if defined(__CUDA_ARCH__) || defined(__HIP_ARCH__)
  111. // use __expf fast approximation for peak bandwidth
  112. return __expf(x);
  113. #else
  114. return ::exp(x);
  115. #endif
  116. }
  117. template <>
  118. C10_HOST_DEVICE inline double exp<double>(double x) {
  119. return ::exp(x);
  120. }
  121. template <typename T>
  122. C10_HOST_DEVICE inline T log(T x) {
  123. static_assert(
  124. !std::is_same_v<T, double>,
  125. "this template must be used with float or less precise type");
  126. #if defined(__CUDA_ARCH__) || defined(__HIP_ARCH__)
  127. // use __logf fast approximation for peak bandwidth
  128. return __logf(x);
  129. #else
  130. return ::log(x);
  131. #endif
  132. }
  133. template <>
  134. C10_HOST_DEVICE inline double log<double>(double x) {
  135. return ::log(x);
  136. }
  137. template <typename T>
  138. C10_HOST_DEVICE inline T log1p(T x) {
  139. static_assert(
  140. !std::is_same_v<T, double>,
  141. "this template must be used with float or less precise type");
  142. #if defined(__CUDA_ARCH__) || defined(__HIP_ARCH__)
  143. // use __logf fast approximation for peak bandwidth
  144. // NOTE: There is no __log1pf so unfortunately we lose precision.
  145. return __logf(1.0f + x);
  146. #else
  147. return ::log1p(x);
  148. #endif
  149. }
  150. template <>
  151. C10_HOST_DEVICE inline double log1p<double>(double x) {
  152. return ::log1p(x);
  153. }
  154. template <typename T>
  155. C10_HOST_DEVICE inline T tan(T x) {
  156. static_assert(
  157. !std::is_same_v<T, double>,
  158. "this template must be used with float or less precise type");
  159. #if defined(__CUDA_ARCH__) || defined(__HIP_ARCH__)
  160. // use __tanf fast approximation for peak bandwidth
  161. return __tanf(x);
  162. #else
  163. return ::tan(x);
  164. #endif
  165. }
  166. template <>
  167. C10_HOST_DEVICE inline double tan<double>(double x) {
  168. return ::tan(x);
  169. }
  170. } // namespace at