code wiki / _hdl_build / nx_nofloat_serialize_gate.nx

nx_nofloat_serialize_gate.nx source

↩ module page · 92 lines · 5558 B

1// nx_nofloat_serialize_gate.nx -- CAP-NF-SERIALIZE: sovereign model PERSISTENCE (save/load trained weights). 2// You can't deploy a model you can't save -- this is the missing systems piece. Own binary format: 3// [magic][n][WN[0..n-1]][weight arrays raw i64][checksum]. Pure integer, sovereign (sys_openat/read/write only). 4// T1 ROUND-TRIP LOSSLESS: deserialized weights are BIT-IDENTICAL to the originals. 5// T2 INTEGRITY: checksum recomputed from the loaded weights == the stored checksum. 6// T3 TEETH: flipping one loaded value makes the recomputed checksum MISMATCH (corruption is detectable). 7// expect_exit: 0 Sovereign: nx_syscalls only. 8import "nx_syscalls.nx" 9import "nx_gate_emit_lib.nx" 10import "nx_gate_verdict.nx" 11const MAGIC: i64 = 6219518 // "NVM1"-ish sovereign model magic 12 13 14func dini(a: *i64, n: i64, sd: i64) -> i64 { var i: i64=0; while i<n { a[i]=(((i*7+sd*13+1)%97)-48)*131 + sd*7; i=i+1 } return 0 } 15func wi64(fd: i64, v: i64) -> i64 { let c: *i64=sys_mmap(8) as *i64; c[0]=v; sys_write(fd, c as *u8, 8); return 0 } 16func ri64(fd: i64) -> i64 { let c: *i64=sys_mmap(8) as *i64; sys_read(fd, c as *u8, 8); return c[0] } 17func checksum(W: *i64, WN: *i64, n: i64) -> i64 { var s: i64=0; var i: i64=0; while i<n { let a: *i64=W[i] as *i64; let cn: i64=WN[i]; var c: i64=0; while c<cn { s=s+a[c]; c=c+1 } i=i+1 } return s } 18 19func serialize(path: *u8, W: *i64, WN: *i64, n: i64) -> i64 { 20 let fd: i64=sys_openat_wr(path, 420) 21 if fd<0 { return 0-1 } 22 wi64(fd, MAGIC); wi64(fd, n) 23 var i: i64=0; while i<n { wi64(fd, WN[i]); i=i+1 } // sizes 24 i=0; while i<n { sys_write(fd, W[i] as *u8, WN[i]*8); i=i+1 } // raw weight arrays 25 wi64(fd, checksum(W,WN,n)) // trailing checksum 26 sys_close(fd) 27 return 0 28} 29// loads into freshly-mmap'd arrays; fills W2[]/WN2[]; returns the stored checksum (or -1 on bad magic/open) 30func deserialize(path: *u8, W2: *i64, WN2: *i64, n_expect: i64) -> i64 { 31 let fd: i64=sys_openat_rd(path) 32 if fd<0 { return 0-1 } 33 let magic: i64=ri64(fd); let n: i64=ri64(fd) 34 if magic!=MAGIC { sys_close(fd); return 0-1 } 35 var i: i64=0; while i<n { WN2[i]=ri64(fd); i=i+1 } 36 i=0; while i<n { let a: *i64=sys_mmap(WN2[i]*8) as *i64; sys_read(fd, a as *u8, WN2[i]*8); W2[i]=a as i64; i=i+1 } 37 let chk: i64=ri64(fd) 38 sys_close(fd) 39 return chk 40} 41 42func main() -> i64 { 43 g_puts("nx_nofloat_serialize gate (sovereign model save/load -- weight persistence for deployment)\n" as *u8) 44 let n: i64=4 45 let W: *i64=sys_mmap(n*8) as *i64; let WN: *i64=sys_mmap(n*8) as *i64 46 WN[0]=96; WN[1]=256; WN[2]=256; WN[3]=48 // representative model weight shapes 47 var i: i64=0; while i<n { let a: *i64=sys_mmap(WN[i]*8) as *i64; dini(a,WN[i],i+1); W[i]=a as i64; i=i+1 } 48 let orig_chk: i64=checksum(W,WN,n) 49 50 let path: *u8="knowledge/store/nf_model.bin" as *u8 51 let sok: i64=serialize(path, W, WN, n) 52 53 let W2: *i64=sys_mmap(n*8) as *i64; let WN2: *i64=sys_mmap(n*8) as *i64 54 let stored_chk: i64=deserialize(path, W2, WN2, n) 55 56 // T1: bit-identical round-trip 57 var identical: i64=1; var totvals: i64=0 58 i=0 59 while i<n { if WN2[i]!=WN[i] { identical=0 } else { let a: *i64=W[i] as *i64; let b: *i64=W2[i] as *i64; var c: i64=0; while c<WN[i] { if a[c]!=b[c] { identical=0 } c=c+1 } totvals=totvals+WN[i] } i=i+1 } 60 // T2: integrity (recomputed-from-loaded == stored) 61 let recomp: i64=checksum(W2,WN2,n) 62 var integ: i64=0; if recomp==stored_chk { if stored_chk==orig_chk { integ=1 } } 63 // T3 teeth: corrupt one loaded value -> checksum must change (mismatch detectable) 64 let b0: *i64=W2[0] as *i64; b0[0]=b0[0]+1 65 let corrupt_chk: i64=checksum(W2,WN2,n) 66 var detected: i64=0; if corrupt_chk!=stored_chk { detected=1 } 67 68 g_puts(" [measure] saved="); if sok==0 { g_puts("ok" as *u8) } else { g_puts("FAIL" as *u8) } 69 g_puts(" values="); g_pn(totvals); g_puts(" orig_chk="); g_pn(orig_chk); g_puts(" stored_chk="); g_pn(stored_chk); g_puts(" identical="); g_pn(identical); g_puts("\n") 70 71 var pass: i64=0; var total: i64=0 72 var t1: i64=0; if sok==0 { if identical==1 { t1=1 } } 73 pass=pass+g_check("T1: ROUND-TRIP LOSSLESS -- loaded weights bit-identical to originals" as *u8, t1); total=total+1 74 pass=pass+g_check("T2: INTEGRITY -- checksum(loaded) == stored == original" as *u8, integ); total=total+1 75 pass=pass+g_check("T3: TEETH -- corrupting one loaded value breaks the checksum (corruption detectable)" as *u8, detected); total=total+1 76 77 var okall: i64=0; if pass==total { okall=1 } 78 if okall==1 { 79 let logf: i64=sys_openat_append("knowledge/status/nofloat_serialize.log" as *u8, 420) 80 if logf>=0 { let x0: i64=sys_write(logf,"NOFLOATSERIALIZE model save/load round-trip + checksum measured\n" as *u8,63); sys_close(logf) } 81 } 82 g_puts("---- serialize gate: passed "); g_pn(pass); g_puts(" / "); g_pn(total); g_puts(" ----\n") 83 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 84 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 85 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 86 let ctr__dry: *i64 = gv_ctr() 87 ctr__dry[0] = pass 88 ctr__dry[1] = total 89 let rc__dry: i64 = gv_verdict("NOFLOAT-SERIALIZE-GATE" as *u8, ctr__dry, "sovereign model persistence: save->load bit-exact + checksum integrity -- deployable no-float models)" as *u8) 90 sys_exit(rc__dry) 91 return rc__dry 92}