File size: 9,410 Bytes
7e50900 |
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
#pragma once
#include <c10/macros/Macros.h>
#include <limits>
C10_CLANG_DIAGNOSTIC_PUSH()
#if C10_CLANG_HAS_WARNING("-Wimplicit-int-float-conversion")
C10_CLANG_DIAGNOSTIC_IGNORE("-Wimplicit-int-float-conversion")
#endif
namespace c10 {
/// Constructors
inline C10_HOST_DEVICE BFloat16::BFloat16(float value) {
#if defined(CUDA_VERSION) && CUDA_VERSION >= 11000 && \
defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
x = __bfloat16_as_ushort(__float2bfloat16(value));
#else
// RNE by default
x = detail::round_to_nearest_even(value);
#endif
}
/// Implicit conversions
inline C10_HOST_DEVICE BFloat16::operator float() const {
#if defined(CUDA_VERSION) && CUDA_VERSION >= 11000
return __bfloat162float(*reinterpret_cast<const __nv_bfloat16*>(&x));
#else
return detail::f32_from_bits(x);
#endif
}
#if defined(CUDA_VERSION) && CUDA_VERSION >= 11000
inline C10_HOST_DEVICE BFloat16::BFloat16(const __nv_bfloat16& value) {
x = *reinterpret_cast<const unsigned short*>(&value);
}
inline C10_HOST_DEVICE BFloat16::operator __nv_bfloat16() const {
return *reinterpret_cast<const __nv_bfloat16*>(&x);
}
#endif
// CUDA intrinsics
#if defined(__CUDACC__) || defined(__HIPCC__)
inline C10_DEVICE BFloat16 __ldg(const BFloat16* ptr) {
#if defined(CUDA_VERSION) && CUDA_VERSION >= 11000 && \
defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 800
return __ldg(reinterpret_cast<const __nv_bfloat16*>(ptr));
#else
return *ptr;
#endif
}
#endif
/// Arithmetic
inline C10_HOST_DEVICE BFloat16
operator+(const BFloat16& a, const BFloat16& b) {
return static_cast<float>(a) + static_cast<float>(b);
}
inline C10_HOST_DEVICE BFloat16
operator-(const BFloat16& a, const BFloat16& b) {
return static_cast<float>(a) - static_cast<float>(b);
}
inline C10_HOST_DEVICE BFloat16
operator*(const BFloat16& a, const BFloat16& b) {
return static_cast<float>(a) * static_cast<float>(b);
}
inline C10_HOST_DEVICE BFloat16 operator/(const BFloat16& a, const BFloat16& b)
__ubsan_ignore_float_divide_by_zero__ {
return static_cast<float>(a) / static_cast<float>(b);
}
inline C10_HOST_DEVICE BFloat16 operator-(const BFloat16& a) {
return -static_cast<float>(a);
}
inline C10_HOST_DEVICE BFloat16& operator+=(BFloat16& a, const BFloat16& b) {
a = a + b;
return a;
}
inline C10_HOST_DEVICE BFloat16& operator-=(BFloat16& a, const BFloat16& b) {
a = a - b;
return a;
}
inline C10_HOST_DEVICE BFloat16& operator*=(BFloat16& a, const BFloat16& b) {
a = a * b;
return a;
}
inline C10_HOST_DEVICE BFloat16& operator/=(BFloat16& a, const BFloat16& b) {
a = a / b;
return a;
}
inline C10_HOST_DEVICE BFloat16& operator|(BFloat16& a, const BFloat16& b) {
a.x = a.x | b.x;
return a;
}
inline C10_HOST_DEVICE BFloat16& operator^(BFloat16& a, const BFloat16& b) {
a.x = a.x ^ b.x;
return a;
}
inline C10_HOST_DEVICE BFloat16& operator&(BFloat16& a, const BFloat16& b) {
a.x = a.x & b.x;
return a;
}
/// Arithmetic with floats
inline C10_HOST_DEVICE float operator+(BFloat16 a, float b) {
return static_cast<float>(a) + b;
}
inline C10_HOST_DEVICE float operator-(BFloat16 a, float b) {
return static_cast<float>(a) - b;
}
inline C10_HOST_DEVICE float operator*(BFloat16 a, float b) {
return static_cast<float>(a) * b;
}
inline C10_HOST_DEVICE float operator/(BFloat16 a, float b) {
return static_cast<float>(a) / b;
}
inline C10_HOST_DEVICE float operator+(float a, BFloat16 b) {
return a + static_cast<float>(b);
}
inline C10_HOST_DEVICE float operator-(float a, BFloat16 b) {
return a - static_cast<float>(b);
}
inline C10_HOST_DEVICE float operator*(float a, BFloat16 b) {
return a * static_cast<float>(b);
}
inline C10_HOST_DEVICE float operator/(float a, BFloat16 b) {
return a / static_cast<float>(b);
}
inline C10_HOST_DEVICE float& operator+=(float& a, const BFloat16& b) {
return a += static_cast<float>(b);
}
inline C10_HOST_DEVICE float& operator-=(float& a, const BFloat16& b) {
return a -= static_cast<float>(b);
}
inline C10_HOST_DEVICE float& operator*=(float& a, const BFloat16& b) {
return a *= static_cast<float>(b);
}
inline C10_HOST_DEVICE float& operator/=(float& a, const BFloat16& b) {
return a /= static_cast<float>(b);
}
/// Arithmetic with doubles
inline C10_HOST_DEVICE double operator+(BFloat16 a, double b) {
return static_cast<double>(a) + b;
}
inline C10_HOST_DEVICE double operator-(BFloat16 a, double b) {
return static_cast<double>(a) - b;
}
inline C10_HOST_DEVICE double operator*(BFloat16 a, double b) {
return static_cast<double>(a) * b;
}
inline C10_HOST_DEVICE double operator/(BFloat16 a, double b) {
return static_cast<double>(a) / b;
}
inline C10_HOST_DEVICE double operator+(double a, BFloat16 b) {
return a + static_cast<double>(b);
}
inline C10_HOST_DEVICE double operator-(double a, BFloat16 b) {
return a - static_cast<double>(b);
}
inline C10_HOST_DEVICE double operator*(double a, BFloat16 b) {
return a * static_cast<double>(b);
}
inline C10_HOST_DEVICE double operator/(double a, BFloat16 b) {
return a / static_cast<double>(b);
}
/// Arithmetic with ints
inline C10_HOST_DEVICE BFloat16 operator+(BFloat16 a, int b) {
return a + static_cast<BFloat16>(b);
}
inline C10_HOST_DEVICE BFloat16 operator-(BFloat16 a, int b) {
return a - static_cast<BFloat16>(b);
}
inline C10_HOST_DEVICE BFloat16 operator*(BFloat16 a, int b) {
return a * static_cast<BFloat16>(b);
}
inline C10_HOST_DEVICE BFloat16 operator/(BFloat16 a, int b) {
return a / static_cast<BFloat16>(b);
}
inline C10_HOST_DEVICE BFloat16 operator+(int a, BFloat16 b) {
return static_cast<BFloat16>(a) + b;
}
inline C10_HOST_DEVICE BFloat16 operator-(int a, BFloat16 b) {
return static_cast<BFloat16>(a) - b;
}
inline C10_HOST_DEVICE BFloat16 operator*(int a, BFloat16 b) {
return static_cast<BFloat16>(a) * b;
}
inline C10_HOST_DEVICE BFloat16 operator/(int a, BFloat16 b) {
return static_cast<BFloat16>(a) / b;
}
//// Arithmetic with int64_t
inline C10_HOST_DEVICE BFloat16 operator+(BFloat16 a, int64_t b) {
return a + static_cast<BFloat16>(b);
}
inline C10_HOST_DEVICE BFloat16 operator-(BFloat16 a, int64_t b) {
return a - static_cast<BFloat16>(b);
}
inline C10_HOST_DEVICE BFloat16 operator*(BFloat16 a, int64_t b) {
return a * static_cast<BFloat16>(b);
}
inline C10_HOST_DEVICE BFloat16 operator/(BFloat16 a, int64_t b) {
return a / static_cast<BFloat16>(b);
}
inline C10_HOST_DEVICE BFloat16 operator+(int64_t a, BFloat16 b) {
return static_cast<BFloat16>(a) + b;
}
inline C10_HOST_DEVICE BFloat16 operator-(int64_t a, BFloat16 b) {
return static_cast<BFloat16>(a) - b;
}
inline C10_HOST_DEVICE BFloat16 operator*(int64_t a, BFloat16 b) {
return static_cast<BFloat16>(a) * b;
}
inline C10_HOST_DEVICE BFloat16 operator/(int64_t a, BFloat16 b) {
return static_cast<BFloat16>(a) / b;
}
// Overloading < and > operators, because std::max and std::min use them.
inline C10_HOST_DEVICE bool operator>(BFloat16& lhs, BFloat16& rhs) {
return float(lhs) > float(rhs);
}
inline C10_HOST_DEVICE bool operator<(BFloat16& lhs, BFloat16& rhs) {
return float(lhs) < float(rhs);
}
} // namespace c10
namespace std {
template <>
class numeric_limits<c10::BFloat16> {
public:
static constexpr bool is_signed = true;
static constexpr bool is_specialized = true;
static constexpr bool is_integer = false;
static constexpr bool is_exact = false;
static constexpr bool has_infinity = true;
static constexpr bool has_quiet_NaN = true;
static constexpr bool has_signaling_NaN = true;
static constexpr auto has_denorm = numeric_limits<float>::has_denorm;
static constexpr auto has_denorm_loss =
numeric_limits<float>::has_denorm_loss;
static constexpr auto round_style = numeric_limits<float>::round_style;
static constexpr bool is_iec559 = false;
static constexpr bool is_bounded = true;
static constexpr bool is_modulo = false;
static constexpr int digits = 8;
static constexpr int digits10 = 2;
static constexpr int max_digits10 = 4;
static constexpr int radix = 2;
static constexpr int min_exponent = -125;
static constexpr int min_exponent10 = -37;
static constexpr int max_exponent = 128;
static constexpr int max_exponent10 = 38;
static constexpr auto traps = numeric_limits<float>::traps;
static constexpr auto tinyness_before =
numeric_limits<float>::tinyness_before;
static constexpr c10::BFloat16 min() {
return c10::BFloat16(0x0080, c10::BFloat16::from_bits());
}
static constexpr c10::BFloat16 lowest() {
return c10::BFloat16(0xFF7F, c10::BFloat16::from_bits());
}
static constexpr c10::BFloat16 max() {
return c10::BFloat16(0x7F7F, c10::BFloat16::from_bits());
}
static constexpr c10::BFloat16 epsilon() {
return c10::BFloat16(0x3C00, c10::BFloat16::from_bits());
}
static constexpr c10::BFloat16 round_error() {
return c10::BFloat16(0x3F00, c10::BFloat16::from_bits());
}
static constexpr c10::BFloat16 infinity() {
return c10::BFloat16(0x7F80, c10::BFloat16::from_bits());
}
static constexpr c10::BFloat16 quiet_NaN() {
return c10::BFloat16(0x7FC0, c10::BFloat16::from_bits());
}
static constexpr c10::BFloat16 signaling_NaN() {
return c10::BFloat16(0x7F80, c10::BFloat16::from_bits());
}
static constexpr c10::BFloat16 denorm_min() {
return c10::BFloat16(0x0001, c10::BFloat16::from_bits());
}
};
} // namespace std
C10_CLANG_DIAGNOSTIC_POP()
|