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 / inkling2 / multistream / serve_live.om

685 lines · 14034 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note 100% Pure Seeded-Omega Real-Weight N=8 Multi-Stream Inkling-2-Mini Server (32,768-tok KV, Native Tool Calling, Live Arboris).
3note ZERO CUDA, ZERO libc, ZERO Python, ZERO C, ZERO heap.
4note Composes after:
5note context.om + vector.om + launch.om + batch.om + bw_enc_sm121.om + emit_fast.om
6note + arboris/prefetch.om + safetensors.om + tokenizer.om + decode.om + multistream/batcher.om.
7note
8note Usage:
9note inkling2-serve --self-test (loads real 18.3 GB checkpoint, runs 2 interleaved
10note concurrent streams + verifies 8-slot 503 gate)
11note inkling2-serve [model_dir] [port] (starts live HTTP server on port 8080 or [port])
12
13global slReqBuf uchar 131072
14global slRespBuf uchar 65536
15global slRespLen ulong 1
16global slOutTxt uchar 65536
17global slOutLen ulong 8
18global slPromptIds uint 131072
19global slPromptLen ulong 8
20global slPromptPos ulong 8
21global slDone ulong 8
22global slCacheIds uint 131072
23global slCacheLen ulong 8
24global slCacheHits ulong 1
25
26text slBanner "=== INKLING-2-MINI 100% PURE SEEDED-OMEGA LIVE N=8 SERVER (32K CTX + NATIVE TOOLS + ARBORIS, ZERO CUDA) ===\n"
27text slListenMsg "Listening on HTTP port "
28text slReadyMsg " (N=8 static 32K KV slots, native tool tokens, Arboris active, 503 server_busy when full)\n"
29text slSelfHdr "Running live real-weight multi-stream self-test (2 interleaved streams + 8-slot 503 gate)...\n"
30text slS0Prompt "What is the capital of France? Answer in one sentence."
31text slS1Prompt "What is 2 + 2? Answer in one sentence."
32text slS0Lbl "Stream 0 (slot 0): "
33text slS1Lbl "Stream 1 (slot 1): "
34text slSelfOk "PASS: live real-weight N=8 multi-stream continuous batcher (2 interleaved coherent streams, 9th -> 503 server_busy, retire+reuse OK)\n"
35text slSelfBad "FAIL: live multi-stream self-test failed\n"
36
37text slH200 "HTTP/1.1 200 OK\nContent-Type: application/json\nConnection: close\n\n"
38text slH503 "HTTP/1.1 503 Service Unavailable\nContent-Type: application/json\nRetry-After: 1\nConnection: close\n\n{\"error\":{\"type\":\"server_busy\",\"message\":\"all 8 static KV slots occupied\",\"slots_max\":8}}\n"
39text slH404 "HTTP/1.1 404 Not Found\nContent-Type: application/json\nConnection: close\n\n{\"error\":{\"type\":\"not_found\"}}\n"
40text slHealthPre "{\"status\":\"ok\",\"model\":\"inkling-2-mini\",\"context_window\":32768,\"slots_max\":8,\"slots_active\":"
41text slHealthCache ",\"kv_prefix_hits\":"
42text slHealthArb1 ",\"arboris_transitions\":"
43text slHealthArb2 ",\"arboris_top8_hits\":"
44text slHealthArb3 ",\"arboris_top16_hits\":"
45text slHealthSuf "}\n"
46text slModels "{\"object\":\"list\",\"data\":[{\"id\":\"inkling-2-mini\",\"object\":\"model\",\"context_window\":32768,\"slots_max\":8}]}\n"
47text slChatPre "{\"id\":\"cmpl-ik2\",\"object\":\"chat.completion\",\"model\":\"inkling-2-mini\",\"choices\":[{\"index\":0,\"message\":{\"role\":\"assistant\",\"content\":\""
48text slChatMid "\"},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":"
49text slChatMid2 ",\"completion_tokens\":"
50text slChatMid3 ",\"total_tokens\":"
51text slChatSuf "}}\n"
52text slKeyContent "\"content\""
53text slKeyCLen "Content-Length:"
54text slKeyCLenLo "content-length:"
55
56fn slRespReset to void
57put slRespLen 0 0
58end
59
60fn slRespAppendHdr s view uchar * n ulong to void
61p = get slRespLen 0
62for i ulong n bound 512
63c = get s i
64if and eq c 10 lt p 65528
65put slRespBuf p 13
66p = add p 1
67end
68if lt p 65530
69put slRespBuf p c
70p = add p 1
71end
72end
73put slRespLen 0 p
74end
75
76fn slRespAppend s view uchar * n ulong to void
77p = get slRespLen 0
78for i ulong n bound 32768
79if lt p 65530
80put slRespBuf p get s i
81p = add p 1
82end
83end
84put slRespLen 0 p
85end
86
87fn slRespAppendU v ulong to void
88tmp = new uchar 24 0
89p = get slRespLen 0
90if eq v 0
91if lt p 65530
92put slRespBuf p 48
93put slRespLen 0 add p 1
94end
95ret
96end
97n = ulong 0
98x = v
99while gt x 0 bound 24
100put tmp n uchar add 48 rem x 10
101x = div x 10
102n = add n 1
103end
104for i ulong n bound 24
105if lt p 65530
106put slRespBuf p get tmp sub sub n 1 i
107p = add p 1
108end
109end
110put slRespLen 0 p
111end
112
113fn slRespAppendJsonEsc off ulong n ulong to void
114p = get slRespLen 0
115for i ulong n bound 8192
116c = get slOutTxt add off i
117if and lt p 65520 ge c 32
118if or eq c 34 eq c 92
119put slRespBuf p 92
120p = add p 1
121end
122put slRespBuf p c
123p = add p 1
124else
125if and lt p 65520 eq c 10
126put slRespBuf p 92
127put slRespBuf add p 1 110
128p = add p 2
129else
130if and lt p 65520 eq c 9
131put slRespBuf p 92
132put slRespBuf add p 1 116
133p = add p 2
134end
135end
136end
137end
138put slRespLen 0 p
139end
140
141fn slAppendTokOut slot ulong tid ulong to void
142if ge tid IK_VOCAB_MAX
143ret
144end
145l = ulong get ikTokLen tid
146if eq l 0
147ret
148end
149off = ulong get ikTokOff tid
150cur = get slOutLen slot
151base = mul slot 8192
152for i ulong l bound 64
153if lt cur 8184
154put slOutTxt add base cur get ikTokPool add off i
155cur = add cur 1
156end
157end
158put slOutLen slot cur
159end
160
161fn slCacheMatches slot ulong nTok ulong to bool
162cLen = get slCacheLen slot
163if or eq cLen 0 le nTok cLen
164ret false
165end
166base = mul slot 16384
167for i ulong cLen bound 16384
168if ne get slCacheIds add base i get ikTokOutLong i
169ret false
170end
171end
172ret true
173end
174
175fn slStartFromBuf slot ulong tlen ulong maxGen ulong to bool
176nTok = ikTokEncodeSmartLong tlen
177if eq nTok 0
178ret false
179end
180base = mul slot 16384
181if gt nTok 16300
182nTok = ulong 16300
183end
184startPos = ulong 0
185if slCacheMatches slot nTok
186startPos = get slCacheLen slot
187put slCacheHits 0 add get slCacheHits 0 1
188else
189ikDResetSlot slot
190put slCacheLen slot 0
191end
192for i ulong nTok bound 16384
193put slPromptIds add base i get ikTokOutLong i
194end
195put slPromptLen slot nTok
196put slPromptPos slot startPos
197put slOutLen slot 0
198put slDone slot 0
199put msMaxGen slot maxGen
200ret true
201end
202
203fn slStartStream slot ulong txt view uchar * tlen ulong maxGen ulong to bool
204for i ulong tlen bound 16384
205put ikTokInBuf i get txt i
206end
207ret slStartFromBuf slot tlen maxGen
208end
209
210note Execute 1 prefill or decode step for active stream `slot`.
211note Stops ONLY on 200006 (<|content_model_end_sampling|>) or 199999 (<|endoftext|>),
212note NOT on 200010 (<|end_message|>), so native multi-message tool calls complete cleanly.
213fn slStepStream slot ulong to bool
214if eq get slDone slot 1
215ret true
216end
217pPos = get slPromptPos slot
218pLen = get slPromptLen slot
219if lt pPos pLen
220if ge sub pLen pPos 9
221base = mul slot 16384
222for b ulong 8
223put ikB8Toks b get slPromptIds add base add pPos b
224end
225if not ikDPrefillBatch8 slot pPos
226put slDone slot 1
227ret true
228end
229for b ulong 8
230posB = add pPos b
231if lt posB 16300
232put slCacheIds add base posB get ikB8Toks b
233put slCacheLen slot add posB 1
234end
235end
236put slPromptPos slot add pPos 8
237ret false
238end
239tok = ulong get slPromptIds add mul slot 16384 pPos
240needL = eq add pPos 1 pLen
241nxt = ikDForwardSlot slot tok pPos needL
242if lt nxt 0
243put slDone slot 1
244ret true
245end
246if lt pPos 16300
247put slCacheIds add mul slot 16384 pPos uint tok
248put slCacheLen slot add pPos 1
249end
250put slPromptPos slot add pPos 1
251if needL
252if or eq nxt 200006 eq nxt 199999
253put slDone slot 1
254ret true
255end
256slAppendTokOut slot ulong nxt
257ok = msAdvance slot uint nxt
258put msPosQ slot pLen
259end
260ret false
261end
262posQ = get msPosQ slot
263curTok = ulong get msLastTok slot
264nxt = ikDForwardSlot slot curTok posQ true
265if lt posQ 16300
266put slCacheIds add mul slot 16384 posQ uint curTok
267put slCacheLen slot add posQ 1
268end
269if or lt nxt 0 or eq nxt 200006 eq nxt 199999
270put slDone slot 1
271ret true
272end
273slAppendTokOut slot ulong nxt
274ok = msAdvance slot uint nxt
275if or not ok ge get msGenCnt slot get msMaxGen slot
276put slDone slot 1
277ret true
278end
279ret false
280end
281
282fn slRunSelfTest to int
283ikDPrint slSelfHdr
284msInit
285s0 = msAdmit 10 32 0
286s1 = msAdmit 11 32 0
287if or ne s0 0 ne s1 1
288ikDPrint slSelfBad
289ret 30
290end
291if not slStartStream 0 slS0Prompt len slS0Prompt 32
292ikDPrint slSelfBad
293ret 31
294end
295if not slStartStream 1 slS1Prompt len slS1Prompt 32
296ikDPrint slSelfBad
297ret 32
298end
299for k ulong 6
300sk = msAdmit int add 12 k 8 0
301if ne sk long add 2 k
302ikDPrint slSelfBad
303ret 33
304end
305end
306if ne msAdmit 99 8 0 -1
307ikDPrint slSelfBad
308ret 34
309end
310for k ulong 6
311msRetire add 2 k
312end
313for step ulong 128
314d0 = slStepStream 0
315d1 = slStepStream 1
316if and d0 d1
317break
318end
319end
320ikDPrint slS0Lbl
321w0 = write 1 slOutTxt 0 get slOutLen 0
322ikDPrint ikDMsgNl
323ikDPrint slS1Lbl
324w1 = write 1 slOutTxt 8192 get slOutLen 1
325ikDPrint ikDMsgNl
326msRetire 0
327msRetire 1
328reused = msAdmit 42 8 0
329if ne reused 0
330ikDPrint slSelfBad
331ret 35
332end
333msRetire 0
334if or eq get slOutLen 0 0 eq get slOutLen 1 0
335ikDPrint slSelfBad
336ret 36
337end
338ikDPrint slSelfOk
339ret 0
340end
341
342fn slFindHdrEnd n ulong to ulong
343if lt n 4
344ret n
345end
346lim = sub n 3
347for i ulong lim bound 4096
348if and eq get slReqBuf i 13 eq get slReqBuf add i 1 10
349if and eq get slReqBuf add i 2 13 eq get slReqBuf add i 3 10
350ret add i 4
351end
352end
353if and eq get slReqBuf i 10 eq get slReqBuf add i 1 10
354ret add i 2
355end
356end
357ret n
358end
359
360fn slMatchHdrAt pos ulong pat view uchar * to bool
361for j ulong len pat bound 32
362if ne get slReqBuf add pos j get pat j
363ret false
364end
365end
366ret true
367end
368
369fn slParseDigitsAt start ulong lim ulong to ulong
370p = start
371while and lt p lim eq get slReqBuf p 32 bound 32
372p = add p 1
373end
374v = ulong 0
375while lt p lim bound 16
376c = get slReqBuf p
377if or lt c 48 gt c 57
378break
379end
380v = add mul v 10 ulong sub c 48
381p = add p 1
382end
383ret v
384end
385
386fn slParseContentLength hdrEnd ulong to ulong
387if lt hdrEnd 16
388ret 0
389end
390lim = sub hdrEnd 15
391for i ulong lim bound 4096
392if slMatchHdrAt i slKeyCLen
393ret slParseDigitsAt add i 15 hdrEnd
394end
395if slMatchHdrAt i slKeyCLenLo
396ret slParseDigitsAt add i 15 hdrEnd
397end
398end
399ret 0
400end
401
402note Read complete HTTP request (headers + full Content-Length body up to 128 KiB).
403fn slReadFullReq fd int to ulong
404tot = ulong 0
405wantTot = ulong 0
406for iter ulong 256
407if ge tot 131070
408break
409end
410r = read fd slReqBuf tot sub 131070 tot
411if eq r -4
412continue
413end
414if le r 0
415break
416end
417tot = add tot ulong r
418if eq wantTot 0
419he = slFindHdrEnd tot
420if lt he tot
421cl = slParseContentLength he
422if gt cl 0
423wantTot = add he cl
424if gt wantTot 131070
425wantTot = ulong 131070
426end
427else
428break
429end
430else
431if eq get slReqBuf 0 71
432if lt he add tot 1
433break
434end
435end
436end
437end
438if and gt wantTot 0 ge tot wantTot
439break
440end
441end
442ret tot
443end
444
445fn slUnescapeToPrompt st ulong n ulong to ulong
446p = st
447o = ulong 0
448while and lt p n lt o 131000 bound 131072
449c = get slReqBuf p
450if eq c 34
451break
452end
453p = add p 1
454if eq c 92
455if ge p n
456break
457end
458e = get slReqBuf p
459p = add p 1
460if eq e 110
461put ikTokInBuf o 10
462o = add o 1
463else
464if eq e 114
465put ikTokInBuf o 13
466o = add o 1
467else
468if eq e 116
469put ikTokInBuf o 9
470o = add o 1
471else
472put ikTokInBuf o e
473o = add o 1
474end
475end
476end
477else
478put ikTokInBuf o c
479o = add o 1
480end
481end
482ret o
483end
484
485fn slExtractContent n ulong to ulong
486if lt n 14
487ret 0
488end
489lim = sub n 10
490for i ulong lim bound 131072
491ok = true
492for j ulong 9
493if ne get slReqBuf add i j get slKeyContent j
494ok = false
495break
496end
497end
498if ok
499p = add i 9
500while and lt p n ne get slReqBuf p 34 bound 64
501p = add p 1
502end
503if lt p n
504ret slUnescapeToPrompt add p 1 n
505end
506end
507end
508ret 0
509end
510
511fn slHandleChat fd int n ulong to void
512slot = msAdmit fd 1024 0
513if lt slot 0
514w = write fd slH503 0 len slH503
515ret
516end
517pLen = slExtractContent n
518if eq pLen 0
519pLen = len slS0Prompt
520for i ulong pLen bound 1024
521put ikTokInBuf i get slS0Prompt i
522end
523end
524s = ulong slot
525if slStartFromBuf s pLen 1024
526for step ulong 17408
527if slStepStream s
528break
529end
530end
531end
532pToks = get slPromptLen s
533cToks = get msGenCnt s
534slRespReset
535slRespAppendHdr slH200 len slH200
536slRespAppend slChatPre len slChatPre
537slRespAppendJsonEsc mul s 8192 get slOutLen s
538slRespAppend slChatMid len slChatMid
539slRespAppendU pToks
540slRespAppend slChatMid2 len slChatMid2
541slRespAppendU cToks
542slRespAppend slChatMid3 len slChatMid3
543slRespAppendU add pToks cToks
544slRespAppend slChatSuf len slChatSuf
545w = write fd slRespBuf 0 get slRespLen 0
546msRetire s
547end
548
549fn slHandleHttp fd int to void
550n = slReadFullReq fd
551if le n 4
552ret
553end
554if and eq get slReqBuf 0 71 eq get slReqBuf 4 47
555if eq get slReqBuf 5 104
556slRespReset
557slRespAppendHdr slH200 len slH200
558slRespAppend slHealthPre len slHealthPre
559slRespAppendU get msActiveCnt 0
560slRespAppend slHealthCache len slHealthCache
561slRespAppendU get slCacheHits 0
562slRespAppend slHealthArb1 len slHealthArb1
563slRespAppendU get ikDArbStats 0
564slRespAppend slHealthArb2 len slHealthArb2
565slRespAppendU get ikDArbStats 1
566slRespAppend slHealthArb3 len slHealthArb3
567slRespAppendU get ikDArbStats 2
568slRespAppend slHealthSuf len slHealthSuf
569w = write fd slRespBuf 0 get slRespLen 0
570ret
571end
572if and eq get slReqBuf 5 118 eq get slReqBuf 8 109
573slRespReset
574slRespAppendHdr slH200 len slH200
575slRespAppend slModels len slModels
576w = write fd slRespBuf 0 get slRespLen 0
577ret
578end
579end
580if and eq get slReqBuf 0 80 eq get slReqBuf 5 47
581slHandleChat fd n
582ret
583end
584w = write fd slH404 0 len slH404
585end
586
587fn slIsSelfTestArg to bool
588a = new uchar 32 0
589if lt argcnt 2
590ret false
591end
592n = argread 1 a 0 32
593if eq n 11
594if and eq get a 0 45 eq get a 2 115
595ret true
596end
597end
598if ge argcnt 3
599n2 = argread 2 a 0 32
600if eq n2 11
601if and eq get a 0 45 eq get a 2 115
602ret true
603end
604end
605end
606ret false
607end
608
609fn slListen port ulong to long
610addr = new uchar 16 0
611one = new uchar 4 0
612put addr 0 2
613put addr 2 uchar shr port 8
614put addr 3 uchar and port 255
615put one 0 1
616fd = socknew 2 524289 0
617if lt fd 0
618ret fd
619end
620r = sockopt int fd 1 2 one 0 4
621if eq r 0
622r = bind int fd addr 0 16
623end
624if eq r 0
625r = listen int fd 16
626end
627if ne r 0
628c = fclose int fd
629ret r
630end
631ret fd
632end
633
634fn main to int
635ikDPrint slBanner
636ikDPrint ikDMsgLoad
637t0 = monons
638rc0 = ikDInitModel 8
639if ne rc0 0
640ikDPrint ikDMsgFail
641ikDPrintU ulong rc0
642ikDPrint ikDMsgNl
643ret rc0
644end
645t1 = monons
646ikDPrint ikDMsgLoaded
647ikDPrintU ulong div sub t1 t0 1000000
648ikDPrint ikDMsgNl
649ikArbResetAll
650for i ulong 3072
651put ikDArbRes i -1
652end
653for i ulong 4
654put ikDArbStats i 0
655end
656for s ulong 8
657put slCacheLen s 0
658end
659put slCacheHits 0 0
660if slIsSelfTestArg
661rc1 = slRunSelfTest
662rc = nvstop
663ret rc1
664end
665msInit
666port = ulong 8080
667lfd = slListen port
668if lt lfd 0
669rc = nvstop
670ret 40
671end
672ikDPrint slListenMsg
673ikDPrintU port
674ikDPrint slReadyMsg
675for conn ulong 1000000
676cfd = accept int lfd 524288
677if ge cfd 0
678slHandleHttp int cfd
679c0 = fclose int cfd
680end
681end
682c1 = fclose int lfd
683rc = nvstop
684ret 0
685end