code wiki / _hdl_build / nx_vec_kernel.nx

nx_vec_kernel.nx source

↩ module page · 47 lines · 2502 B

1// nx_vec_kernel.nx -- R-VEC-0 of the onsite-search S-class ladder: the SOVEREIGN vector-math kernel (LIBRARY), 2// the lowest (silicon) rung every semantic-search capability stands on. Pure INTEGER fixed-point -- deterministic, 3// byte-reproducible, no-float (operator doctrine) -- so a cosine is a known-answer test, not a flaky measurement. 4// KAT lives in nx_vec_kernel_gate.nx (imports this), matching the nx_bm25 / nx_bm25f library+gate split. 5// 6// cosine(a,b) = dot(a,b) / (||a|| * ||b||), bounded in [-1,1] (cited srch_cosine.raw); at scale = a matrix-matrix 7// multiply = BLAS Level 3 / GEMM (cited srch_blas.raw), the kernel the CUDA-exceed roadmap climbs on the RTX 5080. 8// 9// PRECISION TRICK: integer sqrt of small norms collapses (isqrt(2)=1) and would make every short vector look 10// identical. So compute cos SQUARED in a scaled domain and isqrt THAT: 11// cos_milli = sign(dot) * isqrt( dot^2 * 1e6 / (||a||^2 * ||b||^2) ) -> cosine scaled to [-1000,1000] 12// By Cauchy-Schwarz dot^2 <= ||a||^2*||b||^2, so the ratio is in [0,1e6] and isqrt lands in [0,1000] exactly. 13// 14// OVERFLOW CONTRACT (honest boundary): dot^2 * 1e6 must fit i64 (< 9.2e18) => |dot| < ~3e6. Satisfied by 15// L2-normalized int8/int16 embeddings (R-VEC-1 feeds these; R-VEC-4 quantizes to keep norms small). 16// 17// exports: vr_dot, vr_isqrt, vr_cos_milli license_tier: ORIGINAL 18import "nx_syscalls.nx" 19import "nx_vecmath.nx" 20const K_MAGIC_1000000: i64 = 1000000 21 22// exact integer dot product sum a[i]*b[i] 23func vr_dot(a: *i64, b: *i64, n: i64) -> i64 { 24 var s: i64 = 0; var i: i64 = 0 25 while i < n { s = s + a[i] * b[i]; i = i + 1 } 26 return s 27} 28 29// floor(sqrt(x)) -- classic bit-by-bit integer sqrt, exact, no oscillation, overflow-guarded 30func vr_isqrt(x: i64) -> i64 { return vm_isqrt(x) } 31 32// cosine(a,b) scaled to [-1000,1000] (milli-cosine), integer-exact via the cos^2 trick above 33func vr_cos_milli(a: *i64, b: *i64, n: i64) -> i64 { 34 let dot: i64 = vr_dot(a, b, n) 35 if dot == 0 { return 0 } 36 let na: i64 = vr_dot(a, a, n) 37 let nb: i64 = vr_dot(b, b, n) 38 if na == 0 { return 0 } 39 if nb == 0 { return 0 } 40 let d2: i64 = dot * dot 41 let denom: i64 = na * nb 42 let scaled: i64 = (d2 * K_MAGIC_1000000) / denom // cos^2 * 1e6, in [0,1e6] 43 var mag: i64 = vr_isqrt(scaled) // milli-cos magnitude, in [0,1000] 44 if mag > 1000 { mag = 1000 } // clamp rounding (collinear -> exactly 1000) 45 if dot < 0 { return 0 - mag } 46 return mag 47}