interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / om / gpu / q36 / serve / serve_run.om

1146 lines · 22325 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note 100% Pure Seeded-Omega Zero-CUDA Real-Weight Qwen 3.6 35B-A3B HTTP Server + Live Arboris (q36-serve).
3note ZERO libcuda.so.1, ZERO libc, ZERO Python, ZERO C, ZERO Rust.
4note Defaults to Full-W4 mode (67.76 tok/s, ~19.5 GB VRAM); pass --fp8 for Exact FP8 (48.01 tok/s).
5note Endpoints:
6note GET /health (status, model, mode, live Arboris transitions/hits/hit_pct)
7note GET /v1/models (OpenAI model list: qwen3.6-35b-a3b-fp8)
8note POST /v1/chat/completions (OpenAI chat completion with live Arboris tracking)
9note
10note Usage:
11note q36-serve [--model-dir PATH] [--port PORT] [--fp8 | --w4] [--self-test]
12
13global qsReqBuf uchar 524288
14global qsRespBuf uchar 262144
15global qsRespLen ulong 1
16global qsOutTxt uchar 131072
17global qsOutLen ulong 1
18global qsPort ulong 1
19global qsSelfTest ulong 1
20global qsPendPreArb ulong 1
21global qsStreamFd int 1
22
23text qsDefDir "/home/drakestapleton/models/qwen3.6-35b-a3b-fp8-95a723d0"
24text qsSufTok "/tokenizer.json"
25text qsBanner "=== QWEN 3.6 35B-A3B 100% PURE SEEDED-OMEGA ZERO-CUDA SERVER + LIVE ARBORIS (q36-serve) ===\n"
26text qsModeW4 "Mode: Full-W4 (67.76 tok/s class, ~19.5 GB VRAM) + Live Arboris\n"
27text qsModeFp8 "Mode: Exact FP8 (48.01 tok/s class, ~34.7 GB VRAM) + Live Arboris\n"
28text qsTokOk "Loaded Qwen 3.6 ByteLevel BPE tokenizer (248,044 vocab)\n"
29text qsTokFail "FAIL: could not load tokenizer.json\n"
30text qsListenMsg "Listening on HTTP port "
31text qsListenEnd " (GET /health, GET /v1/models, POST /v1/chat/completions)\n"
32text qsSelfHdr "Running live real-weight q36-serve + Arboris self-test...\n"
33text qsP0 "What is the capital of France?"
34text qsP1 "What is 2 + 2?"
35text qsL0 "Prompt 0: "
36text qsL1 "Prompt 1: "
37text qsArbLbl "Live Arboris: hits="
38text qsArbMid " / tot="
39text qsArbPct " hit_pct="
40text qsSelfOk "PASS: q36-serve real-weight 35B + live Arboris self-test\n"
41text qsSelfBad "FAIL: q36-serve self-test\n"
42
43text qsH200 "HTTP/1.1 200 OK\nContent-Type: application/json\nConnection: close\n\n"
44text qsH200Sse "HTTP/1.1 200 OK\nContent-Type: text/event-stream\nCache-Control: no-cache\nConnection: close\n\n"
45text qsH404 "HTTP/1.1 404 Not Found\nContent-Type: application/json\nConnection: close\n\n{\"error\":{\"type\":\"not_found\"}}\n"
46text qsHealthA "{\"status\":\"ok\",\"model\":\"qwen3.6-35b-a3b-fp8\",\"engine\":\"omega-zero-cuda\",\"context_window\":131072,\"precision\":\""
47text qsPrecW4 "w4"
48text qsPrecFp8 "fp8"
49text qsHealthB "\",\"arboris_enabled\":true,\"arboris_hits\":"
50text qsHealthC ",\"arboris_total\":"
51text qsHealthD ",\"arboris_hit_pct\":"
52text qsHealthE "}\n"
53text qsModels "{\"object\":\"list\",\"data\":[{\"id\":\"qwen3.6-35b-a3b-fp8\",\"object\":\"model\",\"owned_by\":\"omega-zero-cuda\",\"context_window\":131072}]}\n"
54text qsChatA "{\"id\":\"cmpl-q36\",\"object\":\"chat.completion\",\"model\":\"qwen3.6-35b-a3b-fp8\",\"choices\":[{\"index\":0,\"message\":{\"role\":\"assistant\",\"content\":\""
55text qsChatB "\"},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":"
56text qsChatC ",\"completion_tokens\":"
57text qsChatD ",\"total_tokens\":"
58text qsChatE "},\"arboris\":{\"hits\":"
59text qsChatF ",\"total\":"
60text qsChatG ",\"hit_pct\":"
61text qsChatH "},\"timings\":{\"prefill_ms\":"
62text qsChatI ",\"decode_ms\":"
63text qsChatJ "}}\n"
64text qsSseData "data: "
65text qsSseDeltaA "{\"id\":\"cmpl-q36\",\"object\":\"chat.completion.chunk\",\"model\":\"qwen3.6-35b-a3b-fp8\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\""
66text qsSseDeltaB "\"},\"finish_reason\":null}]}\n\n"
67text qsSseFinalA "{\"id\":\"cmpl-q36\",\"object\":\"chat.completion.chunk\",\"model\":\"qwen3.6-35b-a3b-fp8\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":"
68text qsSseDone "data: [DONE]\n\n"
69text qsKeyContent "\"content\""
70text qsKeyMaxTok "\"max_tokens\""
71text qsKeyStream "\"stream\""
72text qsKeyClen "content-length:"
73
74fn qsRespReset to void
75put qsRespLen 0 0
76end
77
78fn qsRespAppendHdr s view uchar * n ulong to void
79p = get qsRespLen 0
80for i ulong n bound 512
81c = get s i
82if and eq c 10 lt p 65520
83put qsRespBuf p 13
84p = add p 1
85end
86if lt p 262100
87put qsRespBuf p c
88p = add p 1
89end
90end
91put qsRespLen 0 p
92end
93
94fn qsRespAppend s view uchar * n ulong to void
95p = get qsRespLen 0
96for i ulong n bound 16384
97if lt p 262100
98put qsRespBuf p get s i
99p = add p 1
100end
101end
102put qsRespLen 0 p
103end
104
105fn qsRespAppendU v ulong to void
106tmp = new uchar 24 0
107p = get qsRespLen 0
108if eq v 0
109if lt p 262100
110put qsRespBuf p 48
111put qsRespLen 0 add p 1
112end
113ret
114end
115n = ulong 0
116x = v
117while gt x 0 bound 24
118put tmp n uchar add 48 rem x 10
119x = div x 10
120n = add n 1
121end
122for i ulong n bound 24
123if lt p 262100
124put qsRespBuf p get tmp sub sub n 1 i
125p = add p 1
126end
127end
128put qsRespLen 0 p
129end
130
131fn qsRespAppendEscByte c uchar to void
132p = get qsRespLen 0
133if ge p 262096
134ret
135end
136if or eq c 34 eq c 92
137put qsRespBuf p 92
138put qsRespBuf add p 1 c
139put qsRespLen 0 add p 2
140ret
141end
142if eq c 10
143put qsRespBuf p 92
144put qsRespBuf add p 1 110
145put qsRespLen 0 add p 2
146ret
147end
148if eq c 13
149put qsRespBuf p 92
150put qsRespBuf add p 1 114
151put qsRespLen 0 add p 2
152ret
153end
154if eq c 9
155put qsRespBuf p 92
156put qsRespBuf add p 1 116
157put qsRespLen 0 add p 2
158ret
159end
160if eq c 8
161put qsRespBuf p 92
162put qsRespBuf add p 1 98
163put qsRespLen 0 add p 2
164ret
165end
166if eq c 12
167put qsRespBuf p 92
168put qsRespBuf add p 1 102
169put qsRespLen 0 add p 2
170ret
171end
172if ge c 32
173put qsRespBuf p c
174put qsRespLen 0 add p 1
175end
176end
177
178fn qsRespAppendJsonEsc n ulong to void
179for i ulong n bound 131072
180qsRespAppendEscByte get qsOutTxt i
181end
182end
183
184fn qsSendSseDelta fd int tid ulong to void
185if ge tid Q36_VOCAB_MAX
186ret
187end
188l = ulong get q36TokLen tid
189if eq l 0
190ret
191end
192off = ulong get q36TokOff tid
193qsRespReset
194qsRespAppend qsSseData len qsSseData
195qsRespAppend qsSseDeltaA len qsSseDeltaA
196for i ulong l bound 64
197qsRespAppendEscByte get q36TokPool add off i
198end
199qsRespAppend qsSseDeltaB len qsSseDeltaB
200w = write fd qsRespBuf 0 get qsRespLen 0
201end
202
203fn qsAppendTok tid ulong to void
204if ge tid Q36_VOCAB_MAX
205ret
206end
207l = ulong get q36TokLen tid
208if eq l 0
209ret
210end
211off = ulong get q36TokOff tid
212cur = get qsOutLen 0
213for i ulong l bound 64
214if lt cur 131000
215put qsOutTxt cur get q36TokPool add off i
216cur = add cur 1
217end
218end
219put qsOutLen 0 cur
220end
221
222fn qsArbHitPct to ulong
223tot = get qdArbTot 0
224if eq tot 0
225ret 0
226end
227ret div mul get qdArbHits 0 100 tot
228end
229
230fn qsPrefill nTok ulong to ulong
231if and eq get qdUseW4 0 1 ge nTok 2
232pos = ulong 0
233prevChunk = ulong 0
234hasPrev = false
235while lt pos nTok bound 1024
236chunk = sub nTok pos
237if gt chunk QP_MAX_B
238chunk = QP_MAX_B
239end
240isFinal = eq add pos chunk nTok
241if hasPrev
242if not qdWaitPre
243ret 999999
244end
245okSnap = qdPreSnapTrace prevChunk
246for j ulong chunk bound 512
247put qdPreTokIds j get q36TokOutIds add pos j
248end
249if not qdPreKickChunk chunk pos isFinal
250ret 999999
251end
252if okSnap
253qdPreUpdateArborisSnap prevChunk
254end
255else
256for j ulong chunk bound 512
257put qdPreTokIds j get q36TokOutIds add pos j
258end
259if not qdPreKickChunk chunk pos isFinal
260ret 999999
261end
262end
263prevChunk = chunk
264hasPrev = true
265pos = add pos chunk
266end
267if not qdWaitPre
268ret 999999
269end
270put qsPendPreArb 0 0
271if qdPreSnapTrace prevChunk
272put qsPendPreArb 0 prevChunk
273end
274ret qdPrePickBest prevChunk false
275end
276put qsPendPreArb 0 0
277nextTok = ulong 0
278for p ulong nTok bound 131072
279isLast = eq add p 1 nTok
280nextTok = qdForwardToken ulong get q36TokOutIds p p isLast false
281if eq nextTok 999999
282ret 999999
283end
284end
285ret nextTok
286end
287
288fn qsFlushPendPreArb to void
289n = get qsPendPreArb 0
290if gt n 0
291put qsPendPreArb 0 0
292qdPreUpdateArborisSnap n
293end
294end
295
296fn qsEmitTokStep tid ulong to void
297qsAppendTok tid
298sfd = get qsStreamFd 0
299if ge sfd 0
300qsSendSseDelta sfd tid
301end
302end
303
304fn qsIsStopTok tid ulong to bool
305if eq tid 248046
306ret true
307end
308ret eq tid 248044
309end
310
311global qsSpecCnt ulong 2
312
313fn qsSpecEmitAcc acc ulong cap ulong to bool
314g = get qsSpecCnt 0
315for k ulong acc bound 16
316tokK = get qdSpecPred k
317if qsIsStopTok tokK
318put qsSpecCnt 0 g
319ret true
320end
321if lt g cap
322qsEmitTokStep tokK
323g = add g 1
324h2 = get qdHistLen 0
325if lt h2 131070
326put qdHistToks h2 tokK
327put qdHistLen 0 add h2 1
328end
329end
330end
331put qsSpecCnt 0 g
332ret false
333end
334
335fn qsDecodeLoopSpec firstTok ulong nTok ulong cap ulong to ulong
336M = get qdSpecM 0
337qsFlushPendPreArb
338hl = nTok
339if gt hl 131000
340hl = ulong 131000
341end
342for p ulong hl bound 131072
343put qdHistToks p ulong get q36TokOutIds p
344end
345put qdHistToks hl firstTok
346put qdHistLen 0 add hl 1
347qsEmitTokStep firstTok
348put qsSpecCnt 0 1
349curTok = firstTok
350pos = nTok
351while lt get qsSpecCnt 0 cap bound 4096
352if eq get qdMtpReady 0 1
353if not qdMtpDraftChain curTok sub pos 1 M
354qdSpecDraftNgram curTok M
355end
356else
357qdSpecDraftNgram curTok M
358end
359acc = qdSpecVerifyStep M pos
360if eq acc 0
361break
362end
363if qsSpecEmitAcc acc cap
364break
365end
366if eq get qdMtpReady 0 1
367okH = qdMtpCopyMainHid acc
368end
369curTok = get qdSpecPred sub acc 1
370pos = add pos acc
371end
372ret get qsSpecCnt 0
373end
374
375fn qsDecodeLoop firstTok ulong nTok ulong maxGen ulong to ulong
376cap = maxGen
377if gt add nTok cap QD_MAX_T
378cap = sub QD_MAX_T nTok
379end
380if or eq cap 0 qsIsStopTok firstTok
381qsFlushPendPreArb
382ret 0
383end
384if and eq get qdUseW4 0 1 gt get qdSpecM 0 0
385ret qsDecodeLoopSpec firstTok nTok cap
386end
387qsEmitTokStep firstTok
388genCnt = ulong 1
389if ge genCnt cap
390qsFlushPendPreArb
391ret genCnt
392end
393if not qdPrepAsyncDecode firstTok nTok
394qsFlushPendPreArb
395ret genCnt
396end
397if not qdKickPre 2
398qsFlushPendPreArb
399ret genCnt
400end
401note Overlap final prefill chunk Live Arboris update behind decode step 1 on GPU!
402qsFlushPendPreArb
403for g ulong sub cap 1 bound 16384
404if not qdWaitPre
405break
406end
407nextTok = qdReadOutTok
408if eq nextTok 999999
409break
410end
411okSnap = qdSnapTrace
412if qsIsStopTok nextTok
413if okSnap
414qdUpdateArborisSnap
415end
416break
417end
418if lt add g 2 cap
419if not qdKickPre 2
420if okSnap
421qdUpdateArborisSnap
422end
423qsEmitTokStep nextTok
424genCnt = add genCnt 1
425break
426end
427end
428if okSnap
429qdUpdateArborisSnap
430end
431qsEmitTokStep nextTok
432genCnt = add genCnt 1
433end
434ret genCnt
435end
436
437fn qsGenerate tlen ulong maxGen ulong outCounts edit ulong 4 to bool
438qdResetState
439nTok = q36TokEncodeChatGlobal tlen
440if eq nTok 0
441ret false
442end
443put outCounts 0 nTok
444put qsOutLen 0 0
445t0 = monons
446firstTok = qsPrefill nTok
447if eq firstTok 999999
448ret false
449end
450t1 = monons
451genCnt = qsDecodeLoop firstTok nTok maxGen
452t2 = monons
453put outCounts 1 genCnt
454put outCounts 2 div ulong sub t1 t0 1000000
455put outCounts 3 div ulong sub t2 t1 1000000
456ret true
457end
458
459fn qsMatchKey kbuf view uchar * pos ulong klen ulong to bool
460for j ulong klen bound 16
461if ne get qsReqBuf add pos j get kbuf j
462ret false
463end
464end
465ret true
466end
467
468fn qsScanQuoted p0 ulong n ulong outBounds edit ulong 2 to bool
469p = p0
470while and lt p n ne get qsReqBuf p 34 bound 64
471p = add p 1
472end
473if ge p n
474ret false
475end
476st = add p 1
477q = st
478while and lt q n ne get qsReqBuf q 34 bound 524288
479if eq get qsReqBuf q 92
480q = add q 2
481else
482q = add q 1
483end
484end
485if gt q st
486put outBounds 0 st
487put outBounds 1 sub q st
488ret true
489end
490ret false
491end
492
493fn qsFindLastContent n ulong outBounds edit ulong 2 to bool
494put outBounds 0 0
495put outBounds 1 0
496found = false
497if lt n 14
498ret false
499end
500lim = sub n 10
501for i ulong lim bound 524288
502if qsMatchKey qsKeyContent i 9
503if qsScanQuoted add i 9 n outBounds
504found = true
505end
506end
507end
508ret found
509end
510
511fn qsParseReqU p0 ulong n ulong to ulong
512p = p0
513while and lt p n or eq get qsReqBuf p 32 eq get qsReqBuf p 58 bound 32
514p = add p 1
515end
516v = ulong 0
517while lt p n bound 16
518c = get qsReqBuf p
519if or lt c 48 gt c 57
520break
521end
522v = add mul v 10 ulong sub c 48
523p = add p 1
524end
525ret v
526end
527
528fn qsFindMaxTokens n ulong to ulong
529if lt n 16
530ret 512
531end
532lim = sub n 13
533for i ulong lim bound 524288
534if qsMatchKey qsKeyMaxTok i 12
535v = qsParseReqU add i 12 n
536if and gt v 0 le v 16384
537ret v
538end
539end
540end
541ret 512
542end
543
544fn qsHexNib c uchar to ulong
545if and ge c 48 le c 57
546ret ulong sub c 48
547end
548if and ge c 97 le c 102
549ret ulong add 10 sub c 97
550end
551if and ge c 65 le c 70
552ret ulong add 10 sub c 65
553end
554ret 16
555end
556
557fn qsDecodeHexU4 p ulong to long
558h0 = qsHexNib get qsReqBuf p
559h1 = qsHexNib get qsReqBuf add p 1
560h2 = qsHexNib get qsReqBuf add p 2
561h3 = qsHexNib get qsReqBuf add p 3
562if or or ge h0 16 ge h1 16 or ge h2 16 ge h3 16
563ret -1
564end
565ret long or or shl h0 12 shl h1 8 or shl h2 4 h3
566end
567
568fn qsEmitUtf8Cp cp ulong opos ulong to ulong
569if lt cp 128
570put q36TokInBuf opos uchar cp
571ret add opos 1
572end
573if lt cp 2048
574if lt add opos 1 520000
575put q36TokInBuf opos uchar or 192 shr cp 6
576put q36TokInBuf add opos 1 uchar or 128 and cp 63
577ret add opos 2
578end
579ret opos
580end
581if lt add opos 2 520000
582put q36TokInBuf opos uchar or 224 shr cp 12
583put q36TokInBuf add opos 1 uchar or 128 and shr cp 6 63
584put q36TokInBuf add opos 2 uchar or 128 and cp 63
585ret add opos 3
586end
587ret opos
588end
589
590fn qsCopyUnescapedContent st ulong clen ulong to ulong
591opos = ulong 0
592i = ulong 0
593while lt i clen bound 520000
594if ge opos 520000
595break
596end
597c = get qsReqBuf add st i
598if and eq c 92 lt add i 1 clen
599nx = get qsReqBuf add st add i 1
600i = add i 2
601if eq nx 110
602put q36TokInBuf opos 10
603opos = add opos 1
604continue
605end
606if eq nx 114
607put q36TokInBuf opos 13
608opos = add opos 1
609continue
610end
611if eq nx 116
612put q36TokInBuf opos 9
613opos = add opos 1
614continue
615end
616if eq nx 98
617put q36TokInBuf opos 8
618opos = add opos 1
619continue
620end
621if eq nx 102
622put q36TokInBuf opos 12
623opos = add opos 1
624continue
625end
626if and eq nx 117 le add i 4 clen
627cp = qsDecodeHexU4 add st i
628if ge cp 0
629i = add i 4
630opos = qsEmitUtf8Cp ulong cp opos
631continue
632end
633end
634put q36TokInBuf opos nx
635opos = add opos 1
636else
637put q36TokInBuf opos c
638opos = add opos 1
639i = add i 1
640end
641end
642ret opos
643end
644
645fn qsIsWsColon b uchar to bool
646if or eq b 32 eq b 9
647ret true
648end
649ret eq b 58
650end
651
652fn qsIsTrueAt p ulong n ulong to bool
653if gt add p 4 n
654ret false
655end
656if ne get qsReqBuf p 116
657ret false
658end
659if ne get qsReqBuf add p 1 114
660ret false
661end
662if ne get qsReqBuf add p 2 117
663ret false
664end
665ret eq get qsReqBuf add p 3 101
666end
667
668fn qsFindStream n ulong to bool
669if lt n 14
670ret false
671end
672lim = sub n 9
673for i ulong lim bound 524288
674if qsMatchKey qsKeyStream i 8
675p = add i 8
676while lt p n bound 32
677if not qsIsWsColon get qsReqBuf p
678break
679end
680p = add p 1
681end
682if qsIsTrueAt p n
683ret true
684end
685end
686end
687ret false
688end
689
690fn qsAppendUsageArbTimings cnts view ulong 4 to void
691qsRespAppend qsChatC len qsChatC
692qsRespAppendU get cnts 1
693qsRespAppend qsChatD len qsChatD
694qsRespAppendU add get cnts 0 get cnts 1
695qsRespAppend qsChatE len qsChatE
696qsRespAppendU get qdArbHits 0
697qsRespAppend qsChatF len qsChatF
698qsRespAppendU get qdArbTot 0
699qsRespAppend qsChatG len qsChatG
700qsRespAppendU qsArbHitPct
701qsRespAppend qsChatH len qsChatH
702qsRespAppendU get cnts 2
703qsRespAppend qsChatI len qsChatI
704qsRespAppendU get cnts 3
705qsRespAppend qsChatJ len qsChatJ
706end
707
708fn qsHandleChat fd int n ulong to void
709b = new ulong 2 0
710pLen = ulong 0
711if qsFindLastContent n b
712clen = get b 1
713if gt clen 520000
714clen = ulong 520000
715end
716pLen = qsCopyUnescapedContent get b 0 clen
717else
718pLen = len qsP0
719for i ulong pLen bound 64
720put q36TokInBuf i get qsP0 i
721end
722end
723maxGen = qsFindMaxTokens n
724isStream = qsFindStream n
725if isStream
726put qsStreamFd 0 fd
727qsRespReset
728qsRespAppendHdr qsH200Sse len qsH200Sse
729w = write fd qsRespBuf 0 get qsRespLen 0
730else
731put qsStreamFd 0 -1
732end
733cnts = new ulong 4 0
734ok = qsGenerate pLen maxGen cnts
735put qsStreamFd 0 -1
736if isStream
737qsRespReset
738qsRespAppend qsSseData len qsSseData
739qsRespAppend qsSseFinalA len qsSseFinalA
740qsRespAppendU get cnts 0
741qsAppendUsageArbTimings cnts
742p = get qsRespLen 0
743if lt p 262100
744put qsRespBuf p 10
745put qsRespLen 0 add p 1
746end
747qsRespAppend qsSseDone len qsSseDone
748w = write fd qsRespBuf 0 get qsRespLen 0
749ret
750end
751qsRespReset
752qsRespAppendHdr qsH200 len qsH200
753qsRespAppend qsChatA len qsChatA
754qsRespAppendJsonEsc get qsOutLen 0
755qsRespAppend qsChatB len qsChatB
756qsRespAppendU get cnts 0
757qsAppendUsageArbTimings cnts
758w = write fd qsRespBuf 0 get qsRespLen 0
759end
760
761fn qsIsCrLf2 i ulong n ulong to bool
762if gt add i 4 n
763ret false
764end
765if ne get qsReqBuf i 13
766ret false
767end
768if ne get qsReqBuf add i 1 10
769ret false
770end
771if ne get qsReqBuf add i 2 13
772ret false
773end
774ret eq get qsReqBuf add i 3 10
775end
776
777fn qsFindHdrEnd n ulong to ulong
778if lt n 4
779ret 0
780end
781i = ulong 0
782while lt add i 1 n bound 32768
783if qsIsCrLf2 i n
784ret add i 4
785end
786if and eq get qsReqBuf i 10 eq get qsReqBuf add i 1 10
787ret add i 2
788end
789i = add i 1
790end
791ret 0
792end
793
794fn qsMatchKeyLowerCI kbuf view uchar * pos ulong klen ulong to bool
795for j ulong klen bound 32
796c = get qsReqBuf add pos j
797if and ge c 65 le c 90
798c = add c 32
799end
800if ne c get kbuf j
801ret false
802end
803end
804ret true
805end
806
807fn qsFindContentLength hdrEnd ulong to ulong
808if lt hdrEnd 16
809ret 0
810end
811lim = sub hdrEnd 15
812for i ulong lim bound 32768
813if qsMatchKeyLowerCI qsKeyClen i 15
814ret qsParseReqU add i 15 hdrEnd
815end
816end
817ret 0
818end
819
820fn qsReadHttpRequest fd int to ulong
821tot = ulong 0
822hdrEnd = ulong 0
823need = ulong 0
824for it ulong 1024
825if ge tot 524280
826break
827end
828remCap = sub 524280 tot
829nr = read fd qsReqBuf tot remCap
830if eq nr -4
831continue
832end
833if le nr 0
834break
835end
836tot = add tot ulong nr
837if eq hdrEnd 0
838hdrEnd = qsFindHdrEnd tot
839if gt hdrEnd 0
840clen = qsFindContentLength hdrEnd
841need = add hdrEnd clen
842if gt need 524280
843need = ulong 524280
844end
845end
846end
847if and gt hdrEnd 0 ge tot need
848break
849end
850end
851ret tot
852end
853
854text qsBatchResp "{\"status\":\"ok\",\"streams\":8,\"total_tokens\":128}\n"
855
856fn qsHandleBatchHttp fd int to void
857rc = qdRunBatchDecodeBench 16 8
858qsRespReset
859qsRespAppendHdr qsH200 len qsH200
860qsRespAppend qsBatchResp len qsBatchResp
861w = write fd qsRespBuf 0 get qsRespLen 0
862end
863
864fn qsHandleHttp fd int to void
865n = qsReadHttpRequest fd
866if le n 4
867ret
868end
869if and eq get qsReqBuf 0 71 eq get qsReqBuf 4 47
870if eq get qsReqBuf 5 104
871qsRespReset
872qsRespAppendHdr qsH200 len qsH200
873qsRespAppend qsHealthA len qsHealthA
874if eq get qdUseW4 0 1
875qsRespAppend qsPrecW4 len qsPrecW4
876else
877qsRespAppend qsPrecFp8 len qsPrecFp8
878end
879qsRespAppend qsHealthB len qsHealthB
880qsRespAppendU get qdArbHits 0
881qsRespAppend qsHealthC len qsHealthC
882qsRespAppendU get qdArbTot 0
883qsRespAppend qsHealthD len qsHealthD
884qsRespAppendU qsArbHitPct
885qsRespAppend qsHealthE len qsHealthE
886w = write fd qsRespBuf 0 get qsRespLen 0
887ret
888end
889if and eq get qsReqBuf 5 118 eq get qsReqBuf 8 109
890qsRespReset
891qsRespAppendHdr qsH200 len qsH200
892qsRespAppend qsModels len qsModels
893w = write fd qsRespBuf 0 get qsRespLen 0
894ret
895end
896end
897if and eq get qsReqBuf 0 80 eq get qsReqBuf 5 47
898if and ge n 14 and eq get qsReqBuf 9 98 eq get qsReqBuf 10 97
899qsHandleBatchHttp fd
900ret
901end
902qsHandleChat fd ulong n
903ret
904end
905w = write fd qsH404 0 len qsH404
906end
907
908fn qsListen port ulong to long
909addr = new uchar 16 0
910one = new uchar 4 0
911put addr 0 2
912put addr 2 uchar shr port 8
913put addr 3 uchar and port 255
914put one 0 1
915fd = socknew 2 524289 0
916if lt fd 0
917ret fd
918end
919r = sockopt int fd 1 2 one 0 4
920if eq r 0
921r = bind int fd addr 0 16
922end
923if eq r 0
924r = listen int fd 16
925end
926if ne r 0
927c = fclose int fd
928ret r
929end
930ret fd
931end
932
933fn qsCopyTextToPrompt s view uchar * n ulong to ulong
934for i ulong n bound 256
935put q36TokInBuf i get s i
936end
937ret n
938end
939
940fn qsRunSelfTest to int
941put qsStreamFd 0 -1
942qdPrint qsSelfHdr
943cnts = new ulong 4 0
944n0 = qsCopyTextToPrompt qsP0 len qsP0
945if not qsGenerate n0 32 cnts
946qdPrint qsSelfBad
947ret 30
948end
949qdPrint qsL0
950w0 = write 1 qsOutTxt 0 get qsOutLen 0
951qdPrint qdMsgNl
952if eq get qsOutLen 0 0
953qdPrint qsSelfBad
954ret 31
955end
956if not qsGenerate qsCopyTextToPrompt qsP1 len qsP1 32 cnts
957qdPrint qsSelfBad
958ret 32
959end
960qdPrint qsL1
961w1 = write 1 qsOutTxt 0 get qsOutLen 0
962qdPrint qdMsgNl
963qdPrint qsArbLbl
964qdPrintU get qdArbHits 0
965qdPrint qsArbMid
966qdPrintU get qdArbTot 0
967qdPrint qsArbPct
968qdPrintU qsArbHitPct
969qdPrint qdMsgNl
970if or eq get qsOutLen 0 0 eq get qdArbTot 0 0
971qdPrint qsSelfBad
972ret 33
973end
974note Verify 128K context boundary (pos=131071 across all 10x512 MB KV slabs + 32 MB RoPE table)
975edgeTok = qdForwardToken 271 131071 true false
976if eq edgeTok 999999
977qdPrint qsSelfBad
978ret 34
979end
980if gt get qdBatchN 0 0
981brc = qdRunBatchDecodeBench 16 get qdBatchN 0
982if ne brc 0
983qdPrint qsSelfBad
984ret 35
985end
986end
987qdPrint qsSelfOk
988ret 0
989end
990
991fn qsParseArgs to void
992put qdUseW4 0 1
993put qdUseGdnBf16 0 0
994put qdUseKvInt8 0 0
995put qdUseWtbl 0 0
996put qdRunP2 0 0
997put qdUseMtp 0 0
998put qdBatchN 0 0
999put qdRunCryst 0 0
1000put qdMtpReady 0 0
1001put qdSpecMode 0 0
1002put qdSpecM 0 0
1003put qsPort 0 18091
1004put qsSelfTest 0 0
1005for i ulong len qsDefDir bound 128
1006put qdDirBuf i get qsDefDir i
1007end
1008put qdDirLen 0 len qsDefDir
1009a = new uchar 64 0
1010argc = ulong argcnt
1011ai = ulong 1
1012while lt ai argc bound 32
1013n = argread ai a 0 60
1014if and ge n 5 eq get a 0 45
1015if and eq get a 2 102 eq get a 3 112
1016put qdUseW4 0 0
1017end
1018if and eq get a 2 119 eq get a 3 52
1019put qdUseW4 0 1
1020end
1021if and eq get a 2 103 eq get a 3 100
1022put qdUseGdnBf16 0 1
1023end
1024if and eq get a 2 107 eq get a 3 118
1025put qdUseKvInt8 0 1
1026end
1027if and eq get a 2 115 eq get a 3 112
1028put qdSpecM 0 8
1029end
1030if and eq get a 2 109 eq get a 3 116
1031put qdUseMtp 0 1
1032put qdSpecM 0 4
1033end
1034if and eq get a 2 98 eq get a 3 97
1035put qdBatchN 0 8
1036end
1037if and eq get a 2 115 eq get a 3 101
1038put qsSelfTest 0 1
1039end
1040if and eq get a 2 112 eq get a 3 111
1041if lt add ai 1 argc
1042ai = add ai 1
1043pv = qdParseArgU ai
1044if gt pv 0
1045put qsPort 0 pv
1046end
1047end
1048end
1049if and eq get a 2 109 eq get a 3 111
1050if lt add ai 1 argc
1051ai = add ai 1
1052dl = argread ai qdDirBuf 0 250
1053if gt dl 0
1054put qdDirLen 0 ulong dl
1055end
1056end
1057end
1058end
1059ai = add ai 1
1060end
1061end
1062
1063fn qsLoadTok to bool
1064tlen = qdBuildPath qsSufTok -1
1065pLocal = new uchar 512 0
1066for i ulong tlen bound 512
1067put pLocal i get qdPathBuf i
1068end
1069ret q36TokLoad pLocal tlen
1070end
1071
1072fn main to int
1073qdPrint qsBanner
1074qsParseArgs
1075if eq get qdUseW4 0 1
1076qdPrint qsModeW4
1077else
1078qdPrint qsModeFp8
1079end
1080if not qsLoadTok
1081qdPrint qsTokFail
1082ret 2
1083end
1084qdPrint qsTokOk
1085qdInitRopeFreq
1086qdInitLexRank
1087q36ArbResetAll
1088irc = qdGpuInit
1089if ne irc 0
1090qdPrint qdErr
1091qdPrintU ulong irc
1092qdPrint qdMsgNl
1093ret irc
1094end
1095lrc = qdLoadAll false
1096if ne lrc 0
1097rc = nvstop
1098ret lrc
1099end
1100if and eq get qdUseW4 0 1 gt get qdSpecM 0 0
1101if not qdSpecEmitVerifyPkt get qdSpecM 0
1102r = fclose get qdOutFd 0
1103rc = nvstop
1104ret 17
1105end
1106if eq get qdUseMtp 0 1
1107if not qdLoadMtpW4
1108r = fclose get qdOutFd 0
1109rc = nvstop
1110ret 18
1111end
1112if not qdMtpRecordPackets
1113r = fclose get qdOutFd 0
1114rc = nvstop
1115ret 19
1116end
1117end
1118end
1119if eq get qsSelfTest 0 1
1120prc = qsRunSelfTest
1121r = fclose get qdOutFd 0
1122rc = nvstop
1123ret prc
1124end
1125port = get qsPort 0
1126lfd = qsListen port
1127if lt lfd 0
1128r = fclose get qdOutFd 0
1129rc = nvstop
1130ret 40
1131end
1132qdPrint qsListenMsg
1133qdPrintU port
1134qdPrint qsListenEnd
1135for conn ulong 1000000
1136cfd = accept int lfd 524288
1137if ge cfd 0
1138qsHandleHttp int cfd
1139c0 = fclose int cfd
1140end
1141end
1142c1 = fclose int lfd
1143r = fclose get qdOutFd 0
1144rc = nvstop
1145ret 0
1146end