okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5438023a6
main
1// src/kernels/qsa_prompt_attn_parity.cpp - perf-review D-1: the tensor-core prompt attention (qsa_prompt_attn.hpp) 2// against the FP32 kernel it replaces (`qsa_decode_attn_batch`) and an FP64 host reference (GPU, synthetic, no model). 3// 4// Int8, FP16 and Q4_0 pools with random codes, scales and queries; selections shaped like the prompt path's (the 2,051 5// widest, a recent window plus older cells that drift slowly from one query to the next, so neighbours share most 6// of them as they do in a real prompt; short contexts take every cell). Checks: 7// 1. against FP64, the new kernel's error is no larger than a small multiple of the old kernel's (both FP32 math); 8// 2. the new and old outputs agree to a relative 1e-4 of the output scale; 9// then times both over a prompt chunk (the old one in batches of 32, as prefill.cpp calls it). 10// HIP builds (S6): the same checks for the RDNA4 matrix-core kernel (STRATA_HIP_WMMA), skipped (77) off gfx12. 11// Usage: qsa_prompt_attn_parity [context=32768] [queries=2048] [reps=5] 12#define DPCT_PROFILING_ENABLED 13#include <sycl/sycl.hpp> 14#include <dpct/dpct.hpp> 15#include "strata/kernels/qsa.hpp" 16#include "strata/kernels/qsa_decode_attn.hpp" 17#include "strata/kernels/qsa_prompt_attn.hpp" 18#include "strata/kernels/kv_q4.hpp" 19 20#include <algorithm> 21#include <cmath> 22#include <cstdio> 23#include <cstdlib> 24#include <cstring> 25#include <random> 26#include <vector> 27 28namespace k= strata::kernels; 29namespace strata::kernels {// SYCL port: the prompt path's per-cell attention (qsa_decode_attn.dp.cpp) 30void qsa_prompt_attn_percell (const float * q ,const QsaAttnPools & pools ,const int32_t * ids ,const int32_t * steps , 31int64_t cap ,const QsaShapes & s ,float * scratch ,float * attn ,int64_t n_q ,void * stream ); 32} 33 34namespace { 35void ck (dpct::err0 e ,const char * w ) { 36/* 37DPCT1009: SYCL reports errors using exceptions and does not use error 38codes. Please replace the "get_error_string_dummy(...)" with a real 39error-handling function. 40*/ 41} 42template < typename T > T * up (const std::vector < T >& h ) { 43T * d = nullptr ; 44ck (DPCT_CHECK_ERROR ( 45d = (T * )sycl::malloc_device (h .size ()* sizeof (T )+ 64 , 46 dpct::get_in_order_queue ())), 47"malloc" ); 48/* 49DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 50in the original code the source host memory is pageable memory. If the 51memory is not pageable, call wait() on event return by memcpy API to ensure 52synchronization behavior. 53*/ 54ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 55d ,h .data (),h .size ()* sizeof (T )).wait ()), 56"upload" ); 57return d ; 58} 59float h2f (uint16_t b ) { 60 sycl::half h ;* reinterpret_cast < uint16_t *> (& h )= b ; 61return sycl::vec < sycl::half ,1 > (h ) 62 .convert < float , sycl::rounding_mode::automatic > ()[0 ]; 63} 64uint16_t f2h (float f ) { 65 sycl::half h = 66 sycl::vec < float ,1 > (f ) 67 .convert < sycl::half , sycl::rounding_mode::automatic > ()[0 ]; 68return * reinterpret_cast < uint16_t *> (& h ); 69} 70 71int run (int fmt ,int64_t ctx ,int64_t nq ,int reps ) {// fmt 1 int8, 0 fp16, 2 q4_0 72#if !defined(__HIPCC__ )&& !defined(STRATA_USE_HIP ) 73if (fmt == 2 ) {// mode 4 (Q4_0 KV) runs on sm_80 and newer only: below that the dispatcher keeps the old kernel 74int dev = 0 ,major = 0 ; 75ck (DPCT_CHECK_ERROR (dev = dpct::get_current_device_id ()),"device" ); 76ck (DPCT_CHECK_ERROR (major = dpct::get_device (dev ).get_major_version ()), 77"cc" ); 78if (major < 8 ) { 79 std::printf ("SKIP q4_0 ctx %lld: the tensor-core kernel takes Q4_0 KV on sm_80+ only (this device: sm_%d)\n" , 80 (long long )ctx ,major ); 81return 0 ; 82 } 83 } 84#endif 85const k::QsaShapes s = k::qsa_real_shapes (); 86const int64_t HD = s .head_dim ,NKV = s .n_head_kv ,NH = s .n_head ,PS = s .page_size ; 87const int64_t pages = (ctx + PS - 1 ) /PS ,rows = pages * NKV * PS ; 88 std::mt19937 rng (1234 + fmt ); 89 std::normal_distribution < float > nd (0.f ,1.f ); 90 std::uniform_int_distribution < int > code (-127 ,127 ); 91 std::uniform_real_distribution < float > sc (0.005f ,0.03f ); 92// pools; page table a shuffled permutation (the readers must follow it) 93 std::vector < int8_t > kq ,vq ; 94 std::vector < uint16_t > ks ,vs ,kh ,vh ; 95 std::vector < uint8_t > k4 ,v4 ;// q4_0: 8 blocks of {fp16 d, 16 code bytes} per row (kv_q4.hpp) 96constexpr int64_t B4 = 18 ,ROW4 = 8 * B4 ; 97if (fmt == 2 ) { 98 std::uniform_int_distribution < int > byte (0 ,255 ); 99// the scales as a q4_0 pool holds them: signed (ggml's d = max / -8) and spread over two decades, so a chunk's 100// cells mix signs and magnitudes (a positive-only bound on them overflowed FP16 on real K/V) 101 std::uniform_real_distribution < float > lg (std::log (0.01f ), std::log (1.0f )); 102auto sc4 = [& ](std::mt19937 & r ) {return std::exp (lg (r ))* (byte (r )& 1 ?-1.0f :1.0f ); }; 103k4 .resize (rows * ROW4 );v4 .resize (rows * ROW4 ); 104for (auto * pool : {& k4 ,& v4 }) 105for (int64_t b = 0 ;b < rows * 8 ;++ b ) { 106const uint16_t d = f2h (sc4 (rng )); 107 std::memcpy (pool -> data ()+ b * B4 ,& d ,2 ); 108for (int j = 0 ;j < 16 ;++ j ) (* pool )[b * B4 + 2 + j ]= (uint8_t )byte (rng ); 109 } 110 }else if (fmt == 1 ) { 111kq .resize (rows * HD );vq .resize (rows * HD );ks .resize (rows * 4 );vs .resize (rows * 4 ); 112for (auto & x :kq )x = (int8_t )code (rng ); 113for (auto & x :vq )x = (int8_t )code (rng ); 114for (auto & x :ks )x = f2h (sc (rng )); 115for (auto & x :vs )x = f2h (sc (rng )); 116 }else { 117kh .resize (rows * HD );vh .resize (rows * HD ); 118for (auto & x :kh )x = f2h (nd (rng )* 1.5f ); 119for (auto & x :vh )x = f2h (nd (rng )); 120 } 121 std::vector < int32_t > table (pages ); 122for (int64_t i = 0 ;i < pages ;++ i )table [i ]= (int32_t )i ; 123 std::shuffle (table .begin (),table .end (),rng ); 124if (const char * mask = std::getenv ("QA_MASK_PAGES" )) { 125const bool all = std::strcmp (mask ,"all" )== 0 ; 126for (size_t i = 0 ;i < table .size ();++ i ) 127if (all || i %7 == 0 )table [i ]= -1 ; 128 } 129// queries at positions ctx - nq .. ctx - 1 130const int64_t cap = k::qsa_selection_width (ctx ,s ); 131 std::vector < int32_t > ids ((size_t ) (nq * cap ),0 ),steps ((size_t ) (nq * k::kStepCount ),0 ); 132 std::vector < float > q ((size_t ) (nq * NH * HD )); 133for (auto & x :q )x = nd (rng )* 2.0f ; 134 std::vector < int32_t > old_cells ; 135for (int64_t i = 0 ;i < nq ;++ i ) { 136const int64_t pos = ctx - nq + i ,nkv = pos + 1 ; 137const int64_t w = k::qsa_selection_width (nkv ,s ); 138int32_t * sel = ids .data ()+ i * cap ; 139steps [i * k::kStepCount + k::kStepWidth ]= (int32_t )w ; 140if (w == nkv ) { 141for (int64_t c = 0 ;c < w ;++ c )sel [c ]= (int32_t )c ; 142continue ; 143 } 144if (std::getenv ("QA_BLOCK4" )) {// the engine's selections are whole blocks of four cells (idx_block): 128 recent blocks + random older ones, then the tail cells 145 std::mt19937 r3 ((unsigned ) (i /4 )* 2654435761u + 7 ); 146const int64_t nbk = nkv /4 ,take = std::min < int64_t > (nbk ,w /4 ); 147 std::vector < int32_t > blocks ; 148for (int64_t b = std::max < int64_t > (0 ,nbk - 128 );b < nbk ;++ b )blocks .push_back ((int32_t )b ); 149 std::vector < int32_t > rest ; 150for (int64_t b = 0 ;b < std::max < int64_t > (0 ,nbk - 128 );++ b )rest .push_back ((int32_t )b ); 151 std::shuffle (rest .begin (),rest .end (),r3 ); 152for (int64_t b = 0 ; (int64_t )blocks .size ()< take && b < (int64_t )rest .size ();++ b )blocks .push_back (rest [b ]); 153 std::sort (blocks .begin (),blocks .end ()); 154 std::vector < int32_t > v ; 155for (int32_t b :blocks )for (int r = 0 ;r < 4 ;++ r )v .push_back (b * 4 + r ); 156for (int64_t c = nbk * 4 ;c < nkv ;++ c )v .push_back ((int32_t )c ); 157steps [i * k::kStepCount + k::kStepWidth ]= (int32_t )v .size (); 158 std::copy (v .begin (),v .end (),sel ); 159continue ; 160 } 161const int64_t recent = 512 ,older = w - recent ; 162if ((int64_t )old_cells .size ()!= older ) {// the older cells: a set that drifts ~3% per query 163 std::vector < int32_t > all ((size_t ) (nkv - recent )); 164for (int64_t c = 0 ;c < nkv - recent ;++ c )all [c ]= (int32_t )c ; 165 std::shuffle (all .begin (),all .end (),rng ); 166old_cells .assign (all .begin (),all .begin ()+ older ); 167 }else { 168 std::uniform_int_distribution < int64_t > pick (0 ,older - 1 ),any (0 ,nkv - recent - 1 ); 169for (int r = 0 ;r < older /32 ;++ r ) { 170const int32_t c = (int32_t )any (rng ); 171if (std::find (old_cells .begin (),old_cells .end (),c )== old_cells .end ())old_cells [pick (rng )]= c ; 172 } 173 } 174 std::vector < int32_t > v (old_cells ); 175for (int64_t c = nkv - recent ;c < nkv ;++ c )v .push_back ((int32_t )c ); 176 std::sort (v .begin (),v .end ()); 177 std::copy (v .begin (),v .end (),sel ); 178 } 179// QA_SAME=1 (S23 probe): every query reads the same first-w cells - the best case of any K/V sharing between queries 180// (all loads hit the caches); QA_SAME=2: query i reads a window of w cells at i / 4 * 64 (neighbours share ~90%) 181if (const char * sm = std::getenv ("QA_SAME" )) { 182const int mode = std::atoi (sm ); 183for (int64_t i = 0 ;i < nq ;++ i ) { 184const int64_t w = steps [i * k::kStepCount + k::kStepWidth ],nkv = ctx - nq + i + 1 ; 185if (w == nkv )continue ; 186if (mode == 3 || mode == 4 ) {// 3: w random cells per query (no overlap beyond chance); 4: a 4-query group shares 187// one random set of w - 512 cells (the union of 4 queries = 1x), the last 512 cells recent 188 std::mt19937 r3 ((unsigned ) (mode == 3 ?i :i /4 )* 2654435761u + 7 ); 189 std::vector < int32_t > all ((size_t ) (nkv - 512 )); 190for (int64_t c = 0 ;c < nkv - 512 ;++ c )all [c ]= (int32_t )c ; 191 std::shuffle (all .begin (),all .end (),r3 ); 192 std::vector < int32_t > v (all .begin (),all .begin ()+ (w - 512 )); 193for (int64_t c = nkv - 512 ;c < nkv ;++ c )v .push_back ((int32_t )c ); 194 std::sort (v .begin (),v .end ()); 195 std::copy (v .begin (),v .end (),ids .data ()+ i * cap ); 196continue ; 197 } 198const int64_t base = mode == 1 ?0 : std::min < int64_t > (i /4 * 64 ,nkv - w ); 199for (int64_t c = 0 ;c < w ;++ c )ids [i * cap + c ]= (int32_t ) (base + c ); 200 } 201 } 202// QA_DUMP=<file> QA_LAYER=n (S23 probe): the engine's own selections (STRATA_QSA_DUMP records {layer, pos0, T, cap} + 203// T*cap cells): the last record of that layer, whose T must be nq and pos0 + T ctx 204if (const char * df = std::getenv ("QA_DUMP" )) { 205const int want = std::getenv ("QA_LAYER" ) ? std::atoi (std::getenv ("QA_LAYER" )) :3 ; 206 std::FILE * f = std::fopen (df ,"rb" ); 207if (!f ) { std::fprintf (stderr ,"QA_DUMP: cannot open\n" ); std::exit (2 ); } 208int32_t h [4 ]; 209long at = -1 ; 210while (std::fread (h ,4 ,4 ,f )== 4 ) { 211const long data = std::ftell (f ); 212if (h [0 ]== want && h [2 ]== nq && h [1 ]+ h [2 ]== ctx && h [3 ]== cap )at = data ; 213 std::fseek (f , (long )h [2 ]* h [3 ]* 4 ,SEEK_CUR ); 214 } 215if (at < 0 ) { std::fprintf (stderr ,"QA_DUMP: no record for layer %d, T %lld, ctx %lld\n" ,want , (long long )nq , (long long )ctx ); std::exit (2 ); } 216 std::fseek (f ,at ,SEEK_SET ); 217if (std::fread (ids .data (),4 ,ids .size (),f )!= ids .size ()) std::exit (2 ); 218 std::fclose (f ); 219for (int grp : {2 ,4 ,8 ,16 }) {// the union of `grp` neighbouring queries' cells against grp x the width 220double su = 0 ,sw = 0 ; 221for (int64_t i0 = 0 ;i0 + grp <=nq ;i0 += grp * 16 ) { 222 std::vector < int32_t > u ; 223for (int g2 = 0 ;g2 < grp ;++ g2 ) { 224const int64_t w = steps [(i0 + g2 )* k::kStepCount + k::kStepWidth ]; 225u .insert (u .end (),ids .begin ()+ (i0 + g2 )* cap ,ids .begin ()+ (i0 + g2 )* cap + w ); 226sw += (double )w ; 227 } 228 std::sort (u .begin (),u .end ()); 229su += (double ) (std::unique (u .begin (),u .end ())- u .begin ()); 230 } 231 std::printf ("QA_DUMP overlap: %d-query groups read %.2fx the cells of one query (%.1f%% of %d x)\n" ,grp , 232su * grp /sw ,100.0 * su /sw ,grp ); 233 } 234 } 235 k::QsaAttnPools pl ; 236if (fmt == 2 ) {pl .k_q4 = up (k4 );pl .v_q4 = up (v4 ); } 237else if (fmt == 1 ) {pl .k_q = up (kq );pl .v_q = up (vq );pl .k_scale = up (ks );pl .v_scale = up (vs ); } 238else {pl .k_pool = up (kh );pl .v_pool = up (vh ); } 239// a q4_0 value: block d / 32 of the row, element j = d % 32 in the low nibble of byte j (j < 16), else the high 240// nibble of byte j - 16, minus 8, times the block's scale 241auto q4v = [& ](const std::vector < uint8_t >& pool ,int64_t row ,int64_t d ) { 242const uint8_t * blk = pool .data ()+ row * ROW4 + (d /32 )* B4 ; 243uint16_t sc ; 244 std::memcpy (& sc ,blk ,2 ); 245const int j = (int ) (d %32 ); 246const int code = j < 16 ? (blk [2 + j ]& 0x0F ) : (blk [2 + j - 16 ] >>4 ); 247return (double ) (code - 8 )* h2f (sc ); 248 }; 249pl .page_table = up (table ); 250const int32_t * d_ids = up (ids ); 251const int32_t * d_steps = up (steps ); 252const float * d_q = up (q ); 253float * d_old = nullptr ,* d_new = nullptr ,* scratch = nullptr ; 254const int64_t batch = 32 ; 255ck (DPCT_CHECK_ERROR (d_old = (float * )sycl::malloc_device ( 256nq * NH * HD * 4 , dpct::get_in_order_queue ())), 257"malloc" ); 258ck (DPCT_CHECK_ERROR (d_new = (float * )sycl::malloc_device ( 259nq * NH * HD * 4 , dpct::get_in_order_queue ())), 260"malloc" ); 261ck (DPCT_CHECK_ERROR ( 262scratch = (float * )sycl::malloc_device ( 263batch * k::qsa_decode_attn_scratch_floats (cap ,s )* 4 , 264 dpct::get_in_order_queue ())), 265"malloc" ); 266auto old_run = [& ]() { 267for (int64_t t0 = 0 ;t0 < nq ;t0 += batch ) 268 k::qsa_decode_attn_batch (d_q + t0 * NH * HD ,pl ,d_ids + t0 * cap ,d_steps + t0 * k::kStepCount ,cap ,s , 269scratch ,d_old + t0 * NH * HD , std::min (batch ,nq - t0 ),nullptr ); 270 }; 271float * d_pc = nullptr ;// SYCL port: qsa_prompt_attn_percell's output 272ck (DPCT_CHECK_ERROR (d_pc = (float * )sycl::malloc_device (nq * NH * HD * 4 , dpct::get_in_order_queue ())),"malloc" ); 273auto pc_run = [& ]() { 274for (int64_t t0 = 0 ;t0 < nq ;t0 += batch ) 275 k::qsa_prompt_attn_percell (d_q + t0 * NH * HD ,pl ,d_ids + t0 * cap ,d_steps + t0 * k::kStepCount ,cap ,s , 276scratch ,d_pc + t0 * NH * HD , std::min (batch ,nq - t0 ),nullptr ); 277 }; 278bool refused = false;// SYCL port: the XMX kernel takes int8 and FP16 pools; the per-cell check runs anyway 279auto new_run = [& ]() { 280if (!k::qsa_prompt_attn_batch (d_q ,pl ,d_ids ,d_steps ,cap ,s ,d_new ,nq ,nullptr )) { 281if (!refused ) std::fprintf (stderr ,"qsa_prompt_attn_batch refused the pools: per-cell check only\n" ); 282refused = true; 283 } 284 }; 285old_run (); 286new_run (); 287pc_run (); 288ck (DPCT_CHECK_ERROR (dpct::get_current_device ().queues_wait_and_throw ()), 289"run" ); 290 std::vector < float > o ((size_t ) (nq * NH * HD )),nw (o .size ()),pcw (o .size ()); 291ck (DPCT_CHECK_ERROR (dpct::get_in_order_queue ().memcpy (pcw .data (),d_pc ,pcw .size ()* 4 ).wait ()),"down" ); 292ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 293 .memcpy (o .data (),d_old ,o .size ()* 4 ) 294 .wait ()), 295"down" ); 296ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 297 .memcpy (nw .data (),d_new ,nw .size ()* 4 ) 298 .wait ()), 299"down" ); 300if (refused )nw = o ;// SYCL port: nothing of the XMX kernel to compare 301 { 302uint64_t hh = 1469598103934665603ull ; 303for (float x :o ) {uint32_t u ; std::memcpy (& u ,& x ,4 );hh = (hh ^u )* 1099511628211ull ; } 304 std::printf ("FP32HASH %016llx\n" , (unsigned long long )hh ); 305 } 306 {// a hash of the new kernel's output bits (bitwise A/B of two builds / switches) 307uint64_t hh = 1469598103934665603ull ; 308for (float x :nw ) {uint32_t u ; std::memcpy (& u ,& x ,4 );hh = (hh ^u )* 1099511628211ull ; } 309 std::printf ("NEWHASH %016llx\n" , (unsigned long long )hh ); 310 } 311// 1. FP64 reference on a sample of queries 312double err_old = 0 ,err_new = 0 ,err_pc = 0 ,ref_scale = 0 ; 313for (int64_t i = 0 ;i < nq ;i += std::max < int64_t > (1 ,nq /16 )) { 314const int64_t w = steps [i * k::kStepCount + k::kStepWidth ]; 315const int32_t * sel = ids .data ()+ i * cap ; 316for (int64_t h = 0 ;h < NH ;++ h ) { 317const int64_t kvh = h / (NH /NKV ); 318 std::vector < double > sco ((size_t )w ); 319double mx = -1e300 ; 320for (int64_t c = 0 ;c < w ;++ c ) { 321const int64_t cell = sel [c ],row = ((int64_t )table [cell /PS ]* NKV + kvh )* PS + cell %PS ; 322if (table [cell /PS ]< 0 ) { 323sco [c ]= - INFINITY ; 324continue ; 325 } 326double a = 0 ; 327for (int64_t d = 0 ;d < HD ;++ d ) { 328const double kv = fmt == 2 ?q4v (k4 ,row ,d ) 329 :fmt == 1 ? (double )kq [row * HD + d ]* h2f (ks [row * 4 + d /64 ]) :h2f (kh [row * HD + d ]); 330a += (double )q [(i * NH + h )* HD + d ]* kv ; 331 } 332sco [c ]= a /16.0 ; 333mx = std::max (mx ,sco [c ]); 334 } 335double l = 0 ; 336for (auto & x :sco ) {x = std::exp (x - mx );l += x ; } 337for (int64_t d = 0 ;d < HD ;++ d ) { 338double a = 0 ; 339for (int64_t c = 0 ;c < w ;++ c ) { 340const int64_t cell = sel [c ],row = ((int64_t )table [cell /PS ]* NKV + kvh )* PS + cell %PS ; 341if (table [cell /PS ]< 0 )continue ; 342const double vv = fmt == 2 ?q4v (v4 ,row ,d ) 343 :fmt == 1 ? (double )vq [row * HD + d ]* h2f (vs [row * 4 + d /64 ]) :h2f (vh [row * HD + d ]); 344a += sco [c ]* vv ; 345 } 346const double r = l > 0 ?a /l :0.0 ; 347const size_t at = (size_t ) ((i * NH + h )* HD + d ); 348if (std::getenv ("PARITY_DUMP2" )&& fmt == 1 && i == 0 && h == 0 && d == 0 ) { 349for (int c = 0 ;c < 4 ;++ c )for (int n = 0 ;n < 6 ;++ n ) { 350const int64_t cell = sel [c ],row = ((int64_t )table [cell /PS ]* NKV + 0 )* PS + cell %PS ; 351double a2 = 0 ; 352for (int64_t dd = 0 ;dd < HD ;++ dd )a2 += (double )q [(i * NH + n )* HD + dd ]* ((double )kq [row * HD + dd ]* h2f (ks [row * 4 + dd /64 ])); 353 std::printf ("S c%d n%d ref %.5f new %.5f\n" ,c ,n ,a2 /16.0 * 1.4426950408889634 , (double )nw [c * 16 + n ]); 354 } 355 } 356if (std::getenv ("PARITY_DUMP" )&& i == 0 && (d == 3 || d == 70 )) 357 std::printf ("dump h%lld d%lld ref %.5f old %.5f new %.5f\n" , (long long )h , (long long )d ,r , (double )o [at ], (double )nw [at ]); 358ref_scale = std::max (ref_scale , std::fabs (r )); 359err_old = std::max (err_old , std::fabs (o [at ]- r )); 360err_new = std::max (err_new , std::fabs (nw [at ]- r )); 361err_pc = std::max (err_pc , std::fabs (pcw [at ]- r )); 362 } 363 } 364 } 365// 2. new vs old everywhere 366double diff = 0 ,scale = 0 ,diff_pc = 0 ; 367for (size_t i = 0 ;i < o .size ();++ i ) { 368diff = std::max (diff , (double ) std::fabs (o [i ]- nw [i ])); 369diff_pc = std::max (diff_pc , (double ) std::fabs (o [i ]- pcw [i ])); 370scale = std::max (scale , (double ) std::fabs (o [i ])); 371 } 372// 3. speed 373 dpct::event_ptr e0 ,e1 ; 374e0 = new sycl::event (); 375e1 = new sycl::event (); 376float ms_old = 0 ,ms_new = 0 ; 377 dpct::sync_barrier (e0 ); 378for (int r = 0 ;r < reps ;++ r )old_run (); 379 dpct::sync_barrier (e1 ); 380ck (DPCT_CHECK_ERROR (e1 -> wait_and_throw ()),"time" ); 381ms_old = 382 (e1 -> get_profiling_info < sycl::info::event_profiling::command_end > ()- 383e0 -> get_profiling_info < sycl::info::event_profiling::command_start > ()) / 3841000000.0f ; 385 dpct::sync_barrier (e0 ); 386for (int r = 0 ;r < reps ;++ r )new_run (); 387 dpct::sync_barrier (e1 ); 388ck (DPCT_CHECK_ERROR (e1 -> wait_and_throw ()),"time" ); 389ms_new = 390 (e1 -> get_profiling_info < sycl::info::event_profiling::command_end > ()- 391e0 -> get_profiling_info < sycl::info::event_profiling::command_start > ()) / 3921000000.0f ; 393float ms_pc = 0 ; 394 dpct::sync_barrier (e0 ); 395for (int r = 0 ;r < reps ;++ r )pc_run (); 396 dpct::sync_barrier (e1 ); 397ck (DPCT_CHECK_ERROR (e1 -> wait_and_throw ()),"time" ); 398ms_pc = (e1 -> get_profiling_info < sycl::info::event_profiling::command_end > ()- 399e0 -> get_profiling_info < sycl::info::event_profiling::command_start > ()) /1000000.0f ; 400// SYCL port: the per-cell kernel - FP32 like the warp kernel, another summation order 401const bool ok_pc = err_pc <= std::max (4.0 * err_old ,1e-6 * ref_scale )&& diff_pc <=1e-4 * scale ; 402 std::printf ("%s %s per-cell: vs FP64 %.3g (warp kernel %.3g), vs the warp kernel %.2g of scale; %.3f -> %.3f ms per chunk (%.2fx)\n" , 403ok_pc ?"PASS" :"FAIL" ,fmt == 2 ?"q4_0" :fmt == 1 ?"int8" :"fp16" ,err_pc ,err_old ,diff_pc /scale , 404ms_old /reps ,ms_pc /reps ,ms_old /ms_pc ); 405 sycl::free ((void * )d_pc , dpct::get_in_order_queue ()); 406if (std::getenv ("QA_FP32_GRAPH" )) { 407auto * queue = & dpct::get_in_order_queue (); 408constexpr int calls = 32 ; 409 dpct::experimental::command_graph_ptr graph ; 410 dpct::experimental::begin_recording (queue ); 411for (int i = 0 ;i < calls ;++ i )old_run (); 412 dpct::experimental::end_recording (queue ,& graph ); 413auto executable = graph -> finalize (); 414delete graph ; 415 std::vector < double > times ; 416for (int i = 0 ;i < 20 ;++ i ) { 417auto begin = queue -> ext_oneapi_submit_barrier (); 418queue -> ext_oneapi_graph (executable ); 419auto end = queue -> ext_oneapi_submit_barrier (); 420end .wait_and_throw (); 421if (i >=5 ) 422times .push_back (double ( 423end .get_profiling_info < sycl::info::event_profiling::command_end > ()- 424begin .get_profiling_info < sycl::info::event_profiling::command_start > ()) /1000 /calls ); 425 } 426 std::sort (times .begin (),times .end ()); 427 std::printf ("DECODEGRAPH fmt=%d ctx=%lld nq=%lld us=%.3f\n" , 428fmt , (long long )ctx , (long long )nq ,times [times .size () /2 ]); 429 } 430const bool ok1 = err_new <= std::max (4.0 * err_old ,1e-6 * ref_scale )&& ok_pc ; 431const bool ok2 = diff <=1e-4 * scale ; 432 std::printf ("%s %s ctx %lld, %lld queries: vs FP64 old %.3g new %.3g (output scale %.3g); new vs old %.3g (%.2g of " 433"scale); %.3f -> %.3f ms per chunk (%.2fx)\n" , 434ok1 && ok2 ?"PASS" :"FAIL" ,fmt == 2 ?"q4_0" :fmt == 1 ?"int8" :"fp16" , (long long )ctx , (long long )nq ,err_old , 435err_new ,ref_scale ,diff ,diff /scale ,ms_old /reps ,ms_new /reps ,ms_old /ms_new ); 436 sycl::free ((void * )d_ids , dpct::get_in_order_queue ()); 437 sycl::free ((void * )d_steps , dpct::get_in_order_queue ()); 438 sycl::free ((void * )d_q , dpct::get_in_order_queue ()); 439 sycl::free ((void * )d_old , dpct::get_in_order_queue ()); 440 sycl::free ((void * )d_new , dpct::get_in_order_queue ()); 441 sycl::free ((void * )scratch , dpct::get_in_order_queue ()); 442 sycl::free ((void * )pl .k_q , dpct::get_in_order_queue ()); 443 sycl::free ((void * )pl .v_q , dpct::get_in_order_queue ()); 444 sycl::free ((void * )pl .k_scale , dpct::get_in_order_queue ()); 445 sycl::free ((void * )pl .v_scale , dpct::get_in_order_queue ()); 446 sycl::free ((void * )pl .k_pool , dpct::get_in_order_queue ()); 447 sycl::free ((void * )pl .v_pool , dpct::get_in_order_queue ()); 448 sycl::free ((void * )pl .page_table , dpct::get_in_order_queue ()); 449 sycl::free ((void * )pl .k_q4 , dpct::get_in_order_queue ()); 450 sycl::free ((void * )pl .v_q4 , dpct::get_in_order_queue ()); 451return ok1 && ok2 ?0 :1 ; 452} 453}// namespace 454 455int main (int argc ,char ** argv ) { 456setenv ("STRATA_PROMPT_ATTN_XMX" ,"1" ,0 );// SYCL port: the batched kernel under test is the XMX one (opt-in in the engine) 457#if defined(__HIP_PLATFORM_AMD__ ) 458// S6: on AMD the kernel under test is the RDNA4 matrix-core one (opt-in in the engine); other cards skip 459 { 460int dev = 0 ; 461hipDeviceProp_t prop {}; 462if (hipGetDevice (& dev )!= hipSuccess || hipGetDeviceProperties (& prop ,dev )!= hipSuccess )return 2 ; 463if (std::strncmp (prop .gcnArchName ,"gfx12" ,5 )!= 0 && std::strncmp (prop .gcnArchName ,"gfx11" ,5 )!= 0 ) { 464 std::printf ("SKIP: %s is not gfx11 / gfx12 (the matrix-core prompt attention)\n" ,prop .gcnArchName ); 465return 77 ; 466 } 467#if defined(_WIN32 ) 468_putenv_s ("STRATA_HIP_WMMA" ,"1" ); 469#else 470setenv ("STRATA_HIP_WMMA" ,"1" ,1 ); 471#endif 472 } 473#endif 474const int64_t ctx = argc > 1 ? std::atoll (argv [1 ]) :32768 ; 475const int64_t nq = argc > 2 ? std::atoll (argv [2 ]) :2048 ; 476const int reps = argc > 3 ? std::atoi (argv [3 ]) :5 ; 477const bool int8_only = argc > 4 && std::strcmp (argv [4 ],"--int8-only" )== 0 ; 478if (argc > 4 && !int8_only ) { 479 std::fprintf (stderr ,"usage: qsa_prompt_attn_parity [context] [queries] [reps] [--int8-only]\n" ); 480return 2 ; 481 } 482int fails = 0 ; 483fails += run (1 ,ctx ,nq ,reps ); 484#if !defined(__HIP_PLATFORM_AMD__ ) 485if (!int8_only ) { 486fails += run (0 ,ctx ,nq ,reps );// FP16 KV: the RDNA4 kernel takes int8 KV only 487fails += run (2 ,ctx ,nq ,reps );// Q4_0 KV (mode 4) 488fails += run (2 ,1500 , std::min < int64_t > (nq ,1500 ),reps ); 489 } 490#endif 491fails += run (1 ,1500 , std::min < int64_t > (nq ,1500 ),reps );// short context: the selection is every cell 492fails += run (1 ,2100 , std::min < int64_t > (nq ,256 ),reps );// the identity-to-sparse edge 493 std::printf ("FAILURES: %d\n" ,fails ); 494return fails ; 495}