/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 Y 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 nan_code;
64  }
65  else
66  {
67  if((x_bitwise & nan_mask) == nan_mask)
68  return signed_inf + (mantissa != 0 ? 1 : 0);
69  }
70 
71  // check if x is 0.0
72  if(x_bitwise == 0)
73  return 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 negative_zero_nan ? 0 : (sign << (out_exp + out_mant));
182  mantissa &= (1 << out_mant) - 1;
183  return (sign << (out_exp + out_mant)) | (out_exponent << out_mant) | mantissa;
184 }
185 
186 template <typename X, typename Y, bool negative_zero_nan>
187 __host__ __device__ Y run_cast_from_f8(X x)
188 {
189  // fp8/bf8 exponent/mantissa layout
190  constexpr int in_exp = NumericUtils<X>::exp;
191  constexpr int in_mant = NumericUtils<X>::mant;
192 
193  // resulting type exponent/mantissa layout
194  constexpr int out_exp = NumericUtils<Y>::exp;
195  constexpr int out_mant = NumericUtils<Y>::mant;
196 
197  // prepare the codes
198  constexpr X nan_code = 0x80;
199  using T_bitwise = typename NumericUtils<Y>::bitwise_type;
200 
201  constexpr T_bitwise Inf_bitwise = NumericUtils<Y>::Inf;
202  constexpr T_bitwise NegInf_bitwise = NumericUtils<Y>::NegInf;
203  constexpr T_bitwise NaN_bitwise = NumericUtils<Y>::NaN;
204  constexpr T_bitwise Neg0_bitwise = NumericUtils<Y>::Neg0;
205 
206  constexpr Y Inf = bit_cast<Y>(Inf_bitwise);
207  constexpr Y NegInf = bit_cast<Y>(NegInf_bitwise);
208  constexpr Y NaN = bit_cast<Y>(NaN_bitwise);
209  constexpr Y Neg0 = bit_cast<Y>(Neg0_bitwise);
210 
211  // check if x is 0.0
212  if(x == 0)
213  return static_cast<Y>(0);
214 
215  // unpack the input
216  uint32_t sign = x >> (in_exp + in_mant);
217  uint32_t mantissa = x & ((1 << in_mant) - 1);
218  int exponent = (x & 0x7F) >> in_mant;
219 
220  constexpr int exp_low_cutoff =
221  (1 << (out_exp - 1)) - (1 << (in_exp - 1)) + 1 - (negative_zero_nan ? 1 : 0);
222  T_bitwise retval;
223 
224  if constexpr(negative_zero_nan)
225  {
226  if(x == nan_code)
227  return NaN;
228  }
229  else
230  {
231  if(x == nan_code)
232  return Neg0;
233  if(exponent == ((1 << in_exp) - 1))
234  return (mantissa == 0) ? (sign ? NegInf : Inf) : NaN;
235  }
236 
237  if constexpr((NumericUtils<Y>::mant == 10) && (NumericUtils<X>::mant == 2) &&
238  !negative_zero_nan)
239  {
240  retval = x;
241  retval <<= 8;
242  return bit_cast<Y>(retval);
243  }
244 
245  // subnormal input
246  if(exponent == 0)
247  {
248  // guaranteed mantissa!=0 since cases 0x0 and 0x80 are handled above
249  int sh = 1 + clz(mantissa) - (32 - in_mant);
250  mantissa <<= sh;
251  exponent += 1 - sh;
252  mantissa &= ((1 << in_mant) - 1);
253  }
254  exponent += exp_low_cutoff - 1;
255  mantissa <<= out_mant - in_mant;
256 
257  // subnormal output (occurs when T=half, we=5, negative_zero_nan=true)
258  if(exponent <= 0)
259  {
260  mantissa |= 1 << out_mant;
261  mantissa >>= 1 - exponent;
262  exponent = 0;
263  }
264 
265  retval = (sign << (out_exp + out_mant)) | (exponent << out_mant) | mantissa;
266  return bit_cast<Y>(retval);
267 }
268 
269 } // namespace
270 
271 template <typename X, typename Y, bool negative_zero_nan, bool clip, bool stoch>
272 __host__ __device__ Y cast_to_f8(X x, uint32_t rng)
273 {
274  // check datatypes
275  constexpr bool is_half = std::is_same<X, half_t>::value;
276  constexpr bool is_float = std::is_same<X, float>::value;
277  static_assert(is_half || is_float, "Only half and float can be casted.");
278 
279  return run_cast_to_f8<X, Y, negative_zero_nan, clip, stoch>(x, rng);
280 }
281 
282 template <typename X, typename Y, bool negative_zero_nan>
283 __host__ __device__ Y cast_from_f8(X x)
284 {
285  // check datatype
286  constexpr bool is_half = std::is_same<Y, half_t>::value;
287  constexpr bool is_float = std::is_same<Y, float>::value;
288  static_assert(is_half || is_float, "only half and float are supported.");
289 
290  return run_cast_from_f8<X, Y, negative_zero_nan>(x);
291 }
292 
293 } // 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:283
__host__ __device__ Y cast_to_f8(X x, uint32_t rng)
Definition: f8_utils.hpp:272
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:267
f8_rounding_mode
Definition: f8_utils.hpp:14
__host__ int clz(uint32_t x)
Definition: f8_utils.hpp:19
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1350
unsigned int uint32_t
Definition: stdint.h:126