kapsl-llm 0.1.0

Large language model inference with GGUF and ONNX backend support for Kapsl
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use crate::block_manager::BlockManager;
use crate::sequence::{FinishReason, Sequence, SequenceGroup, SequenceGroupOutput, SequenceStatus};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};

#[derive(Clone)]
pub struct SchedulerConfig {
    pub max_num_batched_tokens: usize,
    pub max_num_seqs: usize,
    pub max_paddings: usize,
}

pub struct SchedulerOutputs {
    pub scheduled_seq_groups: Vec<Arc<Mutex<SequenceGroup>>>,
}

pub struct LLMScheduler {
    config: SchedulerConfig,
    waiting_queue: VecDeque<Arc<Mutex<SequenceGroup>>>,
    running_queue: VecDeque<Arc<Mutex<SequenceGroup>>>,
    swapped_queue: VecDeque<Arc<Mutex<SequenceGroup>>>,
    block_manager: BlockManager,
}

impl LLMScheduler {
    pub fn new(config: SchedulerConfig, block_manager: BlockManager) -> Self {
        Self {
            config,
            waiting_queue: VecDeque::new(),
            running_queue: VecDeque::new(),
            swapped_queue: VecDeque::new(),
            block_manager,
        }
    }

    /// Expose scheduler config fields for engine rebuild in `with_shared_pool`.
    pub fn config_max_num_seqs(&self) -> usize {
        self.config.max_num_seqs
    }

    pub fn config_max_paddings(&self) -> usize {
        self.config.max_paddings
    }

    pub fn config_max_num_batched_tokens(&self) -> usize {
        self.config.max_num_batched_tokens
    }

    pub fn add_sequence_group(&mut self, seq_group: SequenceGroup) {
        self.waiting_queue
            .push_back(Arc::new(Mutex::new(seq_group)));
    }

    pub fn active_sequence_ids(&self) -> Vec<u64> {
        let mut ids = Vec::new();
        for group_arc in self
            .waiting_queue
            .iter()
            .chain(self.running_queue.iter())
            .chain(self.swapped_queue.iter())
        {
            if let Ok(group) = group_arc.lock() {
                ids.extend(group.sequences.keys().copied());
            }
        }
        ids
    }

