okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ee21447ea
main
1// src/core/dflash2_gemm_bench.cpp - the drafter's Q8 GEMM (kernels/dflash2.hpp df2_gemm) on random planar weights: GB/s per shape and column count. 2// 3// dflash2_gemm_bench [iters] [q4] (q4: the Q4_0 pack's GEMM, kernels/cuda/dflash2.cu gemm4_*) 4// 5// Weight sets are cycled through 2 GiB of distinct matrices so the L2 cache cannot hold them (the drafter reads each matrix once per round). 6#include "strata/kernels/dflash2.hpp" 7 8#include <cuda_runtime.h> 9 10#include <cstdint> 11#include <cstdio> 12#include <cstdlib> 13#include <cstring> 14#include <vector> 15 16int main (int argc ,char ** argv ) { 17const int iters = argc > 1 ? std::atoi (argv [1 ]) :50 ; 18const bool q4 = argc > 2 && std::strcmp (argv [2 ],"q4" )== 0 ; 19const int wnum = q4 ?18 :34 ;// bytes of a 32-weight block: the Q8 pack's int8 + fp16 scale, the Q4_0 pack's nibbles + fp16 scale 20struct Shape {const char * name ;int n_in ,n_out ;int mats ; }; 21const Shape shapes []= {{"gate+up 2560->7680 x2" ,2560 ,7680 ,2 }, {"down 7680->2560" ,7680 ,2560 ,1 }, {"qkv 2560->(4096,1024,1024)" ,2560 ,4096 ,3 }, 22 {"o 4096->2560" ,4096 ,2560 ,1 }, {"fc 12800->2560" ,12800 ,2560 ,1 }, {"kv 2560->1024 x2" ,2560 ,1024 ,2 }, 23 {"akp 2560->640" ,2560 ,640 ,1 }, {"sel 2560->256" ,2560 ,256 ,1 }}; 24cudaEvent_t a ,b ; 25cudaEventCreate (& a ); 26cudaEventCreate (& b ); 27for (const Shape & sh :shapes ) { 28const size_t wbytes = (size_t )sh .n_out * sh .n_in * wnum /32 ; 29const size_t per_set = wbytes * sh .mats ; 30const int sets = (int ) std::max < size_t > (2 , ((size_t )1 <<30 ) /per_set ); 31 std::vector < uint8_t *> w ((size_t )sets * sh .mats ); 32for (auto & p :w ) { 33if (cudaMalloc ((void ** )& p ,wbytes )!= cudaSuccess ) { std::fprintf (stderr ,"alloc\n" );return 1 ; } 34cudaMemset (p ,0x11 ,wbytes ); 35 } 36uint8_t * xq ; 37float * xd ,* y ; 38cudaMalloc ((void ** )& xq , (size_t )sh .n_in * 8 ); 39cudaMalloc ((void ** )& xd , (size_t )sh .n_in /32 * 8 * 4 ); 40cudaMalloc ((void ** )& y , (size_t )8 * sh .n_out * 3 * 4 ); 41 { 42 std::vector < int8_t > hx ((size_t )sh .n_in * 8 ); 43uint32_t rs = 777u ; 44for (auto & v :hx ) {rs = rs * 1664525u + 1013904223u ;v = (int8_t ) (rs >>20 ); } 45cudaMemcpy (xq ,hx .data (),hx .size (),cudaMemcpyHostToDevice ); 46 std::vector < float > ones ((size_t )sh .n_in /32 * 8 ); 47for (auto & v :ones ) {rs = rs * 1664525u + 1013904223u ;v = 0.005f + 0.001f * (float ) ((rs >>20 ) %16 ); } 48cudaMemcpy (xd ,ones .data (),ones .size ()* 4 ,cudaMemcpyHostToDevice ); 49 } 50 std::vector < float > yref ; 51 std::vector < int > vars = {1 ,2 ,10 ,11 ,12 ,13 ,14 ,15 ,16 ,17 ,18 ,19 ,20 ,21 ,22 ,23 }; 52if (q4 )for (int v = 24 ;v <=45 ;++ v )vars .push_back (v ); 53for (int var :vars ) { 54 strata::kernels::df2_gemm_variant (var ); 55const int ncols = 8 ; 56const size_t ny = (size_t )8 * sh .n_out * (sh .mats == 3 ?3 :sh .mats ); 57 {// bitwise against variant 1 58 strata::kernels::Df2Mat m [3 ]; 59for (int i = 0 ;i < sh .mats ;++ i )m [i ]= {w [(size_t )i ],y + (size_t )i * 8 * sh .n_out ,sh .n_out ,q4 }; 60if (sh .mats == 3 ) {m [1 ].n_out = 1024 ;m [2 ].n_out = 1024 ; } 61cudaMemset (y ,0 ,ny * 4 ); 62 strata::kernels::df2_gemm (m ,sh .mats ,xq ,xd ,sh .n_in ,ncols ,nullptr ); 63cudaDeviceSynchronize (); 64 std::vector < float > hy (ny ); 65cudaMemcpy (hy .data (),y ,ny * 4 ,cudaMemcpyDeviceToHost ); 66if (var == 1 )yref = hy ; 67 std::printf ("%s %-30s variant %2d: %s " ,q4 ?"q4" :"q8" ,sh .name ,var , std::memcmp (hy .data (),yref .data (),ny * 4 )== 0 ?"bitwise" :"DIFFERS" ); 68 } 69cudaDeviceSynchronize (); 70cudaEventRecord (a ); 71for (int it = 0 ;it < iters ;++ it ) { 72 strata::kernels::Df2Mat m [3 ]; 73for (int i = 0 ;i < sh .mats ;++ i )m [i ]= {w [(size_t ) (it %sets )* sh .mats + i ],y + (size_t )i * 8 * sh .n_out ,sh .n_out ,q4 }; 74if (sh .mats == 3 ) {m [1 ].n_out = 1024 ;m [2 ].n_out = 1024 ; } 75 strata::kernels::df2_gemm (m ,sh .mats ,xq ,xd ,sh .n_in ,ncols ,nullptr ); 76 } 77cudaEventRecord (b ); 78cudaEventSynchronize (b ); 79float ms ; 80cudaEventElapsedTime (& ms ,a ,b ); 81const double bytes = sh .mats == 3 ? (double ) (4096 + 1024 + 1024 )* sh .n_in * wnum /32 : (double )per_set ; 82 std::printf ("%7.1f us %6.1f GB/s\n" ,1000.0 * ms /iters ,bytes * iters / (ms /1000.0 ) /1e9 ); 83 } 84for (auto p :w )cudaFree (p ); 85cudaFree (xq );cudaFree (xd );cudaFree (y ); 86 } 87return 0 ; 88}