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