code wiki / _hdl_build / nx_dr_semjudge_gate.nx

nx_dr_semjudge_gate.nx source

↩ module page · 113 lines · 4727 B

1// nx_dr_semjudge_gate.nx -- KAT + neg-control for the SEMANTIC judge tier (DR-10). 2// Runs BOTH judges on the same input to prove the escalation is real: a source that says 3// "defeated" scores ZERO under the lexical judge for an insight saying "won", but scores 4// above zero under the PPMI semantic judge. That is the measured lift, on the real shipped 5// 41MB model -- not a fixture. Neg-controls: an out-of-vocabulary insight scores exactly 0 6// (no spurious match), and the known-measured synonym pairs must outrank the unrelated ones. 7// Runs from the nxc2 CWD so knowledge/index/semppmi_v1.bin resolves. DRY nx_gate_verdict lib. 8import "nx_dr_semjudge.nx" 9import "nx_dr_run.nx" 10import "nx_dr_verify.nx" 11import "nx_gate_verdict.nx" 12 13func main() -> i64 { 14 let ctr: *i64 = gv_ctr() 15 gv_head("nx_dr_semjudge -- PPMI semantic judge tier over the real shipped model (DR-10)") 16 17 let g: *i64 = sys_mmap(128 * 8) as *i64 18 let loaded: i64 = sj_load(g) 19 20 // T1 the real model loads with its known vocabulary 21 var ok1: i64 = 0 22 if loaded == 1 { if g[71] == 102318 { ok1 = 1 } } 23 gv_check("T1 real PPMI model loads vocab 102318", ok1, ctr) 24 25 // resolve the probe words through the PPMI vocab 26 let w: *i64 = sys_mmap(8 * 8) as *i64 27 let sw: *u8 = "won" as *u8 28 sj_tokenize_ids(g, sw, drr_strlen(sw), w, 1) 29 let id_won: i64 = w[0] 30 let sd: *u8 = "defeated" as *u8 31 sj_tokenize_ids(g, sd, drr_strlen(sd), w, 1) 32 let id_def: i64 = w[0] 33 let sp: *u8 = "purple" as *u8 34 sj_tokenize_ids(g, sp, drr_strlen(sp), w, 1) 35 let id_pur: i64 = w[0] 36 let sc: *u8 = "city" as *u8 37 sj_tokenize_ids(g, sc, drr_strlen(sc), w, 1) 38 let id_city: i64 = w[0] 39 let ss: *u8 = "stadium" as *u8 40 sj_tokenize_ids(g, ss, drr_strlen(ss), w, 1) 41 let id_sta: i64 = w[0] 42 let sj: *u8 = "january" as *u8 43 sj_tokenize_ids(g, sj, drr_strlen(sj), w, 1) 44 let id_jan: i64 = w[0] 45 46 // T2 identity 47 var ok2: i64 = 0 48 if sj_dcos(g, id_won, id_won) == 1000 { ok2 = 1 } 49 gv_check("T2 identity cosine = 1000", ok2, ctr) 50 51 // T3 measured synonym outranks measured unrelated 52 var ok3: i64 = 0 53 if sj_dcos(g, id_won, id_def) > sj_dcos(g, id_won, id_pur) { ok3 = 1 } 54 gv_check("T3 cos(won,defeated) > cos(won,purple)", ok3, ctr) 55 56 // T4 second measured pair 57 var ok4: i64 = 0 58 if sj_dcos(g, id_city, id_sta) > sj_dcos(g, id_city, id_jan) { ok4 = 1 } 59 gv_check("T4 cos(city,stadium) > cos(city,january)", ok4, ctr) 60 61 // ---- the same document judged BOTH ways 62 let doc: *u8 = "the team defeated their rivals at the stadium" as *u8 63 let dlen: i64 = drr_strlen(doc) 64 let ins: *u8 = "won" as *u8 65 let ilen: i64 = drr_strlen(ins) 66 67 // LEXICAL judge (djb2 token containment) 68 let lex_doc: *i64 = sys_mmap(64 * 8) as *i64 69 let nld: i64 = drr_tokenize(doc, dlen, lex_doc, 64) 70 let lex_ins: *i64 = sys_mmap(8 * 8) as *i64 71 let nli: i64 = drr_tokenize(ins, ilen, lex_ins, 8) 72 let lex_score: i64 = dv_entail(lex_ins, nli, lex_doc, nld) 73 74 // SEMANTIC judge (PPMI late-interaction maxsim) 75 let sem_doc: *i64 = sys_mmap(64 * 8) as *i64 76 let nsd: i64 = sj_tokenize_ids(g, doc, dlen, sem_doc, 64) 77 let sem_ins: *i64 = sys_mmap(8 * 8) as *i64 78 let nsi: i64 = sj_tokenize_ids(g, ins, ilen, sem_ins, 8) 79 let sem_score: i64 = sj_maxsim(g, sem_ins, nsi, sem_doc, nsd) 80 81 // T5 THE LIFT: lexical misses it entirely, semantic does not 82 var ok5: i64 = 0 83 if lex_score == 0 { if sem_score > 0 { ok5 = 1 } } 84 gv_check("T5 LIFT lexical=0 but semantic>0 on the same input", ok5, ctr) 85 86 // T6 NEG-CONTROL: an out-of-vocabulary insight scores exactly 0 (no spurious match) 87 let oov: *u8 = "zzqqxxvv" as *u8 88 let oov_ins: *i64 = sys_mmap(8 * 8) as *i64 89 let noi: i64 = sj_tokenize_ids(g, oov, drr_strlen(oov), oov_ins, 8) 90 let oov_score: i64 = sj_maxsim(g, oov_ins, noi, sem_doc, nsd) 91 var ok6: i64 = 0 92 if oov_score == 0 { ok6 = 1 } 93 gv_check("T6 neg-control OOV insight scores 0", ok6, ctr) 94 95 // T7 discrimination: the related insight outscores the nonsense one 96 var ok7: i64 = 0 97 if sem_score > oov_score { ok7 = 1 } 98 gv_check("T7 related insight outscores nonsense", ok7, ctr) 99 100 // T8 the semantic tokenizer reads the whole document 101 var ok8: i64 = 0 102 if nsd == 8 { ok8 = 1 } 103 gv_check("T8 semantic tokenizer count = 8", ok8, ctr) 104 105 // T9 an OOV word resolves to -1 (fail-safe, not a bogus id) 106 var ok9: i64 = 0 107 if oov_ins[0] == (0 - 1) { ok9 = 1 } 108 gv_check("T9 OOV word resolves to -1", ok9, ctr) 109 110 let rc: i64 = gv_verdict("DR-SEMJUDGE", ctr, "PPMI semantic judge on the real model; measured lift over lexical, OOV neg-control") 111 sys_exit(rc) 112 return rc 113}