okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
60d431e2a
main
1// tools/vision/own/test_cpu_kernels.cpp - every kernel tier of the CPU tower (the ones this CPU runs) against double-precision references, odd sizes and tails 2// included, and the fork-join team (every task of every run exactly once). strata-vision-own-cpu-test 3#include "cpu_kernels.h" 4#include "cpu_team.h" 5#include "vit_cpu.h" 6#include "vit_f16.h" 7 8#include <atomic> 9#include <cmath> 10#include <cstdio> 11#include <cstdlib> 12#include <cstring> 13#include <limits> 14#include <random> 15#include <vector> 16 17using namespace strata_vit ; 18 19static int g_fail = 0 ; 20static void check (bool ok ,const char * what ,const char * tier ,double worst ,double tol ) { 21if (!ok ) { 22++ g_fail ; 23 std::printf ("FAIL %-8s %s: worst %.3g (tolerance %.3g)\n" ,tier ,what ,worst ,tol ); 24 } 25} 26 27static std::mt19937 rng (12345 ); 28static float rnd (float lo ,float hi ) {return std::uniform_real_distribution < float > (lo ,hi )(rng ); } 29 30static void test_gemm (const CpuOps & o ) { 31const int Ms []= {1 ,5 ,13 ,49 ,100 },Ks []= {1 ,7 ,72 ,256 ,300 },Ns []= {1 ,8 ,17 ,33 ,72 ,100 }; 32double worst = 0 ,tol = 0 ; 33for (int M :Ms ) 34for (int K :Ks ) 35for (int N :Ns ) 36for (int init = 0 ;init < 3 ;++ init ) { 37const int lda = K + 3 ,ldb = (N + 63 ) /64 * 64 + 64 ,ldc = N + 5 ; 38 std::vector < float > A ((size_t )M * lda ),B ((size_t )K * ldb + 128 ),C ((size_t )M * ldc ),C0 ,bias ((size_t )N ); 39for (auto & v :A )v = rnd (-1 ,1 ); 40for (auto & v :B )v = rnd (-1 ,1 ); 41for (auto & v :C )v = rnd (-1 ,1 ); 42for (auto & v :bias )v = rnd (-1 ,1 ); 43C0 = C ; 44o .gemm (A .data (), (size_t )lda ,M ,B .data (), (size_t )ldb ,K ,N ,C .data (), (size_t )ldc ,init ,init == 1 ?nullptr :bias .data ()); 45for (int m = 0 ;m < M ;++ m ) 46for (int n = 0 ;n < N ;++ n ) { 47double s = init == 0 ?0.0 :C0 [(size_t )m * ldc + n ]; 48if (init != 1 )s += bias [(size_t )n ]; 49for (int k = 0 ;k < K ;++ k )s += (double )A [(size_t )m * lda + k ]* B [(size_t )k * ldb + n ]; 50worst = std::max (worst , std::fabs (s - C [(size_t )m * ldc + n ])); 51 } 52// columns past N and rows past M must be untouched 53for (int m = 0 ;m < M ;++ m ) 54for (int n = N ;n < ldc ;++ n ) 55if (C [(size_t )m * ldc + n ]!= C0 [(size_t )m * ldc + n ])worst = 1e9 ; 56tol = std::max (tol ,1e-5 * (K + 2 )); 57 } 58check (worst <=tol ,"gemm (odd M, K, N; init modes 0, 1, 2)" ,o .name ,worst ,tol ); 59} 60 61static uint16_t rnd_f16bits () {return (uint16_t ) ((rng ()& 0x8000 ) | ((rng () %28 + 2 ) <<10 ) | (rng ()& 0x3ff )); }// a finite normal half 62static uint16_t rnd_bf16bits () {return (uint16_t ) ((rng ()& 0x8000 ) | ((rng () %200 + 20 ) <<7 ) | (rng ()& 0x7f )); } 63 64static void test_pack_values (const CpuOps & o ) { 65const int ldw = 300 ,rows = 40 ,K = 300 ; 66for (int kind = 0 ;kind <=2 ;++ kind ) { 67 std::vector < uint16_t > w16 ((size_t )rows * ldw ); 68 std::vector < float > w32 ((size_t )rows * ldw ); 69for (size_t i = 0 ;i < w16 .size ();++ i ) { 70w16 [i ]= kind == 2 ?rnd_f16bits () :rnd_bf16bits (); 71w32 [i ]= rnd (-2 ,2 ); 72 } 73const void * W = kind == 0 ? (const void * )w32 .data () : (const void * )w16 .data (); 74for (int nvalid : {o .NR ,5 }) { 75const int n0 = 3 ,k0 = 17 ,kc = 200 ; 76 std::vector < float > dst ((size_t )kc * o .NR ,-7.0f ); 77o .pack_w (W ,kind , (size_t )ldw ,n0 ,nvalid ,k0 ,kc ,dst .data ()); 78double worst = 0 ; 79for (int k = 0 ;k < kc ;++ k ) 80for (int c = 0 ;c < o .NR ;++ c ) { 81float want = 0.0f ; 82if (c < nvalid ) { 83const size_t idx = (size_t ) (n0 + c )* ldw + (size_t ) (k0 + k ); 84if (kind == 0 )want = w32 [idx ]; 85else if (kind == 1 )want = quant::bf16_to_f32 (w16 [idx ]); 86else want = quant::f16_to_f32 (w16 [idx ]); 87 } 88worst = std::max (worst , (double ) std::fabs (want - dst [(size_t )k * o .NR + c ])); 89 } 90check (worst == 0.0 ,kind == 0 ?"pack_w float32" :kind == 1 ?"pack_w BF16" :"pack_w F16" ,o .name ,worst ,0.0 ); 91 } 92 } 93 (void )K ; 94} 95 96static void test_conv (const CpuOps & o ) { 97for (int kind = 1 ;kind <=2 ;++ kind ) 98for (size_t n : {1u ,15u ,32u ,33u ,5000u ,8192u + 7u }) { 99 std::vector < uint16_t > w (n ); 100for (auto & v :w )v = kind == 2 ?rnd_f16bits () :rnd_bf16bits (); 101 std::vector < float > d (n ,-1.0f ); 102o .conv (w .data (),kind ,n ,d .data ()); 103double worst = 0 ; 104for (size_t i = 0 ;i < n ;++ i )worst = std::max (worst , (double ) std::fabs (d [i ]- (kind == 1 ? quant::bf16_to_f32 (w [i ]) : quant::f16_to_f32 (w [i ])))); 105check (worst == 0.0 ,kind == 1 ?"conv BF16" :"conv F16" ,o .name ,worst ,0.0 ); 106 } 107} 108 109static void test_round (const CpuOps & o ) { 110const CpuOps & ref = cpu_ops_scalar (); 111for (int kind = 1 ;kind <=2 ;++ kind ) 112for (size_t n : {1u ,7u ,8u ,17u ,100u ,4099u }) { 113 std::vector < float > x (n ),a ,b ; 114for (size_t i = 0 ;i < n ;++ i )x [i ]= (i %5 == 0 ) ?rnd (-1e-4f ,1e-4f ) : (i %7 == 0 ) ?rnd (-3000 ,3000 ) :rnd (-4 ,4 ); 115a = x ; 116b = x ; 117o .round16 (a .data (),n ,kind ); 118ref .round16 (b .data (),n ,kind ); 119size_t bad = 0 ; 120for (size_t i = 0 ;i < n ;++ i )bad += std::memcmp (& a [i ],& b [i ],4 )!= 0 ; 121check (bad == 0 ,kind == 1 ?"round16 BF16 equals the scalar tier bit for bit" :"round16 F16 equals the scalar tier bit for bit" ,o .name , (double )bad ,0.0 ); 122 } 123} 124 125static void test_gelu (const CpuOps & o ) { 126const int rows = 3 ,n = 203 ,ld = 211 ; 127 std::vector < float > x ((size_t )rows * ld ),y ; 128for (auto & v :x )v = rnd (-12 ,12 ); 129y = x ; 130o .gelu (y .data (), (size_t )ld ,rows ,n ); 131double worst = 0 ; 132for (int r = 0 ;r < rows ;++ r ) 133for (int i = 0 ;i < n ;++ i ) { 134const double v = x [(size_t )r * ld + i ]; 135const double want = 0.5 * v * (1.0 + std::tanh (0.7978845608028654 * v * (1.0 + 0.044715 * v * v ))); 136worst = std::max (worst , std::fabs (want - y [(size_t )r * ld + i ]) / std::max (1.0 , std::fabs (v ))); 137 } 138for (int r = 0 ;r < rows ;++ r ) 139for (int i = n ;i < ld ;++ i ) 140if (y [(size_t )r * ld + i ]!= x [(size_t )r * ld + i ])worst = 1e9 ; 141check (worst <=3e-6 ,"gelu" ,o .name ,worst ,3e-6 ); 142} 143 144static void test_layernorm (const CpuOps & o ) { 145for (int D : {1152 ,7 ,72 ,4608 }) { 146 std::vector < float > x ((size_t )D ),w ((size_t )D ),b ((size_t )D ),y ((size_t )D ); 147for (int i = 0 ;i < D ;++ i ) {x [(size_t )i ]= rnd (-3 ,5 );w [(size_t )i ]= rnd (0.5f ,1.5f );b [(size_t )i ]= rnd (-1 ,1 ); } 148o .layernorm (x .data (),y .data (),D ,w .data (),b .data (),1e-6f ); 149double s = 0 ,q = 0 ,worst = 0 ; 150for (int i = 0 ;i < D ;++ i )s += x [(size_t )i ]; 151const double mean = s /D ; 152for (int i = 0 ;i < D ;++ i )q += (x [(size_t )i ]- mean )* (x [(size_t )i ]- mean ); 153const double scale = 1.0 / std::sqrt (q /D + 1e-6 ); 154for (int i = 0 ;i < D ;++ i )worst = std::max (worst , std::fabs ((x [(size_t )i ]- mean )* scale * w [(size_t )i ]+ b [(size_t )i ]- y [(size_t )i ])); 155check (worst <=2e-5 ,"layernorm" ,o .name ,worst ,2e-5 ); 156 } 157} 158 159static void test_add (const CpuOps & o ) { 160for (size_t n : {1u ,7u ,8u ,33u ,1000u }) { 161 std::vector < float > x (n ),y (n ),z ; 162for (size_t i = 0 ;i < n ;++ i ) {x [i ]= rnd (-1 ,1 );y [i ]= rnd (-1 ,1 ); } 163z = x ; 164o .add (z .data (),y .data (),n ); 165double worst = 0 ; 166for (size_t i = 0 ;i < n ;++ i )worst = std::max (worst , (double ) std::fabs (z [i ]- (x [i ]+ y [i ]))); 167check (worst == 0.0 ,"add" ,o .name ,worst ,0.0 ); 168 } 169} 170 171// the online softmax over two key tiles, then P*V by the product kernel, against a plain softmax in double 172static void test_attention (const CpuOps & o ) { 173const int qr = 13 ,hd = 72 ,T1 = 100 ,T2 = 37 ,n = T1 + T2 ,lds = 256 ,ldo = 80 ,ldv = 3 * 1152 ; 174 std::vector < float > S1 ((size_t )qr * lds ),S2 ((size_t )qr * lds ),Sfull ((size_t )qr * n ),V ((size_t )n * ldv + 256 ),O ((size_t )qr * ldo ,0.0f ),m ((size_t )qr ,-3.0e38f ),l ((size_t )qr ,0.0f ); 175for (int r = 0 ;r < qr ;++ r ) 176for (int j = 0 ;j < n ;++ j ) { 177const float s = rnd (-30 ,30 ); 178Sfull [(size_t )r * n + j ]= s ; 179 (j < T1 ?S1 [(size_t )r * lds + j ] :S2 [(size_t )r * lds + (j - T1 )])= s ; 180 } 181for (auto & v :V )v = rnd (-1 ,1 ); 182o .softmax_update (S1 .data (), (size_t )lds ,qr ,T1 ,m .data (),l .data (),O .data (), (size_t )ldo ,hd ); 183o .gemm (S1 .data (), (size_t )lds ,qr ,V .data (), (size_t )ldv ,T1 ,hd ,O .data (), (size_t )ldo ,1 ,nullptr ); 184o .softmax_update (S2 .data (), (size_t )lds ,qr ,T2 ,m .data (),l .data (),O .data (), (size_t )ldo ,hd ); 185o .gemm (S2 .data (), (size_t )lds ,qr ,V .data ()+ (size_t )T1 * ldv , (size_t )ldv ,T2 ,hd ,O .data (), (size_t )ldo ,1 ,nullptr ); 186double worst = 0 ; 187for (int r = 0 ;r < qr ;++ r ) { 188double mx = -1e300 ,den = 0 ; 189for (int j = 0 ;j < n ;++ j )mx = std::max (mx , (double )Sfull [(size_t )r * n + j ]); 190for (int j = 0 ;j < n ;++ j )den += std::exp (Sfull [(size_t )r * n + j ]- mx ); 191for (int d = 0 ;d < hd ;++ d ) { 192double acc = 0 ; 193for (int j = 0 ;j < n ;++ j )acc += std::exp (Sfull [(size_t )r * n + j ]- mx ) /den * V [(size_t )j * ldv + d ]; 194worst = std::max (worst , std::fabs (acc - O [(size_t )r * ldo + d ] /l [(size_t )r ])); 195 } 196 } 197check (worst <=2e-5 ,"online softmax + P*V" ,o .name ,worst ,2e-5 ); 198} 199 200// the rotary of one token row and the position add against double 201static void test_rope_pos (const CpuOps & o ) { 202const int H = 3 ,HD = 72 ,D = H * HD ,half = HD /2 ,quarter = HD /4 ; 203 std::vector < float > row ((size_t )2 * D + 16 ),c1 (quarter ),s1 (quarter ),c2 (quarter ),s2 (quarter ); 204for (auto & v :row )v = rnd (-2 ,2 ); 205for (int j = 0 ;j < quarter ;++ j ) {c1 [j ]= std::cos (0.3f * j );s1 [j ]= std::sin (0.3f * j );c2 [j ]= std::cos (0.7f * j + 1 );s2 [j ]= std::sin (0.7f * j + 1 ); } 206 std::vector < float > got = row ; 207o .rope_row (got .data (), (size_t )D ,H ,HD ,c1 .data (),s1 .data (),c2 .data (),s2 .data ()); 208double worst = 0 ; 209for (int part = 0 ;part < 2 ;++ part ) 210for (int hh = 0 ;hh < H ;++ hh ) 211for (int j = 0 ;j < half ;++ j ) { 212const size_t i0 = (size_t )part * D + (size_t )hh * HD + j ,i1 = i0 + half ; 213const double co = j < quarter ?c1 [j ] :c2 [j - quarter ],si = j < quarter ?s1 [j ] :s2 [j - quarter ]; 214worst = std::max (worst , std::fabs ((double )row [i0 ]* co - (double )row [i1 ]* si - got [i0 ])); 215worst = std::max (worst , std::fabs ((double )row [i0 ]* si + (double )row [i1 ]* co - got [i1 ])); 216 } 217for (size_t i = (size_t )2 * D ;i < row .size ();++ i )if (got [i ]!= row [i ])worst = 1e9 ; 218check (worst <=2e-6 ,"rope_row" ,o .name ,worst ,2e-6 ); 219const int n = 1152 ; 220 std::vector < float > x (n ),a (n ),b (n ),c (n ),d (n ); 221for (int i = 0 ;i < n ;++ i ) {x [i ]= rnd (-1 ,1 );a [i ]= rnd (-1 ,1 );b [i ]= rnd (-1 ,1 );c [i ]= rnd (-1 ,1 );d [i ]= rnd (-1 ,1 ); } 222 std::vector < float > y = x ; 223const float dx = 0.37f ,dy = 0.81f ; 224o .pos_add (y .data (),a .data (),b .data (),c .data (),d .data (),dx ,dy ,n ); 225worst = 0 ; 226for (int i = 0 ;i < n ;++ i ) { 227const double want = x [i ]+ a [i ]* (1.0 - dx )* (1.0 - dy )+ b [i ]* (double )dx * (1.0 - dy )+ c [i ]* (1.0 - dx )* dy + d [i ]* (double )dx * dy ; 228worst = std::max (worst , std::fabs (want - y [i ])); 229 } 230check (worst <=2e-6 ,"pos_add" ,o .name ,worst ,2e-6 ); 231} 232// The SSE4.1 tier multiplies then adds in the scalar tier's k order, so on x86 its products equal the scalar tier's bit for bit (cpu_kernels_sse41.cpp) 233static void test_gemm_equals_scalar (const CpuOps & o ) { 234const CpuOps & ref = cpu_ops_scalar (); 235size_t bad = 0 ; 236for (int M : {1 ,7 ,13 ,50 }) 237for (int K : {1 ,72 ,300 }) 238for (int N : {3 ,8 ,21 ,72 }) 239for (int init = 0 ;init < 3 ;++ init ) { 240const int lda = K + 1 ,ldb = (N + 63 ) /64 * 64 + 64 ,ldc = N + 3 ; 241 std::vector < float > A ((size_t )M * lda ),B ((size_t )K * ldb + 128 ),C ((size_t )M * ldc ),bias ((size_t )N ); 242for (auto & v :A )v = rnd (-1 ,1 ); 243for (auto & v :B )v = rnd (-1 ,1 ); 244for (auto & v :C )v = rnd (-1 ,1 ); 245for (auto & v :bias )v = rnd (-1 ,1 ); 246 std::vector < float > C2 = C ; 247o .gemm (A .data (), (size_t )lda ,M ,B .data (), (size_t )ldb ,K ,N ,C .data (), (size_t )ldc ,init ,bias .data ()); 248ref .gemm (A .data (), (size_t )lda ,M ,B .data (), (size_t )ldb ,K ,N ,C2 .data (), (size_t )ldc ,init ,bias .data ()); 249for (size_t i = 0 ;i < C .size ();++ i )bad += std::memcmp (& C [i ],& C2 [i ],4 )!= 0 ; 250 } 251check (bad == 0 ,"gemm equals the scalar tier bit for bit" ,o .name , (double )bad ,0.0 ); 252} 253static void test_team () { 254for (int nth : {1 ,2 ,7 ,16 }) { 255Team t (nth ); 256bool ok = true; 257for (int run = 0 ;run < 300 && ok ;++ run ) { 258const int n = 1 + (run * 7 ) %90 ; 259 std::vector < std::atomic < int >> hits ((size_t )n ); 260for (auto & h :hits )h .store (0 ); 261 std::atomic < int > bad_thread {0 }; 262t .run (n , [& ](int task ,int thr ) { 263if (thr < 0 || thr >=nth )bad_thread .fetch_add (1 ); 264hits [(size_t )task ].fetch_add (1 ); 265 }); 266if (bad_thread .load ()) {ok = false; std::printf ("team(%d) run %d: %d tasks saw a thread index out of range\n" ,nth ,run ,bad_thread .load ()); } 267for (size_t i = 0 ;i < hits .size ();++ i ) 268if (hits [i ].load ()!= 1 ) {ok = false; std::printf ("team(%d) run %d of %d tasks: task %zu ran %d times\n" ,nth ,run ,n ,i ,hits [i ].load ()); } 269if (run %50 == 0 ) std::this_thread::sleep_for (std::chrono::milliseconds (2 ));// let the workers go to sleep once in a while 270 } 271check (ok ,"team: every task once, thread index in range" ,"-" ,0 ,0 ); 272 } 273} 274 275// the CPU tower's rounding default: none for every weight type (BF16 included since 11-14: code4217.png token 47 0.627 -> 1.000 against float32); 276// STRATA_VIT_REF_ROUND=1 rounds to the weight's 16-bit type, float32 / Q8_0-expanded never round 277static void test_round_choice () { 278struct Case {int kind ;const char * env ;int want ; }; 279const Case cs []= {{1 ,nullptr ,0 }, {2 ,nullptr ,0 }, {0 ,nullptr ,0 }, {1 ,"0" ,0 }, {2 ,"1" ,2 }, {1 ,"1" ,1 }, {2 ,"0" ,0 }, {0 ,"1" ,0 }, {1 ,"" ,0 }, {2 ,"" ,0 }}; 280bool ok = true; 281for (const Case & c :cs )ok = ok && vit_cpu_round_kind (c .kind ,c .env )== c .want ; 282check (ok ,"rounding default per weight type and its override" ,"-" ,0 ,0 ); 283} 284 285// the GPU reader's F16 kind (vit_f16.h): which BF16 values F16 holds, the error of the others, the activation clamp and STRATA_VIT_BF16_AS_F16 286static float bf16_value (uint16_t b ) {const uint32_t u = (uint32_t )b <<16 ;float f ; std::memcpy (& f ,& u ,4 );return f ; } 287static void test_f16_kind () { 288// named cases: 1.0, -1.0, 2^-14, 2^-17 with all mantissa bits, 2^-18 with the lowest bit (not) and the second lowest (yes), 2^-24, 2^-25, 289// 65,280 (the largest BF16 value under 65,504), 65,536, 2^20, +-0, inf, -inf, nan, a BF16 subnormal, -(2^-18 + lowest bit) 290struct Case {uint16_t b ;bool exact ; }; 291const Case cs []= {{0x3F80 , true}, {0xBF80 , true}, {0x3880 , true}, {0x377F , true}, {0x3681 , false}, {0x3682 , true}, {0x3380 , true}, {0x3300 , false}, 292 {0x477F , true}, {0x4780 , false}, {0x4980 , false}, {0x0000 , true}, {0x8000 , true}, {0x7F80 , false}, {0xFF80 , false}, {0x7FC0 , false}, 293 {0x0001 , false}, {0xB681 , false}}; 294bool ok = true; 295for (const Case & c :cs )if (bf16_exact_in_f16 (c .b )!= c .exact ) {ok = false; std::printf (" bf16 0x%04x: exact %d, want %d\n" ,c .b , (int ) !c .exact , (int )c .exact ); } 296check (ok ,"F16 kind: named BF16 values" ,"-" ,0 ,0 ); 297// all 65,536 BF16 values against an independent rule in double precision: finite, |v| <= 65504 and a multiple of F16's step at that size 298// (2^-24 under 2^-14, else 2^(floor(log2 |v|) - 10)); the error of the others in range is the distance to the nearest step multiple (ties to even) 299int bad = 0 ,bad_err = 0 ; 300double worst_err = 0.0 ; 301for (uint32_t b = 0 ;b < 65536 ;++ b ) { 302const double v = bf16_value ((uint16_t )b ),a = std::fabs (v ); 303const bool fin = std::isfinite (v ); 304bool want = false; 305double want_err = 0.0 ; 306if (fin && a <=65504.0 ) { 307int ex = 0 ; 308 (void ) std::frexp (a ,& ex );// a = f * 2^ex, f in [0.5, 1) 309const double step = a < std::ldexp (1.0 ,-14 ) ? std::ldexp (1.0 ,-24 ) : std::ldexp (1.0 ,ex - 1 - 10 ); 310const double q = a /step ,r = std::nearbyint (q ); 311want = a == 0.0 || q == r ; 312want_err = want ?0.0 : std::fabs (q - r )* step ; 313 } 314if (bf16_exact_in_f16 ((uint16_t )b )!= want )++ bad ; 315const double got_err = bf16_f16_abs_error ((uint16_t )b ); 316if (got_err != want_err )++ bad_err ; 317if (got_err > worst_err )worst_err = got_err ; 318 } 319check (bad == 0 ,"F16 kind: every BF16 value against the double-precision rule" ,"-" , (double )bad ,0.0 ); 320check (bad_err == 0 && worst_err == std::ldexp (1.0 ,-25 ),"F16 kind: rounding error of the values under 2^-14 (largest 2^-25)" ,"-" ,worst_err , std::ldexp (1.0 ,-25 )); 321// a matrix: counts, and it stays BF16 only for a weight beyond F16's range 322const uint16_t w1 []= {0x3F80 ,0x3681 ,0x3300 ,0x377F ,0x0001 }; 323const uint16_t w2 []= {0x3F80 ,0x4780 ,0x7FC0 ,0x3681 }; 324const Bf16F16Count c1 = bf16_count_f16 (w1 ,5 ),c2 = bf16_count_f16 (w2 ,4 ); 325check (c1 .not_exact == 3 && c1 .overflow == 0 && c1 .max_abs_error == std::ldexp (1.0 ,-25 )&& bf16_matrix_fits_f16 (c1 .overflow )&& 326c2 .not_exact == 3 && c2 .overflow == 2 && !bf16_matrix_fits_f16 (c2 .overflow ),"F16 kind: matrix counts and the BF16 fallback" ,"-" ,0 ,0 ); 327// the overflow count the load uses (no trace) equals the full count's over all 65,536 values: exponents 143-255, both signs 328 std::vector < uint16_t > all (65536 ); 329for (uint32_t b = 0 ;b < 65536 ;++ b )all [b ]= (uint16_t )b ; 330const Bf16F16Count ca = bf16_count_f16 (all .data (),65536 ); 331const long long nb = bf16_count_beyond_f16 (all .data (),65536 ); 332check (nb == ca .overflow && nb == 113LL * 256 && !bf16_beyond_f16 (0x477F )&& bf16_beyond_f16 (0x4780 )&& bf16_beyond_f16 (0xFFC0 ), 333"F16 kind: the overflow count of the load" ,"-" , (double )nb , (double )ca .overflow ); 334// the activation clamp: F16's finite range, NaN stays NaN 335const float big = 70000.0f ,inf = std::numeric_limits < float > ::infinity (); 336check (f16_clamp (big )== 65504.0f && f16_clamp (- big )== -65504.0f && f16_clamp (inf )== 65504.0f && f16_clamp (- inf )== -65504.0f && 337f16_clamp (65504.0f )== 65504.0f && f16_clamp (-1.5f )== -1.5f && f16_clamp (0.0f )== 0.0f && std::isnan (f16_clamp (std::nanf ("" ))), 338"F16 kind: activation clamp to +-65,504" ,"-" ,0 ,0 ); 339// STRATA_VIT_BF16_AS_F16: unset = the card's default; 0 / empty / not a number = as stored; 2 = F16 inputs; any other number = the BF16-rounded kind 340struct M {const char * env ;int dflt ;int want ; }; 341const M ms []= {{nullptr ,BF16_TO_F16 ,BF16_TO_F16 }, {nullptr ,BF16_ROUNDED ,BF16_ROUNDED }, {"0" ,BF16_TO_F16 ,BF16_STORED }, {"0" ,BF16_ROUNDED ,BF16_STORED }, 342 {"" ,BF16_TO_F16 ,BF16_STORED }, {"x" ,BF16_TO_F16 ,BF16_STORED }, {"1" ,BF16_TO_F16 ,BF16_ROUNDED }, {"1" ,BF16_ROUNDED ,BF16_ROUNDED }, 343 {"2" ,BF16_ROUNDED ,BF16_TO_F16 }, {"2" ,BF16_TO_F16 ,BF16_TO_F16 }, {"3" ,BF16_TO_F16 ,BF16_ROUNDED }, {"-1" ,BF16_TO_F16 ,BF16_ROUNDED }, 344 {" 2" ,BF16_STORED ,BF16_TO_F16 }, {"12" ,BF16_TO_F16 ,BF16_ROUNDED }}; 345bool mok = true; 346for (const M & c :ms )if (vit_gpu_bf16_mode (c .env ,c .dflt )!= c .want ) {mok = false; std::printf (" env '%s' default %d: %d, want %d\n" ,c .env ?c .env :"(unset)" ,c .dflt ,vit_gpu_bf16_mode (c .env ,c .dflt ),c .want ); } 347check (mok ,"F16 kind: STRATA_VIT_BF16_AS_F16 values" ,"-" ,0 ,0 ); 348} 349 350int main (int argc ,char ** argv ) { 351if (argc > 1 && std::strcmp (argv [1 ],"team" )== 0 ) {// only the fork-join team (a loop under ThreadSanitizer) 352const int reps = argc > 2 ? std::atoi (argv [2 ]) :1 ; 353for (int i = 0 ;i < reps ;++ i )test_team (); 354 std::printf (g_fail ?"%d FAILED\n" :"team: all passed\n" ,g_fail ); 355return g_fail ?1 :0 ; 356 } 357const quant::Caps & c = quant::caps (); 358 std::printf ("cpu: sse41 %d avx %d avx2 %d fma %d f16c %d avx512 %d; best tier %s\n" ,c .sse41 ,c .avx ,c .avx2 ,c .fma ,c .f16c ,c .avx512 , 359 quant::isa_name (quant::best_isa ())); 360// every tier this CPU runs, capped by STRATA_FORCE_ISA as in the tower (an emulated older CPU never reaches a wider tier); NEON on Arm 361const int cap = (int ) quant::best_isa (); 362 std::vector < const CpuOps *> tiers = {& cpu_ops_scalar ()}; 363if (cap >= (int ) quant::Isa ::Sse41 && cap != (int ) quant::Isa ::Neon && quant::isa_ok (quant::Isa ::Sse41 ))tiers .push_back (& cpu_ops_sse41 ()); 364if (cap >= (int ) quant::Isa ::Avx && cap != (int ) quant::Isa ::Neon && quant::isa_ok (quant::Isa ::Avx ))tiers .push_back (& cpu_ops_avx ()); 365if (cap >= (int ) quant::Isa ::Avx2 && cap != (int ) quant::Isa ::Neon && quant::isa_ok (quant::Isa ::Avx2 ))tiers .push_back (& cpu_ops_avx2 ()); 366if (cap == (int ) quant::Isa ::Avx512 && quant::isa_ok (quant::Isa ::Avx512 ))tiers .push_back (& cpu_ops_avx512 ()); 367if (quant::isa_ok (quant::Isa ::Neon ))tiers .push_back (& cpu_ops_neon ()); 368for (const CpuOps * o :tiers ) { 369 std::printf ("tier %s (tile %d x %d)\n" ,o -> name ,o -> MR ,o -> NR ); 370test_gemm (* o ); 371test_pack_values (* o ); 372test_conv (* o ); 373test_round (* o ); 374test_gelu (* o ); 375test_layernorm (* o ); 376test_add (* o ); 377test_attention (* o ); 378test_rope_pos (* o ); 379#if defined(__x86_64__ )|| defined(_M_X64 ) 380if (std::strcmp (o -> name ,"sse41" )== 0 )test_gemm_equals_scalar (* o ); 381#endif 382 } 383test_team (); 384test_round_choice (); 385test_f16_kind (); 386 std::printf (g_fail ?"%d FAILED\n" :"all passed\n" ,g_fail ); 387return g_fail ?1 :0 ; 388}