code wiki / (root) / nx_vec_index.nx

nx_vec_index.nx source

↩ module page · 126 lines · 6073 B

1// nx_vec_index.nx -- R3 rung 1: a SOVEREIGN integer dense-vector store + exact cosine k-NN = the MILVUS/ANN 2// replacement (deep-searcher rides Milvus; depstack census graded VDB=BEHIND). No-float: vectors are integer, 3// normalized to fixed scale S with fixed-point precision P via integer isqrt (Newton), similarity = integer 4// dot of unit vectors -> cosine in PERMILLE (0..1000, sign-preserving). Exact brute-force k-NN (correct; 5// approximate HNSW is a SCALE optimization, deferred). HONEST SCOPE: this is the STORE+SEARCH half. The trained 6// EMBEDDER that turns text->vector (the Jina-Embeddings replacement, EMB axis) is R3b -- distributional/trained, 7// NOT built here. main() = KAT gate with a negative control (an orthogonal doc must NOT outrank a parallel one). 8// license_tier: ORIGINAL expect_exit: 0 9import "nx_syscalls.nx" 10const K_MAGIC_1000000: i64 = 1000000 11const K_MAGIC_8192: i64 = 8192 12 13func vw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 14func vn(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 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;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 16// integer sqrt (Newton). floor(sqrt(n)). 17func isqrt(n: i64) -> i64 { 18 if n<0 { return 0 } 19 if n<2 { return n } 20 var x: i64 = n 21 var y: i64 = (x+1)/2 22 while y<x { x=y; y=(x + n/x)/2 } 23 return x 24} 25 26func vec_dot(a: *i64, ao: i64, b: *i64, bo: i64, d: i64) -> i64 { 27 var s: i64=0; var i: i64=0 28 while i<d { s=s+a[ao+i]*b[bo+i]; i=i+1 } 29 return s 30} 31 32func vec_norm2(a: *i64, ao: i64, d: i64) -> i64 { 33 var s: i64=0; var i: i64=0 34 while i<d { s=s+a[ao+i]*a[ao+i]; i=i+1 } 35 return s 36} 37 38// normalize raw[so..] -> dst[doff..] as a unit vector at scale S (fixed-point precision P). |dst| ~ S. 39func vec_normalize(src: *i64, so: i64, dst: *i64, doff: i64, d: i64, S: i64, P: i64) -> i64 { 40 let n2: i64 = vec_norm2(src, so, d) 41 let L: i64 = isqrt(n2*P*P) 42 var i: i64=0 43 if L==0 { while i<d { dst[doff+i]=0; i=i+1 } return 0 } 44 while i<d { dst[doff+i]=(src[so+i]*S*P)/L; i=i+1 } 45 return 0 46} 47 48// cosine in permille; ua,ub already normalized to scale S. 49func cos_permille(u: *i64, ao: i64, bo: i64, d: i64, S: i64) -> i64 { 50 let dp: i64 = vec_dot(u, ao, u, bo, d) 51 return (dp*1000)/(S*S) 52} 53 54// top-1 doc id by cosine to query qunit[0..] over unit[ndoc x d]. 55func vidx_top1(unit: *i64, ndoc: i64, d: i64, q: *i64, S: i64) -> i64 { 56 var best: i64 = 0-1 57 var bestc: i64 = 0-K_MAGIC_1000000 58 var di: i64 = 0 59 while di<ndoc { 60 let c: i64 = vec_dot(q, 0, unit, di*d, d) 61 if c>bestc { bestc=c; best=di } 62 di=di+1 63 } 64 return best 65} 66 67func chk(label: *u8, got: i64, exp: i64, tot: *i64) -> i64 { 68 vw(" "); vw(label); vw(" = "); vn(got) 69 if got==exp { vw(" PASS"); tot[0]=tot[0]+1 } else { vw(" FAIL(exp "); vn(exp); vw(")") } 70 tot[1]=tot[1]+1; vw("\n"); return 0 71} 72 73func main() -> i64 { 74 let D: i64=4; let S: i64=1000; let P: i64=1000 75 let tot: *i64 = sys_mmap(64) as *i64 76 tot[0]=0; tot[1]=0 77 vw("=== nx_vec_index -- sovereign integer dense-vector store + exact cosine k-NN (Milvus/ANN replacement, no-float) ===\n") 78 79 // 5 docs x 4 dims (flat, row-major). Think of dims as [apple, banana, car, house]. 80 let raw: *i64 = sys_mmap(K_MAGIC_8192) as *i64 81 raw[0]=3; raw[1]=0; raw[2]=0; raw[3]=0 // d0 apple 82 raw[4]=0; raw[5]=4; raw[6]=0; raw[7]=0 // d1 banana 83 raw[8]=3; raw[9]=3; raw[10]=0; raw[11]=0 // d2 apple+banana 84 raw[12]=0; raw[13]=0; raw[14]=5; raw[15]=0 // d3 car 85 raw[16]=6; raw[17]=0; raw[18]=0; raw[19]=0 // d4 apple (stronger) 86 let ndoc: i64=5 87 88 // normalize every doc into the unit store 89 let unit: *i64 = sys_mmap(K_MAGIC_8192) as *i64 90 var di: i64=0 91 while di<ndoc { vec_normalize(raw, di*D, unit, di*D, D, S, P); di=di+1 } 92 93 // query = "apple" 94 let rq: *i64 = sys_mmap(64) as *i64 95 rq[0]=1; rq[1]=0; rq[2]=0; rq[3]=0 96 let q: *i64 = sys_mmap(64) as *i64 97 vec_normalize(rq, 0, q, 0, D, S, P) 98 99 vw("query='apple' -> cosine(permille) to each doc:\n") 100 chk("cos(q,d0 apple) " as *u8, cos_permille(q, 0, 0, D, S), 1000, tot) // parallel 101 // compare q against stored units: reuse cos_permille by putting q at unit? simpler: dot(q, unit_i) 102 chk("cos(q,d0)=dot " as *u8, (vec_dot(q,0,unit,0*D,D)*1000)/(S*S), 1000, tot) 103 chk("cos(q,d1 banana) " as *u8, (vec_dot(q,0,unit,1*D,D)*1000)/(S*S), 0, tot) // orthogonal 104 chk("cos(q,d2 apple+ban) " as *u8, (vec_dot(q,0,unit,2*D,D)*1000)/(S*S), 707, tot) // 45 deg 105 chk("cos(q,d3 car) " as *u8, (vec_dot(q,0,unit,3*D,D)*1000)/(S*S), 0, tot) // orthogonal 106 chk("cos(q,d4 apple strong)" as *u8, (vec_dot(q,0,unit,4*D,D)*1000)/(S*S), 1000, tot) // same direction 107 108 let t1: i64 = vidx_top1(unit, ndoc, D, q, S) 109 vw(" top-1 doc id = "); vn(t1); vw(" (expect 0 or 4 = an 'apple' doc)\n") 110 var t1ok: i64=0; if t1==0 { t1ok=1 } if t1==4 { t1ok=1 } 111 if t1ok==1 { vw(" top-1 = apple doc PASS\n"); tot[0]=tot[0]+1 } else { vw(" top-1 wrong FAIL\n") } 112 tot[1]=tot[1]+1 113 114 // NEG-CONTROL: an orthogonal doc (d1 banana, cos 0) must NOT outrank a parallel one (d0, cos 1000). 115 vw(" NEG-CONTROL: orthogonal doc must NOT outrank parallel -> ") 116 let cpar: i64 = (vec_dot(q,0,unit,0*D,D)*1000)/(S*S) 117 let corth: i64 = (vec_dot(q,0,unit,1*D,D)*1000)/(S*S) 118 var neg: i64=0 119 if corth<cpar { vw("orthogonal("); vn(corth); vw(") < parallel("); vn(cpar); vw(") CAUGHT\n"); neg=1 } else { vw("BROKEN\n") } 120 if neg==1 { tot[0]=tot[0]+1 } tot[1]=tot[1]+1 121 122 vw("VEC-INDEX KAT "); vn(tot[0]); vw("/"); vn(tot[1]); vw("\n") 123 if tot[0]==tot[1] { vw("GREEN -- integer cosine k-NN verified; ranks by real similarity (Milvus/ANN store REPLACED sovereignly). R3b = trained EMBEDDER to fill it.\n"); return 0 } 124 vw("RED -- vector index KAT failed\n") 125 return 1 126}