hip_float8_impl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #pragma once
  2. #if defined(__HIPCC__) && (defined(__gfx940__) || defined(__gfx941__) || defined(__gfx942__))
  3. #define __HIP__MI300__
  4. #endif
  5. #ifdef __HIPCC__
  6. #define HIP_FP8_HOST_DEVICE __host__ __device__
  7. #define HIP_FP8_HOST __host__
  8. #define HIP_FP8_DEVICE __device__
  9. #else
  10. #define HIP_FP8_HOST_DEVICE
  11. #define HIP_FP8_HOST
  12. #define HIP_FP8_DEVICE
  13. #endif
  14. namespace hip_fp8_impl
  15. {
  16. #ifdef __HIP__MI300__
  17. HIP_FP8_DEVICE uint8_t to_fp8_from_fp32(float v)
  18. {
  19. uint8_t i8data;
  20. union {
  21. float fval;
  22. uint32_t i32val;
  23. uint8_t i8val[4]; // NOTE: not endian independent
  24. } val;
  25. uint32_t ival = 0;
  26. val.fval = v;
  27. if ((val.i32val & 0x7F800000) != 0x7F800000) { /// propagate NAN/INF, no clipping
  28. val.fval = __builtin_amdgcn_fmed3f(val.fval, 240.0, -240.0);
  29. }
  30. ival = __builtin_amdgcn_cvt_pk_fp8_f32(val.fval, val.fval, ival,
  31. false); // false -> WORD0
  32. val.i32val = ival;
  33. i8data = val.i8val[0];
  34. return i8data;
  35. }
  36. #endif // __HIP__MI300__
  37. HIP_FP8_HOST inline int clz(uint32_t x)
  38. {
  39. return __builtin_clz(x);
  40. }
  41. #if defined(__HIPCC__) || defined(__CUDA_ARCH__)
  42. HIP_FP8_DEVICE inline int clz(uint32_t x)
  43. {
  44. return __clz(x);
  45. }
  46. #endif
  47. template <int we, int wm, typename T, bool negative_zero_nan, bool clip>
  48. HIP_FP8_HOST_DEVICE uint8_t to_float8(T _x, bool stoch = false, uint32_t rng = 0)
  49. {
  50. #ifdef __HIPCC__
  51. constexpr bool is_half = std::is_same<T, _Float16>::value;
  52. #else
  53. constexpr bool is_half = false;
  54. #endif
  55. constexpr bool is_float = std::is_same<T, float>::value;
  56. static_assert(wm + we == 7, "wm+we==7");
  57. static_assert(is_half || is_float, "Only half and float can be cast to f8");
  58. const int mfmt = (sizeof(T) == 4) ? 23 : 10;
  59. uint32_t x;
  60. if (sizeof(T) == 4) {
  61. x = reinterpret_cast<uint32_t&>(_x);
  62. } else {
  63. x = reinterpret_cast<uint16_t&>(_x);
  64. }
  65. uint32_t head, mantissa;
  66. int exponent, bias;
  67. uint32_t sign;
  68. if (sizeof(T) == 4) {
  69. head = x & 0xFF800000;
  70. mantissa = x & 0x7FFFFF;
  71. exponent = (head >> 23) & 0xFF;
  72. sign = head >> 31;
  73. bias = 127;
  74. } else {
  75. head = x & 0xFC00;
  76. mantissa = x & 0x3FF;
  77. exponent = (head >> 10) & 0x1F;
  78. sign = head >> 15;
  79. bias = 15;
  80. }
  81. uint32_t signed_inf = (sign << 7) + (((1 << we) - 1) << wm);
  82. // Deal with inf and NaNs
  83. if (negative_zero_nan) {
  84. if (sizeof(T) == 4) {
  85. if ((x & 0x7F800000) == 0x7F800000) {
  86. return 0x80;
  87. }
  88. } else {
  89. // if(__hisinf(x) || __hisnan(x))
  90. if ((x & 0x7C00) == 0x7C00) {
  91. return 0x80;
  92. }
  93. }
  94. } else {
  95. if (sizeof(T) == 4) {
  96. if ((x & 0x7F800000) == 0x7F800000) {
  97. return signed_inf + (mantissa != 0 ? 1 : 0);
  98. }
  99. } else {
  100. if ((x & 0x7C00) == 0x7C00) {
  101. return signed_inf + (mantissa != 0 ? 1 : 0);
  102. }
  103. }
  104. }
  105. if (x == 0) {
  106. return 0;
  107. }
  108. // First need to check if it is normal or denorm as there is a difference of
  109. // implicit 1 Then need to adjust the exponent to align with the F8 exponent,
  110. // in the meanwhile, shift The mantissa. Then for stochastic rounding, add rng
  111. // to mantissa and truncate. And for RNE, no need to add rng. Then probably
  112. // need to check whether there is carry and adjust exponent and mantissa again
  113. // For IEEE bias mode, the bias is 2^(k-1) -1 where k is the width of exponent
  114. // bits
  115. const int f8_bias = (1 << (we - 1)) - 1 + (negative_zero_nan ? 1 : 0);
  116. const int f8_denormal_act_exponent = 1 - f8_bias; // actual exponent of f8 denormal
  117. // act_exponent is the actual exponent of fp32/fp16 (after subtracting bias)
  118. // f8_exponent is the converted f8 exponent with bias encoding
  119. // exponent_diff is the diff between fp32/fp16 exponent and f8 exponent,
  120. // the difference needs to be adjusted and mantissa shifted
  121. int act_exponent, f8_exponent, exponent_diff;
  122. if (exponent == 0) { // fp32/fp16 is in denormal.
  123. /* fp32 denormal is below 2^-127 so it is usually not a concern here, we
  124. mostly concern fp16 here. In this case, f8 is usually in denormal. But there
  125. could be exceptions. fp16 denormal has exponent bias 15 while bf8 with NANOO has
  126. exponent bias 16. It means that there are some numbers in fp16 denormal but they
  127. are bf8 (NANOO) normals - smallest bf8 (NANOO) normal is 2^-15. fp16 numbers
  128. where exponent==0 (actual exponent -14) and highest bit of mantissa is 1 are bf8
  129. (NANOO) normal. In this case, the fp16 mantissa should be shift left by 1 */
  130. act_exponent = exponent - bias + 1;
  131. exponent_diff = f8_denormal_act_exponent - act_exponent; // actual exponent is exponent-bias+1 as it is denormal
  132. } else { // fp32/fp16 is normal with implicit 1
  133. act_exponent = exponent - bias;
  134. if (act_exponent <= f8_denormal_act_exponent) {
  135. /* This is the case where fp32/fp16 is normal but it is in f8 denormal
  136. range. For example fp8 nanoo mode, denormal exponent is -7, but if the
  137. fp32/fp16 actual exponent is -7, it is actually larger due to the implicit 1,
  138. Therefore it needs to be adjust to -6 and mantissa shift right by 1.
  139. So for fp32/fp16, exponent -8 is the cut point to convert to fp8 nanoo */
  140. exponent_diff = f8_denormal_act_exponent - act_exponent;
  141. } else { // both fp32/fp16 and f8 are in normal range
  142. exponent_diff = 0; // exponent_diff=0 does not mean there is no difference
  143. // for this case,
  144. // act_exponent could be larger. Just that it does not need shift mantissa
  145. }
  146. mantissa += (1 << mfmt); // Add the implicit 1 into mantissa
  147. }
  148. bool midpoint = (mantissa & ((1 << (mfmt - wm + exponent_diff)) - 1)) ==
  149. static_cast<uint32_t>(1 << (mfmt - wm + exponent_diff - 1));
  150. /* This part is a bit tricky. The judgment of whether it is a tie needs to be
  151. done before we shift right as shift right could rip off some residual part
  152. and make something not midpoint look like midpoint. For example, the fp16
  153. number 0x1002 (0 00100 0000000010), it is larger than midpoint, but after
  154. shift right by 4 bits, it would look like midpoint.
  155. */
  156. if (exponent_diff > 0) {
  157. mantissa >>= exponent_diff;
  158. } else if (exponent_diff == -1) {
  159. mantissa <<= -exponent_diff;
  160. }
  161. bool implicit_one = mantissa & (1 << mfmt);
  162. // if there is no implicit 1, it means the f8 is denormal and need to adjust
  163. // to denorm exponent
  164. f8_exponent = (act_exponent + exponent_diff) /*actual f8 exponent*/ + f8_bias - (implicit_one ? 0 : 1);
  165. // Now we have the exponent and mantissa adjusted
  166. uint32_t drop_mask = (1 << (mfmt - wm)) - 1;
  167. bool odd = mantissa & (1 << (mfmt - wm)); // if the least significant bit that
  168. // is not truncated is 1
  169. mantissa += (stoch ? rng : (midpoint ? (odd ? mantissa : mantissa - 1) : mantissa)) & drop_mask;
  170. // Now we deal with overflow
  171. if (f8_exponent == 0) {
  172. if ((1 << mfmt) & mantissa) {
  173. f8_exponent = 1; // denormal overflow to become normal, promote exponent
  174. }
  175. } else {
  176. if ((1 << (mfmt + 1)) & mantissa) {
  177. mantissa >>= 1;
  178. f8_exponent++;
  179. }
  180. }
  181. mantissa >>= (mfmt - wm);
  182. // above range: quantize to maximum possible float of the same sign
  183. const int max_exp = (1 << we) - (negative_zero_nan ? 1 : 2);
  184. if (f8_exponent > max_exp) {
  185. if (clip) {
  186. mantissa = (1 << wm) - 1;
  187. f8_exponent = max_exp;
  188. } else {
  189. return signed_inf;
  190. }
  191. }
  192. if (f8_exponent == 0 && mantissa == 0) {
  193. return negative_zero_nan ? 0 : (sign << 7);
  194. }
  195. mantissa &= (1 << wm) - 1;
  196. return (sign << 7) | (f8_exponent << wm) | mantissa;
  197. }
  198. template <int we, int wm, typename T = float, bool negative_zero_nan = true>
  199. inline HIP_FP8_HOST_DEVICE T from_float8(uint8_t x)
  200. {
  201. #ifdef __HIPCC__
  202. constexpr bool is_half = std::is_same<T, _Float16>::value;
  203. #else
  204. constexpr bool is_half = false;
  205. #endif
  206. constexpr bool is_float = std::is_same<T, float>::value;
  207. static_assert(is_half || is_float, "only half and float are supported");
  208. constexpr int weo = is_half ? 5 : 8;
  209. constexpr int wmo = is_half ? 10 : (is_float ? 23 : 7);
  210. T fInf, fNegInf, fNaN, fNeg0;
  211. #ifdef __HIPCC__
  212. if (is_half) {
  213. const uint16_t ihInf = 0x7C00;
  214. const uint16_t ihNegInf = 0xFC00;
  215. const uint16_t ihNaN = 0x7C01;
  216. const uint16_t ihNeg0 = 0x8000;
  217. fInf = reinterpret_cast<const _Float16&>(ihInf);
  218. fNegInf = reinterpret_cast<const _Float16&>(ihNegInf);
  219. fNaN = reinterpret_cast<const _Float16&>(ihNaN);
  220. fNeg0 = reinterpret_cast<const _Float16&>(ihNeg0);
  221. } else
  222. #endif
  223. if (is_float) {
  224. const uint32_t ifInf = 0x7F800000;
  225. const uint32_t ifNegInf = 0xFF800000;
  226. const uint32_t ifNaN = 0x7F800001;
  227. const uint32_t ifNeg0 = 0x80000000;
  228. fInf = reinterpret_cast<const float&>(ifInf);
  229. fNegInf = reinterpret_cast<const float&>(ifNegInf);
  230. fNaN = reinterpret_cast<const float&>(ifNaN);
  231. fNeg0 = reinterpret_cast<const float&>(ifNeg0);
  232. }
  233. if (x == 0) {
  234. return 0;
  235. }
  236. uint32_t sign = x >> 7;
  237. uint32_t mantissa = x & ((1 << wm) - 1);
  238. int exponent = (x & 0x7F) >> wm;
  239. if (negative_zero_nan) {
  240. if (x == 0x80) {
  241. return fNaN;
  242. }
  243. } else {
  244. if (x == 0x80) {
  245. return fNeg0;
  246. }
  247. if (exponent == ((1 << we) - 1)) {
  248. return (mantissa == 0) ? (sign ? fNegInf : fInf) : fNaN;
  249. }
  250. }
  251. typename std::conditional<sizeof(T) == 2, uint16_t, uint32_t>::type retval;
  252. if (we == 5 && is_half && !negative_zero_nan) {
  253. retval = x << 8;
  254. return reinterpret_cast<const T&>(retval);
  255. }
  256. const int exp_low_cutoff = (1 << (weo - 1)) - (1 << (we - 1)) + 1 - (negative_zero_nan ? 1 : 0);
  257. // subnormal input
  258. if (exponent == 0) {
  259. // guaranteed mantissa!=0 since cases 0x0 and 0x80 are handled above
  260. int sh = 1 + clz(mantissa) - (32 - wm);
  261. mantissa <<= sh;
  262. exponent += 1 - sh;
  263. mantissa &= ((1 << wm) - 1);
  264. }
  265. exponent += exp_low_cutoff - 1;
  266. mantissa <<= wmo - wm;
  267. // subnormal output (occurs when T=half, we=5, negative_zero_nan=true)
  268. if (exponent <= 0) {
  269. mantissa |= 1 << wmo;
  270. mantissa >>= 1 - exponent;
  271. exponent = 0;
  272. }
  273. if (sizeof(T) == 2) {
  274. retval = (sign << 15) | (exponent << 10) | mantissa;
  275. } else {
  276. retval = (sign << 31) | (exponent << 23) | mantissa;
  277. }
  278. return reinterpret_cast<const T&>(retval);
  279. }
  280. } // namespace hip_fp8_impl