NumericUtils.h 5.3 KB

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