code wiki / _hdl_build / nx_vec_kernel.nx
nx_vec_kernel.nx source
↩ module page · 58 lines · 2815 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"
19const K_MAGIC_1000000: i64 = 1000000
20
21// exact integer dot product sum a[i]*b[i]
22func vr_dot(a: *i64, b: *i64, n: i64) -> i64 {
23 var s: i64 = 0; var i: i64 = 0
24 while i < n { s = s + a[i] * b[i]; i = i + 1 }
25 return s
26}
27
28// floor(sqrt(x)) -- classic bit-by-bit integer sqrt, exact, no oscillation, overflow-guarded
29func vr_isqrt(x: i64) -> i64 {
30 if x <= 0 { return 0 }
31 var bit: i64 = 1
32 while bit <= x / 4 { bit = bit * 4 } // highest power of 4 <= x, without overflowing
33 var num: i64 = x
34 var res: i64 = 0
35 while bit != 0 {
36 if num >= res + bit { num = num - (res + bit); res = (res / 2) + bit }
37 else { res = res / 2 }
38 bit = bit / 4
39 }
40 return res
41}
42
43// cosine(a,b) scaled to [-1000,1000] (milli-cosine), integer-exact via the cos^2 trick above
44func vr_cos_milli(a: *i64, b: *i64, n: i64) -> i64 {
45 let dot: i64 = vr_dot(a, b, n)
46 if dot == 0 { return 0 }
47 let na: i64 = vr_dot(a, a, n)
48 let nb: i64 = vr_dot(b, b, n)
49 if na == 0 { return 0 }
50 if nb == 0 { return 0 }
51 let d2: i64 = dot * dot
52 let denom: i64 = na * nb
53 let scaled: i64 = (d2 * K_MAGIC_1000000) / denom // cos^2 * 1e6, in [0,1e6]
54 var mag: i64 = vr_isqrt(scaled) // milli-cos magnitude, in [0,1000]
55 if mag > 1000 { mag = 1000 } // clamp rounding (collinear -> exactly 1000)
56 if dot < 0 { return 0 - mag }
57 return mag
58}