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] {