nx_hifigan_load.nx source
↩ module page · 114 lines · 8384 B
1// nx_hifigan_load.nx -- LOAD real HiFi-GAN weights into our sovereign vocoder (Qwen pattern: 3rd-party weights =
2// data, OUR engine = sovereign, hardware-rung up). Parses the .bin ZIP into a data/N offset+size table, reads raw
3// little-endian f32 blobs directly (STORED, no dequant, NO weight-norm -- SpeechT5 fused it), and verifies the
4// critical new mechanic on conv_pre: sizes match the derived shapes, the values are SANE trained floats (small,
5// not garbage), and the real-dim (512-ch) conv actually runs finite. Once this is trusted, the full generator is
6// the same load pattern x158 tensors -> nx_hifigan_gen at real dims. license_tier: ORIGINAL expect_exit: 0
7import "nx_syscalls.nx"
8import "nx_f32.nx"
9import "nx_vocops.nx"
10const K_MAGIC_67108864: i64 = 67108864
11const K_MAGIC_286720: i64 = 286720
12const K_MAGIC_1146880: i64 = 1146880
13const K_MAGIC_200000: i64 = 200000
14
15func p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
16func pn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let d: *u8=sys_mmap(24); var k: i64=0; while m>0 {d[k]=(48+(m%10)) as u8; m=m/10; k=k+1} var i: i64=k-1; while i>=0 {let o: *u8=sys_mmap(1); o[0]=d[i]; sys_write(1,o,1); i=i-1} return 0 }
17func phx(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 }
18func u16le(b: *u8, o: i64) -> i64 { return (b[o]&0xff) | ((b[o+1]&0xff)<<8) }
19func u32le(b: *u8, o: i64) -> i64 { return (b[o]&0xff) | ((b[o+1]&0xff)<<8) | ((b[o+2]&0xff)<<16) | ((b[o+3]&0xff)<<24) }
20
21// f32 subtract a-b via sign-flip of b (avoids depending on nx_f32_sub): negate b's sign bit, then add.
22func f32_sub(a: i64, b: i64) -> i64 { return nx_f32_add(a, (b & 0xFFFFFFFF) ^ 0x80000000) }
23// read `count` little-endian f32 from blob@boff into out (as i64-held f32 bits).
24func load_f32(buf: *u8, boff: i64, count: i64, out: *i64) -> i64 { var i: i64=0; while i<count { out[i] = u32le(buf, boff + 4*i); i=i+1 } return 0 }
25
26// |f32| magnitude class: return the biased exponent (0..255); trained weights sit ~2^-8..2^1 -> exp ~119..128.
27func f32_exp(x: i64) -> i64 { return (x>>23)&0xFF }
28
29func main() -> i64 {
30 p("=== nx_hifigan_load -- load REAL HiFi-GAN weights (3rd-party data on our sovereign engine) ===\n" as *u8)
31 let path: *u8 = "/home/elderwesto/nx_stage/hifigan_pytorch_model.bin" as *u8
32 let fd: i64 = sys_openat_rd(path)
33 if fd<0 { p("open failed\n" as *u8); sys_exit(1); return 1 }
34 let cap: i64 = K_MAGIC_67108864
35 let buf: *u8 = sys_mmap(cap)
36 var n: i64=0; var go: i64=1
37 while go==1 { let r: i64=sys_read(fd,(buf as i64+n) as *u8,cap-n); if r<=0 {go=0} else {n=n+r; if n>=cap {go=0}} }
38 sys_close(fd)
39
40 // find EOCD + central dir
41 var ez: i64 = n-22
42 while ez>=0 { if buf[ez]==0x50 as u8 { if buf[ez+1]==0x4B as u8 { if buf[ez+2]==0x05 as u8 { if buf[ez+3]==0x06 as u8 { break } } } } ez=ez-1 }
43 if ez<0 { p("no EOCD\n" as *u8); sys_exit(1); return 1 }
44 let n_entries: i64 = u16le(buf, ez+10)
45 // data/N offset+size tables (N up to 200)
46 let doff: *i64 = sys_mmap(256*8) as *i64
47 let dsz: *i64 = sys_mmap(256*8) as *i64
48 var idx: i64=0; while idx<256 { doff[idx]=0-1; dsz[idx]=0; idx=idx+1 }
49 var off: i64 = u32le(buf, ez+16)
50 var e: i64=0
51 while e<n_entries {
52 if buf[off]!=0x50 as u8 { e=n_entries } else {
53 let usize: i64 = u32le(buf, off+24)
54 let fnl: i64 = u16le(buf, off+28)
55 let exl: i64 = u16le(buf, off+30)
56 let cml: i64 = u16le(buf, off+32)
57 let lho: i64 = u32le(buf, off+42)
58 // parse "archive/data/K": find the last '/', read trailing int (skip data.pkl / version)
59 var slash: i64=0-1; var q: i64=0; while q<fnl { if buf[off+46+q]==0x2F as u8 { slash=q } q=q+1 }
60 if slash>=0 {
61 // is the char after slash a digit? (data/K) vs data.pkl/version
62 let c0: i64 = buf[off+46+slash+1] as i64
63 if c0>=48 { if c0<=57 {
64 var kk: i64=0; var pp: i64=slash+1; while pp<fnl { let c: i64=buf[off+46+pp] as i64; if c>=48 { if c<=57 { kk=kk*10+(c-48); pp=pp+1 } else { pp=fnl } } else { pp=fnl } }
65 if kk<256 { let lfnl: i64=u16le(buf,lho+26); let lexl: i64=u16le(buf,lho+28); doff[kk]=lho+30+lfnl+lexl; dsz[kk]=usize }
66 } }
67 }
68 off = off + 46 + fnl + exl + cml
69 e=e+1
70 }
71 }
72
73 // ---- load the tensors we verify (sequential map: mean=0 scale=1 conv_pre.bias=2 conv_pre.weight=3) ----
74 // sizes (bytes): mean/scale = 80 f32 = 320 ; conv_pre.bias = 512 f32 = 2048 ; conv_pre.weight = 512*80*7 = 286720 f32
75 p("data/0 (mean) size="); pn(dsz[0]); p("B data/1 (scale) size="); pn(dsz[1]); p("B\n" as *u8)
76 p("data/2 (conv_pre.bias) size="); pn(dsz[2]); p("B data/3 (conv_pre.weight) size="); pn(dsz[3]); p("B\n" as *u8)
77
78 let mean: *i64 = sys_mmap(128*8) as *i64; load_f32(buf, doff[0], 80, mean)
79 let scale: *i64 = sys_mmap(128*8) as *i64; load_f32(buf, doff[1], 80, scale)
80 let cpb: *i64 = sys_mmap(512*8) as *i64; load_f32(buf, doff[2], 512, cpb)
81 let cpw: *i64 = sys_mmap(K_MAGIC_286720*8) as *i64; load_f32(buf, doff[3], K_MAGIC_286720, cpw)
82
83 p("mean[0..2]="); phx(mean[0]&0xFFFFFFFF); p(" "); phx(mean[1]&0xFFFFFFFF); p(" "); phx(mean[2]&0xFFFFFFFF); p("\n" as *u8)
84 p("scale[0..2]="); phx(scale[0]&0xFFFFFFFF); p(" "); phx(scale[1]&0xFFFFFFFF); p(" "); phx(scale[2]&0xFFFFFFFF); p("\n" as *u8)
85 p("conv_pre.weight[0..3]="); phx(cpw[0]&0xFFFFFFFF); p(" "); phx(cpw[1]&0xFFFFFFFF); p(" "); phx(cpw[2]&0xFFFFFFFF); p(" "); phx(cpw[3]&0xFFFFFFFF); p("\n" as *u8)
86
87 // sanity: trained conv weights are small (|w| < 2, i.e. biased exp < 128) and not all-zero / not garbage-huge.
88 var sane: i64=1; var i: i64=0; var nz: i64=0
89 while i<K_MAGIC_286720 { let ex: i64=f32_exp(cpw[i]&0xFFFFFFFF); if ex>=140 { sane=0 } if (cpw[i]&0x7FFFFFFF)!=0 { nz=nz+1 } i=i+1 }
90 p("conv_pre.weight: nonzero="); pn(nz); p("/286720 max-exp-ok="); pn(sane); p("\n" as *u8)
91
92 // run conv_pre on a small test mel [80, T=4] -> [512, 4], WITH input normalization (mel-mean)/scale
93 let T: i64=4
94 let mel: *i64 = sys_mmap(80*4*8) as *i64
95 var c: i64=0; while c<80 { var t: i64=0; while t<T { mel[c*T+t]=0x3F000000; t=t+1 } c=c+1 } // mel=0.5
96 // normalize: (mel - mean)/scale per channel
97 c=0; while c<80 { var t: i64=0; while t<T { mel[c*T+t]=nx_f32_div(f32_sub(mel[c*T+t], mean[c]), scale[c]); t=t+1 } c=c+1 }
98 let pre: *i64 = sys_mmap(512*4*8) as *i64
99 let lo: i64 = conv1d(mel, cpw, cpb, pre, 80, T, 512, 7, 1, 3, 1)
100 // check finite
101 var finite: i64=1; i=0; while i<512*lo { if f32_exp(pre[i]&0xFFFFFFFF)==0xFF { finite=0 } i=i+1 }
102 p("conv_pre output [512,"); pn(lo); p("] finite="); pn(finite); p(" sample pre[0..2]="); phx(pre[0]&0xFFFFFFFF); p(" "); phx(pre[1]&0xFFFFFFFF); p(" "); phx(pre[2]&0xFFFFFFFF); p("\n" as *u8)
103
104 var pass: i64=0; var tot: i64=5
105 if dsz[3]==K_MAGIC_1146880 { pass=pass+1; p("PASS conv_pre.weight blob = 1146880B = 286720 f32 = 512x80x7 (shape verified vs config)\n" as *u8) } else { p("FAIL cpw size\n" as *u8) }
106 if dsz[0]==320 { if dsz[1]==320 { pass=pass+1; p("PASS mean/scale = 80 f32 each (input-norm buffers)\n" as *u8) } else {p("FAIL scale\n")} } else { p("FAIL mean size\n" as *u8) }
107 if nz > K_MAGIC_200000 { pass=pass+1; p("PASS conv_pre.weight is densely nonzero (real trained tensor, not empty)\n" as *u8) } else { p("FAIL too many zeros\n" as *u8) }
108 if sane==1 { pass=pass+1; p("PASS weight magnitudes are sane trained floats (no garbage-huge exponents)\n" as *u8) } else { p("FAIL insane magnitudes -- byte order / offset wrong\n" as *u8) }
109 if lo==4 { if finite==1 { pass=pass+1; p("PASS conv_pre runs at REAL dims (80->512) on real weights, output finite\n" as *u8) } else {p("FAIL not finite\n")} } else { p("FAIL conv_pre L_out\n" as *u8) }
110
111 p("nx_hifigan_load pass="); pn(pass); p("/"); pn(tot)
112 if pass==tot { p(" GREEN -- real HiFi-GAN weights load correctly + run at real dims. The full generator is this same pattern x158 tensors.\n" as *u8); sys_exit(0); return 0 }
113 p(" RED\n" as *u8); sys_exit(1); return 1
114}