code wiki / _hdl_build / nx_imatrix_test.nx

nx_imatrix_test.nx source

↩ module page · 38 lines · 3068 B

1// nx_imatrix_test.nx -- prove importance-weighted water-filling beats uniform quantization at the 2// SAME total bit budget: bits flow to the high-importance weights, cutting the quality-relevant 3// (importance-weighted) error -- the rate-distortion-optimal allocation, the imatrix win. Exit 0 if 4// water-fill spends the same bits but achieves materially lower weighted error, and protects the 5// most-important group. license_tier: ORIGINAL 6 7import "nx_imatrix.nx" 8import "nx_syscalls.nx" 9 10func it_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 11func it_num(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m}; 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(1, bb, k); return 0 } 12 13func main() -> i64 { 14 it_puts("=== IMATRIX = rate-distortion water-filling: bits go where distortion is expensive ===\n" as *u8) 15 let N: i64 = 4 16 let imp: *i64 = sys_mmap(8*8) as *i64 17 imp[0]=100; imp[1]=10; imp[2]=10; imp[3]=1 // weight-group importances (from calibration) 18 let budget: i64 = 16 // total bits (4/group uniform) 19 let ub: *i64 = sys_mmap(8*8) as *i64; let wb: *i64 = sys_mmap(8*8) as *i64 20 imat_uniform(N, budget, ub) 21 imat_waterfill(N, imp, budget, wb) 22 let ue: i64 = imat_weighted_error(N, imp, ub) 23 let we: i64 = imat_weighted_error(N, imp, wb) 24 it_puts(" uniform bits=[" as *u8); var i: i64 = 0; while i < N { it_num(ub[i]); if i<N-1 {it_puts("," as *u8)} i=i+1 } it_puts("] total=" as *u8); it_num(imat_total_bits(N,ub)); it_puts(" weighted-error=" as *u8); it_num(ue); it_puts("\n" as *u8) 25 it_puts(" waterfill bits=[" as *u8); i = 0; while i < N { it_num(wb[i]); if i<N-1 {it_puts("," as *u8)} i=i+1 } it_puts("] total=" as *u8); it_num(imat_total_bits(N,wb)); it_puts(" weighted-error=" as *u8); it_num(we); it_puts("\n" as *u8) 26 it_puts(" -> error reduction x1000 = " as *u8); it_num((ue * 1000) / we); it_puts(" (bits to the imp=100 group: " as *u8); it_num(wb[0]); it_puts(" vs uniform 4)\n" as *u8) 27 28 let r: *i64 = sys_mmap(8*8) as *i64 29 r[0] = 0; if imat_total_bits(N, wb) == budget { r[0] = 1 } // SAME bit budget (no size cost) 30 r[1] = 0; if we < ue { r[1] = 1 } // lower quality-relevant error 31 r[2] = 0; if we * 2 <= ue { r[2] = 1 } // materially lower (>=2x) 32 r[3] = 0; if wb[0] > 4 { if wb[3] < 4 { r[3] = 1 } } // protected the important, starved the trivial 33 var pass: i64 = 0; var j: i64 = 0 34 while j < 4 { pass = pass + r[j]; j = j + 1 } 35 it_puts("----\n passed " as *u8); it_num(pass); it_puts("/4\n" as *u8) 36 if pass == 4 { it_puts(" IMATRIX PROVEN: same bits, water-filled by importance -> materially lower quality-relevant error. Rate-distortion optimal.\n" as *u8); sys_exit(0); return 0 } 37 it_puts(" FAIL\n" as *u8); sys_exit(1); return 1 38}