okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// A token's native expert result must not depend on how many other tokens share its group. 2// Compare the invariant entry points with the production singleton dispatch, including SwiGLU. 3// Synthetic packed blocks only: no GPU, model download or private fixtures. 4#include "ggml.h" 5#include "strata/kernels/cpu/native_expert.hpp" 6#include "strata/kernels/cpu/tq_cpu.hpp" 7#include <cmath> 8#include <cstdio> 9#include <cstring> 10#include <random> 11#include <vector> 12#include <utility> 13 14namespace cpu= strata::kernels::cpu; 15namespace { 16constexpr int width = 8 ,hidden = 512 ; 17size_t checked = 0 ; 18bool equal (const std::vector < float > & actual ,const std::vector < float > & expected ) { 19for (size_t i = 0 ;i < actual .size ();++ i ) { 20++ checked ; 21if (!std::isfinite (actual [i ])|| std::memcmp (& actual [i ],& expected [i ],sizeof (float ))) 22return false; 23 } 24return true; 25} 26 27bool check_format (int gu ,int down ,int ff ,bool folded = false) { 28 cpu::NativeFmt fmt ; 29 std::string error ; 30if (!cpu::native_fmt (gu ,down ,hidden ,ff ,fmt ,error )) { 31 std::fprintf (stderr ,"%s\n" ,error .c_str ()); 32return false; 33 } 34 std::vector < float > gu_signs (hidden ),down_signs (ff ); 35if (folded ) { 36for (int i = 0 ;i < hidden ;++ i )gu_signs [i ]= i %3 ?1.f :-1.f ; 37for (int i = 0 ;i < ff ;++ i )down_signs [i ]= i %5 ?1.f :-1.f ; 38fmt .had_block = 128 ; 39fmt .had_gu = fmt .had_d = true; 40fmt .gu_signs = gu_signs .data (); 41fmt .d_signs = down_signs .data (); 42 } 43 std::mt19937 rng (20261016 + gu * 100 + down + ff ); 44 std::vector < uint8_t > blob (fmt .bytes ); 45for (auto & byte :blob ) 46byte = static_cast < uint8_t > (rng ()); 47// Use finite block scales and random packed values. NVFP4 has four UE4M3 sub-block scales. 48const uint16_t scale = 0x2400 ;// 2^-6; exclude NaN/Inf scales from the random bytes. 49auto scales = [& ](size_t first ,size_t last ,int type ) { 50const size_t block = cpu::tq_supported (type ) ? cpu::tq_block_bytes (type ) :ggml_type_size (static_cast < ggml_type > (type )); 51for (size_t at = first ;at < last ;at += block ) { 52if (type == cpu::kNvfp4Type ) std::memset (blob .data ()+ at ,0x28 ,4 );// finite UE4M3 sub-block scales 53else { 54 std::memcpy (blob .data ()+ at ,& scale ,sizeof (scale )); 55if (type == 12 || type == 7 ) std::memcpy (blob .data ()+ at + 2 ,& scale ,sizeof (scale )); 56 } 57 } 58 }; 59scales (0 ,fmt .down_off ,gu ); 60scales (fmt .down_off ,fmt .tail_off ?fmt .tail_off :blob .size (),down ); 61if (fmt .tail_off ) { 62const float tail [4 ]= {gu == cpu::kNvfp4Type ?.5f :1.f ,gu == cpu::kNvfp4Type ?.75f :1.f , 63down == cpu::kNvfp4Type ?1.25f :1.f ,0.f }; 64 std::memcpy (blob .data ()+ fmt .tail_off ,tail ,sizeof (tail )); 65 } 66 67 std::vector < std::vector < uint8_t >> act (width , std::vector < uint8_t > (fmt .act_bytes )); 68 std::vector < std::vector < uint8_t >> hq (width , std::vector < uint8_t > (fmt .h_bytes )); 69 std::vector < std::vector < float >> gates (width , std::vector < float > (ff )),downs (width , std::vector < float > (hidden )); 70auto solo_gates = gates ,solo_downs = downs ; 71const void * ap [width ],* hp [width ]; 72float * gp [width ],* dp [width ],* sg [width ],* sd [width ]; 73for (int t = 0 ;t < width ;++ t ) { 74ap [t ]= act [t ].data (); 75hp [t ]= hq [t ].data (); 76gp [t ]= gates [t ].data (); 77dp [t ]= downs [t ].data (); 78sg [t ]= solo_gates [t ].data (); 79sd [t ]= solo_downs [t ].data (); 80 } 81for (float amplitude : {0.f ,.01f ,.3f ,3.f }) { 82 std::vector < float > x (hidden ),h (ff ); 83for (int t = 0 ;t < width ;++ t ) { 84for (auto & v :x ) 85v = (static_cast < int > (rng () %2049 )- 1024 )* amplitude /1024.f ; 86for (auto & v :h ) 87v = (static_cast < int > (rng () %2049 )- 1024 )* amplitude /1024.f ; 88 cpu::native_quant_act (fmt ,x .data (),act [t ].data ()); 89 cpu::native_quant_h (fmt ,h .data (),hq [t ].data ()); 90 cpu::native_gu_rows (fmt ,blob .data (),ap + t ,1 ,sg + t ,0 ,ff ); 91 cpu::native_down_rows (fmt ,blob .data (),hp + t ,1 ,sd + t ,0 ,hidden ); 92 } 93for (int nt = 1 ;nt <=width ;++ nt ) { 94 cpu::native_gu_rows_invariant (fmt ,blob .data (),ap ,nt ,gp ,0 ,ff ); 95 cpu::native_down_rows_invariant (fmt ,blob .data (),hp ,nt ,dp ,0 ,hidden ); 96for (int t = 0 ;t < nt ;++ t ) { 97if (!equal (gates [t ],solo_gates [t ])|| !equal (downs [t ],solo_downs [t ])) { 98 std::fprintf (stderr ,"native arithmetic mismatch: gu=%d down=%d ff=%d nt=%d token=%d scale=%g\n" , 99gu ,down ,ff ,nt ,t ,amplitude ); 100return false; 101 } 102 } 103 } 104 } 105return true; 106} 107}// namespace 108 109int main () { 110// 16/17 and IQ4_XS exercise the per-token legacy fallback as well as optimized 18/21/22. 111for (int gu : {16 ,17 ,18 ,21 ,22 ,23 }) { 112if (!check_format (gu ,20 ,64 )|| !check_format (gu ,20 ,96 )|| !check_format (gu ,42 ,64 )) 113return 1 ; 114 } 115// NVFP4's kernels already preserve singleton arithmetic. Mixed formats also carry scale tails. 116for (auto pair : {std::pair {40 ,40 }, {40 ,20 }, {40 ,42 }, {22 ,40 }, {23 ,40 }}) 117if (!check_format (pair .first ,pair .second ,64 ))return 1 ; 118// STRATA_KQ256 selects grouped Q4_K/Q5_1/Q8_0 kernels only when their target arithmetic matches. 119if (!check_format (12 ,7 ,64 )|| !check_format (12 ,8 ,64 ))return 1 ; 120// 0.2 adds Trellis formats and folded expert inputs. Exercise both their 121// production singleton dispatch and every grouped width through the new API. 122for (int gu : {144 ,145 ,146 }) 123for (int down : {20 ,144 ,145 ,146 }) 124for (bool folded : {false, true}) 125if (!check_format (gu ,down ,256 ,folded ))return 1 ; 126if (!check_format (22 ,20 ,256 , true))return 1 ; 127 std::printf ("native_arithmetic_parity: %zu finite bitwise comparisons passed\n" ,checked ); 128}