code wiki / _hdl_build / nx_semppmi_build.nx

nx_semppmi_build.nx source

↩ module page · 471 lines · 21472 B

1// nx_semppmi_build.nx -- EXPLICIT-VOCAB SPARSE PPMI embeddings = the CANONICAL count-based method at full 2// strength (Levy&Goldberg: full sparse PPMI rows rival trained embeddings on word similarity). This is the 3// last count-method rung before "trained-only" is airtight: v1 (nx_semcorpus_build) failed its probes with 4// HASHED rows (collisions) + a 256-dim PROJECTION (lossy) -- this build removes BOTH: every word gets its own 5// row; contexts are the full vocabulary; cells are PPMI (positive pointwise mutual information, log2 x16 6// fixed-point); rows stay SPARSE (sorted triples). Corpus = same banked benchmark prose (no gold leakage: 7// only JSON strings >= 48 chars). 8// PIPELINE (one corpus walk): tokens -> 61-bit djb2x hash stream (+ -1 markers at string ends) -> 9// vocab = sort(copy)+unique (explicit; 61-bit space => collisions ~0 at 60k words) -> 10// window +-4 pair emission as packed keys (aid<<20|bid, both directions) -> sort -> RLE -> triples(cnt>=2) -> 11// marginals -> PPMI16 -> row index + norms -> PROBES -> persist knowledge/index/semppmi_v1.bin. 12// PROBES decide (same discipline as v1): if won~defeated fails to order again, count methods are DEAD at 13// canonical strength and the R1 trained thread is the proven only door. TEETH structural + neg-controls. 14// expect_exit: 0 license_tier: ORIGINAL 15import "nx_qabench_engine.nx" 16import "nx_estate_path.nx" // ep_anchor: the CWD must not decide this organ verdict 17const SP_MAGIC_262144: i64 = 262144 18const SP_MAGIC_16777216: i64 = 16777216 19const SP_MAGIC_250000: i64 = 250000 20const SP_MAGIC_4000: i64 = 4000 21const SP_MAGIC_32768: i64 = 32768 22const SP_MAGIC_3500000: i64 = 3500000 23const SP_MAGIC_30000000: i64 = 30000000 24const SP_MAGIC_1048576: i64 = 1048576 25const SP_MAGIC_8000000: i64 = 8000000 26const SP_MAGIC_4096: i64 = 4096 27const SP_MAGIC_3000: i64 = 3000 28const SP_MAGIC_10000: i64 = 10000 29const SP_MAGIC_100000: i64 = 100000 30const SP_MAGIC_50000: i64 = 50000 31 32const SP_WIN: i64 = 4 33const SP_MINSTR: i64 = 48 34const SP_MAXV: i64 = 262144 35const SP_MINCNT: i64 = 2 36 37// 61-bit string hash: SHARED single-source db_semhash from the engine (nx_qabench resolves with the same fn) 38func sp_hash(buf: *u8, off: i64, len: i64) -> i64 { return db_semhash(buf, off, len) } 39 40// write n bytes to fd; 0 ok, 1 err 41func sp_wblock(fd: i64, buf: *u8, n: i64) -> i64 { 42 var off: i64 = 0 43 while off < n { 44 var chunk: i64 = SP_MAGIC_262144 45 if n - off < chunk { chunk = n - off } 46 let ww: i64 = sys_write(fd, (buf as i64 + off) as *u8, chunk) 47 if ww <= 0 { return 1 } 48 off = off + ww 49 } 50 return 0 51} 52 53func sp_isqrt(v: i64) -> i64 { 54 if v <= 0 { return 0 } 55 var x: i64 = v 56 var y: i64 = (x + 1) / 2 57 while y < x { x = y; let q: i64 = v / x; y = (x + q) / 2 } 58 return x 59} 60 61// log2 in 1/16ths of the ratio q (q >= 1); bitlen-based + 4-bit linear fraction 62func sp_ilog2x16(q: i64) -> i64 { 63 if q <= 1 { return 0 } 64 var bl: i64 = 0 65 var t: i64 = q 66 while t > 1 { t = t / 2; bl = bl + 1 } 67 var frac: i64 = 0 68 if bl >= 4 { frac = (q / (1 << (bl-4))) - 16 } 69 if bl < 4 { frac = (q << (4-bl)) - 16 } 70 if frac < 0 { frac = 0 } 71 if frac > 15 { frac = 15 } 72 return bl*16 + frac 73} 74 75// in-place quicksort of a[lo..hi] 76func sp_qsort(a: *i64, lo: i64, hi: i64) -> i64 { 77 if lo >= hi { return 0 } 78 let p: i64 = a[(lo+hi)/2] 79 var i: i64 = lo 80 var j: i64 = hi 81 while i <= j { 82 while a[i] < p { i = i + 1 } 83 while a[j] > p { j = j - 1 } 84 if i <= j { 85 let tmp: i64 = a[i] 86 a[i] = a[j] 87 a[j] = tmp 88 i = i + 1 89 j = j - 1 90 } 91 } 92 sp_qsort(a, lo, j) 93 sp_qsort(a, i, hi) 94 return 0 95} 96 97// binary search: SHARED db_bsearch_i64 from the engine 98func sp_bsearch(a: *i64, n: i64, v: i64) -> i64 { return db_bsearch_i64(a, n, v) } 99 100// walk one raw file's long JSON strings -> append token hashes to stream (marker -1 between strings). 101// c[0]=strings c[1]=tokens. stream cap slots in c[2] (bump in c[3]). 102func sp_walk(g: *i64, path: *u8, stream: *i64, c: *i64) -> i64 { 103 let total: i64 = db_read_raw(g, path, SP_MAGIC_16777216) 104 if total <= 0 { db_w(" [absent] "); db_w(path); db_w("\n" as *u8); return 0 } 105 if total == 0-2 { db_w(" [too big] "); db_w(path); db_w("\n" as *u8); return 0 } 106 g[2] = db_body_start(g, total) 107 g[1] = total - g[2] 108 let scr: *u8 = g[5] as *u8 109 let nrm: *u8 = g[6] as *u8 110 let toff: *i64 = g[7] as *i64 111 let tlen: *i64 = g[8] as *i64 112 g[3] = 0 113 while g[3] < g[1] { 114 let ch: i64 = db_b(g, g[3]) 115 if ch == 34 { 116 let sl: i64 = db_dec_str(g, scr, SP_MAGIC_250000) 117 if sl >= SP_MINSTR { 118 c[0] = c[0] + 1 119 let nl: i64 = qs_norm(scr, nrm) 120 let nt: i64 = qs_tok(nrm, nl, toff, tlen, SP_MAGIC_4000) 121 var j: i64 = 0 122 while j < nt { 123 if tlen[j] >= 2 { if db_is_stop(nrm, toff[j], tlen[j]) == 0 { 124 if c[3] < c[2] { 125 let hv: i64 = sp_hash(nrm, toff[j], tlen[j]) 126 stream[c[3]] = hv 127 c[3] = c[3] + 1 128 c[1] = c[1] + 1 129 } 130 } } 131 j = j + 1 132 } 133 if c[3] < c[2] { stream[c[3]] = 0-1; c[3] = c[3] + 1 } 134 } 135 } else { g[3] = g[3] + 1 } 136 } 137 db_w(" walked "); db_w(path); db_w(" strings="); db_n(c[0]); db_w(" tokens="); db_n(c[1]); db_w("\n" as *u8) 138 return 1 139} 140 141// cosine between sparse PPMI rows a and b: rows = triples[ridx[a]..ridx[a+1]) sorted by ctx id. 142// trip layout: tctx[i], tval[i]. norms nrm2[] precomputed (sum of val^2). 143func sp_cos(tctx: *i64, tval: *i64, ridx: *i64, nrm2: *i64, a: i64, b: i64) -> i64 { 144 var ia: i64 = ridx[a] 145 var ib: i64 = ridx[b] 146 let ea: i64 = ridx[a+1] 147 let eb: i64 = ridx[b+1] 148 var dot: i64 = 0 149 while ia < ea { 150 if ib >= eb { ia = ea } else { 151 if tctx[ia] == tctx[ib] { dot = dot + tval[ia]*tval[ib]; ia = ia + 1; ib = ib + 1 } 152 else { if tctx[ia] < tctx[ib] { ia = ia + 1 } else { ib = ib + 1 } } 153 } 154 } 155 if dot <= 0 { return 0 } 156 let d1: i64 = sp_isqrt(nrm2[a]) 157 let d2: i64 = sp_isqrt(nrm2[b]) 158 if d1 == 0 { return 0 } 159 if d2 == 0 { return 0 } 160 var cv: i64 = (dot*1000)/(d1*d2) 161 if cv > 1000 { cv = 1000 } 162 return cv 163} 164 165// probe helper: word -> vocab id (via sorted hash list) 166func sp_wid(vh: *i64, nv: i64, w: *u8) -> i64 { 167 var n: i64 = 0 168 while w[n] != (0 as u8) { n = n + 1 } 169 let hv: i64 = sp_hash(w, 0, n) 170 return sp_bsearch(vh, nv, hv) 171} 172 173func main() -> i64 { 174 // ANCHOR FIRST (2026-08-04, nx_cwdguard finding): this organ reads a RELATIVE 175 // knowledge/ path, so its answer depended on where it was launched. No-op when 176 // already at the estate root, so the cron/MCP context is unchanged. 177 ep_anchor() 178 db_w("=== nx_semppmi_build -- EXPLICIT-vocab sparse PPMI (canonical count method, the last pre-trained rung) ===\n" as *u8) 179 let g: *i64 = sys_mmap(256) as *i64 180 let m0: *u8 = sys_mmap(SP_MAGIC_16777216); g[0] = m0 as i64 181 let m5: *u8 = sys_mmap(SP_MAGIC_262144); g[5] = m5 as i64 182 let m6: *u8 = sys_mmap(SP_MAGIC_262144); g[6] = m6 as i64 183 let m7: *u8 = sys_mmap(SP_MAGIC_32768); g[7] = m7 as i64 184 let m8: *u8 = sys_mmap(SP_MAGIC_32768); g[8] = m8 as i64 185 186 let SCAP: i64 = SP_MAGIC_3500000 187 let stream: *i64 = sys_mmap(SCAP*8) as *i64 188 let c: *i64 = sys_mmap(64) as *i64 189 c[0]=0; c[1]=0; c[2]=SCAP; c[3]=0 190 191 // INPUT CORPUS IS CONF-OVERRIDABLE (2026-08-13, mirror of the output conf below): if 192 // knowledge/index/semppmi_corpus.conf exists, its whitespace-separated tokens are the corpus 193 // files to walk INSTEAD of the qab default -- the domain-matched-model experiment (a model built 194 // FROM the collection it scores) needs the INPUT parameterised exactly like the OUTPUT already 195 // is. No conf = byte-identical historic behaviour (Rule 19). RETIRE the conf after an 196 // experiment: a lingering conf silently changes the model every consumer rebuild reads. 197 var used_conf: i64 = 0 198 let icfd: i64 = sys_openat_rd("knowledge/index/semppmi_corpus.conf" as *u8) 199 if icfd >= 0 { 200 let icb: *u8 = sys_mmap(SP_MAGIC_4096) 201 let icn: i64 = sys_read(icfd, icb, SP_MAGIC_4000) 202 sys_close(icfd) 203 let pbuf: *u8 = sys_mmap(SP_MAGIC_4096) 204 var pi: i64 = 0 205 var pl: i64 = 0 206 var walked: i64 = 0 207 while pi <= icn { 208 var ch: i64 = 0 209 if pi < icn { ch = icb[pi] as i64 } 210 if ch > 32 { pbuf[pl] = ch as u8; pl = pl + 1 } else { 211 if pl > 3 { 212 pbuf[pl] = 0 as u8 213 sp_walk(g, pbuf, stream, c) 214 walked = walked + 1 215 } 216 pl = 0 217 } 218 pi = pi + 1 219 } 220 if walked > 0 { used_conf = 1 } 221 } 222 if used_conf == 0 { 223 sp_walk(g, "knowledge/fetched/qab_squad_big.raw" as *u8, stream, c) 224 sp_walk(g, "knowledge/fetched/qab_hotpot_big.raw" as *u8, stream, c) 225 sp_walk(g, "knowledge/fetched/qab_musique_big.raw" as *u8, stream, c) 226 // corpus-growth chunks (DISJOINT from the eval rows above; absent files skip cleanly) 227 sp_walk(g, "knowledge/fetched/qab_squad_c0.raw" as *u8, stream, c) 228 sp_walk(g, "knowledge/fetched/qab_squad_c1.raw" as *u8, stream, c) 229 sp_walk(g, "knowledge/fetched/qab_squad_c2.raw" as *u8, stream, c) 230 sp_walk(g, "knowledge/fetched/qab_hotpot_c0.raw" as *u8, stream, c) 231 sp_walk(g, "knowledge/fetched/qab_hotpot_c1.raw" as *u8, stream, c) 232 sp_walk(g, "knowledge/fetched/qab_hotpot_c2.raw" as *u8, stream, c) 233 sp_walk(g, "knowledge/fetched/qab_musique_c0.raw" as *u8, stream, c) 234 sp_walk(g, "knowledge/fetched/qab_musique_c1.raw" as *u8, stream, c) 235 } 236 let slen: i64 = c[3] 237 238 // ---- vocab: sort a copy of the hash stream, unique (markers filtered) ---- 239 let vs: *i64 = sys_mmap(SCAP*8) as *i64 240 var vn: i64 = 0 241 var i: i64 = 0 242 while i < slen { let sv: i64 = stream[i]; if sv >= 0 { vs[vn] = sv; vn = vn + 1 } i = i + 1 } 243 sp_qsort(vs, 0, vn-1) 244 let vh: *i64 = sys_mmap(SP_MAXV*8) as *i64 245 var nv: i64 = 0 246 i = 0 247 while i < vn { 248 var take: i64 = 1 249 if i > 0 { if vs[i] == vs[i-1] { take = 0 } } 250 if take == 1 { if nv < SP_MAXV { vh[nv] = vs[i]; nv = nv + 1 } } 251 i = i + 1 252 } 253 db_w("vocab (explicit, unique) = "); db_n(nv); db_w(" words from "); db_n(vn); db_w(" events\n" as *u8) 254 255 // ---- pair emission: packed key (aid<<20)|bid for window pairs, both directions ---- 256 let PCAP: i64 = SP_MAGIC_30000000 257 let pairs: *i64 = sys_mmap(PCAP*8) as *i64 258 var np: i64 = 0 259 i = 0 260 while i < slen { 261 if stream[i] >= 0 { 262 let av: i64 = sp_bsearch(vh, nv, stream[i]) 263 if av >= 0 { 264 var b: i64 = i + 1 265 var bend: i64 = i + SP_WIN 266 if bend > slen-1 { bend = slen-1 } 267 var stop: i64 = 0 268 while b <= bend { 269 if stop == 0 { 270 if stream[b] < 0 { stop = 1 } else { 271 let bv: i64 = sp_bsearch(vh, nv, stream[b]) 272 if bv >= 0 { if np < PCAP-2 { 273 pairs[np] = av*SP_MAGIC_1048576 + bv 274 pairs[np+1] = bv*SP_MAGIC_1048576 + av 275 np = np + 2 276 } } 277 } 278 } 279 b = b + 1 280 } 281 } 282 } 283 i = i + 1 284 } 285 db_w("window pair-events = "); db_n(np); db_w("\n" as *u8) 286 sp_qsort(pairs, 0, np-1) 287 288 // ---- RLE -> triples (cnt >= SP_MINCNT), marginals ---- 289 let TCAP: i64 = SP_MAGIC_8000000 290 let taid: *i64 = sys_mmap(TCAP*8) as *i64 291 let tctx: *i64 = sys_mmap(TCAP*8) as *i64 292 let tcnt: *i64 = sys_mmap(TCAP*8) as *i64 293 let marg: *i64 = sys_mmap(SP_MAXV*8) as *i64 294 var nt3: i64 = 0 295 var run: i64 = 1 296 i = 1 297 var dropped: i64 = 0 298 while i <= np { 299 var flush: i64 = 1 300 if i < np { if pairs[i] == pairs[i-1] { run = run + 1; flush = 0 } } 301 if flush == 1 { 302 let key: i64 = pairs[i-1] 303 let aid: i64 = key / SP_MAGIC_1048576 304 let bid: i64 = key % SP_MAGIC_1048576 305 marg[aid] = marg[aid] + run 306 if run >= SP_MINCNT { if nt3 < TCAP { 307 taid[nt3] = aid; tctx[nt3] = bid; tcnt[nt3] = run; nt3 = nt3 + 1 308 } } else { dropped = dropped + 1 } 309 run = 1 310 } 311 i = i + 1 312 } 313 db_w("triples cnt>="); db_n(SP_MINCNT); db_w(": "); db_n(nt3); db_w(" (singleton pairs dropped="); db_n(dropped); db_w(")\n" as *u8) 314 315 // total mass T for PMI 316 var TT: i64 = 0 317 i = 0 318 while i < nv { TT = TT + marg[i]; i = i + 1 } 319 320 // ---- PPMI16 transform in place on tcnt; row index + norms ---- 321 let ridx: *i64 = sys_mmap((SP_MAXV+2)*8) as *i64 322 let nrm2: *i64 = sys_mmap(SP_MAXV*8) as *i64 323 var cur: i64 = 0 324 i = 0 325 while i < nv { ridx[i] = 0; nrm2[i] = 0; i = i + 1 } 326 ridx[nv] = 0 327 // triples are sorted by (aid,ctx) already (pair sort). build ridx by counting then prefix. 328 i = 0 329 while i < nt3 { let ai: i64 = taid[i]; ridx[ai] = ridx[ai] + 1; i = i + 1 } 330 var pref: i64 = 0 331 i = 0 332 while i <= nv { let cnt0: i64 = ridx[i]; ridx[i] = pref; pref = pref + cnt0; i = i + 1 } 333 // transform + norms (triples already in row-major order; indices align with ridx layout) 334 i = 0 335 var kept: i64 = 0 336 while i < nt3 { 337 let aid2: i64 = taid[i] 338 let bid2: i64 = tctx[i] 339 let cc: i64 = tcnt[i] 340 let den: i64 = marg[aid2] * marg[bid2] 341 var pp: i64 = 0 342 if den > 0 { 343 let q: i64 = (cc * TT * 256) / den 344 let lg: i64 = sp_ilog2x16(q) 345 if lg > 128 { pp = lg - 128 } // subtract log2(256)*16 = 8*16 -> PPMI (positive part) 346 } 347 tcnt[i] = pp 348 if pp > 0 { kept = kept + 1 } 349 nrm2[aid2] = nrm2[aid2] + pp*pp 350 i = i + 1 351 } 352 db_w("PPMI-positive cells = "); db_n(kept); db_w("/"); db_n(nt3); db_w(" total-mass T="); db_n(TT); db_w("\n" as *u8) 353 354 // ---- PROBES (the verdict): explicit sparse PPMI on the wall pairs ---- 355 let w1: i64 = sp_wid(vh, nv, "won" as *u8) 356 let w2: i64 = sp_wid(vh, nv, "defeated" as *u8) 357 let w3: i64 = sp_wid(vh, nv, "purple" as *u8) 358 let w4: i64 = sp_wid(vh, nv, "city" as *u8) 359 let w5: i64 = sp_wid(vh, nv, "stadium" as *u8) 360 let w6: i64 = sp_wid(vh, nv, "january" as *u8) 361 var p1: i64 = 0-1 362 var p2: i64 = 0-1 363 var p3: i64 = 0-1 364 var p4: i64 = 0-1 365 if w1 >= 0 { if w2 >= 0 { p1 = sp_cos(tctx, tcnt, ridx, nrm2, w1, w2) } } 366 if w1 >= 0 { if w3 >= 0 { p2 = sp_cos(tctx, tcnt, ridx, nrm2, w1, w3) } } 367 if w4 >= 0 { if w5 >= 0 { p3 = sp_cos(tctx, tcnt, ridx, nrm2, w4, w5) } } 368 if w4 >= 0 { if w6 >= 0 { p4 = sp_cos(tctx, tcnt, ridx, nrm2, w4, w6) } } 369 db_w("PROBE cos(won,defeated)="); db_n(p1); db_w(" cos(won,purple)="); db_n(p2) 370 var ord1: i64 = 0 371 if p1 > p2 { ord1 = 1; db_w(" [orders CORRECTLY]\n" as *u8) } else { db_w(" [does NOT order]\n" as *u8) } 372 db_w("PROBE cos(city,stadium)="); db_n(p3); db_w(" cos(city,january)="); db_n(p4) 373 var ord2: i64 = 0 374 if p3 > p4 { ord2 = 1; db_w(" [orders CORRECTLY]\n" as *u8) } else { db_w(" [does NOT order]\n" as *u8) } 375 376 // NEG: identity + disjoint-row zero 377 var ident: i64 = 0 378 if w1 >= 0 { ident = sp_cos(tctx, tcnt, ridx, nrm2, w1, w1) } 379 db_w("NEG identity="); db_n(ident); db_w("\n" as *u8) 380 381 // ---- persist the QUERY-side structures -> knowledge/index/semppmi_v1.bin 382 // layout: 32B header [NXPPMI1\0][nv][nt3][reserved] then vh[nv] ridx[nv+1] nrm2[nv] tctx[nt3] tval[nt3] 383 let hdr: *u8 = sys_mmap(SP_MAGIC_4096) 384 hdr[0]=78 as u8; hdr[1]=88 as u8; hdr[2]=80 as u8; hdr[3]=80 as u8; hdr[4]=77 as u8; hdr[5]=73 as u8; hdr[6]=49 as u8; hdr[7]=0 as u8 385 let hi2: *i64 = (hdr as i64 + 8) as *i64 386 hi2[0] = nv 387 hi2[1] = nt3 388 hi2[2] = 0 389 var werr: i64 = 0 390 // OUTPUT PATH IS NO LONGER HARDCODED. 20 organs consume knowledge/index/semppmi_v1.bin 391 // (nx_qabench, nx_beir_eval, nx_dr_semjudge, nx_debtcluster, nx_recall_dense, nx_ppmi_lib_gate, 392 // nx_lawsearch ...), so rebuilding this on a different corpus SILENTLY changed every one of them -- 393 // a shared-artifact clobber, not an upgrade, and the reason no domain model was ever built. 394 // Note the asymmetry this fixes: ppl_load already takes the model path as a PARAMETER, its header 395 // says so callers can A/B the general model against a domain-augmented one. The READ side was 396 // parameterised for exactly this purpose; the WRITE side never was. 397 // If knowledge/index/semppmi_out.conf exists, its first token is the destination; otherwise the 398 // historic default. Rule 19: with no conf present this is byte-identical to the old behaviour, 399 // so every existing consumer is untouched unless someone deliberately opts out. 400 let opath: *u8 = sys_mmap(SP_MAGIC_4096) 401 var opl: i64 = 0 402 let cfd: i64 = sys_openat_rd("knowledge/index/semppmi_out.conf" as *u8) 403 if cfd >= 0 { 404 let cb: *u8 = sys_mmap(SP_MAGIC_4096) 405 let cn: i64 = sys_read(cfd, cb, 1000) 406 sys_close(cfd) 407 var ci: i64 = 0 408 while ci < cn { if cb[ci] > (32 as u8) { opath[opl] = cb[ci]; opl = opl + 1 } ci = ci + 1 } 409 } 410 if opl < 4 { 411 opl = 0 412 let dflt: *u8 = "knowledge/index/semppmi_v1.bin" as *u8 413 while dflt[opl] != (0 as u8) { opath[opl] = dflt[opl]; opl = opl + 1 } 414 } 415 opath[opl] = 0 as u8 416 db_w("persist target: " as *u8); db_w(opath); db_w("\n" as *u8) 417 // FAIL-CLOSED PERSIST. This organ used to write its output UNCONDITIONALLY, even when its own 418 // teeth reported 0/5. On 2026-08-01 that destroyed the shared 43MB model twenty organs read: the 419 // corpus was absent, the build produced vocab=0 from 0 events, went RED, and PERSISTED THE EMPTY 420 // RESULT over the good file. A build that fails its own teeth must NOT ship its output -- writing 421 // after RED converts a DETECTED failure into a DESTROYED artefact, which is strictly worse than 422 // crashing. Refuse instead, and leave the previous artefact untouched. 423 // nv>0 is the minimum bar: an empty vocabulary can never be a usable model, and no legitimate 424 // corpus produces one. The caller keeps whatever was there before, which is always recoverable. 425 var may_write: i64 = 1 426 if nv <= 0 { may_write = 0 } 427 if nt3 <= 0 { may_write = 0 } 428 if may_write == 0 { 429 db_w("PERSIST REFUSED -- empty model (nv=" as *u8); db_n(nv); db_w(" nt=" as *u8); db_n(nt3) 430 db_w("). Refusing to overwrite " as *u8); db_w(opath); db_w(" with a build that failed its own teeth.\n" as *u8) 431 db_w("(fix the corpus, then re-run; the previous artefact is UNTOUCHED)\n" as *u8) 432 return 1 433 } 434 let fd: i64 = sys_openat_wr(opath, 0x1a4) 435 if fd < 0 { werr = 1 } else { 436 var hw: i64 = 0 437 while hw < 32 { let w0: i64 = sys_write(fd, (hdr as i64 + hw) as *u8, 32 - hw); if w0 <= 0 { werr = 1; hw = 32 } else { hw = hw + w0 } } 438 werr = werr + sp_wblock(fd, vh as *u8, nv*8) 439 werr = werr + sp_wblock(fd, ridx as *u8, (nv+1)*8) 440 werr = werr + sp_wblock(fd, nrm2 as *u8, nv*8) 441 werr = werr + sp_wblock(fd, tctx as *u8, nt3*8) 442 werr = werr + sp_wblock(fd, tcnt as *u8, nt3*8) 443 sys_close(fd) 444 } 445 // Report the path we ACTUALLY wrote, not a literal. The first cut of this parameterisation moved 446 // the WRITE to opath but left this message hardcoded, so a verification run printed 447 // "persisted knowledge/index/semppmi_v1.bin" while the bytes went to semppmi_scratch.bin. 448 // A write that moves while its log stays put is WORSE than a hardcoded write: the log is now 449 // confidently wrong, and every reader downstream inherits that wrongness. 450 if werr == 0 { db_w("persisted " as *u8); db_w(opath); db_w(" (query-side sparse PPMI)\n" as *u8) } else { db_w("PERSIST FAILED for " as *u8); db_w(opath); db_w("\n" as *u8) } 451 452 var pass: i64 = 0 453 if c[0] >= SP_MAGIC_3000 { pass = pass + 1 } 454 if nv >= SP_MAGIC_10000 { pass = pass + 1 } 455 if nt3 >= SP_MAGIC_100000 { pass = pass + 1 } 456 if kept >= SP_MAGIC_50000 { pass = pass + 1 } 457 if ident == 1000 { if werr == 0 { pass = pass + 1 } } 458 db_w("TEETH T1(strings)+T2(vocab)+T3(triples)+T4(ppmi-cells)+T5(identity) = "); db_n(pass); db_w("/5\n" as *u8) 459 db_w("PROBE-GATE (decides MODE2 wiring): ord(won-defeated)="); db_n(ord1); db_w(" ord(city-stadium)="); db_n(ord2); db_w("\n" as *u8) 460 if pass == 5 { 461 if ord1 == 1 { if ord2 == 1 { 462 db_w("GREEN + PROBES ORDER -- explicit sparse PPMI carries the signal; wire MODE2-soft next.\n" as *u8) 463 return 0 464 } } 465 db_w("GREEN (structure) but PROBES INSUFFICIENT -- count methods now exhausted at CANONICAL strength\n" as *u8) 466 db_w("(explicit vocab, no projection, full PPMI). The semantic wall is TRAINED-MODEL-BOUND, airtight.\n" as *u8) 467 return 0 468 } 469 db_w("RED -- build integrity failed\n" as *u8) 470 return 1 471}