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/aien-sovereign-core-mv/blob/main/om/scheduler/scheduler.om # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / om / scheduler / scheduler.om` 837 lines ยท 17605 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note core/sovereign-core/om/scheduler/scheduler.om 3 note Pure OM-2 conversion of aien-sovereign-core-mv/crates/aien-scheduler/src/sequence.rs and lib.rs. 4 note Generational 64-bit SequenceId arena, continuous-batching scheduler with chunked prefill, 5 note watermark memory-pressure preemption with anti-thrashing headroom, zero-copy fork admission, 6 note and stale-handle purging. 7 note Compose after si/directives/om2.om and core/sovereign-core/om/scheduler/kv_cache.om. 8 9 const SEQ_ARENA_CAP ulong 64 10 const SEQ_QUEUE_CAP ulong 64 11 const SEQ_BATCH_CAP ulong 64 12 13 note Invalid SequenceId: slot=0xffffffff, generation=0 (generation 0 is strictly invalid). 14 const SEQ_ID_INVALID ulong `18446744069414584320` 15 16 note Priority levels (mirrors aien_platform::Priority) 17 const PRIO_BACKGROUND ulong 0 18 const PRIO_NORMAL ulong 1 19 const PRIO_INTERACTIVE ulong 2 20 const PRIO_REALTIME ulong 3 21 22 note SequencePhase (mirrors sequence.rs::SequencePhase) 23 const PHASE_WAITING ulong 0 24 const PHASE_PREFILL ulong 1 25 const PHASE_PREFILL_INFLIGHT ulong 2 26 const PHASE_DECODE ulong 3 27 const PHASE_DECODE_INFLIGHT ulong 4 28 const PHASE_PREEMPTED ulong 5 29 const PHASE_FINISHED ulong 6 30 const PHASE_CANCELLED ulong 7 31 32 const SCHED_OK ulong 0 33 const SCHED_ERR_ARENA_FULL ulong 1 34 const SCHED_ERR_STALE ulong 2 35 const SCHED_ERR_INVALID_ID ulong 3 36 const SCHED_ERR_QUEUE_FULL ulong 4 37 const SCHED_ERR_KV_FAILED ulong 5 38 39 note Arena arrays (64 slots) 40 global ar_gen ulong 64 41 global ar_active ulong 64 42 global ar_prio ulong 64 43 global ar_phase ulong 64 44 global ar_prompt_len ulong 64 45 global ar_prefilled ulong 64 46 global ar_is_prefilled ulong 64 47 global ar_toks_gen ulong 64 48 global ar_max_toks ulong 64 49 global ar_parent ulong 64 50 global ar_arrival ulong 64 51 52 note SchedulerConfig: 53 note [0]=max_batch_size, [1]=max_batch_tokens, [2]=max_prefill_tokens, 54 note [3]=prefill_chunk_size, [4]=chunk_prefill (0/1), [5]=watermark_blocks 55 global sc_cfg ulong 8 56 57 note SchedulerMetrics & state: 58 note [0]=step_id, [1]=total_steps, [2]=admitted_reqs, [3]=finished_reqs, 59 note [4]=preempted_reqs, [5]=total_prefill_toks, [6]=total_decode_toks, 60 note [7]=chunked_prefill_steps 61 global sc_met ulong 8 62 63 note Waiting queue (ring buffer: head, tail, count in sc_qmeta[0..2]) 64 note Preempted queue (ring buffer: head, tail, count in sc_qmeta[3..5]) 65 note Running list count in sc_qmeta[6] 66 global sc_qmeta ulong 8 67 global sc_wait_q ulong 64 68 global sc_preempt_q ulong 64 69 global sc_running ulong 64 70 71 note Current BatchPlan output: 72 note sc_bmeta: [0]=step_id, [1]=prefill_cnt, [2]=decode_cnt, [3]=total_tokens, [4]=prefill_budget, [5]=preempted_this_step 73 global sc_bmeta ulong 8 74 global sc_bp_seq ulong 64 75 global sc_bp_start ulong 64 76 global sc_bp_len ulong 64 77 global sc_bd_seq ulong 64 78 global sc_bd_pos ulong 64 79 80 note ---- SequenceId bit packing: [slot: u32 high][generation: u32 low] ---- 81 82 fn seq_id_make slot ulong gen ulong to ulong 83 if or eq gen 0 or ge slot SEQ_ARENA_CAP gt gen ulong `4294967295` 84 ret SEQ_ID_INVALID 85 end 86 ret or shl slot ulong 32 and gen ulong 0xffffffff 87 end 88 89 fn seq_id_slot id ulong to ulong 90 ret shr id ulong 32 91 end 92 93 fn seq_id_gen id ulong to ulong 94 ret and id ulong 0xffffffff 95 end 96 97 fn seq_id_is_valid id ulong to bool 98 g = seq_id_gen id 99 if eq g 0 100 ret false 101 end 102 ret lt seq_id_slot id SEQ_ARENA_CAP 103 end 104 105 note ---- SequenceArena ---- 106 107 fn arena_init to void 108 for i ulong SEQ_ARENA_CAP 109 put ar_gen i 1 110 put ar_active i 0 111 put ar_prio i 0 112 put ar_phase i PHASE_WAITING 113 put ar_prompt_len i 0 114 put ar_prefilled i 0 115 put ar_is_prefilled i 0 116 put ar_toks_gen i 0 117 put ar_max_toks i 0 118 put ar_parent i SEQ_ID_INVALID 119 put ar_arrival i 0 120 end 121 end 122 123 fn arena_is_stale id ulong to bool 124 if not seq_id_is_valid id 125 ret true 126 end 127 slot = seq_id_slot id 128 gen = seq_id_gen id 129 if eq get ar_active slot 0 130 ret true 131 end 132 ret ne get ar_gen slot gen 133 end 134 135 fn arena_insert prompt_len ulong max_toks ulong prio ulong arrival_ns ulong to ulong 136 for i ulong SEQ_ARENA_CAP 137 if eq get ar_active i 0 138 gen = get ar_gen i 139 if eq gen 0 140 gen = ulong 1 141 put ar_gen i gen 142 end 143 put ar_active i 1 144 put ar_prio i prio 145 put ar_phase i PHASE_WAITING 146 put ar_prompt_len i prompt_len 147 put ar_prefilled i 0 148 put ar_is_prefilled i 0 149 put ar_toks_gen i 0 150 put ar_max_toks i max_toks 151 put ar_parent i SEQ_ID_INVALID 152 put ar_arrival i arrival_ns 153 ret seq_id_make i gen 154 end 155 end 156 ret SEQ_ID_INVALID 157 end 158 159 fn arena_fork_seq parent_id ulong to ulong 160 if arena_is_stale parent_id 161 ret SEQ_ID_INVALID 162 end 163 pslot = seq_id_slot parent_id 164 for i ulong SEQ_ARENA_CAP 165 if eq get ar_active i 0 166 gen = get ar_gen i 167 if eq gen 0 168 gen = ulong 1 169 put ar_gen i gen 170 end 171 put ar_active i 1 172 put ar_prio i get ar_prio pslot 173 put ar_phase i get ar_phase pslot 174 put ar_prompt_len i get ar_prompt_len pslot 175 put ar_prefilled i get ar_prefilled pslot 176 put ar_is_prefilled i get ar_is_prefilled pslot 177 put ar_toks_gen i get ar_toks_gen pslot 178 put ar_max_toks i get ar_max_toks pslot 179 put ar_parent i parent_id 180 put ar_arrival i get ar_arrival pslot 181 ret seq_id_make i gen 182 end 183 end 184 ret SEQ_ID_INVALID 185 end 186 187 note Release a slot, mark terminal phase, and bump generation so old SequenceIds become stale. 188 fn arena_release id ulong term_phase ulong to ulong 189 if arena_is_stale id 190 ret SCHED_ERR_STALE 191 end 192 slot = seq_id_slot id 193 put ar_phase slot term_phase 194 put ar_active slot 0 195 ngen = add get ar_gen slot 1 196 if or eq ngen 0 gt ngen ulong `4294967295` 197 ngen = ulong 1 198 end 199 put ar_gen slot ngen 200 ret SCHED_OK 201 end 202 203 fn arena_get_phase id ulong to ulong 204 if arena_is_stale id 205 ret PHASE_CANCELLED 206 end 207 ret get ar_phase seq_id_slot id 208 end 209 210 fn arena_get_prefilled id ulong to ulong 211 if arena_is_stale id 212 ret 0 213 end 214 ret get ar_prefilled seq_id_slot id 215 end 216 217 fn arena_get_toks_gen id ulong to ulong 218 if arena_is_stale id 219 ret 0 220 end 221 ret get ar_toks_gen seq_id_slot id 222 end 223 224 note ---- Queue helpers ---- 225 226 fn q_wait_push id ulong to ulong 227 cnt = get sc_qmeta 2 228 if ge cnt SEQ_QUEUE_CAP 229 ret SCHED_ERR_QUEUE_FULL 230 end 231 tail = get sc_qmeta 1 232 put sc_wait_q tail id 233 put sc_qmeta 1 rem add tail 1 SEQ_QUEUE_CAP 234 put sc_qmeta 2 add cnt 1 235 ret SCHED_OK 236 end 237 238 fn q_wait_front to ulong 239 if eq get sc_qmeta 2 0 240 ret SEQ_ID_INVALID 241 end 242 ret get sc_wait_q get sc_qmeta 0 243 end 244 245 fn q_wait_pop to ulong 246 cnt = get sc_qmeta 2 247 if eq cnt 0 248 ret SEQ_ID_INVALID 249 end 250 head = get sc_qmeta 0 251 id = get sc_wait_q head 252 put sc_qmeta 0 rem add head 1 SEQ_QUEUE_CAP 253 put sc_qmeta 2 sub cnt 1 254 ret id 255 end 256 257 fn q_wait_push_front id ulong to ulong 258 cnt = get sc_qmeta 2 259 if ge cnt SEQ_QUEUE_CAP 260 ret SCHED_ERR_QUEUE_FULL 261 end 262 head = get sc_qmeta 0 263 if eq head 0 264 head = sub SEQ_QUEUE_CAP 1 265 else 266 head = sub head 1 267 end 268 put sc_wait_q head id 269 put sc_qmeta 0 head 270 put sc_qmeta 2 add cnt 1 271 ret SCHED_OK 272 end 273 274 fn q_preempt_push id ulong to ulong 275 cnt = get sc_qmeta 5 276 if ge cnt SEQ_QUEUE_CAP 277 ret SCHED_ERR_QUEUE_FULL 278 end 279 tail = get sc_qmeta 4 280 put sc_preempt_q tail id 281 put sc_qmeta 4 rem add tail 1 SEQ_QUEUE_CAP 282 put sc_qmeta 5 add cnt 1 283 ret SCHED_OK 284 end 285 286 fn q_preempt_front to ulong 287 if eq get sc_qmeta 5 0 288 ret SEQ_ID_INVALID 289 end 290 ret get sc_preempt_q get sc_qmeta 3 291 end 292 293 fn q_preempt_pop to ulong 294 cnt = get sc_qmeta 5 295 if eq cnt 0 296 ret SEQ_ID_INVALID 297 end 298 head = get sc_qmeta 3 299 id = get sc_preempt_q head 300 put sc_qmeta 3 rem add head 1 SEQ_QUEUE_CAP 301 put sc_qmeta 5 sub cnt 1 302 ret id 303 end 304 305 fn q_preempt_push_front id ulong to ulong 306 cnt = get sc_qmeta 5 307 if ge cnt SEQ_QUEUE_CAP 308 ret SCHED_ERR_QUEUE_FULL 309 end 310 head = get sc_qmeta 3 311 if eq head 0 312 head = sub SEQ_QUEUE_CAP 1 313 else 314 head = sub head 1 315 end 316 put sc_preempt_q head id 317 put sc_qmeta 3 head 318 put sc_qmeta 5 add cnt 1 319 ret SCHED_OK 320 end 321 322 fn running_push id ulong to ulong 323 cnt = get sc_qmeta 6 324 if ge cnt SEQ_ARENA_CAP 325 ret SCHED_ERR_QUEUE_FULL 326 end 327 put sc_running cnt id 328 put sc_qmeta 6 add cnt 1 329 ret SCHED_OK 330 end 331 332 fn running_remove_at idx ulong to void 333 cnt = get sc_qmeta 6 334 if ge idx cnt 335 ret 336 end 337 last = sub cnt 1 338 for i ulong sub last idx bound 64 339 pos = add idx i 340 put sc_running pos get sc_running add pos 1 341 end 342 put sc_running last SEQ_ID_INVALID 343 put sc_qmeta 6 last 344 end 345 346 note Insertion-sort running_sequences ascending by SequenceId (deterministic scheduling order). 347 fn running_sort to void 348 cnt = get sc_qmeta 6 349 if le cnt 1 350 ret 351 end 352 i = ulong 1 353 while lt i cnt bound 64 354 key = get sc_running i 355 j = i 356 while gt j 0 bound 64 357 prev = get sc_running sub j 1 358 if le prev key 359 break 360 end 361 put sc_running j prev 362 j = sub j 1 363 end 364 put sc_running j key 365 i = add i 1 366 end 367 end 368 369 note ---- Scheduler initialization & public API ---- 370 371 fn sched_init max_bs ulong max_bt ulong max_pt ulong chunk_sz ulong chunk_on ulong watermark ulong to void 372 arena_init 373 put sc_cfg 0 max_bs 374 put sc_cfg 1 max_bt 375 put sc_cfg 2 max_pt 376 put sc_cfg 3 chunk_sz 377 put sc_cfg 4 chunk_on 378 put sc_cfg 5 watermark 379 for i ulong 8 380 put sc_met i 0 381 put sc_qmeta i 0 382 put sc_bmeta i 0 383 end 384 for i ulong SEQ_QUEUE_CAP 385 put sc_wait_q i SEQ_ID_INVALID 386 put sc_preempt_q i SEQ_ID_INVALID 387 put sc_running i SEQ_ID_INVALID 388 end 389 end 390 391 fn sched_waiting_count to ulong 392 ret get sc_qmeta 2 393 end 394 395 fn sched_preempted_count to ulong 396 ret get sc_qmeta 5 397 end 398 399 fn sched_running_count to ulong 400 ret get sc_qmeta 6 401 end 402 403 fn sched_step_id to ulong 404 ret get sc_met 0 405 end 406 407 fn sched_admitted_count to ulong 408 ret get sc_met 2 409 end 410 411 fn sched_finished_count to ulong 412 ret get sc_met 3 413 end 414 415 fn sched_preempted_total to ulong 416 ret get sc_met 4 417 end 418 419 fn sched_prefill_tokens_total to ulong 420 ret get sc_met 5 421 end 422 423 fn sched_decode_tokens_total to ulong 424 ret get sc_met 6 425 end 426 427 fn sched_batch_prefill_count to ulong 428 ret get sc_bmeta 1 429 end 430 431 fn sched_batch_decode_count to ulong 432 ret get sc_bmeta 2 433 end 434 435 fn sched_batch_total_tokens to ulong 436 ret get sc_bmeta 3 437 end 438 439 fn sched_batch_prefill_seq idx ulong to ulong 440 if ge idx get sc_bmeta 1 441 ret SEQ_ID_INVALID 442 end 443 ret get sc_bp_seq idx 444 end 445 446 fn sched_batch_prefill_start idx ulong to ulong 447 if ge idx get sc_bmeta 1 448 ret 0 449 end 450 ret get sc_bp_start idx 451 end 452 453 fn sched_batch_prefill_len idx ulong to ulong 454 if ge idx get sc_bmeta 1 455 ret 0 456 end 457 ret get sc_bp_len idx 458 end 459 460 fn sched_batch_decode_seq idx ulong to ulong 461 if ge idx get sc_bmeta 2 462 ret SEQ_ID_INVALID 463 end 464 ret get sc_bd_seq idx 465 end 466 467 fn sched_batch_decode_pos idx ulong to ulong 468 if ge idx get sc_bmeta 2 469 ret 0 470 end 471 ret get sc_bd_pos idx 472 end 473 474 note Submit a new sequence request to waiting_queue. Returns SequenceId or SEQ_ID_INVALID. 475 fn sched_submit prompt_len ulong max_toks ulong prio ulong arrival_ns ulong to ulong 476 if ge get sc_qmeta 2 SEQ_QUEUE_CAP 477 ret SEQ_ID_INVALID 478 end 479 id = arena_insert prompt_len max_toks prio arrival_ns 480 if eq id SEQ_ID_INVALID 481 ret SEQ_ID_INVALID 482 end 483 st = q_wait_push id 484 if ne st SCHED_OK 485 rc = arena_release id PHASE_CANCELLED 486 ret SEQ_ID_INVALID 487 end 488 put sc_met 2 add get sc_met 2 1 489 ret id 490 end 491 492 note Zero-copy sequence fork: forks arena state and KV block table, adds child directly to running. 493 fn sched_fork_seq parent_id ulong to ulong 494 if arena_is_stale parent_id 495 ret SEQ_ID_INVALID 496 end 497 if not kv_has_sequence parent_id 498 ret SEQ_ID_INVALID 499 end 500 cid = arena_fork_seq parent_id 501 if eq cid SEQ_ID_INVALID 502 ret SEQ_ID_INVALID 503 end 504 kst = kv_fork_seq parent_id cid 505 if ne kst KV_OK 506 rc = arena_release cid PHASE_CANCELLED 507 ret SEQ_ID_INVALID 508 end 509 rst = running_push cid 510 if ne rst SCHED_OK 511 kf = kv_free_sequence cid 512 rc = arena_release cid PHASE_CANCELLED 513 ret SEQ_ID_INVALID 514 end 515 put sc_met 2 add get sc_met 2 1 516 ret cid 517 end 518 519 note Cancel a sequence: frees KV blocks if allocated and marks Cancelled (bumping generation). 520 fn sched_cancel seq_id ulong to ulong 521 if arena_is_stale seq_id 522 ret SCHED_ERR_STALE 523 end 524 if kv_has_sequence seq_id 525 kf = kv_free_sequence seq_id 526 end 527 ret arena_release seq_id PHASE_CANCELLED 528 end 529 530 note Discard stale handles from queue heads and running_sequences without panics. 531 fn sched_purge_stale to void 532 while gt get sc_qmeta 2 0 bound 64 533 fid = q_wait_front 534 if arena_is_stale fid 535 popped = q_wait_pop 536 else 537 break 538 end 539 end 540 while gt get sc_qmeta 5 0 bound 64 541 fid = q_preempt_front 542 if arena_is_stale fid 543 popped = q_preempt_pop 544 else 545 break 546 end 547 end 548 rcnt = get sc_qmeta 6 549 widx = ulong 0 550 for i ulong rcnt bound 64 551 rid = get sc_running i 552 if not arena_is_stale rid 553 put sc_running widx rid 554 widx = add widx 1 555 end 556 end 557 put sc_qmeta 6 widx 558 end 559 560 note Phase 1: Watermark memory-pressure preemption of the lowest-priority running sequence. 561 fn sched_phase1_watermark to void 562 avail = kv_available_blocks 563 wm = get sc_cfg 5 564 rcnt = get sc_qmeta 6 565 if or ge avail wm eq rcnt 0 566 ret 567 end 568 best_idx = ulong 0 569 best_prio = ulong 255 570 found = false 571 for i ulong rcnt bound 64 572 rid = get sc_running i 573 if not arena_is_stale rid 574 p = get ar_prio seq_id_slot rid 575 if or not found lt p best_prio 576 best_prio = p 577 best_idx = i 578 found = true 579 end 580 end 581 end 582 if not found 583 ret 584 end 585 pid = get sc_running best_idx 586 running_remove_at best_idx 587 if kv_has_sequence pid 588 kf = kv_free_sequence pid 589 end 590 pslot = seq_id_slot pid 591 put ar_phase pslot PHASE_PREEMPTED 592 put ar_prefilled pslot 0 593 put ar_is_prefilled pslot 0 594 pst = q_preempt_push pid 595 put sc_met 4 add get sc_met 4 1 596 put sc_bmeta 5 1 597 end 598 599 note Helper: schedule one running sequence (decode or continuing prefill chunk). 600 fn sched_schedule_one rid ulong to void 601 if arena_is_stale rid 602 ret 603 end 604 rslot = seq_id_slot rid 605 if ne get ar_is_prefilled rslot 0 606 if kv_has_sequence rid 607 dcnt = get sc_bmeta 2 608 pos = add get ar_prompt_len rslot get ar_toks_gen rslot 609 put sc_bd_seq dcnt rid 610 put sc_bd_pos dcnt pos 611 put sc_bmeta 2 add dcnt 1 612 put sc_bmeta 3 add get sc_bmeta 3 1 613 end 614 ret 615 end 616 plen = get ar_prompt_len rslot 617 pfill = get ar_prefilled rslot 618 rem_p = ulong 0 619 if gt plen pfill 620 rem_p = sub plen pfill 621 end 622 take = rem_p 623 csz = get sc_cfg 3 624 if gt take csz 625 take = csz 626 end 627 pbud = get sc_bmeta 4 628 if gt take pbud 629 take = pbud 630 end 631 if gt take 0 632 pcnt = get sc_bmeta 1 633 put sc_bp_seq pcnt rid 634 put sc_bp_start pcnt pfill 635 put sc_bp_len pcnt take 636 put sc_bmeta 1 add pcnt 1 637 put sc_bmeta 3 add get sc_bmeta 3 take 638 put sc_bmeta 4 sub pbud take 639 end 640 end 641 642 note Phase 2: Schedule active decode sequences and continuing chunked prefill spans. 643 fn sched_phase2_running to void 644 running_sort 645 rcnt = get sc_qmeta 6 646 for i ulong rcnt bound 64 647 if ge add get sc_bmeta 1 get sc_bmeta 2 get sc_cfg 0 648 break 649 end 650 if ge get sc_bmeta 3 get sc_cfg 1 651 break 652 end 653 sched_schedule_one get sc_running i 654 end 655 end 656 657 note Helper: try admitting one sequence (returns true to keep admitting, false to stop). 658 fn sched_admit_one nid ulong is_pre bool to bool 659 nslot = seq_id_slot nid 660 plen = get ar_prompt_len nslot 661 bsize = kv_block_size 662 req_blks = ulong 0 663 if gt plen 0 664 req_blks = div add plen sub bsize 1 bsize 665 end 666 min_need = req_blks 667 if is_pre 668 min_need = add req_blks get sc_cfg 5 669 end 670 if lt kv_available_blocks min_need 671 ret false 672 end 673 if is_pre 674 popped = q_preempt_pop 675 else 676 popped = q_wait_pop 677 end 678 note Zero-copy fork invariant: pre-allocated KV table skips prefill and enters decode directly. 679 if kv_has_sequence nid 680 put ar_is_prefilled nslot 1 681 put ar_phase nslot PHASE_DECODE 682 put ar_prefilled nslot plen 683 dcnt = get sc_bmeta 2 684 put sc_bd_seq dcnt nid 685 put sc_bd_pos dcnt add plen get ar_toks_gen nslot 686 put sc_bmeta 2 add dcnt 1 687 put sc_bmeta 3 add get sc_bmeta 3 1 688 rst = running_push nid 689 ret true 690 end 691 take = plen 692 if ne get sc_cfg 4 0 693 if gt take get sc_cfg 3 694 take = get sc_cfg 3 695 end 696 end 697 pbud = get sc_bmeta 4 698 if gt take pbud 699 take = pbud 700 end 701 if eq take 0 702 if is_pre 703 ps = q_preempt_push_front nid 704 else 705 ps = q_wait_push_front nid 706 end 707 ret false 708 end 709 if ne kv_allocate_sequence nid plen KV_OK 710 if is_pre 711 ps = q_preempt_push_front nid 712 else 713 ps = q_wait_push_front nid 714 end 715 ret false 716 end 717 pcnt = get sc_bmeta 1 718 put sc_bp_seq pcnt nid 719 put sc_bp_start pcnt 0 720 put sc_bp_len pcnt take 721 put sc_bmeta 1 add pcnt 1 722 put sc_bmeta 3 add get sc_bmeta 3 take 723 put sc_bmeta 4 sub pbud take 724 put ar_phase nslot PHASE_PREFILL 725 rst = running_push nid 726 ret true 727 end 728 729 note Phase 3: Admit preempted (with watermark headroom) then waiting sequences. 730 fn sched_phase3_admit to void 731 if ne get sc_bmeta 5 0 732 ret 733 end 734 while true bound 64 735 if ge add get sc_bmeta 1 get sc_bmeta 2 get sc_cfg 0 736 break 737 end 738 if ge get sc_bmeta 3 get sc_cfg 1 739 break 740 end 741 if eq get sc_bmeta 4 0 742 break 743 end 744 is_pre = false 745 nid = SEQ_ID_INVALID 746 if gt get sc_qmeta 5 0 747 nid = q_preempt_front 748 is_pre = true 749 else 750 if gt get sc_qmeta 2 0 751 nid = q_wait_front 752 else 753 break 754 end 755 end 756 if arena_is_stale nid 757 if is_pre 758 popped = q_preempt_pop 759 else 760 popped = q_wait_pop 761 end 762 continue 763 end 764 if not sched_admit_one nid is_pre 765 break 766 end 767 end 768 end 769 770 note Build the next BatchPlan (returns true if non-empty batch was scheduled). 771 fn sched_build_batch to bool 772 sid = add get sc_met 0 1 773 put sc_met 0 sid 774 sched_purge_stale 775 put sc_bmeta 0 sid 776 put sc_bmeta 1 0 777 put sc_bmeta 2 0 778 put sc_bmeta 3 0 779 put sc_bmeta 4 get sc_cfg 2 780 put sc_bmeta 5 0 781 sched_phase1_watermark 782 sched_phase2_running 783 sched_phase3_admit 784 if and eq get sc_bmeta 1 0 eq get sc_bmeta 2 0 785 ret false 786 end 787 ret true 788 end 789 790 note Complete the scheduled batch step: advance prefill spans, append KV tokens for decode items, 791 note and finish sequences that reached max_tokens (freeing their KV blocks). 792 fn sched_complete_step to ulong 793 pcnt = get sc_bmeta 1 794 dcnt = get sc_bmeta 2 795 put sc_met 1 add get sc_met 1 1 796 if gt pcnt 0 797 put sc_met 7 add get sc_met 7 1 798 end 799 for i ulong pcnt bound 64 800 pid = get sc_bp_seq i 801 plen_chunk = get sc_bp_len i 802 put sc_met 5 add get sc_met 5 plen_chunk 803 if not arena_is_stale pid 804 pslot = seq_id_slot pid 805 if eq get ar_is_prefilled pslot 0 806 np = add get ar_prefilled pslot plen_chunk 807 put ar_prefilled pslot np 808 if ge np get ar_prompt_len pslot 809 put ar_is_prefilled pslot 1 810 put ar_phase pslot PHASE_DECODE 811 end 812 end 813 end 814 end 815 ares = new KvAppendRes 0 0 0 0 816 for i ulong dcnt bound 64 817 did = get sc_bd_seq i 818 if arena_is_stale did 819 continue 820 end 821 dslot = seq_id_slot did 822 st = kv_append_token did ares 823 if ne st KV_OK 824 ret SCHED_ERR_KV_FAILED 825 end 826 ng = add get ar_toks_gen dslot 1 827 put ar_toks_gen dslot ng 828 put sc_met 6 add get sc_met 6 1 829 if ge ng get ar_max_toks dslot 830 kf = kv_free_sequence did 831 rc = arena_release did PHASE_FINISHED 832 put sc_met 3 add get sc_met 3 1 833 end 834 end 835 sched_purge_stale 836 ret SCHED_OK 837 end