okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
331b591b3
main
1// src/kernels/router_chain_bench.cpp - what the FFN-side router chain of one layer costs in a captured graph: 2// the router GEMV (bf16 [512][2560], T rows), the top-10, the doorbell publish; 48 layers' worth with distinct 3// weights (cold), as one graph, per-layer microseconds. Bench only (no parity: the kernels are the engine's own). 4// 5// build/router_chain_bench [T=2] [reps=20] 6#include "strata/kernels/bf16_gemv.hpp" 7#include "strata/kernels/elementwise.hpp" 8#include "strata/kernels/native_router.hpp" 9#include "strata/kernels/verify_kernels.hpp" 10 11#include <cuda_runtime.h> 12 13#include <algorithm> 14#include <cstdint> 15#include <cstdio> 16#include <cstdlib> 17#include <cstring> 18#include <random> 19#include <vector> 20 21namespace K = strata::kernels; 22 23#define CK (x ) do { cudaError_t e_ = (x); if (e_ != cudaSuccess) { std::fprintf(stderr, "%s: %s\n", #x, cudaGetErrorString(e_)); std::exit(1); } } while (0) 24 25 26int main (int argc ,char ** argv ) { 27setvbuf (stdout ,nullptr ,_IONBF ,0 ); 28const int T = argc > 1 ? std::atoi (argv [1 ]) :2 ; 29const int reps = argc > 2 ? std::atoi (argv [2 ]) :20 ; 30const int L = 48 ,N = 2560 ,NE = 512 ,KK = 10 ; 31 std::mt19937 rng (3 ); 32 std::normal_distribution < float > nd (0.f ,1.f ); 33 std::vector < uint16_t > w ((size_t )NE * N ); 34for (auto & x :w ) { 35const float f = 0.05f * nd (rng ); 36uint32_t u ; 37 std::memcpy (& u ,& f ,4 ); 38x = (uint16_t ) ((u + 0x7fff + ((u >>16 )& 1 )) >>16 ); 39 } 40 std::vector < float > xh ((size_t )T * N ); 41for (auto & x :xh )x = nd (rng ); 42 std::vector < uint16_t *> dw (L ); 43for (int l = 0 ;l < L ;++ l ) { 44CK (cudaMalloc ((void ** )& dw [l ],w .size ()* 2 )); 45CK (cudaMemcpy (dw [l ],w .data (),w .size ()* 2 ,cudaMemcpyHostToDevice )); 46 } 47float * dx ,* dlog ,* dwt ,* dxo ; 48int32_t * dids ,* dids_o ,* dres ; 49CK (cudaMalloc ((void ** )& dx ,xh .size ()* 4 )); 50CK (cudaMemcpy (dx ,xh .data (),xh .size ()* 4 ,cudaMemcpyHostToDevice )); 51CK (cudaMalloc ((void ** )& dlog , (size_t )T * NE * 4 )); 52CK (cudaMalloc ((void ** )& dwt , (size_t )T * KK * 4 )); 53CK (cudaMalloc ((void ** )& dids , (size_t )T * KK * 4 )); 54CK (cudaMalloc ((void ** )& dids_o , (size_t )T * KK * 4 )); 55CK (cudaMalloc ((void ** )& dxo ,xh .size ()* 4 )); 56CK (cudaMalloc ((void ** )& dres ,NE * 4 )); 57 std::vector < int32_t > res (NE ); 58for (int i = 0 ;i < NE ;++ i )res [i ]= i ; 59CK (cudaMemcpy (dres ,res .data (),NE * 4 ,cudaMemcpyHostToDevice )); 60uint32_t * hseq ; 61CK (cudaHostAlloc ((void ** )& hseq ,64 ,cudaHostAllocMapped |cudaHostAllocPortable )); 62* hseq = 0 ; 63uint32_t * dseq ; 64CK (cudaHostGetDevicePointer ((void ** )& dseq ,hseq ,0 )); 65int * dz ; 66CK (cudaMalloc ((void ** )& dz ,16 )); 67CK (cudaMemset (dz ,0 ,16 )); 68cudaStream_t s ; 69CK (cudaStreamCreate (& s )); 70 {// outputs of the router GEMV for several shapes and token counts as FNV hashes: compare across STRATA_MMVF_PF=0/1 71const int shapes [][2 ]= {{2560 ,512 }, {2560 ,256 }, {2560 ,64 }, {1280 ,513 }, {640 ,100 }, {4096 ,50 }, {2048 ,7 }, {256 ,33 }, {8192 ,90 }}; 72for (const auto & sh :shapes ) { 73const int ni = sh [0 ],no = sh [1 ]; 74 std::vector < uint16_t > wh ((size_t )no * ni ); 75for (auto & v :wh ) { 76const float f = 0.05f * nd (rng ); 77uint32_t u ; 78 std::memcpy (& u ,& f ,4 ); 79v = (uint16_t ) ((u + 0x7fff + ((u >>16 )& 1 )) >>16 ); 80 } 81 std::vector < float > xs ((size_t )8 * ni ); 82for (auto & v :xs )v = nd (rng ); 83uint16_t * dwp ; 84float * dxp ,* dyp ; 85CK (cudaMalloc ((void ** )& dwp ,wh .size ()* 2 )); 86CK (cudaMemcpy (dwp ,wh .data (),wh .size ()* 2 ,cudaMemcpyHostToDevice )); 87CK (cudaMalloc ((void ** )& dxp ,xs .size ()* 4 )); 88CK (cudaMemcpy (dxp ,xs .data (),xs .size ()* 4 ,cudaMemcpyHostToDevice )); 89CK (cudaMalloc ((void ** )& dyp , (size_t )8 * no * 4 )); 90for (int tk = 1 ;tk <=8 ;++ tk ) { 91CK (cudaMemset (dyp ,0xff , (size_t )8 * no * 4 )); 92K ::bf16_gemv_fp32_mmvf_multi (dxp ,ni ,dwp ,dyp ,no ,ni ,no ,tk ,s ); 93CK (cudaStreamSynchronize (s )); 94 std::vector < float > yh ((size_t )8 * no ); 95CK (cudaMemcpy (yh .data (),dyp ,yh .size ()* 4 ,cudaMemcpyDeviceToHost )); 96unsigned long long h = 1469598103934665603ull ; 97for (size_t b = 0 ;b < yh .size ()* 4 ;++ b )h = (h ^ ((const unsigned char * )yh .data ())[b ])* 1099511628211ull ; 98 std::printf ("PARITY n_in %d n_out %d T %d hash %016llx\n" ,ni ,no ,tk ,h ); 99 } 100cudaFree (dwp );cudaFree (dxp );cudaFree (dyp ); 101 } 102 } 103cudaEvent_t e0 ,e1 ; 104CK (cudaEventCreate (& e0 )); 105CK (cudaEventCreate (& e1 )); 106 {// the BF16 GEMV by shape and token count, 48 distinct weight copies in one graph: us per call (STRATA_MMVF_PF=0 / 1 to compare) 107const int outs []= {512 ,256 ,128 ,64 ,48 ,8 ,1 }; 108const int ins []= {2560 ,2560 ,2560 ,2560 ,2560 ,2560 ,2560 }; 109for (int si = 0 ;si < 7 ;++ si ) { 110const int no = outs [si ],ni = ins [si ]; 111 std::vector < uint16_t *> dws (L ); 112for (int l = 0 ;l < L ;++ l ) { 113CK (cudaMalloc ((void ** )& dws [l ], (size_t )no * ni * 2 )); 114CK (cudaMemcpy (dws [l ],w .data (), (size_t )no * ni * 2 < w .size ()* 2 ? (size_t )no * ni * 2 :w .size ()* 2 ,cudaMemcpyHostToDevice )); 115 } 116for (int tk : {1 ,2 ,3 ,4 ,6 }) { 117cudaGraph_t g ; 118cudaGraphExec_t ge ; 119CK (cudaStreamBeginCapture (s ,cudaStreamCaptureModeThreadLocal )); 120for (int l = 0 ;l < L ;++ l )K ::bf16_gemv_fp32_mmvf_multi (dx ,N ,dws [l ],dlog ,NE ,ni ,no ,tk ,s ); 121CK (cudaStreamEndCapture (s ,& g )); 122CK (cudaGraphInstantiate (& ge ,g ,nullptr ,nullptr ,0 )); 123for (int i = 0 ;i < 5 ;++ i )CK (cudaGraphLaunch (ge ,s )); 124CK (cudaStreamSynchronize (s )); 125 std::vector < float > ms ; 126for (int r = 0 ;r < reps ;++ r ) { 127CK (cudaEventRecord (e0 ,s )); 128CK (cudaGraphLaunch (ge ,s )); 129CK (cudaEventRecord (e1 ,s )); 130CK (cudaEventSynchronize (e1 )); 131float m = 0 ; 132CK (cudaEventElapsedTime (& m ,e0 ,e1 )); 133ms .push_back (m ); 134 } 135 std::sort (ms .begin (),ms .end ()); 136 std::printf ("SHAPE n_in %d n_out %3d T %d : %6.2f us per call\n" ,ni ,no ,tk ,1e3 * ms [ms .size () /2 ] /L ); 137cudaGraphExecDestroy (ge ); 138cudaGraphDestroy (g ); 139 } 140for (int l = 0 ;l < L ;++ l )cudaFree (dws [l ]); 141 } 142 } 143// mode bits: 1 mmvf, 2 route, 4 doorbell, 8 an empty kernel per layer (the node floor) 144const char * names []= {"empty node" ,"mmvf" ,"route" ,"mmvf+route" ,"doorbell" ,"mmvf+doorbell" ,"route+doorbell" ,"mmvf+route+doorbell" }; 145for (int mode = 0 ;mode < 8 ;++ mode ) { 146cudaGraph_t g ; 147cudaGraphExec_t ge ; 148CK (cudaStreamBeginCapture (s ,cudaStreamCaptureModeThreadLocal )); 149for (int l = 0 ;l < L ;++ l ) { 150if (mode == 0 )K ::gpu_stamp ((unsigned long long * )dz ,0 ,s ); 151if (mode & 1 )K ::bf16_gemv_fp32_mmvf_multi (dx ,N ,dw [l ],dlog ,NE ,N ,NE ,T ,s ); 152if (mode & 2 )K ::native_router_top10_multi (dlog ,dids ,dwt ,T ,s ); 153if (mode & 4 )K ::doorbell_publish_res (dx ,dids ,dres ,NE , (int64_t )T * N , (int64_t )T * KK ,dxo ,dids_o ,dseq ,s ); 154 } 155CK (cudaStreamEndCapture (s ,& g )); 156CK (cudaGraphInstantiate (& ge ,g ,nullptr ,nullptr ,0 )); 157for (int i = 0 ;i < 5 ;++ i )CK (cudaGraphLaunch (ge ,s )); 158CK (cudaStreamSynchronize (s )); 159 std::vector < float > ms ; 160for (int r = 0 ;r < reps ;++ r ) { 161CK (cudaEventRecord (e0 ,s )); 162CK (cudaGraphLaunch (ge ,s )); 163CK (cudaEventRecord (e1 ,s )); 164CK (cudaEventSynchronize (e1 )); 165float m = 0 ; 166CK (cudaEventElapsedTime (& m ,e0 ,e1 )); 167ms .push_back (m ); 168 } 169 std::sort (ms .begin (),ms .end ()); 170 std::printf ("T %d %-20s : %6.2f us per layer (median of %d graphs of %d layers)\n" ,T ,names [mode ],1e3 * ms [ms .size () /2 ] /L ,reps ,L ); 171cudaGraphExecDestroy (ge ); 172cudaGraphDestroy (g ); 173 } 174return 0 ; 175}