okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ad33b8074
main
1// src/kernels/s_gemv_q8k_parity.cpp - the Q8_K activation path of `s_gemv`. 2// 3// THE POINT OF THIS KERNEL IS A CONTRACT, so the test is built around the contract rather than around the 4// arithmetic: 5// 6// 1. the kernel against a host reference that dequantizes the SAME Q8_K bytes and the SAME S-form planes, 7// in double - so what is under test is the indexing, not the format; 8// 2. the Q8_K activation is produced by the ACTUAL `quantize_q8_K`, which is byte-exact against ggml 9// (round 198). Building the activation by hand here would make this a test of two transcriptions; 10// 3. **THE Q8_K-vs-FP16 GAP IS MEASURED**, because that is the number the whole activation-contract 11// decision rests on and it has only ever been measured on Q2_0 weights (0.6175%) and on BF16 ones 12// (0.141-0.156%). A K-quant weight is a third case and it is the one the dense projections use. 13#define DPCT_PROFILING_ENABLED 14#include <sycl/sycl.hpp> 15#include <dpct/dpct.hpp> 16#include "strata/kernels/f16_bits.hpp" 17#include "strata/kernels/quantize_act.hpp" 18#include "strata/kernels/s_gemv.hpp" 19 20#include <cmath> 21#include <cstdio> 22#include <cstdlib> 23#include <cstring> 24#include <random> 25#include <vector> 26 27namespace { 28 29using strata::kernels::SForm ; 30using strata::kernels::Codebook ; 31 32void check (dpct::err0 e ,const char * what ) { 33} 34 35/// The host decode, transcribed from `docs/pack-format.md`'s rule: `value = cb[code] * scale + offset`, with 36/// the integer bias applied INSIDE the integer domain before the one multiply. 37const signed char kIq4Nl [16 ]= {-127 ,-104 ,-83 ,-65 ,-49 ,-35 ,-22 ,-10 ,1 ,13 ,25 ,38 ,53 ,69 ,89 ,113 }; 38 39double weight_at (const std::vector < uint8_t >& codes ,const std::vector < float >& scales , 40const std::vector < float >& offset ,const SForm & f ,long long n_in ,long long o ,long long i ) { 41const int per_byte = 8 /f .code_bits ; 42const long long codes_per_row = n_in /per_byte ; 43const long long n_groups = n_in /f .group_elems ; 44const uint8_t * c = codes .data ()+ o * codes_per_row ; 45const int code = (c [i /per_byte ] >> ((int ) (i %per_byte )* f .code_bits ))& ((1 <<f .code_bits )- 1 ); 46const double v = (f .codebook == Codebook ::Iq4Nl ) ? (double )kIq4Nl [code & 0x0F ] 47 : (double ) (code + f .code_bias ); 48const long long g = i /f .group_elems ; 49double w = v * (double )scales [(size_t ) (o * n_groups + g )]; 50if (f .has_offset )w += (double )offset [(size_t ) (o * n_groups + g )]; 51return w ; 52} 53 54/// Dequantize a block_q8_K buffer on the host: 292 bytes per 256 elements. 55double q8k_at (const std::vector < uint8_t >& x ,long long i ) { 56const uint8_t * blk = x .data ()+ (size_t ) (i /256 )* 292 ; 57float d ; 58 std::memcpy (& d ,blk ,4 ); 59const int8_t q = ((const int8_t * ) (blk + 4 ))[i %256 ]; 60return (double )d * (double )q ; 61} 62 63struct Case { 64const char * what ; 65int bits ; 66int bias ; 67int group ; 68Codebook cb ; 69bool has_offset ; 70}; 71 72/// THE RAGGED LAST BLOCK. The split kernels give each warp one row and a block eight warps, so with 73/// `n_out % 8 != 0` the last block holds warps that have no row. Those warps used to return BEFORE the 74/// codebook barrier - undefined behaviour that every real shape (a multiple of 8) avoided. Here `n_out` is 75/// deliberately ragged, every row is checked against the host reference over the same bytes, and a guard 76/// band past `n_out` must come back untouched: a warp without a row must neither hang the block nor write. 77/// (`compute-sanitizer --tool synccheck` on this binary is the direct check of the barrier itself.) 78int ragged_rows (const Case & cs ,long long n_out ) { 79const long long n_in = 512 ; 80const long long guard = 64 ; 81 std::mt19937 rng (77 + (unsigned )n_out ); 82 std::normal_distribution < float > g (0.0f ,1.0f ); 83 std::vector < float > xf ((size_t )n_in ); 84for (auto & v :xf )v = g (rng ); 85SForm f ; 86f .code_bits = cs .bits ; 87f .code_bias = cs .bias ; 88f .group_elems = cs .group ; 89f .codebook = cs .cb ; 90f .has_offset = cs .has_offset ; 91const int per_byte = 8 /cs .bits ; 92 std::vector < uint8_t > codes ((size_t ) (n_out * n_in /per_byte )); 93for (auto & v :codes )v = (uint8_t ) (rng ()& 0xFF ); 94const long long n_groups = n_in /cs .group ; 95 std::vector < float > scales ((size_t ) (n_out * n_groups )),offs ((size_t ) (n_out * n_groups )); 96for (auto & v :scales )v = 0.01f + 0.001f * (float ) (rng () %20 ); 97for (auto & v :offs )v = 0.2f * g (rng ); 98 99float * d_xf = nullptr ; 100uint8_t * d_xk = nullptr ,* d_x0 = nullptr ,* d_codes = nullptr ; 101float * d_scales = nullptr ,* d_offs = nullptr ,* d_y = nullptr ; 102 std::vector < uint8_t > xk ((size_t ) (n_in /256 )* 292 ),x0 ((size_t ) (n_in /32 )* 34 ); 103check (DPCT_CHECK_ERROR (d_xf = (float * )sycl::malloc_device ( 104 (size_t )n_in * 4 , dpct::get_in_order_queue ())), 105"rxf" ); 106check (DPCT_CHECK_ERROR (d_xk = (uint8_t * )sycl::malloc_device ( 107xk .size (), dpct::get_in_order_queue ())), 108"rxk" ); 109check (DPCT_CHECK_ERROR (d_x0 = (uint8_t * )sycl::malloc_device ( 110x0 .size (), dpct::get_in_order_queue ())), 111"rx0" ); 112check (DPCT_CHECK_ERROR (d_codes = (uint8_t * )sycl::malloc_device ( 113codes .size (), dpct::get_in_order_queue ())), 114"rcodes" ); 115check (DPCT_CHECK_ERROR (d_scales = (float * )sycl::malloc_device ( 116scales .size ()* 4 , dpct::get_in_order_queue ())), 117"rscales" ); 118check (DPCT_CHECK_ERROR (d_offs = (float * )sycl::malloc_device ( 119offs .size ()* 4 , dpct::get_in_order_queue ())), 120"roffs" ); 121check (DPCT_CHECK_ERROR ( 122d_y = (float * )sycl::malloc_device ((size_t )(n_out + guard )* 4 , 123 dpct::get_in_order_queue ())), 124"ry" ); 125/* 126DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in 127the original code the source host memory is pageable memory. If the memory 128is not pageable, call wait() on event return by memcpy API to ensure 129synchronization behavior. 130*/ 131check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy (d_xf ,xf .data (), 132 (size_t )n_in * 4 ).wait ()), 133"rcxf" ); 134/* 135DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in 136the original code the source host memory is pageable memory. If the memory 137is not pageable, call wait() on event return by memcpy API to ensure 138synchronization behavior. 139*/ 140check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 141d_codes ,codes .data (),codes .size ()).wait ()), 142"rcc" ); 143/* 144DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in 145the original code the source host memory is pageable memory. If the memory 146is not pageable, call wait() on event return by memcpy API to ensure 147synchronization behavior. 148*/ 149check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 150d_scales ,scales .data (),scales .size ()* 4 ).wait ()), 151"rcs" ); 152/* 153DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in 154the original code the source host memory is pageable memory. If the memory 155is not pageable, call wait() on event return by memcpy API to ensure 156synchronization behavior. 157*/ 158check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 159d_offs ,offs .data (),offs .size ()* 4 ).wait ()), 160"rco" ); 161 strata::kernels::quantize_q8_K (d_xf ,d_xk ,n_in ,nullptr ); 162 strata::kernels::quantize_q8_0 (d_xf ,d_x0 ,n_in ,nullptr ); 163check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 164 .memcpy (xk .data (),d_xk ,xk .size ()) 165 .wait ()), 166"rcxk" ); 167check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 168 .memcpy (x0 .data (),d_x0 ,x0 .size ()) 169 .wait ()), 170"rcx0" ); 171 172int bad = 0 ; 173for (int kind = 0 ;kind < 2 ;++ kind ) {// 0: Q8_K activation, 1: Q8_0 activation 174 std::vector < float > want ((size_t )n_out ); 175 std::vector < double > mag ((size_t )n_out );// sum |w_i a_i| per row: a row whose sum cancels near 0 176for (long long o = 0 ;o < n_out ;++ o ) {// must not turn f32 rounding into a relative failure 177double acc = 0 ,am = 0 ; 178for (long long i = 0 ;i < n_in ;++ i ) { 179double a ; 180if (kind == 0 ) { 181a = q8k_at (xk ,i ); 182 }else { 183const uint8_t * blk = x0 .data ()+ (size_t ) (i /32 )* 34 ; 184uint16_t dbits ; 185 std::memcpy (& dbits ,blk ,2 ); 186a = (double ) strata::kernels::f32_from_f16 (dbits )* (double ) ((const int8_t * ) (blk + 2 ))[i %32 ]; 187 } 188const double wa = weight_at (codes ,scales ,offs ,f ,n_in ,o ,i )* a ; 189acc += wa ; 190am += std::fabs (wa ); 191 } 192want [(size_t )o ]= (float )acc ; 193mag [(size_t )o ]= am ; 194 } 195// the guard band holds a NaN pattern; any write there shows up as a changed bit 196 std::vector < uint32_t > sentinel ((size_t ) (n_out + guard ),0x7FC0DEADu ); 197/* 198DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 199assuming in the original code the source host memory is pageable memory. 200If the memory is not pageable, call wait() on event return by memcpy API 201to ensure synchronization behavior. 202*/ 203check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 204d_y ,sentinel .data (),sentinel .size ()* 4 ).wait ()), 205"rsent" ); 206if (kind == 0 ) 207 strata::kernels::s_gemv_q8k_split (d_xk ,d_codes ,d_scales ,cs .has_offset ?d_offs :nullptr ,d_y ,n_in , 208n_out ,f ,nullptr ); 209else 210 strata::kernels::s_gemv_q8_0_split (d_x0 ,d_codes ,d_scales ,cs .has_offset ?d_offs :nullptr ,d_y ,n_in , 211n_out ,f ,nullptr ); 212check (DPCT_CHECK_ERROR ( 213 dpct::get_current_device ().queues_wait_and_throw ()), 214"ragged split" ); 215 std::vector < uint32_t > got_bits (sentinel .size ()); 216check (DPCT_CHECK_ERROR ( 217 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 218 .memcpy (got_bits .data (),d_y ,got_bits .size ()* 4 ) 219 .wait ()), 220"rcy" ); 221double m = 0 ,d = 0 ; 222for (long long o = 0 ;o < n_out ;++ o ) { 223float v ; 224 std::memcpy (& v ,& got_bits [(size_t )o ],4 ); 225m += mag [(size_t )o ]; 226d += std::fabs ((double )want [(size_t )o ]- (double )v ); 227 } 228long long touched = 0 ; 229for (long long o = n_out ;o < n_out + guard ;++ o )touched += got_bits [(size_t )o ]!= 0x7FC0DEADu ; 230const double rel = d / (m > 1e-30 ?m :1e-30 ); 231const bool ok = rel <=1e-5 && touched == 0 ; 232 std::printf (" %-28s %s n_out %3lld: rel %.3e, guard words written %lld %s\n" ,cs .what , 233kind == 0 ?"Q8_K" :"Q8_0" ,n_out ,rel ,touched ,ok ?"ok" :"*** WRONG ***" ); 234if (!ok )++ bad ; 235 } 236 sycl::free (d_xf , dpct::get_in_order_queue ()); 237 sycl::free (d_xk , dpct::get_in_order_queue ()); 238 sycl::free (d_x0 , dpct::get_in_order_queue ()); 239 sycl::free (d_codes , dpct::get_in_order_queue ()); 240 sycl::free (d_scales , dpct::get_in_order_queue ()); 241 sycl::free (d_offs , dpct::get_in_order_queue ()); 242 sycl::free (d_y , dpct::get_in_order_queue ()); 243return bad ; 244} 245 246}// namespace 247 248int main (int argc ,char ** argv ) { 249bool selftest = false; 250for (int i = 1 ;i < argc ;++ i ) { 251if (std::string (argv [i ])== "--selftest" )selftest = true; 252else { std::fprintf (stderr ,"usage: s_gemv_q8k_parity [--selftest]\n" );return 2 ; } 253 } 254int bad = 0 ; 255 256// The forms the DENSE weights actually use, taken from the manifest rather than invented: 257// attn_qkv.weight IQ4_XS S4 group 32, no offset (the GDN mixer's qkv) 258// attn_gate.weight Q4_K S4 group 32, offset (the GDN mixer's z) 259// ssm_out.weight Q5_K S8 group 32, offset (the GDN mixer's output) 260const Case cases []= { 261 {"IQ4_XS S4 g32 no-offset" ,4 ,-8 ,32 ,Codebook ::Affine , false}, 262 {"Q4_K S4 g32 offset" ,4 ,0 ,32 ,Codebook ::Affine , true}, 263 {"Q5_K S8 g32 offset" ,8 ,0 ,32 ,Codebook ::Affine , true}, 264 }; 265 266for (const Case & cs :cases ) { 267const long long n_in = 512 ,n_out = 64 ;// 2 Q8_K blocks wide, small enough for a host reference 268 std::mt19937 rng (2024 ); 269 std::normal_distribution < float > g (0.0f ,1.0f ); 270 std::vector < float > xf ((size_t )n_in ); 271for (auto & v :xf )v = g (rng ); 272 273SForm f ; 274f .code_bits = cs .bits ; 275f .code_bias = cs .bias ; 276f .group_elems = cs .group ; 277f .codebook = cs .cb ; 278f .has_offset = cs .has_offset ; 279 280const int per_byte = 8 /cs .bits ; 281 std::vector < uint8_t > codes ((size_t ) (n_out * n_in /per_byte )); 282for (auto & v :codes )v = (uint8_t ) (rng ()& 0xFF ); 283const long long n_groups = n_in /cs .group ; 284 std::vector < float > scales ((size_t ) (n_out * n_groups )),offs ((size_t ) (n_out * n_groups )); 285for (auto & v :scales )v = 0.01f + 0.001f * (float ) (rng () %20 ); 286for (auto & v :offs )v = 0.2f * g (rng ); 287 288// ---- the activation, from the REAL quantizer 289 std::vector < uint8_t > xq8k ((size_t ) (n_in /256 )* 292 ); 290float * d_xf = nullptr ; 291uint8_t * d_xq = nullptr ; 292check ( 293DPCT_CHECK_ERROR (d_xf = (float * )sycl::malloc_device ( 294 (size_t )n_in * 4 , dpct::get_in_order_queue ())), 295"xf" ); 296check (DPCT_CHECK_ERROR (d_xq = (uint8_t * )sycl::malloc_device ( 297xq8k .size (), dpct::get_in_order_queue ())), 298"xq" ); 299/* 300DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 301assuming in the original code the source host memory is pageable memory. 302If the memory is not pageable, call wait() on event return by memcpy API 303to ensure synchronization behavior. 304*/ 305check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 306d_xf ,xf .data (), (size_t )n_in * 4 ).wait ()), 307"cxf" ); 308 strata::kernels::quantize_q8_K (d_xf ,d_xq ,n_in ,nullptr ); 309check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 310 .memcpy (xq8k .data (),d_xq ,xq8k .size ()) 311 .wait ()), 312"cxq" ); 313 314// ---- host reference in double, over the same bytes 315 std::vector < float > want ((size_t )n_out ,0.0f ); 316for (long long o = 0 ;o < n_out ;++ o ) { 317double acc = 0 ; 318for (long long i = 0 ;i < n_in ;++ i ) 319acc += weight_at (codes ,scales ,offs ,f ,n_in ,o ,i )* q8k_at (xq8k ,i ); 320want [(size_t )o ]= (float )acc ; 321 } 322 323// ---- device 324uint8_t * d_codes = nullptr ; 325float * d_scales = nullptr ,* d_offs = nullptr ,* d_y = nullptr ; 326check (DPCT_CHECK_ERROR (d_codes = (uint8_t * )sycl::malloc_device ( 327codes .size (), dpct::get_in_order_queue ())), 328"codes" ); 329check (DPCT_CHECK_ERROR ( 330d_scales = (float * )sycl::malloc_device ( 331scales .size ()* 4 , dpct::get_in_order_queue ())), 332"scales" ); 333check ( 334DPCT_CHECK_ERROR (d_offs = (float * )sycl::malloc_device ( 335offs .size ()* 4 , dpct::get_in_order_queue ())), 336"offs" ); 337check (DPCT_CHECK_ERROR ( 338d_y = (float * )sycl::malloc_device ( 339 (size_t )n_out * 4 , dpct::get_in_order_queue ())), 340"y" ); 341/* 342DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 343assuming in the original code the source host memory is pageable memory. 344If the memory is not pageable, call wait() on event return by memcpy API 345to ensure synchronization behavior. 346*/ 347check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 348d_codes ,codes .data (),codes .size ()).wait ()), 349"cc" ); 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*/ 356check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 357d_scales ,scales .data (),scales .size ()* 4 ).wait ()), 358"cs" ); 359/* 360DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 361assuming in the original code the source host memory is pageable memory. 362If the memory is not pageable, call wait() on event return by memcpy API 363to ensure synchronization behavior. 364*/ 365check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 366d_offs ,offs .data (),offs .size ()* 4 ).wait ()), 367"co" ); 368 369 std::vector < float > naive ((size_t )n_out ),warp ((size_t )n_out ); 370 strata::kernels::s_gemv_q8k (d_xq ,d_codes ,d_scales ,cs .has_offset ?d_offs :nullptr ,d_y ,n_in ,n_out , 371f ,nullptr ); 372check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 373 .memcpy (naive .data (),d_y , (size_t )n_out * 4 ) 374 .wait ()), 375"cy1" ); 376 strata::kernels::s_gemv_q8k_split (d_xq ,d_codes ,d_scales ,cs .has_offset ?d_offs :nullptr ,d_y ,n_in , 377n_out ,f ,nullptr ); 378check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 379 .memcpy (warp .data (),d_y , (size_t )n_out * 4 ) 380 .wait ()), 381"cy2" ); 382 383double mag = 0 ,d1 = 0 ,d2 = 0 ; 384for (long long o = 0 ;o < n_out ;++ o ) { 385mag += std::fabs ((double )want [(size_t )o ]); 386d1 += std::fabs ((double )want [(size_t )o ]- (double )naive [(size_t )o ]); 387d2 += std::fabs ((double )want [(size_t )o ]- (double )warp [(size_t )o ]); 388 } 389 std::printf (" %-28s naive %.3e warp %.3e\n" ,cs .what ,d1 /mag ,d2 /mag ); 390// The kernel accumulates in f32, the reference in f64, over 512 terms with values of similar size. 391// 1e-5 is the arithmetic's floor, not a structural allowance. 392if (!(d1 /mag <=1e-5 && d2 /mag <=1e-5 )) { std::printf (" *** over 1e-5 ***\n" );++ bad ; } 393 394// ---- 2b. THE Q8_0 ACTIVATION, on the SAME weight planes. `s_gemv_q8_0_split` exists because the 395// legacy block formats want Q8_0 and `ffn_down_shexp` cannot use Q8_K at all (n_in = 640 is not a 396// multiple of 256). It shares every line with the Q8_K kernel except the activation loader, so what 397// this checks is the LOADER - the 34-byte block, the fp16 `d`, and the element index inside it. 398 { 399 std::vector < uint8_t > xq0 ((size_t ) (n_in /32 )* 34 ); 400uint8_t * d_x0 = nullptr ; 401check (DPCT_CHECK_ERROR (d_x0 = (uint8_t * )sycl::malloc_device ( 402xq0 .size (), dpct::get_in_order_queue ())), 403"x0" ); 404 strata::kernels::quantize_q8_0 (d_xf ,d_x0 ,n_in ,nullptr ); 405check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 406 .memcpy (xq0 .data (),d_x0 ,xq0 .size ()) 407 .wait ()), 408"cx0" ); 409 410// the host reference over the SAME Q8_0 bytes, with the same weight decode 411 std::vector < float > want0 ((size_t )n_out ,0.0f ); 412for (long long o = 0 ;o < n_out ;++ o ) { 413double acc = 0 ; 414for (long long i = 0 ;i < n_in ;++ i ) { 415const uint8_t * blk = xq0 .data ()+ (size_t ) (i /32 )* 34 ; 416uint16_t dbits ; 417 std::memcpy (& dbits ,blk ,2 ); 418const double d = (double ) strata::kernels::f32_from_f16 (dbits ); 419const int8_t q = ((const int8_t * ) (blk + 2 ))[i %32 ]; 420acc += weight_at (codes ,scales ,offs ,f ,n_in ,o ,i )* d * (double )q ; 421 } 422want0 [(size_t )o ]= (float )acc ; 423 } 424 strata::kernels::s_gemv_q8_0_split (d_x0 ,d_codes ,d_scales ,cs .has_offset ?d_offs :nullptr ,d_y , 425n_in ,n_out ,f ,nullptr ); 426 std::vector < float > got0 ((size_t )n_out ); 427check (DPCT_CHECK_ERROR ( 428 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 429 .memcpy (got0 .data (),d_y , (size_t )n_out * 4 ) 430 .wait ()), 431"cy0" ); 432double m0 = 0 ,dd0 = 0 ; 433for (long long o = 0 ;o < n_out ;++ o ) { 434m0 += std::fabs ((double )want0 [(size_t )o ]); 435dd0 += std::fabs ((double )want0 [(size_t )o ]- (double )got0 [(size_t )o ]); 436 } 437 std::printf (" %-28s Q8_0 act %.3e\n" ,cs .what ,dd0 / (m0 > 1e-30 ?m0 :1e-30 )); 438if (!(dd0 / (m0 > 1e-30 ?m0 :1e-30 ) <=1e-5 )) { 439 std::printf (" *** the Q8_0 activation loader is WRONG ***\n" ); 440++ bad ; 441 } 442check ( 443DPCT_CHECK_ERROR (sycl::free (d_x0 , dpct::get_in_order_queue ())), 444"fx0" ); 445 } 446 447// ---- 3. THE Q8_K vs FP16 GAP, on a K-quant weight - the case the contract decision needs 448 { 449 std::vector < uint16_t > x16 ((size_t )n_in ); 450for (long long i = 0 ;i < n_in ;++ i )x16 [(size_t )i ]= strata::kernels::f16_from_f32 (xf [(size_t )i ]); 451uint16_t * d_x16 = nullptr ; 452check (DPCT_CHECK_ERROR ( 453d_x16 = (uint16_t * )sycl::malloc_device ( 454 (size_t )n_in * 2 , dpct::get_in_order_queue ())), 455"x16" ); 456/* 457DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, 458assuming in the original code the source host memory is pageable 459memory. If the memory is not pageable, call wait() on event return 460by memcpy API to ensure synchronization behavior. 461*/ 462check (DPCT_CHECK_ERROR ((dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()).memcpy ( 463d_x16 ,x16 .data (), (size_t )n_in * 2 ).wait ()), 464"cx16" ); 465 std::vector < float > fp16out ((size_t )n_out ); 466 strata::kernels::s_gemv (d_x16 ,d_codes ,d_scales ,cs .has_offset ?d_offs :nullptr ,d_y ,n_in ,n_out , 467f ); 468check (DPCT_CHECK_ERROR ( 469 (dpct::get_current_device ().queues_wait_and_throw (), dpct::get_in_order_queue ()) 470 .memcpy (fp16out .data (),d_y , (size_t )n_out * 4 ) 471 .wait ()), 472"cy3" ); 473double dd = 0 ; 474for (long long o = 0 ;o < n_out ;++ o ) 475dd += std::fabs ((double )naive [(size_t )o ]- (double )fp16out [(size_t )o ]); 476const double rel = dd /mag ; 477const bool visible = rel > 1e-3 ; 478 std::printf (" %-24s %s (%.4f%% apart, tolerance 0.1%%)\n" ,"Q8_K vs fp16 activation" , 479visible ?"yes" :"*** NO ***" ,rel * 100 ); 480if (!visible )++ bad ; 481 sycl::free (d_x16 , dpct::get_in_order_queue ()); 482 } 483 sycl::free (d_xf , dpct::get_in_order_queue ()); 484 sycl::free (d_xq , dpct::get_in_order_queue ()); 485 sycl::free (d_codes , dpct::get_in_order_queue ()); 486 sycl::free (d_scales , dpct::get_in_order_queue ()); 487 sycl::free (d_offs , dpct::get_in_order_queue ()); 488 sycl::free (d_y , dpct::get_in_order_queue ()); 489 } 490 491// ---- 4. output widths that are NOT a multiple of the eight rows per block 492 std::printf ("\n" ); 493for (const Case & cs :cases ) 494for (long long n_out : {1LL ,7LL ,9LL ,61LL })bad += ragged_rows (cs ,n_out ); 495 496 std::printf ("\ns_gemv_q8k: %d failures\n" ,bad ); 497if (bad )return 1 ; 498if (selftest ) std::printf ("s_gemv_q8k_parity OK\n" ); 499return 0 ; 500}