okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/kernels/latent_cpu_verify.cpp - CPU latent expert path, per-stage vs the test bundle's 2// Python float32 reference. Isolates D (Q8_0 matvec), A (native NVFP4 gu+down), U (Q8_0 matvec). 3// 4// latent_cpu_verify <layerL_test.bin> <latent_a_pack> <latent_dense_q8.bin> <layer> 5// 6// Each stage is fed the *reference* output of the previous stage so the errors don't compound: 7// xl = D . x (compare ref_xl) 8// yl = A(xl := ref_xl) (compare ref_yl) -- A = swiglu(A_gu . xl) -> A_dn . ff 9// y = U(yl := ref_yl) (compare ref_y) 10// out = combine (compare ref_out, from my own xl/yl/y) 11#include "strata/core/latent_dense.hpp" 12#include "strata/core/expert_source.hpp" 13#include "strata/kernels/cpu/expert_layout.hpp" 14#include "strata/kernels/cpu/native_expert.hpp" 15 16#include <cmath> 17#include <cstdint> 18#include <cstdio> 19#include <cstdlib> 20#include <cstring> 21#include <fstream> 22#include <string> 23#include <vector> 24 25namespace cpu= strata::kernels::cpu; 26 27// ---- copied verbatim from expert_source.cpp (latent_cpu_run's helpers) ---- 28static inline float latent_f16_to_f32 (uint16_t h ) { 29const uint32_t sign = (uint32_t )(h >>15 )& 1u ; 30uint32_t e = (h >>10 )& 0x1Fu ,m = h & 0x3FFu ,f ; 31if (e == 0 ) { 32if (m == 0 )f = sign <<31 ; 33else {e = 127 - 15 + 1 ;while (!(m & 0x400u )) {m <<=1 ;-- e ; }m &=0x3FFu ;f = (sign <<31 ) | (e <<23 ) | (m <<13 ); } 34 }else if (e == 31 )f = (sign <<31 ) |0x7F800000u | (m <<13 ); 35else f = (sign <<31 ) | ((e - 15 + 127 ) <<23 ) | (m <<13 ); 36float out ; std::memcpy (& out ,& f ,4 );return out ; 37} 38static void q8_0_matvec_f32 (const uint8_t * W ,size_t row_bytes ,int nrows ,int ncols ,const float * x ,float * y ) { 39const int nb = ncols /32 ; 40for (int r = 0 ;r < nrows ;++ r ) { 41const uint8_t * row = W + (size_t )r * row_bytes ; 42float acc = 0.f ; 43for (int b = 0 ;b < nb ;++ b ) { 44const uint8_t * blk = row + (size_t )b * 34 ; 45const float d = latent_f16_to_f32 ((uint16_t )(blk [0 ] | (blk [1 ] <<8 ))); 46const int8_t * q = (const int8_t * )(blk + 2 ); 47const float * xb = x + (size_t )b * 32 ; 48float s = 0.f ; 49for (int j = 0 ;j < 32 ;++ j )s += (float )q [j ]* xb [j ]; 50acc += d * s ; 51 } 52y [r ]= acc ; 53 } 54} 55// ---- end copied ---- 56 57namespace { 58template < typename T > 59std::vector < T > read_n (std::ifstream & f ,size_t n ,const char * what ) { 60 std::vector < T > v (n ); 61if (!f .read ((char * )v .data (), (std::streamsize ) (n * sizeof (T )))) { 62 std::fprintf (stderr ,"short read: %s\n" ,what ); 63 std::exit (1 ); 64 } 65return v ; 66} 67double rel_l2 (const float * got ,const float * ref ,size_t n ) { 68double num = 0 ,den = 0 ; 69for (size_t i = 0 ;i < n ;++ i ) { 70const double d = (double )got [i ]- (double )ref [i ]; 71num += d * d ;den += (double )ref [i ]* (double )ref [i ]; 72 } 73return std::sqrt (num / (den + 1e-30 )); 74} 75float max_abs (const float * got ,const float * ref ,size_t n ) { 76float m = 0 ; 77for (size_t i = 0 ;i < n ;++ i )m = std::fmax (m , std::fabs (got [i ]- ref [i ])); 78return m ; 79} 80void report (const char * name ,const std::vector < float >& got ,const std::vector < float >& ref ) { 81 std::printf (" %-4s relL2 %.4f%% max_abs %.6g\n" ,name , 82100 * rel_l2 (got .data (),ref .data (),got .size ()), 83max_abs (got .data (),ref .data (),got .size ())); 84} 85}// namespace 86 87int main (int argc ,char ** argv ) { 88if (argc < 5 ) { std::fprintf (stderr ,"usage: %s test.bin latent_a_pack dense_q8.bin layer\n" ,argv [0 ]);return 2 ; } 89const int layer = std::atoi (argv [4 ]); 90 std::ifstream f (argv [1 ], std::ios::binary ); 91if (!f ) { std::fprintf (stderr ,"cannot open %s\n" ,argv [1 ]);return 2 ; } 92char magic [4 ]; 93f .read (magic ,4 ); 94if (std::memcmp (magic ,"LAT0" ,4 )!= 0 ) { std::fprintf (stderr ,"bad magic\n" );return 2 ; } 95int32_t hdr [7 ]; 96if (!f .read ((char * )hdr ,sizeof hdr ))return 2 ; 97const int T = hdr [0 ],K = hdr [1 ],G = hdr [2 ],E = hdr [3 ],H = hdr [4 ],ELL = hdr [5 ],I = hdr [6 ]; 98 std::printf ("layer=%d T=%d K=%d G=%d E=%d H=%d ELL=%d I=%d\n" ,layer ,T ,K ,G ,E ,H ,ELL ,I ); 99if (T * K > 128 ) { std::fprintf (stderr ,"cap too large\n" );return 2 ; } 100 101const auto x = read_n < float > (f , (size_t )T * H ,"x" ); 102const auto topk = read_n < int32_t > (f , (size_t )T * K ,"topk" ); 103const auto w = read_n < float > (f , (size_t )T * K ,"w" ); 104const auto labels = read_n < int32_t > (f , (size_t )E ,"labels" ); 105const size_t drow = (size_t )H /32 * 34 ,urow = (size_t )ELL /32 * 34 ; 106f .seekg ((std::streamoff ) ((size_t )G * ELL * drow + (size_t )G * H * urow ), std::ios::cur ); 107const auto ref_xl = read_n < float > (f , (size_t )T * G * ELL ,"ref_xl" ); 108const auto ref_yl = read_n < float > (f , (size_t )T * K * ELL ,"ref_yl" ); 109const auto ref_y = read_n < float > (f , (size_t )T * K * H ,"ref_y" ); 110const auto ref_out = read_n < float > (f , (size_t )T * H ,"ref_out" ); 111 112 std::string err ; 113if (!cpu::expert_layout_load (argv [2 ],48 ,512 ,err )) { std::fprintf (stderr ,"layout: %s\n" ,err .c_str ());return 2 ; } 114 strata::core::FileExpertSource src ; 115if (!src .open (argv [2 ],48 ,512 ,err )) { std::fprintf (stderr ,"source: %s\n" ,err .c_str ());return 2 ; } 116 strata::core::LatentDense dense ; 117if (!dense .open (argv [3 ],48 ,G ,H ,ELL ,err )) { std::fprintf (stderr ,"dense: %s\n" ,err .c_str ());return 2 ; } 118 119const cpu::NativeFmt & nf = cpu::expert_layout ().fmt [(size_t )layer ]; 120 std::printf ("NativeFmt: gu_type=%d d_type=%d n_embd=%lld n_ff=%lld gu_row=%zu up_off=%zu down_off=%zu act_bytes=%zu h_bytes=%zu tail_off=%zu\n" , 121nf .gu_type ,nf .d_type , (long long )nf .n_embd , (long long )nf .n_ff , 122nf .gu_row ,nf .up_off ,nf .down_off ,nf .act_bytes ,nf .h_bytes ,nf .tail_off ); 123 124const uint8_t * D = dense .d (layer ); 125const uint8_t * U = dense .u (layer ); 126 127// ---- D stage: xl = D . x, compare ref_xl ---- 128 std::vector < float > my_xl ((size_t )T * G * ELL ); 129for (int t = 0 ;t < T ;++ t ) 130for (int g = 0 ;g < G ;++ g ) 131q8_0_matvec_f32 (D + (size_t )g * ELL * drow ,drow ,ELL ,H ,x .data ()+ (size_t )t * H , 132my_xl .data ()+ ((size_t )t * G + g )* ELL ); 133report ("xl" ,my_xl ,ref_xl ); 134 135// ---- A stage: yl = A(ref_xl), compare ref_yl (isolated: feed ref_xl, not my_xl) ---- 136 std::vector < float > my_yl ((size_t )T * K * ELL ); 137 { 138 std::vector < uint8_t > act ((size_t ) cpu::kNativeActBytes ),hq ((size_t ) cpu::kNativeHBytes ); 139 std::vector < float > ff ((size_t )I ); 140const void * actp = act .data ();float * ffp = ff .data (); 141const void * hqp = hq .data ();float * hp = nullptr ; 142for (int i = 0 ;i < T * K ;++ i ) { 143const int e = topk [i ],t = i /K ,g = labels [(size_t )e ]; 144const uint8_t * blob = src .blob (layer ,e ); 145if (blob == nullptr ) { std::fprintf (stderr ,"no blob for e=%d\n" ,e );return 2 ; } 146const float * xl_tg = ref_xl .data ()+ ((size_t )t * G + g )* ELL ; 147 cpu::native_quant_act (nf ,xl_tg ,act .data ()); 148 cpu::native_gu_rows (nf ,blob ,& actp ,1 ,& ffp ,0 ,I ); 149 cpu::native_quant_h (nf ,ff .data (),hq .data ()); 150hp = my_yl .data ()+ (size_t )i * ELL ; 151 cpu::native_down_rows (nf ,blob ,& hqp ,1 ,& hp ,0 ,ELL ); 152 } 153 } 154report ("yl" ,my_yl ,ref_yl ); 155 156// ---- U stage: y = U(ref_yl), compare ref_y (isolated: feed ref_yl, not my_yl) ---- 157 std::vector < float > my_y ((size_t )T * K * H ); 158for (int i = 0 ;i < T * K ;++ i ) { 159const int e = topk [i ],g = labels [(size_t )e ]; 160q8_0_matvec_f32 (U + (size_t )g * H * urow ,urow ,H ,ELL ,ref_yl .data ()+ (size_t )i * ELL , 161my_y .data ()+ (size_t )i * H ); 162 } 163report ("y" ,my_y ,ref_y ); 164 165// ---- combine (using my own xl/yl/y): out[t] = sum_k w[r] * y[r] ---- 166 std::vector < float > my_out ((size_t )T * H ,0.f ); 167for (int i = 0 ;i < T * K ;++ i ) { 168const int t = i /K ; 169const float wi = w [(size_t )i ]; 170for (int c = 0 ;c < H ;++ c )my_out [(size_t )t * H + c ]+= wi * my_y [(size_t )i * H + c ]; 171 } 172report ("out" ,my_out ,ref_out ); 173 174 std::printf ("DONE\n" ); 175return 0 ; 176}