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

333 lines · 6910 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/gpu/moe_plan.om: Pure OM-2 persistent MoE request queue state machine and
3note unified MoE layer execution planner + memory traffic estimator (Priority 2 & 3).
4note
5note Queue buffer layout: q[512] uint (8-word header + 64 slots x 6 uint words = 392 words):
6note q[0]=magic (0x4d4f4551), q[1]=cap (1..64), q[2]=head, q[3]=tail,
7note q[4]=epoch, q[5]=active_count, q[6]=done_count, q[7]=err_count
8note slot s (0..63) at base = 8 + s * 6:
9note [0]=req_id, [1]=tokens, [2]=layer, [3]=state (0=EMPTY,1=QUEUED,2=RUNNING,3=DONE,4=ERROR),
10note [4]=flags, [5]=batch_epoch
11note
12note Plan output layout: dst[128] uint (up to 32 ops x 4 uint words):
13note dst[4*k + 0] = op, dst[4*k + 1] = mark_independent, dst[4*k + 2] = arg0, dst[4*k + 3] = arg1
14om2
15
16const MOEQ_MAGIC ulong 0x4d4f4551
17const MOEQ_MAX_CAP ulong 64
18const MOEQ_MAX_TOKENS ulong 1024
19const MOEQ_ERR ulong 0xffffffff
20
21const ST_EMPTY ulong 0
22const ST_QUEUED ulong 1
23const ST_RUNNING ulong 2
24const ST_DONE ulong 3
25const ST_ERROR ulong 4
26
27const MOP_ROUTER_LOGITS ulong 1
28const MOP_ACT_QUANT ulong 2
29const MOP_EXPERT_GATEUP_GEMM ulong 3
30const MOP_EXPERT_GATEUP_GEMV ulong 4
31const MOP_EXPERT_DOWN_GEMM ulong 5
32const MOP_EXPERT_DOWN_GEMV ulong 6
33const MOP_MOE_COMBINE ulong 7
34
35fn mq_emit dst edit uint 128 idx ulong op ulong indep ulong a0 ulong a1 ulong to ulong
36if ge idx 32
37ret 32
38end
39b = mul idx 4
40put dst b uint op
41put dst add b 1 uint indep
42put dst add b 2 uint a0
43put dst add b 3 uint a1
44ret add idx 1
45end
46
47export fn moe_queue_init q edit uint 512 cap ulong to ulong
48if eq cap 0
49ret 1
50end
51if gt cap MOEQ_MAX_CAP
52ret 1
53end
54for i ulong 512
55put q i uint 0
56end
57put q 0 uint MOEQ_MAGIC
58put q 1 uint cap
59ret 0
60end
61
62fn mq_valid q view uint 512 to ulong
63if ne ulong get q 0 MOEQ_MAGIC
64ret 0
65end
66cap = ulong get q 1
67if eq cap 0
68ret 0
69end
70if gt cap MOEQ_MAX_CAP
71ret 0
72end
73ret cap
74end
75
76export fn moe_queue_enqueue q edit uint 512 req_id ulong tokens ulong layer ulong flags ulong to ulong
77cap = mq_valid q
78if eq cap 0
79ret MOEQ_ERR
80end
81if eq tokens 0
82ret MOEQ_ERR
83end
84if gt tokens MOEQ_MAX_TOKENS
85ret MOEQ_ERR
86end
87tail = ulong get q 3
88if ge sub tail ulong get q 2 cap
89ret MOEQ_ERR
90end
91slot = rem tail cap
92base = add 8 mul slot 6
93if ne ulong get q add base 3 ST_EMPTY
94ret MOEQ_ERR
95end
96put q base uint req_id
97put q add base 1 uint tokens
98put q add base 2 uint layer
99put q add base 3 uint ST_QUEUED
100put q add base 4 uint flags
101put q add base 5 uint 0
102put q 3 uint add tail 1
103ret slot
104end
105
106export fn moe_queue_claim_batch q edit uint 512 max_tokens ulong out_slots edit uint 64 to ulong
107cap = mq_valid q
108if eq cap 0
109ret 0
110end
111if eq max_tokens 0
112ret 0
113end
114epoch = add ulong get q 4 1
115claimed = ulong 0
116tok_sum = ulong 0
117cur = ulong get q 2
118while lt cur ulong get q 3 bound 64
119slot = rem cur cap
120base = add 8 mul slot 6
121st = ulong get q add base 3
122if eq st ST_QUEUED
123if gt add tok_sum ulong get q add base 1 max_tokens
124break
125end
126put q add base 3 uint ST_RUNNING
127put q add base 5 uint epoch
128put out_slots claimed uint slot
129claimed = add claimed 1
130tok_sum = add tok_sum ulong get q add base 1
131cur = add cur 1
132else
133if eq st ST_RUNNING
134cur = add cur 1
135else
136break
137end
138end
139end
140if gt claimed 0
141put q 4 uint epoch
142put q 5 uint add ulong get q 5 claimed
143end
144ret claimed
145end
146
147fn mq_advance_head q edit uint 512 cap ulong to void
148head = ulong get q 2
149while lt head ulong get q 3 bound 64
150base = add 8 mul rem head cap 6
151st = ulong get q add base 3
152if eq st ST_DONE
153put q add base 3 uint ST_EMPTY
154head = add head 1
155else
156if eq st ST_ERROR
157put q add base 3 uint ST_EMPTY
158head = add head 1
159else
160break
161end
162end
163end
164put q 2 uint head
165end
166
167export fn moe_queue_complete_batch q edit uint 512 n ulong slots view uint 64 status ulong to ulong
168cap = mq_valid q
169if eq cap 0
170ret 1
171end
172if gt n cap
173ret 1
174end
175if lt ulong get q 5 n
176ret 1
177end
178done_st = ST_DONE
179if ne status 0
180done_st = ST_ERROR
181end
182for i ulong n bound 64
183slot = ulong get slots i
184if ge slot cap
185ret 1
186end
187base = add 8 mul slot 6
188if ne ulong get q add base 3 ST_RUNNING
189ret 1
190end
191put q add base 3 uint done_st
192end
193put q 5 uint sub ulong get q 5 n
194if eq status 0
195put q 6 uint add ulong get q 6 n
196else
197put q 7 uint add ulong get q 7 n
198end
199mq_advance_head q cap
200ret 0
201end
202
203note cfg: 0=tokens (1..1024), 1=hidden, 2=inter, 3=experts, 4=top_k, 5=flags
204note flags: bit0=router, bit1=gate_up, bit2=down, bit3=combine, bit4=use_qmma, bit5=overlap_router_quant
205export fn moe_plan_layer cfg view ulong 8 dst edit uint 128 to ulong
206toks = get cfg 0
207hd = get cfg 1
208inter = get cfg 2
209exp = get cfg 3
210topk = get cfg 4
211flags = get cfg 5
212if eq toks 0
213ret 0
214end
215if gt toks MOEQ_MAX_TOKENS
216ret 0
217end
218if eq hd 0
219ret 0
220end
221if eq inter 0
222ret 0
223end
224if eq exp 0
225ret 0
226end
227if eq topk 0
228ret 0
229end
230if gt topk exp
231ret 0
232end
233rows = mul toks topk
234use_qmma = and shr flags 4 1
235ov_rq = and shr flags 5 1
236k = ulong 0
237if ne and flags 1 0
238k = mq_emit dst k MOP_ROUTER_LOGITS 0 toks exp
239end
240if ne use_qmma 0
241ind = ulong 0
242if ne and flags 1 0
243if ne ov_rq 0
244ind = ulong 1
245end
246end
247k = mq_emit dst k MOP_ACT_QUANT ind toks hd
248end
249if ne and flags 2 0
250if eq toks 1
251if eq use_qmma 0
252k = mq_emit dst k MOP_EXPERT_GATEUP_GEMV 0 rows mul inter 2
253else
254k = mq_emit dst k MOP_EXPERT_GATEUP_GEMM 0 rows mul inter 2
255end
256else
257k = mq_emit dst k MOP_EXPERT_GATEUP_GEMM 0 rows mul inter 2
258end
259end
260if ne and flags 4 0
261if eq toks 1
262if eq use_qmma 0
263k = mq_emit dst k MOP_EXPERT_DOWN_GEMV 0 rows hd
264else
265k = mq_emit dst k MOP_EXPERT_DOWN_GEMM 0 rows hd
266end
267else
268k = mq_emit dst k MOP_EXPERT_DOWN_GEMM 0 rows hd
269end
270end
271if ne and flags 8 0
272k = mq_emit dst k MOP_MOE_COMBINE 0 toks topk
273end
274ret k
275end
276
277note Compute theoretical memory traffic (bytes) per stage for a planned MoE layer:
278note out[0]=router, out[1]=gate_up, out[2]=down, out[3]=combine, out[4]=total
279export fn moe_plan_traffic cfg view ulong 8 distinct_experts ulong out_bytes edit ulong 8 to ulong
280if eq get cfg 0 0
281ret 1
282end
283if eq get cfg 1 0
284ret 1
285end
286if eq get cfg 2 0
287ret 1
288end
289if eq get cfg 3 0
290ret 1
291end
292if eq get cfg 4 0
293ret 1
294end
295if gt distinct_experts get cfg 3
296ret 1
297end
298for i ulong 8
299put out_bytes i 0
300end
301rows = mul get cfg 0 get cfg 4
302flags = get cfg 5
303b0 = ulong 0
304b1 = ulong 0
305b2 = ulong 0
306if ne and flags 1 0
307b0 = mul mul get cfg 0 get cfg 1 4
308b1 = mul mul get cfg 3 get cfg 1 4
309b2 = mul mul get cfg 0 get cfg 3 4
310put out_bytes 0 add add b0 b1 b2
311end
312if ne and flags 2 0
313b0 = mul mul distinct_experts mul get cfg 2 2 get cfg 1
314b1 = mul mul get cfg 0 get cfg 1 4
315b2 = mul mul rows mul get cfg 2 2 4
316put out_bytes 1 add add b0 b1 b2
317end
318if ne and flags 4 0
319b0 = mul distinct_experts mul get cfg 1 get cfg 2
320b1 = mul mul rows get cfg 2 4
321b2 = mul mul rows get cfg 1 4
322put out_bytes 2 add add b0 b1 b2
323end
324if ne and flags 8 0
325b0 = mul mul rows get cfg 1 4
326b1 = mul mul get cfg 0 get cfg 1 4
327b2 = mul rows 8
328put out_bytes 3 add add b0 b1 b2
329end
330tot = add add get out_bytes 0 get out_bytes 1 add get out_bytes 2 get out_bytes 3
331put out_bytes 4 tot
332ret 0
333end