    pub fn schedule(&mut self) -> SchedulerOutputs {
        // Calculate current running tokens (decode): 1 token per running sequence.
        let mut num_batched_tokens = 0usize;

        for group in &self.running_queue {
            let group = group.lock().unwrap();
            num_batched_tokens += group.cached_running_count();
        }

        let block_size = self.block_manager.block_size();

        // 1. Swap in swapped-out groups (best-effort FIFO).
        while let Some(group_arc) = self.swapped_queue.front().cloned() {
            let cancelled = {
                let group = group_arc.lock().unwrap();
                group
                    .cancellation
                    .as_ref()
                    .is_some_and(|t| t.is_cancelled())
            };
            if cancelled {
                Self::cancel_group(&group_arc);
                let _ = self.swapped_queue.pop_front();
                continue;
            }

            let seqs: Vec<(u64, Arc<Mutex<Sequence>>)> = {
                let group = group_arc.lock().unwrap();
                if group.is_finished() {
                    // Nothing to do; drop it.
                    drop(group);
                    let _ = self.swapped_queue.pop_front();
                    continue;
                }

                group
                    .sequences
                    .iter()
                    .map(|(seq_id, seq_arc)| (*seq_id, seq_arc.clone()))
                    .collect()
            };

            let mut requirements: Vec<(u64, Arc<Mutex<Sequence>>, usize)> =
                Vec::with_capacity(seqs.len());
            let mut total_additional_blocks = 0usize;

            for (seq_id, seq_arc) in &seqs {
                let (is_finished, seq_len) = {
                    let seq = seq_arc.lock().unwrap();
                    (seq.is_finished(), seq.get_len())
                };
                if is_finished {
                    continue;
                }

                let required_blocks = seq_len.div_ceil(block_size);
                let existing_blocks = self
                    .block_manager
                    .get_block_table(*seq_id)
                    .map(|table| table.len())
                    .unwrap_or(0);
                let additional_blocks = required_blocks.saturating_sub(existing_blocks);
                total_additional_blocks += additional_blocks;
                requirements.push((*seq_id, seq_arc.clone(), additional_blocks));
            }

            let group_decode_tokens = requirements.len();
            if group_decode_tokens == 0 {
                let _ = self.swapped_queue.pop_front();
                continue;
            }

            if num_batched_tokens + group_decode_tokens > self.config.max_num_batched_tokens {
                break;
            }

            if !self.block_manager.can_allocate(total_additional_blocks) {
                break;
            }

            let mut status_updates: Vec<(SequenceStatus, SequenceStatus)> =
                Vec::with_capacity(requirements.len());
            for (seq_id, seq_arc, additional_blocks) in &requirements {
                for _ in 0..*additional_blocks {
                    let _ = self.block_manager.allocate(*seq_id);
                }

                let (old_status, new_status) = {
                    let mut seq = seq_arc.lock().unwrap();
                    let old_status = seq.status;
                    if !seq.is_finished() && seq.status != SequenceStatus::Running {
                        seq.status = SequenceStatus::Running;
                    }
                    (old_status, seq.status)
                };
                if old_status != new_status {
                    status_updates.push((old_status, new_status));
                }
            }
            if !status_updates.is_empty() {
                let mut group = group_arc.lock().unwrap();
                for (old_status, new_status) in status_updates {
                    group.update_seq_status(old_status, new_status);
                }
            }

            let group_arc = self.swapped_queue.pop_front().unwrap();
            self.running_queue.push_back(group_arc);
            num_batched_tokens += group_decode_tokens;
        }

        // 2. Schedule waiting groups (Prefill).
        // Try to add new groups from waiting queue
        while let Some(group_arc) = self.waiting_queue.front().cloned() {
            let cancelled = {
                let group = group_arc.lock().unwrap();
                group
                    .cancellation
                    .as_ref()
                    .is_some_and(|t| t.is_cancelled())
            };
            if cancelled {
                Self::cancel_group(&group_arc);
                let _ = self.waiting_queue.pop_front();
                continue;
            }

            let seqs: Vec<(u64, Arc<Mutex<Sequence>>)> = {
                let group = group_arc.lock().unwrap();
                if group.is_finished() {
                    // Unexpected in the waiting queue, but safe to drop.
                    drop(group);
                    let _ = self.waiting_queue.pop_front();
                    continue;
                }

                group
                    .sequences
                    .iter()
                    .map(|(seq_id, seq_arc)| (*seq_id, seq_arc.clone()))
                    .collect()
            };

            let mut num_pending_tokens = 0usize;
            let mut requirements: Vec<(u64, Arc<Mutex<Sequence>>, usize)> =
                Vec::with_capacity(seqs.len());
            let mut total_additional_blocks = 0usize;

            for (seq_id, seq_arc) in &seqs {
                let (is_finished, seq_len, kv_cached_len) = {
                    let seq = seq_arc.lock().unwrap();
                    (seq.is_finished(), seq.get_len(), seq.kv_cached_len)
                };
                if is_finished {
                    continue;
                }

                num_pending_tokens += seq_len.saturating_sub(kv_cached_len);

                let required_blocks = seq_len.div_ceil(block_size);
                let existing_blocks = self
                    .block_manager
                    .get_block_table(*seq_id)
                    .map(|table| table.len())
                    .unwrap_or(0);
                let additional_blocks = required_blocks.saturating_sub(existing_blocks);
                total_additional_blocks += additional_blocks;
                requirements.push((*seq_id, seq_arc.clone(), additional_blocks));
            }

            if num_pending_tokens == 0 {
                // Nothing to prefill; move to running to let decode proceed.
                let group_arc = self.waiting_queue.pop_front().unwrap();
                self.running_queue.push_back(group_arc);
                continue;
            }

            if num_batched_tokens + num_pending_tokens > self.config.max_num_batched_tokens {
                break; // Token limit reached
            }

            if !self.block_manager.can_allocate(total_additional_blocks) {
                // Before stalling, attempt to reclaim blocks from lower-priority
                // running groups. Only proceed if preemption succeeds and we can
                // now satisfy the request.
                if !self.try_preempt_for_blocks(total_additional_blocks)
                    || !self.block_manager.can_allocate(total_additional_blocks)
                {
                    break; // Not enough memory even after preemption
                }
            }

            let mut status_updates: Vec<(SequenceStatus, SequenceStatus)> =
                Vec::with_capacity(requirements.len());
            for (seq_id, seq_arc, additional_blocks) in &requirements {
                for _ in 0..*additional_blocks {
                    let _ = self.block_manager.allocate(*seq_id);
                }

                let (old_status, new_status) = {
                    let mut seq = seq_arc.lock().unwrap();
                    let old_status = seq.status;
                    if !seq.is_finished() && seq.status != SequenceStatus::Running {
                        seq.status = SequenceStatus::Running;
                    }
                    (old_status, seq.status)
                };
                if old_status != new_status {
                    status_updates.push((old_status, new_status));
                }
            }
            if !status_updates.is_empty() {
                let mut group = group_arc.lock().unwrap();
                for (old_status, new_status) in status_updates {
                    group.update_seq_status(old_status, new_status);
                }
            }

            let group_arc = self.waiting_queue.pop_front().unwrap();
            self.running_queue.push_back(group_arc);
            num_batched_tokens += num_pending_tokens;
        }

        let scheduled_seq_groups = self.running_queue.iter().cloned().collect();

        SchedulerOutputs {
            scheduled_seq_groups,
        }
    }

