okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e2b0cbcde
main
1// src/kernels/bf16_bits_test.cpp - `bf16_bits.hpp` against ggml's rule, over EVERY f32 bit pattern (CPU only). 2// 3// the rounding add `i + 0x7FFF + bit16` carries out of the mantissa for a NaN, so 0x7FFFFFFF came back as 4// -0 and 0x7F800001 as +inf - an upstream NaN was masked instead of propagated. `ggml_compute_fp32_to_bf16` 5// (ggml-impl.h at the pinned llama.cpp) tests the NaN first and returns `(i >> 16) | 64`. Three checks, all 6// exhaustive (2^32 inputs, a few seconds): 7// 8// 1. the header equals ggml's function, transcribed below, on every input; 9// 2. on every NON-NaN input it equals the previous rounding, so the change cannot move a finite value or an 10// infinity by a single bit; 11// 3. every NaN input gives a quiet NaN of the same sign. 12// 13// And the round trip over all 65,536 bf16 patterns: a non-NaN bf16 survives f32 and back unchanged. 14#include "strata/kernels/bf16_bits.hpp" 15 16#include <cstdint> 17#include <cstdio> 18#include <cstring> 19 20namespace { 21 22/// ggml_compute_fp32_to_bf16, ggml/src/ggml-impl.h (MIT, the ggml authors), on raw bits. 23uint16_t ggml_bf16 (uint32_t u ) { 24if ((u & 0x7fffffffu )> 0x7f800000u )return (uint16_t ) ((u >>16 ) |64 ); 25return (uint16_t ) ((u + (0x7fffu + ((u >>16 )& 1u ))) >>16 ); 26} 27 28/// The rule before this fix, kept only to prove the change is confined to NaN inputs. 29uint16_t previous_bf16 (uint32_t u ) { 30u = (u + ((u >>16 )& 1u )+ 0x7FFFu )& 0xFFFF0000u ; 31return (uint16_t ) (u >>16 ); 32} 33 34bool is_nan_bits (uint32_t u ) {return (u & 0x7fffffffu )> 0x7f800000u ; } 35 36}// namespace 37 38int main () { 39unsigned long long vs_ggml = 0 ,vs_previous = 0 ,nan_lost = 0 ,nan_inputs = 0 ,previous_masked = 0 ; 40uint32_t first_bad = 0 ; 41bool have_bad = false; 42for (uint64_t k = 0 ;k <=0xFFFFFFFFull ;++ k ) { 43const uint32_t u = (uint32_t )k ; 44float f ; 45 std::memcpy (& f ,& u ,4 ); 46const uint16_t got = strata::kernels::bf16_from_f32 (f ); 47if (got != ggml_bf16 (u )) { 48++ vs_ggml ; 49if (!have_bad ) {first_bad = u ;have_bad = true; } 50 } 51if (is_nan_bits (u )) { 52++ nan_inputs ; 53const uint32_t back = (uint32_t )got <<16 ; 54// a quiet NaN (bit 22 of the f32) with the input's sign 55if (!is_nan_bits (back )|| (back & 0x00400000u )== 0 || (back >>31 )!= (u >>31 ))++ nan_lost ; 56if (!is_nan_bits ((uint32_t )previous_bf16 (u ) <<16 ))++ previous_masked ; 57 }else if (got != previous_bf16 (u )) { 58++ vs_previous ; 59if (!have_bad ) {first_bad = u ;have_bad = true; } 60 } 61 } 62 std::printf (" f32 -> bf16 over 2^32 inputs: %llu differ from ggml, %llu non-NaN differ from the previous rule\n" , 63vs_ggml ,vs_previous ); 64 std::printf (" NaN inputs: %llu, not a quiet same-sign NaN now: %llu (the previous rule masked %llu of them)\n" , 65nan_inputs ,nan_lost ,previous_masked ); 66if (have_bad ) std::printf (" first mismatch at input 0x%08x\n" ,first_bad ); 67 68unsigned long long trip_bad = 0 ; 69for (uint32_t h = 0 ;h <=0xFFFFu ;++ h ) { 70const float f = strata::kernels::f32_from_bf16 ((uint16_t )h ); 71const uint16_t back = strata::kernels::bf16_from_f32 (f ); 72const bool nan = is_nan_bits (h <<16 ); 73if (back != (nan ? (uint16_t ) (h |64u ) : (uint16_t )h ))++ trip_bad ; 74 } 75 std::printf (" bf16 -> f32 -> bf16 over 65536 patterns: %llu wrong\n" ,trip_bad ); 76 77const bool ok = vs_ggml == 0 && vs_previous == 0 && nan_lost == 0 && trip_bad == 0 && previous_masked > 0 ; 78 std::printf ("\nbf16_bits_test %s\n" ,ok ?"OK" :"FAILED" ); 79return ok ?0 :1 ; 80}