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