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