/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/utility/f8_utils.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/utility/f8_utils.hpp Source File#

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck/utility/f8_utils.hpp Source File
f8_utils.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: MIT
2 // Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
3 
4 #pragma once
5 
7 
8 namespace ck {
9 
10 // fp8 rounding modes
11 // use standard for rounding to nearest, the faster one
12 // use stochastic for stochastic rounding, helps to avoid error accumulation
13 enum class f8_rounding_mode
14 {
15  standard,
17 };
18 
19 __host__ inline int clz(uint32_t x) { return __builtin_clz(x); }
20 __device__ inline int clz(uint32_t x) { return __clz(x); }
21 
22 } // namespace ck
23 
24 namespace ck::utils {
25 
26 namespace {
27 
28 template <typename X, typename Y, bool negative_zero_nan, bool clip, bool stoch>
29 __host__ __device__ Y run_cast_to_f8(X x, uint32_t rng)
30 {
31  // fp8/bf8 exponent/mantissa layout
32  constexpr int out_exp = NumericUtils<Y>::exp;
33  constexpr int out_mant = NumericUtils<Y>::mant;
34 
35  // original type exponent/mantissa layout
36  constexpr int in_exp = NumericUtils<X>::exp;
37  constexpr int in_mant = NumericUtils<X>::mant;
38 
39  int exponent, bias;
40  uint32_t head, mantissa, sign;
41  // nan code is same for float and half
42  constexpr uint8_t nan_code = 0x80;
43  constexpr uint32_t nan_mask = NumericUtils<X>::nan_mask;
44 
45  // convert to bitwise
46  using T_bitwise = typename NumericUtils<X>::bitwise_type;
47  T_bitwise x_bitwise = bit_cast<T_bitwise>(x);
48 
49  // unpack the input, depends on datatype
50  head = x_bitwise & NumericUtils<X>::head_mask;
51  mantissa = x_bitwise & NumericUtils<X>::mant_mask;
52  exponent = (head >> in_mant) & NumericUtils<X>::exp_mask;
53  sign = head >> (in_exp + in_mant);
54  bias = NumericUtils<X>::bias;
55 
56  uint32_t signed_inf = (sign << (in_exp + in_mant)) + (((1 << in_exp) - 1) << in_mant);
57  uint32_t drop_mask = (1 << (in_mant - out_mant)) - 1;
58  constexpr int max_exp = (1 << out_exp) - (negative_zero_nan ? 1 : 2);
59 
60  if constexpr(negative_zero_nan)
61  {
62  if((x_bitwise & nan_mask) == nan_mask)
63  return Y{nan_code};
64  }
65  else
66  {
67  if((x_bitwise & nan_mask) == nan_mask)
68  return Y{static_cast<uint8_t>(signed_inf + (mantissa != 0 ? 1 : 0))};
69  }
70 
71  // check if x is 0.0
72  if(x_bitwise == 0)
73  return Y{0};
74 
75  // First need to check if it is normal or denorm as there is a difference of implict 1
76  // Then need to adjust the exponent to align with the F8 exponent, in the meanwhile, shift
77  // The mantissa. Then for stochastic rounding, add rng to mantissa and truncate. And for
78  // RNE, no need to add rng. Then probably need to check whether there is carry and adjust
79  // exponent and mantissa again3
80 
81  // For IEEE bias mode, the bias is 2^(k-1)-1 where k is the width of exponent bits
82  const int out_bias = (1 << (out_exp - 1)) - 1 + (negative_zero_nan ? 1 : 0);
83  const int out_denormal_act_exponent = 1 - out_bias; // actual exponent of f8 denormal
84  // act_exponent is the actual exponent of fp32/fp16 (after subtracting bias)
85  // out_exponent is the converted f8 exponent with bias encoding
86  // exponent_diff is the diff between fp32/fp16 exponent and f8 exponent,
87  // the difference needs to be adjusted and mantissa shifted
88  int act_exponent, out_exponent, exponent_diff;
89 
90  if(exponent == 0)
91  { // fp32/fp16 is in denormal.
92  /* fp32 denormal is below 2^-127 so it is usually not a concern here, we mostly concern fp16
93 here. In this case, f8 is usually in denormal. But there could be exceptions. fp16 denormal has
94 exponent bias 15 while bf8 with NANOO has exponent bias 16. It means that there are some numbers in
95 fp16 denormal but they are bf8 (NANOO) normals - smallest bf8 (NANOO) normal is 2^-15. fp16 numbers
96 where exponent==0 (actual exponent -14) and highest bit of mantissa is 1 are bf8 (NANOO) normal.
97 In this case, the fp16 mantissa should be shift left by 1 */
98  act_exponent = exponent - bias + 1;
99  exponent_diff = out_denormal_act_exponent -
100  act_exponent; // actual exponent is exponent-bias+1 as it is denormal
101  }
102  else
103  { // fp32/fp16 is normal with implicit 1
104  act_exponent = exponent - bias;
105  if(act_exponent <= out_denormal_act_exponent)
106  {
107  /* This is the case where fp32/fp16 is normal but it is in f8 denormal range.
108  For example fp8 nanoo mode, denormal exponent is -7, but if the fp32/fp16
109  actual exponent is -7, it is actually larger due to the implict 1,
110  Therefore it needs to be adjust to -6 and mantissa shift right by 1.
111  So for fp32/fp16, exponent -8 is the cut point to convert to fp8 nanoo */
112  exponent_diff = out_denormal_act_exponent - act_exponent;
113  }
114  else
115  { // both fp32/fp16 and f8 are in normal range
116  exponent_diff =
117  0; // exponent_diff=0 does not mean there is no difference for this case,
118  // act_exponent could be larger. Just that it does not need shift mantissa
119  }
120  mantissa += (1 << in_mant); // Add the implicit 1 into mantissa
121  }
122 
123  bool midpoint = (mantissa & ((1 << (in_mant - out_mant + exponent_diff)) - 1)) ==
124  (1 << (in_mant - out_mant + exponent_diff - 1));
125  /* This part is a bit tricky. The judgment of whether it is a tie needs to be done before we
126  shift right as shift right could rip off some residual part and make something not midpoint look
127  like midpoint. For example, the fp16 number 0x1002 (0 00100 0000000010), it is larger than
128  midpoint, but after shift right by 4 bits, it would look like midpoint. */
129 
130  if(exponent_diff > 0)
131  mantissa >>= exponent_diff;
132  else if(exponent_diff == -1)
133  mantissa <<= -exponent_diff;
134  bool implicit_one = mantissa & (1 << in_mant);
135  // if there is no implict 1, it means the f8 is denormal and need to adjust to denorm exponent
136  out_exponent =
137  (act_exponent + exponent_diff) /*actual f8 exponent*/ + out_bias - (implicit_one ? 0 : 1);
138 
139  // Now we have the exponent and mantissa adjusted
140  bool odd =
141  mantissa &
142  (1 << (in_mant - out_mant)); // if the least significant bit that is not truncated is 1
143  mantissa += (stoch ? rng : (midpoint ? (odd ? mantissa : mantissa - 1) : mantissa)) & drop_mask;
144 
145  // Now we deal with overflow
146  if(out_exponent == 0)
147  {
148  if((1 << in_mant) & mantissa)
149  {
150  out_exponent = 1; // denormal overflow to become normal, promote exponent
151  // No need to make 1 implicit now as it will be addressed later
152  }
153  }
154  else
155  {
156  if((1 << (in_mant + 1)) & mantissa)
157  {
158  mantissa >>= 1;
159  out_exponent++;
160  // No need to make 1 implicit now as it will be addressed later
161  }
162  }
163 
164  mantissa >>= (in_mant - out_mant);
165 
166  if(out_exponent > max_exp)
167  {
168  if constexpr(clip)
169  {
170  mantissa = (1 << out_mant) - 1;
171  out_exponent = max_exp;
172  }
173  else
174  {
175  return signed_inf;
176  }
177  }
178 
179  // check if x is 0.0 or -0.0
180  if(out_exponent == 0 && mantissa == 0)
181  return Y{negative_zero_nan ? 0 : static_cast<uint8_t>(sign << (out_exp + out_mant))};
182  mantissa &= (1 << out_mant) - 1;
183  return Y{static_cast<uint8_t>((sign << (out_exp + out_mant)) | (out_exponent << out_mant) |
184  mantissa)};
185 }
186 
187 template <typename X, typename Y, bool negative_zero_nan>
188 __host__ __device__ Y run_cast_from_f8(X x)
189 {
190  // fp8/bf8 exponent/mantissa layout
191  constexpr int in_exp = NumericUtils<X>::exp;
192  constexpr int in_mant = NumericUtils<X>::mant;
193 
194  // resulting type exponent/mantissa layout
195  constexpr int out_exp = NumericUtils<Y>::exp;
196  constexpr int out_mant = NumericUtils<Y>::mant;
197 
198  // prepare the codes
199  constexpr uint8_t nan_code = 0x80;
200  using T_bitwise = typename NumericUtils<Y>::bitwise_type;
201 
202  constexpr T_bitwise Inf_bitwise = NumericUtils<Y>::Inf;
203  constexpr T_bitwise NegInf_bitwise = NumericUtils<Y>::NegInf;
204  constexpr T_bitwise NaN_bitwise = NumericUtils<Y>::NaN;
205  constexpr T_bitwise Neg0_bitwise = NumericUtils<Y>::Neg0;
206 
207  constexpr Y Inf = bit_cast<Y>(Inf_bitwise);
208  constexpr Y NegInf = bit_cast<Y>(NegInf_bitwise);
209  constexpr Y NaN = bit_cast<Y>(NaN_bitwise);
210  constexpr Y Neg0 = bit_cast<Y>(Neg0_bitwise);
211 
212  // check if x is 0.0
213  if(!static_cast<uint8_t>(x))
214  return static_cast<Y>(0);
215 
216  // unpack the input
217  uint32_t sign = static_cast<uint8_t>(x) >> (in_exp + in_mant);
218  uint32_t mantissa = static_cast<uint8_t>(x) & ((1 << in_mant) - 1);
219  int exponent = (static_cast<uint8_t>(x) & 0x7F) >> in_mant;
220 
221  constexpr int exp_low_cutoff =
222  (1 << (out_exp - 1)) - (1 << (in_exp - 1)) + 1 - (negative_zero_nan ? 1 : 0);
223  T_bitwise retval;
224 
225  if constexpr(negative_zero_nan)
226  {
227  if(static_cast<uint8_t>(x) == nan_code)
228  return NaN;
229  }
230  else
231  {
232  if(static_cast<uint8_t>(x) == nan_code)
233  return Neg0;
234  if(exponent == ((1 << in_exp) - 1))
235  return (mantissa == 0) ? (sign ? NegInf : Inf) : NaN;
236  }
237 
238  if constexpr((NumericUtils<Y>::mant == 10) && (NumericUtils<X>::mant == 2) &&
239  !negative_zero_nan)
240  {
241  retval = x;
242  retval <<= 8;
243  return bit_cast<Y>(retval);
244  }
245 
246  // subnormal input
247  if(exponent == 0)
248  {
249  // guaranteed mantissa!=0 since cases 0x0 and 0x80 are handled above
250  int sh = 1 + clz(mantissa) - (32 - in_mant);
251  mantissa <<= sh;
252  exponent += 1 - sh;
253  mantissa &= ((1 << in_mant) - 1);
254  }
255  exponent += exp_low_cutoff - 1;
256  mantissa <<= out_mant - in_mant;
257 
258  // subnormal output (occurs when T=half, we=5, negative_zero_nan=true)
259  if(exponent <= 0)
260  {
261  mantissa |= 1 << out_mant;
262  mantissa >>= 1 - exponent;
263  exponent = 0;
264  }
265 
266  retval = (sign << (out_exp + out_mant)) | (exponent << out_mant) | mantissa;
267  return bit_cast<Y>(retval);
268 }
269 
270 } // namespace
271 
272 template <typename X, typename Y, bool negative_zero_nan, bool clip, bool stoch>
273 __host__ __device__ Y cast_to_f8(X x, uint32_t rng)
274 {
275  // check datatypes
276  constexpr bool is_half = is_same<X, half_t>::value;
277  constexpr bool is_float = is_same<X, float>::value;
278  static_assert(is_half || is_float, "Only half and float can be casted.");
279 
280  return run_cast_to_f8<X, Y, negative_zero_nan, clip, stoch>(x, rng);
281 }
282 
283 template <typename X, typename Y, bool negative_zero_nan>
284 __host__ __device__ Y cast_from_f8(X x)
285 {
286  // check datatype
287  constexpr bool is_half = is_same<Y, half_t>::value;
288  constexpr bool is_float = is_same<Y, float>::value;
289  static_assert(is_half || is_float, "only half and float are supported.");
290 
291  return run_cast_from_f8<X, Y, negative_zero_nan>(x);
292 }
293 
294 } // namespace ck::utils
__host__ T exp(T x)
Definition: math_v2.hpp:391
Definition: check_err.hpp:24
__host__ __device__ Y cast_from_f8(X x)
Definition: f8_utils.hpp:284
__host__ __device__ Y cast_to_f8(X x, uint32_t rng)
Definition: f8_utils.hpp:273
CK_TILE_HOST_DEVICE DstT run_cast_to_f8(SrcT src, unsigned int rng=0)
Definition: float8.hpp:250
CK_TILE_HOST_DEVICE DstT run_cast_from_f8(SrcT x)
Definition: float8.hpp:476
Definition: ck.hpp:268
f8_rounding_mode
Definition: f8_utils.hpp:14
__host__ int clz(uint32_t x)
Definition: f8_utils.hpp:19
unsigned int uint32_t
Definition: stdint.h:126
unsigned char uint8_t
Definition: stdint.h:124
Definition: type.hpp:177