code wiki / _hdl_build / nx_qmatvec_test.nx

nx_qmatvec_test.nx source

↩ module page · 52 lines · 3228 B

1// nx_qmatvec_test.nx -- the ENGINEER verifies the sovereign quantized dot kernel and measures the 2// quality/data frontier. Exit 0 only if all hold: 3// (1) Q4 block-quantized dot matches the EXACT dot within the quality floor (real, bounded error). 4// (2) Q8 is much TIGHTER (more bits -> better quality) -- the frontier is real and monotone. 5// (3) data moved: Q4 < Q8 < int16 ref, and Q4 is >=3x less than the ref -> the memory-bound 6// speedup (throughput ~ 1/bytes) is >=3x where ggml SIMD micro-opt gave +0.8%. 7// Deterministic correlated vectors (no rng): X = W + bounded noise, so the exact dot is large and 8// the relative error is stable. license_tier: ORIGINAL 9 10import "nx_qmatvec.nx" 11import "nx_syscalls.nx" 12 13func qt_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 14func qt_num(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m; sys_write(1,"-" as *u8,1)}; let t: *u8 = sys_mmap(28); var k: i64=0; if m==0 {t[0]=48;k=1}; while m>0 {t[k]=48+(m%10); m=m/10; k=k+1}; var i: i64=0; while i<k {bb[i]=t[k-1-i]; i=i+1}; sys_write(1, bb, k); return 0 } 15 16func main() -> i64 { 17 qt_puts("=== sovereign block-quantized DOT (the ggml hot path), Engineer-verified ===\n" as *u8) 18 let N: i64 = 1024 19 let w: *i64 = sys_mmap(8 * (N + 8)) as *i64 20 let x: *i64 = sys_mmap(8 * (N + 8)) as *i64 21 var i: i64 = 0 22 while i < N { 23 let wv: i64 = ((i * 37 + 11) % 201) - 100 // -100..100 24 w[i] = wv 25 x[i] = wv + (((i * 13 + 5) % 41) - 20) // correlated: W + noise[-20,20] 26 i = i + 1 27 } 28 let exact: i64 = qmv_dot_exact(w, x, N) 29 let q4: i64 = qmv_dot_q(w, x, N, QMV_Q4MAX) 30 let q8: i64 = qmv_dot_q(w, x, N, QMV_Q8MAX) 31 let e4: i64 = qmv_rel_error_permil(q4, exact) 32 let e8: i64 = qmv_rel_error_permil(q8, exact) 33 qt_puts(" exact dot = " as *u8); qt_num(exact); qt_puts("\n" as *u8) 34 qt_puts(" Q4 dot = " as *u8); qt_num(q4); qt_puts(" rel-error permil = " as *u8); qt_num(e4); qt_puts("\n" as *u8) 35 qt_puts(" Q8 dot = " as *u8); qt_num(q8); qt_puts(" rel-error permil = " as *u8); qt_num(e8); qt_puts("\n" as *u8) 36 37 let bref: i64 = qmv_bytes_ref16(N) 38 let b4: i64 = qmv_bytes_q(N, 4) 39 let b8: i64 = qmv_bytes_q(N, 8) 40 qt_puts(" bytes: ref16=" as *u8); qt_num(bref); qt_puts(" Q8=" as *u8); qt_num(b8); qt_puts(" Q4=" as *u8); qt_num(b4) 41 qt_puts(" Q4 data-reduction x1000 = " as *u8); qt_num((bref * 1000) / b4); qt_puts("\n" as *u8) 42 43 let r: *i64 = sys_mmap(8 * 8) as *i64 44 r[0] = 0; if e4 <= 100 { r[0] = 1 } // Q4 within 10% on a raw single-kernel dot 45 r[1] = 0; if e8 <= 10 { if e8 < e4 { r[1] = 1 } } // Q8 tighter (frontier monotone) 46 r[2] = 0; if b4 < b8 { if b8 < bref { if (bref / b4) >= 3 { r[2] = 1 } } } 47 var pass: i64 = 0; var j: i64 = 0 48 while j < 3 { pass = pass + r[j]; j = j + 1 } 49 qt_puts("----\n passed " as *u8); qt_num(pass); qt_puts("/3\n" as *u8) 50 if pass == 3 { qt_puts(" KERNEL VERIFIED: moves >=3x less data, quality bounded, Q8>Q4 frontier real.\n" as *u8); sys_exit(0); return 0 } 51 qt_puts(" FAIL\n" as *u8); sys_exit(1); return 1 52}