okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5af104b51
main
1// src/kernels/kv_hybrid_parity.cpp - hybrid K8V4 KV against host references (GPU, no model). 2// 3// Exercises the EXACT composed calls the engine's hybrid path makes (layer.cpp / prefill.cpp): the q8/q4 4// append and gather kernels invoked with the unused half's pointers folded onto the used pool, and the 5// fused decode attention's KV_MODE 3 (INT8 K, rotated Q4_0 V) followed by the output's inverse Hadamard. 6// 1. K: INT8 codes + FP16 scales bitwise vs the host reference (kv_q8.hpp's rules); 7// 2. V: after fwht256, Q4_0 blocks bitwise vs the host reference (kv_q4.hpp's rules, PR #21); 8// 3. the composed gathers bitwise vs the host dequantization of those codes; 9// 4. qsa_decode_attn<3> + output fwht vs a host attention over the SAME dequantized K/V (fp32 math), and 10// bounded against the fp32-true attention from the unquantized K/V. 11#define DPCT_PROFILING_ENABLED 12#include <sycl/sycl.hpp> 13#include <dpct/dpct.hpp> 14#include "strata/kernels/f16_bits.hpp" 15#include "strata/kernels/kv_q4.hpp" 16#include "strata/kernels/kv_q8.hpp" 17#include "strata/kernels/qsa.hpp" 18#include "strata/kernels/qsa_decode_attn.hpp" 19#include "strata/kernels/qsa_prompt_attn.hpp" 20 21#include <algorithm> 22#include <cmath> 23#include <cstdio> 24#include <cstdlib> 25#include <cstring> 26#include <random> 27#include <vector> 28 29namespace k= strata::kernels; 30 31namespace { 32int g_fail = 0 ; 33void ck (dpct::err0 e ,const char * w ) { 34/* 35DPCT1009: SYCL reports errors using exceptions and does not use error 36codes. Please replace the "get_error_string_dummy(...)" with a real 37error-handling function. 38*/ 39} 40template < typename T > T * dalloc (size_t n ) { 41T * p = nullptr ; 42ck (DPCT_CHECK_ERROR (p = (T * )sycl::malloc_device ( 43n * sizeof (T )+ 64 , dpct::get_in_order_queue ())), 44"malloc" ); 45ck (DPCT_CHECK_ERROR ( 46 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memset (p ,0 ,n * sizeof (T )+ 64 ).wait ()), 47"memset" ); 48return p ; 49} 50 51// kv_q4_parity.cpp's exact CPU references. 52void fwht256_host_reference (float * x ) { 53constexpr int n = 256 ; 54for (int s = 1 ;s < n ;s *=2 ) { 55for (int i = 0 ;i < n ;i += 2 * s ) { 56for (int j = 0 ;j < s ;++ j ) { 57const float u = x [i + j ]; 58const float v = x [i + j + s ]; 59x [i + j ]= u + v ; 60x [i + j + s ]= u - v ; 61 } 62 } 63 } 64const float scale = 1.0f /16.0f ; 65for (int i = 0 ;i < n ;++ i )x [i ] *=scale ; 66} 67void quantize_block_q4_0_host (const float * x , k::block_q4_0 & blk ) { 68float amax = 0.0f ,max_val = 0.0f ; 69for (int i = 0 ;i < 32 ;++ i ) { 70const float v = x [i ]; 71if (amax < std::fabs (v )) {amax = std::fabs (v );max_val = v ; } 72 } 73const float d = max_val /-8.0f ; 74const float id = d != 0.0f ?1.0f /d :0.0f ; 75blk .d = k::f16_from_f32 (d ); 76for (int i = 0 ;i < 16 ;++ i ) { 77const int q0 = (int ) (x [i ]* id + 8.5f ); 78const int q1 = (int ) (x [i + 16 ]* id + 8.5f ); 79const uint8_t xi0 = (uint8_t ) std::min (15 , std::max (0 ,q0 )); 80const uint8_t xi1 = (uint8_t ) std::min (15 , std::max (0 ,q1 )); 81blk .qs [i ]= xi0 | (xi1 <<4 ); 82 } 83} 84}// namespace 85 86int main () { 87// SYCL port: qsa_prompt_attn's tensor-core (mma.sync PTX) kernel is not ported; on SYCL the prompt attention this 88// test's last step checks exists as the XMX kernel (opt-in in the engine, where the FP32 fallback is faster). 89// Test the kernel the port has, unless the caller chose (STRATA_PROMPT_ATTN_XMX=0 shows the refusal). 90setenv ("STRATA_PROMPT_ATTN_XMX" ,"1" ,0 ); 91 std::printf ("=== Running kv_hybrid_parity test ===\n" ); 92 k::QsaShapes s = k::qsa_real_shapes (); 93s .page_size = 64 ; 94const int H = (int )s .n_head_kv ,D = (int )s .head_dim ,P = (int )s .page_size ,G = D / k::KV_Q8_GROUP ; 95const int QH = (int )s .n_head ,PER = QH /H ;// query heads per KV head 96const int pages = 24 ,cells = pages * P ;// n_chunks >= n_head: the batched merge overlaps queries below that 97 std::mt19937 rng (17 ); 98 std::normal_distribution < float > nd (0.0f ,1.0f ); 99 100 std::vector < int32_t > table (pages ); 101for (int i = 0 ;i < pages ;++ i )table [i ]= (i * 5 + 3 ) %pages ; 102int32_t * d_table = dalloc < int32_t > (pages ); 103/* 104DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 105in the original code the source host memory is pageable memory. If the 106memory is not pageable, call wait() on event return by memcpy API to ensure 107synchronization behavior. 108*/ 109ck (DPCT_CHECK_ERROR ( 110 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_table ,table .data (),pages * 4 ).wait ()), 111"table" ); 112 113 std::vector < float > K ((size_t )cells * H * D ),V ((size_t )cells * H * D ); 114for (auto & x :K )x = nd (rng )* 0.5f ; 115for (auto & x :V )x = nd (rng )* 0.5f ; 116 117// ---- host reference: K in INT8 (kv_q8.hpp), rotated V in Q4_0 (kv_q4.hpp) 118 std::vector < int8_t > rk ((size_t )cells * H * D ); 119 std::vector < uint16_t > rk_s ((size_t )cells * H * G ); 120for (int c = 0 ;c < cells ;++ c ) 121for (int h = 0 ;h < H ;++ h ) 122for (int g = 0 ;g < G ;++ g ) { 123float amax = 0.0f ; 124for (int t = 0 ;t < k::KV_Q8_GROUP ;++ t ) 125amax = std::max (amax , std::fabs (K [((size_t )c * H + h )* D + g * k::KV_Q8_GROUP + t ])); 126const uint16_t sb = k::f16_from_f32 (amax /127.0f ); 127const float sf = k::f32_from_f16 (sb ); 128for (int t = 0 ;t < k::KV_Q8_GROUP ;++ t ) { 129int q = 0 ; 130if (sf > 0.0f ) { 131q = (int ) std::nearbyint (K [((size_t )c * H + h )* D + g * k::KV_Q8_GROUP + t ] /sf );// rn: ties to even, as the kernel 132q = std::max (-127 , std::min (127 ,q )); 133 } 134rk [((size_t )c * H + h )* D + g * k::KV_Q8_GROUP + t ]= (int8_t )q ; 135 } 136rk_s [((size_t )c * H + h )* G + g ]= sb ; 137 } 138 std::vector < float > Vrot ((size_t )cells * H * D ); 139 std::memcpy (Vrot .data (),V .data (),V .size ()* 4 ); 140for (int c = 0 ;c < cells * H ;++ c )fwht256_host_reference (Vrot .data ()+ (size_t )c * D ); 141 std::vector < k::block_q4_0 > rv4 ((size_t )cells * H * (D /32 )); 142for (int c = 0 ;c < cells ;++ c ) 143for (int h = 0 ;h < H ;++ h ) 144for (int b = 0 ;b < D /32 ;++ b ) 145quantize_block_q4_0_host (& Vrot [((size_t )c * H + h )* D + b * 32 ], 146rv4 [((size_t )c * H + h )* (D /32 )+ b ]); 147 148// ---- device: one cell appended through the COMPOSED hybrid calls, then gathered 149const int64_t q4_row = (int64_t ) k::kv_q4_bytes_per_head (D ); 150int8_t * d_kq = dalloc < int8_t > ((size_t )cells * H * D ); 151uint16_t * d_ks = dalloc < uint16_t > ((size_t )cells * H * G ); 152uint8_t * d_v4 = dalloc < uint8_t > ((size_t )cells * H * q4_row ); 153float * d_kcur = dalloc < float > ((size_t )H * D ),* d_vcur = dalloc < float > ((size_t )H * D ); 154int32_t * d_step = dalloc < int32_t > (k::kStepCount ); 155 156int append_bad = 0 ,gather_bad = 0 ; 157for (int c = 0 ;c < cells ;++ c ) { 158ck (DPCT_CHECK_ERROR ( 159 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 160 .memcpy (d_kcur ,& K [(size_t )c * H * D ], (size_t )H * D * 4 ) 161 .wait ()), 162"kcur" ); 163ck (DPCT_CHECK_ERROR ( 164 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 165 .memcpy (d_vcur ,& V [(size_t )c * H * D ], (size_t )H * D * 4 ) 166 .wait ()), 167"vcur" ); 168int32_t step [k::kStepCount ]; 169 k::qsa_step_fill (step ,c ,s ); 170/* 171DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 172assuming in the original code the source host memory is pageable memory. 173If the memory is not pageable, call wait() on event return by memcpy API 174to ensure synchronization behavior. 175*/ 176ck (DPCT_CHECK_ERROR ( 177 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_step ,step ,sizeof step ).wait ()), 178"step" ); 179// exactly what qsa_layer's hybrid branch does 180 k::fwht256_inplace_cuda (d_vcur ,H ,nullptr ); 181 k::kv_append_q8_step (d_kq ,d_kq ,d_ks ,d_ks ,d_table ,d_step ,d_kcur ,d_kcur ,s ,nullptr ,nullptr ); 182 k::kv_append_q4_step (d_v4 ,d_v4 ,d_table ,d_step ,d_vcur ,d_vcur ,s ,nullptr ,nullptr ); 183/* 184DPCT1010: SYCL uses exceptions to report errors and does not use 185the error codes. The cudaGetLastError function call was replaced with 0. 186You need to rewrite this code. 187*/ 188ck (0 != 0 ?0 :0 ,"append" ); 189 } 190ck (DPCT_CHECK_ERROR (dpct::get_current_device ().queues_wait_and_throw ()), 191"append sync" ); 192 193 std::vector < int8_t > hk ((size_t )cells * H * D ); 194 std::vector < uint16_t > hks ((size_t )cells * H * G ); 195 std::vector < uint8_t > hv4 ((size_t )cells * H * q4_row ); 196ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 197 .memcpy (hk .data (),d_kq ,hk .size ()) 198 .wait ()), 199"kq" ); 200ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 201 .memcpy (hks .data (),d_ks ,hks .size ()* 2 ) 202 .wait ()), 203"ks" ); 204ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 205 .memcpy (hv4 .data (),d_v4 ,hv4 .size ()) 206 .wait ()), 207"v4" ); 208// the pools are PHYSICAL (page, head, slot); the reference is by LOGICAL cell - compare through the table 209const k::block_q4_0 * hv4b = reinterpret_cast < const k::block_q4_0 *> (hv4 .data ()); 210for (int c = 0 ;c < cells ;++ c ) { 211const long long row = ((long long )table [c /P ]* H ); 212for (int h = 0 ;h < H ;++ h ) { 213const long long r = (row + h )* P + c %P ; 214for (int d = 0 ;d < D ;++ d ) 215if (hk [(size_t )r * D + d ]!= rk [((size_t )c * H + h )* D + d ])++ append_bad ; 216for (int g = 0 ;g < G ;++ g ) 217if (hks [(size_t )r * G + g ]!= rk_s [((size_t )c * H + h )* G + g ])++ append_bad ; 218for (int b = 0 ;b < D /32 ;++ b ) { 219const k::block_q4_0 & got = hv4b [(size_t )r * (D /32 )+ b ]; 220if (got .d != rv4 [((size_t )c * H + h )* (D /32 )+ b ].d || 221 std::memcmp (got .qs ,rv4 [((size_t )c * H + h )* (D /32 )+ b ].qs ,16 )!= 0 )++ append_bad ; 222 } 223 } 224 } 225 std::printf ("[1/3] composed appends: %s (%d bad values of %zu K / %zu V)\n" , 226append_bad == 0 ?"PASS" :"FAIL" ,append_bad ,hk .size (),rv4 .size ()); 227 228// gather every cell (ids = all, ascending - the identity selection for width = cells) 229 std::vector < int32_t > ids (cells ); 230for (int i = 0 ;i < cells ;++ i )ids [i ]= i ; 231int32_t * d_ids = dalloc < int32_t > (cells ); 232/* 233DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 234in the original code the source host memory is pageable memory. If the 235memory is not pageable, call wait() on event return by memcpy API to ensure 236synchronization behavior. 237*/ 238ck (DPCT_CHECK_ERROR ( 239 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_ids ,ids .data (),cells * 4 ).wait ()), 240"ids" ); 241int32_t step [k::kStepCount ]; 242 k::qsa_step_fill (step ,cells - 1 ,s ); 243step [k::kStepWidth ]= cells ;// select everything 244/* 245DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 246in the original code the source host memory is pageable memory. If the 247memory is not pageable, call wait() on event return by memcpy API to ensure 248synchronization behavior. 249*/ 250ck (DPCT_CHECK_ERROR ( 251 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_step ,step ,sizeof step ).wait ()), 252"step2" ); 253uint16_t * d_ksc = dalloc < uint16_t > ((size_t )cells * H * D ),* d_vsc = dalloc < uint16_t > ((size_t )cells * H * D ); 254 k::kv_gather_q8_step (d_kq ,d_kq ,d_ks ,d_ks ,d_table ,d_ids ,d_step ,cells ,s ,d_ksc ,d_ksc ,nullptr ); 255 k::kv_gather_q4_step (d_v4 ,d_v4 ,d_table ,d_ids ,d_step ,cells ,s ,d_vsc ,d_vsc ,nullptr ); 256ck (DPCT_CHECK_ERROR (dpct::get_current_device ().queues_wait_and_throw ()), 257"gather sync" ); 258 std::vector < uint16_t > h_ksc ((size_t )cells * H * D ),h_vsc ((size_t )cells * H * D ); 259ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 260 .memcpy (h_ksc .data (),d_ksc ,h_ksc .size ()* 2 ) 261 .wait ()), 262"ksc" ); 263ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 264 .memcpy (h_vsc .data (),d_vsc ,h_vsc .size ()* 2 ) 265 .wait ()), 266"vsc" ); 267for (int c = 0 ;c < cells ;++ c ) 268for (int h = 0 ;h < H ;++ h ) 269for (int d = 0 ;d < D ;++ d ) { 270const float kref = k::f32_from_f16 (k::f16_from_f32 ( 271 (float )rk [((size_t )c * H + h )* D + d ]* k::f32_from_f16 (rk_s [((size_t )c * H + h )* G + d / k::KV_Q8_GROUP ]))); 272if (k::f32_from_f16 (h_ksc [((size_t )c * H + h )* D + d ])!= kref )++ gather_bad ; 273const k::block_q4_0 & b = rv4 [((size_t )c * H + h )* (D /32 )+ d /32 ]; 274const int nib = d %32 < 16 ? (b .qs [d %32 ]& 0x0F )- 8 : ((b .qs [d %32 - 16 ] >>4 )- 8 ); 275const float vref = k::f32_from_f16 (k::f16_from_f32 ((float )nib * k::f32_from_f16 (b .d ))); 276if (k::f32_from_f16 (h_vsc [((size_t )c * H + h )* D + d ])!= vref )++ gather_bad ; 277 } 278 std::printf ("[2/3] composed gathers: %s (%d bad of %zu)\n" , 279gather_bad == 0 ?"PASS" :"FAIL" ,gather_bad ,h_ksc .size ()); 280 281// ---- attention: qsa_decode_attn KV_MODE 3 over the hybrid pools + output fwht, vs fp32 references 282 std::vector < float > q ((size_t )QH * D ); 283for (auto & x :q )x = nd (rng ); 284float * d_q = dalloc < float > (q .size ()); 285/* 286DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming 287in the original code the source host memory is pageable memory. If the 288memory is not pageable, call wait() on event return by memcpy API to ensure 289synchronization behavior. 290*/ 291ck (DPCT_CHECK_ERROR ( 292 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_q ,q .data (),q .size ()* 4 ).wait ()), 293"q" ); 294 k::QsaAttnPools pools ; 295pools .k_q = d_kq ;pools .k_scale = d_ks ;pools .v_q4 = d_v4 ;pools .page_table = d_table ; 296float * d_scr = dalloc < float > (k::qsa_decode_attn_scratch_floats (cells ,s )+ 16 ); 297float * d_attn = dalloc < float > ((size_t )QH * D ); 298 k::qsa_decode_attn_step (d_q ,pools ,d_ids ,d_step ,cells ,s ,d_scr ,d_attn ,nullptr ); 299 k::fwht256_inplace_cuda (d_attn ,QH ,nullptr );// the engine's inverse rotation of the output 300ck (DPCT_CHECK_ERROR (dpct::get_current_device ().queues_wait_and_throw ()), 301"attn sync" ); 302 std::vector < float > h_attn ((size_t )QH * D ); 303ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 304 .memcpy (h_attn .data (),d_attn ,h_attn .size ()* 4 ) 305 .wait ()), 306"attn" ); 307 308// reference A: attention over the SAME dequantized K/V (isolates the kernel; fp32 host math) 309// reference B: attention over the TRUE unquantized K/V (bounds the format's error) 310auto host_attn = [& ](const std::vector < float >& Keq ,const std::vector < float >& Veq , std::vector < float >& out ) { 311out .assign ((size_t )QH * D ,0.0f ); 312for (int h = 0 ;h < QH ;++ h ) { 313const int kvh = h /PER ; 314 std::vector < float > sc (cells ); 315float m = -1e30f ; 316for (int c = 0 ;c < cells ;++ c ) { 317float d_ = 0.0f ; 318for (int t = 0 ;t < D ;++ t ) 319d_ += q [(size_t )h * D + t ]* Keq [((size_t )c * H + kvh )* D + t ]; 320sc [c ]= d_ / std::sqrt ((float )D ); 321m = std::max (m ,sc [c ]); 322 } 323float l = 0.0f ; 324for (int c = 0 ;c < cells ;++ c ) {sc [c ]= std::exp (sc [c ]- m );l += sc [c ]; } 325for (int c = 0 ;c < cells ;++ c ) 326for (int t = 0 ;t < D ;++ t )out [(size_t )h * D + t ]+= sc [c ] /l * Veq [((size_t )c * H + kvh )* D + t ]; 327 } 328 }; 329 std::vector < float > Kdq ((size_t )cells * H * D ),Vdq ((size_t )cells * H * D ); 330for (int c = 0 ;c < cells ;++ c ) 331for (int h = 0 ;h < H ;++ h ) 332for (int d = 0 ;d < D ;++ d ) { 333Kdq [((size_t )c * H + h )* D + d ]= 334 (float )rk [((size_t )c * H + h )* D + d ]* k::f32_from_f16 (rk_s [((size_t )c * H + h )* G + d / k::KV_Q8_GROUP ]); 335const k::block_q4_0 & b = rv4 [((size_t )c * H + h )* (D /32 )+ d /32 ]; 336const int nib = d %32 < 16 ? (b .qs [d %32 ]& 0x0F )- 8 : ((b .qs [d %32 - 16 ] >>4 )- 8 ); 337Vdq [((size_t )c * H + h )* D + d ]= (float )nib * k::f32_from_f16 (b .d ); 338 } 339// un-rotate the dequantized V (the kernel returns the output in the UNROTATED domain) 340 std::vector < float > Vdq_un (Vdq .size ()); 341 std::memcpy (Vdq_un .data (),Vdq .data (),Vdq .size ()* 4 ); 342for (int c = 0 ;c < cells * H ;++ c )fwht256_host_reference (Vdq_un .data ()+ (size_t )c * D ); 343 344// ---- batched form with TWO queries and per-query step arrays (the verify.cpp window's exact call) 345 { 346const int nq = 2 ; 347 std::vector < float > q2 ((size_t )nq * QH * D ); 348for (auto & x :q2 )x = nd (rng ); 349float * d_q2 = dalloc < float > (q2 .size ()); 350/* 351DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 352assuming in the original code the source host memory is pageable memory. 353If the memory is not pageable, call wait() on event return by memcpy API 354to ensure synchronization behavior. 355*/ 356ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_q2 ,q2 .data (), 357q2 .size ()* 4 ).wait ()), 358"q2" ); 359// the batched kernel indexes ids[z * cap + ...]: one FULL selection per query 360 std::vector < int32_t > ids2 ((size_t )nq * cells ); 361for (int i = 0 ;i < nq ;++ i )for (int c = 0 ;c < cells ;++ c )ids2 [(size_t )i * cells + c ]= c ; 362int32_t * d_ids2 = dalloc < int32_t > ((size_t )nq * cells ); 363/* 364DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 365assuming in the original code the source host memory is pageable memory. 366If the memory is not pageable, call wait() on event return by memcpy API 367to ensure synchronization behavior. 368*/ 369ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 370d_ids2 ,ids2 .data (),ids2 .size ()* 4 ).wait ()), 371"ids2" ); 372int32_t * d_steps2 = dalloc < int32_t > (nq * k::kStepCount ); 373for (int i = 0 ;i < nq ;++ i ) { 374int32_t st2 [k::kStepCount ]; 375 k::qsa_step_fill (st2 ,cells - 1 - i ,s ); 376st2 [k::kStepWidth ]= cells - i ;// slightly different widths, as verify's windows have 377/* 378DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 379assuming in the original code the source host memory is pageable 380memory. If the memory is not pageable, call wait() on event return 381by memcpy API to ensure synchronization behavior. 382*/ 383ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 384d_steps2 + i * k::kStepCount ,st2 ,sizeof st2 ).wait ()), 385"steps2" ); 386 } 387float * d_scr2 = dalloc < float > (k::qsa_decode_attn_scratch_floats (cells ,s )* nq + 64 ); 388float * d_at2 = dalloc < float > ((size_t )nq * QH * D ); 389 k::qsa_decode_attn_batch (d_q2 ,pools ,d_ids2 ,d_steps2 ,cells ,s ,d_scr2 ,d_at2 ,nq ,nullptr ); 390 k::fwht256_inplace_cuda (d_at2 , (int64_t )nq * QH ,nullptr ); 391ck (DPCT_CHECK_ERROR (dpct::get_current_device ().queues_wait_and_throw ()), 392"attn2 sync" ); 393 std::vector < float > h_at2 ((size_t )nq * QH * D ); 394ck (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 395 .memcpy (h_at2 .data (),d_at2 ,h_at2 .size ()* 4 ) 396 .wait ()), 397"at2" ); 398double max_b = 0.0 ; 399double per_q [2 ]= {0.0 ,0.0 }; 400for (int i = 0 ;i < nq ;++ i ) 401for (int h = 0 ;h < QH ;++ h ) { 402// reference over the cells this query's width selects (ids are ascending: the first width) 403const int w = cells - i ; 404 std::vector < float > sc (w ); 405float m = -1e30f ; 406for (int c = 0 ;c < w ;++ c ) { 407float d_ = 0.0f ; 408for (int t = 0 ;t < D ;++ t ) 409d_ += q2 [((size_t )i * QH + h )* D + t ]* Kdq [((size_t )c * H + h /PER )* D + t ]; 410sc [c ]= d_ / std::sqrt ((float )D ); 411m = std::max (m ,sc [c ]); 412 } 413float l = 0.0f ; 414for (int c = 0 ;c < w ;++ c ) {sc [c ]= std::exp (sc [c ]- m );l += sc [c ]; } 415for (int d = 0 ;d < D ;++ d ) { 416float acc = 0.0f ; 417for (int c = 0 ;c < w ;++ c ) 418acc += sc [c ] /l * Vdq_un [((size_t )c * H + h /PER )* D + d ];// dim d only 419const double e = (double ) std::fabs (h_at2 [((size_t )i * QH + h )* D + d ]- acc ); 420max_b = std::max (max_b ,e ); 421per_q [i ]= std::max (per_q [i ],e ); 422 } 423 } 424const bool bok = max_b < 5e-3 ; 425 std::printf (" (per-query max: q0 %.2e, q1 %.2e)\n" ,per_q [0 ],per_q [1 ]); 426 std::printf ("[4/4] mode-3 BATCHED attention (verify's form): %s (vs dequant ref %.2e)\n" , 427bok ?"PASS" :"FAIL" ,max_b ); 428if (!bok )g_fail = 1 ; 429 } 430 431 std::vector < float > ref_deq ,ref_true ; 432host_attn (Kdq ,Vdq_un ,ref_deq ); 433host_attn (K ,V ,ref_true ); 434double max_deq = 0.0 ,max_true = 0.0 ; 435for (size_t i = 0 ;i < h_attn .size ();++ i ) { 436max_deq = std::max (max_deq , (double ) std::fabs (h_attn [i ]- ref_deq [i ])); 437max_true = std::max (max_true , (double ) std::fabs (h_attn [i ]- ref_true [i ])); 438 } 439// The tensor-core prompt path (qsa_prompt_attn, 0.1.22+) takes hybrid pools as KV_MODE 3: INT8 K, and V 440// dequantized from its q4_0 blocks to fp16 at gather. Its summation order differs from the old kernel's 441// (accuracy-level, not bitwise - qsa_prompt_attn.hpp says the same of its int8 mode), so: tolerance check 442// against the SAME dequant reference, output un-rotated exactly as prefill.cpp does. 443 { 444float * d_at4 = dalloc < float > ((size_t )QH * D ); 445const bool took = k::qsa_prompt_attn_batch (d_q ,pools ,d_ids ,d_step ,cells ,s ,d_at4 ,1 ,nullptr ); 446if (!took ) { 447#if defined(STRATA_USE_HIP )|| defined(STRATA_HIP_GFX906 ) 448// AMD: the tensor-core prompt path is CUDA-only, so it refuses every pool and the old kernel runs 449 std::printf ("[5/5] qsa_prompt_attn mode 3: PASS (refused on HIP - the old kernel runs)\n" ); 450#else 451 std::printf ("[5/5] qsa_prompt_attn mode 3: FAIL (refused the hybrid pools)\n" ); 452g_fail = 1 ; 453#endif 454 }else { 455 k::fwht256_inplace_cuda (d_at4 ,QH ,nullptr ); 456ck (DPCT_CHECK_ERROR ( 457 dpct::get_current_device ().queues_wait_and_throw ()), 458"at4 sync" ); 459 std::vector < float > h_at4 ((size_t )QH * D ); 460ck (DPCT_CHECK_ERROR ( 461 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 462 .memcpy (h_at4 .data (),d_at4 ,h_at4 .size ()* 4 ) 463 .wait ()), 464"at4" ); 465double max_p = 0.0 ; 466for (size_t i = 0 ;i < h_at4 .size ();++ i ) 467max_p = std::max (max_p , (double ) std::fabs (h_at4 [i ]- ref_deq [i ])); 468const bool pok = max_p < 1e-2 ; 469 std::printf ("[5/5] qsa_prompt_attn mode 3 (tensor cores): %s (vs dequant ref %.2e)\n" , 470pok ?"PASS" :"FAIL" ,max_p ); 471if (!pok )g_fail = 1 ; 472 } 473 } 474 475const bool attn_ok = max_deq < 5e-3 ;// fp32 kernel math vs fp64-ish host accumulation 476 std::printf ("[3/3] mode-3 attention + output fwht: %s (vs dequant ref %.2e, vs true fp32 %.2e)\n" , 477attn_ok ?"PASS" :"FAIL" ,max_deq ,max_true ); 478 479const bool ok = append_bad == 0 && gather_bad == 0 && attn_ok && g_fail == 0 ; 480 std::printf ("=== kv_hybrid_parity: %s ===\n" ,ok ?"ALL PASS" :"FAILED" ); 481return ok ?0 :1 ; 482}