    /// Attempt to free at least `needed_blocks` by swapping out running groups
    /// in ascending priority order (lowest-priority first, then FIFO within a tier).
    ///
    /// Blocks are returned to the pool immediately. The evicted groups are
    /// moved to `swapped_queue` for later re-admission. Returns `true` when the
    /// required number of blocks was successfully freed.
    fn try_preempt_for_blocks(&mut self, needed_blocks: usize) -> bool {
        if needed_blocks == 0 {
            return true;
        }

        // Build (priority, queue_index) pairs for non-finished running groups.
        let mut candidates: Vec<(u8, usize)> = self
            .running_queue
            .iter()
            .enumerate()
            .filter_map(|(idx, group_arc)| {
                let group = group_arc.lock().unwrap();
                if group.is_finished() {
                    None
                } else {
                    Some((group.priority, idx))
                }
            })
            .collect();

        // Lowest priority first; break ties by queue position (FIFO within a tier).
        candidates.sort_unstable_by_key(|&(priority, idx)| (priority, idx));

        let mut freed = 0usize;
        let mut to_swap: Vec<usize> = Vec::new();

        for (_, idx) in &candidates {
            if freed >= needed_blocks {
                break;
            }
            let group_arc = &self.running_queue[*idx];
            let seq_ids: Vec<u64> = group_arc
                .lock()
                .unwrap()
                .sequences
                .keys()
                .copied()
                .collect();
            for seq_id in &seq_ids {
                freed += self.block_manager.blocks_for_sequence(*seq_id);
            }
            to_swap.push(*idx);
        }

        if freed < needed_blocks {
            return false;
        }

        // Remove selected entries from running_queue in reverse-index order so
        // earlier indices stay valid during removal.
        to_swap.sort_unstable_by(|a, b| b.cmp(a));
        for idx in to_swap {
            let group_arc = self.running_queue.remove(idx).unwrap();

            let seq_entries: Vec<(u64, Arc<Mutex<Sequence>>)> = {
                let group = group_arc.lock().unwrap();
                group
                    .sequences
                    .iter()
                    .map(|(id, arc)| (*id, arc.clone()))
                    .collect()
            };

            let mut status_updates: Vec<(SequenceStatus, SequenceStatus)> = Vec::new();
            for (seq_id, seq_arc) in &seq_entries {
                let (old_status, new_status) = {
                    let mut seq = seq_arc.lock().unwrap();
                    let old = seq.status;
                    if !seq.is_finished() {
                        seq.status = SequenceStatus::Swapped;
                    }
                    (old, seq.status)
                };
                if old_status != new_status {
                    status_updates.push((old_status, new_status));
                }
                self.block_manager.free(*seq_id);
            }

            if !status_updates.is_empty() {
                let mut group = group_arc.lock().unwrap();
                for (old, new) in status_updates {
                    group.update_seq_status(old, new);
                }
            }

            self.swapped_queue.push_back(group_arc);
        }

        true
    }

    fn cancel_group(group_arc: &Arc<Mutex<SequenceGroup>>) {
        let (request_id, response_tx, seqs, already_finished) = {
            let group = group_arc.lock().unwrap();
            (
                group.request_id.clone(),
                group.response_tx.clone(),
                group.sequences.values().cloned().collect::<Vec<_>>(),
                group.is_finished(),
            )
        };
        if already_finished {
            return;
        }

        let mut status_updates: Vec<(SequenceStatus, SequenceStatus)> = Vec::new();
        for seq_arc in &seqs {
            let (old_status, new_status) = {
                let mut seq = seq_arc.lock().unwrap();
                let old_status = seq.status;
                if !seq.is_finished() {
                    seq.status = SequenceStatus::Finished(FinishReason::Cancelled);
                }
                (old_status, seq.status)
            };
            if old_status != new_status {
                status_updates.push((old_status, new_status));
            }
        }

        if !status_updates.is_empty() {
            let mut group = group_arc.lock().unwrap();
            for (old_status, new_status) in status_updates {
                group.update_seq_status(old_status, new_status);
            }
        }

        let _ = response_tx.try_send(SequenceGroupOutput {
            request_id,
            text: String::new(),
            finish_reason: Some(FinishReason::Cancelled),
        });
    }

    pub fn free_finished_sequences(&mut self) -> Vec<u64> {
        let mut finished_ids = Vec::new();
        self.running_queue.retain(|group_arc| {
            let group = group_arc.lock().unwrap();
            if group.is_finished() {
                if group.session_id.is_none() {
                    // Free blocks for non-session sequences.
                    for seq_id in group.sequences.keys() {
                        self.block_manager.free(*seq_id);
                        finished_ids.push(*seq_id);
                    }
                }
                false
            } else {
                true
            }
        });
        finished_ids
    }
}

#[path = "scheduler_tests.rs"]
mod scheduler_tests;