// include/strata/kernels/f16_bits.hpp - the fp16 conversions, written out, as bits. // // WHY THIS IS NOT `__float2half`. Round 193 found `__float2half` producing 0x2600 for a value whose correct // fp16 encoding is 0x26DB, and 0x3C01 for a value that is exactly 1.0 - i.e. wrong bits, not a rounding // difference. The cause was never isolated (it is either `__float2half` itself or `__half_as_ushort`), so // `src/kernels/cuda/quantize_act.cu` replaced it with the version below, which then came out byte-exact over // all 20,480 blocks of the fixture. `shared_expert.cu` carries the same function privately for the same // reason. Three copies of a converter whose failure mode is silent wrong bits is two copies too many, so the // copy lives here and the kernel and its TEST both include it - with the test checking it against vectors // generated by numpy's float16 (an oracle that shares no code with this), which is what neither private copy // had. // // The rounding is ROUND-TO-NEAREST-EVEN, which is what numpy's `.astype(np.float16)` does and what IEEE-754 // specifies for the default mode. The three interesting regimes are all handled explicitly and all three are // covered by the test's oracle vectors: // // * normal - exponent fits, mantissa is rounded from 13 surviving bits with a sticky remainder // * subnormal - the implicit 1 is restored and the result is shifted right, so the rounding point MOVES // * overflow - exp >= 31 saturates to inf (0x7C00) and keeps the sign; a NaN input stays a NaN // // `f32_from_f16` deliberately does NOT round: it is used to compare against a reference that holds the same // bits, so it only has to be exact. #pragma once #include #include #if defined(__CUDACC__) || defined(__HIPCC__) #define STRATA_HD __host__ __device__ #else #define STRATA_HD #endif namespace strata::kernels { /// Round-to-nearest-even f32 -> fp16, returned as raw bits. STRATA_HD inline uint16_t f16_from_f32(float f) { uint32_t x; #if defined(__HIP_DEVICE_COMPILE__) __builtin_memcpy(&x, &f, 4); #else std::memcpy(&x, &f, 4); #endif const uint32_t sign = (x >> 16) & 0x8000u; const uint32_t rawexp = (x >> 23) & 0xFFu; int exp = (int) rawexp - 127 + 15; uint32_t man = x & 0x7FFFFFu; // TWO different things reach here and they must not be conflated: an f32 inf/NaN (raw exponent 255) stays // an inf/NaN, and a FINITE value too large for fp16 (1e30) SATURATES TO INF. The version of this function // in quantize_act.cu and shared_expert.cu tests `if (exp >= 31) return sign | 0x7C00 | (man ? 0x200 : 0)` // and therefore returns a NaN for every overflowing finite value - which their own fixtures cannot see, // because all of their values are O(1). Round 198's numpy oracle caught it here on the first run (1e30, // 65536, 1e45: numpy says inf, the conflated test said NaN). if (rawexp == 0xFFu) return (uint16_t) (sign | 0x7C00u | (man ? 0x200u : 0u)); // inf, or a quiet/NaN if (exp >= 31) return (uint16_t) (sign | 0x7C00u); // finite overflow -> inf if (exp <= 0) { if (exp < -10) return (uint16_t) sign; // underflows to a signed zero man |= 0x800000u; // restore the implicit 1 const uint32_t sh = (uint32_t) (14 - exp); uint32_t h = (man >> sh) & 0x3FFu; const uint32_t rem = man & ((1u << sh) - 1u); if (rem > (1u << (sh - 1)) || (rem == (1u << (sh - 1)) && (h & 1u))) ++h; return (uint16_t) (sign | h); } uint16_t h = (uint16_t) (sign | ((uint32_t) exp << 10) | (man >> 13)); const uint32_t rem = man & 0x1FFFu; if (rem > 0x1000u || (rem == 0x1000u && (h & 1u))) ++h; // the carry may bump the exponent return h; } /// Exact fp16 bits -> f32. No rounding, no exceptions: every one of the 65,536 inputs maps to a value f32 /// represents exactly, including the subnormals (which is why the `ex == 0` case cannot be skipped). STRATA_HD inline float f32_from_f16(uint16_t h) { const uint32_t sign = (uint32_t) (h & 0x8000u) << 16; const uint32_t ex = (h >> 10) & 0x1Fu, man = h & 0x3FFu; uint32_t out; if (ex == 0) { // Subnormal or zero. The value is `man * 2^-24`, so after `s` left shifts the leading 1 reaches bit // 10 and the value is `(1 + frac) * 2^(-14-s)`: the f32 exponent field is 113 - s and `frac` is the low // ten bits. Flushing these to zero - which the two private copies of this function in the tree do - // maps 0x0001 to 0.0 instead of 2^-24, a 100% relative error on any KL-quant scale that lands here. if (man == 0) { out = sign; } else { uint32_t m = man; int s = 0; while ((m & 0x400u) == 0) { m <<= 1; ++s; } out = sign | ((uint32_t) (113 - s) << 23) | ((m & 0x3FFu) << 13); } } else if (ex == 31) { out = sign | 0x7F800000u | (man << 13); } else { out = sign | ((ex - 15 + 127) << 23) | (man << 13); } float f; #if defined(__HIP_DEVICE_COMPILE__) __builtin_memcpy(&f, &out, 4); #else std::memcpy(&f, &out, 4); #endif return f; } } // namespace strata::kernels