use std::fmt;
use std::time::Instant;
use crate::paged_kv::SeqId;
use super::request::Token;
#[derive(Debug, Clone, PartialEq)]
pub enum SchedulingPolicy {
FCFS,
SJF,
Priority { preempt_enabled: bool },
FairShare,
}
impl Default for SchedulingPolicy {
fn default() -> Self {
SchedulingPolicy::FCFS
}
}
impl fmt::Display for SchedulingPolicy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SchedulingPolicy::FCFS => write!(f, "FCFS"),
SchedulingPolicy::SJF => write!(f, "SJF"),
SchedulingPolicy::Priority { preempt_enabled } => {
write!(f, "Priority(preempt={})", preempt_enabled)
}
SchedulingPolicy::FairShare => write!(f, "FairShare"),
}
}
}
#[derive(Debug, Clone)]
pub struct BatchSchedule {
pub sequence_ids: Vec<SeqId>,
pub batch_size: usize,
pub total_tokens: usize,
pub prefill_count: usize,
pub decode_count: usize,
}
impl BatchSchedule {
pub fn empty() -> Self {
Self {
sequence_ids: Vec::new(),
batch_size: 0,
total_tokens: 0,
prefill_count: 0,
decode_count: 0,
}
}
pub fn is_empty(&self) -> bool {
self.batch_size == 0
}
}
#[derive(Debug, Clone)]
pub struct TokenOutput {
pub seq_id: SeqId,
pub token: Token,
pub is_eos: bool,
}
#[derive(Debug, Clone, Default)]
pub struct BatcherStats {
pub total_tokens: u64,
pub total_requests: u64,
pub total_preemptions: u64,
pub total_swaps: u64,
pub start_time: Option<Instant>,
}
impl BatcherStats {
pub fn throughput(&self) -> f64 {
if let Some(start) = self.start_time {
let elapsed = start.elapsed().as_secs_f64();
if elapsed > 0.0 {
return self.total_tokens as f64 / elapsed;
}
}
0.0
}
}