| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
#ifndef _TENSILE_BFLOAT16_H_ |
| 33 |
#define _TENSILE_BFLOAT16_H_ |
| 34 |
|
| 35 |
|
| 36 |
|
| 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 |
| 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 |
|
| 59 |
|
| 60 |
__host__ __device__ tensile_bfloat16() {} |
| 61 |
|
| 62 |
|
| 63 |
explicit __host__ __device__ tensile_bfloat16(float f) |
| 64 |
: data(float_to_bfloat16(f)) |
| 65 |
{ |
| 66 |
} |
| 67 |
|
| 68 |
|
| 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 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
u.int32 += 0x7fff + ((u.int32 >> 16) & 1); |
| 106 |
} |
| 107 |
else if(u.int32 & 0xffff) |
| 108 |
{ |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
u.int32 |= 0x10000; |
| 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 |
| 250 |
|
| 251 |
#endif |