okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
06da32193
main
1// bench_blob_layout (tests/prefill/bench_blob_layout.hpp): the byte prefill_expert_paths_bench changes per expert is never a scale byte, for every 2// format in the table. No GPU, no model, no ggml. 3#include <cstdint> 4#include <cstdio> 5#include <set> 6#include <vector> 7 8#include "bench_blob_layout.hpp" 9#if defined(BBL_HAVE_QUANT ) 10#include "strata/quant/types.hpp" 11#endif 12 13using strata::bench_blob::BlockLayout ; 14using strata::bench_blob::is_scale ; 15using strata::bench_blob::layout_of ; 16using strata::bench_blob::perturb_offset ; 17 18static int failed = 0 ; 19#define CHECK (x ) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0) 20 21int main () { 22const int types []= {2 ,3 ,6 ,7 ,8 ,10 ,11 ,12 ,13 ,14 ,16 ,17 ,18 ,19 ,20 ,21 ,22 ,23 ,29 ,40 ,42 }; 23for (int t :types ) { 24BlockLayout L ; 25CHECK (layout_of (t ,L )); 26CHECK (L .type == t && L .bytes > 0 && L .values > 0 ); 27int data = 0 ; 28for (int b = 0 ;b < L .bytes ;++ b )data += !is_scale (L ,b ); 29CHECK (data > 0 && data < L .bytes );// every format has a scale and quant bytes 30for (const auto & s :L .scale )CHECK (s .begin >=0 && s .begin < s .end && s .end <=L .bytes ); 31#if defined(BBL_HAVE_QUANT ) 32// the geometry equals the engine's own type table (the one the bench checks against at run time) 33const strata::quant::TypeInfo * ti = strata::quant::type_info (t ); 34CHECK (ti != nullptr && (int )ti -> bytes == L .bytes && ti -> blck == L .values ); 35#endif 36 37// a base blob of 40 blocks (the bench's gate row of 2,560 values is 10 to 80 blocks), every expert of a 512-expert layer 38const size_t n = (size_t )L .bytes * 40 ; 39 std::vector < uint8_t > base (n ); 40uint32_t x = 2463534242u + (uint32_t )t ; 41for (uint8_t & v :base ) {x ^=x <<13 ;x ^=x >>17 ;x ^=x <<5 ;v = (uint8_t )x ; } 42 std::set < size_t > offs ; 43for (int e = 0 ;e < 512 ;++ e ) { 44const size_t o = perturb_offset (L ,e ); 45CHECK (o >=4 && o < n ); 46CHECK (!is_scale (L , (int ) (o % (size_t )L .bytes ))); 47offs .insert (o ); 48 std::vector < uint8_t > b = base ; 49b [o ] ^= (uint8_t )e ; 50int differ = 0 ,scale_differ = 0 ; 51for (size_t i = 0 ;i < n ;++ i ) { 52if (b [i ]== base [i ])continue ; 53++ differ ; 54scale_differ += is_scale (L , (int ) (i % (size_t )L .bytes )); 55 } 56CHECK (scale_differ == 0 );// the scales stay bit-equal to the base 57CHECK (differ == ((uint8_t )e != 0 ?1 :0 ));// one quant byte, or none where e is a multiple of 256 58 } 59CHECK (offs .size ()== 16 );// sixteen distinct places, as the fixed offsets had 60 } 61 62// Q2_0: the old offsets 18 and 19 were block 1's scale; the walk takes 4..17, then 20 and 21 63BlockLayout q ; 64CHECK (layout_of (42 ,q )); 65CHECK (is_scale (q ,18 %q .bytes )&& is_scale (q ,19 %q .bytes )); 66CHECK (perturb_offset (q ,0 )== 4 && perturb_offset (q ,13 )== 17 && perturb_offset (q ,14 )== 20 && perturb_offset (q ,15 )== 21 ); 67CHECK (perturb_offset (q ,31 )== 21 && perturb_offset (q ,16 )== 4 ); 68// IQ4_NL as a gate/up type had the same fault (18-byte blocks) 69BlockLayout nl ; 70CHECK (layout_of (20 ,nl )&& perturb_offset (nl ,14 )== 20 ); 71// formats whose bytes 4..19 hold no scale bits keep the old offsets (and so the blobs of earlier runs) 72for (int t : {6 ,7 ,8 ,14 ,17 ,18 ,21 ,22 ,29 }) { 73BlockLayout L ; 74CHECK (layout_of (t ,L )); 75for (int e = 0 ;e < 16 ;++ e )CHECK (perturb_offset (L ,e )== (size_t ) (4 + e )); 76 } 77// IQ2_XXS: byte 9 and 17 carry a group's scale nibble, so the walk steps over them 78BlockLayout xx ; 79CHECK (layout_of (16 ,xx )); 80CHECK (is_scale (xx ,9 )&& is_scale (xx ,17 )&& perturb_offset (xx ,4 )== 8 && perturb_offset (xx ,5 )== 10 ); 81// IQ4_XS: scales_l sits at 4..7, so the walk starts at 8 82BlockLayout x4 ; 83CHECK (layout_of (23 ,x4 )&& perturb_offset (x4 ,0 )== 8 ); 84// a type the table does not know is refused, not guessed 85BlockLayout u ; 86CHECK (!layout_of (24 ,u )&& !layout_of (144 ,u )&& !layout_of (-1 ,u )); 87 88if (failed ) { 89 std::fprintf (stderr ,"bench_blob_layout_test: %d check(s) failed\n" ,failed ); 90return 1 ; 91 } 92#if defined(BBL_HAVE_QUANT ) 93 std::printf ("bench_blob_layout_test: OK (geometry checked against strata::quant::type_info)\n" ); 94#else 95 std::printf ("bench_blob_layout_test: OK (no strata_quant in this build: geometry not cross-checked)\n" ); 96#endif 97return 0 ; 98}