okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fd19246dc
main
1// src/prefill/lat_mmq_bench.cpp - the latent A stage at the engine's own shapes and group sizes: gate/up Q3_K 2// [1280 x 1280], down IQ4_NL [1280 x 640], ELL = 1280, n experts a launch, R rows each. Every stage timed alone. 3// 4// lat_mmq_bench [R ...] default R = 95 (a 4K chunk: 42450 entries / 512 experts / 15.6 a group) 5// 6// The gather is the engine's per-expert `gather_native` into the MMQ group buffers, with a plain D2D memcpy of the 7// same bytes as the reference: the question is whether that copy or the products are the A stage's cost. 8#include "strata/prefill/moe_mmq.hpp" 9 10#include "ggml.h" 11 12#include <cuda_runtime.h> 13 14#include <algorithm> 15#include <cstdio> 16#include <cstdlib> 17#include <cstring> 18#include <random> 19#include <vector> 20 21namespace mmq= strata::prefill::mmq; 22 23namespace { 24 25void ck (cudaError_t e ,const char * what ) { 26if (e != cudaSuccess ) { std::fprintf (stderr ,"%s: %s\n" ,what ,cudaGetErrorString (e )); std::exit (1 ); } 27} 28 29std::vector < uint8_t > weights (ggml_type t ,int64_t rows ,int64_t cols ,int experts , std::mt19937 & rng ) { 30const size_t per = mmq::matrix_bytes (t ,rows ,cols ),bs = ggml_type_size (t ); 31 std::vector < uint8_t > w ((size_t )experts * per ); 32 std::uniform_int_distribution < int > byte (0 ,255 ); 33for (auto & b :w )b = (uint8_t )byte (rng ); 34for (size_t i = 0 ;i < w .size () /bs ;++ i ) { 35const uint16_t d = (uint16_t ) (0x2800 + byte (rng ) %0x800 ); 36 std::memcpy (w .data ()+ i * bs + (t == GGML_TYPE_Q3_K ?bs - 2 :0 ),& d ,2 ); 37 } 38return w ; 39} 40 41cudaStream_t g_s = nullptr ;// the engine's compute stream is not the default one 42 43float med (void (* fire )(void * ),void * arg ) { 44cudaEvent_t a ,b ; 45cudaEventCreate (& a );cudaEventCreate (& b ); 46 std::vector < float > ts ; 47for (int i = 0 ;i < 26 ;++ i ) { 48cudaEventRecord (a ,g_s ); 49fire (arg ); 50cudaEventRecord (b ,g_s ); 51ck (cudaEventSynchronize (b ),"sync" ); 52float ms = 0 ;cudaEventElapsedTime (& ms ,a ,b ); 53if (i >=3 )ts .push_back (ms ); 54 } 55cudaEventDestroy (a );cudaEventDestroy (b ); 56 std::sort (ts .begin (),ts .end ()); 57return ts [ts .size () /2 ]; 58} 59 60const int64_t ELL = 1280 ,FF = 640 ; 61const size_t GU_B = 1280 * 550 ;// Q3_K [1280, 1280]: 5 blocks of 256 x 110 B 62const size_t DN_B = 1280 * 20 * 18 ;// IQ4_NL [1280, 640]: 20 blocks of 32 x 18 B 63 64struct Ctx { 65int n = 16 ; 66int64_t R = 95 ; 67 std::vector < uint8_t > wgu ,wdn ; 68 std::vector < float > xl ; 69 std::vector < int32_t > bounds ; 70void * dgu = nullptr ,* ddn = nullptr ,* dxl = nullptr ,* dxq = nullptr ,* dgo = nullptr ,* dho = nullptr ; 71void * dhq = nullptr ,* ddo = nullptr ,* db = nullptr ,* dids = nullptr ,* ggu = nullptr ,* gdn = nullptr ; 72 mmq::Context ctx ; 73 mmq::Product gu ,dn ; 74 mmq::Product g2s ,d2s ;// the engine's geometry, timed 75}; 76 77void fire_gather (void * a ) {// one launch per expert, as the engine does it 78Ctx * c = (Ctx * )a ; 79for (int q = 0 ;q < c -> n ;++ q ) 80 mmq::gather_native (c -> ddn , (const uint8_t * )c -> ddn + DN_B + GU_B /2 ,GU_B /2 , 81 (const uint8_t * )c -> ddn + DN_B + GU_B ,DN_B , 82 (uint8_t * )c -> ggu + (size_t )q * GU_B , (uint8_t * )c -> gdn + (size_t )q * DN_B ,g_s ); 83} 84void fire_gather_group (void * a ) {// one launch for the whole group 85Ctx * c = (Ctx * )a ; 86 mmq::GatherGroup g ; 87g .first = 0 ; 88g .n = std::min (c -> n , mmq::kGatherGroupMax ); 89for (int q = 0 ;q < g .n ;++ q )g .blob [q ]= (const uint8_t * )c -> ddn ; 90 mmq::gather_native_group (g ,DN_B + GU_B /2 ,GU_B /2 ,DN_B + GU_B ,DN_B ,c -> ggu ,GU_B ,c -> gdn ,DN_B ,g_s ); 91} 92void fire_memcpy (void * a ) { 93Ctx * c = (Ctx * )a ; 94for (int q = 0 ;q < c -> n ;++ q ) { 95ck (cudaMemcpyAsync ((uint8_t * )c -> ggu + (size_t )q * GU_B ,c -> ddn ,GU_B ,cudaMemcpyDeviceToDevice ,nullptr ),"cp" ); 96ck (cudaMemcpyAsync ((uint8_t * )c -> gdn + (size_t )q * DN_B ,c -> ddn ,DN_B ,cudaMemcpyDeviceToDevice ,nullptr ),"cp2" ); 97 } 98} 99void fire_quant (void * a ) { 100Ctx * c = (Ctx * )a ; 101 mmq::quantize (c -> xl .data (),nullptr ,c -> dxq ,GGML_TYPE_Q3_K ,ELL ,ELL , (int64_t )c -> n * c -> R ,nullptr ); 102} 103void fire_gu (void * a ) {Ctx * c = (Ctx * )a ;c -> ctx .run (c -> gu ,g_s ); } 104void fire_swiglu (void * a ) { 105Ctx * c = (Ctx * )a ; 106 mmq::swiglu ((const float * )c -> dgo , (float * )c -> dho , (int64_t )c -> n * c -> R ,640 , false,nullptr ); 107} 108void fire_quanth (void * a ) { 109Ctx * c = (Ctx * )a ; 110 mmq::quantize ((const float * )c -> dho ,nullptr ,c -> dhq ,GGML_TYPE_IQ4_NL ,640 ,640 , (int64_t )c -> n * c -> R ,nullptr ); 111} 112void fire_dn (void * a ) {Ctx * c = (Ctx * )a ;c -> ctx .run (c -> dn ,g_s ); } 113 114}// namespace 115 116int main (int argc ,char ** argv ) { 117 std::vector < int64_t > Rs ; 118for (int i = 1 ;i < argc ;++ i )Rs .push_back (std::atoll (argv [i ])); 119if (Rs .empty ())Rs = {95 }; 120 std::printf ("lat_mmq_bench: gu Q3_K [1280 x 1280] (%.0f KB), dn IQ4_NL [1280 x 640] (%.0f KB), medians of 23\n" , 121GU_B /1024.0 ,DN_B /1024.0 ); 122ck (cudaStreamCreate (& g_s ),"stream" ); 123 std::mt19937 rng (7 ); 124for (int64_t R :Rs ) 125for (int n : {1 ,4 ,16 }) { 126Ctx c ; 127c .n = n ;c .R = R ; 128const int64_t T = (int64_t )n * R ; 129c .wgu = weights (GGML_TYPE_Q3_K ,1280 ,ELL ,n ,rng ); 130c .wdn = weights (GGML_TYPE_IQ4_NL ,ELL ,FF ,n ,rng ); 131 std::normal_distribution < float > nd (0.f ,1.f ); 132c .xl .resize ((size_t )T * ELL ); 133for (auto & v :c .xl )v = nd (rng ); 134c .bounds .resize ((size_t )n + 1 ); 135for (int e = 0 ;e <=n ;++ e )c .bounds [(size_t )e ]= (int32_t ) (e * R ); 136auto alloc = [](void ** p ,size_t bytes ) {ck (cudaMalloc (p ,bytes ?bytes :16 ),"alloc" ); }; 137alloc (& c .dgu ,c .wgu .size ()); 138alloc (& c .ddn ,c .wdn .size ()+ GU_B + DN_B );// room for the "expert blob" the gather reads 139alloc (& c .ggu , (size_t )n * GU_B ); 140alloc (& c .gdn , (size_t )n * DN_B ); 141alloc (& c .dxl ,c .xl .size ()* 4 ); 142alloc (& c .dxq , mmq::q8_bytes (T ,ELL )); 143alloc (& c .dgo , (size_t )T * 1280 * 4 ); 144alloc (& c .dho , (size_t )T * 640 * 4 ); 145alloc (& c .dhq , mmq::q8_bytes (T ,640 )); 146alloc (& c .ddo , (size_t )T * ELL * 4 ); 147alloc (& c .db , ((size_t )n + 1 )* 4 ); 148alloc (& c .dids , (size_t ) (T + 1024 )* 4 ); 149ck (cudaMemcpy (c .dgu ,c .wgu .data (),c .wgu .size (),cudaMemcpyHostToDevice ),"w" ); 150ck (cudaMemcpy (c .ddn ,c .wdn .data (),c .wdn .size (),cudaMemcpyHostToDevice ),"w2" ); 151ck (cudaMemcpy (c .dxl ,c .xl .data (),c .xl .size ()* 4 ,cudaMemcpyHostToDevice ),"x" ); 152ck (cudaMemcpy (c .db ,c .bounds .data (), ((size_t )n + 1 )* 4 ,cudaMemcpyHostToDevice ),"b" ); 153 mmq::iota ((int32_t * )c .dids ,T + 1024 ,nullptr ); 154 mmq::quantize (c .xl .data (),nullptr ,c .dxq ,GGML_TYPE_Q3_K ,ELL ,ELL ,T ,nullptr ); 155 mmq::swiglu ((const float * )c .dgo , (float * )c .dho ,T ,640 , false,nullptr ); 156 mmq::quantize ((const float * )c .dho ,nullptr ,c .dhq ,GGML_TYPE_IQ4_NL ,640 ,640 ,T ,nullptr ); 157c .gu .w = c .dgu ;c .gu .type = GGML_TYPE_Q3_K ;c .gu .w_rows = 1280 ;c .gu .w_cols = ELL ; 158c .gu .expert_bytes = GU_B ;c .gu .n = n ;c .gu .xq = c .dxq ;c .gu .bounds = (const int32_t * )c .db ; 159c .gu .ids = (const int32_t * )c .dids ;c .gu .total_rows = T ;c .gu .max_rows = R ; 160c .gu .dst = (float * )c .dgo ;c .gu .ld_dst = 1280 ; 161c .dn .w = c .ddn ;c .dn .type = GGML_TYPE_IQ4_NL ;c .dn .w_rows = ELL ;c .dn .w_cols = 640 ; 162c .dn .expert_bytes = DN_B ;c .dn .n = n ;c .dn .xq = c .dhq ;c .dn .bounds = (const int32_t * )c .db ; 163c .dn .ids = (const int32_t * )c .dids ;c .dn .total_rows = T ;c .dn .max_rows = R ; 164c .dn .dst = (float * )c .ddo ;c .dn .ld_dst = ELL ; 165if (n == 16 ) {// the engine's own layer-0 group: uneven bounds, maxr 370, total_rows 1278 166const int32_t ub [17 ]= {0 ,266 ,267 ,637 ,827 ,828 ,1019 ,1206 ,1207 ,1212 ,1213 ,1216 ,1218 ,1219 , 1671263 ,1277 ,1278 }; 168ck (cudaMemcpy (c .db ,ub ,17 * 4 ,cudaMemcpyHostToDevice ),"ub" ); 169const int64_t UN = 1278 ; 170ck (cudaStreamSynchronize (g_s ),"pre" ); 171 mmq::Product g2 = c .gu ; 172g2 .total_rows = UN ;g2 .max_rows = 370 ; 173c .ctx .run (g2 ,g_s ); 174 std::printf (" UNEVEN gu n=16 nr=%lld maxr=370: %s\n" , (long long )UN , 175cudaGetErrorString (cudaStreamSynchronize (g_s ))); 176 mmq::Product d2 = c .dn ; 177d2 .total_rows = UN ;d2 .max_rows = 370 ; 178c .ctx .run (d2 ,g_s ); 179c .g2s = g2 ;c .d2s = d2 ; 180 { 181auto tg = [](void * q ) {Ctx * x = (Ctx * )q ;x -> ctx .run (x -> g2s ,g_s ); }; 182auto td = [](void * q ) {Ctx * x = (Ctx * )q ;x -> ctx .run (x -> d2s ,g_s ); }; 183const float tg2 = med (tg ,& c ),td2 = med (td ,& c ); 184 std::printf (" UNEVEN TIMED n=16 nr=1278 maxr=370: gu %7.1f us | dn %7.1f us (dn/gu %.1fx)\n" , 185tg2 * 1000 ,td2 * 1000 ,tg2 > 0 ?td2 /tg2 :0.0 ); 186 } 187 std::printf (" UNEVEN dn n=16 nr=%lld maxr=370: %s\n" , (long long )UN , 188cudaGetErrorString (cudaStreamSynchronize (g_s ))); 189ck (cudaMemcpy (c .db ,c .bounds .data (), ((size_t )n + 1 )* 4 ,cudaMemcpyHostToDevice ),"ub2" ); 190 } 191const double mb = (double )n * (GU_B + DN_B ) /1e6 ; 192const float tg = med (fire_gather ,& c ),tgg = med (fire_gather_group ,& c ),tc = med (fire_memcpy ,& c ); 193const float tq = med (fire_quant ,& c ),tgu = med (fire_gu ,& c ),ts = med (fire_swiglu ,& c ); 194const float th = med (fire_quanth ,& c ),td = med (fire_dn ,& c ); 195 std::printf (" R %3lld n %2d rows %5lld | gather %8.1f us (%5.0f GB/s per-exp) | gather_group %7.1f us" 196" (%5.0f GB/s) | memcpy %7.1f us (%5.0f GB/s) [%.1f MB]\n" , 197 (long long )R ,n , (long long )T ,tg * 1000 ,mb / (tg * 1e3 ) /1e3 ,tgg * 1000 , 198mb / (tgg * 1e3 ) /1e3 ,tc * 1000 ,mb / (tc * 1e3 ) /1e3 ,mb ); 199 std::printf (" | quantize %6.1f | gu %7.1f | swiglu %5.1f | quantH %5.1f | dn %7.1f us" 200" (sum %.1f us)\n" ,tq * 1000 ,tgu * 1000 ,ts * 1000 ,th * 1000 ,td * 1000 , 201 (tq + tgu + ts + th + td )* 1000 ); 202for (void * p : {c .dgu ,c .ddn ,c .ggu ,c .gdn ,c .dxl ,c .dxq ,c .dgo ,c .dho ,c .dhq ,c .ddo ,c .db ,c .dids }) 203cudaFree (p ); 204 } 205return 0 ; 206}