code wiki / _hdl_build / nx_ppmi_lib_gate.nx

nx_ppmi_lib_gate.nx source

↩ module page · 101 lines · 4531 B

1// nx_ppmi_lib_gate.nx -- KAT + neg-controls for the shared PPMI library (seq283) and its new 2// IDF weighting. Proves: the path-parameterised loader works, informativeness weighting really 3// does rank a ubiquitous function word below a discriminative one, out-of-vocabulary terms are 4// EXCLUDED from the weighted denominator instead of dragging a flat mean toward zero, and the 5// weighted scorer still agrees with the flat one where no weighting distinction exists. 6// Runs from the nxc2 CWD so knowledge/index/semppmi_v1.bin resolves. DRY nx_gate_verdict lib. 7import "nx_ppmi_lib.nx" 8import "nx_gate_verdict.nx" 9 10// resolve a single word literal -> vocab id 11func plg_wid(g: *i64, s: *u8) -> i64 { 12 var n: i64 = 0 13 while s[n] != (0 as u8) { n = n + 1 } 14 let out: *i64 = sys_mmap(8) as *i64 15 ppl_tokenize_ids(g, s, n, out, 1) 16 return out[0] 17} 18 19func main() -> i64 { 20 let ctr: *i64 = gv_ctr() 21 gv_head("nx_ppmi_lib -- shared PPMI loader + IDF weighting (seq283)") 22 23 let g: *i64 = sys_mmap(128 * 8) as *i64 24 let ok_load: i64 = ppl_load(g, "knowledge/index/semppmi_v1.bin" as *u8) 25 26 // T1 the path-parameterised loader works (this is what made model A/B possible) 27 var ok1: i64 = 0 28 if ok_load == 1 { if g[71] > 1000 { ok1 = 1 } } 29 gv_check("T1 path-parameterised loader loads a real model", ok1, ctr) 30 31 // ⚠FINDING (this gate caught a false premise): the model builder STOP-FILTERS, so "the" is 32 // NOT in the vocabulary at all -- comparing against it made the weighting test pass for the 33 // wrong reason (a bare OOV zero). Both probes must be genuinely in-vocab for T3 to mean 34 // anything. "city" is the broader/more frequent term, "stadium" the narrower one. 35 let id_city: i64 = plg_wid(g, "city" as *u8) 36 let id_sta: i64 = plg_wid(g, "stadium" as *u8) 37 38 // T2 both probe words are in vocabulary (guards T3 from silently testing a -1) 39 var ok2: i64 = 0 40 if id_city >= 0 { if id_sta >= 0 { ok2 = 1 } } 41 gv_check("T2 probe words resolve in vocabulary", ok2, ctr) 42 43 // T3 the broader term (more contexts) weighs LESS than the narrower one -- both in-vocab, 44 // so this is a real comparison rather than an OOV artefact. 45 var ok3: i64 = 0 46 if ppl_idf_w(g, id_city) < ppl_idf_w(g, id_sta) { ok3 = 1 } 47 gv_check("T3 broader term weighs less than narrower term", ok3, ctr) 48 49 // T4 out-of-vocabulary weighs exactly nothing 50 var ok4: i64 = 0 51 if ppl_idf_w(g, 0 - 1) == 0 { ok4 = 1 } 52 gv_check("T4 OOV term weighs 0", ok4, ctr) 53 54 // ---- document containing "stadium" 55 let doc: *u8 = "the crowd filled the stadium" as *u8 56 var dl: i64 = 0 57 while doc[dl] != (0 as u8) { dl = dl + 1 } 58 let dids: *i64 = sys_mmap(64 * 8) as *i64 59 let nd: i64 = ppl_tokenize_ids(g, doc, dl, dids, 64) 60 61 // ---- insight = one in-vocab hit + one OOV term 62 let ins: *u8 = "stadium zzqqxxvv" as *u8 63 var il: i64 = 0 64 while ins[il] != (0 as u8) { il = il + 1 } 65 let iids: *i64 = sys_mmap(16 * 8) as *i64 66 let ni: i64 = ppl_tokenize_ids(g, ins, il, iids, 16) 67 68 let flat: i64 = ppl_maxsim(g, iids, ni, dids, nd) 69 let wtd: i64 = ppl_maxsim_idf(g, iids, ni, dids, nd) 70 71 // T5 the flat mean is dragged down by the OOV term; the weighted scorer excludes it 72 var ok5: i64 = 0 73 if wtd > flat { ok5 = 1 } 74 gv_check("T5 weighting excludes OOV instead of averaging it in", ok5, ctr) 75 76 // T6 an exact in-vocab hit still scores full under weighting 77 var ok6: i64 = 0 78 if wtd == 1000 { ok6 = 1 } 79 gv_check("T6 exact hit still scores 1000 when weighted", ok6, ctr) 80 81 // T7 NEG-CONTROL: an all-OOV insight scores 0 under BOTH scorers (no spurious credit) 82 let non: *u8 = "zzqqxxvv qqzzvvxx" as *u8 83 var nl: i64 = 0 84 while non[nl] != (0 as u8) { nl = nl + 1 } 85 let nids: *i64 = sys_mmap(16 * 8) as *i64 86 let nn: i64 = ppl_tokenize_ids(g, non, nl, nids, 16) 87 var ok7: i64 = 0 88 if ppl_maxsim(g, nids, nn, dids, nd) == 0 { if ppl_maxsim_idf(g, nids, nn, dids, nd) == 0 { ok7 = 1 } } 89 gv_check("T7 neg-control all-OOV insight scores 0 both ways", ok7, ctr) 90 91 // T8 identity: a term matched against itself scores full under weighting 92 let self1: *i64 = sys_mmap(8) as *i64 93 self1[0] = id_sta 94 var ok8: i64 = 0 95 if ppl_maxsim_idf(g, self1, 1, self1, 1) == 1000 { ok8 = 1 } 96 gv_check("T8 identity scores 1000 under weighting", ok8, ctr) 97 98 let rc: i64 = gv_verdict("PPMI-LIB", ctr, "shared loader + IDF weighting + OOV exclusion, neg-control") 99 sys_exit(rc) 100 return rc 101}