|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <c10/macros/Macros.h>
|
|
|
#include <c10/util/TypeSafeSignMath.h>
|
|
|
#include <c10/util/floating_point_utils.h>
|
|
|
|
|
|
#if defined(__cplusplus)
|
|
|
#include <cstdint>
|
|
|
#elif !defined(__OPENCL_VERSION__)
|
|
|
#include <math.h>
|
|
|
#include <stdint.h>
|
|
|
#endif
|
|
|
|
|
|
#include <iosfwd>
|
|
|
#include <ostream>
|
|
|
|
|
|
namespace c10 {
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline C10_HOST_DEVICE uint8_t fp8e5m2fnuz_from_fp32_value(float f) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t fnuz_max = UINT32_C(0x8F) << 23;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constexpr uint32_t denorm_mask = UINT32_C(0x85) << 23;
|
|
|
|
|
|
uint32_t f_bits = fp32_to_bits(f);
|
|
|
uint32_t result = 0u;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const uint32_t sign = f_bits & UINT32_C(0x80000000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
f_bits ^= sign;
|
|
|
|
|
|
if (f_bits >= fnuz_max) {
|
|
|
|
|
|
return 0x80;
|
|
|
}
|
|
|
|
|
|
if (f_bits < (UINT32_C(0x70) << 23) ) {
|
|
|
|
|
|
|
|
|
f_bits = fp32_to_bits(fp32_from_bits(f_bits) + fp32_from_bits(denorm_mask));
|
|
|
result = static_cast<uint8_t>(f_bits - denorm_mask);
|
|
|
if (result == 0) {
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
} else {
|
|
|
|
|
|
uint8_t mant_odd = (f_bits >> 21) & 1;
|
|
|
|
|
|
|
|
|
f_bits += ((uint32_t)(16 - 127) << 23) + 0xFFFFF;
|
|
|
|
|
|
|
|
|
f_bits += mant_odd;
|
|
|
|
|
|
|
|
|
result = static_cast<uint8_t>(f_bits >> 21);
|
|
|
}
|
|
|
|
|
|
result |= sign >> 24;
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
struct alignas(1) Float8_e5m2fnuz {
|
|
|
uint8_t x;
|
|
|
|
|
|
struct from_bits_t {};
|
|
|
C10_HOST_DEVICE static constexpr from_bits_t from_bits() {
|
|
|
return from_bits_t();
|
|
|
}
|
|
|
|
|
|
Float8_e5m2fnuz() = default;
|
|
|
|
|
|
constexpr C10_HOST_DEVICE Float8_e5m2fnuz(uint8_t bits, from_bits_t)
|
|
|
: x(bits) {}
|
|
|
inline C10_HOST_DEVICE Float8_e5m2fnuz(float value);
|
|
|
inline C10_HOST_DEVICE operator float() const;
|
|
|
inline C10_HOST_DEVICE bool isnan() const;
|
|
|
inline C10_HOST_DEVICE bool isinf() const;
|
|
|
};
|
|
|
|
|
|
C10_API inline std::ostream& operator<<(
|
|
|
std::ostream& out,
|
|
|
const Float8_e5m2fnuz& value) {
|
|
|
out << (float)value;
|
|
|
return out;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#include <c10/util/Float8_e5m2fnuz-inl.h>
|
|
|
|