Debian Codegraph @

tensilelite/Tensile/Source/tensile_bfloat16.h

raw symbols SCIP
1 /*******************************************************************************
2 *
3 * MIT License
4 *
5 * Copyright (C) 2022 Advanced Micro Devices, Inc. All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 *******************************************************************************/
26
27 /*!\file
28 * \brief tensile_bfloat16.h provides struct for tensile_bfloat16 typedef
29 */
30
31 #pragma once
32 #ifndef _TENSILE_BFLOAT16_H_
33 #define _TENSILE_BFLOAT16_H_
34
35 // If this is a C compiler, C++ compiler below C++11, or a host-only compiler,
36 // we only include a minimal definition of tensile_bfloat16
37 #if __cplusplus < 201103L || !defined(__HIPCC__)
38
39 #include <stdint.h>
40 typedef struct
41 {
42 uint16_t data;
43 } tensile_bfloat16;
44
45 #else // __cplusplus < 201103L || !defined(__HIPCC__)
46
47 #include <cmath>
48 #include <cstddef>
49 #include <cstdint>
50 #include <hip/hip_runtime.h>
51 #include <ostream>
52 #include <type_traits>
53
54 struct tensile_bfloat16
55 {
56 uint16_t data;
57
58 // Don't initialize `data` in purpose so that it could be used with
59 // `__shared__`, which forbid any initializer.
60 __host__ __device__ tensile_bfloat16() {}
61
62 // round upper 16 bits of IEEE float to convert to bfloat16
63 explicit __host__ __device__ tensile_bfloat16(float f)
64 : data(float_to_bfloat16(f))
65 {
66 }
67
68 // zero extend lower 16 bits of bfloat16 to convert to IEEE float
69 __host__ __device__ operator float() const
70 {
71 union
72 {
73 uint32_t int32;
74 float fp32;
75 } u = {uint32_t(data) << 16};
76 return u.fp32;
77 }
78
79 private:
80 static __host__ __device__ uint16_t float_to_bfloat16(float f)
81 {
82 union
83 {
84 float fp32;
85 uint32_t int32;
86 } u = {f};
87 if(~u.int32 & 0x7f800000)
88 {
89 // When the exponent bits are not all 1s, then the value is zero, normal,
90 // or subnormal. We round the bfloat16 mantissa up by adding 0x7FFF, plus
91 // 1 if the least significant bit of the bfloat16 mantissa is 1 (odd).
92 // This causes the bfloat16's mantissa to be incremented by 1 if the 16
93 // least significant bits of the float mantissa are greater than 0x8000,
94 // or if they are equal to 0x8000 and the least significant bit of the
95 // bfloat16 mantissa is 1 (odd). This causes it to be rounded to even when
96 // the lower 16 bits are exactly 0x8000. If the bfloat16 mantissa already
97 // has the value 0x7f, then incrementing it causes it to become 0x00 and
98 // the exponent is incremented by one, which is the next higher FP value
99 // to the unrounded bfloat16 value. When the bfloat16 value is subnormal
100 // with an exponent of 0x00 and a mantissa of 0x7F, it may be rounded up
101 // to a normal value with an exponent of 0x01 and a mantissa of 0x00.
102 // When the bfloat16 value has an exponent of 0xFE and a mantissa of 0x7F,
103 // incrementing it causes it to become an exponent of 0xFF and a mantissa
104 // of 0x00, which is Inf, the next higher value to the unrounded value.
105 u.int32 += 0x7fff + ((u.int32 >> 16) & 1); // Round to nearest, round to even
106 }
107 else if(u.int32 & 0xffff)
108 {
109 // When all of the exponent bits are 1, the value is Inf or NaN.
110 // Inf is indicated by a zero mantissa. NaN is indicated by any nonzero
111 // mantissa bit. Quiet NaN is indicated by the most significant mantissa
112 // bit being 1. Signaling NaN is indicated by the most significant
113 // mantissa bit being 0 but some other bit(s) being 1. If any of the
114 // lower 16 bits of the mantissa are 1, we set the least significant bit
115 // of the bfloat16 mantissa, in order to preserve signaling NaN in case
116 // the bloat16's mantissa bits are all 0.
117 u.int32 |= 0x10000; // Preserve signaling NaN
118 }
119 return uint16_t(u.int32 >> 16);
120 }
121 };
122
123 static_assert(std::is_standard_layout<tensile_bfloat16>{},
124 "tensile_bfloat16 is not a standard layout type, and thus is "
125 "incompatible with C.");
126
127 static_assert(std::is_trivially_copyable<tensile_bfloat16>{},
128 "tensile_bfloat16 is not trivially copyable, and thus is "
129 "incompatible with C.");
130
131 inline std::ostream& operator<<(std::ostream& os, const tensile_bfloat16& bf16)
132 {
133 return os << float(bf16);
134 }
135 inline __host__ __device__ tensile_bfloat16 operator+(tensile_bfloat16 a)
136 {
137 return a;
138 }
139 inline __host__ __device__ tensile_bfloat16 operator-(tensile_bfloat16 a)
140 {
141 a.data ^= 0x8000;
142 return a;
143 }
144 inline __host__ __device__ tensile_bfloat16 operator+(tensile_bfloat16 a, tensile_bfloat16 b)
145 {
146 return tensile_bfloat16(float(a) + float(b));
147 }
148 inline __host__ __device__ tensile_bfloat16 operator+(int a, tensile_bfloat16 b)
149 {
150 return static_cast<tensile_bfloat16>(static_cast<float>(a) + static_cast<float>(b));
151 }
152 inline __host__ __device__ tensile_bfloat16 operator+(tensile_bfloat16 a, int b)
153 {
154 return static_cast<tensile_bfloat16>(static_cast<float>(a) + static_cast<float>(b));
155 }
156 inline __host__ __device__ tensile_bfloat16 operator-(tensile_bfloat16 a, tensile_bfloat16 b)
157 {
158 return tensile_bfloat16(float(a) - float(b));
159 }
160 inline __host__ __device__ tensile_bfloat16 operator*(tensile_bfloat16 a, tensile_bfloat16 b)
161 {
162 return tensile_bfloat16(float(a) * float(b));
163 }
164 inline __host__ __device__ tensile_bfloat16 operator/(tensile_bfloat16 a, tensile_bfloat16 b)
165 {
166 return tensile_bfloat16(float(a) / float(b));
167 }
168 inline __host__ __device__ bool operator<(tensile_bfloat16 a, tensile_bfloat16 b)
169 {
170 return float(a) < float(b);
171 }
172 inline __host__ __device__ bool operator==(tensile_bfloat16 a, tensile_bfloat16 b)
173 {
174 return float(a) == float(b);
175 }
176 inline __host__ __device__ bool operator>(tensile_bfloat16 a, tensile_bfloat16 b)
177 {
178 return b < a;
179 }
180 inline __host__ __device__ bool operator<=(tensile_bfloat16 a, tensile_bfloat16 b)
181 {
182 return !(a > b);
183 }
184 inline __host__ __device__ bool operator!=(tensile_bfloat16 a, tensile_bfloat16 b)
185 {
186 return !(a == b);
187 }
188 inline __host__ __device__ bool operator>=(tensile_bfloat16 a, tensile_bfloat16 b)
189 {
190 return !(a < b);
191 }
192 inline __host__ __device__ tensile_bfloat16& operator+=(tensile_bfloat16& a, tensile_bfloat16 b)
193 {
194 return a = a + b;
195 }
196 inline __host__ __device__ tensile_bfloat16& operator-=(tensile_bfloat16& a, tensile_bfloat16 b)
197 {
198 return a = a - b;
199 }
200 inline __host__ __device__ tensile_bfloat16& operator*=(tensile_bfloat16& a, tensile_bfloat16 b)
201 {
202 return a = a * b;
203 }
204 inline __host__ __device__ tensile_bfloat16& operator/=(tensile_bfloat16& a, tensile_bfloat16 b)
205 {
206 return a = a / b;
207 }
208 inline __host__ __device__ tensile_bfloat16& operator++(tensile_bfloat16& a)
209 {
210 return a += tensile_bfloat16(1.0f);
211 }
212 inline __host__ __device__ tensile_bfloat16& operator--(tensile_bfloat16& a)
213 {
214 return a -= tensile_bfloat16(1.0f);
215 }
216 inline __host__ __device__ tensile_bfloat16 operator++(tensile_bfloat16& a, int)
217 {
218 tensile_bfloat16 orig = a;
219 ++a;
220 return orig;
221 }
222 inline __host__ __device__ tensile_bfloat16 operator--(tensile_bfloat16& a, int)
223 {
224 tensile_bfloat16 orig = a;
225 --a;
226 return orig;
227 }
228 inline __host__ __device__ bool isinf(tensile_bfloat16 a)
229 {
230 return !(~a.data & 0x7f80) && !(a.data & 0x7f);
231 }
232 inline __host__ __device__ bool isnan(tensile_bfloat16 a)
233 {
234 return !(~a.data & 0x7f80) && +(a.data & 0x7f);
235 }
236 inline __host__ __device__ bool iszero(tensile_bfloat16 a)
237 {
238 return !(a.data & 0x7fff);
239 }
240 inline tensile_bfloat16 sin(tensile_bfloat16 a)
241 {
242 return tensile_bfloat16(sinf(float(a)));
243 }
244 inline tensile_bfloat16 cos(tensile_bfloat16 a)
245 {
246 return tensile_bfloat16(cosf(float(a)));
247 }
248
249 #endif // __cplusplus < 201103L || !defined(__HIPCC__)
250
251 #endif // _TENSILE_BFLOAT16_H_