code wiki / (root) / nx_hifigan_weights_fetch.nx

nx_hifigan_weights_fetch.nx source

↩ module page · 55 lines · 3886 B

1// nx_hifigan_weights_fetch.nx -- fetch the REAL ~50MB HiFi-GAN model.safetensors from HuggingFace over our own 2// TLS (the last unknown: does our client reach HF's CDN for a big binary?). Uses the proven high-level fetcher 3// (connect+TLS+redirect) with a 64MB buffer, saves to the model stage, and confirms it's a valid safetensors 4// (first 8 bytes = header length, plausible < filesize). If GREEN, the neural vocoder has its weights and the 5// assembled nx_hifigan_gen graph can run for REAL. expect_exit: 0 license_tier: ORIGINAL 6import "nx_syscalls.nx" 7import "nx_x509_trust_store.nx" 8import "nx_trust_store_load_from_certdata.nx" 9import "nx_https_fetch_follow.nx" 10const K_MAGIC_4194304: i64 = 4194304 11const K_MAGIC_134217728: i64 = 134217728 12 13func p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 14func 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 } 15 16func main() -> i64 { 17 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304) 18 if r<=0 { p("certdata load failed\n" as *u8); return 1 } 19 let store: *TrustStore = r as *TrustStore 20 p("CA roots="); pn(trust_store_count(store)); p("\n" as *u8) 21 let cap: i64 = K_MAGIC_134217728 // 128MB (the .bin may be up to ~96MB) 22 let out: *u8 = sys_mmap(cap) 23 let opath: *u8 = "/home/elderwesto/nx_stage/hifigan_pytorch_model.bin" as *u8 24 25 let fd0: i64 = sys_openat_rd(opath) 26 if fd0 >= 0 { sys_close(fd0); p("[have-skip] weights already on stage\n" as *u8) } 27 else { 28 p("fetching pytorch_model.bin (~50-96MB over our TLS -- may take a while)...\n" as *u8) 29 let status: *i64 = sys_mmap(8) as *i64 30 let n: i64 = nx_https_fetch_follow("https://huggingface.co/microsoft/speecht5_hifigan/resolve/main/pytorch_model.bin" as *u8, store, out, cap, 6, status) 31 p("status="); pn(status[0]); p(" bytes="); pn(n); p("\n" as *u8) 32 if n<=0 { p("verdict=FAIL (fetch returned nothing -- CDN/redirect/size issue)\n" as *u8); sys_exit(1); return 1 } 33 var gz: i64=0; if n>=2 { if out[0]==0x1f as u8 { if out[1]==0x8b as u8 { gz=1 } } } 34 if gz==1 { p("verdict=FAIL (gzip -- need identity encoding)\n" as *u8); sys_exit(1); return 1 } 35 let wfd: i64 = sys_openat_wr(opath, 0x1a4) 36 if wfd<0 { p("verdict=FAIL (save)\n" as *u8); sys_exit(1); return 1 } 37 sys_write(wfd, out, n); sys_close(wfd) 38 p("SAVED "); pn(n); p(" bytes -> "); p(opath); p("\n" as *u8) 39 } 40 41 // confirm format: PyTorch .bin is a ZIP archive (magic PK\x03\x04). Peek the first bytes. 42 let vfd: i64 = sys_openat_rd(opath) 43 if vfd<0 { p("verdict=FAIL (reopen)\n" as *u8); sys_exit(1); return 1 } 44 let hdr8: *u8 = sys_mmap(64) 45 let rd: i64 = sys_read(vfd, hdr8, 32) 46 sys_close(vfd) 47 p("first bytes: "); var i: i64=0; while i<8 { let bb: *u8=sys_mmap(4); var nib: i64=(hdr8[i]>>4)&0xF; if nib<10 {bb[0]=(48+nib) as u8} else {bb[0]=(87+nib) as u8} nib=hdr8[i]&0xF; if nib<10 {bb[1]=(48+nib) as u8} else {bb[1]=(87+nib) as u8} bb[2]=32 as u8; sys_write(1,bb,3); i=i+1 } p("\n" as *u8) 48 let is_zip: i64 = 0 49 if hdr8[0]==0x50 as u8 { if hdr8[1]==0x4B as u8 { if hdr8[2]==0x03 as u8 { if hdr8[3]==0x04 as u8 { 50 p("verdict=GREEN (real weights on stage; format = ZIP/pickle [PK..] as expected for PyTorch .bin -> sovereign loader = ZIP-reader + pickle-VM over THIS file, feeding the already-built nx_hifigan_gen graph)\n" as *u8) 51 sys_exit(0); return 0 52 } } } } 53 p("verdict=NOTE (downloaded, but not the ZIP magic -- inspect first bytes above)\n" as *u8) 54 sys_exit(1); return 1 55}