code wiki / _hdl_build / nx_vec_embed.nx

nx_vec_embed.nx source

↩ module page · 40 lines · 2122 B

1// nx_vec_embed.nx -- R-VEC-1 of the onsite-search S-class ladder: SOVEREIGN text->vector embeddings (LIBRARY), 2// the CLASSICAL (count-based distributional) kind -- "methods to generate this mapping include neural networks" 3// (cited srch_embedding.raw); this is the pre-neural, fully-sovereign, no-external-weights path (ladder option A). 4// Principle (distributional semantics, cited srch_word2vec.raw): words in SIMILAR CONTEXTS get SIMILAR vectors. 5// Realized with zero ML training instability: a term x term co-occurrence matrix from the corpus; each term's 6// vector is its co-occurrence row; a query/doc is the bag-sum of its term vectors; relatedness = cosine 7// (R-VEC-0 kernel). KAT lives in nx_vec_embed_gate.nx (library+gate split, matching nx_vec_kernel/nx_vec_fuse). 8// 9// HONEST: count-based, NOT neural understanding -- the neural cross-encoder stays census-BEHIND (R-VEC-6). 10// Production scale = fill the matrix from the real manifest + reduce dims (R-VEC-4 quantization) -- wiring, not math. 11// 12// exports: vr_embed (term->vec), ve_docvec (bag->vec), ve_build_co. license_tier: ORIGINAL 13import "nx_syscalls.nx" 14 15const VE_V: i64 = 17 // controlled-corpus vocab size (gate); production reads vocab from the index 16 17// accumulate co-occurrence for one sentence (every ordered pair of distinct terms) 18func ve_build_co(M: *i64, sent: *i64, L: i64) -> i64 { 19 var i: i64=0 20 while i<L { 21 var j: i64=0 22 while j<L { 23 if i!=j { let a: i64=sent[i]; let b: i64=sent[j]; M[a*VE_V+b]=M[a*VE_V+b]+1 } 24 j=j+1 25 } 26 i=i+1 27 } 28 return 0 29} 30 31// vr_embed: term id -> its distributional vector (co-occurrence row pointer) 32func vr_embed(M: *i64, t: i64) -> *i64 { return ((M as i64) + t*VE_V*8) as *i64 } 33 34// ve_docvec: a bag of term ids -> one summed vector (out is VE_V-dim, written here) 35func ve_docvec(M: *i64, terms: *i64, nt: i64, out: *i64) -> i64 { 36 var k: i64=0; while k<VE_V { out[k]=0; k=k+1 } 37 var i: i64=0 38 while i<nt { let row: *i64=vr_embed(M, terms[i]); var c: i64=0; while c<VE_V { out[c]=out[c]+row[c]; c=c+1 } i=i+1 } 39 return 0 40}