From 714eda0a05d302f9297f84ce1b8fe8e070e0dc3d Mon Sep 17 00:00:00 2001 From: yatesdr <6937857+yatesdr@users.noreply.github.com> Date: Thu, 19 Nov 2026 00:10:33 -0500 Subject: [PATCH] Size mixed prefill rows to quoted checkpoint work Follows up #357. When checkpoint storage is short, the quoted cut can move past the estimated one used to choose a mixed graph (prompt prefill combined with decode or verification rows, from packages exported with --mixed-prefill). #357 resized ordinary prefill plans to the quoted work; mixed plans kept their prompt capacity and the quoted work was clipped to it, so the prompt row ran short and was finished in another step. Apply the same resize to a mixed plan's prompt row: pick the smallest mixed shape with the plan's batch and decode width whose prompt capacity covers the quoted work, and recompute the duration and the combined prompt and decode value. Decode rows, refresh and discovery plans are unchanged, and a shape without a measured duration keeps the plan as on main. The regression schedules a 200-token prompt as 200 tokens on a 256-token mixed shape; main runs 128 tokens on a 128-token shape. It also covers 300- and 1,024-token prompts (the latter bounded by the 512-token catalog maximum), ordinary and speculative decode rows and the missing-duration fallback. It fails on main and passes with the fix (Linux aarch64). On cn4 (Linux x86_64, release) the scheduler and runtime phase tests pass: 426 passed, 4 ignored. Not tested: serving on a --mixed-prefill package, scored prefill or image spans under shortage, more than one decode row. Design-Signed-Off-By: yatesdr; design-review=1af4 --- docs/scheduling-runtime-design.md | 10 +++-- helios/src/scheduler/policy.rs | 49 +++++++++++++++++----- helios/src/scheduler/policy_tests.rs | 63 ++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 14 deletions(-) diff --git a/docs/scheduling-runtime-design.md b/docs/scheduling-runtime-design.md index fb6fc1d03..273753236 100644 --- a/docs/scheduling-runtime-design.md +++ b/docs/scheduling-runtime-design.md @@ -180,10 +180,12 @@ 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 +ordinary prefill or mixed plan advances toward the quoted cut up to the catalog +maximum and uses the smallest catalog capacity covering that work, with duration +and value recomputed. Mixed plans resize only the prompt capacity among shapes +with the same batch capacity and decode width; decode lanes retain their token +counts. If a required lookup is unavailable, it retains the winning candidate +and clips work to that candidate's capacity. 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. diff --git a/helios/src/scheduler/policy.rs b/helios/src/scheduler/policy.rs index f6983a834..e54fd89d5 100644 --- a/helios/src/scheduler/policy.rs +++ b/helios/src/scheduler/policy.rs @@ -1172,13 +1172,20 @@ impl RequestScheduler { log_scale: f64, origin: PlanOrigin, ) -> Option { - if candidate.kind.is_prefill() && matches!(origin, PlanOrigin::Ordinary) { - let slot = bits(candidate.mask).next().unwrap(); + if (candidate.kind.is_prefill() || candidate.prefill_slot.is_some()) + && matches!(origin, PlanOrigin::Ordinary) + { + let slot = candidate.prefill_slot.unwrap_or_else(|| bits(candidate.mask).next().unwrap()); let request = self.slots[slot].request.as_ref().unwrap(); + let (capacity, work) = if candidate.kind == GraphKind::Mixed { + (candidate.prefill_capacity, self.prefill_work(request, candidate.prefill_capacity)) + } else { + (candidate.capacity, candidate.work) + }; let cut = self.checkpoint_cut(request); - if self.prefill_work_through(request, candidate.capacity, cut) != candidate.work { + if self.prefill_work_through(request, capacity, cut) != work { if let Some(resized) = - self.resize_prefill(candidate, request, cut, values[slot], age_price) + self.resize_prefill(candidate, request, cut, values, age_price) { candidate = resized; } @@ -1199,19 +1206,41 @@ impl RequestScheduler { mut candidate: Candidate, request: &LiveRequest, cut: Option, - value: LaneValue, + values: &[LaneValue; REQUEST_LANES], 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)?; + let mixed = candidate.kind == GraphKind::Mixed; + let (capacity, prefill, work) = if mixed { + let catalog = self.mixed_catalog.iter() + .filter(|&&(batch, width, _)| batch == candidate.batch && width == candidate.capacity) + .map(|&(_, _, prefill)| prefill); + let work = self.prefill_work_through(request, catalog.clone().max()?, cut); + let prefill = catalog.filter(|&capacity| capacity >= work).min()?; + (candidate.capacity, prefill, work) + } else { + 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)?; + (capacity, 0, work) + }; + let duration_ns = self.costs.shape_duration(candidate.kind, candidate.batch, capacity, prefill)?; candidate.capacity = capacity; + candidate.prefill_capacity = prefill; candidate.work = work; candidate.duration_ns = duration_ns; + let slot = request.handle.slot as usize; + let kind = if mixed { GraphKind::Prefill } else { candidate.kind }; + let mut expected = self.lane_value_for_tokens(request, kind, work, duration_ns, values[slot]); + if mixed { + let decode = if self.speculative_widths.is_empty() { GraphKind::Decode } else { GraphKind::Speculative }; + expected += bits(candidate.mask & !(1 << slot)).map(|slot| { + self.lane_value(self.slots[slot].request.as_ref().unwrap(), decode, + candidate.capacity, duration_ns, values[slot]) + }).sum::(); + } candidate.rate = ValueRate::new( duration_ns, - self.lane_value_for_tokens(request, candidate.kind, work, duration_ns, value), + expected, age_price, ); Some(candidate) diff --git a/helios/src/scheduler/policy_tests.rs b/helios/src/scheduler/policy_tests.rs index ae3708432..c31a40664 100644 --- a/helios/src/scheduler/policy_tests.rs +++ b/helios/src/scheduler/policy_tests.rs @@ -1862,6 +1862,69 @@ fn mixed_prefill_exploration_requires_additional_useful_tokens() { } } +#[test] +fn mixed_prefill_capacity_matches_quoted_work_without_snapshot_storage() { + for speculative in [false, true] { + for (prompt_tokens, capacity, missing_duration) in [ + (200, 256, false), (300, 512, false), (1024, 512, false), (200, 128, true), + ] { + let width = if speculative { 8 } else { 1 }; + let shapes = [ + (2, width, 128, 128_000), + (2, width, 256, 256_000), + (2, width, 512, 512_000), + ]; + let measured: Vec<_> = shapes.iter().copied() + .filter(|&(_, _, prefill, _)| !missing_duration || prefill != 256).collect(); + let mut s = mixed_engine(2, speculative, &measured); + s.mixed_graphs(&shapes.map(|(batch, width, prefill, _)| (batch, width, prefill))).unwrap(); + throughput_only(&mut s); + s.policy.checkpoint_extra_work_fraction = 0.5; + let decoder = add(&mut s, 1, 64, 1024); + ready(&mut s, &[decoder]); + let mut spec = super::tests::spec(prompt_tokens, 1); + spec.message_boundaries = vec![100, prompt_tokens]; + let prompt = s.admit(spec).unwrap(); + initialize(&mut s, prompt); + let before = s.plan().unwrap(); + assert_eq!(before.kind, GraphKind::Mixed); + assert_eq!((before.prefill_capacity(), before.lanes[1].tokens), (128, 100)); + let mut held = Vec::new(); + while let Some(snapshot) = s.reserve_snapshot(0) { + held.push(snapshot); + } + assert!(!held.is_empty()); + assert!(s.free_snapshots.is_empty()); + let request = s.request(prompt).unwrap(); + assert_eq!(s.estimated_checkpoint_cut(request), Some(100)); + assert_eq!(s.checkpoint_cut(request), Some(prompt_tokens)); + let plan = s.plan().unwrap(); + assert_eq!(plan.kind, GraphKind::Mixed); + assert_eq!((plan.prefill_capacity(), plan.lanes[1].tokens), + (capacity, prompt_tokens.min(capacity))); + assert_eq!((plan.batch_capacity, plan.token_capacity), (2, width)); + assert_eq!(plan.mixed.as_ref().unwrap().prefill_row, Some(1)); + assert_eq!(plan.mixed.as_ref().unwrap().speculative, speculative); + assert_eq!(plan.lanes[0].request, decoder); + assert_eq!(plan.lanes[0].tokens, before.lanes[0].tokens); + assert_eq!(plan.lanes[0].start_position, before.lanes[0].start_position); + assert_eq!(plan.lanes[1].request, prompt); + 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, PlanOrigin::Ordinary); + if missing_duration { + assert_eq!(plan.selection_value_rate(), before.selection_value_rate()); + continue; + } + let output = s.expected_speculative_tokens(s.request(decoder).unwrap(), plan.lanes[0].tokens); + let expected = (plan.lanes[1].tokens as f64 + 64.0 * output + + if prompt_tokens <= 512 { 64.0 } else { 0.0 }) + / (plan.estimated_duration_ns() as f64 / 1e6); + assert!((plan.selection_value_rate() * plan.selection_log_scale().exp() - expected).abs() < 1e-10); + } + } +} + #[test] fn mixed_value_selects_an_efficiency_knee_and_a_fitting_prompt_tail() { for prompt_tokens in [64, 8192] {