hkimi / src /common /numeric.h
Ozomozyan's picture
hkimi 0.1.0
68888be verified
Raw History Blame Contribute Delete
3.83 kB
// Number formats of the checkpoints: BF16, IEEE half, E8M0. All conversions are exact.
#pragma once
#include <cmath>
#include <cstdint>
#include <cstring>
namespace hk {
// bfloat16 = the top 16 bits of a float32
inline float bf16_to_f32(uint16_t v) {
uint32_t u = (uint32_t)v << 16;
float f;
memcpy(&f, &u, 4);
return f;
}
// round to nearest even, as PyTorch does
inline uint16_t f32_to_bf16(float f) {
uint32_t u;
memcpy(&u, &f, 4);
if ((u & 0x7F800000u) == 0x7F800000u) return (uint16_t)(u >> 16); // inf / nan: keep the top bits
uint32_t lsb = (u >> 16) & 1u;
u += 0x7FFFu + lsb;
return (uint16_t)(u >> 16);
}
inline float f16_to_f32(uint16_t h) {
uint32_t sign = (uint32_t)(h & 0x8000u) << 16;
uint32_t exp = (h >> 10) & 0x1Fu;
uint32_t man = h & 0x3FFu;
uint32_t u;
if (exp == 0) {
if (man == 0) {
u = sign;
} else { // subnormal: renormalize
int shift = 0;
while ((man & 0x400u) == 0) {
man <<= 1;
++shift;
}
man &= 0x3FFu;
u = sign | ((uint32_t)(127 - 15 - shift + 1) << 23) | (man << 13);
}
} else if (exp == 31) {
u = sign | 0x7F800000u | (man << 13);
} else {
u = sign | ((exp + (127 - 15)) << 23) | (man << 13);
}
float f;
memcpy(&f, &u, 4);
return f;
}
// round to nearest even, saturating at the largest half (no inf from a finite input)
inline uint16_t f32_to_f16(float f) {
uint32_t u;
memcpy(&u, &f, 4);
uint32_t sign = (u >> 16) & 0x8000u;
int32_t exp = (int32_t)((u >> 23) & 0xFFu) - 127 + 15;
uint32_t man = u & 0x7FFFFFu;
if (((u >> 23) & 0xFFu) == 0xFFu) return (uint16_t)(sign | 0x7C00u | (man ? 0x200u : 0));
if (exp >= 31) return (uint16_t)(sign | 0x7BFFu); // clamp to the largest finite half
if (exp <= 0) {
if (exp < -10) return (uint16_t)sign; // underflow to zero
man |= 0x800000u;
uint32_t shift = (uint32_t)(14 - exp);
uint32_t half = man >> shift;
uint32_t rem = man & ((1u << shift) - 1);
uint32_t mid = 1u << (shift - 1);
if (rem > mid || (rem == mid && (half & 1u))) ++half;
return (uint16_t)(sign | half);
}
uint32_t half = ((uint32_t)exp << 10) | (man >> 13);
uint32_t rem = man & 0x1FFFu;
if (rem > 0x1000u || (rem == 0x1000u && (half & 1u))) ++half; // may carry into the exponent: still correct
if (half >= 0x7C00u) half = 0x7BFFu;
return (uint16_t)(sign | half);
}
// E8M0, the MXFP4 block scale: 2^(e - 127). e == 0 is read as zero: 2^-127 would
// be a subnormal float, slow and as good as zero here.
inline float e8m0_to_f32(uint8_t e) {
if (e == 0) return 0.0f;
uint32_t u = (uint32_t)e << 23; // biased exponent, zero mantissa, positive
float f;
memcpy(&f, &u, 4);
return f;
}
inline uint32_t load_le32(const uint8_t* p) {
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
}
inline void store_le32(uint8_t* p, uint32_t v) {
p[0] = (uint8_t)v;
p[1] = (uint8_t)(v >> 8);
p[2] = (uint8_t)(v >> 16);
p[3] = (uint8_t)(v >> 24);
}
inline uint16_t load_le16(const uint8_t* p) { return (uint16_t)(p[0] | (p[1] << 8)); }
inline void store_le16(uint8_t* p, uint16_t v) {
p[0] = (uint8_t)v;
p[1] = (uint8_t)(v >> 8);
}
inline uint64_t load_le64(const uint8_t* p) { return (uint64_t)load_le32(p) | ((uint64_t)load_le32(p + 4) << 32); }
inline void store_le64(uint8_t* p, uint64_t v) {
store_le32(p, (uint32_t)v);
store_le32(p + 4, (uint32_t)(v >> 32));
}
inline float sigmoidf(float x) { return 1.0f / (1.0f + std::exp(-x)); }
inline float softplusf(float x) { return x > 20.0f ? x : std::log1p(std::exp(x)); }
} // namespace hk