code wiki / _hdl_build / nx_nofloat_serve_gate.nx

nx_nofloat_serve_gate.nx source

↩ module page · 157 lines · 7898 B

1// nx_nofloat_serve_gate.nx -- gates the no-float LLM serve CORE in-process (pure-core+gate+daemon idiom: 2// no sockets here; synthetic HTTP requests straight into nsv_handle). Teeth: 3// T1 GET /health -> 200 + ok:1 (init fail-fast worked; model + both caches live) 4// T2 POST /gen "The capital of France is" (i32) -> 200 + READABLE " Paris. It is the" (real spaces -- 5// the byte-level-BPE decode; raw pieces would read "GParis." with G-dot 0xC4 0xA0) 6// T3 arbitrary second prompt -> 200 + gen_tokens>0 + NO 0xC4 0xA0 byte pair anywhere (decode total) 7// T4 mode i8 on the France prompt -> 200 + contains "Paris" (fast mode serves) 8// T5 GET /nope -> 404 (router honest) 9// Requires /home/elderwesto/nx_stage/nx_real_model.gguf. expect_exit: 0 license_tier: ORIGINAL 10import "nx_nofloat_serve_core.nx" 11 12func sg_puts(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func sg_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 } 14func sg_slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 15// substring search (needle NUL-terminated) in buf[0,n) 16func sg_has(buf: *u8, n: i64, needle: *u8) -> i64 { 17 let m: i64 = sg_slen(needle) 18 if m == 0 { return 1 } 19 var i: i64 = 0 20 while i + m <= n { 21 var k: i64 = 0 22 var ok: i64 = 1 23 while k < m { if buf[i+k] != needle[k] { ok = 0; k = m } else { k = k + 1 } } 24 if ok == 1 { return 1 } 25 i = i + 1 26 } 27 return 0 28} 29func sg_check(name: *u8, cond: i64) -> i64 { 30 if cond==1 { sg_puts(" PASS " as *u8) } else { sg_puts(" FAIL " as *u8) } 31 sg_puts(name) 32 sg_puts("\n" as *u8) 33 return cond 34} 35// drive one synthetic request through the core; returns response length. 36func sg_req(req: *u8, resb: *u8) -> i64 { 37 return nsv_handle(req, sg_slen(req), resb, 262144) 38} 39 40func main() -> i64 { 41 sg_puts("=== nx_nofloat_serve_gate: pure-core HTTP handler over the sovereign no-float LLM ===\n" as *u8) 42 var pass: i64 = 0 43 var total: i64 = 0 44 let t0: i64 = sys_now_ms() 45 let rc: i64 = nsv_init("/home/elderwesto/nx_stage/nx_real_model.gguf" as *u8) 46 let t1: i64 = sys_now_ms() 47 sg_puts(" [init] rc=" as *u8) 48 sg_pn(rc) 49 sg_puts(" (" as *u8) 50 sg_pn(t1-t0) 51 sg_puts(" ms: model + i32 cache + i8 cache + head)\n" as *u8) 52 if rc != 0 { sg_puts("NX-NOFLOAT-SERVE-GATE verdict=RED (init)\n" as *u8); return 1 } 53 54 let resb: *u8 = sys_mmap(262144) 55 // T1 health 56 let n1: i64 = sg_req("GET /health HTTP/1.1\r\nHost: x\r\n\r\n" as *u8, resb) 57 var ok1: i64 = 0 58 if sg_has(resb, n1, "200 OK" as *u8) == 1 { if sg_has(resb, n1, "\"ok\":1" as *u8) == 1 { ok1 = 1 } } 59 pass = pass + sg_check("T1 GET /health -> 200 ok:1" as *u8, ok1) 60 total = total + 1 61 // T2 France prompt, i32 lossless, readable text 62 let n2: i64 = sg_req("POST /gen HTTP/1.1\r\nHost: x\r\nContent-Type: application/json\r\nContent-Length: 62\r\n\r\n{\"prompt\":\"The capital of France is\",\"max_new\":6,\"mode\":\"i32\"}" as *u8, resb) 63 sg_puts(" [gen-i32] " as *u8) 64 var show: i64 = n2 65 if show > 300 { show = 300 } 66 var si: i64 = 0 67 var bodyat: i64 = 0 68 while si + 3 < n2 { if resb[si]==(13 as u8) { if resb[si+1]==(10 as u8) { if resb[si+2]==(13 as u8) { if resb[si+3]==(10 as u8) { bodyat = si + 4; si = n2 } } } } si = si + 1 } 69 sys_write(1, ((resb as i64)+bodyat) as *u8, n2-bodyat) 70 sg_puts("\n" as *u8) 71 var ok2: i64 = 0 72 if sg_has(resb, n2, "200 OK" as *u8) == 1 { if sg_has(resb, n2, " Paris. It is the" as *u8) == 1 { ok2 = 1 } } 73 pass = pass + sg_check("T2 POST /gen i32 -> 200 + READABLE ' Paris. It is the' (spaces real, BPE-decoded)" as *u8, ok2) 74 total = total + 1 75 // T3 arbitrary prompt + zero raw G-dot bytes (0xC4 0xA0) in the response 76 let n3: i64 = sg_req("POST /gen HTTP/1.1\r\nHost: x\r\nContent-Length: 44\r\n\r\n{\"prompt\":\"Water is made of\",\"max_new\":8}" as *u8, resb) 77 var ok3: i64 = 0 78 if sg_has(resb, n3, "200 OK" as *u8) == 1 { if sg_has(resb, n3, "\"gen_tokens\":" as *u8) == 1 { ok3 = 1 } } 79 var gdot: i64 = 0 80 var gi: i64 = 0 81 while gi + 1 < n3 { if resb[gi]==(196 as u8) { if resb[gi+1]==(160 as u8) { gdot = 1; gi = n3 } } gi = gi + 1 } 82 if gdot == 1 { ok3 = 0 } 83 pass = pass + sg_check("T3 arbitrary prompt -> 200 + tokens + NO raw G-dot bytes (decode is total)" as *u8, ok3) 84 total = total + 1 85 // T4 i8 fast mode 86 let n4: i64 = sg_req("POST /gen HTTP/1.1\r\nHost: x\r\nContent-Length: 61\r\n\r\n{\"prompt\":\"The capital of France is\",\"max_new\":4,\"mode\":\"i8\"}" as *u8, resb) 87 var ok4: i64 = 0 88 if sg_has(resb, n4, "200 OK" as *u8) == 1 { if sg_has(resb, n4, "Paris" as *u8) == 1 { if sg_has(resb, n4, "\"mode\":\"i8\"" as *u8) == 1 { ok4 = 1 } } } 89 pass = pass + sg_check("T4 mode i8 -> 200 + Paris + mode echoed" as *u8, ok4) 90 total = total + 1 91 // T5 unknown route 92 let n5: i64 = sg_req("GET /nope HTTP/1.1\r\nHost: x\r\n\r\n" as *u8, resb) 93 var ok5: i64 = 0 94 if sg_has(resb, n5, "404" as *u8) == 1 { ok5 = 1 } 95 pass = pass + sg_check("T5 GET /nope -> 404" as *u8, ok5) 96 total = total + 1 97 // T6 SSE streaming: drive nsv_handle_stream with a FILE fd (same bytes a socket would get), then 98 // read the file back and assert the event-stream shape + the first streamed piece. 99 let sfd: i64 = sys_openat_wr("/tmp/nsv_sse_test.out" as *u8, 0x1a4) 100 let sreq: *u8 = "POST /gen HTTP/1.1\r\nHost: x\r\nContent-Length: 57\r\n\r\n{\"prompt\":\"The capital of France is\",\"max_new\":4,\"stream\":1}" as *u8 101 let hs: i64 = nsv_handle_stream(sfd, sreq, sg_slen(sreq)) 102 sys_close(sfd) 103 let slen_p: *i64 = sys_mmap(16) as *i64 104 let sbuf: *u8 = sys_read_file("/tmp/nsv_sse_test.out" as *u8, slen_p) 105 var ok6: i64 = 0 106 if hs == 1 { if (sbuf as i64) != 0 { 107 if sg_has(sbuf, slen_p[0], "text/event-stream" as *u8) == 1 { 108 if sg_has(sbuf, slen_p[0], "\"piece\":\" Paris\"" as *u8) == 1 { 109 if sg_has(sbuf, slen_p[0], "\"done\":1,\"ok\":1" as *u8) == 1 { ok6 = 1 } 110 } 111 } 112 } } 113 pass = pass + sg_check("T6 SSE stream: event-stream headers + piece ' Paris' + done frame" as *u8, ok6) 114 total = total + 1 115 // T7 seeded sampling is DETERMINISTIC: same prompt+temp+seed twice -> byte-identical text (the 116 // no-float exceed extended to sampling: the seed is part of the input). 117 let pr7: *u8 = "The capital of France is" as *u8 118 let tx1: *u8 = sys_mmap(8192) 119 let tx2: *u8 = sys_mmap(8192) 120 let mt7: *i64 = sys_mmap(8*8) as *i64 121 let gp7: *i64 = sys_mmap(16*8) as *i64 122 gp7[0]=pr7 as i64 123 gp7[1]=sg_slen(pr7) 124 gp7[2]=6 125 gp7[3]=0 126 gp7[4]=tx1 as i64 127 gp7[5]=8000 128 gp7[6]=mt7 as i64 129 gp7[7]=0-1 130 gp7[8]=800 131 gp7[9]=950 132 gp7[10]=64 133 gp7[11]=42 134 let l1: i64 = nsv_generate(gp7) 135 gp7[4]=tx2 as i64 136 let l2: i64 = nsv_generate(gp7) 137 var ok7: i64 = 0 138 if l1 > 0 { if l1 == l2 { 139 var eq: i64 = 1 140 var ci7: i64 = 0 141 while ci7 < l1 { if tx1[ci7] != tx2[ci7] { eq = 0; ci7 = l1 } else { ci7 = ci7 + 1 } } 142 if eq == 1 { ok7 = 1 } 143 } } 144 sg_puts(" [sample temp=0.8 seed=42] '" as *u8) 145 if l1 > 0 { sys_write(1, tx1, l1) } 146 sg_puts("'\n" as *u8) 147 pass = pass + sg_check("T7 seeded sampling deterministic: same seed -> byte-identical text" as *u8, ok7) 148 total = total + 1 149 150 sg_puts("NX-NOFLOAT-SERVE-GATE passed " as *u8) 151 sg_pn(pass) 152 sg_puts("/" as *u8) 153 sg_pn(total) 154 if pass == total { sg_puts(" verdict=GREEN (serve core: readable text, both modes, honest routes)\n" as *u8); return 0 } 155 sg_puts(" verdict=RED\n" as *u8) 156 return 1 157}