okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b5abd7792
main
1// sycl/src/kernels/dflash2_bench.cpp - DF2-SYCL: what one DFlash2 drafter round costs on an Intel Arc (the Q4_0 pack's real shapes), and checks of the SYCL kernels. 2// dflash2_bench [test|bench] (default bench) 3// env DF2B_POS (ring position, default 600), DF2B_ITERS (default 100), DF2B_NNEW (context rows, default 3), DF2B_ND (drafts, 1..7, default 7), 4// DF2B_STANDIN_MB (a memory-bound kernel run before each timed round, default 0), DF2B_GAPMS (host gap between rounds, default 0), 5// DF2B_PROFILE=1 (a marker after every launch: ms per kernel; direct launches only), DF2B_GRAPH=0 (skip the sycl graph replay) 6// One round = the context update for n_new rows (fc 12800 -> 2560, per layer K/V projections, norm + rope, ring writes) + the 8-row block (5 layers: dynamic 7// conv projections, q/k/v, norm + rope, windowed attention over the ring, o, conv + rms, gate/up, swiglu, down) + the head over the draft vocabulary 8// (nd rows, Q8_0 rows through the engine's native_mmvq) + top-16 + the candidate selector. The weights are random bytes in the pack's real sizes 9// (tools/dflash2_pack.py --q4_0: 268 MB of Q4_0 matrices + 254 MB of bf16 selector codebooks); the embedding is a small table gather. 10#include "strata/kernels/dflash2.hpp" 11#include "strata/kernels/native_mmvq.hpp" 12#include "strata/sycl_queue.hpp" 13 14#include <sycl/sycl.hpp> 15#include <dpct/dpct.hpp> 16 17#include <algorithm> 18#include <chrono> 19#include <cmath> 20#include <cstdint> 21#include <cstdio> 22#include <cstdlib> 23#include <cstring> 24#include <string> 25#include <thread> 26#include <vector> 27 28using namespace strata::kernels ; 29namespace sx= sycl::ext::oneapi::experimental; 30 31namespace { 32constexpr int N = 2560 ,L = 5 ,I = 7680 ,NH = 32 ,NKV = 8 ,HD = 128 ,B = 8 ,RING = 2064 ,WINDOW = 2048 ,NDV = 106299 ,RANK = 256 ,VOCAB = 248320 ,GROUP = 16 ; 33constexpr float EPS = 1e-6f ; 34constexpr int GGML_Q8_0 = 8 ; 35 36uint32_t rs = 12345 ; 37uint32_t rnd () {rs = rs * 1664525u + 1013904223u ;return rs >>8 ; } 38float frand () {return (float ) (rnd ()& 0xffff ) /65536.0f - 0.5f ; } 39uint16_t f2h (float f ) { 40uint32_t x ; std::memcpy (& x ,& f ,4 ); 41const uint32_t s = (x >>16 )& 0x8000 ,e = ((x >>23 )& 0xff ),m = x & 0x7fffff ; 42if (e < 103 )return (uint16_t )s ; 43if (e > 142 )return (uint16_t ) (s |0x7c00 ); 44if (e < 113 ) {const uint32_t mm = (m |0x800000 ) >> (126 - e );return (uint16_t ) (s | (mm >>13 )); } 45return (uint16_t ) (s | ((e - 112 ) <<10 ) | (m >>13 )); 46} 47float h2f (uint16_t h ) { 48const uint32_t s = (h & 0x8000u ) <<16 ,e = (h >>10 )& 31 ,m = h & 1023 ; 49uint32_t x ; 50if (e == 0 ) {if (m == 0 )x = s ;else {float f = m /1024.0f /16384.0f ; std::memcpy (& x ,& f ,4 );x |=s ; } } 51else x = s | ((e + 112 ) <<23 ) | (m <<13 ); 52float f ; std::memcpy (& f ,& x ,4 );return f ; 53} 54double med (std::vector < double > v ) { std::sort (v .begin (),v .end ());return v [v .size () /2 ]; } 55double pct (std::vector < double > v ,double p ) { std::sort (v .begin (),v .end ());return v [std::min (v .size ()- 1 , (size_t ) (p * v .size ()))]; } 56int envi (const char * k ,int d ) {const char * e = std::getenv (k );return e && e [0 ] ? std::atoi (e ) :d ; } 57double envd (const char * k ,double d ) {const char * e = std::getenv (k );return e && e [0 ] ? std::atof (e ) :d ; } 58using Clock = std::chrono::steady_clock ; 59double ms_since (Clock ::time_point t ) {return std::chrono::duration < double , std::milli > (Clock ::now ()- t ).count (); } 60 61sycl::queue * Q = nullptr ; 62template < class T > T * dev (size_t n ) {T * p = sycl::malloc_device < T > (n ,* Q );if (!p ) { std::fprintf (stderr ,"out of device memory (%zu bytes)\n" ,n * sizeof (T )); std::exit (2 ); }return p ; } 63template < class T > void up (T * d ,const std::vector < T >& h ) {Q -> memcpy (d ,h .data (),h .size ()* sizeof (T )).wait (); } 64template < class T > std::vector < T > down (const T * d ,size_t n ) { std::vector < T > h (n );Q -> memcpy (h .data (),d ,n * sizeof (T )).wait ();return h ; } 65 66// A Q4_0 matrix in the tiled layout: n_out * n_in / 2 random bytes (any byte is two valid nibbles) then the fp16 scale plane (n_out * n_in / 32 random small halves). 67struct Mat4 {uint8_t * w ;int n_out ,n_in ; }; 68Mat4 make_mat (int n_out ,int n_in ) { 69const size_t nib = (size_t )n_out * n_in /2 ,nsc = (size_t )n_out * n_in /32 ; 70 std::vector < uint8_t > h (nib + nsc * 2 ); 71for (size_t i = 0 ;i < nib ;++ i )h [i ]= (uint8_t )rnd (); 72for (size_t i = 0 ;i < nsc ;++ i ) {const uint16_t d = (uint16_t ) (0x1800 + (rnd () %0xC00 )); std::memcpy (& h [nib + i * 2 ],& d ,2 ); } 73uint8_t * d = dev < uint8_t > (h .size ()); 74Q -> memcpy (d ,h .data (),h .size ()).wait (); 75return Mat4 {d ,n_out ,n_in }; 76} 77float * make_f32 (size_t n ,float scale ,float off = 0.f ) { 78 std::vector < float > h (n ); 79for (auto & v :h )v = off + scale * frand (); 80float * d = dev < float > (n ); 81Q -> memcpy (d ,h .data (),n * 4 ).wait (); 82return d ; 83} 84 85struct Layer {Mat4 q ,k ,v ,o ,gate ,up ,down ,akp ,mkp ;float * base_a ,* base_m ,* ln_in ,* ln_post ,* qn ,* kn ; }; 86 87struct Drafter { 88 std::vector < Layer > ly ; 89Mat4 fc ,hsel ; 90float * hidden_norm ,* final_norm ; 91uint16_t * pred_cb ,* succ_cb ; 92uint8_t * head_w ;int32_t * dvocab ; 93float * emb_tab ; 94// buffers 95int32_t * d_meta ;int32_t * h_meta ;int32_t * h_out ;float * h_prob ;int32_t * d_out ; 96float * kring [L ],* vring [L ];int64_t * tag ;float * rt_ctx ;float * rt_blk ; 97uint8_t * xq_head ,* xqp ,* xqc ;float * xdp ,* xdc ; 98float * taps ,* fcy ,* kc ,* vc ,* h ,* xn ,* dyn ,* qv ,* qn ,* kv ,* vv ,* kblk ,* ao ,* o ,* gate ,* up ,* mlp ,* part ,* fn ,* logits ,* topval ,* hsel_out ; 99int32_t * toptok ;uint32_t * topscratch ; 100int nd = 7 ; 101 std::vector < std::pair < std::string , sycl::event >> marks ; 102bool profile = false; 103void stamp (const char * name ) {if (profile )marks .emplace_back (name ,Q -> single_task ([] {})); } 104}; 105 106uint8_t * make_head () {// Q8_0 rows: 34 bytes a block (fp16 d, 32 int8), NDV rows of N 107const size_t blocks = (size_t )NDV * (N /32 ); 108 std::vector < uint8_t > h (blocks * 34 ); 109for (size_t b = 0 ;b < blocks ;++ b ) { 110const uint16_t d = f2h (0.002f + 0.0004f * (float ) (rnd () %10 )); 111 std::memcpy (& h [b * 34 ],& d ,2 ); 112for (int i = 0 ;i < 32 ;++ i )h [b * 34 + 2 + i ]= (uint8_t )rnd (); 113 } 114uint8_t * d = dev < uint8_t > (h .size ()); 115Q -> memcpy (d ,h .data (),h .size ()).wait (); 116return d ; 117} 118 119void build (Drafter & D ) { 120D .ly .resize (L ); 121for (int l = 0 ;l < L ;++ l ) { 122Layer & y = D .ly [l ]; 123y .q = make_mat (NH * HD ,N );y .k = make_mat (NKV * HD ,N );y .v = make_mat (NKV * HD ,N );y .o = make_mat (N ,NH * HD ); 124y .gate = make_mat (I ,N );y .up = make_mat (I ,N );y .down = make_mat (N ,I );y .akp = make_mat (640 ,N );y .mkp = make_mat (640 ,N ); 125y .base_a = make_f32 ((size_t )4 * N ,0.2f ,0.5f );y .base_m = make_f32 ((size_t )4 * N ,0.2f ,0.5f ); 126y .ln_in = make_f32 (N ,0.2f ,1.f );y .ln_post = make_f32 (N ,0.2f ,1.f );y .qn = make_f32 (HD ,0.2f ,1.f );y .kn = make_f32 (HD ,0.2f ,1.f ); 127 } 128D .fc = make_mat (N ,5 * N ); 129D .hsel = make_mat (RANK ,N ); 130D .hidden_norm = make_f32 (N ,0.2f ,1.f );D .final_norm = make_f32 (N ,0.2f ,1.f ); 131 { 132 std::vector < uint16_t > cb ((size_t )VOCAB * RANK ); 133for (auto & v :cb )v = (uint16_t ) (0x3c00 + (rnd ()& 0x1ff )- 0x100 - 0x300 );// bf16 near 2^-4 .. 2^-3, either sign mixed below 134for (size_t i = 0 ;i < cb .size ();i += 5 )cb [i ] |=0x8000 ; 135D .pred_cb = dev < uint16_t > (cb .size ());up (D .pred_cb ,cb ); 136for (size_t i = 0 ;i < cb .size ();i += 3 )cb [i ] ^=0x8000 ; 137D .succ_cb = dev < uint16_t > (cb .size ());up (D .succ_cb ,cb ); 138 } 139D .head_w = make_head (); 140 { 141 std::vector < int32_t > v (NDV ); 142for (int i = 0 ;i < NDV ;++ i )v [i ]= (int32_t ) (((int64_t )i * 2654435761ll ) %VOCAB );// draft vocabulary -> token id 143D .dvocab = dev < int32_t > (NDV );up (D .dvocab ,v ); 144 } 145D .emb_tab = make_f32 ((size_t )64 * N ,1.0f ); 146D .d_meta = dev < int32_t > (32 + 2048 );// 32 ints, then the rope tables 147D .h_meta = (int32_t * ) strata::host_malloc_polled ((32 + 2048 )* 4 ,* Q );// 32 ints, then the rope tables of the 16 rows (host-computed: no device FP64) 148D .h_out = (int32_t * ) strata::host_malloc_polled (64 * 4 ,* Q ); 149 std::memset (D .h_meta ,0 , (32 + 2048 )* 4 ); std::memset (D .h_out ,0 ,256 ); 150D .h_prob = (float * )D .h_out + 16 ; 151D .d_out = dev < int32_t > (64 );Q -> memset (D .d_out ,0 ,256 ).wait (); 152for (int l = 0 ;l < L ;++ l ) {D .kring [l ]= make_f32 ((size_t )RING * NKV * HD ,2.f );D .vring [l ]= make_f32 ((size_t )RING * NKV * HD ,2.f ); } 153D .tag = dev < int64_t > (RING ); 154D .rt_ctx = (float * ) (D .d_meta + 32 ); 155D .rt_blk = D .rt_ctx + B * 128 ; 156D .xq_head = dev < uint8_t > (native_q8_1_bytes (N ,B )+ 4096 ); 157D .xqp = dev < uint8_t > ((size_t )5 * N * 8 + 4096 );D .xdp = dev < float > ((size_t )5 * N /32 * 8 + 64 ); 158D .xqc = dev < uint8_t > ((size_t )5 * N * 8 + 4096 );D .xdc = dev < float > ((size_t )5 * N /32 * 8 + 64 ); 159Q -> memset (D .xqp ,0 , (size_t )5 * N * 8 + 4096 );Q -> memset (D .xdp ,0 , ((size_t )5 * N /32 * 8 + 64 )* 4 ); 160Q -> memset (D .xqc ,0 , (size_t )5 * N * 8 + 4096 );Q -> memset (D .xdc ,0 , ((size_t )5 * N /32 * 8 + 64 )* 4 ); 161D .taps = make_f32 ((size_t )B * 5 * N ,1.0f ); 162D .fcy = dev < float > ((size_t )B * N );D .kc = dev < float > ((size_t )B * NKV * HD );D .vc = dev < float > ((size_t )B * NKV * HD ); 163D .h = dev < float > ((size_t )B * N );D .xn = dev < float > ((size_t )B * N );D .dyn = dev < float > ((size_t )B * 640 ); 164D .qv = dev < float > ((size_t )B * NH * HD );D .qn = dev < float > ((size_t )B * NH * HD );D .kv = dev < float > ((size_t )B * NKV * HD );D .vv = dev < float > ((size_t )B * NKV * HD ); 165D .kblk = dev < float > ((size_t )B * NKV * HD );D .ao = dev < float > ((size_t )B * NH * HD );D .o = dev < float > ((size_t )B * N ); 166D .gate = dev < float > ((size_t )B * I );D .up = dev < float > ((size_t )B * I );D .mlp = dev < float > ((size_t )B * N ); 167D .part = dev < float > ((size_t )kDf2PartChunks * 8 * 32 * 130 );D .fn = dev < float > ((size_t )B * N ); 168D .logits = dev < float > ((size_t )7 * NDV );D .topval = dev < float > (7 * 16 );D .toptok = dev < int32_t > (7 * 16 );D .hsel_out = dev < float > (7 * RANK ); 169D .topscratch = dev < uint32_t > ((size_t )7 * kDf2TopBlocks * kDf2TopK * 2 ); 170Q -> memset (D .tag ,0xff ,RING * 8 ).wait (); 171Q -> wait (); 172} 173 174void reset_ring (Drafter & D ,int pos ) { 175 std::vector < int64_t > t (RING ,-1 ); 176for (int p = std::max (0 ,pos - (RING - 16 ));p < pos ;++ p )t [p %RING ]= ((int64_t )1 <<32 ) |p ; 177Q -> memcpy (D .tag ,t .data (),RING * 8 ).wait (); 178} 179 180void set_meta (Drafter & D ,int n_new ,int64_t pos0 ,int anchor ) { 181const int64_t s = pos0 + n_new ; 182for (int i = 0 ;i < 8 ;++ i ) {D .h_meta [i ]= (int32_t ) (pos0 + std::min (i ,n_new - 1 ));D .h_meta [8 + i ]= (int32_t ) (s + i ); } 183D .h_meta [16 ]= anchor ;D .h_meta [17 ]= (int32_t )s ;D .h_meta [18 ]= 1 ;D .h_meta [24 ]= anchor ; 184for (int i = 1 ;i < 8 ;++ i )D .h_meta [24 + i ]= 7 ; 185df2_rope_table_cpu (D .h_meta ,16 ,10000.0f , (float * ) (D .h_meta + 32 )); 186for (int i = 0 ;i < 16 ;++ i ) ((volatile int32_t * )D .h_out )[i ]= -1 ; 187 std::atomic_thread_fence (std::memory_order_seq_cst ); 188} 189 190void gemm1 (Drafter & ,const Mat4 & m ,float * y ,int nc ,const uint8_t * xq ,const float * xd ) { 191Df2Mat mats [1 ]= {Df2Mat {m .w ,y ,m .n_out , true}}; 192df2_gemm (mats ,1 ,xq ,xd ,m .n_in ,nc ,Q ); 193} 194void gemm2 (const Mat4 & a ,const Mat4 & b ,float * ya ,float * yb ,int nc ,const uint8_t * xq ,const float * xd ) { 195Df2Mat mats [2 ]= {Df2Mat {a .w ,ya ,a .n_out , true},Df2Mat {b .w ,yb ,b .n_out , true}}; 196df2_gemm (mats ,2 ,xq ,xd ,a .n_in ,nc ,Q ); 197} 198void gemm3 (const Mat4 & a ,const Mat4 & b ,const Mat4 & c ,float * ya ,float * yb ,float * yc ,int nc ,const uint8_t * xq ,const float * xd ) { 199Df2Mat mats [3 ]= {Df2Mat {a .w ,ya ,a .n_out , true},Df2Mat {b .w ,yb ,b .n_out , true},Df2Mat {c .w ,yc ,c .n_out , true}}; 200df2_gemm (mats ,3 ,xq ,xd ,a .n_in ,nc ,Q ); 201} 202 203bool g_io = true; 204int g_meta_kernel = 0 ;// DF2B_METAKERNEL=1: a kernel copies the meta from host memory 205int g_host_out = 0 ;// DF2B_HOSTOUT=1: the select kernel writes the host buffer itself 206std::string g_dup ;// DF2B_DUP=name: every launch stamped `name` runs twice (the marginal cost of that kernel in the round) 207int g_dupn = 1 ;// DF2B_DUPN: how many extra launches of the DF2B_DUP kernel (layer 0 only when > 1) 208int g_layer = 0 ; 209#define DO (name ,call ) do { call; if (!g_dup.empty() && g_dup == name && (g_dupn == 1 || g_layer == 0)) { for (int rep = 0; rep < g_dupn; ++rep) { call; } } D.stamp(name); } while (0) 210void record_context (Drafter & D ,int n ) { 211const int tap_n = 5 * N ; 212DO ("quant_rows" ,df2_quant_rows (D .taps ,tap_n ,n ,D .xqc ,D .xdc ,Q )); 213DO ("gemm:fc" ,gemm1 (D ,D .fc ,D .fcy ,n ,D .xqc ,D .xdc )); 214DO ("rms_quant" ,df2_rms_quant (D .fcy ,D .hidden_norm ,nullptr ,n ,N ,EPS ,D .xqc ,D .xdc ,Q )); 215for (int l = 0 ;l < L ;++ l ) { 216DO ("gemm:kp" ,gemm2 (D .ly [l ].k ,D .ly [l ].v ,D .kc ,D .vc ,n ,D .xqc ,D .xdc )); 217DO ("kv_ring" ,df2_kv_ring (D .kc ,D .vc ,D .ly [l ].kn ,D .d_meta + 0 ,D .rt_ctx ,D .kring [l ],D .vring [l ],RING ,n ,NKV ,EPS ,Q ,D .tag ,D .d_meta + 18 )); 218 } 219} 220 221// the stand-in for the engine's embedding gather: 8 rows of a small table 222void embed_stub (Drafter & D ) { 223const float * tab = D .emb_tab ;const int32_t * tok = D .d_meta + 24 ;float * out = D .h ; 224Q -> parallel_for (sycl::nd_range < 1 > ((size_t )B * N ,256 ), [= ](sycl::nd_item < 1 > it ) { 225const int i = (int )it .get_global_id (0 ),r = i /N ,c = i %N ; 226out [i ]= tab [(size_t ) (tok [r ]& 63 )* N + c ]; 227 }); 228} 229 230void record_block (Drafter & D ) { 231DO ("embed" ,embed_stub (D )); 232DO ("rms_quant" ,df2_rms_quant (D .h ,D .ly [0 ].ln_in ,D .xn ,B ,N ,EPS ,D .xqp ,D .xdp ,Q )); 233for (int l = 0 ;l < L ;++ l ) { 234Layer & y = D .ly [l ];g_layer = l ; 235DO ("gemm:akp" ,gemm1 (D ,y .akp ,D .dyn ,B ,D .xqp ,D .xdp )); 236DO ("conv_quant" ,df2_conv_quant (D .xn ,D .dyn ,640 ,y .base_a ,B ,N ,GROUP ,D .xqp ,D .xdp ,Q )); 237DO ("gemm:qkv" ,gemm3 (y .q ,y .k ,y .v ,D .qv ,D .kv ,D .vv ,B ,D .xqp ,D .xdp )); 238DO ("norm_rope_qk" ,df2_norm_rope_qk (D .qv ,D .kv ,y .qn ,y .kn ,D .rt_blk ,D .qn ,D .kblk ,B ,NH ,NKV ,EPS ,Q )); 239DO ("attention" ,df2_attention (D .qn ,D .kring [l ],D .vring [l ],D .kblk ,D .vv ,D .d_meta + 17 ,RING ,WINDOW ,D .part ,D .xqp ,D .xdp ,Q ,D .tag ,D .d_meta + 18 )); 240DO ("gemm:o" ,gemm1 (D ,y .o ,D .o ,B ,D .xqp ,D .xdp )); 241DO ("conv_rms_quant" ,df2_conv_rms_quant (D .o ,D .dyn + 320 ,640 ,y .base_a + 2 * N ,D .h ,y .ln_post ,D .xn ,B ,N ,GROUP ,EPS ,D .xqp ,D .xdp ,Q )); 242DO ("gemm:mkp" ,gemm1 (D ,y .mkp ,D .dyn ,B ,D .xqp ,D .xdp )); 243DO ("conv_quant" ,df2_conv_quant (D .xn ,D .dyn ,640 ,y .base_m ,B ,N ,GROUP ,D .xqp ,D .xdp ,Q )); 244DO ("gemm:gate_up" ,gemm2 (y .gate ,y .up ,D .gate ,D .up ,B ,D .xqp ,D .xdp )); 245DO ("swiglu_quant" ,df2_swiglu_quant (D .gate ,D .up ,B ,I ,D .xqp ,D .xdp ,Q )); 246DO ("gemm:down" ,gemm1 (D ,y .down ,D .mlp ,B ,D .xqp ,D .xdp )); 247const float * next_gamma = l + 1 < L ?D .ly [l + 1 ].ln_in :D .final_norm ; 248DO ("conv_rms_quant" ,df2_conv_rms_quant (D .mlp ,D .dyn + 320 ,640 ,y .base_m + 2 * N ,D .h ,next_gamma ,l + 1 < L ?D .xn :D .fn ,B ,N ,GROUP ,EPS ,D .xqp ,D .xdp ,Q )); 249 } 250DO ("quant_q8_1" ,native_quantize_q8_1 (D .fn + N ,D .xq_head ,N ,D .nd ,Q )); 251DO ("quant_rows" ,df2_quant_rows (D .fn + N ,N ,D .nd ,D .xqp ,D .xdp ,Q )); 252DO ("head" ,native_mmvq (GGML_Q8_0 ,D .head_w ,D .xq_head ,D .logits ,N ,NDV ,D .nd ,Q )); 253DO ("topk" ,df2_topk (D .logits ,NDV ,D .dvocab ,D .nd ,D .topscratch ,D .topval ,D .toptok ,Q )); 254DO ("gemm:hsel" ,gemm1 (D ,D .hsel ,D .hsel_out ,D .nd ,D .xqp ,D .xdp )); 255DO ("select" ,df2_select (D .hsel_out ,D .topval ,D .toptok ,D .pred_cb ,D .succ_cb ,RANK ,D .nd ,D .d_meta + 16 ,g_host_out ?D .h_out :D .d_out ,g_host_out ? (float * )D .h_out + 16 : (float * )D .d_out + 16 ,Q )); 256if (g_host_out == 0 && g_io )DO ("d2h" ,Q -> memcpy (D .h_out ,D .d_out ,64 * 4 )); 257} 258 259void record_round (Drafter & D ,int n_new ,bool io = true) { 260g_io = io ; 261// meta + rope tables to the device with one copy (no kernel touches host memory: a kernel that reads or writes host USM slows every later kernel of the process on the A750) 262if (!io ) { 263 }else if (g_meta_kernel ) { 264const int32_t * hm = D .h_meta ;int32_t * dm = D .d_meta ; 265Q -> parallel_for (sycl::range < 1 > (32 + 2048 ), [= ](sycl::id < 1 > i ) {dm [i ]= hm [i ]; }); 266 }else { 267Q -> memcpy (D .d_meta ,D .h_meta , (32 + 2048 )* 4 ); 268 } 269D .stamp ("meta_rope" ); 270record_context (D ,n_new ); 271record_block (D ); 272} 273 274uint64_t checksum (const void * p ,size_t n ) {uint64_t h = 1469598103934665603ull ;for (size_t i = 0 ;i < n ;++ i )h = (h ^ ((const uint8_t * )p )[i ])* 1099511628211ull ;return h ; } 275 276// ---------------------------------------------------------------- tests 277int g_fail = 0 ; 278void check (bool ok ,const char * what ) { std::printf (" %-64s %s\n" ,what ,ok ?"ok" :"FAIL" );if (!ok )++ g_fail ; } 279 280// CPU reference of the Q4_0 GEMM, bitwise (the lane partial sums over the steps in order, fma, the xor shuffle tree) 281void gemm_ref (const std::vector < uint8_t >& w ,int n_out ,int n_in ,const std::vector < int8_t >& xq ,const std::vector < float >& xd ,int nc , std::vector < float >& y ) { 282const int nb = n_in /32 ,steps = nb /16 ; 283const size_t nib = (size_t )n_out * n_in /2 ; 284y .assign ((size_t )nc * n_out ,0.f ); 285for (int row = 0 ;row < n_out ;++ row ) { 286const int g = row /8 ,rr = row %8 ; 287for (int c = 0 ;c < nc ;++ c ) { 288float lane [32 ]; 289for (int l = 0 ;l < 32 ;++ l ) { 290float acc = 0.f ; 291for (int st = 0 ;st < steps ;++ st ) { 292const uint8_t * wp = & w [(size_t )g * 8 * (n_in /2 )+ (size_t )st * 2048 + (size_t )rr * 256 + (size_t )l * 8 ]; 293uint32_t rx ,ry ; std::memcpy (& rx ,wp ,4 ); std::memcpy (& ry ,wp + 4 ,4 ); 294const uint32_t ws [4 ]= {rx & 0x0F0F0F0Fu , (rx >>4 )& 0x0F0F0F0Fu ,ry & 0x0F0F0F0Fu , (ry >>4 )& 0x0F0F0F0Fu }; 295const int8_t * xp = & xq [(size_t )c * n_in + (size_t )st * 512 + (size_t )l * 16 ]; 296int isum = 0 ,xsum = 0 ; 297for (int k = 0 ;k < 4 ;++ k ) 298for (int b = 0 ;b < 4 ;++ b ) {isum += (int ) ((ws [k ] >> (8 * b ))& 0xff )* (int )xp [k * 4 + b ];xsum += (int )xp [k * 4 + b ]; } 299uint16_t dh ; std::memcpy (& dh ,& w [nib + ((size_t )g * 8 * nb + (size_t )st * 128 + rr * 16 + (l >>1 ))* 2 ],2 ); 300const float dw = h2f (dh ),xs = xd [(size_t )c * nb + st * 16 + (l >>1 )]; 301const float prod = dw * xs ; 302acc = std::fmaf (prod , (float ) (isum - 8 * xsum ),acc ); 303 } 304lane [l ]= acc ; 305 } 306for (int o = 16 ;o > 0 ;o >>=1 ) {float nl [32 ];for (int l = 0 ;l < 32 ;++ l )nl [l ]= lane [l ]+ lane [l ^o ]; std::memcpy (lane ,nl ,sizeof lane ); } 307y [(size_t )c * n_out + row ]= lane [0 ]; 308 } 309 } 310} 311 312 313// DF-04: the head, top-k and selector for the first nd rows give exactly the first nd rows of the 7-row path (distinct random rows, so the picks differ per row) 314void prefix_test () { 315 std::printf ("DF-04 prefix: head / top-k / selector for nd rows == the first nd rows of the 7-row path (bitwise)\n" ); 316uint8_t * head_w = make_head (); 317 std::vector < int32_t > vv (NDV );for (int i = 0 ;i < NDV ;++ i )vv [i ]= (int32_t ) (((int64_t )i * 2654435761ll ) %VOCAB ); 318int32_t * dvocab = dev < int32_t > (NDV );up (dvocab ,vv ); 319 std::vector < uint16_t > cb ((size_t )VOCAB * RANK ); 320for (auto & v :cb )v = (uint16_t ) (0x3c00 + (rnd ()& 0x1ff )- 0x400 );for (size_t i = 0 ;i < cb .size ();i += 5 )cb [i ] |=0x8000 ; 321uint16_t * pred = dev < uint16_t > (cb .size ());up (pred ,cb ); 322for (size_t i = 0 ;i < cb .size ();i += 3 )cb [i ] ^=0x8000 ; 323uint16_t * succ = dev < uint16_t > (cb .size ());up (succ ,cb ); 324Mat4 hsel = make_mat (RANK ,N ); 325float * fn = make_f32 ((size_t )B * N ,1.0f ); 326uint8_t * xq_head = dev < uint8_t > (native_q8_1_bytes (N ,B )+ 4096 ); 327uint8_t * xqp = dev < uint8_t > ((size_t )5 * N * 8 + 4096 );float * xdp = dev < float > ((size_t )5 * N /32 * 8 + 64 ); 328float * logits = dev < float > ((size_t )7 * NDV );float * hs = dev < float > (7 * RANK );float * tv = dev < float > (7 * 16 );int32_t * tt = dev < int32_t > (7 * 16 ); 329uint32_t * sc = dev < uint32_t > ((size_t )7 * kDf2TopBlocks * kDf2TopK * 2 ); 330int32_t * anchor = dev < int32_t > (1 );int32_t a5 = 777 ;Q -> memcpy (anchor ,& a5 ,4 ).wait (); 331int32_t * otok = dev < int32_t > (8 );float * oprob = dev < float > (8 ); 332struct Out { std::vector < float > lg ,hs ,tv ; std::vector < int32_t > tt ,ot ; std::vector < float > op ; }; 333auto run = [& ](int nd ) { 334native_quantize_q8_1 (fn + N ,xq_head ,N ,nd ,Q ); 335df2_quant_rows (fn + N ,N ,nd ,xqp ,xdp ,Q ); 336native_mmvq (GGML_Q8_0 ,head_w ,xq_head ,logits ,N ,NDV ,nd ,Q ); 337df2_topk (logits ,NDV ,dvocab ,nd ,sc ,tv ,tt ,Q ); 338 {Df2Mat m1 [1 ]= {Df2Mat {hsel .w ,hs ,hsel .n_out , true}};df2_gemm (m1 ,1 ,xqp ,xdp ,hsel .n_in ,nd ,Q ); } 339df2_select (hs ,tv ,tt ,pred ,succ ,RANK ,nd ,anchor ,otok ,oprob ,Q ); 340Q -> wait (); 341Out o ;o .lg = down (logits , (size_t )nd * NDV );o .hs = down (hs , (size_t )nd * RANK );o .tv = down (tv , (size_t )nd * 16 );o .tt = down (tt , (size_t )nd * 16 ); 342o .ot = down (otok ,nd );o .op = down (oprob ,nd ); 343return o ; 344 }; 345const Out full = run (7 ); 346bool distinct = false;for (int i = 1 ;i < 7 ;++ i )distinct |=full .ot [i ]!= full .ot [0 ]; 347check (distinct ,"the 7 picks of the reference differ (the test can tell rows apart)" ); 348for (int nd : {5 ,3 ,1 }) { 349const Out o = run (nd ); 350char buf [96 ]; 351bool ok = std::memcmp (o .lg .data (),full .lg .data (),o .lg .size ()* 4 )== 0 && std::memcmp (o .hs .data (),full .hs .data (),o .hs .size ()* 4 )== 0 && 352 std::memcmp (o .tv .data (),full .tv .data (),o .tv .size ()* 4 )== 0 && std::memcmp (o .tt .data (),full .tt .data (),o .tt .size ()* 4 )== 0 && 353 std::memcmp (o .ot .data (),full .ot .data (),o .ot .size ()* 4 )== 0 && std::memcmp (o .op .data (),full .op .data (),o .op .size ()* 4 )== 0 ; 354 std::snprintf (buf ,sizeof buf ,"nd = %d: logits, selector projection, top-16, picks, probabilities == first %d rows of nd = 7" ,nd ,nd ); 355check (ok ,buf ); 356 } 357} 358 359int run_tests () { 360 std::printf ("device: %s\n" ,Q -> get_device ().get_info < sycl::info::device::name > ().c_str ()); 361struct Shape {const char * name ;int n_out ,n_in ,nmat ; }; 362const Shape shapes []= {{"fc" ,N ,5 * N ,1 }, {"qkv(3 matrices)" ,NH * HD ,N ,3 }, {"o" ,N ,NH * HD ,1 }, {"gate+up(2)" ,I ,N ,2 }, {"down" ,N ,I ,1 }, {"akp" ,640 ,N ,1 }, {"hsel" ,RANK ,N ,1 }}; 363const int variants []= {17 ,28 ,12 ,11 ,10 ,24 ,25 ,34 ,40 ,41 ,42 ,43 }; 364 std::printf ("Q4_0 GEMM against the CPU reference (bitwise), every variant, columns 1..8; and the first n columns of the 8-column launch against the n-column launch (DF-03)\n" ); 365for (const Shape & s :shapes ) { 366const int n_in = s .n_in ; 367 std::vector < Mat4 > mats ; 368 std::vector < std::vector < uint8_t >> host ; 369 std::vector < float *> ys ; 370for (int m = 0 ;m < s .nmat ;++ m ) {mats .push_back (make_mat (s .n_out ,n_in ));host .push_back (down (mats .back ().w , (size_t )s .n_out * n_in /2 + (size_t )s .n_out * n_in /16 ));ys .push_back (dev < float > ((size_t )8 * s .n_out )); } 371 std::vector < int8_t > xq ((size_t )8 * n_in ); 372 std::vector < float > xd ((size_t )8 * n_in /32 ); 373for (auto & v :xq )v = (int8_t ) (rnd () %255 - 127 ); 374for (auto & v :xd )v = 0.001f + 0.00002f * (float ) (rnd () %100 ); 375int8_t * dxq = dev < int8_t > (xq .size ());float * dxd = dev < float > (xd .size ()); 376Q -> memcpy (dxq ,xq .data (),xq .size ()).wait ();Q -> memcpy (dxd ,xd .data (),xd .size ()* 4 ).wait (); 377bool all_ref = true,all_nc = true; 378 std::vector < std::vector < float >> ref (s .nmat ); 379for (int m = 0 ;m < s .nmat ;++ m )gemm_ref (host [m ],s .n_out ,n_in ,xq ,xd ,8 ,ref [m ]); 380for (int v :variants ) { 381df2_gemm_variant (v ); 382 std::vector < std::vector < float >> full (s .nmat ); 383for (int nc = 8 ;nc >=1 ;-- nc ) { 384 std::vector < Df2Mat > dm ; 385for (int m = 0 ;m < s .nmat ;++ m ) {Q -> memset (ys [m ],0 , (size_t )8 * s .n_out * 4 );dm .push_back (Df2Mat {mats [m ].w ,ys [m ],s .n_out , true}); } 386df2_gemm (dm .data (),s .nmat ,dxq ,dxd ,n_in ,nc ,Q ); 387Q -> wait (); 388for (int m = 0 ;m < s .nmat ;++ m ) { 389auto y = down (ys [m ], (size_t )nc * s .n_out ); 390if (nc == 8 ) {full [m ]= y ;all_ref &= std::memcmp (y .data (),ref [m ].data (),y .size ()* 4 )== 0 ; } 391else all_nc &= std::memcmp (y .data (),full [m ].data (),y .size ()* 4 )== 0 ; 392 } 393 } 394 } 395df2_gemm_variant (0 ); 396char buf [160 ]; 397 std::snprintf (buf ,sizeof buf ,"%s (%d x %d): 8 variants x 8 NC vs CPU" ,s .name ,s .n_out ,n_in );check (all_ref ,buf ); 398 std::snprintf (buf ,sizeof buf ,"%s: NC < 8 == first NC columns of NC = 8" ,s .name );check (all_nc ,buf ); 399 } 400// top-16 against the CPU (value descending, ties to the lower index) 401 { 402const int rows = 7 ; 403 std::vector < float > lg ((size_t )rows * NDV ); 404for (auto & v :lg )v = (float ) (rnd () %20000 ) /100.f ;// many ties 405float * dl = dev < float > (lg .size ());up (dl ,lg ); 406int32_t * dmap = dev < int32_t > (NDV ); std::vector < int32_t > mp (NDV );for (int i = 0 ;i < NDV ;++ i )mp [i ]= i * 2 ;up (dmap ,mp ); 407uint32_t * sc = dev < uint32_t > ((size_t )rows * kDf2TopBlocks * kDf2TopK * 2 );float * tv = dev < float > (rows * 16 );int32_t * tt = dev < int32_t > (rows * 16 ); 408df2_topk (dl ,NDV ,dmap ,rows ,sc ,tv ,tt ,Q );Q -> wait (); 409auto hv = down (tv ,rows * 16 );auto ht = down (tt ,rows * 16 ); 410bool ok = true; 411for (int r = 0 ;r < rows ;++ r ) { 412 std::vector < int > idx (NDV );for (int i = 0 ;i < NDV ;++ i )idx [i ]= i ; 413 std::partial_sort (idx .begin (),idx .begin ()+ 16 ,idx .end (), [& ](int a ,int b ) {const float va = lg [(size_t )r * NDV + a ],vb = lg [(size_t )r * NDV + b ];return va > vb || (va == vb && a < b ); }); 414for (int k = 0 ;k < 16 ;++ k )ok &=hv [r * 16 + k ]== lg [(size_t )r * NDV + idx [k ]]&& ht [r * 16 + k ]== idx [k ]* 2 ; 415 } 416check (ok ,"top-16 of 7 x 106299 logits (ties) == CPU, with the id map" ); 417 } 418// quantize rows against the CPU (exact) 419 { 420const int K = N ,rows = 5 ; 421 std::vector < float > x ((size_t )rows * K );for (auto & v :x )v = 3.f * frand (); 422float * dx = dev < float > (x .size ());up (dx ,x ); 423int8_t * dq = dev < int8_t > ((size_t )rows * K );float * dd = dev < float > ((size_t )rows * K /32 ); 424df2_quant_rows (dx ,K ,rows ,dq ,dd ,Q );Q -> wait (); 425auto hq = down (dq , (size_t )rows * K );auto hd = down (dd , (size_t )rows * K /32 ); 426bool ok = true; 427for (int r = 0 ;r < rows ;++ r ) 428for (int b = 0 ;b < K /32 ;++ b ) { 429float amax = 0 ;for (int i = 0 ;i < 32 ;++ i )amax = std::max (amax , std::fabs (x [(size_t )r * K + b * 32 + i ])); 430const float d = amax /127.f ,inv = amax > 0 ?127.f /amax :0.f ; 431ok &=hd [(size_t )r * K /32 + b ]== d ; 432for (int i = 0 ;i < 32 ;++ i )ok &=hq [(size_t )r * K + b * 32 + i ]== (int8_t ) std::nearbyint (x [(size_t )r * K + b * 32 + i ]* inv ); 433 } 434check (ok ,"df2_quant_rows == CPU (scales and int8, exact)" ); 435 } 436// attention against a double-precision CPU reference 437 { 438Drafter * Dp = new Drafter ();Drafter & D = * Dp ; 439const int POS = 700 ,n_new = 3 ; 440D .kring [0 ]= make_f32 ((size_t )RING * NKV * HD ,2.f );D .vring [0 ]= make_f32 ((size_t )RING * NKV * HD ,2.f ); 441D .tag = dev < int64_t > (RING );D .part = dev < float > ((size_t )kDf2PartChunks * 8 * 32 * 130 ); 442 std::vector < int64_t > t (RING ,-1 );for (int p = 0 ;p < POS + n_new ;++ p )t [p %RING ]= ((int64_t )1 <<32 ) |p ;up (D .tag ,t ); 443 std::vector < float > q ((size_t )8 * 32 * HD ),kb ((size_t )8 * NKV * HD ),vb (kb .size ()); 444for (auto & v :q )v = frand ()* 2 ;for (auto & v :kb )v = frand ()* 2 ;for (auto & v :vb )v = frand ()* 2 ; 445float * dq = dev < float > (q .size ()),* dk = dev < float > (kb .size ()),* dv = dev < float > (vb .size ());up (dq ,q );up (dk ,kb );up (dv ,vb ); 446int32_t * ap = dev < int32_t > (1 );int32_t sv = POS + n_new ;Q -> memcpy (ap ,& sv ,4 ).wait ();int32_t * ep = dev < int32_t > (1 );int32_t e1 = 1 ;Q -> memcpy (ep ,& e1 ,4 ).wait (); 447int8_t * xq = dev < int8_t > (8 * 32 * HD );float * xd = dev < float > (8 * 32 * HD /32 ); 448for (int avk : {1064 ,2064 ,1128 ,2128 }) { 449const int av = avk /1000 ,ck = avk %1000 ; 450df2_attention_variant (av );df2_attention_chunk (ck ); 451df2_attention (dq ,D .kring [0 ],D .vring [0 ],dk ,dv ,ap ,RING ,WINDOW ,D .part ,xq ,xd ,Q ,D .tag ,ep );Q -> wait (); 452auto kr = down (D .kring [0 ], (size_t )RING * NKV * HD );auto vr = down (D .vring [0 ], (size_t )RING * NKV * HD ); 453auto hq = down (xq , (size_t )8 * 32 * HD );auto hd = down (xd , (size_t )8 * 32 * HD /32 ); 454double maxerr = 0 ,maxref = 0 ; 455const int s = POS + n_new ,lo = std::max (0 ,s - (WINDOW - 1 )); 456for (int row = 0 ;row < 8 ;++ row ) 457for (int hh = 0 ;hh < 32 ;++ hh ) { 458const int kvh = hh /4 ; 459 std::vector < double > sc ; std::vector < const float *> vp ; 460for (int j = lo ;j < s ;++ j ) {if (j <=s + row - WINDOW )continue ;double a = 0 ;const float * kp = & kr [((size_t )kvh * RING + j %RING )* HD ];for (int d = 0 ;d < HD ;++ d )a += (double )q [((size_t )row * 32 + hh )* HD + d ]* kp [d ];sc .push_back (a / std::sqrt ((double )HD ));vp .push_back (& vr [((size_t )kvh * RING + j %RING )* HD ]); } 461for (int j = 0 ;j < 8 ;++ j ) {double a = 0 ;const float * kp = & kb [((size_t )j * NKV + kvh )* HD ];for (int d = 0 ;d < HD ;++ d )a += (double )q [((size_t )row * 32 + hh )* HD + d ]* kp [d ];sc .push_back (a / std::sqrt ((double )HD ));vp .push_back (& vb [((size_t )j * NKV + kvh )* HD ]); } 462double m = -1e300 ;for (double v :sc )m = std::max (m ,v ); 463double Z = 0 ; std::vector < double > o (HD ,0.0 ); 464for (size_t j = 0 ;j < sc .size ();++ j ) {const double w = std::exp (sc [j ]- m );Z += w ;for (int d = 0 ;d < HD ;++ d )o [d ]+= w * vp [j ][d ]; } 465for (int d = 0 ;d < HD ;++ d ) { 466const double ref = o [d ] /Z ; 467const size_t blk = (size_t )hh * (HD /32 )+ d /32 ;// (kvh * 4 + g) * 4 + d / 32 468const double got = (double )hq [(size_t )row * 32 * HD + blk * 32 + d %32 ]* hd [(size_t )row * 32 * HD /32 + blk ]; 469maxerr = std::max (maxerr , std::fabs (got - ref ));maxref = std::max (maxref , std::fabs (ref )); 470 } 471 } 472 std::printf (" attention: max |got - ref| = %.3e (max |ref| = %.3e; int8 quantization step ~ %.1e)\n" ,maxerr ,maxref ,maxref /127.0 ); 473 {char b2 [96 ]; std::snprintf (b2 ,sizeof b2 ,"df2_attention v%d chunk %d (ring 700 + 3, tags, window) == CPU double within the int8 step" ,av ,ck );check (maxerr < 2.0 * maxref /127.0 ,b2 ); } 474 } 475df2_attention_variant (0 );df2_attention_chunk (0 ); 476 } 477prefix_test (); 478 std::printf ("%s\n" ,g_fail ?"SOME TESTS FAILED" :"ALL TESTS PASSED" ); 479return g_fail ?1 :0 ; 480} 481 482// ---------------------------------------------------------------- bench 483struct Stats {double med ,p5 ,p95 ,mean ; }; 484Stats stats (const std::vector < double >& v ) {double s = 0 ;for (double x :v )s += x ;return Stats {med (v ),pct (v ,0.05 ),pct (v ,0.95 ),s / (double )v .size ()}; } 485 486int run_bench () { 487const int POS = envi ("DF2B_POS" ,600 ),ITERS = envi ("DF2B_ITERS" ,100 ),NNEW = envi ("DF2B_NNEW" ,3 ); 488const int standin_mb = envi ("DF2B_STANDIN_MB" ,0 ); 489const double gap = envd ("DF2B_GAPMS" ,0.0 ); 490 std::printf ("device: %s max wg %zu mem %.1f GiB\n" ,Q -> get_device ().get_info < sycl::info::device::name > ().c_str (), 491 (size_t )Q -> get_device ().get_info < sycl::info::device::max_work_group_size > (), 492 (double )Q -> get_device ().get_info < sycl::info::device::global_mem_size > () / (1024.0 * 1024 * 1024 )); 493Drafter D ; 494D .nd = envi ("DF2B_ND" ,7 ); 495build (D ); 496reset_ring (D ,POS ); 497float * standin = nullptr ;size_t standin_n = (size_t )standin_mb * 1024 * 1024 /4 ;float * sink = nullptr ; 498if (standin_mb ) {standin = make_f32 (standin_n ,1.f );sink = dev < float > (1024 ); } 499auto run_standin = [& ] { 500if (!standin )return ; 501const float * s = standin ;float * sk = sink ;const size_t n = standin_n ; 502Q -> parallel_for (sycl::nd_range < 1 > (1024 * 256 ,256 ), [= ](sycl::nd_item < 1 > it ) { 503float a = 0 ;for (size_t i = it .get_global_id (0 );i < n ;i += 1024 * 256 )a += s [i ]; 504if (a == 12345.678f )sk [0 ]= a ; 505 }); 506 }; 507double vram = 0 ; 508 std::printf ("config: ring pos %d, n_new %d, drafts %d, iters %d, stand-in %d MB, gap %.2f ms\n" ,POS ,NNEW ,D .nd ,ITERS ,standin_mb ,gap ); 509 (void )vram ; 510// warm up (JIT every kernel) 511set_meta (D ,NNEW ,POS ,5 ); 512for (int i = 0 ;i < 6 ;++ i ) {set_meta (D ,NNEW ,POS ,5 );record_round (D ,NNEW );Q -> wait (); } 513 {// finite-ness of the round's data (random weights could overflow; NaN / Inf would also slow special-value paths) 514auto stat = [& ](const char * nm ,const float * d ,size_t n ) { 515auto h = down (d ,n );size_t bad = 0 ;double mx = 0 ; 516for (float v :h ) {if (!std::isfinite (v ))++ bad ;else mx = std::max (mx , (double ) std::fabs (v )); } 517 std::printf ("data %-8s: %zu values, %zu not finite, max |v| %.3g\n" ,nm ,n ,bad ,mx ); 518 }; 519stat ("h" ,D .h , (size_t )B * N );stat ("fn" ,D .fn , (size_t )B * N );stat ("dyn" ,D .dyn , (size_t )B * 640 );stat ("qv" ,D .qv , (size_t )B * NH * HD ); 520stat ("ao-ish o" ,D .o , (size_t )B * N );stat ("gate" ,D .gate , (size_t )B * I );stat ("logits" ,D .logits , (size_t )D .nd * NDV ); 521 } 522uint64_t cs = checksum (D .h_out ,64 ); 523 std::printf ("draft ids: " );for (int i = 0 ;i < 7 ;++ i ) std::printf ("%d " ,D .h_out [i ]); std::printf (" probs: " );for (int i = 0 ;i < 7 ;++ i ) std::printf ("%.4f " ,D .h_prob [i ]); std::printf (" checksum %016llx\n" , (unsigned long long )cs ); 524 525// (1) direct launches 526 std::vector < double > wall ; 527for (int i = 0 ;i < ITERS + 10 ;++ i ) { 528run_standin (); 529if (gap > 0 ) {Q -> wait (); std::this_thread::sleep_for (std::chrono::duration < double , std::milli > (gap )); } 530Q -> wait (); 531const auto t0 = Clock ::now (); 532set_meta (D ,NNEW ,POS ,5 ); 533record_round (D ,NNEW ); 534Q -> wait (); 535const double w = ms_since (t0 ); 536if (i >=10 )wall .push_back (w ); 537 } 538const Stats sd = stats (wall ); 539 std::printf ("DIRECT wall ms/round: median %.3f p5 %.3f p95 %.3f mean %.3f\n" ,sd .med ,sd .p5 ,sd .p95 ,sd .mean ); 540// host submit time alone 541 { 542 std::vector < double > sub ; 543for (int i = 0 ;i < 30 ;++ i ) {Q -> wait ();const auto t0 = Clock ::now ();set_meta (D ,NNEW ,POS ,5 );record_round (D ,NNEW );sub .push_back (ms_since (t0 ));Q -> wait (); } 544 std::printf ("DIRECT host submit ms (until the last launch returns): median %.3f\n" ,med (sub )); 545 } 546// (2) graph replay 547if (envi ("DF2B_GRAPH" ,1 )) { 548try { 549 sx::command_graph < sx::graph_state::modifiable > g (Q -> get_context (),Q -> get_device ()); 550g .begin_recording (* Q ); 551record_round (D ,NNEW , false);// compute only: the meta copy and the result copy stay outside the graph 552g .end_recording (); 553auto exec = g .finalize (); 554 std::vector < double > gw ; 555for (int i = 0 ;i < ITERS + 10 ;++ i ) { 556run_standin (); 557if (gap > 0 ) {Q -> wait (); std::this_thread::sleep_for (std::chrono::duration < double , std::milli > (gap )); } 558Q -> wait (); 559const auto t0 = Clock ::now (); 560set_meta (D ,NNEW ,POS ,5 ); 561Q -> memcpy (D .d_meta ,D .h_meta , (32 + 2048 )* 4 ); 562Q -> ext_oneapi_graph (exec ); 563Q -> memcpy (D .h_out ,D .d_out ,64 * 4 ); 564Q -> wait (); 565if (i >=10 )gw .push_back (ms_since (t0 )); 566 } 567const Stats sg = stats (gw ); 568 std::printf ("GRAPH wall ms/round: median %.3f p5 %.3f p95 %.3f mean %.3f checksum %016llx\n" ,sg .med ,sg .p5 ,sg .p95 ,sg .mean , (unsigned long long )checksum (D .h_out ,64 )); 569 }catch (const std::exception & e ) { 570 std::printf ("GRAPH unavailable: %s\n" ,e .what ()); 571 } 572 } 573// (3) per-kernel profile (markers after each launch) 574if (envi ("DF2B_PROFILE" ,1 )) { 575D .profile = true; 576 std::vector < std::pair < std::string ,double >> tot ;int nrun = 0 ; 577for (int i = 0 ;i < 20 ;++ i ) { 578D .marks .clear (); 579Q -> wait (); 580 sycl::event e0 = Q -> single_task ([] {}); 581D .marks .emplace_back ("start" ,e0 ); 582set_meta (D ,NNEW ,POS ,5 ); 583record_round (D ,NNEW ); 584Q -> wait (); 585if (i < 3 )continue ; 586++ nrun ; 587for (size_t k = 1 ;k < D .marks .size ();++ k ) { 588const double a = (double )D .marks [k - 1 ].second .get_profiling_info < sycl::info::event_profiling::command_end > (); 589const double b = (double )D .marks [k ].second .get_profiling_info < sycl::info::event_profiling::command_end > (); 590const double ms = (b - a )* 1e-6 ; 591auto it = std::find_if (tot .begin (),tot .end (), [& ](auto & t ) {return t .first == D .marks [k ].first ; }); 592if (it == tot .end ())tot .emplace_back (D .marks [k ].first ,ms );else it -> second += ms ; 593 } 594 } 595D .profile = false; 596double sum = 0 ;for (auto & t :tot )sum += t .second ; 597 std::printf ("PROFILE (marker after every launch, so each includes its launch gap; ms per round, sum %.3f over %d rounds):\n" ,sum /nrun ,nrun ); 598 std::sort (tot .begin (),tot .end (), [](auto & a ,auto & b ) {return a .second > b .second ; }); 599for (auto & t :tot ) std::printf (" %-16s %7.3f\n" ,t .first .c_str (),t .second /nrun ); 600 } 601return 0 ; 602} 603 604// ---------------------------------------------------------------- per-kernel micro timings 605template < class F > double time_us (F && f ,int iters ) { 606for (int i = 0 ;i < 5 ;++ i )f (); 607Q -> wait (); 608const auto t0 = Clock ::now (); 609for (int i = 0 ;i < iters ;++ i )f (); 610Q -> wait (); 611return std::chrono::duration < double , std::micro > (Clock ::now ()- t0 ).count () /iters ; 612} 613 614int run_gemm_bench () { 615if (envi ("DF2B_HOSTALLOC" ,0 )== 1 ) (void ) strata::host_malloc_polled (4096 ,* Q );// uncached host memory (zeMemAllocHost), as the engine's doorbells 616if (envi ("DF2B_HOSTALLOC" ,0 )== 2 ) (void ) sycl::malloc_host (4096 ,* Q ); 617if (envi ("DF2B_BIGALLOC_MB" ,0 )) {void * bp = sycl::malloc_device ((size_t )envi ("DF2B_BIGALLOC_MB" ,0 ) <<20 ,* Q );Q -> memset (bp ,1 , (size_t )envi ("DF2B_BIGALLOC_MB" ,0 ) <<20 ).wait (); }// touched 618for (int i = 0 ;i < envi ("DF2B_NALLOC" ,0 );++ i ) (void ) sycl::malloc_device (4096 + 64 * i ,* Q ); 619for (int i = 0 ;i < envi ("DF2B_NMED" ,0 );++ i ) {void * mp = sycl::malloc_device (8 <<20 ,* Q );Q -> memset (mp ,1 ,8 <<20 ); } 620Q -> wait (); 621if (const char * pre = std::getenv ("DF2B_PRE" )) { 622const std::string ps = pre ; 623if (ps .find ("rope" )!= std::string::npos ) {double * r = dev < double > (64 );df2_rope_inv (10000.0f ,r ,Q );Q -> wait (); } 624if (ps .find ("head" )!= std::string::npos ) (void )make_head (); 625if (ps .find ("cb" )!= std::string::npos ) { std::vector < uint16_t > cb ((size_t )VOCAB * RANK ,0x3c00 );uint16_t * d = dev < uint16_t > (cb .size ());up (d ,cb ); } 626if (ps .find ("ring" )!= std::string::npos )for (int l = 0 ;l < 10 ;++ l ) (void )make_f32 ((size_t )RING * NKV * HD ,2.f ); 627if (ps .find ("layers" )!= std::string::npos )for (int l = 0 ;l < 5 ;++ l ) {make_mat (I ,N );make_mat (I ,N );make_mat (N ,I );make_mat (4096 ,N ); } 628if (ps .find ("host" )!= std::string::npos ) { (void ) strata::host_malloc_polled (128 ,* Q ); (void ) strata::host_malloc_polled (256 ,* Q ); } 629Q -> wait (); 630 } 631 std::printf ("device: %s\n" ,Q -> get_device ().get_info < sycl::info::device::name > ().c_str ()); 632struct Shape {const char * name ;int n_out ,n_in ,nmat ; }; 633const Shape shapes []= {{"fc" ,N ,5 * N ,1 }, {"qkv(3)" ,NH * HD ,N ,3 }, {"o" ,N ,NH * HD ,1 }, {"gate+up(2)" ,I ,N ,2 }, {"down" ,N ,I ,1 }, {"akp/mkp" ,640 ,N ,1 }, {"hsel" ,RANK ,N ,1 }}; 634int vs [16 ],nv = 0 ; 635if (const char * e = std::getenv ("DF2B_VARIANTS" )) {for (const char * c = e ;* c ;) {vs [nv ++ ]= std::atoi (c );while (* c && * c != ',' )++ c ;if (* c == ',' )++ c ; } } 636else {const int d []= {28 ,12 ,40 ,41 ,42 ,43 };for (int v :d )vs [nv ++ ]= v ; } 637const int ncs []= {8 ,4 ,1 }; 638 std::printf ("us per launch (200 back-to-back), GB/s of weights+scales; columns 8 / 4 / 1\n%-12s %8s" ,"shape" ,"MB" ); 639for (int i = 0 ;i < nv ;++ i ) std::printf (" | v%-2d 8col 4col 1col" ,vs [i ]); 640 std::printf ("\n" ); 641const int ncopy = envi ("DF2B_COLD" ,0 ) ?8 :1 ;// cold: the launches rotate over 8 copies of the matrices (> L2), as a round's 5 layers do 642for (const Shape & s :shapes ) { 643 std::vector < std::vector < Mat4 >> mats (ncopy ); std::vector < float *> ys ; 644for (int k = 0 ;k < ncopy ;++ k ) 645for (int m = 0 ;m < s .nmat ;++ m ) {mats [k ].push_back (make_mat (s .n_out ,s .n_in ));if (k == 0 )ys .push_back (dev < float > ((size_t )8 * s .n_out )); } 646int8_t * xq = dev < int8_t > ((size_t )8 * s .n_in );float * xd = dev < float > ((size_t )8 * s .n_in /32 ); 647 {// random activations (the bit toggling of real data), scales 0.01 648 std::vector < int8_t > hq ((size_t )8 * s .n_in );for (auto & v :hq )v = (int8_t )rnd (); 649 std::vector < float > hd ((size_t )8 * s .n_in /32 ,0.01f ); 650Q -> memcpy (xq ,hq .data (),hq .size ()).wait ();Q -> memcpy (xd ,hd .data (),hd .size ()* 4 ).wait (); 651 } 652const double mb = (double )s .nmat * s .n_out * s .n_in * 0.5625 /1e6 ; 653 std::printf ("%-12s %8.1f" ,s .name ,mb ); 654for (int i = 0 ;i < nv ;++ i ) { 655df2_gemm_variant (vs [i ]); 656 std::printf (" | " ); 657for (int nc :ncs ) { 658 std::vector < std::vector < Df2Mat >> dm (ncopy ); 659for (int k = 0 ;k < ncopy ;++ k )for (int m = 0 ;m < s .nmat ;++ m )dm [k ].push_back (Df2Mat {mats [k ][m ].w ,ys [m ],s .n_out , true}); 660int it = 0 ; 661const double us = time_us ([& ] {df2_gemm (dm [it ++ %ncopy ].data (),s .nmat ,xq ,xd ,s .n_in ,nc ,Q ); },200 ); 662 std::printf (" %5.0f(%3.0f)" ,us ,mb * 1e3 /us ); 663 } 664 } 665 std::printf ("\n" ); 666for (auto & v :mats )for (auto & m :v ) sycl::free (m .w ,* Q ); 667for (auto y :ys ) sycl::free (y ,* Q ); 668 sycl::free (xq ,* Q ); sycl::free (xd ,* Q ); 669 } 670df2_gemm_variant (0 ); 671return 0 ; 672} 673 674int run_attn_bench () { 675 std::printf ("device: %s\n" ,Q -> get_device ().get_info < sycl::info::device::name > ().c_str ()); 676float * kr = make_f32 ((size_t )RING * NKV * HD ,2.f );float * vr = make_f32 ((size_t )RING * NKV * HD ,2.f ); 677int64_t * tag = dev < int64_t > (RING );float * part = dev < float > ((size_t )kDf2PartChunks * 8 * 32 * 130 ); 678float * q = make_f32 ((size_t )8 * 32 * HD ,2.f );float * kb = make_f32 ((size_t )8 * NKV * HD ,2.f );float * vb = make_f32 ((size_t )8 * NKV * HD ,2.f ); 679int32_t * ap = dev < int32_t > (1 );int32_t * ep = dev < int32_t > (1 );int32_t e1 = 1 ;Q -> memcpy (ep ,& e1 ,4 ).wait (); 680int8_t * xq = dev < int8_t > (8 * 32 * HD );float * xd = dev < float > (8 * 32 * HD /32 ); 681for (int avk : {1064 ,2064 ,1128 ,2128 })for (int pos : {100 ,600 ,1200 ,2100 }) { 682const int av = avk /1000 ,ck = avk %1000 ; 683df2_attention_variant (av );df2_attention_chunk (ck ); 684 std::vector < int64_t > t (RING ,-1 );for (int p = std::max (0 ,pos - 2047 );p < pos ;++ p )t [p %RING ]= ((int64_t )1 <<32 ) |p ; 685Q -> memcpy (tag ,t .data (),RING * 8 ).wait (); 686int32_t sv = pos ;Q -> memcpy (ap ,& sv ,4 ).wait (); 687const double us = time_us ([& ] {df2_attention (q ,kr ,vr ,kb ,vb ,ap ,RING ,WINDOW ,part ,xq ,xd ,Q ,tag ,ep ); },100 ); 688 std::printf ("attention v%d chunk %d, s = %d: %.0f us per layer\n" ,av ,ck ,pos ,us ); 689 } 690return 0 ; 691} 692 693// ---------------------------------------------------------------- dp4a throughput 694extern "C" SYCL_EXTERNAL int __builtin_IB_dp4a_ss (int c ,int a ,int b ); 695template < int MODE > int dp4a_mode (int a ,int b ,int c ) { 696if constexpr (MODE == 0 )return dpct::dp4a (a ,b ,c ); 697else if constexpr (MODE == 1 )return __builtin_IB_dp4a_ss (c ,a ,b ); 698else {// sycl vec of int8 699const auto va = sycl::bit_cast < sycl::vec < int8_t ,4 >> (a ),vb = sycl::bit_cast < sycl::vec < int8_t ,4 >> (b ); 700return c + (int )va [0 ]* vb [0 ]+ (int )va [1 ]* vb [1 ]+ (int )va [2 ]* vb [2 ]+ (int )va [3 ]* vb [3 ]; 701 } 702} 703template < int MODE > double dp4a_rate (int iters ) { 704const size_t items = 28 * 128 * 64 ; 705int * out = dev < int > (items ); 706int * in = dev < int > (items ); std::vector < int > h (items );for (auto & v :h )v = (int )rnd ()* 2654435761u ;up (in ,h ); 707auto run = [& ] { 708Q -> parallel_for (sycl::nd_range < 1 > (items ,64 ), [= ](sycl::nd_item < 1 > it ) [[sycl ::reqd_sub_group_size (32 )]] { 709const size_t i = it .get_global_id (0 ); 710int a0 = in [i ],a1 = a0 ^0x1234567 ,a2 = a0 * 3 ,a3 = a0 + 77 ,b = in [(i + 1 ) %items ]; 711int s0 = 0 ,s1 = 0 ,s2 = 0 ,s3 = 0 ; 712for (int k = 0 ;k < iters ;++ k ) { 713s0 = dp4a_mode < MODE > (a0 ,b ,s0 );s1 = dp4a_mode < MODE > (a1 ,b ,s1 );s2 = dp4a_mode < MODE > (a2 ,b ,s2 );s3 = dp4a_mode < MODE > (a3 ,b ,s3 ); 714a0 += 0x01010101 ;a1 += 0x01010101 ;a2 += 0x01010101 ;a3 += 0x01010101 ; 715 } 716out [i ]= s0 + s1 + s2 + s3 ; 717 }); 718 }; 719run ();Q -> wait (); 720const auto t0 = Clock ::now ();run ();Q -> wait (); 721const double sec = ms_since (t0 )* 1e-3 ; 722 sycl::free (out ,* Q ); sycl::free (in ,* Q ); 723return (double )items * iters * 4 /sec /1e9 ; 724} 725int run_dp4a () { 726 std::printf ("dp4a throughput, Gdp4a/s (4 independent chains): dpct::dp4a %.0f __builtin_IB_dp4a_ss %.0f vec<int8,4> %.0f\n" ,dp4a_rate < 0 > (20000 ),dp4a_rate < 1 > (20000 ),dp4a_rate < 2 > (20000 )); 727return 0 ; 728} 729 730int run_bubble () { 731float * a = dev < float > (4096 );Q -> memset (a ,0 ,4096 * 4 ).wait (); 732const double us1 = time_us ([& ] {Q -> single_task ([] {}); },2000 ); 733const double us2 = time_us ([& ] {Q -> parallel_for (sycl::nd_range < 1 > (256 ,64 ), [= ](sycl::nd_item < 1 > it ) {a [it .get_global_id (0 )]+= 1.f ; }); },2000 ); 734const double us3 = time_us ([& ] {Q -> parallel_for (sycl::nd_range < 1 > (32 * 28 * 64 ,64 ), [= ](sycl::nd_item < 1 > it ) {a [it .get_global_id (0 )& 4095 ]+= 1.f ; }); },2000 ); 735 std::printf ("back-to-back in-order launches, us each: empty single_task %.1f 256-item kernel %.1f 57344-item kernel %.1f\n" ,us1 ,us2 ,us3 ); 736return 0 ; 737} 738 739int run_small () { 740Drafter D ;D .nd = 7 ;build (D );reset_ring (D ,600 ); 741set_meta (D ,3 ,600 ,5 ); 742Layer & y = D .ly [0 ]; 743 { 744const double d0 = time_us ([& ] {gemm2 (y .gate ,y .up ,D .gate ,D .up ,B ,D .xqp ,D .xdp ); },200 ); 745 std::printf ("gate_up right after build (before any round): %.1f us\n" ,d0 ); 746record_round (D ,3 );Q -> wait (); 747const double d1 = time_us ([& ] {gemm2 (y .gate ,y .up ,D .gate ,D .up ,B ,D .xqp ,D .xdp ); },200 ); 748 std::printf ("gate_up after one round: %.1f us\n" ,d1 ); 749const double d2 = time_us ([& ] {df2_attention (D .qn ,D .kring [0 ],D .vring [0 ],D .kblk ,D .vv ,D .d_meta + 17 ,RING ,WINDOW ,D .part ,D .xqp ,D .xdp ,Q ,D .tag ,D .d_meta + 18 ); },200 ); 750 std::printf ("attention after one round: %.1f us\n" ,d2 ); 751 } 752auto t_all = [& ](const char * tag ) { 753const double a = time_us ([& ] {df2_conv_quant (D .xn ,D .dyn ,640 ,y .base_m ,B ,N ,GROUP ,D .xqp ,D .xdp ,Q ); },1000 ); 754const double b = time_us ([& ] {df2_conv_rms_quant (D .o ,D .dyn + 320 ,640 ,y .base_a + 2 * N ,D .h ,y .ln_post ,D .xn ,B ,N ,GROUP ,EPS ,D .xqp ,D .xdp ,Q ); },1000 ); 755const double c = time_us ([& ] {df2_swiglu_quant (D .gate ,D .up ,B ,I ,D .xqp ,D .xdp ,Q ); },1000 ); 756const double d = time_us ([& ] {gemm2 (y .gate ,y .up ,D .gate ,D .up ,B ,D .xqp ,D .xdp ); },200 ); 757const double e = time_us ([& ] {df2_attention (D .qn ,D .kring [0 ],D .vring [0 ],D .kblk ,D .vv ,D .d_meta + 17 ,RING ,WINDOW ,D .part ,D .xqp ,D .xdp ,Q ,D .tag ,D .d_meta + 18 ); },200 ); 758 std::printf ("%-26s conv_quant %6.1f conv_rms_quant %6.1f swiglu_quant %6.1f gate_up %6.1f attention %6.1f (us)\n" ,tag ,a ,b ,c ,d ,e ); 759 }; 760t_all ("in-round data" ); 761Q -> memset (D .xn ,0 , (size_t )B * N * 4 );Q -> memset (D .dyn ,0 , (size_t )B * 640 * 4 );Q -> memset (D .o ,0 , (size_t )B * N * 4 );Q -> memset (D .h ,0 , (size_t )B * N * 4 ); 762Q -> memset (D .gate ,0 , (size_t )B * I * 4 );Q -> memset (D .up ,0 , (size_t )B * I * 4 );Q -> memset (D .xqp ,0 , (size_t )5 * N * 8 );Q -> memset (D .xdp ,0 ,5 * N /32 * 8 * 4 );Q -> wait (); 763t_all ("zeroed activations" ); 764 std::vector < float > hx ((size_t )B * N );for (auto & v :hx )v = frand (); 765Q -> memcpy (D .xn ,hx .data (),hx .size ()* 4 );Q -> memcpy (D .o ,hx .data (),hx .size ()* 4 );Q -> memcpy (D .h ,hx .data (),hx .size ()* 4 ); 766 std::vector < float > hg ((size_t )B * I );for (auto & v :hg )v = frand (); 767Q -> memcpy (D .gate ,hg .data (),hg .size ()* 4 );Q -> memcpy (D .up ,hg .data (),hg .size ()* 4 ); 768 std::vector < float > hd ((size_t )B * 640 );for (auto & v :hd )v = frand (); 769Q -> memcpy (D .dyn ,hd .data (),hd .size ()* 4 );Q -> wait (); 770t_all ("small random activations" ); 771return 0 ; 772} 773 774int run_poison () { 775Drafter D ;D .nd = 7 ;build (D );reset_ring (D ,600 ); 776set_meta (D ,3 ,600 ,5 ); 777Q -> memcpy (D .d_meta ,D .h_meta ,128 ).wait (); 778Layer & y = D .ly [0 ]; 779auto probe = [& ](const char * after ) { 780const double e = time_us ([& ] {df2_attention (D .qn ,D .kring [0 ],D .vring [0 ],D .kblk ,D .vv ,D .d_meta + 17 ,RING ,WINDOW ,D .part ,D .xqp ,D .xdp ,Q ,D .tag ,D .d_meta + 18 ); },100 ); 781const double g = time_us ([& ] {gemm2 (y .gate ,y .up ,D .gate ,D .up ,B ,D .xqp ,D .xdp ); },100 ); 782const double c = time_us ([& ] {df2_conv_quant (D .xn ,D .dyn ,640 ,y .base_m ,B ,N ,GROUP ,D .xqp ,D .xdp ,Q ); },300 ); 783 std::printf ("after %-26s attention %7.1f gate_up %7.1f conv_quant %6.1f us\n" ,after ,e ,g ,c ); 784 }; 785probe ("build" ); 786df2_quant_rows (D .taps ,5 * N ,3 ,D .xqc ,D .xdc ,Q );Q -> wait ();probe ("quant_rows" ); 787df2_rms_quant (D .fcy ,D .hidden_norm ,nullptr ,3 ,N ,EPS ,D .xqc ,D .xdc ,Q );Q -> wait ();probe ("rms_quant" ); 788df2_kv_ring (D .kc ,D .vc ,D .ly [0 ].kn ,D .d_meta ,D .rt_ctx ,D .kring [0 ],D .vring [0 ],RING ,3 ,NKV ,EPS ,Q ,D .tag ,D .d_meta + 18 );Q -> wait ();probe ("kv_ring" ); 789embed_stub (D );Q -> wait ();probe ("embed" ); 790df2_conv_rms_quant (D .o ,D .dyn + 320 ,640 ,y .base_a + 2 * N ,D .h ,y .ln_post ,D .xn ,B ,N ,GROUP ,EPS ,D .xqp ,D .xdp ,Q );Q -> wait ();probe ("conv_rms_quant" ); 791df2_swiglu_quant (D .gate ,D .up ,B ,I ,D .xqp ,D .xdp ,Q );Q -> wait ();probe ("swiglu_quant" ); 792df2_norm_rope_qk (D .qv ,D .kv ,y .qn ,y .kn ,D .rt_blk ,D .qn ,D .kblk ,B ,NH ,NKV ,EPS ,Q );Q -> wait ();probe ("norm_rope_qk" ); 793native_quantize_q8_1 (D .fn + N ,D .xq_head ,N ,7 ,Q );Q -> wait ();probe ("native_quantize_q8_1" ); 794native_mmvq (GGML_Q8_0 ,D .head_w ,D .xq_head ,D .logits ,N ,NDV ,7 ,Q );Q -> wait ();probe ("native_mmvq head" ); 795df2_topk (D .logits ,NDV ,D .dvocab ,7 ,D .topscratch ,D .topval ,D .toptok ,Q );Q -> wait ();probe ("topk" ); 796df2_select (D .hsel_out ,D .topval ,D .toptok ,D .pred_cb ,D .succ_cb ,RANK ,7 ,D .d_meta + 16 ,D .d_out , (float * )D .d_out + 16 ,Q );Q -> wait ();probe ("select" ); 797return 0 ; 798} 799 800int run_poison2 () { 801Drafter D ;D .nd = 7 ;build (D );reset_ring (D ,600 ); 802Layer & y = D .ly [0 ]; 803auto probe = [& ](const char * after ) { 804const double e = time_us ([& ] {df2_attention (D .qn ,D .kring [0 ],D .vring [0 ],D .kblk ,D .vv ,D .d_meta + 17 ,RING ,WINDOW ,D .part ,D .xqp ,D .xdp ,Q ,D .tag ,D .d_meta + 18 ); },100 ); 805const double g = time_us ([& ] {gemm2 (y .gate ,y .up ,D .gate ,D .up ,B ,D .xqp ,D .xdp ); },100 ); 806const double c = time_us ([& ] {df2_conv_quant (D .xn ,D .dyn ,640 ,y .base_m ,B ,N ,GROUP ,D .xqp ,D .xdp ,Q ); },300 ); 807 std::printf ("after %-44s attention %7.1f gate_up %7.1f conv_quant %6.1f us\n" ,after ,e ,g ,c ); 808 }; 809const int mode = envi ("DF2B_P2" ,0 ); 810probe ("nothing" ); 811int32_t * hp = (int32_t * ) strata::host_malloc_polled (4096 ,* Q );// uncached host 812int32_t * hc = (int32_t * ) sycl::malloc_host (4096 ,* Q );// cached host 813int32_t * sh = (int32_t * ) sycl::malloc_shared (4096 ,* Q ); 814int32_t * dv = dev < int32_t > (1024 ); 815 std::memset (hp ,0 ,4096 ); std::memset (hc ,0 ,4096 );Q -> memset (dv ,0 ,4096 ).wait (); 816if (mode == 0 || mode == 1 ) {Q -> parallel_for (sycl::range < 1 > (64 ), [= ](sycl::id < 1 > i ) {dv [i ]= hp [i ]; }).wait ();probe ("kernel READ of uncached host USM" ); } 817if (mode == 0 || mode == 2 ) {Q -> parallel_for (sycl::range < 1 > (64 ), [= ](sycl::id < 1 > i ) {dv [i ]= hc [i ]; }).wait ();probe ("kernel READ of cached host USM" ); } 818if (mode == 0 || mode == 3 ) {Q -> parallel_for (sycl::range < 1 > (64 ), [= ](sycl::id < 1 > i ) {hp [i ]= (int )i ; }).wait ();probe ("kernel WRITE of uncached host USM" ); } 819if (mode == 0 || mode == 4 ) {Q -> parallel_for (sycl::range < 1 > (64 ), [= ](sycl::id < 1 > i ) {hc [i ]= (int )i ; }).wait ();probe ("kernel WRITE of cached host USM" ); } 820if (mode == 0 || mode == 5 ) {Q -> parallel_for (sycl::range < 1 > (64 ), [= ](sycl::id < 1 > i ) {sh [i ]= (int )i ; }).wait ();probe ("kernel WRITE of shared USM" ); } 821return 0 ; 822} 823 824// ---------------------------------------------------------------- the prompt's context update (the drafter's tap rows of the last 2047 prompt positions) 825int run_ctx () { 826Drafter D ;D .nd = 7 ;build (D );reset_ring (D ,0 ); 827const int NCH = envi ("DF2B_CHUNKS" ,256 ),ROWS = 8 ; 828int8_t * hq = (int8_t * ) sycl::malloc_host ((size_t )ROWS * N ,* Q ); 829float * hd = (float * ) sycl::malloc_host ((size_t )ROWS * (N /32 )* 4 ,* Q ); 830 std::vector < int32_t *> metas ;// per chunk, its own host staging (positions + rope tables), for the modes that do not wait between chunks 831for (int k = 0 ;k < NCH ;++ k ) {set_meta (D ,ROWS , (int64_t )k * ROWS ,0 );int32_t * m = (int32_t * ) sycl::malloc_host ((32 + 2048 )* 4 ,* Q ); std::memcpy (m ,D .h_meta , (32 + 2048 )* 4 );metas .push_back (m ); } 832auto warm = [& ] {for (int i = 0 ;i < 4 ;++ i ) {set_meta (D ,ROWS ,0 ,0 );Q -> memcpy (D .d_meta ,D .h_meta , (32 + 2048 )* 4 );record_context (D ,ROWS ); }Q -> wait (); }; 833warm (); 834auto per = [& ](const char * name ,double total_ms ,int nch ) { std::printf (" %-62s %8.1f ms total, %6.3f ms per 8-row chunk (%d chunks = %d rows)\n" ,name ,total_ms ,total_ms /nch ,nch ,nch * ROWS ); }; 835 std::printf ("context update of %d rows in chunks of %d, the Q4_0 pack's shapes (fc 12800 -> 2560, K/V of 5 layers):\n" ,NCH * ROWS ,ROWS ); 836 {// (a) as Dflash2Drafter::context does it: host staging + meta copy + the update's kernels + the log's copy to the host + a wait, per chunk 837const auto t0 = Clock ::now (); 838for (int k = 0 ;k < NCH ;++ k ) { 839set_meta (D ,ROWS , (int64_t )k * ROWS ,0 ); 840Q -> memcpy (D .d_meta ,D .h_meta , (32 + 2048 )* 4 ); 841record_context (D ,ROWS ); 842Q -> memcpy (hq ,D .xqc , (size_t )ROWS * N );Q -> memcpy (hd ,D .xdc , (size_t )ROWS * (N /32 )* 4 ); 843Q -> wait (); 844 } 845per ("(a) the engine's loop: stage, 13 kernels, log copy, wait per chunk" ,ms_since (t0 ),NCH ); 846 } 847 {// (b) host staging alone 848const auto t0 = Clock ::now (); 849for (int k = 0 ;k < NCH ;++ k )set_meta (D ,ROWS , (int64_t )k * ROWS ,0 ); 850per ("(b) the host staging alone (positions + 16 rope tables)" ,ms_since (t0 ),NCH ); 851 } 852 {// (c) the kernels alone, queued back to back, one wait 853const auto t0 = Clock ::now (); 854for (int k = 0 ;k < NCH ;++ k )record_context (D ,ROWS ); 855Q -> wait (); 856per ("(c) the 13 kernels alone, queued back to back, one wait" ,ms_since (t0 ),NCH ); 857 } 858 {// (d) staging copies + kernels queued, no wait per chunk (each chunk's staging from its own buffer) 859const auto t0 = Clock ::now (); 860for (int k = 0 ;k < NCH ;++ k ) {Q -> memcpy (D .d_meta ,metas [(size_t )k ], (32 + 2048 )* 4 );record_context (D ,ROWS ); } 861Q -> wait (); 862per ("(d) meta copy + 13 kernels per chunk, pre-staged, one wait" ,ms_since (t0 ),NCH ); 863 } 864 {// (e) one chunk, a marker after every launch: GPU ms per kernel of the update 865D .profile = true; 866 std::vector < std::pair < std::string ,double >> tot ;int nrun = 0 ; 867for (int i = 0 ;i < 12 ;++ i ) { 868D .marks .clear ();Q -> wait (); 869D .marks .emplace_back ("start" ,Q -> single_task ([] {})); 870set_meta (D ,ROWS ,0 ,0 );Q -> memcpy (D .d_meta ,D .h_meta , (32 + 2048 )* 4 );record_context (D ,ROWS );Q -> wait (); 871if (i < 2 )continue ; 872++ nrun ; 873for (size_t k = 1 ;k < D .marks .size ();++ k ) { 874const double a = (double )D .marks [k - 1 ].second .get_profiling_info < sycl::info::event_profiling::command_end > (); 875const double b = (double )D .marks [k ].second .get_profiling_info < sycl::info::event_profiling::command_end > (); 876auto it = std::find_if (tot .begin (),tot .end (), [& ](auto & t ) {return t .first == D .marks [k ].first ; }); 877if (it == tot .end ())tot .emplace_back (D .marks [k ].first , (b - a )* 1e-6 );else it -> second += (b - a )* 1e-6 ; 878 } 879 } 880D .profile = false; 881double sum = 0 ;for (auto & t :tot )sum += t .second ; 882 std::printf (" (e) one 8-row chunk, a marker after every launch (each includes its launch gap), ms: total %.3f over %d launches:" ,sum /nrun , (int )D .marks .size ()- 1 ); 883for (auto & t :tot ) std::printf (" %s %.3f" ,t .first .c_str (),t .second /nrun ); 884 std::printf ("\n" ); 885 } 886 {// (f) weight bytes read per 8-row chunk and the bandwidth the best case would need 887const double mb = (18.4 + 5 * 3.54 ); 888 std::printf (" (f) weight bytes per chunk: fc 18.4 MB + 5 x (k,v) 3.5 MB = %.1f MB; for %d chunks %.2f GB; at 400 GB/s that is %.1f ms per 8-row chunk\n" ,mb ,NCH ,mb * NCH /1000.0 ,mb /400.0 ); 889 } 890return 0 ; 891} 892}// namespace 893 894int main (int argc ,char ** argv ) { 895if (const char * d = std::getenv ("DF2B_DUP" ))g_dup = d ; 896g_dupn = envi ("DF2B_DUPN" ,1 ); 897g_host_out = envi ("DF2B_HOSTOUT" ,0 );g_meta_kernel = envi ("DF2B_METAKERNEL" ,0 ); 898static sycl::queue q (sycl::gpu_selector_v , sycl::property_list {sycl::property::queue::in_order (), sycl::property::queue::enable_profiling ()}); 899Q = & q ; 900const std::string mode = argc > 1 ?argv [1 ] :"bench" ; 901return mode == "test" ?run_tests () :mode == "gemm" ?run_gemm_bench () :mode == "attn" ?run_attn_bench () :mode == "dp4a" ?run_dp4a () :mode == "bubble" ?run_bubble () :mode == "small" ?run_small () :mode == "poison" ?run_poison () :mode == "poison2" ?run_poison2 () :mode == "ctx" ?run_ctx () :run_bench (); 902}