Half.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. #pragma once
  2. /// Defines the Half type (half-precision floating-point) including conversions
  3. /// to standard C types and basic arithmetic operations. Note that arithmetic
  4. /// operations are implemented by converting to floating point and
  5. /// performing the operation in float32, instead of using CUDA half intrinsics.
  6. /// Most uses of this type within ATen are memory bound, including the
  7. /// element-wise kernels, and the half intrinsics aren't efficient on all GPUs.
  8. /// If you are writing a compute bound kernel, you can use the CUDA half
  9. /// intrinsics directly on the Half type from device code.
  10. #include <c10/macros/Macros.h>
  11. #include <c10/util/C++17.h>
  12. #include <c10/util/TypeSafeSignMath.h>
  13. #include <c10/util/complex.h>
  14. #include <type_traits>
  15. #if defined(__cplusplus) && (__cplusplus >= 201103L)
  16. #include <cmath>
  17. #include <cstdint>
  18. #elif !defined(__OPENCL_VERSION__)
  19. #include <math.h>
  20. #include <stdint.h>
  21. #endif
  22. #ifdef _MSC_VER
  23. #include <intrin.h>
  24. #endif
  25. #include <complex>
  26. #include <cstdint>
  27. #include <cstring>
  28. #include <iosfwd>
  29. #include <limits>
  30. #include <sstream>
  31. #include <stdexcept>
  32. #include <string>
  33. #include <utility>
  34. #ifdef __CUDACC__
  35. #include <cuda_fp16.h>
  36. #endif
  37. #ifdef __HIPCC__
  38. #include <hip/hip_fp16.h>
  39. #endif
  40. #if defined(CL_SYCL_LANGUAGE_VERSION)
  41. #include <CL/sycl.hpp> // for SYCL 1.2.1
  42. #elif defined(SYCL_LANGUAGE_VERSION)
  43. #include <sycl/sycl.hpp> // for SYCL 2020
  44. #endif
  45. // Standard check for compiling CUDA with clang
  46. #if defined(__clang__) && defined(__CUDA__) && defined(__CUDA_ARCH__)
  47. #define C10_DEVICE_HOST_FUNCTION __device__ __host__
  48. #else
  49. #define C10_DEVICE_HOST_FUNCTION
  50. #endif
  51. #include <typeinfo> // operator typeid
  52. namespace c10 {
  53. namespace detail {
  54. C10_DEVICE_HOST_FUNCTION inline float fp32_from_bits(uint32_t w) {
  55. #if defined(__OPENCL_VERSION__)
  56. return as_float(w);
  57. #elif defined(__CUDA_ARCH__)
  58. return __uint_as_float((unsigned int)w);
  59. #elif defined(__INTEL_COMPILER)
  60. return _castu32_f32(w);
  61. #else
  62. union {
  63. uint32_t as_bits;
  64. float as_value;
  65. } fp32 = {w};
  66. return fp32.as_value;
  67. #endif
  68. }
  69. C10_DEVICE_HOST_FUNCTION inline uint32_t fp32_to_bits(float f) {
  70. #if defined(__OPENCL_VERSION__)
  71. return as_uint(f);
  72. #elif defined(__CUDA_ARCH__)
  73. return (uint32_t)__float_as_uint(f);
  74. #elif defined(__INTEL_COMPILER)
  75. return _castf32_u32(f);
  76. #else
  77. union {
  78. float as_value;
  79. uint32_t as_bits;
  80. } fp32 = {f};
  81. return fp32.as_bits;
  82. #endif
  83. }
  84. /*
  85. * Convert a 16-bit floating-point number in IEEE half-precision format, in bit
  86. * representation, to a 32-bit floating-point number in IEEE single-precision
  87. * format, in bit representation.
  88. *
  89. * @note The implementation doesn't use any floating-point operations.
  90. */
  91. inline uint32_t fp16_ieee_to_fp32_bits(uint16_t h) {
  92. /*
  93. * Extend the half-precision floating-point number to 32 bits and shift to the
  94. * upper part of the 32-bit word:
  95. * +---+-----+------------+-------------------+
  96. * | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
  97. * +---+-----+------------+-------------------+
  98. * Bits 31 26-30 16-25 0-15
  99. *
  100. * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0
  101. * - zero bits.
  102. */
  103. const uint32_t w = (uint32_t)h << 16;
  104. /*
  105. * Extract the sign of the input number into the high bit of the 32-bit word:
  106. *
  107. * +---+----------------------------------+
  108. * | S |0000000 00000000 00000000 00000000|
  109. * +---+----------------------------------+
  110. * Bits 31 0-31
  111. */
  112. const uint32_t sign = w & UINT32_C(0x80000000);
  113. /*
  114. * Extract mantissa and biased exponent of the input number into the bits 0-30
  115. * of the 32-bit word:
  116. *
  117. * +---+-----+------------+-------------------+
  118. * | 0 |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
  119. * +---+-----+------------+-------------------+
  120. * Bits 30 27-31 17-26 0-16
  121. */
  122. const uint32_t nonsign = w & UINT32_C(0x7FFFFFFF);
  123. /*
  124. * Renorm shift is the number of bits to shift mantissa left to make the
  125. * half-precision number normalized. If the initial number is normalized, some
  126. * of its high 6 bits (sign == 0 and 5-bit exponent) equals one. In this case
  127. * renorm_shift == 0. If the number is denormalize, renorm_shift > 0. Note
  128. * that if we shift denormalized nonsign by renorm_shift, the unit bit of
  129. * mantissa will shift into exponent, turning the biased exponent into 1, and
  130. * making mantissa normalized (i.e. without leading 1).
  131. */
  132. #ifdef _MSC_VER
  133. unsigned long nonsign_bsr;
  134. _BitScanReverse(&nonsign_bsr, (unsigned long)nonsign);
  135. uint32_t renorm_shift = (uint32_t)nonsign_bsr ^ 31;
  136. #else
  137. uint32_t renorm_shift = __builtin_clz(nonsign);
  138. #endif
  139. renorm_shift = renorm_shift > 5 ? renorm_shift - 5 : 0;
  140. /*
  141. * Iff half-precision number has exponent of 15, the addition overflows
  142. * it into bit 31, and the subsequent shift turns the high 9 bits
  143. * into 1. Thus inf_nan_mask == 0x7F800000 if the half-precision number
  144. * had exponent of 15 (i.e. was NaN or infinity) 0x00000000 otherwise
  145. */
  146. const int32_t inf_nan_mask =
  147. ((int32_t)(nonsign + 0x04000000) >> 8) & INT32_C(0x7F800000);
  148. /*
  149. * Iff nonsign is 0, it overflows into 0xFFFFFFFF, turning bit 31
  150. * into 1. Otherwise, bit 31 remains 0. The signed shift right by 31
  151. * broadcasts bit 31 into all bits of the zero_mask. Thus zero_mask ==
  152. * 0xFFFFFFFF if the half-precision number was zero (+0.0h or -0.0h)
  153. * 0x00000000 otherwise
  154. */
  155. const int32_t zero_mask = (int32_t)(nonsign - 1) >> 31;
  156. /*
  157. * 1. Shift nonsign left by renorm_shift to normalize it (if the input
  158. * was denormal)
  159. * 2. Shift nonsign right by 3 so the exponent (5 bits originally)
  160. * becomes an 8-bit field and 10-bit mantissa shifts into the 10 high
  161. * bits of the 23-bit mantissa of IEEE single-precision number.
  162. * 3. Add 0x70 to the exponent (starting at bit 23) to compensate the
  163. * different in exponent bias (0x7F for single-precision number less 0xF
  164. * for half-precision number).
  165. * 4. Subtract renorm_shift from the exponent (starting at bit 23) to
  166. * account for renormalization. As renorm_shift is less than 0x70, this
  167. * can be combined with step 3.
  168. * 5. Binary OR with inf_nan_mask to turn the exponent into 0xFF if the
  169. * input was NaN or infinity.
  170. * 6. Binary ANDNOT with zero_mask to turn the mantissa and exponent
  171. * into zero if the input was zero.
  172. * 7. Combine with the sign of the input number.
  173. */
  174. return sign |
  175. ((((nonsign << renorm_shift >> 3) + ((0x70 - renorm_shift) << 23)) |
  176. inf_nan_mask) &
  177. ~zero_mask);
  178. }
  179. /*
  180. * Convert a 16-bit floating-point number in IEEE half-precision format, in bit
  181. * representation, to a 32-bit floating-point number in IEEE single-precision
  182. * format.
  183. *
  184. * @note The implementation relies on IEEE-like (no assumption about rounding
  185. * mode and no operations on denormals) floating-point operations and bitcasts
  186. * between integer and floating-point variables.
  187. */
  188. inline float fp16_ieee_to_fp32_value(uint16_t h) {
  189. /*
  190. * Extend the half-precision floating-point number to 32 bits and shift to the
  191. * upper part of the 32-bit word:
  192. * +---+-----+------------+-------------------+
  193. * | S |EEEEE|MM MMMM MMMM|0000 0000 0000 0000|
  194. * +---+-----+------------+-------------------+
  195. * Bits 31 26-30 16-25 0-15
  196. *
  197. * S - sign bit, E - bits of the biased exponent, M - bits of the mantissa, 0
  198. * - zero bits.
  199. */
  200. const uint32_t w = (uint32_t)h << 16;
  201. /*
  202. * Extract the sign of the input number into the high bit of the 32-bit word:
  203. *
  204. * +---+----------------------------------+
  205. * | S |0000000 00000000 00000000 00000000|
  206. * +---+----------------------------------+
  207. * Bits 31 0-31
  208. */
  209. const uint32_t sign = w & UINT32_C(0x80000000);
  210. /*
  211. * Extract mantissa and biased exponent of the input number into the high bits
  212. * of the 32-bit word:
  213. *
  214. * +-----+------------+---------------------+
  215. * |EEEEE|MM MMMM MMMM|0 0000 0000 0000 0000|
  216. * +-----+------------+---------------------+
  217. * Bits 27-31 17-26 0-16
  218. */
  219. const uint32_t two_w = w + w;
  220. /*
  221. * Shift mantissa and exponent into bits 23-28 and bits 13-22 so they become
  222. * mantissa and exponent of a single-precision floating-point number:
  223. *
  224. * S|Exponent | Mantissa
  225. * +-+---+-----+------------+----------------+
  226. * |0|000|EEEEE|MM MMMM MMMM|0 0000 0000 0000|
  227. * +-+---+-----+------------+----------------+
  228. * Bits | 23-31 | 0-22
  229. *
  230. * Next, there are some adjustments to the exponent:
  231. * - The exponent needs to be corrected by the difference in exponent bias
  232. * between single-precision and half-precision formats (0x7F - 0xF = 0x70)
  233. * - Inf and NaN values in the inputs should become Inf and NaN values after
  234. * conversion to the single-precision number. Therefore, if the biased
  235. * exponent of the half-precision input was 0x1F (max possible value), the
  236. * biased exponent of the single-precision output must be 0xFF (max possible
  237. * value). We do this correction in two steps:
  238. * - First, we adjust the exponent by (0xFF - 0x1F) = 0xE0 (see exp_offset
  239. * below) rather than by 0x70 suggested by the difference in the exponent bias
  240. * (see above).
  241. * - Then we multiply the single-precision result of exponent adjustment by
  242. * 2**(-112) to reverse the effect of exponent adjustment by 0xE0 less the
  243. * necessary exponent adjustment by 0x70 due to difference in exponent bias.
  244. * The floating-point multiplication hardware would ensure than Inf and
  245. * NaN would retain their value on at least partially IEEE754-compliant
  246. * implementations.
  247. *
  248. * Note that the above operations do not handle denormal inputs (where biased
  249. * exponent == 0). However, they also do not operate on denormal inputs, and
  250. * do not produce denormal results.
  251. */
  252. constexpr uint32_t exp_offset = UINT32_C(0xE0) << 23;
  253. // const float exp_scale = 0x1.0p-112f;
  254. constexpr uint32_t scale_bits = (uint32_t)15 << 23;
  255. float exp_scale_val;
  256. std::memcpy(&exp_scale_val, &scale_bits, sizeof(exp_scale_val));
  257. const float exp_scale = exp_scale_val;
  258. const float normalized_value =
  259. fp32_from_bits((two_w >> 4) + exp_offset) * exp_scale;
  260. /*
  261. * Convert denormalized half-precision inputs into single-precision results
  262. * (always normalized). Zero inputs are also handled here.
  263. *
  264. * In a denormalized number the biased exponent is zero, and mantissa has
  265. * on-zero bits. First, we shift mantissa into bits 0-9 of the 32-bit word.
  266. *
  267. * zeros | mantissa
  268. * +---------------------------+------------+
  269. * |0000 0000 0000 0000 0000 00|MM MMMM MMMM|
  270. * +---------------------------+------------+
  271. * Bits 10-31 0-9
  272. *
  273. * Now, remember that denormalized half-precision numbers are represented as:
  274. * FP16 = mantissa * 2**(-24).
  275. * The trick is to construct a normalized single-precision number with the
  276. * same mantissa and thehalf-precision input and with an exponent which would
  277. * scale the corresponding mantissa bits to 2**(-24). A normalized
  278. * single-precision floating-point number is represented as: FP32 = (1 +
  279. * mantissa * 2**(-23)) * 2**(exponent - 127) Therefore, when the biased
  280. * exponent is 126, a unit change in the mantissa of the input denormalized
  281. * half-precision number causes a change of the constructud single-precision
  282. * number by 2**(-24), i.e. the same amount.
  283. *
  284. * The last step is to adjust the bias of the constructed single-precision
  285. * number. When the input half-precision number is zero, the constructed
  286. * single-precision number has the value of FP32 = 1 * 2**(126 - 127) =
  287. * 2**(-1) = 0.5 Therefore, we need to subtract 0.5 from the constructed
  288. * single-precision number to get the numerical equivalent of the input
  289. * half-precision number.
  290. */
  291. constexpr uint32_t magic_mask = UINT32_C(126) << 23;
  292. constexpr float magic_bias = 0.5f;
  293. const float denormalized_value =
  294. fp32_from_bits((two_w >> 17) | magic_mask) - magic_bias;
  295. /*
  296. * - Choose either results of conversion of input as a normalized number, or
  297. * as a denormalized number, depending on the input exponent. The variable
  298. * two_w contains input exponent in bits 27-31, therefore if its smaller than
  299. * 2**27, the input is either a denormal number, or zero.
  300. * - Combine the result of conversion of exponent and mantissa with the sign
  301. * of the input number.
  302. */
  303. constexpr uint32_t denormalized_cutoff = UINT32_C(1) << 27;
  304. const uint32_t result = sign |
  305. (two_w < denormalized_cutoff ? fp32_to_bits(denormalized_value)
  306. : fp32_to_bits(normalized_value));
  307. return fp32_from_bits(result);
  308. }
  309. /*
  310. * Convert a 32-bit floating-point number in IEEE single-precision format to a
  311. * 16-bit floating-point number in IEEE half-precision format, in bit
  312. * representation.
  313. *
  314. * @note The implementation relies on IEEE-like (no assumption about rounding
  315. * mode and no operations on denormals) floating-point operations and bitcasts
  316. * between integer and floating-point variables.
  317. */
  318. inline uint16_t fp16_ieee_from_fp32_value(float f) {
  319. // const float scale_to_inf = 0x1.0p+112f;
  320. // const float scale_to_zero = 0x1.0p-110f;
  321. constexpr uint32_t scale_to_inf_bits = (uint32_t)239 << 23;
  322. constexpr uint32_t scale_to_zero_bits = (uint32_t)17 << 23;
  323. float scale_to_inf_val, scale_to_zero_val;
  324. std::memcpy(&scale_to_inf_val, &scale_to_inf_bits, sizeof(scale_to_inf_val));
  325. std::memcpy(
  326. &scale_to_zero_val, &scale_to_zero_bits, sizeof(scale_to_zero_val));
  327. const float scale_to_inf = scale_to_inf_val;
  328. const float scale_to_zero = scale_to_zero_val;
  329. #if defined(_MSC_VER) && _MSC_VER == 1916
  330. float base = ((signbit(f) != 0 ? -f : f) * scale_to_inf) * scale_to_zero;
  331. #else
  332. float base = (fabsf(f) * scale_to_inf) * scale_to_zero;
  333. #endif
  334. const uint32_t w = fp32_to_bits(f);
  335. const uint32_t shl1_w = w + w;
  336. const uint32_t sign = w & UINT32_C(0x80000000);
  337. uint32_t bias = shl1_w & UINT32_C(0xFF000000);
  338. if (bias < UINT32_C(0x71000000)) {
  339. bias = UINT32_C(0x71000000);
  340. }
  341. base = fp32_from_bits((bias >> 1) + UINT32_C(0x07800000)) + base;
  342. const uint32_t bits = fp32_to_bits(base);
  343. const uint32_t exp_bits = (bits >> 13) & UINT32_C(0x00007C00);
  344. const uint32_t mantissa_bits = bits & UINT32_C(0x00000FFF);
  345. const uint32_t nonsign = exp_bits + mantissa_bits;
  346. return static_cast<uint16_t>(
  347. (sign >> 16) |
  348. (shl1_w > UINT32_C(0xFF000000) ? UINT16_C(0x7E00) : nonsign));
  349. }
  350. } // namespace detail
  351. struct alignas(2) Half {
  352. unsigned short x;
  353. struct from_bits_t {};
  354. C10_HOST_DEVICE static constexpr from_bits_t from_bits() {
  355. return from_bits_t();
  356. }
  357. // HIP wants __host__ __device__ tag, CUDA does not
  358. #if defined(USE_ROCM)
  359. C10_HOST_DEVICE Half() = default;
  360. #else
  361. Half() = default;
  362. #endif
  363. constexpr C10_HOST_DEVICE Half(unsigned short bits, from_bits_t) : x(bits){};
  364. inline C10_HOST_DEVICE Half(float value);
  365. inline C10_HOST_DEVICE operator float() const;
  366. #if defined(__CUDACC__) || defined(__HIPCC__)
  367. inline C10_HOST_DEVICE Half(const __half& value);
  368. inline C10_HOST_DEVICE operator __half() const;
  369. #endif
  370. #ifdef SYCL_LANGUAGE_VERSION
  371. inline C10_HOST_DEVICE Half(const sycl::half& value);
  372. inline C10_HOST_DEVICE operator sycl::half() const;
  373. #endif
  374. };
  375. // TODO : move to complex.h
  376. template <>
  377. struct alignas(4) complex<Half> {
  378. Half real_;
  379. Half imag_;
  380. // Constructors
  381. complex() = default;
  382. // Half constructor is not constexpr so the following constructor can't
  383. // be constexpr
  384. C10_HOST_DEVICE explicit inline complex(const Half& real, const Half& imag)
  385. : real_(real), imag_(imag) {}
  386. C10_HOST_DEVICE inline complex(const c10::complex<float>& value)
  387. : real_(value.real()), imag_(value.imag()) {}
  388. // Conversion operator
  389. inline C10_HOST_DEVICE operator c10::complex<float>() const {
  390. return {real_, imag_};
  391. }
  392. constexpr C10_HOST_DEVICE Half real() const {
  393. return real_;
  394. }
  395. constexpr C10_HOST_DEVICE Half imag() const {
  396. return imag_;
  397. }
  398. C10_HOST_DEVICE complex<Half>& operator+=(const complex<Half>& other) {
  399. real_ = static_cast<float>(real_) + static_cast<float>(other.real_);
  400. imag_ = static_cast<float>(imag_) + static_cast<float>(other.imag_);
  401. return *this;
  402. }
  403. C10_HOST_DEVICE complex<Half>& operator-=(const complex<Half>& other) {
  404. real_ = static_cast<float>(real_) - static_cast<float>(other.real_);
  405. imag_ = static_cast<float>(imag_) - static_cast<float>(other.imag_);
  406. return *this;
  407. }
  408. C10_HOST_DEVICE complex<Half>& operator*=(const complex<Half>& other) {
  409. auto a = static_cast<float>(real_);
  410. auto b = static_cast<float>(imag_);
  411. auto c = static_cast<float>(other.real());
  412. auto d = static_cast<float>(other.imag());
  413. real_ = a * c - b * d;
  414. imag_ = a * d + b * c;
  415. return *this;
  416. }
  417. };
  418. // In some versions of MSVC, there will be a compiler error when building.
  419. // C4146: unary minus operator applied to unsigned type, result still unsigned
  420. // C4804: unsafe use of type 'bool' in operation
  421. // It can be addressed by disabling the following warning.
  422. #ifdef _MSC_VER
  423. #pragma warning(push)
  424. #pragma warning(disable : 4146)
  425. #pragma warning(disable : 4804)
  426. #pragma warning(disable : 4018)
  427. #endif
  428. // The overflow checks may involve float to int conversion which may
  429. // trigger precision loss warning. Re-enable the warning once the code
  430. // is fixed. See T58053069.
  431. #ifdef __clang__
  432. #pragma GCC diagnostic push
  433. #pragma GCC diagnostic ignored "-Wunknown-warning-option"
  434. #pragma GCC diagnostic ignored "-Wimplicit-int-float-conversion"
  435. #endif
  436. // bool can be converted to any type.
  437. // Without specializing on bool, in pytorch_linux_trusty_py2_7_9_build:
  438. // `error: comparison of constant '255' with boolean expression is always false`
  439. // for `f > limit::max()` below
  440. template <typename To, typename From>
  441. typename std::enable_if<std::is_same<From, bool>::value, bool>::type overflows(
  442. From /*f*/) {
  443. return false;
  444. }
  445. // skip isnan and isinf check for integral types
  446. template <typename To, typename From>
  447. typename std::enable_if<
  448. std::is_integral<From>::value && !std::is_same<From, bool>::value,
  449. bool>::type
  450. overflows(From f) {
  451. using limit = std::numeric_limits<typename scalar_value_type<To>::type>;
  452. if (!limit::is_signed && std::numeric_limits<From>::is_signed) {
  453. // allow for negative numbers to wrap using two's complement arithmetic.
  454. // For example, with uint8, this allows for `a - b` to be treated as
  455. // `a + 255 * b`.
  456. return greater_than_max<To>(f) ||
  457. (c10::is_negative(f) && -static_cast<uint64_t>(f) > limit::max());
  458. } else {
  459. return c10::less_than_lowest<To>(f) || greater_than_max<To>(f);
  460. }
  461. }
  462. template <typename To, typename From>
  463. typename std::enable_if<std::is_floating_point<From>::value, bool>::type
  464. overflows(From f) {
  465. using limit = std::numeric_limits<typename scalar_value_type<To>::type>;
  466. if (limit::has_infinity && std::isinf(static_cast<double>(f))) {
  467. return false;
  468. }
  469. if (!limit::has_quiet_NaN && (f != f)) {
  470. return true;
  471. }
  472. return f < limit::lowest() || f > limit::max();
  473. }
  474. #ifdef __clang__
  475. #pragma GCC diagnostic pop
  476. #endif
  477. #ifdef _MSC_VER
  478. #pragma warning(pop)
  479. #endif
  480. template <typename To, typename From>
  481. typename std::enable_if<is_complex<From>::value, bool>::type overflows(From f) {
  482. // casts from complex to real are considered to overflow if the
  483. // imaginary component is non-zero
  484. if (!is_complex<To>::value && f.imag() != 0) {
  485. return true;
  486. }
  487. // Check for overflow componentwise
  488. // (Technically, the imag overflow check is guaranteed to be false
  489. // when !is_complex<To>, but any optimizer worth its salt will be
  490. // able to figure it out.)
  491. return overflows<
  492. typename scalar_value_type<To>::type,
  493. typename From::value_type>(f.real()) ||
  494. overflows<
  495. typename scalar_value_type<To>::type,
  496. typename From::value_type>(f.imag());
  497. }
  498. C10_API std::ostream& operator<<(std::ostream& out, const Half& value);
  499. } // namespace c10
  500. #include <c10/util/Half-inl.h> // IWYU pragma: keep