okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f2eedfe9f
main
1// src/kernels/cpu/cpu_dispatch_dump.cpp - round 0216: what the CPU pool's expert calls (native_gu_rows + native_down_rows, both 2// arithmetics) put out, as a hash per (pair, token count, row range). Run the same binary with a change switched off and on (or a 3// baseline build and a candidate build) and compare the lines: an E change (bit-exact against the code it replaces) gives the same text. 4// 5// cpu_dispatch_dump [--pairs iq4_xs/q5_0,...] [--nt-max 8] [--experts 3] 6// 7// For every pair: --experts random experts (ggml-quantized random weights, valid scales), nt = 1..nt-max tokens, whole rows and 8// row ranges that start and end off any vector boundary (tails), the Default and the WindowInvariant entry points. 9#include "strata/kernels/cpu/expert_layout.hpp" 10#include "strata/kernels/cpu/native_expert.hpp" 11 12#include "ggml.h" 13#include "ggml-cpu.h" 14 15#include <cmath> 16#include <cstdint> 17#include <cstdio> 18#include <cstdlib> 19#include <cstring> 20#include <random> 21#include <string> 22#include <vector> 23 24namespace cpu= strata::kernels::cpu; 25 26namespace { 27 28constexpr int64_t kH = 2560 ,kFF = 640 ; 29constexpr int kMaxT = 8 ; 30 31int type_of (const std::string & s ) { 32for (int t = 0 ;t < GGML_TYPE_COUNT ;++ t ) { 33const char * n = ggml_type_name ((ggml_type )t ); 34if (n != nullptr && s == n )return t ; 35 } 36return -1 ; 37} 38 39std::vector < std::string > split (const std::string & s ) { 40 std::vector < std::string > v ; 41size_t from = 0 ; 42for (;;) { 43const size_t c = s .find (',' ,from ); 44v .push_back (s .substr (from ,c == std::string::npos ? std::string::npos :c - from )); 45if (c == std::string::npos )break ; 46from = c + 1 ; 47 } 48return v ; 49} 50 51uint64_t fnv (uint64_t h ,const void * p ,size_t n ) { 52const uint8_t * b = (const uint8_t * )p ; 53for (size_t i = 0 ;i < n ;++ i )h = (h ^b [i ])* 1099511628211ull ; 54return h ; 55} 56 57}// namespace 58 59int main (int argc ,char ** argv ) { 60setvbuf (stdout ,nullptr ,_IONBF ,0 ); 61 std::vector < std::string > pairs = split ("iq3_xxs/q5_0,iq3_xxs/iq4_nl,iq4_xs/q5_0,iq4_xs/iq4_nl,iq3_s/iq4_nl,iq2_s/iq4_nl,iq4_xs/q8_0,iq2_s/q2_0,iq3_xxs/q2_0" ); 62int nt_max = 8 ,experts = 3 ; 63for (int i = 1 ;i < argc ;++ i ) { 64const std::string a = argv [i ]; 65if (a == "--pairs" && i + 1 < argc )pairs = split (argv [++ i ]); 66else if (a == "--nt-max" && i + 1 < argc )nt_max = std::atoi (argv [++ i ]); 67else if (a == "--experts" && i + 1 < argc )experts = std::atoi (argv [++ i ]); 68else { std::fprintf (stderr ,"usage: cpu_dispatch_dump [--pairs a/b,...] [--nt-max 8] [--experts 3]\n" );return 2 ; } 69 } 70ggml_cpu_init (); 71for (const std::string & pair :pairs ) { 72const size_t slash = pair .find ('/' ); 73const int gt = slash == std::string::npos ?-1 :type_of (pair .substr (0 ,slash )); 74const int dt = slash == std::string::npos ?-1 :type_of (pair .substr (slash + 1 )); 75 cpu::NativeFmt f ; 76 std::string err ; 77if (gt < 0 || dt < 0 || !cpu::native_fmt (gt ,dt ,kH ,kFF ,f ,err )) { 78 std::printf ("%s: %s\n" ,pair .c_str (),err .empty () ?"unknown types" :err .c_str ()); 79continue ; 80 } 81ggml_quantize_init ((ggml_type )gt ); 82ggml_quantize_init ((ggml_type )dt ); 83 std::mt19937 rng (4321 + gt * 64 + dt ); 84 std::normal_distribution < float > nd (0.f ,0.02f ),xd (0.f ,1.f ); 85 std::vector < float > row ((size_t ) std::max (kH ,kFF )); 86const std::vector < float > imatrix ((size_t ) std::max (kH ,kFF ),1.0f ); 87auto quant = [& ](int type ,uint8_t * dst ,int64_t n ) { 88ggml_quantize_chunk ((ggml_type )type ,row .data (),dst ,0 ,1 ,n ,ggml_quantize_requires_imatrix ((ggml_type )type ) ?imatrix .data () :nullptr ); 89 }; 90 std::vector < uint8_t > blob (f .bytes + 64 ); 91 std::vector < std::vector < uint8_t >> xa (kMaxT , std::vector < uint8_t > (cpu::kNativeActBytes )),ha (kMaxT , std::vector < uint8_t > (cpu::kNativeHBytes )); 92 std::vector < std::vector < float >> ffv (kMaxT , std::vector < float > (kFF )),outv (kMaxT , std::vector < float > (kH )); 93const void * act [kMaxT ]; 94const void * hq [kMaxT ]; 95float * ff [kMaxT ]; 96float * out [kMaxT ]; 97for (int t = 0 ;t < kMaxT ;++ t ) {act [t ]= xa [(size_t )t ].data ();hq [t ]= ha [(size_t )t ].data ();ff [t ]= ffv [(size_t )t ].data ();out [t ]= outv [(size_t )t ].data (); } 98// a Q2_0 down (or gate/up) takes the pool's own path (pool.cpp): the activation as an ActQ and Strata's Q2_0 rows, not ggml's dot 99const bool q2_down = cpu::q2_native_kernels (f .d_type ),q2_gu = cpu::q2_native_kernels (f .gu_type ); 100 std::vector < cpu::ActQ > h2 (q2_down ?kMaxT :0 ),x2 (q2_gu ?kMaxT :0 ); 101const cpu::ActQ * a2 [kMaxT ]= {}; 102const cpu::ActQ * ax [kMaxT ]= {}; 103 std::vector < std::vector < float >> gv (q2_gu ?kMaxT :0 , std::vector < float > (kFF )),uv (q2_gu ?kMaxT :0 , std::vector < float > (kFF )); 104float * gp2 [kMaxT ]= {}; 105float * up2 [kMaxT ]= {}; 106uint64_t total = 1469598103934665603ull ; 107for (int e = 0 ;e < experts ;++ e ) { 108for (int64_t r = 0 ;r < 2 * kFF ;++ r ) { 109for (int64_t i = 0 ;i < kH ;++ i )row [(size_t )i ]= nd (rng ); 110quant (f .gu_type ,blob .data ()+ (size_t )r * f .gu_row ,kH ); 111 } 112for (int64_t r = 0 ;r < kH ;++ r ) { 113for (int64_t i = 0 ;i < kFF ;++ i )row [(size_t )i ]= nd (rng ); 114quant (f .d_type ,blob .data ()+ f .down_off + (size_t )r * f .d_row ,kFF ); 115 } 116for (int t = 0 ;t < kMaxT ;++ t ) { 117 std::vector < float > x (kH ),h (kFF ); 118for (auto & v :x )v = xd (rng ); 119for (auto & v :h )v = 0.3f * xd (rng ); 120if (q2_gu ) { cpu::act_quant_any (x .data (), (int )kH ,x2 [(size_t )t ]);ax [t ]= & x2 [(size_t )t ];gp2 [t ]= gv [(size_t )t ].data ();up2 [t ]= uv [(size_t )t ].data (); } 121else cpu::native_quant_act (f ,x .data (),xa [(size_t )t ].data ()); 122if (q2_down ) { cpu::act_quant_any (h .data (), (int )kFF ,h2 [(size_t )t ]);a2 [t ]= & h2 [(size_t )t ]; } 123else cpu::native_quant_h (f ,h .data (),ha [(size_t )t ].data ()); 124 } 125for (int nt = 1 ;nt <=nt_max && nt <=kMaxT ;++ nt ) { 126// whole range, then a range starting and ending off any 8/16/64 boundary 127const int ranges [2 ][4 ]= {{0 , (int )kFF ,0 , (int )kH }, {3 , (int )kFF - 5 ,7 , (int )kH - 9 }}; 128for (int inv = 0 ;inv < 2 ;++ inv ) { 129for (int rg = 0 ;rg < 2 ;++ rg ) { 130for (auto & v :ffv ) std::fill (v .begin (),v .end (),0.f ); 131for (auto & v :outv ) std::fill (v .begin (),v .end (),0.f ); 132if (q2_gu || q2_down ) { 133if (inv )continue ;// the Q2_0 rows have one arithmetic 134if (q2_gu ) { 135 cpu::q2_rows_any (blob .data (),f .gu_row , (int ) (kH /64 ),ax ,nt ,gp2 ,ranges [rg ][0 ],ranges [rg ][1 ]); 136 cpu::q2_rows_any (blob .data ()+ f .up_off ,f .gu_row , (int ) (kH /64 ),ax ,nt ,up2 ,ranges [rg ][0 ],ranges [rg ][1 ]); 137for (int t = 0 ;t < nt ;++ t ) 138for (int r = ranges [rg ][0 ];r < ranges [rg ][1 ];++ r ) 139ffv [(size_t )t ][(size_t )r ]= (gp2 [t ][r ] / (1.f + std::exp (- gp2 [t ][r ])))* up2 [t ][r ]; 140 }else { 141 cpu::native_gu_rows (f ,blob .data (),act ,nt ,ff ,ranges [rg ][0 ],ranges [rg ][1 ]); 142 } 143if (q2_down ) cpu::q2_rows_any (blob .data ()+ f .down_off ,f .d_row , (int ) (f .n_ff /64 ),a2 ,nt ,out ,ranges [rg ][2 ],ranges [rg ][3 ]); 144else cpu::native_down_rows (f ,blob .data (),hq ,nt ,out ,ranges [rg ][2 ],ranges [rg ][3 ]); 145 }else if (inv ) { 146 cpu::native_gu_rows_invariant (f ,blob .data (),act ,nt ,ff ,ranges [rg ][0 ],ranges [rg ][1 ]); 147 cpu::native_down_rows_invariant (f ,blob .data (),hq ,nt ,out ,ranges [rg ][2 ],ranges [rg ][3 ]); 148 }else { 149 cpu::native_gu_rows (f ,blob .data (),act ,nt ,ff ,ranges [rg ][0 ],ranges [rg ][1 ]); 150 cpu::native_down_rows (f ,blob .data (),hq ,nt ,out ,ranges [rg ][2 ],ranges [rg ][3 ]); 151 } 152uint64_t hg = 1469598103934665603ull ,hd = 1469598103934665603ull ; 153for (int t = 0 ;t < nt ;++ t ) { 154hg = fnv (hg ,ffv [(size_t )t ].data (),sizeof (float )* (size_t )kFF ); 155hd = fnv (hd ,outv [(size_t )t ].data (),sizeof (float )* (size_t )kH ); 156 } 157total = fnv (fnv (total ,& hg ,8 ),& hd ,8 ); 158 std::printf ("%s e%d nt%d %s range%d gu %016llx down %016llx\n" ,pair .c_str (),e ,nt ,inv ?"inv" :"def" ,rg , 159 (unsigned long long )hg , (unsigned long long )hd ); 160 } 161 } 162 } 163 } 164 std::printf ("%s TOTAL %016llx\n" ,pair .c_str (), (unsigned long long )total ); 165 } 166return 0 ; 167}