code wiki / (root) / nx_f16_kat.nx

nx_f16_kat.nx source

↩ module page · 45 lines · 2380 B

1// nx_f16_kat.nx -- regression KAT for nx_f16_to_f32, guarding the subnormal fix (nx_f32_cvt.nx: f32_exp was 2// 114-exp_adj, correct 113-exp_adj). The old bug DOUBLED every subnormal f16 -> the Q6_K ffn_down 2x forward bug 3// that blocked the sovereign LLM for the project's whole history. This KAT asserts exact f32 bits for normals, 4// subnormals (incl smallest 2^-24 and the largest-subnormal/smallest-normal boundary), and zero. GREEN = the 5// fix holds. license_tier: ORIGINAL expect_exit: 0 6import "nx_syscalls.nx" 7import "nx_tier.nx" 8import "nx_f32_cvt.nx" 9 10func kw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 11func kh(v: i64) -> i64 { let b: *u8=sys_mmap(8); var i: i64=0; while i<8 { let n: i64=(v>>((7-i)*4))&0xF; if n<10 {b[i]=(48+n) as u8} else {b[i]=(87+n) as u8} i=i+1 } sys_write(1,b,8); return 0 } 12 13func chk(label: *u8, f16: i64, want: i64, pass: *i64, tot: *i64) -> i64 { 14 tot[0] = tot[0] + 1 15 let got: i64 = nx_f16_to_f32(f16) & 0xFFFFFFFF 16 kw(" "); kw(label); kw(" f16="); kh(f16); kw(" -> "); kh(got); kw(" want "); kh(want) 17 if got == want { pass[0] = pass[0] + 1; kw(" OK\n") } else { kw(" <== FAIL\n") } 18 return 0 19} 20 21func main() -> i64 { 22 kw("=== nx_f16_to_f32 KAT (guards the subnormal 113-vs-114 fix) ===\n" as *u8) 23 let pass: *i64 = sys_mmap(8) as *i64 24 let tot: *i64 = sys_mmap(8) as *i64 25 pass[0] = 0; tot[0] = 0 26 // normals 27 chk("1.0 " as *u8, 0x3C00, 0x3F800000, pass, tot) 28 chk("2.0 " as *u8, 0x4000, 0x40000000, pass, tot) 29 chk("0.5 " as *u8, 0x3800, 0x3F000000, pass, tot) 30 chk("-2.0 " as *u8, 0xC000, 0xC0000000, pass, tot) 31 // zero 32 chk("+0 " as *u8, 0x0000, 0x00000000, pass, tot) 33 // smallest normal f16 = 2^-14 34 chk("min-nrm" as *u8, 0x0400, 0x38800000, pass, tot) 35 // SUBNORMALS (the fix): smallest subnormal = 2^-24; old bug gave 2^-23 (0x34000000) 36 chk("min-sub" as *u8, 0x0001, 0x33800000, pass, tot) 37 // subnormal 2 * min = 2^-23 38 chk("2^-23 " as *u8, 0x0002, 0x34000000, pass, tot) 39 // subnormal 4 * min = 2^-22 40 chk("2^-22 " as *u8, 0x0004, 0x34800000, pass, tot) 41 42 kw("nx_f16_kat pass="); kh(pass[0]); kw(" / "); kh(tot[0]) 43 if pass[0] == tot[0] { kw(" GREEN (subnormal f16 correct; the 2x LLM bug stays dead)\n" as *u8); sys_exit(0); return 0 } 44 kw(" RED\n" as *u8); sys_exit(1); return 1 45}