okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f2b41fd29
main
1// src/prefill/mmq_nvfp4_parity.cpp - the prompt path's NVFP4 expert products (MMQ) against an exact reference. 2// 3// STRATA_PREFILL_NVFP4=w4a8|w4a4 mmq_nvfp4_parity pack/experts.bin [rows ...] 4// STRATA_PREFILL_NVFP4=w4a8|w4a4 mmq_nvfp4_parity pack/experts.bin --group 5// STRATA_PREFILL_NVFP4=w4a8|w4a4 mmq_nvfp4_parity pack/experts.bin --layers [expert] 6// STRATA_PREFILL_NVFP4=w4a8|w4a4 mmq_nvfp4_parity pack/experts.bin --real moe_input.bin layer 7// 8// --group: the prompt path's launch shape - 20 experts of layer 0 with 0..60 rows each, one activation array in 9// expert order, gate/up in two launches (experts 0-15, then 16-19 through the same absolute bounds, 16 in), and the 10// second group's down from its own rows through relative bounds. 11// --layers: one expert (default 7) of every one of the 48 layers, 64 rows - scales differ by layer. 12// --real: a layer's MoE input from a prompt (STRATA_DUMP_MOE_INPUT) through its 12 most-routed experts, gate/up on 13// the real rows and down on the hidden they produce; also an FP16-activation product (the fp16 path) and the rows' 14// outlier ratio (max |x| / rms x). 15// 16// One real expert (layer 0, expert 0): gate/up [1280, 2560] and down [2560, 640], as the prompt path gathers them. 17// Activations are random rows; the reference dequantizes the weights with ggml's own NVFP4 decode and sums in double. 18// Reported per product: relative Frobenius error and the worst element relative to the row's largest output. 19// 8-bit activations land near 0.5%; FP4 activations near 10% - the difference between the two modes. 20#include "strata/prefill/mmq_ids_margin.hpp" 21#include "strata/prefill/moe_mmq.hpp" 22 23#include "ggml.h" 24 25#include <cuda_runtime.h> 26#include <immintrin.h> 27 28#include <algorithm> 29#include <cmath> 30#include <cstdio> 31#include <cstdlib> 32#include <cstring> 33#include <fstream> 34#include <string> 35#include <random> 36#include <vector> 37 38namespace mmq= strata::prefill::mmq; 39 40namespace { 41 42void ck (cudaError_t e ,const char * what ) { 43if (e != cudaSuccess ) { std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); std::exit (1 ); } 44} 45 46// one product: x [T, cols] times w [rows, cols]^T 47float half_round (float v ) {return _mm_cvtss_f32 (_mm_cvtph_ps (_mm_cvtps_ph (_mm_set_ss (v ),0 ))); } 48 49int product (const uint8_t * w_host ,int64_t rows ,int64_t cols ,int T ,const char * what , std::mt19937 & rng , 50const std::vector < float >* given = nullptr ,double * rel_out = nullptr ,double * rel16_out = nullptr , 51 std::vector < double >* ref_out = nullptr ) { 52const size_t wbytes = mmq::matrix_bytes (GGML_TYPE_NVFP4 ,rows ,cols ); 53 std::vector < float > wf ((size_t )rows * cols ); 54ggml_get_type_traits (GGML_TYPE_NVFP4 )-> to_float (w_host ,wf .data (),rows * cols ); 55 std::vector < float > x ((size_t )T * cols ); 56if (given ) { 57x = * given ; 58 }else { 59 std::normal_distribution < float > nd (0.f ,1.f ); 60 std::uniform_real_distribution < float > mag (0.1f ,10.f ); 61for (int t = 0 ;t < T ;++ t ) { 62const float m = mag (rng );// rows of different magnitude, like a residual 63for (int64_t k = 0 ;k < cols ;++ k )x [(size_t )t * cols + k ]= nd (rng )* m ; 64 } 65 } 66 std::vector < double > ref ((size_t )T * rows ); 67for (int t = 0 ;t < T ;++ t ) 68for (int64_t r = 0 ;r < rows ;++ r ) { 69double s = 0 ; 70const float * a = x .data ()+ (size_t )t * cols ; 71const float * b = wf .data ()+ (size_t )r * cols ; 72for (int64_t k = 0 ;k < cols ;++ k )s += (double )a [k ]* b [k ]; 73ref [(size_t )t * rows + r ]= s ; 74 } 75 76uint8_t * dw = nullptr ;float * dx = nullptr ;void * dq = nullptr ;float * dys = nullptr ;float * dd = nullptr ; 77int32_t * dids = nullptr ;int32_t * dbounds = nullptr ; 78ck (cudaMalloc (& dw ,wbytes + 4096 ),"w" ); 79ck (cudaMemset (dw ,0 ,wbytes + 4096 ),"w" ); 80ck (cudaMemcpy (dw ,w_host ,wbytes ,cudaMemcpyHostToDevice ),"w" ); 81ck (cudaMalloc (& dx ,x .size ()* sizeof (float )),"x" ); 82ck (cudaMemcpy (dx ,x .data (),x .size ()* sizeof (float ),cudaMemcpyHostToDevice ),"x" ); 83ck (cudaMalloc (& dq , mmq::q8_bytes (T ,cols )),"xq" ); 84ck (cudaMalloc (& dys ,T * sizeof (float )),"yscale" ); 85ck (cudaMalloc (& dd , (size_t )T * rows * sizeof (float )),"dst" ); 86const size_t ids_rows = (size_t ) mmq::mmq_ids_table_rows ((uint64_t )T , (uint64_t )T );// (mul_mat_q reads the row table past its last row: mmq_ids_margin.hpp) 87ck (cudaMalloc (& dids ,ids_rows * sizeof (int32_t )),"ids" ); 88ck (cudaMalloc (& dbounds ,2 * sizeof (int32_t )),"bounds" ); 89const int32_t bounds [2 ]= {0 ,T }; 90ck (cudaMemcpy (dbounds ,bounds ,sizeof bounds ,cudaMemcpyHostToDevice ),"bounds" ); 91 mmq::iota (dids , (int64_t )ids_rows ,nullptr ); 92 mmq::quantize (dx ,nullptr ,dq ,GGML_TYPE_NVFP4 ,cols ,cols ,T ,nullptr ,dys ); 93 mmq::Context ctx ; 94 mmq::Product p ; 95p .w = dw ;p .type = GGML_TYPE_NVFP4 ;p .w_rows = rows ;p .w_cols = cols ;p .expert_bytes = wbytes ;p .n = 1 ; 96p .xq = dq ;p .bounds = dbounds ;p .ids = dids ;p .total_rows = T ;p .max_rows = T ;p .dst = dd ;p .ld_dst = rows ; 97p .y_scale = mmq::fp4_activations (GGML_TYPE_NVFP4 ) ?dys :nullptr ; 98ctx .run (p ,nullptr ); 99ck (cudaDeviceSynchronize (),"run" ); 100 std::vector < float > got ((size_t )T * rows ); 101ck (cudaMemcpy (got .data (),dd ,got .size ()* sizeof (float ),cudaMemcpyDeviceToHost ),"dst" ); 102cudaFree (dw );cudaFree (dx );cudaFree (dq );cudaFree (dys );cudaFree (dd );cudaFree (dids );cudaFree (dbounds ); 103 104double num = 0 ,den = 0 ,worst = 0 ; 105for (int t = 0 ;t < T ;++ t ) { 106double rmax = 0 ; 107for (int64_t r = 0 ;r < rows ;++ r )rmax = std::fmax (rmax , std::fabs (ref [(size_t )t * rows + r ])); 108for (int64_t r = 0 ;r < rows ;++ r ) { 109const double e = (double )got [(size_t )t * rows + r ]- ref [(size_t )t * rows + r ]; 110num += e * e ; 111den += ref [(size_t )t * rows + r ]* ref [(size_t )t * rows + r ]; 112worst = std::fmax (worst , std::fabs (e ) / (rmax > 0 ?rmax :1 )); 113 } 114 } 115const double rel = std::sqrt (num /den ); 116if (rel_out )* rel_out = rel ; 117if (ref_out )* ref_out = ref ; 118if (rel16_out ) {// the fp16 path: activations rounded to FP16 119double n16 = 0 ; 120for (int t = 0 ;t < T ;++ t ) { 121 std::vector < float > xh (cols ); 122for (int64_t k = 0 ;k < cols ;++ k )xh [k ]= half_round (x [(size_t )t * cols + k ]); 123for (int64_t r = 0 ;r < rows ;++ r ) { 124double s = 0 ; 125for (int64_t k = 0 ;k < cols ;++ k )s += (double )xh [k ]* wf [(size_t )r * cols + k ]; 126const double e = s - ref [(size_t )t * rows + r ]; 127n16 += e * e ; 128 } 129 } 130* rel16_out = std::sqrt (n16 /den ); 131return rel > 0.2 ?1 :0 ; 132 } 133 std::printf ("%-8s T %4d [%lld x %lld] rel. error %.4f%% worst %.4f%% of the row max%s\n" ,what ,T , 134 (long long )rows , (long long )cols ,100 * rel ,100 * worst ,rel > 0.05 ?" <-- FP4-level or wrong" :"" ); 135return rel > 0.2 ?1 :0 ; 136} 137 138double dot_ref (const float * a ,const float * b ,int64_t n ) { 139double s = 0 ; 140for (int64_t k = 0 ;k < n ;++ k )s += (double )a [k ]* b [k ]; 141return s ; 142} 143 144struct Err { 145double num = 0 ,den = 0 ; 146void add (double got ,double ref ) {num += (got - ref )* (got - ref );den += ref * ref ; } 147double rel ()const {return den > 0 ? std::sqrt (num /den ) :0 ; } 148}; 149 150int group (const char * path ) { 151constexpr int64_t N = 2560 ,FF = 640 ; 152constexpr int E = 20 ; 153constexpr size_t STRIDE = 2764816 ;// one layer-0 blob in experts.bin 154const size_t gub = mmq::matrix_bytes (GGML_TYPE_NVFP4 ,2 * FF ,N ),db = mmq::matrix_bytes (GGML_TYPE_NVFP4 ,N ,FF ); 155 std::vector < uint8_t > all ((size_t )E * STRIDE ); 156 std::ifstream f (path , std::ios::binary ); 157if (!f .read ((char * )all .data (), (std::streamsize )all .size ())) { std::fprintf (stderr ,"cannot read %s\n" ,path );return 2 ; } 158 std::vector < uint8_t > G ((size_t )E * gub + 4096 ,0 ),D ((size_t )E * db + 4096 ,0 ); 159for (int e = 0 ;e < E ;++ e ) { 160 std::memcpy (G .data ()+ e * gub ,all .data ()+ e * STRIDE ,gub ); 161 std::memcpy (D .data ()+ e * db ,all .data ()+ e * STRIDE + gub ,db ); 162 } 163 std::mt19937 rng (5 ); 164 std::uniform_int_distribution < int > cntd (0 ,60 ); 165 std::vector < int32_t > bounds (E + 1 ,0 ); 166int maxA = 0 ,maxB = 0 ; 167for (int e = 0 ;e < E ;++ e ) { 168const int c = (e %7 == 3 ) ?0 :cntd (rng ); 169bounds [e + 1 ]= bounds [e ]+ c ; 170if (e < 16 )maxA = std::max (maxA ,c );else maxB = std::max (maxB ,c ); 171 } 172const int T = bounds [E ]; 173 std::normal_distribution < float > nd (0.f ,1.f ); 174 std::uniform_real_distribution < float > mag (0.1f ,10.f ); 175 std::vector < float > x ((size_t )T * N ); 176for (int t = 0 ;t < T ;++ t ) {const float m = mag (rng );for (int64_t k = 0 ;k < N ;++ k )x [(size_t )t * N + k ]= nd (rng )* m ; } 177// group B's down input: its own rows of FF values 178const int rB = bounds [16 ],nB = T - rB ; 179 std::vector < float > h ((size_t ) std::max (nB ,1 )* FF ); 180for (auto & v :h )v = nd (rng ); 181 std::vector < int32_t > relB (5 ); 182for (int i = 0 ;i <=4 ;++ i )relB [i ]= bounds [16 + i ]- rB ; 183 184uint8_t * dG ,* dD ;float * dx ,* dh ,* dgu ,* ddn ,* dys ,* dhs ;void * dxq ,* dhq ;int32_t * dids ,* dbounds ,* drel ; 185ck (cudaMalloc (& dG ,G .size ()),"G" );ck (cudaMemcpy (dG ,G .data (),G .size (),cudaMemcpyHostToDevice ),"G" ); 186ck (cudaMalloc (& dD ,D .size ()),"D" );ck (cudaMemcpy (dD ,D .data (),D .size (),cudaMemcpyHostToDevice ),"D" ); 187ck (cudaMalloc (& dx ,x .size ()* 4 ),"x" );ck (cudaMemcpy (dx ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice ),"x" ); 188ck (cudaMalloc (& dh ,h .size ()* 4 ),"h" );ck (cudaMemcpy (dh ,h .data (),h .size ()* 4 ,cudaMemcpyHostToDevice ),"h" ); 189ck (cudaMalloc (& dgu , (size_t )T * 2 * FF * 4 ),"gu" );ck (cudaMemset (dgu ,0 , (size_t )T * 2 * FF * 4 ),"gu" ); 190ck (cudaMalloc (& ddn , (size_t ) std::max (nB ,1 )* N * 4 ),"dn" ); 191ck (cudaMalloc (& dxq , mmq::q8_bytes (T ,N )),"xq" );ck (cudaMalloc (& dhq , mmq::q8_bytes (std::max (nB ,1 ),FF )),"hq" ); 192ck (cudaMalloc (& dys ,T * 4 ),"ys" );ck (cudaMalloc (& dhs , std::max (nB ,1 )* 4 ),"hs" ); 193const size_t ids_rows = (size_t ) mmq::mmq_ids_table_rows ((uint64_t )T , (uint64_t )T );// (mul_mat_q reads the row table past its last row: mmq_ids_margin.hpp) 194ck (cudaMalloc (& dids ,ids_rows * 4 ),"ids" );ck (cudaMalloc (& dbounds , (E + 1 )* 4 ),"b" );ck (cudaMalloc (& drel ,5 * 4 ),"rel" ); 195ck (cudaMemcpy (dbounds ,bounds .data (), (E + 1 )* 4 ,cudaMemcpyHostToDevice ),"b" ); 196ck (cudaMemcpy (drel ,relB .data (),5 * 4 ,cudaMemcpyHostToDevice ),"rel" ); 197 mmq::iota (dids , (int64_t )ids_rows ,nullptr ); 198const bool fp4 = mmq::fp4_activations (GGML_TYPE_NVFP4 ); 199 mmq::quantize (dx ,nullptr ,dxq ,GGML_TYPE_NVFP4 ,N ,N ,T ,nullptr ,dys ); 200 mmq::Context ctx ; 201for (int grp = 0 ;grp < 2 ;++ grp ) { 202 mmq::Product p ; 203p .w = dG + (grp ?16 * gub :0 );p .type = GGML_TYPE_NVFP4 ;p .w_rows = 2 * FF ;p .w_cols = N ;p .expert_bytes = gub ; 204p .n = grp ?4 :16 ;p .xq = dxq ;p .bounds = dbounds + (grp ?16 :0 );p .ids = dids ;p .total_rows = T ; 205p .max_rows = grp ?maxB :maxA ;p .dst = dgu ;p .ld_dst = 2 * FF ;p .y_scale = fp4 ?dys :nullptr ; 206ctx .run (p ,nullptr ); 207 } 208 mmq::quantize (dh ,nullptr ,dhq ,GGML_TYPE_NVFP4 ,FF ,FF ,nB ,nullptr ,dhs ); 209 { 210 mmq::Product p ; 211p .w = dD + 16 * db ;p .type = GGML_TYPE_NVFP4 ;p .w_rows = N ;p .w_cols = FF ;p .expert_bytes = db ;p .n = 4 ; 212p .xq = dhq ;p .bounds = drel ;p .ids = dids ;p .total_rows = nB ;p .max_rows = maxB ;p .dst = ddn ;p .ld_dst = N ; 213p .y_scale = fp4 ?dhs :nullptr ; 214ctx .run (p ,nullptr ); 215 } 216ck (cudaDeviceSynchronize (),"run" ); 217 std::vector < float > gu ((size_t )T * 2 * FF ),dn ((size_t ) std::max (nB ,1 )* N ); 218ck (cudaMemcpy (gu .data (),dgu ,gu .size ()* 4 ,cudaMemcpyDeviceToHost ),"gu" ); 219ck (cudaMemcpy (dn .data (),ddn ,dn .size ()* 4 ,cudaMemcpyDeviceToHost ),"dn" ); 220cudaFree (dG );cudaFree (dD );cudaFree (dx );cudaFree (dh );cudaFree (dgu );cudaFree (ddn );cudaFree (dxq );cudaFree (dhq ); 221cudaFree (dys );cudaFree (dhs );cudaFree (dids );cudaFree (dbounds );cudaFree (drel ); 222 223Err ea ,eb ,ed ; 224 std::vector < float > wf ((size_t )2 * FF * N ),wd ((size_t )N * FF ); 225for (int e = 0 ;e < E ;++ e ) { 226ggml_get_type_traits (GGML_TYPE_NVFP4 )-> to_float (G .data ()+ e * gub ,wf .data (),2 * FF * N ); 227for (int t = bounds [e ];t < bounds [e + 1 ];++ t ) 228for (int64_t r = 0 ;r < 2 * FF ;++ r ) 229 (e < 16 ?ea :eb ).add (gu [(size_t )t * 2 * FF + r ],dot_ref (x .data ()+ (size_t )t * N ,wf .data ()+ r * N ,N )); 230if (e >=16 ) { 231ggml_get_type_traits (GGML_TYPE_NVFP4 )-> to_float (D .data ()+ e * db ,wd .data (),N * FF ); 232for (int t = bounds [e ];t < bounds [e + 1 ];++ t ) 233for (int64_t r = 0 ;r < N ;++ r ) 234ed .add (dn [(size_t ) (t - rB )* N + r ],dot_ref (h .data ()+ (size_t ) (t - rB )* FF ,wd .data ()+ r * FF ,FF )); 235 } 236 } 237 std::printf ("group: %d rows over %d experts (max %d / %d per expert)\n" ,T ,E ,maxA ,maxB ); 238 std::printf (" gate/up, experts 0-15 (bounds from 0) rel. error %.4f%%\n" ,100 * ea .rel ()); 239 std::printf (" gate/up, experts 16-19 (bounds from 16) rel. error %.4f%%\n" ,100 * eb .rel ()); 240 std::printf (" down, experts 16-19 (relative bounds) rel. error %.4f%%\n" ,100 * ed .rel ()); 241const double worst = std::max (ea .rel (), std::max (eb .rel (),ed .rel ())); 242 std::printf ("RESULT: %s\n" ,worst > 0.2 ?"WRONG (error beyond 20%)" :"ok" ); 243return worst > 0.2 ?1 :0 ; 244} 245 246int layers (const char * path ,int e ) { 247constexpr int64_t N = 2560 ,FF = 640 ; 248constexpr size_t STRIDE = 2764816 ,LAYER = 512 * STRIDE ; 249const size_t gub = mmq::matrix_bytes (GGML_TYPE_NVFP4 ,2 * FF ,N ),db = mmq::matrix_bytes (GGML_TYPE_NVFP4 ,N ,FF ); 250 std::vector < uint8_t > blob (gub + db ); 251 std::ifstream f (path , std::ios::binary ); 252 std::mt19937 rng (3 ); 253int bad = 0 ; 254for (int l = 0 ;l < 48 ;++ l ) { 255f .seekg ((std::streamoff ) (l * LAYER + (size_t )e * STRIDE )); 256if (!f .read ((char * )blob .data (), (std::streamsize )blob .size ())) { std::fprintf (stderr ,"cannot read layer %d\n" ,l );return 2 ; } 257char name [32 ]; 258 std::snprintf (name ,sizeof name ,"L%02d gu" ,l ); 259bad += product (blob .data (),2 * FF ,N ,64 ,name ,rng ); 260 std::snprintf (name ,sizeof name ,"L%02d dn" ,l ); 261bad += product (blob .data ()+ gub ,N ,FF ,64 ,name ,rng ); 262 } 263 std::printf ("RESULT: %s\n" ,bad ?"WRONG (error beyond 20%)" :"ok" ); 264return bad ?1 :0 ; 265} 266 267int real (const char * experts_bin ,const char * dump ,int layer ) { 268 std::ifstream d (dump , std::ios::binary ); 269int64_t hdr [3 ]; 270if (!d .read ((char * )hdr ,sizeof hdr )) { std::fprintf (stderr ,"cannot read %s\n" ,dump );return 2 ; } 271const int64_t T = hdr [0 ],N = hdr [1 ],K = hdr [2 ],FF = 640 ; 272 std::vector < float > x ((size_t ) (T * N )); 273 std::vector < int32_t > ids ((size_t ) (T * K )); 274d .read ((char * )x .data (), (std::streamsize ) (x .size ()* 4 )); 275d .read ((char * )ids .data (), (std::streamsize ) (ids .size ()* 4 )); 276// the rows' outlier ratio 277 std::vector < double > ratio ; 278for (int64_t t = 0 ;t < T ;++ t ) { 279double mx = 0 ,ss = 0 ; 280for (int64_t k = 0 ;k < N ;++ k ) {const double v = x [(size_t ) (t * N + k )];mx = std::fmax (mx , std::fabs (v ));ss += v * v ; } 281ratio .push_back (mx / std::sqrt (ss /N + 1e-30 )); 282 } 283 std::sort (ratio .begin (),ratio .end ()); 284 std::printf ("layer %d: %lld tokens, max|x| / rms: median %.1f, p99 %.1f, max %.1f\n" ,layer , (long long )T , 285ratio [ratio .size () /2 ],ratio [ratio .size ()* 99 /100 ],ratio .back ()); 286 std::vector < int > cnt (512 ,0 ); 287for (int32_t e :ids )if (e >=0 && e < 512 )++ cnt [e ]; 288 std::vector < int > order (512 ); 289for (int i = 0 ;i < 512 ;++ i )order [i ]= i ; 290 std::sort (order .begin (),order .end (), [& ](int a ,int b ) {return cnt [a ]> cnt [b ]; }); 291constexpr size_t STRIDE = 2764816 ,LAYER = 512 * STRIDE ; 292const size_t gub = mmq::matrix_bytes (GGML_TYPE_NVFP4 ,2 * FF ,N ); 293 std::ifstream f (experts_bin , std::ios::binary ); 294 std::vector < uint8_t > blob (STRIDE ); 295 std::mt19937 rng (1 ); 296double s_gu = 0 ,s_gu16 = 0 ,s_dn = 0 ,s_dn16 = 0 ; 297int n = 0 ; 298 std::printf ("%6s %5s %10s %10s %10s %10s\n" ,"expert" ,"rows" ,"gu mmq" ,"gu fp16" ,"down mmq" ,"down fp16" ); 299for (int i = 0 ;i < 12 ;++ i ) { 300const int e = order [i ]; 301f .seekg ((std::streamoff ) (layer * LAYER + (size_t )e * STRIDE )); 302f .read ((char * )blob .data (), (std::streamsize )blob .size ()); 303float tail [4 ]; 304 std::memcpy (tail ,blob .data ()+ STRIDE - 16 ,sizeof tail ); 305 std::vector < float > xe ; 306for (int64_t t = 0 ;t < T ;++ t ) 307for (int64_t k = 0 ;k < K ;++ k ) 308if (ids [(size_t ) (t * K + k )]== e )xe .insert (xe .end (),x .begin ()+ t * N ,x .begin ()+ (t + 1 )* N ); 309const int Te = (int ) (xe .size () /N ); 310double gu = 0 ,gu16 = 0 ,dn = 0 ,dn16 = 0 ; 311 std::vector < double > ref ; 312product (blob .data (),2 * FF ,N ,Te ,"gu" ,rng ,& xe ,& gu ,& gu16 ,& ref ); 313 std::vector < float > h ((size_t )Te * FF );// the hidden the exact gate/up gives 314for (int t = 0 ;t < Te ;++ t ) 315for (int64_t r = 0 ;r < FF ;++ r ) { 316const double g = ref [(size_t )t * 2 * FF + r ]* tail [0 ],u = ref [(size_t )t * 2 * FF + FF + r ]* tail [1 ]; 317h [(size_t )t * FF + r ]= (float ) (g / (1 + std::exp (- g ))* u ); 318 } 319product (blob .data ()+ gub ,N ,FF ,Te ,"down" ,rng ,& h ,& dn ,& dn16 ); 320 std::printf ("%6d %5d %9.3f%% %9.3f%% %9.3f%% %9.3f%%\n" ,e ,Te ,100 * gu ,100 * gu16 ,100 * dn ,100 * dn16 ); 321s_gu += gu ;s_gu16 += gu16 ;s_dn += dn ;s_dn16 += dn16 ;++ n ; 322 } 323 std::printf ("%6s %5s %9.3f%% %9.3f%% %9.3f%% %9.3f%%\n" ,"mean" ,"" ,100 * s_gu /n ,100 * s_gu16 /n , 324100 * s_dn /n ,100 * s_dn16 /n ); 325return 0 ; 326} 327 328}// namespace 329 330int main (int argc ,char ** argv ) { 331if (argc < 2 ) { std::fprintf (stderr ,"usage: mmq_nvfp4_parity pack/experts.bin [rows ...]\n" );return 2 ; } 332constexpr int64_t N = 2560 ,FF = 640 ; 333const size_t gu_bytes = mmq::matrix_bytes (GGML_TYPE_NVFP4 ,2 * FF ,N ),d_bytes = mmq::matrix_bytes (GGML_TYPE_NVFP4 ,N ,FF ); 334 std::vector < uint8_t > blob (gu_bytes + d_bytes ); 335 std::ifstream f (argv [1 ], std::ios::binary ); 336if (!f .read ((char * )blob .data (), (std::streamsize )blob .size ())) { std::fprintf (stderr ,"cannot read %s\n" ,argv [1 ]);return 2 ; } 337if (argc >=3 && std::string (argv [2 ])== "--group" )return group (argv [1 ]); 338if (argc >=5 && std::string (argv [2 ])== "--real" )return real (argv [1 ],argv [3 ], std::atoi (argv [4 ])); 339if (argc >=3 && std::string (argv [2 ])== "--layers" )return layers (argv [1 ],argc >=4 ? std::atoi (argv [3 ]) :7 ); 340const char * mode = std::getenv ("STRATA_PREFILL_NVFP4" ); 341 std::printf ("mode %s, FP4 activations: %s\n" ,mode ?mode :"default" , mmq::fp4_activations (GGML_TYPE_NVFP4 ) ?"yes" :"no" ); 342 std::vector < int > Ts ; 343for (int i = 2 ;i < argc ;++ i )Ts .push_back (std::atoi (argv [i ])); 344if (Ts .empty ())Ts = {1 ,7 ,37 ,128 ,300 }; 345 std::mt19937 rng (11 ); 346int bad = 0 ; 347for (int T :Ts ) { 348bad += product (blob .data (),2 * FF ,N ,T ,"gate/up" ,rng ); 349bad += product (blob .data ()+ gu_bytes ,N ,FF ,T ,"down" ,rng ); 350 } 351 std::printf ("RESULT: %s\n" ,bad ?"WRONG (error beyond 20%)" :"ok" ); 352return bad ?1 :0 ; 353}