code wiki / _hdl_build / nx_nofloat_serve.nx

nx_nofloat_serve.nx source

↩ module page · 153 lines · 6979 B

1// nx_nofloat_serve.nx -- the DEPLOYABLE no-float LLM daemon (ops shell around the gated pure core 2// nx_nofloat_serve_core). Binds 127.0.0.1:8032 and serves the interactive app + JSON API for the sovereign 3// 100pct-integer Qwen2.5-0.5B: POST /gen {prompt,max_new,mode} -> readable text (byte-level-BPE decoded), 4// GET / app page, GET /health, GET /api. FAIL-FAST startup (Rule 20): the model is loaded and BOTH 5// dequant-once caches are built BEFORE bind/listen -- a daemon that accepts is a daemon that can serve. 6// Single accept loop = requests naturally serialize (one 16-core box, one pool). WSL2 forwards localhost 7// to Windows, so the app is browsable at http://localhost:8032/ on the host. Runs forever; build with 8// --build-only, run deliberately (same pattern as nx_relate_daemon :8027). license_tier: ORIGINAL 9import "nx_nofloat_serve_core.nx" 10const NSD_MAGIC_131072: i64 = 131072 11const NSD_MAGIC_262144: i64 = 262144 12 13const NSD_PORT: i64 = 0x1f60 // 8032 14const NSD_MODEL: *u8 = "/home/elderwesto/nx_stage/nx_real_model.gguf" 15 16func nsd_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 17func nsd_pn(v: i64) -> i64 { let b: *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{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 18 19func nsd_addr(out: *u8, port: i64) -> i64 { 20 out[0] = 2 as u8 21 out[1] = 0 as u8 22 out[2] = ((port >> 8) & 0xff) as u8 23 out[3] = (port & 0xff) as u8 24 out[4] = 127 as u8 25 out[5] = 0 as u8 26 out[6] = 0 as u8 27 out[7] = 1 as u8 28 var i: i64 = 8 29 while i < 16 { out[i] = 0 as u8; i = i + 1 } 30 return 0 31} 32func nsd_write_all(fd: i64, buf: *u8, n: i64) -> i64 { 33 var w: i64 = 0 34 while w < n { let k: i64 = sys_write(fd, (buf as i64 + w) as *u8, n - w); if k <= 0 { return 0 - 1 } w = w + k } 35 return 0 36} 37// read ONE full HTTP request: until CRLFCRLF, plus Content-Length body bytes if present. returns total. 38func nsd_read_req(fd: i64, buf: *u8, cap: i64) -> i64 { 39 var n: i64 = 0 40 var hdr_end: i64 = 0 - 1 41 var want: i64 = 0 - 1 42 var go: i64 = 1 43 while go == 1 { 44 if n >= cap - 1 { go = 0 } else { 45 let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, cap - 1 - n) 46 if r <= 0 { go = 0 } else { 47 n = n + r 48 if hdr_end < 0 { 49 var i: i64 = 0 50 while i + 3 < n { 51 if buf[i] == (13 as u8) { if buf[i+1] == (10 as u8) { if buf[i+2] == (13 as u8) { if buf[i+3] == (10 as u8) { hdr_end = i + 4; i = n } } } } 52 i = i + 1 53 } 54 if hdr_end >= 0 { 55 var cl: i64 = 0 - 1 56 let key: *u8 = "Content-Length:" as *u8 57 let kl: i64 = 15 58 var j: i64 = 0 59 while j + kl < hdr_end { 60 var k2: i64 = 0 61 var ok: i64 = 1 62 while k2 < kl { if buf[j+k2] != key[k2] { ok = 0; k2 = kl } else { k2 = k2 + 1 } } 63 if ok == 1 { 64 var p2: i64 = j + kl 65 cl = 0 66 var num: i64 = 1 67 while num == 1 { 68 if p2 >= hdr_end { num = 0 } else { 69 let c: i64 = buf[p2] & 0xff 70 if c == 32 { p2 = p2 + 1 } else { 71 if c >= 48 { if c <= 57 { cl = cl * 10 + (c - 48); p2 = p2 + 1 } else { num = 0 } } else { num = 0 } 72 } 73 } 74 } 75 j = hdr_end 76 } 77 j = j + 1 78 } 79 if cl >= 0 { want = hdr_end + cl } else { want = hdr_end } 80 } 81 } 82 if want >= 0 { if n >= want { go = 0 } } 83 } 84 } 85 } 86 return n 87} 88 89// argv (both optional, defaults = the original :8032 instruct contract -- no hardcode, one binary 90// serves any model/port pair): argv[1]=model gguf path, argv[2]=port. The CODER serve for the forge 91// runs the SAME elf as `nx_nofloat_serve.sov.elf <nx_coder_model.gguf> 8033` (port registry: 92// 8031 survey, 8032 instruct app, 11434/11438 f32 seats -> 8033 = coder no-float serve). 93func main(argc: i64, argv: *i64) -> i64 { 94 var model: *u8 = NSD_MODEL 95 var port: i64 = NSD_PORT 96 if argc >= 2 { model = argv[1] as *u8 } 97 if argc >= 3 { 98 let ps: *u8 = argv[2] as *u8 99 var pv: i64 = 0 100 var pi: i64 = 0 101 while ps[pi] != (0 as u8) { 102 let c: i64 = ps[pi] & 0xff 103 if c >= 48 { if c <= 57 { pv = pv * 10 + (c - 48) } } 104 pi = pi + 1 105 } 106 if pv > 0 { port = pv } 107 } 108 nsd_puts("NX-NOFLOAT-SERVE loading model + building dequant-once caches (fail-fast before bind)...\n" as *u8) 109 nsd_puts(" model=" as *u8) 110 nsd_puts(model) 111 nsd_puts(" port=" as *u8) 112 nsd_pn(port) 113 nsd_puts("\n" as *u8) 114 let rc: i64 = nsv_init(model) 115 if rc != 0 { 116 nsd_puts("NX-NOFLOAT-SERVE init FAILED rc=" as *u8) 117 nsd_pn(rc) 118 nsd_puts(" (model missing/unparseable or cache OOM) -- fail closed, fail loud\n" as *u8) 119 return 1 120 } 121 let addr: *u8 = sys_mmap(16) 122 nsd_addr(addr, port) 123 let lfd: i64 = sys_socket(AF_INET, SOCK_STREAM, 0) 124 if lfd < 0 { nsd_puts("NX-NOFLOAT-SERVE socket FAILED\n" as *u8); return 1 } 125 if sys_bind(lfd, addr, 16) < 0 { 126 nsd_puts("NX-NOFLOAT-SERVE bind 127.0.0.1:" as *u8) 127 nsd_pn(port) 128 nsd_puts(" FAILED (port busy?)\n" as *u8) 129 return 1 130 } 131 if sys_listen(lfd, 16) < 0 { nsd_puts("NX-NOFLOAT-SERVE listen FAILED\n" as *u8); return 1 } 132 nsd_puts("NX-NOFLOAT-SERVE ready http://127.0.0.1:" as *u8) 133 nsd_pn(port) 134 nsd_puts("/ (modes i32 lossless / i8 fast; /gen /embed /health /api; accept loop)\n" as *u8) 135 let reqb: *u8 = sys_mmap(NSD_MAGIC_131072) 136 let resb: *u8 = sys_mmap(NSD_MAGIC_262144) 137 var go: i64 = 1 138 while go == 1 { 139 let cfd: i64 = sys_accept(lfd) 140 if cfd >= 0 { 141 let rn: i64 = nsd_read_req(cfd, reqb, NSD_MAGIC_131072) 142 if rn > 0 { 143 // streaming route first (SSE writes straight to the socket); 0 = not-a-stream -> buffered path 144 if nsv_handle_stream(cfd, reqb, rn) == 0 { 145 let on: i64 = nsv_handle(reqb, rn, resb, NSD_MAGIC_262144) 146 nsd_write_all(cfd, resb, on) 147 } 148 } 149 sys_close(cfd) 150 } 151 } 152 return 0 153}