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

316 lines · 7033 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Pure Seeded OM-2 Host Verification & Negative-Control Gate for MTPVarlen + N-gram Drafter.
3note Compose after: om/gpu/spec/mtp_varlen.om om/gpu/spec/ngram_draft.om
4
5text msgPass "PASS: spec_mtp_varlen gate ("
6text msgChecks " checks, 0 failures)\n"
7text msgFail "FAIL: check="
8text msgNl "\n"
9
10fn ckPrint s view uchar * to void
11r = write 1 s 0 len s
12end
13
14fn ckPrintU x ulong to void
15b = new uchar 24 0
16pos = ulong 24
17v = x
18while true
19pos = sub pos 1
20put b pos uchar add 48 rem v 10
21v = div v 10
22if eq v 0
23break
24end
25end
26r = write 1 b pos sub 24 pos
27end
28
29fn ckFail code ulong to int
30ckPrint msgFail
31ckPrintU code
32ckPrint msgNl
33ret int code
34end
35
36note Check 1: Single-stream light load with high confidence verifies all 5 draft tokens (seqlen_q = 6).
37fn ckTestLightLoad to ulong
38specResetAll
39specInitGb10DefaultCurve
40r = new f32 8 0.0f32
41put r 0 0.95f32
42put r 1 0.90f32
43put r 2 0.85f32
44put r 3 0.80f32
45put r 4 0.75f32
46if not specSetStream 0 5 1 1 1
47ret 10
48end
49if not specSetStreamRates 0 true 1 r 5
50ret 11
51end
52if not specSetStreamRates 0 false 1 r 5
53ret 12
54end
55if not specSchedule 1
56ret 13
57end
58if ne get specOutBestBsz 0 6
59ret 14
60end
61if ne get specOutLens 0 6
62ret 15
63end
64ret 0
65end
66
67note Check 2: Multi-stream heavy load (8 streams) with sharp curve knee trims low-confidence tails
68note and allocates verification slots strictly to highest-confidence streams.
69fn ckTestHeavyLoadTrim to ulong
70specResetAll
71note Synthetic knee curve: M=8 takes 20ms, M=12 takes 24ms, M=16 takes 28ms, M=24 takes 60ms, M=48 takes 140ms.
72ok = specAddCurvePoint 8 20.0f32
73ok = specAddCurvePoint 12 24.0f32
74ok = specAddCurvePoint 16 28.0f32
75ok = specAddCurvePoint 24 60.0f32
76ok = specAddCurvePoint 48 140.0f32
77hi = new f32 8 0.0f32
78put hi 0 0.95f32
79put hi 1 0.90f32
80put hi 2 0.85f32
81put hi 3 0.80f32
82put hi 4 0.10f32
83lo = new f32 8 0.0f32
84put lo 0 0.15f32
85put lo 1 0.10f32
86put lo 2 0.05f32
87put lo 3 0.05f32
88put lo 4 0.02f32
89for s ulong 8
90ok = specSetStream s 5 2 2 1
91if lt s 2
92ok = specSetStreamRates s true 1 hi 5
93ok = specSetStreamRates s false 1 hi 5
94else
95ok = specSetStreamRates s true 1 lo 5
96ok = specSetStreamRates s false 1 lo 5
97end
98end
99if not specSchedule 8
100ret 20
101end
102note Optimal operating point must pick M=16 (8 golden + 8 high-confidence drafts = 4 each for streams 0 & 1),
103note rejecting the low-confidence 0.15/0.10/0.05 tails that would push into the 60ms knee!
104if ne get specOutBestBsz 0 16
105ret 21
106end
107if or ne get specOutLens 0 5 ne get specOutLens 1 5
108ret 22
109end
110for s ulong 6
111if ne get specOutLens add s 2 1
112ret 23
113end
114end
115ret 0
116end
117
118note Check 3: Async ZOS N-2 / N-1 decoupling (schedule_cnt == process_cnt + 1 vs paired).
119note Proves best_bsz is determined by N-2 rates while per-stream allocation uses N-1 rates.
120fn ckTestAsyncDecoupling to ulong
121specResetAll
122ok = specAddCurvePoint 2 10.0f32
123ok = specAddCurvePoint 4 12.0f32
124ok = specAddCurvePoint 8 40.0f32
125note Paired (sched=2, proc=2): prev = N-2, cur = N-1.
126note Stream 0 had high N-2 rates (0.95, 0.90) -> drives best_bsz = 4 (+2 draft slots).
127note In N-1 (cur), Stream 1 flipped to high confidence (0.95, 0.90) and Stream 0 dropped to (0.05, 0.02).
128note So best_bsz MUST be 4 (from N-2), and BOTH draft slots MUST go to Stream 1 (from N-1)!
129hi = new f32 8 0.0f32
130put hi 0 0.95f32
131put hi 1 0.90f32
132put hi 2 0.05f32
133lo = new f32 8 0.0f32
134put lo 0 0.05f32
135put lo 1 0.02f32
136put lo 2 0.01f32
137ok = specSetStream 0 3 2 2 1
138ok = specSetStream 1 3 2 2 1
139ok = specSetStreamRates 0 true 1 hi 3
140ok = specSetStreamRates 1 true 1 lo 3
141ok = specSetStreamRates 0 false 1 lo 3
142ok = specSetStreamRates 1 false 1 hi 3
143if not specSchedule 2
144ret 30
145end
146if ne get specOutBestBsz 0 4
147ret 31
148end
149if or ne get specOutLens 0 1 ne get specOutLens 1 3
150ret 32
151end
152ret 0
153end
154
155note Check 4: Online ForwardRuntimeEstimator + SamplerRuntimeEstimator + negative-slope clamp.
156fn ckTestRuntimeEstimators to ulong
157specResetAll
158ok = specAddCurvePoint 4 20.0f32
159ok = specAddCurvePoint 8 30.0f32
160note Feed 5 negative-residual samples -> slope MUST clamp to 0.0 (never negative!).
161for i ulong 5
162ok = specUpdateRuntime 2 4 18.0f32 0.5f32
163end
164if ne get specOutSlopeMs 0 0.0f32
165ret 40
166end
167sl0 = specFwdGetSlope
168if lt sl0 0.0f32
169ret 41
170end
171note Feed 5 positive-residual samples at graph_size=8: (38.0 - 30.0) / 8 = 1.0 ms/tok.
172note Median across graph 4 (-0.5) and graph 8 (+1.0) = +0.25 ms/tok!
173for i ulong 5
174ok = specUpdateRuntime 2 8 38.0f32 1.2f32
175end
176sl1 = specFwdGetSlope
177if or lt sl1 0.24f32 gt sl1 0.26f32
178ret 42
179end
180samp = specGetSamplerEst 2
181if or lt samp 0.49f32 gt samp 1.21f32
182ret 43
183end
184ret 0
185end
186
187note Check 5: Lossless greedy speculative verifier + N-gram drafter + EMA calibration.
188fn ckTestVerifyAndNgram to ulong
189target = new ulong 9 0
190draft = new ulong 8 0
191emitted = new ulong 9 0
192note Case A: 3 of 5 match, target[3] corrects draft[3] -> acc=3 (4 emitted tokens).
193put draft 0 100
194put draft 1 200
195put draft 2 300
196put draft 3 400
197put draft 4 500
198put target 0 100
199put target 1 200
200put target 2 300
201put target 3 999
202put target 4 888
203put target 5 777
204acc = specVerifyGreedy target draft 5 emitted
205if ne acc 3
206ret 50
207end
208if or ne get emitted 0 100 ne get emitted 3 999
209ret 51
210end
211note Case B: All 5 match -> acc=5 (6 emitted tokens including bonus target[5]).
212put target 3 400
213put target 4 500
214put target 5 600
215acc = specVerifyGreedy target draft 5 emitted
216if ne acc 5
217ret 52
218end
219if ne get emitted 5 600
220ret 53
221end
222note Case C: N-gram drafter finds repeating suffix [10, 20, 30, 40] -> drafts [50, 60, 70, 80, 90].
223ngReset
224for k ulong 10
225ngPushToken mul add k 1 10
226end
227ngPushToken 10
228ngPushToken 20
229ngPushToken 30
230ngPushToken 40
231dToks = new ulong 8 0
232dRates = new f32 8 0.0f32
233nd = ngPropose 5 dToks dRates
234if ne nd 5
235ret 54
236end
237if or ne get dToks 0 50 ne get dToks 4 90
238ret 55
239end
240note Rates must be monotonically non-increasing cumulative probabilities in (0, 1).
241for d ulong 4
242if gt get dRates add d 1 get dRates d
243ret 56
244end
245end
246ngObserveResult 5 5
247if le get ngEmaAccept 0 0.72f32
248ret 57
249end
250ret 0
251end
252
253note Check 6: Hostile/boundary negative controls (must refuse cleanly, never trap).
254fn ckTestNegativeControls to ulong
255specResetAll
256if specAddCurvePoint 0 10.0f32
257ret 60
258end
259if specAddCurvePoint 4 0.0f32
260ret 61
261end
262if not specAddCurvePoint 8 20.0f32
263ret 62
264end
265note Non-increasing graph_size must be refused.
266if specAddCurvePoint 8 25.0f32
267ret 63
268end
269if specAddCurvePoint 4 15.0f32
270ret 64
271end
272note num_seqs == 0 or > SPEC_MAX_SEQS must be refused.
273if specSchedule 0
274ret 65
275end
276if specSchedule 17
277ret 66
278end
279note Inconsistent async counter (schedule_cnt > process_cnt + 1) must be refused.
280ok = specSetStream 0 3 5 2 1
281if specSchedule 1
282ret 67
283end
284ret 0
285end
286
287fn main to int
288rc = ckTestLightLoad
289if ne rc 0
290ret ckFail rc
291end
292rc = ckTestHeavyLoadTrim
293if ne rc 0
294ret ckFail rc
295end
296rc = ckTestAsyncDecoupling
297if ne rc 0
298ret ckFail rc
299end
300rc = ckTestRuntimeEstimators
301if ne rc 0
302ret ckFail rc
303end
304rc = ckTestVerifyAndNgram
305if ne rc 0
306ret ckFail rc
307end
308rc = ckTestNegativeControls
309if ne rc 0
310ret ckFail rc
311end
312ckPrint msgPass
313ckPrintU 6
314ckPrint msgChecks
315ret 0
316end