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 / batcher.om

170 lines · 4249 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note Static N-Slot Paged-KV Table + Continuous Batcher + MoE Expert Deduplicator (ZERO HEAP).
3note Deterministic memory ceiling: N_MAX = 8 concurrent streams.
4note Admission returns slot 0..7 or -1 (HTTP 503 server_busy) only when all 8 static KV slots are occupied.
5
6const MS_MAX_STREAMS ulong 8
7const MS_KV_PAGES_PER_STREAM ulong 16
8const MS_PAGE_TOKENS ulong 2048
9
10note Static N-slot stream descriptor table (ZERO heap):
11global msActive ulong 8
12global msPosQ ulong 8
13global msGenCnt ulong 8
14global msMaxGen ulong 8
15global msClientFd int 8
16global msLastTok uint 8
17global msActiveCnt ulong 1
18note Static paged-KV table: page IDs [8 streams x 16 pages] -> flat page index 0..127 (0xffffffff = free)
19global msPageOwner ulong 128
20global msStreamPages ulong 128
21global msStreamPageCnt ulong 8
22
23note Deduplicated MoE batch metadata (matches nvfp4_gemm_dedup_fast / mxfp8_gemm_dedup_fast ABI):
24note msUniqEids[0..63], msUniqCnt[0..63], msSlotMap[64 * 8]
25global msUniqEids uint 64
26global msUniqCnt uint 64
27global msSlotMap uint 512
28global msUniqNum ulong 1
29
30fn msInit to void
31put msActiveCnt 0 0
32for s ulong MS_MAX_STREAMS
33put msActive s 0
34put msPosQ s 0
35put msGenCnt s 0
36put msMaxGen s 0
37put msClientFd s -1
38put msLastTok s 0
39put msStreamPageCnt s 0
40end
41for p ulong 128
42put msPageOwner p 18446744073709551615
43put msStreamPages p 18446744073709551615
44end
45end
46
47fn msAllocPage s ulong to ulong
48for p ulong 128
49if eq get msPageOwner p 18446744073709551615
50put msPageOwner p s
51pc = get msStreamPageCnt s
52if ge pc MS_KV_PAGES_PER_STREAM
53put msPageOwner p 18446744073709551615
54ret 18446744073709551615
55end
56put msStreamPages add mul s MS_KV_PAGES_PER_STREAM pc p
57put msStreamPageCnt s add pc 1
58ret p
59end
60end
61ret 18446744073709551615
62end
63
64fn msFreeStreamPages s ulong to void
65for p ulong 128
66if eq get msPageOwner p s
67put msPageOwner p 18446744073709551615
68end
69end
70for k ulong MS_KV_PAGES_PER_STREAM
71put msStreamPages add mul s MS_KV_PAGES_PER_STREAM k 18446744073709551615
72end
73put msStreamPageCnt s 0
74end
75
76note Admit a new stream into the static N-slot table.
77note Returns slot 0..7 on success, or -1 (caller returns HTTP 503 server_busy) when all 8 slots are full.
78fn msAdmit fd int maxGen ulong initTok uint to long
79for s ulong MS_MAX_STREAMS
80if eq get msActive s 0
81pg = msAllocPage s
82if eq pg 18446744073709551615
83ret -1
84end
85put msActive s 1
86put msPosQ s 0
87put msGenCnt s 0
88put msMaxGen s maxGen
89put msClientFd s fd
90put msLastTok s initTok
91put msActiveCnt 0 add get msActiveCnt 0 1
92ret long s
93end
94end
95ret -1
96end
97
98note Advance a stream by 1 token, allocating a new 64-token KV page when crossing a page boundary.
99fn msAdvance s ulong nextTok uint to bool
100if or ge s MS_MAX_STREAMS eq get msActive s 0
101ret false
102end
103pos = add get msPosQ s 1
104put msPosQ s pos
105put msGenCnt s add get msGenCnt s 1
106put msLastTok s nextTok
107needPages = add div pos MS_PAGE_TOKENS 1
108if gt needPages get msStreamPageCnt s
109pg = msAllocPage s
110if eq pg 18446744073709551615
111ret false
112end
113end
114ret true
115end
116
117note Retire a finished/disconnected stream and release its static KV pages immediately.
118fn msRetire s ulong to void
119if or ge s MS_MAX_STREAMS eq get msActive s 0
120ret
121end
122msFreeStreamPages s
123put msActive s 0
124put msClientFd s -1
125if gt get msActiveCnt 0 0
126put msActiveCnt 0 sub get msActiveCnt 0 1
127end
128end
129
130note Deduplicate routed expert IDs across B active streams (B * 8 slots -> U <= 64 unique experts).
131note Each unique expert u records all flat slots (s*8 + k) that routed to it in msSlotMap[u*8 .. u*8 + cnt - 1].
132fn msDedupExperts routedIds view uint 64 nStreams ulong to ulong
133put msUniqNum 0 0
134for u ulong 64
135put msUniqEids u 0xffffffff
136put msUniqCnt u 0
137for j ulong 8
138put msSlotMap add mul u 8 j 0xffffffff
139end
140end
141totSlots = mul nStreams 8
142for slot ulong totSlots bound 64
143eid = get routedIds slot
144if lt eid 128
145uCnt = get msUniqNum 0
146found = ulong 64
147for u ulong uCnt bound 64
148if eq get msUniqEids u eid
149found = u
150break
151end
152end
153if eq found 64
154if lt uCnt 64
155put msUniqEids uCnt eid
156put msUniqCnt uCnt 1
157put msSlotMap mul uCnt 8 uint slot
158put msUniqNum 0 add uCnt 1
159end
160else
161c = ulong get msUniqCnt found
162if lt c 8
163put msSlotMap add mul found 8 c uint slot
164put msUniqCnt found uint add c 1
165end
166end
167end
168end
169ret get msUniqNum 0
170end