okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
073e2fe28
main
1// src/kernels/cpu/expert_multi_test.cpp - plan v0.3 P6: one expert, several tokens (CPU only, synthetic data). 2// 3// expert_multi_test bitwise check: s2_expert_vnni_multi vs s2_expert_vnni_q per token, n = 1..8 4// expert_multi_test --bench [E] time per expert for n = 1..5 over E synthetic experts (default 256 = 354 MB, 5// far beyond L3, so the blob comes from DRAM as it does in the engine); 1 thread 6// 7// The bench measures the speculation model's `extra_use_cost`: the CPU time of each extra token routed to an 8// expert, as a fraction of reading the expert once (tools/spec_economics.py assumes 0.2). 9#include "strata/kernels/cpu/expert.hpp" 10 11#include <chrono> 12#include <cstdio> 13#include <cstdlib> 14#include <cstring> 15#include <random> 16#include <vector> 17 18namespace c= strata::kernels::cpu; 19 20namespace { 21// the pool's path: the gate/up rows, the second activation quantization, the down rows (what the engine's expert 22// jobs run), for one expert and n tokens 23void rows_path (const uint8_t * blob ,const c::ActQ * const * a1 ,int n ,float * const * out , c::ExpertScratchMulti & ws ) { 24float * ffp [c::MAXT ]; 25const c::ActQ * a2 [c::MAXT ]; 26for (int t = 0 ;t < n ;++ t )ffp [t ]= ws .ff [t ]; 27 c::s2_expert_gu_rows_multi (blob ,a1 ,n ,ffp ,0 , c::FF ); 28for (int t = 0 ;t < n ;++ t ) { 29 c::act_quant_q8_1 (ws .ff [t ], c::FF ,ws .a2 [t ]); 30a2 [t ]= & ws .a2 [t ]; 31 } 32 c::s2_expert_down_rows_multi (blob ,a2 ,n ,out ,0 , c::H ); 33} 34 35void make_blob (uint8_t * b , std::mt19937 & rng ) { 36for (size_t i = 0 ;i < c::O_GU_SCALES ;++ i )b [i ]= (uint8_t )rng ();// codes 37for (size_t i = c::O_GU_SCALES ;i < c::BLOB ;i += 2 ) {// fp16 scales ~0.004-0.016 38const uint16_t h = (uint16_t ) (0x1C00 + rng () %0x0800 ); 39 std::memcpy (b + i ,& h ,2 ); 40 } 41} 42 43double now_ms () { 44return std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ().time_since_epoch ()).count (); 45} 46}// namespace 47 48int main (int argc ,char ** argv ) { 49if (const c::CpuFeatures feat = c::cpu_features (); !feat .usable ()) {// the AVX-512 kernel: nothing to test here 50 std::printf ("expert_multi_test: CPU lacks %s: SKIPPED\n" ,feat .reason ()); 51return 77 ; 52 } 53 std::mt19937 rng (9 ); 54const bool bench = argc > 1 && std::strcmp (argv [1 ],"--bench" )== 0 ; 55const int E = bench ? (argc > 2 ? std::atoi (argv [2 ]) :256 ) :4 ; 56 std::vector < uint8_t > blobs ((size_t )E * c::BLOB ); 57for (int e = 0 ;e < E ;++ e )make_blob (& blobs [(size_t )e * c::BLOB ],rng ); 58 std::normal_distribution < float > nd (0.f ,1.f ); 59static c::ActQ acts [c::MAXT ]; 60static float xs [c::MAXT ][c::H ]; 61for (int t = 0 ;t < c::MAXT ;++ t ) { 62for (float & v :xs [t ])v = nd (rng ); 63 c::act_quant_q8_1 (xs [t ], c::H ,acts [t ]); 64 } 65const c::ActQ * a1 [c::MAXT ]; 66for (int t = 0 ;t < c::MAXT ;++ t )a1 [t ]= & acts [t ]; 67static float out_multi [c::MAXT ][c::H ],out_single [c::H ]; 68float * outs [c::MAXT ]; 69for (int t = 0 ;t < c::MAXT ;++ t )outs [t ]= out_multi [t ]; 70static c::ExpertScratchMulti wsm ; 71static c::ExpertScratch ws ; 72 73if (!bench ) { 74int fail = 0 ; 75for (int e = 0 ;e < E ;++ e ) { 76const uint8_t * blob = & blobs [(size_t )e * c::BLOB ]; 77for (int n = 1 ;n <= c::MAXT ;++ n ) { 78 c::s2_expert_vnni_multi (blob ,a1 ,n ,outs ,wsm ); 79for (int t = 0 ;t < n ;++ t ) { 80 c::s2_expert_vnni_q (blob ,acts [t ],out_single ,ws ); 81if (std::memcmp (out_single ,out_multi [t ],sizeof out_single )!= 0 ) { 82 std::fprintf (stderr ,"FAIL expert %d n=%d token %d differs\n" ,e ,n ,t ); 83++ fail ; 84 } 85 } 86static float out_rows [c::MAXT ][c::H ]; 87float * orow [c::MAXT ]; 88for (int t = 0 ;t < c::MAXT ;++ t )orow [t ]= out_rows [t ]; 89rows_path (blob ,a1 ,n ,orow ,wsm ); 90for (int t = 0 ;t < n ;++ t ) 91if (std::memcmp (out_rows [t ],out_multi [t ],sizeof out_rows [t ])!= 0 ) { 92 std::fprintf (stderr ,"FAIL expert %d n=%d token %d: rows path differs from the multi kernel\n" ,e ,n ,t ); 93++ fail ; 94 } 95 } 96 } 97 std::printf ("expert_multi_test: %s\n" ,fail ?"FAILED" :"OK (every token bitwise equal to the single-token kernel)" ); 98return fail ?1 :0 ; 99 } 100 101 std::printf ("%d experts x %.2f MB, one thread\n" ,E , c::BLOB /1e6 ); 102double base = 0 ; 103for (int n = 1 ;n <=5 ;++ n ) { 104 c::s2_expert_vnni_multi (& blobs [0 ],a1 ,n ,outs ,wsm );// warm the code path 105const double t0 = now_ms (); 106for (int e = 0 ;e < E ;++ e ) c::s2_expert_vnni_multi (& blobs [(size_t )e * c::BLOB ],a1 ,n ,outs ,wsm ); 107const double us = 1000.0 * (now_ms ()- t0 ) /E ; 108if (n == 1 )base = us ; 109 std::printf ("tokens per expert %d: %7.1f us per expert (%.2fx one token; extra token = %.2f of one read), %.1f GB/s\n" , 110n ,us ,us /base ,n > 1 ? (us /base - 1.0 ) / (n - 1 ) :0.0 , c::BLOB / (us * 1e3 )); 111rows_path (& blobs [0 ],a1 ,n ,outs ,wsm ); 112const double r0 = now_ms (); 113for (int e = 0 ;e < E ;++ e )rows_path (& blobs [(size_t )e * c::BLOB ],a1 ,n ,outs ,wsm ); 114const double rus = 1000.0 * (now_ms ()- r0 ) /E ; 115 std::printf (" rows path (the pool's): %7.1f us per expert, %.1f GB/s\n" ,rus , c::BLOB / (rus * 1e3 )); 116 } 117return 0 ; 118}