code wiki / _hdl_build / nx_gmmlu_ingest.nx

nx_gmmlu_ingest.nx source

↩ module page · 225 lines · 9725 B

1// nx_gmmlu_ingest.nx -- F786 rung-2: parse the fetched Global-MMLU-Lite pages into a 2// CONTAMINATION-SPLIT battery, so an uncontaminated seat can sit a REAL public benchmark and 3// nx_battery_grade scores it straight into model-card cell b16 (Global-MMLU-Lite). 4// knowledge/store/gmmlutask- : sample_id<TAB>subject<TAB>question<TAB>opt_a<TAB>opt_b<TAB>opt_c<TAB>opt_d 5// knowledge/store/gmmlugold- : sample_id<TAB>answer (SEPARATE PLANE -- a seat reading the 6// task plane can NEVER see the answers; same guard as the F788 battery) 7// A seat then writes gmmluans- rows (sample_id<TAB>given-letter) and: 8// nx_battery_grade knowledge/store/gmmlugold- knowledge/store/gmmluans- knowledge/status/gmmlu_grade.log 9// 10// SCHEMA VERIFIED LIVE 2026-07-20 against datasets-server first-rows (CohereLabs/Global-MMLU-Lite, config 11// en, split test): sample_id / subject / subject_category / question / option_a..option_d / answer (answer 12// is a single letter A-D = exact-match gradeable). ⚠The dataset was RENAMED from CohereForAI/ -> CohereLabs/. 13// 14// Escape-aware JSON string-value end (terminating '"' = preceded by an EVEN run of backslashes) so option 15// and question text containing \" never mis-terminates a field; row search BOUNDED to [row,next-row) so a 16// malformed row cannot steal a sibling's fields. Values are stored JSON-ESCAPED VERBATIM, which is 17// TAB/NEWLINE-safe by construction (JSON escapes them as two-char \t/\n, never literal bytes). 18// TOOTH: task rows == gold rows AND > 0, else verdict=RED and NEITHER store is touched (fail-closed). 19// Truncations are COUNTED and REPORTED, never silent (round-12 law). 20// expect_exit: 0 (GREEN) | 1 RED license_tier: ORIGINAL No hw writes (Rule 26). 21import "nx_store_seed_lib.nx" 22import "nx_seg_store.nx" 23import "nx_syscalls.nx" 24 25static g_t: *u8 26static g_to: i64 27static g_g: *u8 28static g_go: i64 29static g_rows: i64 30static g_gold: i64 31static g_ve: i64 32static g_trunc: i64 33 34const GI_TASK: *u8 = "knowledge/store/gmmlutask-" as *u8 35const GI_GOLD: *u8 = "knowledge/store/gmmlugold-" as *u8 36const GI_LOG: *u8 = "knowledge/status/gmmlu_ingest.log" as *u8 37const GI_FCAP: i64 = 4194304 38const GI_OCAP: i64 = 1048576 39const GI_FIELDCAP: i64 = 16384 40const GI_SBCAP: i64 = 2048 41const GI_PATHCAP: i64 = 256 42const GI_TAB: i64 = 9 43const GI_NL: i64 = 10 44const GI_Q: i64 = 34 45const GI_BSL: i64 = 92 46const GI_STDERR: i64 = 2 47const GI_MODE: i64 = 0x1a4 48const GI_D0: i64 = 48 49const GI_B10: i64 = 10 50const GI_NUMB: i64 = 24 51const GI_EXIT_RED: i64 = 1 52 53func gi_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 54func gi_werr(s: *u8) -> i64 { sys_write(GI_STDERR, s, gi_slen(s)); return 0 } 55func gi_read(path: *u8, buf: *u8, cap: i64) -> i64 { 56 let fd: i64 = sys_openat_rd(path) 57 if fd < 0 { return 0 - 1 } 58 var n: i64 = 0 59 var go: i64 = 1 60 while go == 1 { 61 let base: i64 = buf as i64 62 let r: i64 = sys_read(fd, (base + n) as *u8, cap - n) 63 if r <= 0 { go = 0 } else { n = n + r } 64 if n >= cap { go = 0 } 65 } 66 sys_close(fd) 67 return n 68} 69func gi_find(buf: *u8, needle: *u8, from: i64, limit: i64) -> i64 { 70 let m: i64 = gi_slen(needle) 71 if m == 0 { return 0 - 1 } 72 var i: i64 = from 73 while i + m <= limit { 74 var j: i64 = 0 75 var ok: i64 = 1 76 while j < m { if buf[i+j] != needle[j] { ok = 0; j = m } else { j = j + 1 } } 77 if ok == 1 { if i > 0 { if buf[i-1] == (GI_BSL as u8) { ok = 0 } } } 78 if ok == 1 { return i } 79 i = i + 1 80 } 81 return 0 - 1 82} 83func gi_vend(buf: *u8, vs: i64, limit: i64) -> i64 { 84 var i: i64 = vs 85 while i < limit { 86 if buf[i] == (GI_Q as u8) { 87 var b: i64 = 0 88 var j: i64 = i - 1 89 var g: i64 = 1 90 while g == 1 { if j < vs { g = 0 } else { if buf[j] == (GI_BSL as u8) { b = b + 1; j = j - 1 } else { g = 0 } } } 91 if b % 2 == 0 { return i } 92 } 93 i = i + 1 94 } 95 return limit 96} 97func gi_field(buf: *u8, needle: *u8, from: i64, limit: i64) -> i64 { 98 let p: i64 = gi_find(buf, needle, from, limit) 99 if p < 0 { return 0 - 1 } 100 let vs: i64 = p + gi_slen(needle) 101 g_ve = gi_vend(buf, vs, limit) 102 return vs 103} 104// append a bounded slice to the TASK buffer 105func gi_t_slice(buf: *u8, a: i64, b: i64) -> i64 { 106 var e: i64 = b 107 if b - a > GI_FIELDCAP { e = a + GI_FIELDCAP; g_trunc = g_trunc + 1 } 108 var i: i64 = a 109 while i < e { if g_to < GI_OCAP - 2 { g_t[g_to] = buf[i]; g_to = g_to + 1 } i = i + 1 } 110 return 0 111} 112func gi_t_c(c: i64) -> i64 { if g_to < GI_OCAP - 2 { g_t[g_to] = c as u8; g_to = g_to + 1 } return 0 } 113func gi_g_slice(buf: *u8, a: i64, b: i64) -> i64 { 114 var i: i64 = a 115 while i < b { if g_go < GI_OCAP - 2 { g_g[g_go] = buf[i]; g_go = g_go + 1 } i = i + 1 } 116 return 0 117} 118func gi_g_c(c: i64) -> i64 { if g_go < GI_OCAP - 2 { g_g[g_go] = c as u8; g_go = g_go + 1 } return 0 } 119func gi_bcat(d: *u8, off: i64, s: *u8) -> i64 { 120 var o: i64 = off 121 var j: i64 = 0 122 while s[j] != (0 as u8) { d[o] = s[j]; o = o + 1; j = j + 1 } 123 return o 124} 125func gi_bcatn(d: *u8, off: i64, v: i64) -> i64 { 126 var o: i64 = off 127 var m: i64 = v 128 if m == 0 { d[o] = GI_D0 as u8; return o + 1 } 129 if m < 0 { m = 0 - m } 130 let t: *u8 = sys_mmap(GI_NUMB) 131 var k: i64 = 0 132 while m > 0 { t[k] = (GI_D0 + (m % GI_B10)) as u8; m = m / GI_B10; k = k + 1 } 133 var i: i64 = 0 134 while i < k { d[o] = t[k - 1 - i]; o = o + 1; i = i + 1 } 135 return o 136} 137// emit one task column from a field needle; returns 1 ok / 0 missing 138func gi_col(buf: *u8, needle: *u8, a: i64, limit: i64) -> i64 { 139 let vs: i64 = gi_field(buf, needle, a, limit) 140 if vs < 0 { return 0 } 141 let ve: i64 = g_ve 142 gi_t_c(GI_TAB) 143 gi_t_slice(buf, vs, ve) 144 return 1 145} 146func gi_scan(buf: *u8, n: i64) -> i64 { 147 var p: i64 = 0 148 var go: i64 = 1 149 while go == 1 { 150 let a: i64 = gi_find(buf, "\"row\":{\"sample_id\":\"" as *u8, p, n) 151 if a < 0 { go = 0 } else { 152 let nx: i64 = gi_find(buf, "\"row\":{\"sample_id\":\"" as *u8, a + 1, n) 153 var limit: i64 = n 154 if nx >= 0 { limit = nx } 155 let ivs: i64 = gi_field(buf, "\"sample_id\":\"" as *u8, a, limit) 156 let ive: i64 = g_ve 157 let avs: i64 = gi_field(buf, "\"answer\":\"" as *u8, a, limit) 158 let ave: i64 = g_ve 159 if ivs >= 0 { if avs >= 0 { 160 // TASK row: id + subject + question + 4 options (NO answer -- contamination split) 161 let mark: i64 = g_to 162 gi_t_slice(buf, ivs, ive) 163 var ok: i64 = 1 164 if gi_col(buf, "\"subject\":\"" as *u8, a, limit) == 0 { ok = 0 } 165 if gi_col(buf, "\"question\":\"" as *u8, a, limit) == 0 { ok = 0 } 166 if gi_col(buf, "\"option_a\":\"" as *u8, a, limit) == 0 { ok = 0 } 167 if gi_col(buf, "\"option_b\":\"" as *u8, a, limit) == 0 { ok = 0 } 168 if gi_col(buf, "\"option_c\":\"" as *u8, a, limit) == 0 { ok = 0 } 169 if gi_col(buf, "\"option_d\":\"" as *u8, a, limit) == 0 { ok = 0 } 170 if ok == 1 { 171 gi_t_c(GI_NL) 172 g_rows = g_rows + 1 173 // GOLD row: id + answer letter 174 gi_g_slice(buf, ivs, ive) 175 gi_g_c(GI_TAB) 176 gi_g_slice(buf, avs, ave) 177 gi_g_c(GI_NL) 178 g_gold = g_gold + 1 179 } else { g_to = mark } 180 } } 181 if nx < 0 { go = 0 } else { p = nx } 182 } 183 } 184 return 0 185} 186 187func main() -> i64 { 188 g_t = sys_mmap(GI_OCAP) 189 g_g = sys_mmap(GI_OCAP) 190 g_to = 0 191 g_go = 0 192 g_rows = 0 193 g_gold = 0 194 g_trunc = 0 195 let fbuf: *u8 = sys_mmap(GI_FCAP) 196 let n: i64 = gi_read("knowledge/fetched/gmmlu_rows_000.raw" as *u8, fbuf, GI_FCAP) 197 if n <= 0 { gi_werr("GMMLU-INGEST FAIL verdict=RED page absent (run nx_gmmlu_research_fetch first)\n" as *u8); sys_exit(GI_EXIT_RED); return GI_EXIT_RED } 198 gi_scan(fbuf, n) 199 if g_rows <= 0 { gi_werr("GMMLU-INGEST FAIL verdict=RED rows=0 (anchor/schema drift?) stores untouched\n" as *u8); sys_exit(GI_EXIT_RED); return GI_EXIT_RED } 200 if g_rows != g_gold { gi_werr("GMMLU-INGEST FAIL verdict=RED task/gold count mismatch, stores untouched\n" as *u8); sys_exit(GI_EXIT_RED); return GI_EXIT_RED } 201 if sts_seed(GI_TASK, g_t, g_to) < 0 { gi_werr("GMMLU-INGEST FAIL verdict=RED task-store commit\n" as *u8); sys_exit(GI_EXIT_RED); return GI_EXIT_RED } 202 if sts_seed(GI_GOLD, g_g, g_go) < 0 { gi_werr("GMMLU-INGEST FAIL verdict=RED gold-store commit\n" as *u8); sys_exit(GI_EXIT_RED); return GI_EXIT_RED } 203 204 let sb: *u8 = sys_mmap(GI_SBCAP) 205 var o: i64 = gi_bcat(sb, 0, "GMMLU-INGEST OK verdict=GREEN questions=" as *u8) 206 o = gi_bcatn(sb, o, g_rows) 207 o = gi_bcat(sb, o, " gold=" as *u8) 208 o = gi_bcatn(sb, o, g_gold) 209 o = gi_bcat(sb, o, " field_truncated=" as *u8) 210 o = gi_bcatn(sb, o, g_trunc) 211 o = gi_bcat(sb, o, " task_bytes=" as *u8) 212 o = gi_bcatn(sb, o, g_to) 213 o = gi_bcat(sb, o, " gold_bytes=" as *u8) 214 o = gi_bcatn(sb, o, g_go) 215 o = gi_bcat(sb, o, " (Global-MMLU-Lite en/test, CohereLabs; task plane carries NO answers - a seat reading it cannot self-grade; sit it, write gmmluans-, then nx_battery_grade -> card b16)\n" as *u8) 216 let tp: *u8 = sys_mmap(GI_PATHCAP) 217 var to: i64 = gi_bcat(tp, 0, GI_LOG) 218 to = gi_bcat(tp, to, ".nxtmp" as *u8) 219 tp[to] = 0 as u8 220 let fd: i64 = sys_openat_wr(tp, GI_MODE) 221 if fd >= 0 { sys_write(fd, sb, o); sys_close(fd); sys_renameat(tp, GI_LOG) } 222 sys_write(GI_STDERR, sb, o) 223 sys_exit(0) 224 return 0 225}