code wiki / _hdl_build / _t4_first_model_authored.nx

_t4_first_model_authored.nx source

↩ module page · 611 lines · 27963 B

1// _t4_first_model_authored.nx -- T4 of the training-substrate ladder: the FIRST REAL MODEL. 2// Composes the GATED T6/T5 core (nx_tgrad_core.nx, extracted verbatim from _tensor_grad_authored.nx) 3// into two trained models, all bits-up sovereign f32, zero randomness: 4// LANE 1 (nonconvex proof): XOR via a 2-4-1 relu MLP + AdamW, deterministic symmetry-breaking 5// init (the zero-init relu trap documented at T6 is closed by a FIXED pattern table, not RNG -- 6// gate-proven here; the principled-init research row stays open in the roadmap). 7// LANE 2 (REAL TEAM DATA): 8-way service-class model on the ACTUAL elder census 8// (knowledge/status/elder_census.log) -- first 6 name bytes -> {svc,gen,data,orch,worker,mon,util,other}. 9// PARSE ORACLE: the census file carries its own ground truth (CENSUS-SUM per-class counts); 10// the parsed+deduped dataset must match it EXACTLY before training is even attempted. 11// GATES: O census-parse==CENSUS-SUM oracle | G XOR learned (loss<1/100, 4/4 preds within 1/4) 12// | H census train accuracy == services (100%, per-class pred counts == ground truth) 13// | I BIT-EXACT two-run training, BOTH lanes (the standing EXCEED axis). 14// Evidence: loss curve + verdicts appended to knowledge/status/modelwright.log (MODELWRIGHT rows 15// = the role's first durable evidence; the scorecard reads this file). 16// LAWS: struct-free, flat ifs, no &&/||, no unary-minus literals, no randomness. license_tier: ORIGINAL 17import "nx_tgrad_core.nx" 18 19const T4_XOR_EPOCHS: i64 = 1500 20const T4_CEN_EPOCHS: i64 = 1500 21const T4_LOG_EVERY: i64 = 100 22const T4_HID: i64 = 24 23const T4_INB: i64 = 8 24const T4_NCLS: i64 = 8 25const T4_MAXSVC: i64 = 200 26 27// write s / decimal v to an fd (durable log helpers; _tg_puts/_tg_num from the core cover stdout) 28func t4_fp(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 29func t4_fn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(fd,"-" as *u8,1)}; let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48;k=1}; while m>0{t[k]=48+(m%10);m=m/10;k=k+1}; var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1}; sys_write(fd,bb,k); return 0 } 30// both stdout and the durable log 31func t4_bp(fd: i64, s: *u8) -> i64 { _tg_puts(s); t4_fp(fd, s); return 0 } 32func t4_bn(fd: i64, v: i64) -> i64 { _tg_num(v); t4_fn(fd, v); return 0 } 33 34// deterministic symmetry-breaking init: p[i] = (((salt + i*7) mod 13) - 6) / den 35// fixed pattern, varied signs/magnitudes, same bits every run by construction 36func t4_init(p: *i64, n: i64, salt: i64, den: i64) -> i64 { 37 var i: i64 = 0 38 while i < n { 39 let m: i64 = ((salt + i*7) % 13) - 6 40 if m < 0 { p[i] = nx_f32_neg(tg_q(0 - m, den)) } else { p[i] = tg_q(m, den) } 41 i = i + 1 42 } 43 return 0 44} 45 46// does buffer b at i match literal lit (lit NUL-terminated, b bounded by n)? 47func t4_match(b: *u8, i: i64, n: i64, lit: *u8) -> i64 { 48 var k: i64 = 0 49 while lit[k] != (0 as u8) { 50 if i + k >= n { return 0 } 51 if b[i+k] != lit[k] { return 0 } 52 k = k + 1 53 } 54 return 1 55} 56// token at [i, le) equals lit exactly (delimiter = space or end-of-line)? 57func t4_tokeq(b: *u8, i: i64, le: i64, lit: *u8) -> i64 { 58 var k: i64 = 0 59 while lit[k] != (0 as u8) { 60 if i + k >= le { return 0 } 61 if b[i+k] != lit[k] { return 0 } 62 k = k + 1 63 } 64 if i + k == le { return 1 } 65 if b[i+k] == (32 as u8) { return 1 } 66 return 0 67} 68// parse the unsigned int right after key= inside [ls, le); 0-1 if absent 69func t4_keynum(b: *u8, ls: i64, le: i64, key: *u8) -> i64 { 70 var i: i64 = ls 71 while i < le { 72 if t4_match(b, i, le, key) == 1 { 73 var k: i64 = 0 74 while key[k] != (0 as u8) { k = k + 1 } 75 var j: i64 = i + k 76 var v: i64 = 0 77 var got: i64 = 0 78 while j < le { 79 let c: i64 = b[j] as i64 80 if c < 48 { j = le } else { if c > 57 { j = le } else { v = v*10 + (c - 48); got = 1; j = j + 1 } } 81 } 82 if got == 1 { return v } 83 } 84 i = i + 1 85 } 86 return 0 - 1 87} 88func t4_classid(b: *u8, i: i64, le: i64) -> i64 { 89 if t4_tokeq(b, i, le, "svc" as *u8) == 1 { return 0 } 90 if t4_tokeq(b, i, le, "gen" as *u8) == 1 { return 1 } 91 if t4_tokeq(b, i, le, "data" as *u8) == 1 { return 2 } 92 if t4_tokeq(b, i, le, "orch" as *u8) == 1 { return 3 } 93 if t4_tokeq(b, i, le, "worker" as *u8) == 1 { return 4 } 94 if t4_tokeq(b, i, le, "mon" as *u8) == 1 { return 5 } 95 if t4_tokeq(b, i, le, "util" as *u8) == 1 { return 6 } 96 if t4_tokeq(b, i, le, "other" as *u8) == 1 { return 7 } 97 return 0 - 1 98} 99func t4_nameq(a: *u8, b: *u8) -> i64 { 100 var i: i64 = 0 101 while a[i] != (0 as u8) { 102 if a[i] != b[i] { return 0 } 103 i = i + 1 104 } 105 if b[i] == (0 as u8) { return 1 } 106 return 0 107} 108 109// parse census: dedup by name; names = T4_MAXSVC x 24 bytes; labels per unique svc. 110// sums = 9 cells: per-class ground truth from the LAST CENSUS-SUM line + [8]=services. 111// returns unique count; 0-1 on read failure 112func t4_parse(cpath: *u8, names: *u8, labels: *i64, sums: *i64) -> i64 { 113 let lenp: *i64 = sys_mmap(16) as *i64 114 let b: *u8 = sys_read_file(cpath, lenp) 115 let n: i64 = lenp[0] 116 if n <= 0 { return 0 - 1 } 117 var cnt: i64 = 0 118 var ls: i64 = 0 119 while ls < n { 120 var le: i64 = ls 121 var stop: i64 = 0 122 while stop == 0 { 123 if le >= n { stop = 1 } else { if b[le] == (10 as u8) { stop = 1 } else { le = le + 1 } } 124 } 125 let realle: i64 = le 126 if t4_match(b, ls, realle, "CENSUS-SVC name=" as *u8) == 1 { 127 let ni: i64 = ls + 16 128 var ne: i64 = ni 129 var stop2: i64 = 0 130 while stop2 == 0 { 131 if ne >= realle { stop2 = 1 } else { if b[ne] == (32 as u8) { stop2 = 1 } else { ne = ne + 1 } } 132 } 133 var ci: i64 = 0 - 1 134 var j: i64 = ni 135 while j < realle { 136 if t4_match(b, j, realle, "class=" as *u8) == 1 { ci = t4_classid(b, j + 6, realle); j = realle } else { j = j + 1 } 137 } 138 var nlen: i64 = ne - ni 139 if nlen > 23 { nlen = 23 } 140 let tmp: *u8 = sys_mmap(32) 141 var k: i64 = 0 142 while k < nlen { tmp[k] = b[ni+k]; k = k + 1 } 143 tmp[nlen] = 0 as u8 144 if ci >= 0 { 145 var seen: i64 = 0 146 var s: i64 = 0 147 while s < cnt { 148 let np: *u8 = (names as i64 + s*24) as *u8 149 if t4_nameq(tmp, np) == 1 { seen = 1; s = cnt } else { s = s + 1 } 150 } 151 if seen == 0 { 152 if cnt < T4_MAXSVC { 153 let dst: *u8 = (names as i64 + cnt*24) as *u8 154 k = 0 155 while k <= nlen { dst[k] = tmp[k]; k = k + 1 } 156 labels[cnt] = ci 157 cnt = cnt + 1 158 } 159 } 160 } 161 } 162 if t4_match(b, ls, realle, "CENSUS-SUM " as *u8) == 1 { 163 sums[0] = t4_keynum(b, ls, realle, "svc=" as *u8) 164 sums[1] = t4_keynum(b, ls, realle, "gen=" as *u8) 165 sums[2] = t4_keynum(b, ls, realle, "data=" as *u8) 166 sums[3] = t4_keynum(b, ls, realle, "orch=" as *u8) 167 sums[4] = t4_keynum(b, ls, realle, "worker=" as *u8) 168 sums[5] = t4_keynum(b, ls, realle, "mon=" as *u8) 169 sums[6] = t4_keynum(b, ls, realle, "util=" as *u8) 170 sums[7] = t4_keynum(b, ls, realle, "other=" as *u8) 171 sums[8] = t4_keynum(b, ls, realle, "services=" as *u8) 172 } 173 ls = le + 1 174 } 175 return cnt 176} 177 178// features: first T4_INB name bytes CENTERED, (byte-84)/32 (~[-1.2,1.2] for ASCII names; 179// pad past NUL = the same transform of 0 = a distinctive length signal). Centering matters: 180// the first build used raw byte/64 (all-positive inputs) and plateaued -- positive-orthant 181// features kill relu units dead and the loss froze at 43-84 milli across THREE optimizers. 182// targets: one-hot over T4_NCLS 183func t4_featurize(names: *u8, labels: *i64, cnt: i64, xs: *i64, ts: *i64) -> i64 { 184 var s: i64 = 0 185 while s < cnt { 186 let np: *u8 = (names as i64 + s*24) as *u8 187 var j: i64 = 0 188 var done: i64 = 0 189 while j < T4_INB { 190 var byv: i64 = 0 191 if done == 0 { 192 byv = np[j] as i64 193 if byv == 0 { done = 1; byv = 0 } 194 } 195 let m: i64 = byv - 84 196 if m < 0 { xs[s*T4_INB + j] = nx_f32_neg(tg_q(0 - m, 32)) } else { xs[s*T4_INB + j] = tg_q(m, 32) } 197 j = j + 1 198 } 199 var c: i64 = 0 200 while c < T4_NCLS { 201 if c == labels[s] { ts[s*T4_NCLS + c] = nx_i32_to_f32(1) } else { ts[s*T4_NCLS + c] = 0 } 202 c = c + 1 203 } 204 s = s + 1 205 } 206 return 0 207} 208 209// census weight layout offsets (one source of truth): w1[0..w1n) b1[b1o..w2o) w2[w2o..b2o) b2[b2o..tot) 210func t4_w1n() -> i64 { return T4_HID * T4_INB } 211func t4_b1o() -> i64 { return t4_w1n() } 212func t4_w2o() -> i64 { return t4_w1n() + T4_HID } 213func t4_b2o() -> i64 { return t4_w2o() + T4_NCLS * T4_HID } 214func t4_tot() -> i64 { return t4_b2o() + T4_NCLS } 215 216// census model plain forward into out8; returns argmax class 217func t4_cen_fwd(w: *i64, x: *i64, out8: *i64) -> i64 { 218 let h: *i64 = sys_mmap(256) as *i64 219 var i: i64 = 0 220 while i < T4_HID { 221 var s: i64 = w[t4_b1o() + i] 222 var j: i64 = 0 223 while j < T4_INB { s = nx_f32_add(s, nx_f32_mul(w[i*T4_INB + j], x[j])); j = j + 1 } 224 if nx_f32_lt(s, 0) == 1 { s = 0 } 225 h[i] = s 226 i = i + 1 227 } 228 var c: i64 = 0 229 var best: i64 = 0 230 while c < T4_NCLS { 231 var o: i64 = w[t4_b2o() + c] 232 i = 0 233 while i < T4_HID { o = nx_f32_add(o, nx_f32_mul(w[t4_w2o() + c*T4_HID + i], h[i])); i = i + 1 } 234 out8[c] = o 235 if c > 0 { if nx_f32_gt(o, out8[best]) == 1 { best = c } } 236 c = c + 1 237 } 238 return best 239} 240 241// train the census MLP (layout from t4_w1n/b1o/w2o/b2o): 242// FULL-BATCH gradient accumulation (per-sample tapes, grads summed over all services) + ONE 243// gated ad_step per epoch, with INVERSE-FREQUENCY CLASS WEIGHTS (wts[s] = (1/NCLS)/hist[label_s], 244// computed from the parsed histogram -- data-driven, no magic numbers). Lessons banked from 245// MEASURED failures: per-sample SGD plateaued 141/149; per-sample Adam collapsed to the 246// 88-sample majority (88/149); unweighted full-batch Adam left EXACTLY the two rarest classes 247// wrong (all 5 util + both mon = 142/149) -- on 88:2 imbalance the minority gradient only 248// survives inside a CLASS-BALANCED full-batch sum. 249func t4_cen_train(tape: *i64, nb: *i64, arena: *i64, ab: *i64, xs: *i64, ts: *i64, wts: *i64, cnt: i64, w: *i64, logfd: i64, out2: *i64, curve: *i64, curven: *i64) -> i64 { 250 let w1n: i64 = t4_w1n() 251 let w2n: i64 = T4_NCLS * T4_HID 252 t4_init(w, w1n, 1, 16) 253 var z: i64 = t4_b1o() 254 while z < t4_w2o() { w[z] = 0; z = z + 1 } 255 t4_init((w as i64 + t4_w2o()*8) as *i64, w2n, 2, 16) 256 z = t4_b2o() 257 while z < t4_tot() { w[z] = 0; z = z + 1 } 258 let lr: i64 = tg_q(1, 20) 259 let b1c: i64 = tg_q(9, 10) 260 let b2c: i64 = tg_q(999, 1000) 261 let eps: i64 = tg_q(1, 100000) 262 let mw1: *i64 = sys_mmap(4096) as *i64 263 let vw1: *i64 = sys_mmap(4096) as *i64 264 let mb1: *i64 = sys_mmap(1024) as *i64 265 let vb1: *i64 = sys_mmap(1024) as *i64 266 let mw2: *i64 = sys_mmap(4096) as *i64 267 let vw2: *i64 = sys_mmap(4096) as *i64 268 let mb2: *i64 = sys_mmap(1024) as *i64 269 let vb2: *i64 = sys_mmap(1024) as *i64 270 let gaw1: *i64 = sys_mmap(4096) as *i64 271 let gab1: *i64 = sys_mmap(1024) as *i64 272 let gaw2: *i64 = sys_mmap(4096) as *i64 273 let gab2: *i64 = sys_mmap(1024) as *i64 274 let invn: i64 = nx_f32_div(nx_i32_to_f32(1), nx_i32_to_f32(cnt)) 275 curven[0] = 0 276 curven[1] = 0 - 1 277 var ep: i64 = 0 278 while ep < T4_CEN_EPOCHS { 279 var epsum: i64 = 0 280 var z2: i64 = 0 281 while z2 < w1n { gaw1[z2] = 0; z2 = z2 + 1 } 282 z2 = 0 283 while z2 < T4_HID { gab1[z2] = 0; z2 = z2 + 1 } 284 z2 = 0 285 while z2 < w2n { gaw2[z2] = 0; z2 = z2 + 1 } 286 z2 = 0 287 while z2 < T4_NCLS { gab2[z2] = 0; z2 = z2 + 1 } 288 var s: i64 = 0 289 while s < cnt { 290 nb[0] = 0 291 ab[0] = 0 292 let lw1: i64 = tg_leaf(tape, nb, arena, ab, w, T4_HID, T4_INB) 293 let lb1: i64 = tg_leaf(tape, nb, arena, ab, (w as i64 + t4_b1o()*8) as *i64, T4_HID, 1) 294 let lw2: i64 = tg_leaf(tape, nb, arena, ab, (w as i64 + t4_w2o()*8) as *i64, T4_NCLS, T4_HID) 295 let lb2: i64 = tg_leaf(tape, nb, arena, ab, (w as i64 + t4_b2o()*8) as *i64, T4_NCLS, 1) 296 let lx: i64 = tg_leaf(tape, nb, arena, ab, (xs as i64 + s*T4_INB*8) as *i64, T4_INB, 1) 297 let lt: i64 = tg_leaf(tape, nb, arena, ab, (ts as i64 + s*T4_NCLS*8) as *i64, T4_NCLS, 1) 298 let hh: i64 = tg_reluvec(tape, nb, arena, ab, tg_addvec(tape, nb, arena, ab, tg_matvec(tape, nb, arena, ab, lw1, lx), lb1)) 299 let oo: i64 = tg_addvec(tape, nb, arena, ab, tg_matvec(tape, nb, arena, ab, lw2, hh), lb2) 300 let lwt: i64 = tg_leaf(tape, nb, arena, ab, (wts as i64 + s*8) as *i64, 1, 1) 301 let loss: i64 = tg_smul(tape, nb, arena, ab, tg_mse(tape, nb, arena, ab, oo, lt), lwt) 302 tg_backward(tape, nb[0], loss) 303 let lv: *i64 = tg_valp(tape, loss) 304 epsum = nx_f32_add(epsum, lv[0]) 305 let pg1: *i64 = tg_gradp(tape, lw1) 306 var q: i64 = 0 307 while q < w1n { gaw1[q] = nx_f32_add(gaw1[q], pg1[q]); q = q + 1 } 308 let pb1: *i64 = tg_gradp(tape, lb1) 309 q = 0 310 while q < T4_HID { gab1[q] = nx_f32_add(gab1[q], pb1[q]); q = q + 1 } 311 let pg2: *i64 = tg_gradp(tape, lw2) 312 q = 0 313 while q < w2n { gaw2[q] = nx_f32_add(gaw2[q], pg2[q]); q = q + 1 } 314 let pb2: *i64 = tg_gradp(tape, lb2) 315 q = 0 316 while q < T4_NCLS { gab2[q] = nx_f32_add(gab2[q], pb2[q]); q = q + 1 } 317 s = s + 1 318 } 319 var lrcur: i64 = lr 320 if ep >= (T4_CEN_EPOCHS * 4) / 5 { lrcur = nx_f32_div(lr, nx_i32_to_f32(8)) } 321 ad_step(w, gaw1, mw1, vw1, w1n, lrcur, b1c, b2c, eps, 0, ep + 1) 322 ad_step((w as i64 + t4_b1o()*8) as *i64, gab1, mb1, vb1, T4_HID, lrcur, b1c, b2c, eps, 0, ep + 1) 323 ad_step((w as i64 + t4_w2o()*8) as *i64, gaw2, mw2, vw2, w2n, lrcur, b1c, b2c, eps, 0, ep + 1) 324 ad_step((w as i64 + t4_b2o()*8) as *i64, gab2, mb2, vb2, T4_NCLS, lrcur, b1c, b2c, eps, 0, ep + 1) 325 let avg: i64 = nx_f32_mul(epsum, invn) 326 if ep == 0 { out2[0] = avg } 327 out2[1] = avg 328 let micro: i64 = tg_milli(nx_f32_mul(avg, nx_i32_to_f32(1000))) 329 if ep % T4_LOG_EVERY == 0 { 330 if curven[0] < 120 { 331 curve[curven[0]*2+0] = ep 332 curve[curven[0]*2+1] = micro 333 curven[0] = curven[0] + 1 334 } 335 if logfd >= 0 { 336 t4_bp(logfd, "MODELWRIGHT T4 LOSSCURVE ep=" as *u8); t4_bn(logfd, ep) 337 t4_bp(logfd, " loss_micro=" as *u8); t4_bn(logfd, micro); t4_bp(logfd, "\n" as *u8) 338 } 339 } 340 // EARLY STOP at convergence (deterministic: stop epoch is a pure function of the losses). 341 // Measured failure this guards: at constant lr Adam CONVERGED at ~ep600 (loss ~0) then 342 // BLEW UP at ep800 (grads->0 => v->0 => update ~ mh/eps spikes) and landed in a worse basin. 343 if nx_f32_lt(avg, tg_q(1, 250000)) == 1 { 344 if curven[0] < 120 { 345 curve[curven[0]*2+0] = ep 346 curve[curven[0]*2+1] = micro 347 curven[0] = curven[0] + 1 348 } 349 curven[1] = ep 350 if logfd >= 0 { t4_bp(logfd, "MODELWRIGHT T4 CONVERGED ep=" as *u8); t4_bn(logfd, ep); t4_bp(logfd, "\n" as *u8) } 351 ep = T4_CEN_EPOCHS 352 } else { ep = ep + 1 } 353 } 354 return 0 355} 356 357// XOR model plain forward (layout: w1[0..15] b1[16..23] w2[24..31] b2[32]) 358func t4_xor_fwd(w: *i64, x0: i64, x1: i64) -> i64 { 359 let h: *i64 = sys_mmap(128) as *i64 360 var i: i64 = 0 361 while i < 8 { 362 var s: i64 = w[16+i] 363 s = nx_f32_add(s, nx_f32_mul(w[i*2+0], x0)) 364 s = nx_f32_add(s, nx_f32_mul(w[i*2+1], x1)) 365 if nx_f32_lt(s, 0) == 1 { s = 0 } 366 h[i] = s 367 i = i + 1 368 } 369 var o: i64 = w[32] 370 i = 0 371 while i < 8 { o = nx_f32_add(o, nx_f32_mul(w[24+i], h[i])); i = i + 1 } 372 return o 373} 374 375// train XOR 2-8-1 with AdamW full-batch; out2 = {first, final} loss 376func t4_xor_train(tape: *i64, nb: *i64, arena: *i64, ab: *i64, w: *i64, out2: *i64) -> i64 { 377 t4_init(w, 16, 3, 8) 378 var z: i64 = 16 379 while z < 24 { w[z] = 0; z = z + 1 } 380 t4_init((w as i64 + 24*8) as *i64, 8, 4, 8) 381 w[32] = 0 382 let xs: *i64 = sys_mmap(128) as *i64 383 let ys: *i64 = sys_mmap(64) as *i64 384 xs[0] = 0; xs[1] = 0; ys[0] = 0 385 xs[2] = 0; xs[3] = nx_i32_to_f32(1); ys[1] = nx_i32_to_f32(1) 386 xs[4] = nx_i32_to_f32(1); xs[5] = 0; ys[2] = nx_i32_to_f32(1) 387 xs[6] = nx_i32_to_f32(1); xs[7] = nx_i32_to_f32(1); ys[3] = 0 388 let inv4: *i64 = sys_mmap(64) as *i64 389 inv4[0] = tg_q(1, 4) 390 let lr: i64 = tg_q(1, 20) 391 let b1c: i64 = tg_q(9, 10) 392 let b2c: i64 = tg_q(999, 1000) 393 let eps: i64 = tg_q(1, 100000) 394 let mw1: *i64 = sys_mmap(128) as *i64 395 let vw1: *i64 = sys_mmap(128) as *i64 396 let mb1: *i64 = sys_mmap(64) as *i64 397 let vb1: *i64 = sys_mmap(64) as *i64 398 let mw2: *i64 = sys_mmap(64) as *i64 399 let vw2: *i64 = sys_mmap(64) as *i64 400 let mb2: *i64 = sys_mmap(32) as *i64 401 let vb2: *i64 = sys_mmap(32) as *i64 402 var ep: i64 = 0 403 while ep < T4_XOR_EPOCHS { 404 nb[0] = 0 405 ab[0] = 0 406 let lw1: i64 = tg_leaf(tape, nb, arena, ab, w, 8, 2) 407 let lb1: i64 = tg_leaf(tape, nb, arena, ab, (w as i64 + 16*8) as *i64, 8, 1) 408 let lw2: i64 = tg_leaf(tape, nb, arena, ab, (w as i64 + 24*8) as *i64, 1, 8) 409 let lb2: i64 = tg_leaf(tape, nb, arena, ab, (w as i64 + 32*8) as *i64, 1, 1) 410 var accn: i64 = 0 - 1 411 var k: i64 = 0 412 while k < 4 { 413 let lx: i64 = tg_leaf(tape, nb, arena, ab, (xs as i64 + k*16) as *i64, 2, 1) 414 let lt: i64 = tg_leaf(tape, nb, arena, ab, (ys as i64 + k*8) as *i64, 1, 1) 415 let hh: i64 = tg_reluvec(tape, nb, arena, ab, tg_addvec(tape, nb, arena, ab, tg_matvec(tape, nb, arena, ab, lw1, lx), lb1)) 416 let oo: i64 = tg_addvec(tape, nb, arena, ab, tg_matvec(tape, nb, arena, ab, lw2, hh), lb2) 417 let m: i64 = tg_mse(tape, nb, arena, ab, oo, lt) 418 if accn < 0 { accn = m } else { accn = tg_addvec(tape, nb, arena, ab, accn, m) } 419 k = k + 1 420 } 421 let li: i64 = tg_leaf(tape, nb, arena, ab, inv4, 1, 1) 422 let loss: i64 = tg_smul(tape, nb, arena, ab, accn, li) 423 tg_backward(tape, nb[0], loss) 424 let lv: *i64 = tg_valp(tape, loss) 425 if ep == 0 { out2[0] = lv[0] } 426 out2[1] = lv[0] 427 ad_step(w, tg_gradp(tape, lw1), mw1, vw1, 16, lr, b1c, b2c, eps, 0, ep + 1) 428 ad_step((w as i64 + 16*8) as *i64, tg_gradp(tape, lb1), mb1, vb1, 8, lr, b1c, b2c, eps, 0, ep + 1) 429 ad_step((w as i64 + 24*8) as *i64, tg_gradp(tape, lw2), mw2, vw2, 8, lr, b1c, b2c, eps, 0, ep + 1) 430 ad_step((w as i64 + 32*8) as *i64, tg_gradp(tape, lb2), mb2, vb2, 1, lr, b1c, b2c, eps, 0, ep + 1) 431 ep = ep + 1 432 } 433 return 0 434} 435 436func main(argc: i64, argv: *i64) -> i64 { 437 _tg_puts("=== T4 FIRST REAL MODEL (composes the GATED T6/T5 core; XOR nonconvex + REAL census corpus; bit-exact) ===\n" as *u8) 438 // optional argv[1] = census path override -- the Referee's tamper hook (prove the gate goes RED) 439 var cpath: *u8 = "knowledge/status/elder_census.log" as *u8 440 if argc >= 2 { cpath = argv[1] as *u8 } 441 let lfd: i64 = sys_openat_append("knowledge/status/modelwright.log" as *u8, 0x1a4) 442 if lfd < 0 { _tg_puts(" modelwright log open FAILED\n" as *u8); sys_exit(1); return 1 } 443 t4_fp(lfd, "MODELWRIGHT T4 RUN epoch_unix=" as *u8); t4_fn(lfd, sys_now_realtime_sec()); t4_fp(lfd, "\n" as *u8) 444 445 let tape: *i64 = sys_mmap(32768) as *i64 446 let nb: *i64 = sys_mmap(16) as *i64 447 let arena: *i64 = sys_mmap(131072) as *i64 448 let ab: *i64 = sys_mmap(16) as *i64 449 450 // ---- GATE O: parse the REAL census; the file's own CENSUS-SUM is the oracle ---- 451 let names: *u8 = sys_mmap(8192) 452 let labels: *i64 = sys_mmap(2048) as *i64 453 let sums: *i64 = sys_mmap(128) as *i64 454 var si: i64 = 0 455 while si < 9 { sums[si] = 0 - 1; si = si + 1 } 456 let cnt: i64 = t4_parse(cpath, names, labels, sums) 457 var po: i64 = 1 458 if cnt <= 0 { po = 0 } 459 if sums[8] != cnt { po = 0 } 460 let hist: *i64 = sys_mmap(128) as *i64 461 var s2: i64 = 0 462 while s2 < cnt { hist[labels[s2]] = hist[labels[s2]] + 1; s2 = s2 + 1 } 463 var c2: i64 = 0 464 while c2 < T4_NCLS { 465 if hist[c2] != sums[c2] { po = 0 } 466 c2 = c2 + 1 467 } 468 t4_bp(lfd, "MODELWRIGHT T4 DATA services=" as *u8); t4_bn(lfd, cnt) 469 t4_bp(lfd, " svc=" as *u8); t4_bn(lfd, hist[0]) 470 t4_bp(lfd, " gen=" as *u8); t4_bn(lfd, hist[1]) 471 t4_bp(lfd, " data=" as *u8); t4_bn(lfd, hist[2]) 472 t4_bp(lfd, " orch=" as *u8); t4_bn(lfd, hist[3]) 473 t4_bp(lfd, " worker=" as *u8); t4_bn(lfd, hist[4]) 474 t4_bp(lfd, " mon=" as *u8); t4_bn(lfd, hist[5]) 475 t4_bp(lfd, " util=" as *u8); t4_bn(lfd, hist[6]) 476 t4_bp(lfd, " other=" as *u8); t4_bn(lfd, hist[7]) 477 if po == 1 { t4_bp(lfd, " sum_oracle=MATCH\n" as *u8) } else { t4_bp(lfd, " sum_oracle=MISMATCH\n" as *u8) } 478 if po == 1 { _tg_puts(" GATE O census parse == CENSUS-SUM ground truth: PASS\n" as *u8) } else { _tg_puts(" GATE O census-parse oracle: FAIL\n" as *u8) } 479 480 // ---- GATE G: XOR (the nonconvex proof; deterministic init kills the zero-init trap) ---- 481 let wx1: *i64 = sys_mmap(256) as *i64 482 let ox1: *i64 = sys_mmap(64) as *i64 483 t4_xor_train(tape, nb, arena, ab, wx1, ox1) 484 var pg: i64 = 1 485 if nx_f32_lt(ox1[1], tg_q(1, 100)) == 0 { pg = 0 } 486 if nx_f32_lt(ox1[1], ox1[0]) == 0 { pg = 0 } 487 let one: i64 = nx_i32_to_f32(1) 488 var okp: i64 = 0 489 var k: i64 = 0 490 while k < 4 { 491 var xa: i64 = 0 492 var xb: i64 = 0 493 var tv: i64 = 0 494 if k == 1 { xb = one; tv = one } 495 if k == 2 { xa = one; tv = one } 496 if k == 3 { xa = one; xb = one } 497 let pr: i64 = t4_xor_fwd(wx1, xa, xb) 498 if nx_f32_lt(nx_f32_abs(nx_f32_sub(pr, tv)), tg_q(1, 4)) == 1 { okp = okp + 1 } 499 k = k + 1 500 } 501 if okp != 4 { pg = 0 } 502 t4_bp(lfd, "MODELWRIGHT T4 XOR loss_milli=" as *u8); t4_bn(lfd, tg_milli(ox1[1])) 503 t4_bp(lfd, " preds_ok=" as *u8); t4_bn(lfd, okp); t4_bp(lfd, "/4\n" as *u8) 504 if pg == 1 { _tg_puts(" GATE G XOR learned (2-8-1 relu MLP + AdamW, deterministic init): PASS\n" as *u8) } else { _tg_puts(" GATE G XOR: FAIL\n" as *u8) } 505 506 // ---- GATE H: census model trains to 100% on the real corpus ---- 507 let xs: *i64 = sys_mmap(16384) as *i64 508 let ts: *i64 = sys_mmap(16384) as *i64 509 t4_featurize(names, labels, cnt, xs, ts) 510 let wts: *i64 = sys_mmap(2048) as *i64 511 s2 = 0 512 while s2 < cnt { wts[s2] = nx_f32_div(tg_q(1, T4_NCLS), nx_i32_to_f32(hist[labels[s2]])); s2 = s2 + 1 } 513 let wc1: *i64 = sys_mmap(4096) as *i64 514 let oc1: *i64 = sys_mmap(64) as *i64 515 let curve: *i64 = sys_mmap(4096) as *i64 516 let curven: *i64 = sys_mmap(32) as *i64 517 t4_cen_train(tape, nb, arena, ab, xs, ts, wts, cnt, wc1, lfd, oc1, curve, curven) 518 let out8: *i64 = sys_mmap(128) as *i64 519 let pcount: *i64 = sys_mmap(128) as *i64 520 var acc: i64 = 0 521 s2 = 0 522 while s2 < cnt { 523 let pred: i64 = t4_cen_fwd(wc1, (xs as i64 + s2*T4_INB*8) as *i64, out8) 524 pcount[pred] = pcount[pred] + 1 525 if pred == labels[s2] { acc = acc + 1 } else { 526 _tg_puts(" MISS name=" as *u8); _tg_puts((names as i64 + s2*24) as *u8) 527 _tg_puts(" label=" as *u8); _tg_num(labels[s2]) 528 _tg_puts(" pred=" as *u8); _tg_num(pred); _tg_puts("\n" as *u8) 529 } 530 s2 = s2 + 1 531 } 532 var ph: i64 = 1 533 if acc != cnt { ph = 0 } 534 c2 = 0 535 while c2 < T4_NCLS { 536 if pcount[c2] != hist[c2] { ph = 0 } 537 c2 = c2 + 1 538 } 539 if nx_f32_lt(oc1[1], oc1[0]) == 0 { ph = 0 } 540 t4_bp(lfd, "MODELWRIGHT T4 CENSUS acc=" as *u8); t4_bn(lfd, acc) 541 t4_bp(lfd, "/" as *u8); t4_bn(lfd, cnt) 542 t4_bp(lfd, " first_loss_milli=" as *u8); t4_bn(lfd, tg_milli(oc1[0])) 543 t4_bp(lfd, " final_loss_milli=" as *u8); t4_bn(lfd, tg_milli(oc1[1])); t4_bp(lfd, "\n" as *u8) 544 if ph == 1 { _tg_puts(" GATE H census model 100% on the REAL corpus (per-class counts == ground truth): PASS\n" as *u8) } else { _tg_puts(" GATE H census model: FAIL\n" as *u8) } 545 546 // ---- GATE I: bit-exact two-run training, both lanes (the standing EXCEED axis) ---- 547 let wx2: *i64 = sys_mmap(256) as *i64 548 let ox2: *i64 = sys_mmap(64) as *i64 549 t4_xor_train(tape, nb, arena, ab, wx2, ox2) 550 let wc2: *i64 = sys_mmap(4096) as *i64 551 let oc2: *i64 = sys_mmap(64) as *i64 552 let curve2: *i64 = sys_mmap(4096) as *i64 553 let curven2: *i64 = sys_mmap(32) as *i64 554 t4_cen_train(tape, nb, arena, ab, xs, ts, wts, cnt, wc2, 0 - 1, oc2, curve2, curven2) 555 var pi: i64 = 1 556 k = 0 557 while k < 33 { if wx1[k] != wx2[k] { pi = 0 } k = k + 1 } 558 k = 0 559 while k < t4_tot() { if wc1[k] != wc2[k] { pi = 0 } k = k + 1 } 560 t4_bp(lfd, "MODELWRIGHT T4 BITEXACT xor_cells=33 census_cells=" as *u8); t4_bn(lfd, t4_tot()) 561 t4_bp(lfd, " identical=" as *u8); t4_bn(lfd, pi); t4_bp(lfd, "\n" as *u8) 562 if pi == 1 { _tg_puts(" GATE I bit-exact two-run training (xor + census weight cells identical): PASS\n" as *u8) } else { _tg_puts(" GATE I bit-exact: FAIL\n" as *u8) } 563 564 // ---- machine-readable training report for nx_train_triage (override runs -> /tmp, 565 // never the durable path: the T4b report-clobber lesson) ---- 566 var rpath: *u8 = "knowledge/status/train_report_t4.log" as *u8 567 if argc >= 2 { rpath = "/tmp/_t4_report_override.log" as *u8 } 568 let rfd: i64 = sys_openat_wr(rpath, 0x1a4) 569 if rfd >= 0 { 570 c2 = 0 571 while c2 < T4_NCLS { 572 t4_fp(rfd, "TRIAGE-CLASS id=" as *u8); t4_fn(rfd, c2) 573 t4_fp(rfd, " count=" as *u8); t4_fn(rfd, hist[c2]); t4_fp(rfd, "\n" as *u8) 574 c2 = c2 + 1 575 } 576 c2 = 0 577 while c2 < T4_NCLS { 578 t4_fp(rfd, "TRIAGE-PRED id=" as *u8); t4_fn(rfd, c2) 579 t4_fp(rfd, " count=" as *u8); t4_fn(rfd, pcount[c2]); t4_fp(rfd, "\n" as *u8) 580 c2 = c2 + 1 581 } 582 t4_fp(rfd, "TRIAGE-ACC right=" as *u8); t4_fn(rfd, acc) 583 t4_fp(rfd, " total=" as *u8); t4_fn(rfd, cnt); t4_fp(rfd, "\n" as *u8) 584 var ci: i64 = 0 585 while ci < curven[0] { 586 t4_fp(rfd, "TRIAGE-LOSS ep=" as *u8); t4_fn(rfd, curve[ci*2+0]) 587 t4_fp(rfd, " micro=" as *u8); t4_fn(rfd, curve[ci*2+1]); t4_fp(rfd, "\n" as *u8) 588 ci = ci + 1 589 } 590 sys_close(rfd) 591 _tg_puts(" training report written: " as *u8); _tg_puts(rpath); _tg_puts("\n" as *u8) 592 } 593 594 var gates: i64 = 0 595 if po == 1 { gates = gates + 1 } 596 if pg == 1 { gates = gates + 1 } 597 if ph == 1 { gates = gates + 1 } 598 if pi == 1 { gates = gates + 1 } 599 t4_bp(lfd, "MODELWRIGHT T4 VERDICT gates=" as *u8); t4_bn(lfd, gates) 600 t4_bp(lfd, "/4" as *u8) 601 if gates == 4 { t4_bp(lfd, " pass=1\n" as *u8) } else { t4_bp(lfd, " pass=0\n" as *u8) } 602 sys_close(lfd) 603 if gates == 4 { 604 _tg_puts(" T4 FIRST-MODEL GATE: PASS (first real models trained on the team substrate, real team data, bit-exact)\n" as *u8) 605 sys_exit(0) 606 return 0 607 } 608 _tg_puts(" T4 FIRST-MODEL GATE: FAIL\n" as *u8) 609 sys_exit(1) 610 return 1 611}