okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
9adf4e01a
main
1// src/kernels/q8_gemv_bench.cpp - MODEL-UPDATE: the dense Q8_0 GEMVs of Unsloth's files (attention, SSM, shared expert, 2// hyper-connections, head: all Q8_0 there) at the engine's shapes, 1..6 columns (a verify window's tokens), through 3// native_mmvq - the call the engine makes - against the card's memory bandwidth. Enough copies of each matrix are 4// cycled to miss the GPU's last-level cache, as a layer's call does. 5// 6// build/q8_gemv_bench [--cols 1,2,3,4,6] [--iters 50] [--bw GB/s] 7#include "strata/kernels/native_mmvq.hpp" 8 9#include <cuda_runtime.h> 10 11#include <algorithm> 12#include <cstdint> 13#include <cstdio> 14#include <cstdlib> 15#include <cstring> 16#include <random> 17#include <string> 18#include <vector> 19 20namespace K = strata::kernels; 21 22namespace { 23struct Shape {const char * what ;int n_in ,n_out ; }; 24const Shape SHAPES []= { 25 {"GDN qkv 2560->10240" ,2560 ,10240 }, {"GDN gate 2560->6144" ,2560 ,6144 }, {"GDN/QSA out 6144->2560" ,6144 ,2560 }, 26 {"QSA q 2560->12288" ,2560 ,12288 }, {"QSA k/v 2560->512" ,2560 ,512 }, {"HC down 10240->320" ,10240 ,320 }, 27 {"HC up 320->10240" ,320 ,10240 }, {"shared gate/up 2560->640" ,2560 ,640 }, {"shared down 640->2560" ,640 ,2560 }, 28 {"head 2560->248320" ,2560 ,248320 }, 29}; 30void ck (cudaError_t e ,const char * w ) { 31if (e != cudaSuccess ) { std::fprintf (stderr ,"%s: %s\n" ,w ,cudaGetErrorString (e )); std::exit (1 ); } 32} 33std::vector < int > ints (const std::string & s ) { 34 std::vector < int > v ; 35size_t from = 0 ; 36for (;;) { 37const size_t c = s .find (',' ,from ); 38v .push_back (std::atoi (s .substr (from ,c == std::string::npos ? std::string::npos :c - from ).c_str ())); 39if (c == std::string::npos )break ; 40from = c + 1 ; 41 } 42return v ; 43} 44}// namespace 45 46int main (int argc ,char ** argv ) { 47 std::vector < int > cols = {1 ,2 ,3 ,4 ,6 }; 48int iters = 50 ; 49double bw = 0 ; 50for (int i = 1 ;i < argc ;++ i ) { 51const std::string a = argv [i ]; 52if (a == "--cols" && i + 1 < argc )cols = ints (argv [++ i ]); 53else if (a == "--iters" && i + 1 < argc )iters = std::atoi (argv [++ i ]); 54else if (a == "--bw" && i + 1 < argc )bw = std::atof (argv [++ i ]); 55 } 56cudaDeviceProp prop ; 57ck (cudaGetDeviceProperties (& prop ,0 ),"props" ); 58 std::printf ("q8_gemv_bench: %s\n" ,prop .name ); 59 std::mt19937 rng (7 ); 60for (const Shape & s :SHAPES ) { 61const size_t bytes = (size_t )s .n_out * (s .n_in /32 )* 34 ; 62const int copies = (int ) std::max < size_t > (1 , std::min < size_t > (16 , ((size_t )512 <<20 ) /bytes )); 63 std::vector < uint8_t > host (bytes ); 64for (size_t b = 0 ;b < bytes ;b += 34 ) {// fp16 scale 0.01 (0x211f), then random int8 weights 65host [b ]= 0x1f ; 66host [b + 1 ]= 0x21 ; 67for (int k = 0 ;k < 32 ;++ k )host [b + 2 + k ]= (uint8_t ) (rng ()& 0xff ); 68 } 69 std::vector < void *> w (copies ); 70for (auto & p :w ) { 71ck (cudaMalloc (& p ,bytes ),"malloc w" ); 72ck (cudaMemcpy (p ,host .data (),bytes ,cudaMemcpyHostToDevice ),"copy w" ); 73 } 74const int maxc = * std::max_element (cols .begin (),cols .end ()); 75 std::vector < float > xh ((size_t )s .n_in * maxc ); 76for (auto & v :xh )v = std::normal_distribution < float > (0.f ,1.f )(rng ); 77float * x = nullptr ,* y = nullptr ; 78void * xq = nullptr ; 79ck (cudaMalloc (& x ,xh .size ()* 4 ),"x" ); 80ck (cudaMemcpy (x ,xh .data (),xh .size ()* 4 ,cudaMemcpyHostToDevice ),"x copy" ); 81ck (cudaMalloc (& xq ,K ::native_q8_1_bytes (s .n_in ,maxc )),"xq" ); 82ck (cudaMalloc (& y , (size_t )s .n_out * maxc * 4 ),"y" ); 83cudaStream_t st ; 84ck (cudaStreamCreate (& st ),"stream" ); 85cudaEvent_t e0 ,e1 ; 86cudaEventCreate (& e0 ); 87cudaEventCreate (& e1 ); 88 std::printf ("\n%s: %.2f MB, %d copies\n" ,s .what ,bytes /1e6 ,copies ); 89for (int c :cols ) { 90K ::native_quantize_q8_1 (x ,xq ,s .n_in ,c ,st ); 91for (int i = 0 ;i < 3 ;++ i )K ::native_mmvq (8 ,w [i %copies ],xq ,y ,s .n_in ,s .n_out ,c ,st ); 92cudaEventRecord (e0 ,st ); 93for (int i = 0 ;i < iters ;++ i )K ::native_mmvq (8 ,w [i %copies ],xq ,y ,s .n_in ,s .n_out ,c ,st ); 94cudaEventRecord (e1 ,st ); 95ck (cudaEventSynchronize (e1 ),"sync" ); 96float ms = 0 ; 97cudaEventElapsedTime (& ms ,e0 ,e1 ); 98const double us = 1000.0 * ms /iters ,gbs = bytes / (us * 1e3 ); 99 std::printf (" %d col%s: %8.1f us %7.1f GB/s%s\n" ,c ,c == 1 ?" " :"s" ,us ,gbs , 100bw > 0 ? (" = " + std::to_string ((int ) (100 * gbs /bw + 0.5 ))+ "% of bw" ).c_str () :"" ); 101 } 102for (auto p :w )cudaFree (p ); 103cudaFree (x ); 104cudaFree (xq ); 105cudaFree (y ); 106cudaStreamDestroy (st ); 107 } 108return 0 ; 109}