-
Notifications
You must be signed in to change notification settings - Fork 78
/
scheduler.rs
489 lines (425 loc) · 16.4 KB
/
scheduler.rs
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use crate::{
config::RllmConfig,
seq::{FinishReason, SchedulingPhase, Sequence, SequenceGroup},
util::limit_str,
HashMap, ModelExec, SequenceManager, TBlockSpaceManager,
};
use aicirt::api::SequenceResult;
use std::{
cell::RefCell,
ops::Deref,
sync::{Arc, Mutex},
vec::Vec,
};
/// Preemption modes.
#[derive(Debug, Clone, Copy)]
pub enum PreemptionMode {
/// Swap out the blocks of the preempted sequences to CPU memory
/// and swap them back in when the sequences are resumed.
Swap,
/// Discard the blocks of the preempted sequences and
/// recompute them when the sequences are resumed, treating the sequences as
/// new prompts.
Recompute,
}
/// Scheduler outputs.
pub struct SchedulerOutputs {
pub prompt_run: bool,
pub num_batched_tokens: usize,
pub blocks_to_swap_in: HashMap<usize, usize>,
pub blocks_to_swap_out: HashMap<usize, usize>,
pub blocks_to_copy: HashMap<usize, Vec<usize>>,
pub next_seq_groups: Vec<SequenceGroup>,
pub dropped_seq_groups: Vec<SequenceGroup>,
}
impl SchedulerOutputs {
pub fn new() -> Self {
SchedulerOutputs {
prompt_run: false,
num_batched_tokens: 0,
blocks_to_swap_in: HashMap::default(),
blocks_to_swap_out: HashMap::default(),
blocks_to_copy: HashMap::default(),
dropped_seq_groups: Vec::new(),
next_seq_groups: Vec::new(),
}
}
fn validate(&self) {
assert!(self.blocks_to_swap_in.is_empty() || self.blocks_to_swap_out.is_empty());
// swapping not impl yet
assert!(self.blocks_to_swap_in.is_empty());
assert!(self.blocks_to_swap_out.is_empty());
self.dropped_seq_groups.iter().for_each(|sg| {
assert!(sg.is_finished());
});
self.next_seq_groups.iter().for_each(|sg| {
assert!(!sg.is_finished());
});
}
pub fn is_empty(&self) -> bool {
// We do not consider the ignored sequence groups.
self.next_seq_groups.is_empty()
&& self.blocks_to_swap_in.is_empty()
&& self.blocks_to_swap_out.is_empty()
&& self.blocks_to_copy.is_empty()
}
pub fn copy_block(&mut self, src_block: usize, dst_block: usize) {
self.blocks_to_copy
.entry(src_block)
.or_insert_with(Vec::new)
.push(dst_block);
}
}
#[derive(Debug, Clone, Copy)]
enum Queue {
/// These have no KV cache stored anywhere. Each sequence group has only 1 sequence.
Waiting,
/// These currently sit on GPU but are not scheduled to run next. The ones to run next are in SchedulerOutputs.
OnGpu,
/// These are swapped out to CPU memory.
Swapped,
}
const NUM_QUEUES: usize = Queue::Swapped as usize + 1;
/// Scheduler.
pub struct Scheduler<ME: ModelExec> {
pub(crate) config: Arc<RllmConfig<ME>>,
prompt_limit: usize,
pub(crate) block_manager: ME::BlockSpaceManager,
freed_seq_ids: RefCell<Vec<usize>>,
seq_mgr: Arc<ME::SequenceManager>,
queues: Mutex<Vec<Vec<SequenceGroup>>>,
}
impl<ME: ModelExec> Scheduler<ME> {
fn q_with<T>(&self, q: Queue, f: impl FnOnce(&mut Vec<SequenceGroup>) -> T) -> T {
let mut queues = self.queues.lock().unwrap();
f(&mut queues[q as usize])
}
fn q_map<T>(&self, q: Queue, mut f: impl FnMut(&mut SequenceGroup) -> T) -> Vec<T> {
self.q_with(q, |q| q.iter_mut().map(&mut f).collect())
}
fn q_for_each(&self, q: Queue, mut f: impl FnMut(&mut SequenceGroup)) {
self.q_with(q, |q| q.iter_mut().for_each(&mut f));
}
fn q_len(&self, q: Queue) -> usize {
self.q_with(q, |q| q.len())
}
fn q_push(&self, q: Queue, sg: SequenceGroup) {
self.q_with(q, move |q| q.push(sg));
}
fn q_pop(&self, q: Queue) -> Option<SequenceGroup> {
self.q_with(q, |q| q.pop())
}
pub fn for_each_waiting_sg(&self, f: impl FnMut(&mut SequenceGroup)) {
self.q_for_each(Queue::Waiting, f)
}
pub fn for_each_ongpu_sg(&self, f: impl FnMut(&mut SequenceGroup)) {
self.q_for_each(Queue::OnGpu, f)
}
pub fn for_each_sg(&self, mut f: impl FnMut(&mut SequenceGroup)) {
self.queues
.lock()
.unwrap()
.iter_mut()
.for_each(|q| q.iter_mut().for_each(&mut f));
}
pub fn for_each_seq(&self, mut f: impl FnMut(&mut Sequence)) {
self.for_each_sg(|sg| sg.seqs.iter_mut().for_each(&mut f));
}
pub fn new(
seq_mgr: Arc<ME::SequenceManager>,
block_manager: ME::BlockSpaceManager,
config: Arc<RllmConfig<ME>>,
) -> Self {
let prompt_limit = std::cmp::min(
config.scheduler.max_model_len,
config.scheduler.max_num_batched_tokens,
);
Self {
config,
seq_mgr,
prompt_limit,
block_manager,
freed_seq_ids: RefCell::new(Vec::new()),
queues: Mutex::new((0..NUM_QUEUES).map(|_| Vec::new()).collect()),
}
}
pub(crate) fn get_freed_seq_ids(&self) -> Vec<usize> {
self.freed_seq_ids.borrow_mut().drain(..).collect()
}
pub fn add_seq_group(&mut self, seq_group: SequenceGroup) {
let len = seq_group.seqs[0].prompt_len;
log::debug!(
"add_seq_group: {}; {len} tokens; {:?}",
seq_group.request_id,
limit_str(&seq_group.prompt, 200)
);
self.q_push(Queue::Waiting, seq_group);
}
pub fn abort_seq_group(&mut self, request_id: &str) {
self.for_each_sg(|seq_group| {
if seq_group.request_id == request_id {
self.set_phase(seq_group, SchedulingPhase::Finished(FinishReason::Aborted));
}
});
}
pub fn has_unfinished_seqs(&self) -> bool {
self.get_num_unfinished_seq_groups() > 0
}
pub fn get_num_unfinished_seq_groups(&self) -> usize {
self.queues.lock().unwrap().iter().map(|q| q.len()).sum()
}
fn drop_finished(outputs: &mut SchedulerOutputs, q: &mut Vec<SequenceGroup>) {
if q.iter().any(|sg| sg.is_finished()) {
let mut not_finished = Vec::new();
for e in q.drain(..) {
if e.is_finished() {
outputs.dropped_seq_groups.push(e);
} else {
not_finished.push(e);
}
}
assert!(q.is_empty());
log::debug!(
"dropped {} seq groups; now {}",
outputs.dropped_seq_groups.len(),
q.len()
);
q.extend(not_finished);
}
}
fn step_drop_finished(&mut self, outputs: &mut SchedulerOutputs) {
self.for_each_sg(|sg| {
if sg.sampling_params.controller.is_some() {
let fuel = sg.usage.fuel_tokens();
let max_fuel = std::cmp::min(
sg.sampling_params.aici_fuel.unwrap_or(usize::MAX),
self.config.aici.max_fuel,
);
if fuel > max_fuel {
log::warn!("seq_group {} ran out of fuel", sg.request_id);
self.set_phase(sg, SchedulingPhase::Finished(FinishReason::AiciOutOfFuel));
}
}
});
self.q_for_each(Queue::Waiting, |seq_group| {
assert!(seq_group.seqs.len() == 1);
let num_prompt_tokens = seq_group.get_seqs(None)[0].get_len();
if num_prompt_tokens > self.prompt_limit {
log::warn!(
"Sequence group {} has a prompt that is too long ({} > {})",
seq_group.request_id,
num_prompt_tokens,
self.prompt_limit
);
self.set_phase(seq_group, SchedulingPhase::Finished(FinishReason::Failed));
}
});
self.queues.lock().unwrap().iter_mut().for_each(|q| {
Self::drop_finished(outputs, q);
});
}
fn max_num_running_seq(&self, q: Queue) -> usize {
self.q_map(q, |sg| sg.get_max_num_running_seqs())
.iter()
.sum()
}
fn step_prompts(&mut self, outputs: &mut SchedulerOutputs) {
log::trace!("step_start_waiting ({} seqs)", self.q_len(Queue::Waiting));
self.sort_by_priority(Queue::Waiting);
let mut num_curr_seqs = self.max_num_running_seq(Queue::OnGpu);
while let Some(mut seq_group) = self.q_pop(Queue::Waiting) {
let num_prompt_tokens = seq_group.only_seq().get_len();
let num_new_seqs = seq_group.get_max_num_running_seqs();
log::trace!(
"seq_group {} has {} prompt tokens and {} new seqs",
seq_group.request_id,
num_prompt_tokens,
num_new_seqs
);
// Check allocation and batch token limits
if !self.block_manager.can_allocate(&seq_group)
|| outputs.num_batched_tokens + num_prompt_tokens
> self.config.scheduler.max_num_batched_tokens
|| num_curr_seqs + num_new_seqs > self.config.scheduler.max_num_seqs
{
self.q_push(Queue::Waiting, seq_group); // Put back the sequence group
break;
}
self._allocate(&mut seq_group);
outputs.next_seq_groups.push(seq_group);
outputs.num_batched_tokens += num_prompt_tokens;
num_curr_seqs += num_new_seqs;
}
}
fn sort_by_priority(&self, q: Queue) {
self.q_with(q, |seq_groups| {
// note that we take elements first from the end of the queue (Vec::pop())
seq_groups.sort_by_key(|g| g.arrival_time);
seq_groups.reverse();
});
}
/// Move sequences from OnGpu queue to outputs.next_seq_groups or
/// to Swapped/Waiting queues (preemption).
fn step_generation(&mut self, outputs: &mut SchedulerOutputs) -> bool {
let mut did_preempt = false;
self.sort_by_priority(Queue::OnGpu);
let mut suspended = Vec::new();
while let Some(mut seq_group) = self.q_pop(Queue::OnGpu) {
if seq_group.is_suspended() {
suspended.push(seq_group);
continue;
}
while !self.block_manager.can_append_slot(&seq_group) {
did_preempt = true;
if self.q_len(Queue::OnGpu) > 0 {
// take the first group in queue (lowest priority)
let victim_seq_group = self.q_with(Queue::OnGpu, |q| q.remove(0));
self._preempt(victim_seq_group, outputs);
} else {
// preempt the current sequence group and stop
self._preempt(seq_group, outputs);
return did_preempt;
}
}
self._append_slots(&mut seq_group, outputs);
outputs.next_seq_groups.push(seq_group);
}
if suspended.len() > 0 {
self.q_with(Queue::OnGpu, |q| q.append(&mut suspended));
}
return did_preempt;
}
fn _allocate(&mut self, seq_group: &mut SequenceGroup) {
self.block_manager.allocate(seq_group);
self.set_phase(seq_group, SchedulingPhase::Running);
}
fn _append_slots(&mut self, seq_group: &mut SequenceGroup, outputs: &mut SchedulerOutputs) {
for seq in &mut seq_group.seqs {
if seq.sched_phase == SchedulingPhase::Running {
self.block_manager.append_slots(seq, outputs);
}
}
}
fn _swap_in(&mut self, seq_group: &mut SequenceGroup, outputs: &mut SchedulerOutputs) {
let src_to_dst = self.block_manager.swap_in(seq_group);
outputs.blocks_to_swap_in.extend(src_to_dst);
}
fn _preempt(&mut self, mut seq_group: SequenceGroup, outputs: &mut SchedulerOutputs) {
let mode = if seq_group.get_max_num_running_seqs() == 1 {
PreemptionMode::Recompute
} else {
PreemptionMode::Swap
};
log::debug!("preempting seq_group {} ({:?})", seq_group.request_id, mode);
match mode {
PreemptionMode::Swap => {
if !self.block_manager.can_swap_out(&seq_group) {
panic!("Aborted due to the lack of CPU swap space. Please increase the swap space to avoid this error.");
}
let map = self.block_manager.swap_out(&mut seq_group);
outputs.blocks_to_swap_out.extend(map);
self.q_push(Queue::Swapped, seq_group);
}
PreemptionMode::Recompute => {
self.set_phase(&mut seq_group, SchedulingPhase::Waiting);
self.q_push(Queue::Waiting, seq_group);
}
}
}
fn step_swap_in(&mut self, outputs: &mut SchedulerOutputs) {
self.sort_by_priority(Queue::Swapped);
let mut num_curr_seqs = self.max_num_running_seq(Queue::OnGpu);
while let Some(mut seq_group) = self.q_pop(Queue::Swapped) {
let num_new_seqs = seq_group.get_max_num_running_seqs();
if !self.block_manager.can_swap_in(&seq_group)
|| num_curr_seqs + num_new_seqs > self.config.scheduler.max_num_seqs
{
self.q_push(Queue::Swapped, seq_group);
break;
}
self._swap_in(&mut seq_group, outputs);
self._append_slots(&mut seq_group, outputs);
num_curr_seqs += num_new_seqs;
self.q_push(Queue::OnGpu, seq_group);
}
}
pub fn step_finished(&mut self, mut outputs: SchedulerOutputs) {
// everything that used to be "next_step" is now just on the GPU
self.q_with(Queue::OnGpu, |seq_groups| {
seq_groups.append(&mut outputs.next_seq_groups);
});
self.for_each_seq(|seq| {
if seq.sched_phase == SchedulingPhase::Suspended {
seq.sched_phase = SchedulingPhase::Running;
}
});
}
pub fn schedule(&mut self) -> SchedulerOutputs {
let mut outputs = SchedulerOutputs::new();
self.step_drop_finished(&mut outputs);
if self.q_len(Queue::Swapped) == 0 {
self.step_prompts(&mut outputs);
}
if outputs.next_seq_groups.is_empty() {
let did_preempt = self.step_generation(&mut outputs);
// Swap in logic for swapped sequences
if !did_preempt {
self.step_swap_in(&mut outputs);
}
// Update num_batched_tokens based on the sequences in the RUNNING state
outputs.num_batched_tokens = self
.q_map(Queue::OnGpu, |sg| {
sg.get_seqs(Some(SchedulingPhase::Running))
.iter()
.map(|seq| seq.get_len())
.sum()
})
.iter()
.sum();
}
outputs.validate();
outputs
}
pub fn finish_seq(&self, seq: &mut Sequence, reason: FinishReason) {
if seq.is_finished() {
return;
}
if reason != FinishReason::AiciStop && seq.has_aici {
seq.aici_logs.push(SequenceResult::from_error(format!(
"\nAbnormal finish: {:?}",
reason
)))
}
seq.sched_phase = SchedulingPhase::Finished(reason);
self.freed_seq_ids.borrow_mut().push(seq.seq_id.to_num());
self.seq_mgr.delete(seq.seq_id);
}
/// Sets the phase of all sequences in a group.
fn set_phase(&self, seq_group: &mut SequenceGroup, status: SchedulingPhase) {
let to_waiting = match status {
SchedulingPhase::Waiting => true,
SchedulingPhase::Suspended => false,
SchedulingPhase::Running => false,
SchedulingPhase::Swapped => false,
SchedulingPhase::Finished(reason) => {
log::debug!("seq_group {} finished: {:?}", seq_group.request_id, reason);
seq_group
.seqs
.iter_mut()
.for_each(|seq| self.finish_seq(seq, reason));
return;
}
};
for seq in seq_group.seqs.iter_mut() {
assert!(!seq.is_finished());
seq.sched_phase = status;
if to_waiting {
seq.clear_computed_kv(self.seq_mgr.deref());
}
}
}
}
pub struct CacheSize {
pub gpu: usize,
pub cpu: usize,
}