okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
60d431e2a
main
1// tools/vision/own/vit_f16.h - the GPU reader's rules for holding a BF16 matrix as F16 (vit_gpu.cu), plain C++ so the CPU unit test 2// (test_cpu_kernels.cpp) checks them without a GPU. 3#pragma once 4 5#include <cmath> 6#include <cstdint> 7 8#if defined(__CUDACC__ )|| defined(__HIP__ ) 9#define VIT_F16_HD __host__ __device__ 10#else 11#define VIT_F16_HD 12#endif 13 14namespace strata_vit { 15 16// how a BF16 matrix reaches the GPU's products (STRATA_VIT_BF16_AS_F16) 17enum Bf16Mode { 18BF16_STORED = 0 ,// BF16 weights, BF16 inputs 19BF16_ROUNDED = 1 ,// weights as F16, inputs rounded to BF16 and then written as F16 (the gfx1151 kind: the products see BF16 values) 20BF16_TO_F16 = 2 ,// weights as F16, inputs written as F16 (10-bit mantissa instead of BF16's 7) 21}; 22 23// the mode from STRATA_VIT_BF16_AS_F16 (env, nullptr when unset) and the card's default: "2" is BF16_TO_F16; "0", an empty or a non-numeric value 24// BF16_STORED and every other number BF16_ROUNDED, as before the F16 kind existed 25inline int vit_gpu_bf16_mode (const char * env ,int card_default ) { 26if (!env )return card_default ; 27long v = 0 ; 28bool neg = false; 29const char * p = env ; 30while (* p == ' ' || * p == '\t' )++ p ; 31if (* p == '+' || * p == '-' ) {neg = * p == '-' ;++ p ; } 32for (;* p >='0' && * p <='9' ;++ p ) {v = v * 10 + (* p - '0' );if (v > 1000000 )break ; } 33if (neg )v = - v ; 34return v == 0 ?BF16_STORED :v == 2 ?BF16_TO_F16 :BF16_ROUNDED ; 35} 36 37// true when the BF16 value b is an F16 value: finite, |v| <= 65504, and no mantissa bit below F16's step (2^-24 under 2^-14). Every BF16 value 38// with an exponent from -14 to 15 is one (the largest, 65,280, included); below 2^-14 only those whose lowest set bit is at 2^-24 or above. 39VIT_F16_HD inline bool bf16_exact_in_f16 (uint16_t b ) { 40const int e = (b >>7 )& 0xff ,m = b & 0x7f ; 41if (e == 0xff )return false;// inf, nan 42if (e == 0 )return m == 0 ;// +-0; BF16 subnormals (under 1.2e-38) are not F16 values 43const int E = e - 127 ; 44if (E > 15 )return false;// 65,536 and above 45if (E >=-17 )return true;// the 8 mantissa bits (with the leading one) land at 2^(E-7) >= 2^-24 46if (E < -24 )return false; 47const int s = -17 - E ;// 1..7 bits under 2^-24 48return (m & ((1 <<s )- 1 ))== 0 ; 49} 50 51// an activation clamped to F16's finite range (NaN stays NaN), before it is written as F16 52VIT_F16_HD inline float f16_clamp (float v ) {return v > 65504.0f ?65504.0f :v < -65504.0f ?-65504.0f :v ; } 53 54// true when the BF16 value b lies beyond F16's range (|v| >= 65,536 by its exponent, inf, nan): a matrix with one stays BF16 55VIT_F16_HD inline bool bf16_beyond_f16 (uint16_t b ) {return ((b >>7 )& 0xff )> 127 + 15 ; } 56inline long long bf16_count_beyond_f16 (const uint16_t * w ,long long n ) { 57long long c = 0 ; 58for (long long i = 0 ;i < n ;++ i )c += bf16_beyond_f16 (w [i ]); 59return c ; 60} 61 62// whether a BF16 matrix may be held as F16 in BF16_TO_F16 mode: no weight beyond F16's range. Weights under 2^-14 that F16 holds only to its 63// 2^-24 step (an error of at most 2^-25 each) are counted for the trace but do not keep the matrix in BF16. 64inline bool bf16_matrix_fits_f16 (long long n_overflow ) {return n_overflow == 0 ; } 65 66// the absolute error of a BF16 value F16 cannot hold exactly but holds inside its range (|v| < 2^-14): F16 rounds it to nearest even at its 67// 2^-24 step, so the error is at most 2^-25. 0 for an F16 value or one beyond the range (those are not held as F16). 68inline double bf16_f16_abs_error (uint16_t b ) { 69const int e = (b >>7 )& 0xff ,m = b & 0x7f ; 70if (bf16_exact_in_f16 (b )|| bf16_beyond_f16 (b ))return 0.0 ; 71if (e == 0 )return std ::ldexp ((double )m ,-133 );// a BF16 subnormal: F16 holds 0 72const double q = std ::ldexp ((double ) (0x80 |m ),e - 127 - 7 + 24 );// the value in units of 2^-24 73return std ::ldexp (std ::fabs (q - std ::nearbyint (q )),-24 ); 74} 75 76// counts of one BF16 matrix: weights F16 cannot hold exactly, of those the ones beyond its range (|w| > 65504, inf, nan), and the largest 77// absolute error of the others 78struct Bf16F16Count {long long not_exact = 0 ,overflow = 0 ;double max_abs_error = 0.0 ; }; 79inline Bf16F16Count bf16_count_f16 (const uint16_t * w ,long long n ) { 80Bf16F16Count c ; 81for (long long i = 0 ;i < n ;++ i ) { 82if (bf16_exact_in_f16 (w [i ]))continue ; 83++ c .not_exact ; 84if (bf16_beyond_f16 (w [i ])) {++ c .overflow ;continue ; } 85const double err = bf16_f16_abs_error (w [i ]); 86if (err > c .max_abs_error )c .max_abs_error = err ; 87 } 88return c ; 89} 90 91}// namespace strata_vit