interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / om / scheduler / scheduler.om

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