diff --git a/docs/scheduling-runtime-design.md b/docs/scheduling-runtime-design.md index 6810664a0..fb6fc1d03 100644 --- a/docs/scheduling-runtime-design.md +++ b/docs/scheduling-runtime-design.md @@ -172,18 +172,23 @@ from ordinary selection. Selection does not enumerate exponentially many request subsets. 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. +work, including optional cuts and their split and copy budgets +([checkpoint retention](#prerequisites-and-completion)), without quoting snapshot +storage. Estimates and quoted selections are cached separately within each +planning scope. Both paths reuse a stored selection when it contains any +non-anchor cut. Submission quotes storage for the winning prompt row. When no +quote fails, estimated and submitted work agree. Storage shortage can change +which cuts fit the work budget, so the estimate and quoted selection can differ +in either direction. If the quoted work differs from the winning estimate, an +ordinary prefill plan advances toward the quoted cut up to the catalog maximum and +uses the smallest catalog capacity covering that work, with its cost and duration +recomputed. If a required lookup is unavailable, it retains the winning candidate +and clips work to that candidate's capacity. Mixed, refresh, and discovery plans +retain their capacities and clip work to fit. 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. +Candidate scoring has no smallest-fitting-shape filter, 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/README.md b/helios/README.md index 2920a0f22..3596fdc86 100644 --- a/helios/README.md +++ b/helios/README.md @@ -355,7 +355,7 @@ estimated graph duration, less a duration-proportional waiting cost for requests that are decoding or awaiting terminal retirement. Each loaded prefill capacity up to `prefill_chunk_size` competes on the prompt tokens it can actually consume, which the next selected message boundary, atomic image spans, -and the prompt end limit. No rule pads a logical length to the smallest covering +and the prompt end limit. Candidate scoring does not require the smallest covering capacity. Among candidates of one graph kind and cohort that perform the same logical work, selection first discards any shape whose capacities are all at least as large as another candidate's and larger in at least one. An unmeasured diff --git a/helios/src/scheduler/checkpoints.rs b/helios/src/scheduler/checkpoints.rs index 0fe8a6e6b..98c2e42aa 100644 --- a/helios/src/scheduler/checkpoints.rs +++ b/helios/src/scheduler/checkpoints.rs @@ -694,6 +694,80 @@ mod tests { measure(scheduler, GraphKind::Snapshot, 1, 1); } + #[test] + fn prefill_capacity_matches_submitted_work_without_snapshot_storage() { + for (prompt_tokens, capacity, refresh) in [ + (200, 256, false), + (300, 512, false), + (1024, 512, false), + (300, 256, true), + ] { + let mut scheduler = super::super::tests::scheduler(1, 128, 16); + scheduler.prefill_graphs(&[128, 256, 512]).unwrap(); + linear_costs(&mut scheduler); + scheduler + .configure_policy(PolicyConfig { + checkpoint_extra_work_fraction: 0.5, + ..Default::default() + }) + .unwrap(); + let mut spec = super::super::tests::spec(prompt_tokens, 1); + spec.message_boundaries = vec![100, prompt_tokens]; + let handle = scheduler.admit(spec).unwrap(); + let records = |sequence: i64, consumed: usize| { + let output = consumed == prompt_tokens; + let mut records = vec![CompletionRecord::default(); 4]; + records[handle.slot as usize] = CompletionRecord { + generation: handle.generation as i64, + submission_sequence: sequence, + consumed_tokens: consumed as i64, + generated_tokens: i64::from(output), + token_id: if output { 42 } else { -1 }, + status: 1, + ..Default::default() + }; + records + }; + let init = scheduler.plan().unwrap(); + assert_eq!(init.kind, GraphKind::Init); + scheduler.commit(&init, Fence(1)).unwrap(); + scheduler + .complete(Fence(1), &records(1, 0), Default::default()) + .unwrap(); + let origin = if refresh { + scheduler + .advance_to(scheduler.policy.estimate_ttl_ns) + .unwrap(); + measure(&mut scheduler, GraphKind::Snapshot, 1, 1); + scheduler.refresh.credit_ns = refresh::CREDIT_CAP_NS; + let plan = scheduler.plan().unwrap(); + assert_eq!(plan.token_capacity, capacity); + assert!(matches!(plan.origin, PlanOrigin::RefreshStart(_))); + plan.origin + } else { + PlanOrigin::Ordinary + }; + let mut held = Vec::new(); + while let Some(snapshot) = scheduler.reserve_snapshot(0) { + held.push(snapshot); + } + assert_eq!(held.len(), 16); + assert_eq!(scheduler.free_snapshots.len(), 0); + let request = scheduler.request(handle).unwrap(); + assert_eq!(scheduler.estimated_checkpoint_cut(request), Some(100)); + assert_eq!(scheduler.checkpoint_cut(request), Some(prompt_tokens)); + let plan = scheduler.plan().unwrap(); + assert_eq!(plan.kind, GraphKind::Prefill); + assert_eq!( + (plan.token_capacity, plan.lanes[0].tokens), + (capacity, prompt_tokens.min(capacity)) + ); + assert_eq!(plan.estimated_duration_ns(), capacity as u64 * 1000); + assert_eq!(plan.selected_cost.retained_ns, capacity as u64 * 1000); + assert_eq!(plan.origin, origin); + } + } + #[test] fn missing_cost_retries_original_uncached_interval_after_progress() { let mut scheduler = super::super::tests::scheduler(1, 128, 16); diff --git a/helios/src/scheduler/policy.rs b/helios/src/scheduler/policy.rs index 8e7abcc67..f6983a834 100644 --- a/helios/src/scheduler/policy.rs +++ b/helios/src/scheduler/policy.rs @@ -820,7 +820,19 @@ impl RequestScheduler { duration_ns: u64, value: LaneValue, ) -> f64 { - let tokens = self.lane_tokens(r, kind, capacity); + self.lane_value_for_tokens( + r, kind, self.lane_tokens(r, kind, capacity), duration_ns, value, + ) + } + + fn lane_value_for_tokens( + &self, + r: &LiveRequest, + kind: GraphKind, + tokens: usize, + duration_ns: u64, + value: LaneValue, + ) -> f64 { match kind { GraphKind::Prefill | GraphKind::ScoredPrefill => value.prefill( tokens, @@ -1122,7 +1134,7 @@ impl RequestScheduler { }; let ordinary = *candidates[..ordinary_count].iter().max_by(|a, b| compare(a, b))?; if ordinary.rate.duration_ns == 0 { - return self.available_plan(ordinary, log_scale, PlanOrigin::Discovery); + return self.available_plan(ordinary, &values, age_price, log_scale, PlanOrigin::Discovery); } if self.refresh_ready() { let mut targets: Vec<_> = self.costs.stale_keys().filter(|key| *key != ordinary.cost_key()) @@ -1141,20 +1153,37 @@ impl RequestScheduler { } } if let Some((_, target, candidate)) = best { - return self.available_plan(candidate, log_scale, PlanOrigin::RefreshStart(target)); + return self.available_plan(candidate, &values, age_price, log_scale, PlanOrigin::RefreshStart(target)); } } if self.refresh.acceptance_ready() && self.speculative_fixed_width.is_none() { if let Some(candidate) = self.acceptance_probe(ordinary, &values, age_price) { - return self.available_plan(candidate, log_scale, PlanOrigin::AcceptanceStart); + return self.available_plan(candidate, &values, age_price, log_scale, PlanOrigin::AcceptanceStart); } } - self.available_plan(ordinary, log_scale, PlanOrigin::Ordinary) + self.available_plan(ordinary, &values, age_price, log_scale, PlanOrigin::Ordinary) } - fn available_plan( - &self, candidate: Candidate, log_scale: f64, origin: PlanOrigin, + pub(super) fn available_plan( + &self, + mut candidate: Candidate, + values: &[LaneValue; REQUEST_LANES], + age_price: f64, + log_scale: f64, + origin: PlanOrigin, ) -> Option { + if candidate.kind.is_prefill() && matches!(origin, PlanOrigin::Ordinary) { + let slot = bits(candidate.mask).next().unwrap(); + let request = self.slots[slot].request.as_ref().unwrap(); + let cut = self.checkpoint_cut(request); + if self.prefill_work_through(request, candidate.capacity, cut) != candidate.work { + if let Some(resized) = + self.resize_prefill(candidate, request, cut, values[slot], age_price) + { + candidate = resized; + } + } + } let plan = self.materialize(candidate, log_scale, origin); // A winning feedback operation drains the queue instead of yielding to // lower-value work that would keep its execution window occupied. @@ -1165,7 +1194,35 @@ impl RequestScheduler { Some(plan) } - fn materialize(&self, candidate: Candidate, log_scale: f64, origin: PlanOrigin) -> ScheduledPlan { + fn resize_prefill( + &self, + mut candidate: Candidate, + request: &LiveRequest, + cut: Option, + value: LaneValue, + age_price: f64, + ) -> Option { + let catalog = self.prefill_catalog(candidate.kind); + let work = self.prefill_work_through(request, *catalog.last()?, cut); + let capacity = *catalog.iter().find(|&&capacity| capacity >= work)?; + let duration_ns = self.costs.duration(candidate.kind, candidate.batch, capacity)?; + candidate.capacity = capacity; + candidate.work = work; + candidate.duration_ns = duration_ns; + candidate.rate = ValueRate::new( + duration_ns, + self.lane_value_for_tokens(request, candidate.kind, work, duration_ns, value), + age_price, + ); + Some(candidate) + } + + fn materialize( + &self, + candidate: Candidate, + log_scale: f64, + origin: PlanOrigin, + ) -> ScheduledPlan { let lengths = (candidate.packed_token_capacity != 0).then(|| candidate.host_lengths.unwrap_or_else(|| self.round_lengths_optimistic(candidate.mask, candidate.batch, candidate.draft_tokens, @@ -1193,7 +1250,8 @@ impl RequestScheduler { } else { candidate.capacity }; - self.prefill_work_through(r, capacity, self.checkpoint_cut(r)) + let cut = self.checkpoint_cut(r); + self.prefill_work_through(r, capacity, cut) } else { if candidate.packed_token_capacity != 0 { lengths.as_ref().unwrap()[slot] diff --git a/helios/src/scheduler/policy_tests.rs b/helios/src/scheduler/policy_tests.rs index f980e67be..ae3708432 100644 --- a/helios/src/scheduler/policy_tests.rs +++ b/helios/src/scheduler/policy_tests.rs @@ -61,6 +61,33 @@ fn equal_counts_from_different_cohorts_and_different_logical_prefixes_are_not_pr assert!(!s.dominates_padding(candidate, Candidate { batch: 4, packed_token_capacity: 4, mask: 1 << handles[1].slot, ..candidate })); } +#[test] +fn prefill_keeps_candidate_without_resized_duration() { + let mut s = engine(1, false); + s.prefill_graphs(&[128, 256, 512]).unwrap(); + let mut graphs = super::tests::duration_graphs(1); + graphs.retain(|graph| graph.kind != GraphKind::Prefill || graph.token_capacity != 256); + s.measured_graph_durations(&graphs).unwrap(); + let handle = add(&mut s, 1, 200, 1); + s.request_mut(handle).unwrap().phase = RequestPhase::Prefill; + let (values, age_price, scale) = s.lane_values(s.window_remaining().unwrap()); + let mut candidates = Vec::new(); + s.candidates(&mut candidates, &values, age_price); + let mut candidate = *candidates.iter() + .find(|c| c.kind == GraphKind::Prefill && c.capacity == 128) + .unwrap(); + // The retained estimate stopped at a cut that the quoted selection omits. + candidate.work = 100; + assert_eq!(s.costs.duration(GraphKind::Prefill, 1, 256), None); + let plan = s.available_plan(candidate, &values, age_price, scale, PlanOrigin::Ordinary) + .unwrap(); + assert_eq!((plan.token_capacity, plan.lanes[0].tokens), (128, 128)); + assert_eq!(plan.estimated_duration_ns(), candidate.duration_ns); + assert_eq!(plan.selected_cost.retained_ns, candidate.duration_ns); + assert_eq!(plan.selection_value_rate(), candidate.rate.score); + assert_eq!(plan.origin, PlanOrigin::Ordinary); +} + fn engine(active: usize, speculative: bool) -> RequestScheduler { engine_with_geometry(active, speculative, 64, None) }