okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
f2b41fd29
main
1// src/prefill/mmq_expert_bench.cpp - the prompt path's routed-expert products (MMQ) timed at the engine's shapes. 2// 3// [STRATA_PREFILL_NVFP4=w4a8|w4a4] mmq_expert_bench [rows per expert ...] 4// 5// One MMQ group as the prompt path launches it: 16 experts, R rows each, in one activation array in expert order. 6// gate/up [1280 x 2560] and down [2560 x 640] in NVFP4, down in Q8_0 (the GPTQ + Q8_0-down pack's 27 layers), and 7// UD-IQ4_XS's IQ3_S gate/up and IQ4_NL down for comparison. 8// Synthetic weights of the right format (valid scales), random activations of row-varying magnitude. Per product: 9// the median of 25 runs, and the rate in int8-equivalent TOPS (2 x rows x out x in). Default R: 12 (a 600-token 10// chunk: 600 x 10 / 512), 160 (8K), 626 (32K). 11#include "strata/prefill/mmq_ids_margin.hpp" 12#include "strata/prefill/moe_mmq.hpp" 13 14#include "ggml.h" 15 16#include <cuda_runtime.h> 17 18#include <algorithm> 19#include <cstdio> 20#include <cstdlib> 21#include <cstring> 22#include <random> 23#include <vector> 24 25namespace mmq= strata::prefill::mmq; 26 27namespace { 28 29void ck (cudaError_t e ,const char * what ) { 30if (e != cudaSuccess ) { std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); std::exit (1 ); } 31} 32 33// valid synthetic weights: NVFP4 blocks are {e4m3 scales, e2m1 codes}; Q8_0 {f16 d, int8 q[32]}. Random bytes with 34// the scale fields overwritten by small positive values (an e4m3 / f16 in the model's range). 35std::vector < uint8_t > weights (ggml_type t ,int64_t rows ,int64_t cols ,int experts , std::mt19937 & rng ) { 36const size_t per = mmq::matrix_bytes (t ,rows ,cols ); 37 std::vector < uint8_t > w ((size_t )experts * per + 4096 ); 38 std::uniform_int_distribution < int > byte (0 ,255 ); 39for (auto & b :w )b = (uint8_t )byte (rng ); 40const size_t bs = ggml_type_size (t ),nblk = (size_t )experts * per /bs ; 41for (size_t i = 0 ;i < nblk ;++ i ) { 42uint8_t * blk = w .data ()+ i * bs ; 43if (t != GGML_TYPE_NVFP4 ) {// Q8_0, IQ3_S, IQ4_NL: an f16 d leads the block 44const uint16_t d = 0x2000 + (uint16_t )byte (rng );// f16 ~ 0.0078..0.016 45 std::memcpy (blk ,& d ,2 ); 46 }else {// NVFP4: the scales lead the block 47const int nsc = (int ) (bs - (size_t )ggml_blck_size (t ) /2 ); 48for (int s = 0 ;s < nsc ;++ s )blk [s ]= (uint8_t ) (0x30 + byte (rng ) %16 );// e4m3 ~ 0.5..1 49 } 50 } 51return w ; 52} 53 54struct Case { 55const char * name ; 56ggml_type t ; 57int64_t rows ,cols ; 58}; 59 60double run_case (const Case & c ,int R , std::mt19937 & rng ,float * ms_out ) { 61constexpr int E = 16 ; 62const int T = E * R ; 63const size_t per = mmq::matrix_bytes (c .t ,c .rows ,c .cols ); 64 std::vector < uint8_t > w = weights (c .t ,c .rows ,c .cols ,E ,rng ); 65 std::vector < float > x ((size_t )T * c .cols ); 66 std::normal_distribution < float > nd (0.f ,1.f ); 67 std::uniform_real_distribution < float > mag (0.1f ,10.f ); 68for (int t = 0 ;t < T ;++ t ) {const float m = mag (rng );for (int64_t k = 0 ;k < c .cols ;++ k )x [(size_t )t * c .cols + k ]= nd (rng )* m ; } 69 std::vector < int32_t > bounds (E + 1 ); 70for (int e = 0 ;e <=E ;++ e )bounds [e ]= e * R ; 71 72uint8_t * dw ;float * dx ,* dd ,* dys ;void * dq ;int32_t * dids ,* db ; 73ck (cudaMalloc (& dw ,w .size ()),"w" );ck (cudaMemcpy (dw ,w .data (),w .size (),cudaMemcpyHostToDevice ),"w" ); 74ck (cudaMalloc (& dx ,x .size ()* 4 ),"x" );ck (cudaMemcpy (dx ,x .data (),x .size ()* 4 ,cudaMemcpyHostToDevice ),"x" ); 75ck (cudaMalloc (& dq , mmq::q8_bytes (T ,c .cols )),"xq" ); 76ck (cudaMalloc (& dys , (size_t )T * 4 ),"ys" ); 77ck (cudaMalloc (& dd , (size_t )T * c .rows * 4 ),"dst" ); 78const size_t ids_rows = (size_t ) mmq::mmq_ids_table_rows ((uint64_t )T , (uint64_t )R );// (mul_mat_q reads the row table past its last row: mmq_ids_margin.hpp) 79ck (cudaMalloc (& dids ,ids_rows * 4 ),"ids" ); 80ck (cudaMalloc (& db , (E + 1 )* 4 ),"bounds" ); 81ck (cudaMemcpy (db ,bounds .data (), (E + 1 )* 4 ,cudaMemcpyHostToDevice ),"bounds" ); 82 mmq::iota (dids , (int64_t )ids_rows ,nullptr ); 83 mmq::quantize (dx ,nullptr ,dq ,c .t ,c .cols ,c .cols ,T ,nullptr ,dys ); 84 mmq::Context ctx ; 85 mmq::Product p ; 86p .w = dw ;p .type = c .t ;p .w_rows = c .rows ;p .w_cols = c .cols ;p .expert_bytes = per ;p .n = E ; 87p .xq = dq ;p .bounds = db ;p .ids = dids ;p .total_rows = T ;p .max_rows = R ;p .dst = dd ;p .ld_dst = c .rows ; 88p .y_scale = mmq::fp4_activations (c .t ) ?dys :nullptr ; 89cudaEvent_t e0 ,e1 ; 90cudaEventCreate (& e0 );cudaEventCreate (& e1 ); 91 std::vector < float > ts ; 92for (int i = 0 ;i < 28 ;++ i ) { 93cudaEventRecord (e0 ); 94ctx .run (p ,nullptr ); 95cudaEventRecord (e1 ); 96ck (cudaEventSynchronize (e1 ),"run" ); 97float ms ;cudaEventElapsedTime (& ms ,e0 ,e1 ); 98if (i >=3 )ts .push_back (ms ); 99 } 100 std::sort (ts .begin (),ts .end ()); 101const float ms = ts [ts .size () /2 ]; 102if (ms_out )* ms_out = ms ; 103cudaFree (dw );cudaFree (dx );cudaFree (dq );cudaFree (dys );cudaFree (dd );cudaFree (dids );cudaFree (db ); 104cudaEventDestroy (e0 );cudaEventDestroy (e1 ); 105return 2.0 * T * (double )c .rows * c .cols / (ms * 1e9 ); 106} 107 108}// namespace 109 110int main (int argc ,char ** argv ) { 111 std::vector < int > Rs ; 112for (int i = 1 ;i < argc ;++ i )Rs .push_back (std::atoi (argv [i ])); 113if (Rs .empty ())Rs = {12 ,160 ,626 }; 114const char * mode = std::getenv ("STRATA_PREFILL_NVFP4" ); 115 std::printf ("mmq_expert_bench: mode %s (FP4 activations: %s), 16 experts a launch, medians of 25\n" , 116mode ?mode :"w4a8" , mmq::fp4_activations (GGML_TYPE_NVFP4 ) ?"yes" :"no" ); 117const Case cases []= {{"nvfp4 gate/up" ,GGML_TYPE_NVFP4 ,1280 ,2560 }, 118 {"nvfp4 down" ,GGML_TYPE_NVFP4 ,2560 ,640 }, 119 {"q8_0 down" ,GGML_TYPE_Q8_0 ,2560 ,640 }, 120 {"iq3_s gate/up" ,GGML_TYPE_IQ3_S ,1280 ,2560 }, 121 {"iq4_nl down" ,GGML_TYPE_IQ4_NL ,2560 ,640 }}; 122 std::mt19937 rng (7 ); 123for (int R :Rs ) 124for (const Case & c :cases ) { 125float ms = 0 ; 126const double tops = run_case (c ,R ,rng ,& ms ); 127 std::printf (" R %4d %-14s %8.3f ms %7.1f TOPS\n" ,R ,c .name ,ms ,tops ); 128 } 129return 0 ; 130}