nx_vocops_test.nx source
↩ module page · 65 lines · 5348 B
1// nx_vocops_test.nx -- byte-exact KATs for the neural-vocoder op library (conv1d, conv_transpose1d, leaky_relu,
2// resblock). Verifies bricks 1-3 of the HiFi-GAN forward on the sovereign f32 substrate. license_tier: ORIGINAL expect_exit: 0
3import "nx_syscalls.nx"
4import "nx_f32.nx"
5import "nx_vocops.nx"
6
7func cw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
8func cn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} 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} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 }
9func chx(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 }
10func eqf(a: i64, e: i64) -> i64 { if (a&0xFFFFFFFF)==e { return 1 } return 0 }
11
12func main() -> i64 {
13 cw("=== nx_vocops_test -- HiFi-GAN forward bricks 1-3 byte-exact ===\n" as *u8)
14 var pass: i64=0; var tot: i64=0
15
16 // conv1d: box filter [1,1,1] pad1 over [1..5] -> [3,6,9,12,9]
17 let inp: *i64=sys_mmap(8*8) as *i64; inp[0]=0x3F800000; inp[1]=0x40000000; inp[2]=0x40400000; inp[3]=0x40800000; inp[4]=0x40A00000
18 let w111: *i64=sys_mmap(4*8) as *i64; w111[0]=0x3F800000; w111[1]=0x3F800000; w111[2]=0x3F800000
19 let bz: *i64=sys_mmap(8) as *i64; bz[0]=0
20 let o: *i64=sys_mmap(16*8) as *i64
21 conv1d(inp, w111, bz, o, 1, 5, 1, 3, 1, 1, 1)
22 tot=tot+1
23 if eqf(o[0],0x40400000)==1 { if eqf(o[3],0x41400000)==1 { if eqf(o[4],0x41100000)==1 { pass=pass+1; cw("PASS conv1d box+pad [3,6,9,12,9]\n" as *u8) } else {cw("FAIL conv1d c\n")} } else {cw("FAIL conv1d b\n")} } else {cw("FAIL conv1d a\n")}
24
25 // conv1d dilated (dil2,pad2): [1..5] w[1,1,1] -> t0=4,t2=9,t4=8
26 conv1d(inp, w111, bz, o, 1, 5, 1, 3, 1, 2, 2)
27 tot=tot+1
28 if eqf(o[0],0x40800000)==1 { if eqf(o[2],0x41100000)==1 { if eqf(o[4],0x41000000)==1 { pass=pass+1; cw("PASS conv1d dilated (HiFi-GAN MRF op)\n" as *u8) } else {cw("FAIL dil c\n")} } else {cw("FAIL dil b\n")} } else {cw("FAIL dil a\n")}
29
30 // conv_transpose1d: HiFi-style stride2/K4/pad1, [1,2] w[1,2,3,4] -> [2,5,8,6]
31 let in2: *i64=sys_mmap(8*8) as *i64; in2[0]=0x3F800000; in2[1]=0x40000000
32 let w4: *i64=sys_mmap(8*8) as *i64; w4[0]=0x3F800000; w4[1]=0x40000000; w4[2]=0x40400000; w4[3]=0x40800000
33 let ot: *i64=sys_mmap(16*8) as *i64
34 conv_transpose1d(in2, w4, bz, ot, 1, 2, 1, 4, 2, 1, 0, 1)
35 tot=tot+1
36 if eqf(ot[0],0x40000000)==1 { if eqf(ot[1],0x40A00000)==1 { if eqf(ot[2],0x41000000)==1 { if eqf(ot[3],0x40C00000)==1 { pass=pass+1; cw("PASS conv_transpose1d upsampler [2,5,8,6]\n" as *u8) } else {cw("FAIL ct d\n")} } else {cw("FAIL ct c\n")} } else {cw("FAIL ct b\n")} } else {cw("FAIL ct a\n")}
37
38 // leaky_relu slope 0.5: -2 -> -1
39 tot=tot+1
40 if eqf(leaky_relu(0xC0000000, 0x3F000000), 0xBF800000)==1 { if eqf(leaky_relu(0x40000000, 0x3F000000), 0x40000000)==1 { pass=pass+1; cw("PASS leaky_relu\n" as *u8) } else {cw("FAIL lr b\n")} } else {cw("FAIL lr a\n")}
41
42 // resblock (C=1,L=3,K=1,dil=1,slope0.5): x=[1,2,-3], w1=w2=[1],b=0 -> [2,4,-3.75]
43 let x: *i64=sys_mmap(8*8) as *i64; x[0]=0x3F800000; x[1]=0x40000000; x[2]=0xC0400000 // 1,2,-3
44 let w1: *i64=sys_mmap(8) as *i64; w1[0]=0x3F800000
45 let w2: *i64=sys_mmap(8) as *i64; w2[0]=0x3F800000
46 let rout: *i64=sys_mmap(8*8) as *i64
47 let t1: *i64=sys_mmap(8*8) as *i64; let t2: *i64=sys_mmap(8*8) as *i64
48 resblock1_1dil(x, w1, bz, w2, bz, rout, 1, 3, 1, 1, 0x3F000000, t1, t2)
49 cw("resblock out=["); chx(rout[0]&0xFFFFFFFF); cw(" "); chx(rout[1]&0xFFFFFFFF); cw(" "); chx(rout[2]&0xFFFFFFFF); cw("] (expect 40000000 40800000 c0700000 = 2 4 -3.75)\n" as *u8)
50 tot=tot+1
51 if eqf(rout[0],0x40000000)==1 { if eqf(rout[1],0x40800000)==1 { if eqf(rout[2],0xC0700000)==1 { pass=pass+1; cw("PASS resblock residual+2conv+2leaky composition byte-exact\n" as *u8) } else {cw("FAIL rb c\n")} } else {cw("FAIL rb b\n")} } else {cw("FAIL rb a\n")}
52
53 // weight_norm: v=[[3,4],[1,0]] (C_out=2,inner=2), g=[10,5] -> W=[6,8,5,0] (co0 norm5, co1 norm1)
54 let v: *i64=sys_mmap(8*8) as *i64; v[0]=0x40400000; v[1]=0x40800000; v[2]=0x3F800000; v[3]=0 // 3,4,1,0
55 let g: *i64=sys_mmap(4*8) as *i64; g[0]=0x41200000; g[1]=0x40A00000 // 10, 5
56 let wn: *i64=sys_mmap(8*8) as *i64
57 weight_norm_apply(v, g, wn, 2, 2)
58 cw("weight_norm W=["); chx(wn[0]&0xFFFFFFFF); cw(" "); chx(wn[1]&0xFFFFFFFF); cw(" "); chx(wn[2]&0xFFFFFFFF); cw(" "); chx(wn[3]&0xFFFFFFFF); cw("] (expect 40c00000 41000000 40a00000 00000000 = 6 8 5 0)\n" as *u8)
59 tot=tot+1
60 if eqf(wn[0],0x40C00000)==1 { if eqf(wn[1],0x41000000)==1 { if eqf(wn[2],0x40A00000)==1 { if eqf(wn[3],0)==1 { pass=pass+1; cw("PASS weight_norm reconstruction (g*V/||V|| per output channel) byte-exact\n" as *u8) } else {cw("FAIL wn d\n")} } else {cw("FAIL wn c\n")} } else {cw("FAIL wn b\n")} } else {cw("FAIL wn a\n")}
61
62 cw("nx_vocops_test pass="); cn(pass); cw("/"); cn(tot)
63 if pass==tot { cw(" GREEN -- HiFi-GAN forward bricks 1-3 (conv/upsample/resblock) verified. Remaining: tanh, weight-norm, graph, load-weights, run.\n" as *u8); sys_exit(0); return 0 }
64 cw(" RED\n" as *u8); sys_exit(1); return 1
65}