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 / spec / mtp_varlen.om

495 lines · 11428 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Pure Seeded OM-2 Confidence-Scheduled Speculative Decoding Engine (MTPVarlen).
3note Implements DeepInfer's confidence-scheduled variable-length verification
4note (csrc/engine/mtp_varlen.hpp) + online ForwardRuntimeEstimator / SamplerRuntimeEstimator
5note + lossless greedy speculative verifier for GB10 (SM121) single- and multi-stream serving.
6note
7note Compose in any OM-2 unit (no external deps, zero heap, zero C, zero CUDA).
8
9const SPEC_MAX_SEQS ulong 16
10const SPEC_MAX_DRAFT ulong 8
11const SPEC_MAX_CURVE ulong 32
12const SPEC_MAX_Q ulong 128
13const SPEC_SAMP_WIN ulong 9
14const SPEC_SAMP_MIN ulong 5
15const SPEC_FWD_WIN ulong 32
16const SPEC_FWD_MIN ulong 5
17
18note Hardware decode throughput curve: (graph_size, baseline_ms), strictly increasing in graph_size.
19global specCurveN ulong 1
20global specCurveM ulong 32
21global specCurveMs f32 32
22
23note Per-num_seqs SamplerRuntimeEstimator (slots 0..16; slot 0 = global fallback).
24global specSampRing f32 153
25global specSampSz ulong 17
26global specSampNext ulong 17
27
28note Per-graph-size ForwardRuntimeEstimator (up to 32 curve points x 32-sample ring).
29global specFwdRing f32 1024
30global specFwdSz ulong 32
31global specFwdNext ulong 32
32
33note Per-stream MTP state (up to SPEC_MAX_SEQS streams, up to SPEC_MAX_DRAFT draft tokens each).
34global specSeqNd ulong 16
35global specSeqSchedCnt ulong 16
36global specSeqProcCnt ulong 16
37global specSeqHasDraft ulong 16
38global specSeqHasPrev ulong 16
39global specSeqHasCur ulong 16
40global specSeqPrevRate f32 128
41global specSeqCurRate f32 128
42
43note Scheduler outputs & diagnostics.
44global specOutLens ulong 16
45global specOutBestBsz ulong 1
46global specOutBestTps f32 1
47global specOutSlopeMs f32 1
48global specOutSampMs f32 1
49
50note Scratch queues for sorting candidate (rate, seq_idx) pairs (up to SPEC_MAX_Q = 128).
51global specQBszRate f32 128
52global specQBszSeq ulong 128
53global specQSeqRate f32 128
54global specQSeqSeq ulong 128
55global specTmpSort f32 32
56
57fn specResetAll to void
58put specCurveN 0 0
59for i ulong 32
60put specCurveM i 0
61put specCurveMs i 0.0f32
62put specFwdSz i 0
63put specFwdNext i 0
64end
65for i ulong 17
66put specSampSz i 0
67put specSampNext i 0
68end
69for i ulong 153
70put specSampRing i 0.0f32
71end
72for i ulong 1024
73put specFwdRing i 0.0f32
74end
75for s ulong SPEC_MAX_SEQS
76put specSeqNd s 0
77put specSeqSchedCnt s 0
78put specSeqProcCnt s 0
79put specSeqHasDraft s 0
80put specSeqHasPrev s 0
81put specSeqHasCur s 0
82put specOutLens s 0
83end
84for i ulong 128
85put specSeqPrevRate i 0.0f32
86put specSeqCurRate i 0.0f32
87end
88put specOutBestBsz 0 0
89put specOutBestTps 0 0.0f32
90put specOutSlopeMs 0 0.0f32
91put specOutSampMs 0 0.0f32
92end
93
94note Append a (graph_size, baseline_ms) point; enforces strictly increasing graph_size and > 0 ms.
95fn specAddCurvePoint gSize ulong baseMs f32 to bool
96n = get specCurveN 0
97if ge n SPEC_MAX_CURVE
98ret false
99end
100if or eq gSize 0 le baseMs 0.0f32
101ret false
102end
103if gt n 0
104if le gSize get specCurveM sub n 1
105ret false
106end
107end
108put specCurveM n gSize
109put specCurveMs n baseMs
110put specCurveN 0 add n 1
111ret true
112end
113
114note Default GB10 (SM121, 236.9 GB/s LPDDR5X) baseline curve modeled from measured single- vs multi-row
115note scaling (q36 Full-W4 ~14.7 ms at M=1; multi-row weight reuse with MoE expert growth at larger M).
116note Callers can replace this with live calibrated points via specResetAll + specAddCurvePoint.
117fn specInitGb10DefaultCurve to void
118put specCurveN 0 0
119ok = specAddCurvePoint 1 14.73f32
120ok = specAddCurvePoint 2 16.20f32
121ok = specAddCurvePoint 3 17.65f32
122ok = specAddCurvePoint 4 19.10f32
123ok = specAddCurvePoint 6 22.10f32
124ok = specAddCurvePoint 8 25.40f32
125ok = specAddCurvePoint 12 33.20f32
126ok = specAddCurvePoint 16 42.50f32
127ok = specAddCurvePoint 24 63.00f32
128ok = specAddCurvePoint 32 86.00f32
129ok = specAddCurvePoint 48 138.00f32
130ok = specAddCurvePoint 64 196.00f32
131end
132
133note Insertion sort specTmpSort[0..n-1] ascending and return exact median.
134fn specMedianTmp n ulong to f32
135if eq n 0
136ret 0.0f32
137end
138for i ulong n bound 32
139v = get specTmpSort i
140j = i
141while gt j 0 bound 32
142prev = get specTmpSort sub j 1
143if le prev v
144break
145end
146put specTmpSort j prev
147j = sub j 1
148end
149put specTmpSort j v
150end
151mid = div n 2
152if eq rem n 2 0
153ret mul 0.5f32 add get specTmpSort sub mid 1 get specTmpSort mid
154end
155ret get specTmpSort mid
156end
157
158fn specSampUpdateSlot slot ulong valMs f32 to void
159nxt = get specSampNext slot
160base = mul slot SPEC_SAMP_WIN
161put specSampRing add base nxt valMs
162put specSampNext slot rem add nxt 1 SPEC_SAMP_WIN
163sz = get specSampSz slot
164if lt sz SPEC_SAMP_WIN
165put specSampSz slot add sz 1
166end
167end
168
169fn specSampGetSlot slot ulong to f32
170sz = get specSampSz slot
171if lt sz SPEC_SAMP_MIN
172ret -1.0f32
173end
174base = mul slot SPEC_SAMP_WIN
175for i ulong sz bound 9
176put specTmpSort i get specSampRing add base i
177end
178ret specMedianTmp sz
179end
180
181fn specFindCurveIdx gSize ulong to ulong
182n = get specCurveN 0
183for i ulong n bound 32
184if ge get specCurveM i gSize
185ret i
186end
187end
188ret SPEC_MAX_CURVE
189end
190
191fn specFwdUpdateIdx cidx ulong normRes f32 to void
192nxt = get specFwdNext cidx
193base = mul cidx SPEC_FWD_WIN
194put specFwdRing add base nxt normRes
195put specFwdNext cidx rem add nxt 1 SPEC_FWD_WIN
196sz = get specFwdSz cidx
197if lt sz SPEC_FWD_WIN
198put specFwdSz cidx add sz 1
199end
200end
201
202fn specFwdGetSlope to f32
203n = get specCurveN 0
204gMeds = new f32 32 0.0f32
205gCnt = ulong 0
206for c ulong n bound 32
207sz = get specFwdSz c
208if ge sz SPEC_FWD_MIN
209base = mul c SPEC_FWD_WIN
210for i ulong sz bound 32
211put specTmpSort i get specFwdRing add base i
212end
213put gMeds gCnt specMedianTmp sz
214gCnt = add gCnt 1
215end
216end
217if eq gCnt 0
218ret 0.0f32
219end
220for i ulong gCnt bound 32
221put specTmpSort i get gMeds i
222end
223slope = specMedianTmp gCnt
224if lt slope 0.0f32
225ret 0.0f32
226end
227ret slope
228end
229
230note Feed observed decode forward_ms and sampler_ms back into the online estimators.
231fn specUpdateRuntime numSeqs ulong gSize ulong forwardMs f32 samplerMs f32 to bool
232cidx = specFindCurveIdx gSize
233if ge cidx get specCurveN 0
234ret false
235end
236baseMs = get specCurveMs cidx
237gsz = get specCurveM cidx
238normRes = div sub forwardMs baseMs f32 gsz
239specFwdUpdateIdx cidx normRes
240if and gt numSeqs 0 le numSeqs SPEC_MAX_SEQS
241specSampUpdateSlot numSeqs samplerMs
242end
243specSampUpdateSlot 0 samplerMs
244ret true
245end
246
247note Configure per-stream MTP state before calling specSchedule.
248fn specSetStream s ulong nd ulong schedCnt ulong procCnt ulong hasDraft ulong to bool
249if or ge s SPEC_MAX_SEQS gt nd SPEC_MAX_DRAFT
250ret false
251end
252put specSeqNd s nd
253put specSeqSchedCnt s schedCnt
254put specSeqProcCnt s procCnt
255put specSeqHasDraft s hasDraft
256ret true
257end
258
259fn specSetStreamRates s ulong isPrev bool hasRates ulong rates view f32 * nd ulong to bool
260if or ge s SPEC_MAX_SEQS gt nd SPEC_MAX_DRAFT
261ret false
262end
263base = mul s SPEC_MAX_DRAFT
264if isPrev
265put specSeqHasPrev s hasRates
266for j ulong nd bound 8
267put specSeqPrevRate add base j get rates j
268end
269else
270put specSeqHasCur s hasRates
271for j ulong nd bound 8
272put specSeqCurRate add base j get rates j
273end
274end
275ret true
276end
277
278fn specSortBszQueue n ulong to void
279for i ulong n bound 128
280rv = get specQBszRate i
281sv = get specQBszSeq i
282j = i
283while gt j 0 bound 128
284pr = get specQBszRate sub j 1
285ps = get specQBszSeq sub j 1
286swap = lt pr rv
287if and not swap eq pr rv
288swap = gt ps sv
289end
290if not swap
291break
292end
293put specQBszRate j pr
294put specQBszSeq j ps
295j = sub j 1
296end
297put specQBszRate j rv
298put specQBszSeq j sv
299end
300end
301
302fn specSortSeqQueue n ulong to void
303for i ulong n bound 128
304rv = get specQSeqRate i
305sv = get specQSeqSeq i
306j = i
307while gt j 0 bound 128
308pr = get specQSeqRate sub j 1
309ps = get specQSeqSeq sub j 1
310swap = lt pr rv
311if and not swap eq pr rv
312swap = gt ps sv
313end
314if not swap
315break
316end
317put specQSeqRate j pr
318put specQSeqSeq j ps
319j = sub j 1
320end
321put specQSeqRate j rv
322put specQSeqSeq j sv
323end
324end
325
326fn specGetSamplerEst numSeqs ulong to f32
327est = f32 -1.0f32
328if and gt numSeqs 0 le numSeqs SPEC_MAX_SEQS
329est = specSampGetSlot numSeqs
330end
331if lt est 0.0f32
332est = specSampGetSlot 0
333end
334if lt est 0.0f32
335ret 0.0f32
336end
337ret est
338end
339
340fn specRateBsz s ulong j ulong paired bool hasDr bool to f32
341base = add mul s SPEC_MAX_DRAFT j
342if paired
343if ne get specSeqHasPrev s 0
344ret get specSeqPrevRate base
345end
346else
347if ne get specSeqHasCur s 0
348ret get specSeqCurRate base
349end
350end
351if hasDr
352ret 1.0f32
353end
354ret 0.0f32
355end
356
357fn specRateSeq s ulong j ulong hasDr bool to f32
358if ne get specSeqHasCur s 0
359ret get specSeqCurRate add mul s SPEC_MAX_DRAFT j
360end
361if hasDr
362ret 1.0f32
363end
364ret 0.0f32
365end
366
367fn specPushStream s ulong qLen ulong to long
368sc = get specSeqSchedCnt s
369pc = get specSeqProcCnt s
370if or gt pc sc gt sc add pc 1
371ret -1
372end
373paired = eq pc sc
374hasDr = or ne get specSeqHasDraft s 0 eq sc add pc 1
375nd = get specSeqNd s
376ql = qLen
377for j ulong nd bound 8
378if ge ql SPEC_MAX_Q
379ret -1
380end
381put specQBszRate ql specRateBsz s j paired hasDr
382put specQBszSeq ql s
383put specQSeqRate ql specRateSeq s j hasDr
384put specQSeqSeq ql s
385ql = add ql 1
386end
387ret long ql
388end
389
390fn specBuildQueues numSeqs ulong to long
391qLen = ulong 0
392for s ulong numSeqs bound 16
393nxt = specPushStream s qLen
394if lt nxt 0
395ret -1
396end
397qLen = ulong nxt
398end
399ret long qLen
400end
401
402fn specEvalStep curExp f32 curBsz ulong cIdx ulong to f32
403cN = get specCurveN 0
404if ge cIdx cN
405ret 0.0f32
406end
407gSz = get specCurveM cIdx
408bMs = get specCurveMs cIdx
409tMs = add add bMs mul get specOutSlopeMs 0 f32 gSz get specOutSampMs 0
410if le tMs 0.0f32
411ret 0.0f32
412end
413ret div curExp tMs
414end
415
416fn specPickBestBsz numSeqs ulong qLen ulong to ulong
417cN = get specCurveN 0
418bestTps = f32 0.0f32
419bestBsz = numSeqs
420curExp = f32 numSeqs
421curBsz = numSeqs
422cIdx = ulong 0
423for step ulong add qLen 1 bound 129
424if gt step 0
425curExp = add curExp get specQBszRate sub step 1
426curBsz = add curBsz 1
427end
428while and lt cIdx cN gt curBsz get specCurveM cIdx bound 32
429cIdx = add cIdx 1
430end
431tps = specEvalStep curExp curBsz cIdx
432if gt tps bestTps
433bestTps = tps
434bestBsz = curBsz
435end
436end
437put specOutBestTps 0 bestTps
438ret bestBsz
439end
440
441note Core MTPVarlen::schedule() in pure OM-2.
442note Populates specOutLens[0..numSeqs-1], specOutBestBsz, specOutBestTps.
443note Returns true on valid schedule, false on invalid arguments/invariant violation.
444fn specSchedule numSeqs ulong to bool
445if or eq numSeqs 0 gt numSeqs SPEC_MAX_SEQS
446ret false
447end
448if eq get specCurveN 0 0
449for s ulong numSeqs bound 16
450put specOutLens s add 1 get specSeqNd s
451end
452put specOutBestBsz 0 numSeqs
453ret true
454end
455ql = specBuildQueues numSeqs
456if lt ql 0
457ret false
458end
459qLen = ulong ql
460specSortBszQueue qLen
461specSortSeqQueue qLen
462put specOutSampMs 0 specGetSamplerEst numSeqs
463put specOutSlopeMs 0 specFwdGetSlope
464bestBsz = specPickBestBsz numSeqs qLen
465put specOutBestBsz 0 bestBsz
466for s ulong numSeqs bound 16
467put specOutLens s 1
468end
469extra = sub bestBsz numSeqs
470for i ulong extra bound 128
471si = get specQSeqSeq i
472put specOutLens si add get specOutLens si 1
473end
474ret true
475end
476
477note Lossless greedy speculative verification.
478note targetToks[0..nDraft] = argmax logits from the 1+nDraft batched forward pass:
479note targetToks[j] is the target model's greedy next token given prefix + draft[0..j-1].
480note Writes outEmitted[0..a] (a+1 tokens total, bit-identical to target greedy decoding)
481note and returns accepted draft count a in [0..nDraft] (total emitted = a + 1).
482fn specVerifyGreedy targetToks view ulong 9 draftToks view ulong 8 nDraft ulong outEmitted edit ulong 9 to ulong
483acc = ulong 0
484for j ulong nDraft bound 8
485tt = get targetToks j
486put outEmitted j tt
487if eq tt get draftToks j
488acc = add acc 1
489else
490ret acc
491end
492end
493put outEmitted acc get targetToks acc
494ret acc
495end