code wiki / (root) / nx_jina_embed_gate.nx

nx_jina_embed_gate.nx source

↩ module page · 269 lines · 10869 B

1// nx_jina_embed_gate.nx -- E-ARC e2: JINA-WEIGHT ORACLE gate (2026-07-15). Runs the SAME sovereign 2// integer embed path (nsv_embed, last-token pooled final-norm, i8 mode) on the DOWNLOADED 3// jina-code-embeddings-0.5b Q8_0 weights (sha256-verified vs origin ETag; arch-identical Qwen2.5-Coder 4// backbone) and grades our pipeline against the model card's bf16 2x2 cosine oracle 817/124/120/553 pm. 5// cc-by-nc-4.0 weights -> BENCHMARK ORACLE ONLY (like gcc), never the production embedder. 6// GATES: 7// A ORDER cos(q1,p1)>cos(q1,p2) AND cos(q2,p2)>cos(q2,p1) 8// A2 SEPARATION tuned margins (c11-c12) and (c22-c21) must EXCEED 300 pm -- the discriminating test 9// vs BASE-coder arm (nx_code_embed_gate measured margins 72/49 pm): only truly-loaded 10// tuned weights can show the oracle's ~693/433 pm class separation. 11// B DETERM re-embed q1 after all other texts -> BITWISE identical (KV/state-leak liar killer) 12// C SELF cos(q1,q1') >= 999 pm 13// D RETRIEVE Nishi 3x3 NL->code diagonal 3/3 (vs base arm for margin comparison) 14// F DISTRACTOR unrelated prose passage must lose to the true match on BOTH queries (NEG control) 15// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 16import "nx_nofloat_serve_core.nx" 17 18const CE_NE: i64 = 896 19const CE_SLOTS: i64 = 12 20 21func ce_ws(s: *u8) -> i64 { 22 var n: i64 = 0 23 while s[n] != (0 as u8) { n = n + 1 } 24 sys_write(1, s, n) 25 return 0 26} 27 28func ce_wdec(v: i64) -> i64 { 29 let b: *u8 = sys_mmap(28) 30 let t: *u8 = sys_mmap(28) 31 var m: i64 = v 32 if m < 0 { ce_ws("-" as *u8); m = 0 - m } 33 var k: i64 = 0 34 if m == 0 { t[0] = 48 as u8; k = 1 } 35 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 36 var i: i64 = 0 37 while i < k { b[i] = t[k - 1 - i]; i = i + 1 } 38 sys_write(1, b, k) 39 return 0 40} 41 42func ce_kv(label: *u8, v: i64) -> i64 { 43 ce_ws(label) 44 ce_ws("=" as *u8) 45 ce_wdec(v) 46 ce_ws(" " as *u8) 47 return 0 48} 49 50func ce_isqrt(v: i64) -> i64 { 51 if v < 2 { return v } 52 var x: i64 = v 53 var y: i64 = (x + 1) / 2 54 while y < x { x = y; y = (x + v / x) / 2 } 55 return x 56} 57 58// cosine similarity in per-mille, overflow-safe: pre-scale both vectors by the SAME power of two so 59// max|elem| <= 2^20 -> squares*ne <= 2^50, dot*1000 <= 2^60, isqrt product <= 2^50 (all inside i64). 60func ce_cos_pm(a: *i64, b: *i64) -> i64 { 61 var mx: i64 = 1 62 var i: i64 = 0 63 while i < CE_NE { 64 var va: i64 = a[i] 65 if va < 0 { va = 0 - va } 66 var vb: i64 = b[i] 67 if vb < 0 { vb = 0 - vb } 68 if va > mx { mx = va } 69 if vb > mx { mx = vb } 70 i = i + 1 71 } 72 var dv: i64 = 1 73 while mx > 1048575 { mx = mx / 2; dv = dv * 2 } 74 var dab: i64 = 0 75 var daa: i64 = 0 76 var dbb: i64 = 0 77 i = 0 78 while i < CE_NE { 79 let ea: i64 = a[i] / dv 80 let eb: i64 = b[i] / dv 81 dab = dab + ea * eb 82 daa = daa + ea * ea 83 dbb = dbb + eb * eb 84 i = i + 1 85 } 86 let sa: i64 = ce_isqrt(daa) 87 let sb: i64 = ce_isqrt(dbb) 88 if sa == 0 { return 0 } 89 if sb == 0 { return 0 } 90 return (dab * 1000) / (sa * sb) 91} 92 93func ce_bytes_eq(a: *i64, b: *i64) -> i64 { 94 var i: i64 = 0 95 while i < CE_NE { if a[i] != b[i] { return 0 } i = i + 1 } 96 return 1 97} 98 99func ce_maxabs(a: *i64) -> i64 { 100 var mx: i64 = 0 101 var i: i64 = 0 102 while i < CE_NE { 103 var v: i64 = a[i] 104 if v < 0 { v = 0 - v } 105 if v > mx { mx = v } 106 i = i + 1 107 } 108 return mx 109} 110 111// embed prefix+content into vec slot; prints slot/ntok/ms; returns ntok (negative = error) 112func ce_embed(vecs: *i64, slot: i64, pre: *u8, content: *u8) -> i64 { 113 let txt: *u8 = sys_mmap(2048) 114 var l: i64 = nsv_cat(txt, 0, pre) 115 l = nsv_cat(txt, l, content) 116 let vp_i: i64 = (vecs as i64) + slot * CE_NE * 8 117 let vp: *i64 = vp_i as *i64 118 let t0: i64 = sys_now_ms() 119 let n: i64 = nsv_embed(txt, l, 1, vp) 120 let t1: i64 = sys_now_ms() 121 ce_ws("[embed] " as *u8) 122 ce_kv("slot" as *u8, slot) 123 ce_kv("ntok" as *u8, n) 124 ce_kv("ms" as *u8, t1 - t0) 125 ce_ws("\n" as *u8) 126 return n 127} 128 129func ce_vp(vecs: *i64, slot: i64) -> *i64 { 130 let p: i64 = (vecs as i64) + slot * CE_NE * 8 131 return p as *i64 132} 133 134func main() -> i64 { 135 ce_ws("[jina-oracle-gate] sovereign integer embed path on jina-code-embeddings-0.5b Q8_0 (benchmark oracle, cc-by-nc-4.0)\n" as *u8) 136 let ir: i64 = nsv_init("/home/elderwesto/nx_stage/nx_jina_embed_model.gguf\x00" as *u8) 137 if ir != 0 { 138 ce_kv("[jina-oracle-gate] INIT-FAIL rc" as *u8, ir) 139 ce_ws("\n" as *u8) 140 return 1 141 } 142 let vecs: *i64 = sys_mmap(CE_SLOTS * CE_NE * 8) as *i64 143 // jina EXACT task instruction prefixes (nl2code) 144 let qp: *u8 = "Find the most relevant code snippet given the following query:\n" as *u8 145 let pp: *u8 = "Candidate code snippet:\n" as *u8 146 // jina model-card 2x2 demo (bf16 jina-tuned oracle: q1p1=817 q1p2=124 q2p1=120 q2p2=553 pm) 147 var rc: i64 = 0 148 rc = ce_embed(vecs, 0, qp, "print hello world in python" as *u8) 149 if rc < 1 { return 1 } 150 rc = ce_embed(vecs, 1, qp, "initialize array of 5 zeros in c++" as *u8) 151 if rc < 1 { return 1 } 152 rc = ce_embed(vecs, 2, pp, "print('Hello World!')" as *u8) 153 if rc < 1 { return 1 } 154 rc = ce_embed(vecs, 3, pp, "int arr[5] = {0, 0, 0, 0, 0};" as *u8) 155 if rc < 1 { return 1 } 156 // Nishi-corpus 3x3 micro-retrieval (REAL API shapes from runtime/) -- base-arm comparison rows 157 rc = ce_embed(vecs, 4, qp, "read a u32 metadata value from a gguf header by key" as *u8) 158 if rc < 1 { return 1 } 159 rc = ce_embed(vecs, 5, qp, "connect to a unix domain socket at a filesystem path" as *u8) 160 if rc < 1 { return 1 } 161 rc = ce_embed(vecs, 6, qp, "rms normalize a hidden row with gamma weights in fixed point" as *u8) 162 if rc < 1 { return 1 } 163 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) 164 if rc < 1 { return 1 } 165 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) 166 if rc < 1 { return 1 } 167 rc = ce_embed(vecs, 9, pp, "rmsnorm_gamma_row_q24(h1, gout, 0, ne, normed, 0)" as *u8) 168 if rc < 1 { return 1 } 169 // B: re-embed q1 AFTER all corpus texts -> must be BITWISE identical to slot 0 170 rc = ce_embed(vecs, 10, qp, "print hello world in python" as *u8) 171 if rc < 1 { return 1 } 172 // F: unrelated prose distractor passage (NEG control) 173 rc = ce_embed(vecs, 11, pp, "The weather is sunny today and the birds are singing in the park." as *u8) 174 if rc < 1 { return 1 } 175 ce_kv("[scale] maxabs_q24_slot0" as *u8, ce_maxabs(ce_vp(vecs, 0))) 176 // ⚠HOISTED: `ce_vp(vecs, 0)[0]` INDEXES A CALL RESULT, which nx_cc miscompiles SILENTLY -- it does not 177 // crash and printed output still looks right, but the value read is wrong (debt 1785532031, gotcha 178 // banked 2026-07-31; prior in-code sightings: nx_pagerank_build_gate.nx:38 'inline cast-index 179 // miscompiles' and nx_rw_emptystr.nx 'aliases the next literal'). This site is INSIDE A GATE, so a bad 180 // read corrupts this gate's own evidence. Assign to a temp, then index the temp. 181 let v0p: *i64 = ce_vp(vecs, 0) 182 ce_kv("v0" as *u8, v0p[0]) 183 ce_ws("(weight-identity evidence: must differ from base-arm values)\n" as *u8) 184 // ---- jina 2x2 vs bf16 oracle ---- 185 let c11: i64 = ce_cos_pm(ce_vp(vecs, 0), ce_vp(vecs, 2)) 186 let c12: i64 = ce_cos_pm(ce_vp(vecs, 0), ce_vp(vecs, 3)) 187 let c21: i64 = ce_cos_pm(ce_vp(vecs, 1), ce_vp(vecs, 2)) 188 let c22: i64 = ce_cos_pm(ce_vp(vecs, 1), ce_vp(vecs, 3)) 189 ce_ws("[jina-2x2 pm] " as *u8) 190 ce_kv("q1p1" as *u8, c11) 191 ce_kv("q1p2" as *u8, c12) 192 ce_kv("q2p1" as *u8, c21) 193 ce_kv("q2p2" as *u8, c22) 194 ce_ws("(bf16 oracle: 817 124 120 553)\n" as *u8) 195 ce_ws("[oracle-delta pm] " as *u8) 196 ce_kv("d11" as *u8, c11 - 817) 197 ce_kv("d12" as *u8, c12 - 124) 198 ce_kv("d21" as *u8, c21 - 120) 199 ce_kv("d22" as *u8, c22 - 553) 200 ce_ws("\n" as *u8) 201 var gate_a: i64 = 0 202 if c11 > c12 { if c22 > c21 { gate_a = 1 } } 203 // A2: tuned separation (base arm measured margins 72/49 pm; oracle margins 693/433 pm) 204 let m1: i64 = c11 - c12 205 let m2: i64 = c22 - c21 206 var gate_a2: i64 = 0 207 if m1 > 300 { if m2 > 300 { gate_a2 = 1 } } 208 ce_ws("[separation pm] " as *u8) 209 ce_kv("margin1" as *u8, m1) 210 ce_kv("margin2" as *u8, m2) 211 ce_ws("(base arm: 72 49; oracle: 693 433; gate >300)\n" as *u8) 212 // ---- determinism / state-leak ---- 213 let gate_b: i64 = ce_bytes_eq(ce_vp(vecs, 0), ce_vp(vecs, 10)) 214 let selfpm: i64 = ce_cos_pm(ce_vp(vecs, 0), ce_vp(vecs, 10)) 215 var gate_c: i64 = 0 216 if selfpm >= 999 { gate_c = 1 } 217 ce_ws("[determinism] " as *u8) 218 ce_kv("bitwise_eq" as *u8, gate_b) 219 ce_kv("self_pm" as *u8, selfpm) 220 ce_ws("\n" as *u8) 221 // ---- nishi 3x3 retrieval ---- 222 var hits: i64 = 0 223 var r: i64 = 0 224 while r < 3 { 225 let s0: i64 = ce_cos_pm(ce_vp(vecs, 4 + r), ce_vp(vecs, 7)) 226 let s1: i64 = ce_cos_pm(ce_vp(vecs, 4 + r), ce_vp(vecs, 8)) 227 let s2: i64 = ce_cos_pm(ce_vp(vecs, 4 + r), ce_vp(vecs, 9)) 228 var am: i64 = 0 229 var best: i64 = s0 230 if s1 > best { am = 1; best = s1 } 231 if s2 > best { am = 2; best = s2 } 232 ce_ws("[nishi-3x3] " as *u8) 233 ce_kv("row" as *u8, r) 234 ce_kv("s0" as *u8, s0) 235 ce_kv("s1" as *u8, s1) 236 ce_kv("s2" as *u8, s2) 237 ce_kv("argmax" as *u8, am) 238 if am == r { hits = hits + 1; ce_ws("HIT\n" as *u8) } else { ce_ws("MISS\n" as *u8) } 239 r = r + 1 240 } 241 ce_kv("[retrieval] hits" as *u8, hits) 242 ce_ws("/3\n" as *u8) 243 // ---- F: distractor NEG control ---- 244 let cd1: i64 = ce_cos_pm(ce_vp(vecs, 0), ce_vp(vecs, 11)) 245 let cd2: i64 = ce_cos_pm(ce_vp(vecs, 1), ce_vp(vecs, 11)) 246 var gate_f: i64 = 0 247 if cd1 < c11 { if cd2 < c22 { gate_f = 1 } } 248 ce_ws("[distractor pm] " as *u8) 249 ce_kv("q1_vs_weather" as *u8, cd1) 250 ce_kv("q2_vs_weather" as *u8, cd2) 251 ce_kv("gate_f" as *u8, gate_f) 252 ce_ws("\n" as *u8) 253 // ---- verdict ---- 254 var green: i64 = 0 255 if gate_a == 1 { if gate_a2 == 1 { if gate_b == 1 { if gate_c == 1 { if hits == 3 { if gate_f == 1 { green = 1 } } } } } } 256 if green == 1 { 257 ce_ws("VERDICT GREEN jina-weight-oracle (A order + A2 tuned-separation + B bitwise-determinism + C self + D 3/3 + F distractor; our integer embed path reproduces the tuned model's signature)\n" as *u8) 258 return 0 259 } 260 ce_ws("VERDICT RED " as *u8) 261 ce_kv("A" as *u8, gate_a) 262 ce_kv("A2" as *u8, gate_a2) 263 ce_kv("B" as *u8, gate_b) 264 ce_kv("C" as *u8, gate_c) 265 ce_kv("D" as *u8, hits) 266 ce_kv("F" as *u8, gate_f) 267 ce_ws("\n" as *u8) 268 return 1 269}