diff --git a/docs/scheduling-runtime-design.md b/docs/scheduling-runtime-design.md index 6bae0de87..6810664a0 100644 --- a/docs/scheduling-runtime-design.md +++ b/docs/scheduling-runtime-design.md @@ -171,11 +171,19 @@ candidate serving two requests. Bounded cost-refresh probes are separate from ordinary selection. Selection does not enumerate exponentially many request subsets. -Each prefill capacity competes using the actual tokens it can consume. Atomic -image spans, system boundaries, and prompt length restrict logical work. A -4096-token graph processing a 64-token tail earns 64 prompt units and at most one -output token. There is no smallest-fitting-shape filter, duration ceiling, -forced oldest-request filter, or per-chunk prefill lateness cost. +Each prefill capacity competes using the checkpoint selection loop's predicted +work, including optional cuts and their split and copy budgets, without quoting +snapshot storage. Estimates and quoted selections are cached separately within +each planning scope. Both paths reuse persisted selections containing optional +cuts; persisted cuts can include the anchor itself. Submission quotes storage +for the winning prompt row and stops at its first selected cut or graph +capacity. With sufficient storage, estimated and submitted work agree; storage +shortage can exclude optional cuts, so the estimate can stop earlier than the +quoted selection. Atomic image spans, feature windows, and prompt length also +restrict logical work. A graph with 64 useful prompt tokens earns 64 prompt +units and at most one output token, regardless of physical capacity. There is +no duration ceiling, forced oldest-request filter, or per-chunk prefill +lateness cost. With no active decoder, the age term vanishes. Fully occupied large prefill graphs win when their useful value rate is highest. With decoding streams, a diff --git a/helios/src/scheduler/cache_memory.rs b/helios/src/scheduler/cache_memory.rs index 52a22a5fc..864e99ba0 100644 --- a/helios/src/scheduler/cache_memory.rs +++ b/helios/src/scheduler/cache_memory.rs @@ -8,6 +8,11 @@ use super::*; +#[cfg(test)] +thread_local! { + pub(super) static STORAGE_SIMULATIONS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + #[derive(Clone, Debug, PartialEq, Eq)] pub(super) struct SnapshotQuote { pub(super) snapshots: Vec, @@ -250,6 +255,8 @@ impl RequestScheduler { rank: crate::cache::RetentionRank, allowed: Option<&BTreeSet>, ) -> Option { + #[cfg(test)] + STORAGE_SIMULATIONS.set(STORAGE_SIMULATIONS.get() + 1); let keep_pages = keep_pages.max(self.decode_growth_pages()); let mut cache = self.cache.clone(); let mut backing = self.free_backing(); diff --git a/helios/src/scheduler/checkpoints.rs b/helios/src/scheduler/checkpoints.rs index b1f877334..0fe8a6e6b 100644 --- a/helios/src/scheduler/checkpoints.rs +++ b/helios/src/scheduler/checkpoints.rs @@ -8,7 +8,7 @@ use super::*; -pub(super) type SelectionCache = HashMap; +pub(super) type SelectionCache = HashMap<(RequestHandle, bool), (CheckpointAccounting, bool, bool)>; pub(super) struct PlanningScope<'a>(&'a std::cell::RefCell>); @@ -214,17 +214,25 @@ impl RequestScheduler { } fn checkpoint_selection(&self, request: &LiveRequest) -> (CheckpointAccounting, bool, bool) { + self.checkpoint_selection_cached(request, true) + } + + fn checkpoint_selection_cached( + &self, + request: &LiveRequest, + quote_storage: bool, + ) -> (CheckpointAccounting, bool, bool) { if let Some(value) = self .checkpoint_selections .borrow() .as_ref() - .and_then(|cache| cache.get(&request.handle)) + .and_then(|cache| cache.get(&(request.handle, quote_storage))) { return value.clone(); } - let selection = self.checkpoint_selection_uncached(request); + let selection = self.checkpoint_selection_uncached(request, quote_storage); if let Some(cache) = self.checkpoint_selections.borrow_mut().as_mut() { - cache.insert(request.handle, selection.clone()); + cache.insert((request.handle, quote_storage), selection.clone()); } selection } @@ -232,6 +240,7 @@ impl RequestScheduler { fn checkpoint_selection_uncached( &self, request: &LiveRequest, + quote_storage: bool, ) -> (CheckpointAccounting, bool, bool) { let mut accounting = request.checkpoints.clone(); if accounting @@ -333,7 +342,7 @@ impl RequestScheduler { .filter(|cut| cut.endpoint > start) .count() - usize::from(request.anchor_guard.is_some() && start < anchor); - if self + if quote_storage && self .quote_snapshots_preserving(1, rows, self.checkpoint_headroom(), rank) .is_none() { @@ -351,6 +360,15 @@ impl RequestScheduler { (accounting, unknown, budget) } + pub(super) fn estimated_checkpoint_cut(&self, request: &LiveRequest) -> Option { + let (accounting, _, _) = self.checkpoint_selection_cached(request, false); + accounting + .cuts + .iter() + .find(|cut| cut.endpoint > request.consumed_tokens as usize) + .map(|cut| cut.endpoint) + } + pub(super) fn checkpoint_cut(&self, request: &LiveRequest) -> Option { let (accounting, _, _) = self.checkpoint_selection(request); accounting diff --git a/helios/src/scheduler/policy.rs b/helios/src/scheduler/policy.rs index 10367ac42..8e7abcc67 100644 --- a/helios/src/scheduler/policy.rs +++ b/helios/src/scheduler/policy.rs @@ -327,8 +327,12 @@ impl RequestScheduler { } pub(super) fn prefill_work(&self, request: &LiveRequest, capacity: usize) -> usize { + self.prefill_work_through(request, capacity, self.estimated_checkpoint_cut(request)) + } + + fn prefill_work_through(&self, request: &LiveRequest, capacity: usize, cut: Option) -> usize { let start = request.consumed_tokens as usize; - let mut end = self.checkpoint_cut(request) + let mut end = cut .unwrap_or(request.prompt_tokens) .min(start.saturating_add(capacity)) .min(request.prefill_limit()); @@ -1184,11 +1188,12 @@ impl RequestScheduler { request: r.handle, start_position: r.scheduled_consumed, tokens: if kind.is_prefill() { - if candidate.kind == GraphKind::Mixed { - self.prefill_work(r, candidate.prefill_capacity) + let capacity = if candidate.kind == GraphKind::Mixed { + candidate.prefill_capacity } else { - candidate.work - } + candidate.capacity + }; + self.prefill_work_through(r, capacity, self.checkpoint_cut(r)) } else { if candidate.packed_token_capacity != 0 { lengths.as_ref().unwrap()[slot] diff --git a/helios/src/scheduler/tests.rs b/helios/src/scheduler/tests.rs index c089722d6..68bd6a59b 100644 --- a/helios/src/scheduler/tests.rs +++ b/helios/src/scheduler/tests.rs @@ -241,6 +241,64 @@ fn records( }; records } + +#[test] +fn waiting_prefills_do_not_simulate_checkpoint_storage() { + let mut scheduler = scheduler(4, 128, 16); + scheduler.prefill_graphs(&[128, 256, 512]).unwrap(); + let mut graphs = duration_graphs(4); + for graph in &mut graphs { + graph.estimated_duration_ns = if graph.kind.is_prefill() { 4000 + graph.token_capacity as u64 * 1000 } else { 1 }; + } + scheduler.measured_graph_durations(&graphs).unwrap(); + scheduler.configure_policy(PolicyConfig { + checkpoint_extra_work_fraction: 0.5, + ..Default::default() + }).unwrap(); + for slot in 0..4 { + let mut request = spec(1024, 1); + request.prompt[0] += slot + 100; + request.message_boundaries = vec![100, 900]; + scheduler.admit(request).unwrap(); + } + let mut first_cuts = 0; + for sequence in 1..=64 { + cache_memory::STORAGE_SIMULATIONS.set(0); + let plan = scheduler.plan().unwrap(); + let simulations = cache_memory::STORAGE_SIMULATIONS.get(); + // Two optional-cut quotes and one capture quote each simulate at most twice. + assert!(simulations <= 6, "{simulations} storage simulations in one plan"); + let mut completions = state_records(&scheduler, &plan, sequence); + for (row, lane) in plan.lanes.iter().enumerate() { + if !plan.lane_kind(row).is_prefill() { + continue; + } + let request = scheduler.request(lane.request).unwrap(); + let cut = scheduler.checkpoint_cut(request).unwrap_or(request.prompt_tokens); + let end = lane.start_position as usize + lane.tokens; + assert_eq!(end, cut.min(lane.start_position as usize + plan.token_capacity)); + assert_eq!(plan.token_capacity, *scheduler.prefill_catalog.iter().find(|&&c| c >= lane.tokens).unwrap()); + first_cuts += usize::from(end == 100); + let output = end == request.prompt_tokens; + completions[lane.request.slot as usize] = records( + lane.request, sequence, end as i64, i64::from(output), + if output { 42 } else { -1 }, 1, + )[lane.request.slot as usize].clone(); + } + scheduler.advance_to(scheduler.now_ns + plan.estimated_duration_ns).unwrap(); + scheduler.commit(&plan, Fence(sequence)).unwrap(); + let generated = scheduler.complete(Fence(sequence), &completions, Default::default()).unwrap(); + for token in generated { + scheduler.published_output(token.request, Fence(sequence)).unwrap(); + } + if scheduler.slots.iter().filter_map(|slot| slot.request.as_ref()).all(|r| r.consumed_tokens >= 1024) { + break; + } + } + assert_eq!(first_cuts, 4); + assert!(scheduler.slots.iter().filter_map(|slot| slot.request.as_ref()).all(|r| r.consumed_tokens >= 1024)); +} + fn init(scheduler: &mut RequestScheduler, fence: u64) { let step = scheduler.plan().unwrap(); assert!(matches!(step.kind, GraphKind::Init | GraphKind::Restore));