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