code wiki / _hdl_build / nx_vcodec_band_gate.nx

nx_vcodec_band_gate.nx source

↩ module page · 137 lines · 10022 B

1import "nx_gate_base.nx" 2// nx_vcodec_band_gate.nx -- TILE-PARALLEL BAND proof (task #47 rung 1b): a full 3-plane frame split into K 3// independent bands via vv_enc_band. Decode the bands in REVERSE order and reconstruct byte-exact -> each 4// band is self-contained (its own Web Worker / wasm instance now, GPU workgroup later). Measures the per-band 5// encode time = the K-core speedup a fanned-out client realizes. license_tier: ORIGINAL 6import "nx_syscalls.nx" 7import "nx_video_codec_wasm.nx" 8import "nx_gate_verdict.nx" 9 10func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 11" as *u8); return ok } 12func gn(v: i64) -> i64 { 13 let b: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 14 let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} 15 var i: i64=0; while i<k{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 16func ck(name: *u8, ok: i64, p: *i64, t: *i64) -> i64 { 17 t[0]=t[0]+1; gw(" " as *u8); gw(name); if ok==1 { gw(" PASS\n" as *u8); p[0]=p[0]+1 } else { gw(" FAIL\n" as *u8) } return 0 } 18func eqb(a: *u8, b: *u8, n: i64) -> i64 { var i: i64=0; while i<n { if (a[i]&0xff)!=(b[i]&0xff) { return 0 } i=i+1 } return 1 } 19// djb2-64 over a byte range (wrap-around multiply = deterministic). THE ANTI-SELF-CONSISTENCY TOOTH 20// (2026-08-03, bite killed=0/15): every original tooth compared encoder-recon vs decoder-recon THROUGH 21// THE SAME imported codec, so any SYMMETRIC mutation (shared quantize/transform, consistent wire-format 22// change) survived -- equivalence-of-a-mutant-with-itself. The fixture is fully deterministic (fillf, 23// fixed W/H/q), so the stream bytes and recon planes are PINNABLE: these digests bind the codec's 24// OBSERVABLE OUTPUT to a golden captured from the proven build, and any mutation that moves either the 25// wire or the pixels now dies even when it moves both arms consistently. 26func bdig(h0: i64, b: *u8, n: i64) -> i64 { var h: i64=h0; var i: i64=0; while i<n { h=h*33+(b[i] as i64 & 0xff); i=i+1 } return h } 27func fillf(yuv: *u8, W: i64, H: i64, sh: i64) -> i64 { 28 let N: i64=W*H; let C: i64=(W/2)*(H/2) 29 var r: i64=0 30 while r<H { var c: i64=0 31 while c<W { let sc: i64=c+sh; var v: i64=(sc*255)/W + (((sc/2+r/3)%7)*20-60); if sc>W/2 { v=v+45 } 32 if v<0 {v=0} if v>255 {v=255}; yuv[r*W+c]=v as u8; c=c+1 } r=r+1 } 33 var i: i64=N; while i<N+C { yuv[i]=110 as u8; i=i+1 } 34 while i<N+2*C { yuv[i]=140 as u8; i=i+1 } return 0 } 35 36// CAPTURE MODE: goldens start 0; the first promoted run prints the measured digests, which are then 37// PINNED here (two-step by design -- a golden invented rather than captured would itself be a fabrication). 38const BG_GOLD_STREAM: i64 = 1304691377225617806 39const BG_GOLD_RECON: i64 = 2073622208902865948 40const BG_GOLD_KSTREAM: i64 = 4130506216870815332 41const BG_GOLD_KRECON: i64 = 0 - 7393586302180263609 42const BG_GOLD_Q34S: i64 = 7872532560960160334 43const BG_GOLD_Q34R: i64 = 7815703861763894762 44 45func main() -> i64 { 46 gw("=== nx_vcodec_band_gate: tile-parallel BANDS -- full 3-plane, independent, bit-exact (rung 1b) ===\n" as *u8) 47 let p: *i64=sys_mmap(16) as *i64; let t: *i64=sys_mmap(16) as *i64 48 let W: i64=256; let H: i64=192; let q: i64=18 49 let N: i64=W*H; let C: i64=(W/2)*(H/2); let sz: i64=N+2*C 50 let BH: i64=H/16; let K: i64=3; let tr: i64=BH/K // 12 MB rows / 3 = 4 each (EVEN -> chroma-aligned) 51 let cur: *u8=sys_mmap(sz); let prev: *u8=sys_mmap(sz) 52 let reconE: *u8=sys_mmap(sz); let reconD: *u8=sys_mmap(sz) 53 let blk: *i64=sys_mmap(512) as *i64; let mv: *i64=sys_mmap(128) as *i64 54 let s0: *u8=sys_mmap(131072); let s1: *u8=sys_mmap(131072); let s2: *u8=sys_mmap(131072) 55 var z: i64=0; while z<sz { prev[z]=0 as u8; z=z+1 } 56 57 // ---- KEYFRAME: 3 bands ---- 58 fillf(cur, W, H, 0) 59 let l0: i64=vv_enc_band(cur, prev, reconE, W, H, 0*tr, 1*tr, q, 1, 6016, s0, 131072, blk, mv) 60 let l1: i64=vv_enc_band(cur, prev, reconE, W, H, 1*tr, 2*tr, q, 1, 6016, s1, 131072, blk, mv) 61 let l2: i64=vv_enc_band(cur, prev, reconE, W, H, 2*tr, 3*tr, q, 1, 6016, s2, 131072, blk, mv) 62 var okenc: i64=1; if l0<0 { okenc=0 } if l1<0 { okenc=0 } if l2<0 { okenc=0 } 63 // decode in REVERSE order -> proves band independence 64 vv_dec_band(prev, reconD, W, H, 2*tr, 3*tr, q, s2, l2, blk, mv) 65 vv_dec_band(prev, reconD, W, H, 1*tr, 2*tr, q, s1, l1, blk, mv) 66 vv_dec_band(prev, reconD, W, H, 0*tr, 1*tr, q, s0, l0, blk, mv) 67 ck("keyframe: 3 bands (3 planes) decode REVERSE-order byte-exact\x00" as *u8, okenc & eqb(reconE, reconD, sz), p, t) 68 // KEYFRAME goldens too (2026-08-03, wide bite killed=0/22): the P-frame goldens below leave every 69 // KEYFRAME-only code path bound by nothing but symmetric equivalence -- a mutant living there still 70 // survives. Same capture-then-pin recipe, second pin. 71 var hks: i64 = bdig(5381, s0, l0); hks = bdig(hks, s1, l1); hks = bdig(hks, s2, l2) 72 let hkr: i64 = bdig(5381, reconE, sz) 73 gw(" golden: K-stream=" as *u8); gn(hks); gw(" K-recon=" as *u8); gn(hkr); gw("\n" as *u8) 74 ck("keyframe stream bytes match the pinned golden\x00" as *u8, (hks==BG_GOLD_KSTREAM) as i64, p, t) 75 ck("keyframe recon plane matches the pinned golden\x00" as *u8, (hkr==BG_GOLD_KRECON) as i64, p, t) 76 77 // ---- P-FRAME: bands reference the full prev, decode out-of-order ---- 78 fillf(prev, W, H, 0); fillf(cur, W, H, 5) 79 var z2: i64=0; while z2<sz { reconE[z2]=0xCC as u8; reconD[z2]=0xCC as u8; z2=z2+1 } 80 let m0: i64=vv_enc_band(cur, prev, reconE, W, H, 0, tr, q, 0, 6016, s0, 131072, blk, mv) 81 let m1: i64=vv_enc_band(cur, prev, reconE, W, H, tr, 2*tr, q, 0, 6016, s1, 131072, blk, mv) 82 let m2: i64=vv_enc_band(cur, prev, reconE, W, H, 2*tr, 3*tr, q, 0, 6016, s2, 131072, blk, mv) 83 vv_dec_band(prev, reconD, W, H, tr, 2*tr, q, s1, m1, blk, mv) 84 vv_dec_band(prev, reconD, W, H, 0, tr, q, s0, m0, blk, mv) 85 vv_dec_band(prev, reconD, W, H, 2*tr, 3*tr, q, s2, m2, blk, mv) 86 ck("P-frame: bands decode out-of-order byte-exact (motion via shared prev)\x00" as *u8, eqb(reconE, reconD, sz), p, t) 87 88 // ---- GOLDEN DIGESTS (anti-self-consistency; see bdig) ---- 89 var hs: i64 = bdig(5381, s0, m0); hs = bdig(hs, s1, m1); hs = bdig(hs, s2, m2) 90 let hr: i64 = bdig(5381, reconE, sz) 91 gw(" golden: P-stream=" as *u8); gn(hs); gw(" P-recon=" as *u8); gn(hr); gw("\n" as *u8) 92 ck("P-frame stream bytes match the pinned golden (wire format bound)\x00" as *u8, (hs==BG_GOLD_STREAM) as i64, p, t) 93 ck("P-frame recon plane matches the pinned golden (pixels bound)\x00" as *u8, (hr==BG_GOLD_RECON) as i64, p, t) 94 95 // ---- SECOND QP ARM (2026-08-03, fixture-coverage lane: 22/22 mutants proven OUTPUT-INVARIANT at 96 // q=18, so the remaining distance is INPUT DIVERSITY, not assertion strength -- a different quantizer 97 // exercises different quant/dequant/clamp branches; same capture-then-pin recipe, third pin.) ---- 98 fillf(prev, W, H, 0); fillf(cur, W, H, 9) 99 var z4: i64=0; while z4<sz { reconE[z4]=0xCC as u8; reconD[z4]=0xCC as u8; z4=z4+1 } 100 let q2: i64 = 34 101 let n0: i64=vv_enc_band(cur, prev, reconE, W, H, 0, tr, q2, 0, 6016, s0, 131072, blk, mv) 102 let n1: i64=vv_enc_band(cur, prev, reconE, W, H, tr, 2*tr, q2, 0, 6016, s1, 131072, blk, mv) 103 let n2: i64=vv_enc_band(cur, prev, reconE, W, H, 2*tr, 3*tr, q2, 0, 6016, s2, 131072, blk, mv) 104 vv_dec_band(prev, reconD, W, H, 2*tr, 3*tr, q2, s2, n2, blk, mv) 105 vv_dec_band(prev, reconD, W, H, 0, tr, q2, s0, n0, blk, mv) 106 vv_dec_band(prev, reconD, W, H, tr, 2*tr, q2, s1, n1, blk, mv) 107 ck("q34 P-frame: bands decode out-of-order byte-exact\x00" as *u8, eqb(reconE, reconD, sz), p, t) 108 var hq: i64 = bdig(5381, s0, n0); hq = bdig(hq, s1, n1); hq = bdig(hq, s2, n2) 109 let hq2: i64 = bdig(5381, reconE, sz) 110 gw(" golden: q34-stream=" as *u8); gn(hq); gw(" q34-recon=" as *u8); gn(hq2); gw("\n" as *u8) 111 ck("q34 stream bytes match the pinned golden\x00" as *u8, (hq==BG_GOLD_Q34S) as i64, p, t) 112 ck("q34 recon plane matches the pinned golden\x00" as *u8, (hq2==BG_GOLD_Q34R) as i64, p, t) 113 114 // ---- speedup: whole-frame vv_enc vs one band ---- 115 let full: *u8=sys_mmap(262144); let pe: *u8=sys_mmap(sz); var z3: i64=0; while z3<sz { pe[z3]=0 as u8; z3=z3+1 } 116 let ITER: i64=40 117 let ta: i64=sys_now_us(); var ia: i64=0 118 while ia<ITER { vv_enc(cur, pe, reconE, W, H, q, 0, 6016, full, 262144, blk, mv); ia=ia+1 } 119 let serial: i64=(sys_now_us()-ta)/ITER 120 let tb: i64=sys_now_us(); ia=0 121 while ia<ITER { vv_enc_band(cur, pe, reconE, W, H, 0, tr, q, 0, 6016, s0, 131072, blk, mv); ia=ia+1 } 122 let band1: i64=(sys_now_us()-tb)/ITER 123 gw(" serial vv_enc=" as *u8); gn(serial); gw("us one-of-" as *u8); gn(K); gw("-bands=" as *u8); gn(band1) 124 gw("us => " as *u8); gn(K); gw(" workers ~= " as *u8); gn(serial/(band1+1)); gw("x encode speedup\n" as *u8) 125 ck("bands add no serial overhead (K bands ~= 1 frame of work)\x00" as *u8, (band1*K < serial*13/10 + 300) as i64, p, t) 126 127 gw("BAND-GATE: " as *u8); gn(p[0]); gw("/" as *u8); gn(t[0]) 128 if p[0]==t[0] { gw(" GREEN -- bands are INDEPENDENT 3-plane units -> client can fan them across Workers now\n" as *u8) } else { gw(" RED\n" as *u8) } 129 // D001: the legacy BAND-GATE line above is this gate's public signature and is KEPT verbatim; the 130 // machine-readable verdict below is what every sweep and every bite actually parses. Without it the 131 // fold read this gate as pass=0/0 -- INDISTINGUISHABLE FROM A GATE THAT ASSERTS NOTHING, even though 132 // it has three real teeth (keyframe reverse-order byte-exact, P-frame out-of-order byte-exact, no 133 // serial overhead). I filed it as empty on that reading; it was never empty, only unreadable. 134 let ctr: *i64 = sys_mmap(16) as *i64 135 ctr[0] = p[0] 136 ctr[1] = t[0] 137 return gv_verdict("VCODEC-BAND-GATE" as *u8, ctr, "tile-parallel banded encode/decode equivalence, 3 checks" as *u8) }