BFloat16-math.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #include <c10/util/BFloat16.h>
  4. #include <c10/util/Half.h>
  5. C10_CLANG_DIAGNOSTIC_PUSH()
  6. #if C10_CLANG_HAS_WARNING("-Wimplicit-float-conversion")
  7. C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-float-conversion")
  8. #endif
  9. namespace c10 {
  10. template <typename T>
  11. struct is_reduced_floating_point
  12. : std::integral_constant<
  13. bool,
  14. std::is_same_v<T, c10::Half> || std::is_same_v<T, c10::BFloat16>> {};
  15. template <typename T>
  16. constexpr bool is_reduced_floating_point_v =
  17. is_reduced_floating_point<T>::value;
  18. } // namespace c10
  19. namespace std {
  20. #if !defined(FBCODE_CAFFE2) && !defined(C10_NODEPRECATED)
  21. using c10::is_reduced_floating_point;
  22. using c10::is_reduced_floating_point_v;
  23. #endif // !defined(FBCODE_CAFFE2) && !defined(C10_NODEPRECATED)
  24. template <
  25. typename T,
  26. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  27. inline T acos(T a) {
  28. return std::acos(float(a));
  29. }
  30. template <
  31. typename T,
  32. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  33. inline T asin(T a) {
  34. return std::asin(float(a));
  35. }
  36. template <
  37. typename T,
  38. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  39. inline T atan(T a) {
  40. return std::atan(float(a));
  41. }
  42. template <
  43. typename T,
  44. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  45. inline T atanh(T a) {
  46. return std::atanh(float(a));
  47. }
  48. template <
  49. typename T,
  50. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  51. inline T erf(T a) {
  52. return std::erf(float(a));
  53. }
  54. template <
  55. typename T,
  56. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  57. inline T erfc(T a) {
  58. return std::erfc(float(a));
  59. }
  60. template <
  61. typename T,
  62. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  63. inline T exp(T a) {
  64. return std::exp(float(a));
  65. }
  66. template <
  67. typename T,
  68. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  69. inline T expm1(T a) {
  70. return std::expm1(float(a));
  71. }
  72. template <
  73. typename T,
  74. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  75. inline bool isfinite(T a) {
  76. return std::isfinite(float(a));
  77. }
  78. template <
  79. typename T,
  80. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  81. inline T log(T a) {
  82. return std::log(float(a));
  83. }
  84. template <
  85. typename T,
  86. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  87. inline T log10(T a) {
  88. return std::log10(float(a));
  89. }
  90. template <
  91. typename T,
  92. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  93. inline T log1p(T a) {
  94. return std::log1p(float(a));
  95. }
  96. template <
  97. typename T,
  98. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  99. inline T log2(T a) {
  100. return std::log2(float(a));
  101. }
  102. template <
  103. typename T,
  104. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  105. inline T ceil(T a) {
  106. return std::ceil(float(a));
  107. }
  108. template <
  109. typename T,
  110. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  111. inline T cos(T a) {
  112. return std::cos(float(a));
  113. }
  114. template <
  115. typename T,
  116. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  117. inline T floor(T a) {
  118. return std::floor(float(a));
  119. }
  120. template <
  121. typename T,
  122. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  123. inline T nearbyint(T a) {
  124. return std::nearbyint(float(a));
  125. }
  126. template <
  127. typename T,
  128. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  129. inline T sin(T a) {
  130. return std::sin(float(a));
  131. }
  132. template <
  133. typename T,
  134. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  135. inline T tan(T a) {
  136. return std::tan(float(a));
  137. }
  138. template <
  139. typename T,
  140. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  141. inline T sinh(T a) {
  142. return std::sinh(float(a));
  143. }
  144. template <
  145. typename T,
  146. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  147. inline T cosh(T a) {
  148. return std::cosh(float(a));
  149. }
  150. template <
  151. typename T,
  152. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  153. inline T tanh(T a) {
  154. return std::tanh(float(a));
  155. }
  156. template <
  157. typename T,
  158. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  159. inline T trunc(T a) {
  160. return std::trunc(float(a));
  161. }
  162. template <
  163. typename T,
  164. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  165. inline T lgamma(T a) {
  166. return std::lgamma(float(a));
  167. }
  168. template <
  169. typename T,
  170. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  171. inline T sqrt(T a) {
  172. return std::sqrt(float(a));
  173. }
  174. template <
  175. typename T,
  176. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  177. inline T rsqrt(T a) {
  178. return 1.0 / std::sqrt(float(a));
  179. }
  180. template <
  181. typename T,
  182. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  183. inline T abs(T a) {
  184. return std::abs(float(a));
  185. }
  186. #if defined(_MSC_VER) && defined(__CUDACC__)
  187. template <
  188. typename T,
  189. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  190. inline T pow(T a, double b) {
  191. return std::pow(float(a), float(b));
  192. }
  193. #else
  194. template <
  195. typename T,
  196. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  197. inline T pow(T a, double b) {
  198. return std::pow(float(a), b);
  199. }
  200. #endif
  201. template <
  202. typename T,
  203. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  204. inline T pow(T a, T b) {
  205. return std::pow(float(a), float(b));
  206. }
  207. template <
  208. typename T,
  209. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  210. inline T fmod(T a, T b) {
  211. return std::fmod(float(a), float(b));
  212. }
  213. /*
  214. The following function is inspired from the implementation in `musl`
  215. Link to License: https://git.musl-libc.org/cgit/musl/tree/COPYRIGHT
  216. ----------------------------------------------------------------------
  217. Copyright © 2005-2020 Rich Felker, et al.
  218. Permission is hereby granted, free of charge, to any person obtaining
  219. a copy of this software and associated documentation files (the
  220. "Software"), to deal in the Software without restriction, including
  221. without limitation the rights to use, copy, modify, merge, publish,
  222. distribute, sublicense, and/or sell copies of the Software, and to
  223. permit persons to whom the Software is furnished to do so, subject to
  224. the following conditions:
  225. The above copyright notice and this permission notice shall be
  226. included in all copies or substantial portions of the Software.
  227. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  228. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  229. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  230. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  231. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  232. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  233. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  234. ----------------------------------------------------------------------
  235. */
  236. template <
  237. typename T,
  238. typename std::enable_if_t<c10::is_reduced_floating_point_v<T>, int> = 0>
  239. C10_HOST_DEVICE inline T nextafter(T from, T to) {
  240. // Reference:
  241. // https://git.musl-libc.org/cgit/musl/tree/src/math/nextafter.c
  242. using int_repr_t = uint16_t;
  243. constexpr uint8_t bits = 16;
  244. union {
  245. T f;
  246. int_repr_t i;
  247. } ufrom = {from}, uto = {to};
  248. // get a mask to get the sign bit i.e. MSB
  249. int_repr_t sign_mask = int_repr_t{1} << (bits - 1);
  250. // short-circuit: if either is NaN, return NaN
  251. if (from != from || to != to) {
  252. return from + to;
  253. }
  254. // short-circuit: if they are exactly the same.
  255. if (ufrom.i == uto.i) {
  256. return from;
  257. }
  258. // mask the sign-bit to zero i.e. positive
  259. // equivalent to abs(x)
  260. int_repr_t abs_from = ufrom.i & ~sign_mask;
  261. int_repr_t abs_to = uto.i & ~sign_mask;
  262. if (abs_from == 0) {
  263. // if both are zero but with different sign,
  264. // preserve the sign of `to`.
  265. if (abs_to == 0) {
  266. return to;
  267. }
  268. // smallest subnormal with sign of `to`.
  269. ufrom.i = (uto.i & sign_mask) | int_repr_t{1};
  270. return ufrom.f;
  271. }
  272. // if abs(from) > abs(to) or sign(from) != sign(to)
  273. if (abs_from > abs_to || ((ufrom.i ^ uto.i) & sign_mask)) {
  274. ufrom.i--;
  275. } else {
  276. ufrom.i++;
  277. }
  278. return ufrom.f;
  279. }
  280. } // namespace std
  281. C10_CLANG_DIAGNOSTIC_POP()
  282. #else
  283. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  284. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)