code wiki / _hdl_build / nx_vec_kernel.nx
nx_vec_kernel.nx
buildroot/runtime/_hdl_build/nx_vec_kernel.nx
about
nx_vec_kernel.nx -- R-VEC-0 of the onsite-search S-class ladder: the SOVEREIGN vector-math kernel (LIBRARY),
the lowest (silicon) rung every semantic-search capability stands on. Pure INTEGER fixed-point -- deterministic,
byte-reproducible, no-float (operator doctrine) -- so a cosine is a known-answer test, not a flaky measurement.
KAT lives in nx_vec_kernel_gate.nx (imports this), matching the nx_bm25 / nx_bm25f library+gate split.
cosine(a,b) = dot(a,b) / (||a|| * ||b||), bounded in [-1,1] (cited srch_cosine.raw); at scale = a matrix-matrix
multiply = BLAS Level 3 / GEMM (cited srch_blas.raw), the kernel the CUDA-exceed roadmap climbs on the RTX 5080.
PRECISION TRICK: integer sqrt of small norms collapses (isqrt(2)=1) and would make every short vector look
identical. So compute cos SQUARED in a scaled domain and isqrt THAT:
cos_milli = sign(dot) * isqrt( dot^2 * 1e6 / (||a||^2 * ||b||^2) ) -> cosine scaled to [-1000,1000]
By Cauchy-Schwarz dot^2 <= ||a||^2*||b||^2, so the ratio is in [0,1e6] and isqrt lands in [0,1000] exactly.
OVERFLOW CONTRACT (honest boundary): dot^2 * 1e6 must fit i64 (< 9.2e18) => |dot| < ~3e6. Satisfied by
L2-normalized int8/int16 embeddings (R-VEC-1 feeds these; R-VEC-4 quantizes to keep norms small).
exports: vr_dot, vr_isqrt, vr_cos_milli license_tier: ORIGINAL
dependencies 1 imports · 5 importers
imports: nx_syscalls.nx
imported by: nx_vec_embed_gate.nxnx_vec_hybrid_gate.nxnx_vec_kernel_gate.nxnx_vec_nsw.nxnx_vec_nsw_gate.nx
structs
| none |
consts
| 19 | const K_MAGIC_1000000: i64 = 1000000 |
functions
| 22 | func vr_dot(a: *i64, b: *i64, n: i64) -> i64 called by 1: vr_cos_milli |
| 29 | func vr_isqrt(x: i64) -> i64 |
| 44 | func vr_cos_milli(a: *i64, b: *i64, n: i64) -> i64 |