code wiki / (root) / nx_code_embed_gate.nx

nx_code_embed_gate.nx source

↩ module page · 265 lines · 10373 B

1// nx_code_embed_gate.nx -- SOVEREIGN CODE-EMBEDDING gate (2026-07-14). Grounds the jina-code-embeddings 2// recipe (arXiv:2508.21290, backbone = Qwen2.5-Coder-0.5B = OUR coder) on the no-float stack: embedding = 3// last-token pooled final-norm hidden state = nsv_embed(). Task instruction prefixes = jina's EXACT strings. 4// GATES (liar-killer style): 5// A ORDER jina model-card 2x2 demo: cos(q1,p1)>cos(q1,p2) AND cos(q2,p2)>cos(q2,p1) 6// (bf16 jina-tuned oracle values 817/124/120/553 pm -- we gate ORDER only: base weights differ) 7// B DETERM re-embed q1 AFTER all other texts -> vector must be BITWISE IDENTICAL (catches KV/state leak) 8// C SELF cos(q1,q1') >= 999 pm (isqrt floor can give 1000+/-1) 9// D RETRIEVE 3x3 Nishi-corpus NL->code argmax must hit the diagonal 3/3 10// E API synthetic HTTP POST /embed through nsv_handle -> 200 + ok:1 + dim:896 (the daemon serves 11// these exact bytes -> the capability is API-reachable, rule-27 style, not gate-only) 12// HONEST SCOPE: this proves PIPELINE CORRECTNESS on BASE coder weights. SOTA retrieval quality is NOT 13// claimed -- that grading pends the jina-weight oracle (cc-by-nc-4.0 -> benchmark-only, like gcc). 14// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 15import "nx_nofloat_serve_core.nx" 16 17const CE_NE: i64 = 896 18const CE_SLOTS: i64 = 11 19 20func ce_ws(s: *u8) -> i64 { 21 var n: i64 = 0 22 while s[n] != (0 as u8) { n = n + 1 } 23 sys_write(1, s, n) 24 return 0 25} 26 27func ce_wdec(v: i64) -> i64 { 28 let b: *u8 = sys_mmap(28) 29 let t: *u8 = sys_mmap(28) 30 var m: i64 = v 31 if m < 0 { ce_ws("-" as *u8); m = 0 - m } 32 var k: i64 = 0 33 if m == 0 { t[0] = 48 as u8; k = 1 } 34 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 35 var i: i64 = 0 36 while i < k { b[i] = t[k - 1 - i]; i = i + 1 } 37 sys_write(1, b, k) 38 return 0 39} 40 41func ce_kv(label: *u8, v: i64) -> i64 { 42 ce_ws(label) 43 ce_ws("=" as *u8) 44 ce_wdec(v) 45 ce_ws(" " as *u8) 46 return 0 47} 48 49func ce_isqrt(v: i64) -> i64 { 50 if v < 2 { return v } 51 var x: i64 = v 52 var y: i64 = (x + 1) / 2 53 while y < x { x = y; y = (x + v / x) / 2 } 54 return x 55} 56 57// cosine similarity in per-mille, overflow-safe: pre-scale both vectors by the SAME power of two so 58// max|elem| <= 2^20 -> squares*ne <= 2^50, dot*1000 <= 2^60, isqrt product <= 2^50 (all inside i64). 59func ce_cos_pm(a: *i64, b: *i64) -> i64 { 60 var mx: i64 = 1 61 var i: i64 = 0 62 while i < CE_NE { 63 var va: i64 = a[i] 64 if va < 0 { va = 0 - va } 65 var vb: i64 = b[i] 66 if vb < 0 { vb = 0 - vb } 67 if va > mx { mx = va } 68 if vb > mx { mx = vb } 69 i = i + 1 70 } 71 var dv: i64 = 1 72 while mx > 1048575 { mx = mx / 2; dv = dv * 2 } 73 var dab: i64 = 0 74 var daa: i64 = 0 75 var dbb: i64 = 0 76 i = 0 77 while i < CE_NE { 78 let ea: i64 = a[i] / dv 79 let eb: i64 = b[i] / dv 80 dab = dab + ea * eb 81 daa = daa + ea * ea 82 dbb = dbb + eb * eb 83 i = i + 1 84 } 85 let sa: i64 = ce_isqrt(daa) 86 let sb: i64 = ce_isqrt(dbb) 87 if sa == 0 { return 0 } 88 if sb == 0 { return 0 } 89 return (dab * 1000) / (sa * sb) 90} 91 92func ce_bytes_eq(a: *i64, b: *i64) -> i64 { 93 var i: i64 = 0 94 while i < CE_NE { if a[i] != b[i] { return 0 } i = i + 1 } 95 return 1 96} 97 98// substring search: index of needle in hay[0,hn) or -1 99func ce_find(hay: *u8, hn: i64, needle: *u8) -> i64 { 100 var nl: i64 = 0 101 while needle[nl] != (0 as u8) { nl = nl + 1 } 102 if nl == 0 { return 0 } 103 var i: i64 = 0 104 while i + nl <= hn { 105 var k: i64 = 0 106 var ok: i64 = 1 107 while k < nl { if hay[i+k] != needle[k] { ok = 0; k = nl } else { k = k + 1 } } 108 if ok == 1 { return i } 109 i = i + 1 110 } 111 return 0 - 1 112} 113 114func ce_maxabs(a: *i64) -> i64 { 115 var mx: i64 = 0 116 var i: i64 = 0 117 while i < CE_NE { 118 var v: i64 = a[i] 119 if v < 0 { v = 0 - v } 120 if v > mx { mx = v } 121 i = i + 1 122 } 123 return mx 124} 125 126// embed prefix+content into vec slot; prints slot/ntok/ms; returns ntok (negative = error) 127func ce_embed(vecs: *i64, slot: i64, pre: *u8, content: *u8) -> i64 { 128 let txt: *u8 = sys_mmap(2048) 129 var l: i64 = nsv_cat(txt, 0, pre) 130 l = nsv_cat(txt, l, content) 131 let vp_i: i64 = (vecs as i64) + slot * CE_NE * 8 132 let vp: *i64 = vp_i as *i64 133 let t0: i64 = sys_now_ms() 134 let n: i64 = nsv_embed(txt, l, 1, vp) 135 let t1: i64 = sys_now_ms() 136 ce_ws("[embed] " as *u8) 137 ce_kv("slot" as *u8, slot) 138 ce_kv("ntok" as *u8, n) 139 ce_kv("ms" as *u8, t1 - t0) 140 ce_ws("\n" as *u8) 141 return n 142} 143 144func ce_vp(vecs: *i64, slot: i64) -> *i64 { 145 let p: i64 = (vecs as i64) + slot * CE_NE * 8 146 return p as *i64 147} 148 149func main() -> i64 { 150 ce_ws("[embed-gate] sovereign code embedding (jina recipe arXiv:2508.21290) on no-float coder, mode=i8\n" as *u8) 151 let ir: i64 = nsv_init("/home/elderwesto/nx_stage/nx_coder_model.gguf\x00" as *u8) 152 if ir != 0 { 153 ce_kv("[embed-gate] INIT-FAIL rc" as *u8, ir) 154 ce_ws("\n" as *u8) 155 return 1 156 } 157 let vecs: *i64 = sys_mmap(CE_SLOTS * CE_NE * 8) as *i64 158 // jina EXACT task instruction prefixes (nl2code) 159 let qp: *u8 = "Find the most relevant code snippet given the following query:\n" as *u8 160 let pp: *u8 = "Candidate code snippet:\n" as *u8 161 // jina model-card 2x2 demo (bf16 jina-tuned oracle: q1p1=817 q1p2=124 q2p1=120 q2p2=553 pm) 162 var rc: i64 = 0 163 rc = ce_embed(vecs, 0, qp, "print hello world in python" as *u8) 164 if rc < 1 { return 1 } 165 rc = ce_embed(vecs, 1, qp, "initialize array of 5 zeros in c++" as *u8) 166 if rc < 1 { return 1 } 167 rc = ce_embed(vecs, 2, pp, "print('Hello World!')" as *u8) 168 if rc < 1 { return 1 } 169 rc = ce_embed(vecs, 3, pp, "int arr[5] = {0, 0, 0, 0, 0};" as *u8) 170 if rc < 1 { return 1 } 171 // Nishi-corpus 3x3 micro-retrieval (REAL API shapes from runtime/) 172 rc = ce_embed(vecs, 4, qp, "read a u32 metadata value from a gguf header by key" as *u8) 173 if rc < 1 { return 1 } 174 rc = ce_embed(vecs, 5, qp, "connect to a unix domain socket at a filesystem path" as *u8) 175 if rc < 1 { return 1 } 176 rc = ce_embed(vecs, 6, qp, "rms normalize a hidden row with gamma weights in fixed point" as *u8) 177 if rc < 1 { return 1 } 178 rc = ce_embed(vecs, 7, pp, "nx_gguf_meta_find(buf, flen, hdr, key, klen, voff, vty)\nlet v: i64 = nx_gguf_meta_read_u32(buf, voff[0])" as *u8) 179 if rc < 1 { return 1 } 180 rc = ce_embed(vecs, 8, pp, "let fd: i64 = sys_socket(1, 1, 0)\nsa[0] = 1 as u8\nsys_connect(fd, sa as *u8, 110)" as *u8) 181 if rc < 1 { return 1 } 182 rc = ce_embed(vecs, 9, pp, "rmsnorm_gamma_row_q24(h1, gout, 0, ne, normed, 0)" as *u8) 183 if rc < 1 { return 1 } 184 // B: re-embed q1 LAST (after every other text) -> must be BITWISE identical to slot 0 185 rc = ce_embed(vecs, 10, qp, "print hello world in python" as *u8) 186 if rc < 1 { return 1 } 187 ce_kv("[scale] maxabs_q24_slot0" as *u8, ce_maxabs(ce_vp(vecs, 0))) 188 ce_ws("\n" as *u8) 189 // ---- jina 2x2 ---- 190 let c11: i64 = ce_cos_pm(ce_vp(vecs, 0), ce_vp(vecs, 2)) 191 let c12: i64 = ce_cos_pm(ce_vp(vecs, 0), ce_vp(vecs, 3)) 192 let c21: i64 = ce_cos_pm(ce_vp(vecs, 1), ce_vp(vecs, 2)) 193 let c22: i64 = ce_cos_pm(ce_vp(vecs, 1), ce_vp(vecs, 3)) 194 ce_ws("[jina-2x2 pm] " as *u8) 195 ce_kv("q1p1" as *u8, c11) 196 ce_kv("q1p2" as *u8, c12) 197 ce_kv("q2p1" as *u8, c21) 198 ce_kv("q2p2" as *u8, c22) 199 ce_ws("(jina bf16 oracle: 817 124 120 553)\n" as *u8) 200 var gate_a: i64 = 0 201 if c11 > c12 { if c22 > c21 { gate_a = 1 } } 202 // ---- determinism / state-leak ---- 203 let gate_b: i64 = ce_bytes_eq(ce_vp(vecs, 0), ce_vp(vecs, 10)) 204 let selfpm: i64 = ce_cos_pm(ce_vp(vecs, 0), ce_vp(vecs, 10)) 205 var gate_c: i64 = 0 206 if selfpm >= 999 { gate_c = 1 } 207 ce_ws("[determinism] " as *u8) 208 ce_kv("bitwise_eq" as *u8, gate_b) 209 ce_kv("self_pm" as *u8, selfpm) 210 ce_ws("\n" as *u8) 211 // ---- nishi 3x3 retrieval ---- 212 var hits: i64 = 0 213 var r: i64 = 0 214 while r < 3 { 215 let s0: i64 = ce_cos_pm(ce_vp(vecs, 4 + r), ce_vp(vecs, 7)) 216 let s1: i64 = ce_cos_pm(ce_vp(vecs, 4 + r), ce_vp(vecs, 8)) 217 let s2: i64 = ce_cos_pm(ce_vp(vecs, 4 + r), ce_vp(vecs, 9)) 218 var am: i64 = 0 219 var best: i64 = s0 220 if s1 > best { am = 1; best = s1 } 221 if s2 > best { am = 2; best = s2 } 222 ce_ws("[nishi-3x3] " as *u8) 223 ce_kv("row" as *u8, r) 224 ce_kv("s0" as *u8, s0) 225 ce_kv("s1" as *u8, s1) 226 ce_kv("s2" as *u8, s2) 227 ce_kv("argmax" as *u8, am) 228 if am == r { hits = hits + 1; ce_ws("HIT\n" as *u8) } else { ce_ws("MISS\n" as *u8) } 229 r = r + 1 230 } 231 ce_kv("[retrieval] hits" as *u8, hits) 232 ce_ws("/3\n" as *u8) 233 // ---- E: the API route (synthetic HTTP request through nsv_handle; the daemon serves these bytes) ---- 234 let req: *u8 = sys_mmap(4096) 235 let qn: i64 = nsv_cat(req, 0, "POST /embed HTTP/1.1\r\nContent-Type: application/json\r\n\r\n{\"text\":\"open a file and read all of its bytes\",\"task\":\"nl2code\",\"kind\":\"query\",\"mode\":\"i8\"}" as *u8) 236 let resb: *u8 = sys_mmap(65536) 237 let rl: i64 = nsv_handle(req, qn, resb, 65536) 238 var gate_e: i64 = 0 239 if rl > 60 { if resb[9]==(50 as u8) { if resb[10]==(48 as u8) { if resb[11]==(48 as u8) { 240 if ce_find(resb, rl, "\"ok\":1" as *u8) >= 0 { if ce_find(resb, rl, "\"dim\":896" as *u8) >= 0 { gate_e = 1 } } 241 } } } } 242 ce_ws("[api] POST /embed via nsv_handle " as *u8) 243 ce_kv("resp_len" as *u8, rl) 244 ce_kv("gate_e" as *u8, gate_e) 245 ce_ws("\n[api] resp head: " as *u8) 246 var eh: i64 = rl 247 if eh > 200 { eh = 200 } 248 sys_write(1, resb, eh) 249 ce_ws("...\n" as *u8) 250 // ---- verdict ---- 251 var green: i64 = 0 252 if gate_a == 1 { if gate_b == 1 { if gate_c == 1 { if hits == 3 { if gate_e == 1 { green = 1 } } } } } 253 if green == 1 { 254 ce_ws("VERDICT GREEN sovereign-code-embed cpu-i8 (A order + B bitwise-determinism + C self + D 3/3 + E api-route; pipeline correctness on BASE weights -- SOTA grading pends jina-weight oracle)\n" as *u8) 255 return 0 256 } 257 ce_ws("VERDICT RED " as *u8) 258 ce_kv("A" as *u8, gate_a) 259 ce_kv("B" as *u8, gate_b) 260 ce_kv("C" as *u8, gate_c) 261 ce_kv("D" as *u8, hits) 262 ce_kv("E" as *u8, gate_e) 263 ce_ws("\n" as *u8) 264 return 1 265}