Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions docs/scheduling-runtime-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,28 +111,45 @@ confidence forecast is adjusted by two online corrections derived solely from
completed rounds:

1. **Hierarchical per-request, per-depth drafter calibration (`RequestCalibration`
+ `DepthCalibration`):** A slow round-level global prior `DepthCalibration`
(`clamp(realized / predicted, 0.0, 2.0)`, defaulting to `1.0` when
`predicted <= 1e-6`, initialized at `realized == predicted == 0.5^d` with
prior weight `4.0`, updated **once per completed round** from the mean
realized and predicted survival across contributing lanes with EWMA cap
`64.0`) provides instant model-wide protection for brand-new requests (e.g.,
if a drafter breaks globally) while preventing a single correlated
large-batch dip from overwriting the prior in one step. Each live request
maintains its own aged per-depth `RequestCalibration` (seeded
+ `DepthCalibration`):** Both calibration levels clamp `realized / predicted`
to `[0.0, calibration_factor_max(depth)]` (defaulting to `1.0` when
`predicted <= 1e-6`), where `calibration_factor_max(depth) = 2.0 * 1.75^depth`
with `depth` capped at `CALIBRATION_MAX_DEPTH = 30` (`CALIBRATION_FACTOR_MAX = 2.0`
at `depth = 0`, scaled by `CALIBRATION_STEP_FACTOR_MAX = 1.75` per additional
draft step). Per the model-agnostic sidecar contract in
[helios/model-contract.md](../helios/model-contract.md),
`proposal_confidence[frame, row, d]` carries per-step conditional confidence
values, which the host multiplies into cumulative prefix survival
(`prefix *= confidence[row][depth]`) across `depth + 1` draft steps. When a
drafter signal—for example, per-step Rényi-2 collision probability
`sum_i q_{j,i}^2`—underpredicts realized verification acceptance on each
step, that underprediction compounds multiplicatively across the cumulative
product. Compounding the ceiling across `depth` additional steps as
`2.0 * 1.75^depth` preserves a conservative per-step shrinkage margin against
noise (`0.5 * 1.75 = 0.875 < 1` relative to doubling per step) while
allowing sustained multi-step survival to calibrate past shallow packed
widths (such as `packed_token_capacity = 3`).
A slow round-level global prior `DepthCalibration` (initialized at
`realized == predicted == 0.5^d` with prior weight `4.0`, updated **once per
completed round** from the mean realized and predicted survival across
contributing lanes with EWMA cap `64.0`) provides instant model-wide
protection for brand-new requests (e.g., if a drafter breaks globally) while
preventing a single correlated large-batch dip from overwriting the prior in
one step. Each live request maintains its own aged per-depth `RequestCalibration` (seeded
scale-invariantly at `anchor = max(predicted, 0.05)` with prior weight `2.0`
from the live global factor on its first observation at that depth, EWMA cap
`32.0`, 128-token half-life aging matching `PrefixEstimate`) so sequential
workloads, context lengths, and heterogeneous concurrent lanes do not bleed
calibration across requests, and censored unobserved depths decay their prior
weight so a single funded acceptance probe can escape stale priors. Updates
use the exact offered, determined prefix outcomes computed once in
`complete_inner` (Rao-Blackwellized with `completion_speculative_survival`
when reported, else binary acceptance/rejection). Pricing multiplies each
depth's stored mean by the request's calibration factor, adds the Hoeffding
optimistic margin using the minimum aged weight of the confidence forecast
and verification calibration during funded acceptance learning, clamps to
`[0, 1]`, and enforces nonincreasing prefix survival across depths.
`32.0`, 128-token half-life aging matching `PrefixEstimate`, and clamped to
`calibration_factor_max(depth)`) so sequential workloads, context lengths, and
heterogeneous concurrent lanes do not bleed calibration across requests, and
censored unobserved depths decay their prior weight so a single funded
acceptance probe can escape stale priors. Updates use the exact offered,
determined prefix outcomes computed once in `complete_inner` (Rao-Blackwellized
with `completion_speculative_survival` when reported, else binary
acceptance/rejection). Pricing multiplies each depth's stored mean by the
request's calibration factor, adds the Hoeffding optimistic margin using the
minimum aged weight of the confidence forecast and verification calibration
during funded acceptance learning, clamps to `[0, 1]`, and enforces
nonincreasing prefix survival across depths.
2. **Cross-lane order-statistic allocation surplus (`SurplusHistory`):** Inside
a packed GPU-allocated round (`B >= 2`, `P < B * W`, `depth >= 1`), the
device allocator observes fresh per-step confidences across all active lanes
Expand Down
5 changes: 3 additions & 2 deletions helios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,9 @@ outcomes. When pricing GPU-confidence shapes, stored per-depth forecasts are
scaled by a request-local aged per-depth verification calibration ratio (seeded
scale-invariantly with prior weight `2` from a once-per-round population prior
capped at weight `64`, 128-token half-life, request EWMA cap `32`, clamped to
`[0, 2]`), and crowded GPU-allocated packed shapes (`B >= 2`, `P < B * W`,
`depth >= 1`) additionally apply a bounded empirical cross-lane order-statistic
`[0, 2 * 1.75^depth]` with `depth` capped at `30`), and crowded GPU-allocated
packed shapes (`B >= 2`, `P < B * W`, `depth >= 1`) additionally apply a bounded
empirical cross-lane order-statistic
surplus (from dual 256-entry rolling windows—one for `depth >= 2` curves and a
dedicated depth-1 ring for `W=2`—with conservative `eff_depth <= depth`
fallback, excess scaled by `(1 - 1/b) * min(alpha, 1)` and capped at 100% draft
Expand Down
168 changes: 168 additions & 0 deletions helios/src/scheduler/policy_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6429,3 +6429,171 @@ fn expensive_stale_shape_does_not_starve_smaller_stale_shapes_under_credit_refil
// Bound P <= (0.002 / 0.998) * O + D uses the most recent refresh episode duration D.
assert!(refresh_ns as f64 <= ordinary_ns as f64 * 0.002 / 0.998 + last_refresh_ns as f64);
}

#[test]
fn gpu_confidence_calibration_ceiling_scales_per_step_across_depth() {
let mut forecasts = rounds::Forecasts::new(6);
let mut clamped = rounds::Forecasts::new(6);
for _ in 0..256 {
// Depth 0 ceiling is 2.0 * 1.75^0 = 2.0: 0.99 / 0.10 = 9.9 clamps to 2.0.
forecasts.observe_calibration(0, 0.99, 0.10);
// Depth 3 ceiling is 2.0 * 1.75^3 = 10.71875: 0.99 / 0.01 = 99.0 clamps to 10.71875.
forecasts.observe_calibration(3, 0.99, 0.01);
// Depth 4 ceiling is 2.0 * 1.75^4 = 18.7578125: 0.75 / 0.05 = 15.0 passes unclamped.
forecasts.observe_calibration(4, 0.75, 0.05);
// Depth 4 ceiling is 2.0 * 1.75^4 = 18.7578125: 0.99 / 0.01 = 99.0 clamps to 18.7578125.
clamped.observe_calibration(4, 0.99, 0.01);
}
assert!((forecasts.calibration_factor(0) - 2.0).abs() < 1e-6);
assert!((forecasts.calibration_factor(3) - 10.71875).abs() < 1e-6);
assert!((forecasts.calibration_factor(4) - 15.0).abs() < 0.25);
assert!((clamped.calibration_factor(4) - 18.7578125).abs() < 1e-6);
}

fn single_lane_packed_calibration_engine() -> RequestScheduler {
let mut s = engine(1, false);
s.configure_policy(PolicyConfig {
adaptive_verification_alpha: 0.75,
token_fairness: 0.0,
delivery_fairness: 0.0,
decode_age_price: 0.0,
..PolicyConfig::default()
})
.unwrap();
let mut graphs: Vec<_> = super::tests::duration_graphs(1)
.into_iter()
.filter(|g| g.kind != GraphKind::Speculative)
.collect();
for g in &mut graphs {
g.estimated_duration_ns = MS;
}
graphs.push(crate::execution::GraphDescriptor {
name: "speculative-b1-d0-w1".into(),
kind: GraphKind::Speculative,
sampling_mode: SamplingMode::General,
handle: 100,
batch_capacity: 1,
draft_tokens: 0,
token_capacity: 1,
packed_token_capacity: 0,
prefill_capacity: 0,
estimated_duration_ns: 28 * MS,
confidence_width: 0,
tuned_bindings: vec![],
input_bindings: vec![],
});
for (packed, nanos) in [
(2usize, 29_500_000u64),
(3, 31_200_000),
(4, 35_000_000),
(5, 37_000_000),
(6, 38_800_000),
] {
graphs.push(crate::execution::GraphDescriptor {
name: format!("speculative-b1-d5-w6-p{packed}"),
kind: GraphKind::Speculative,
sampling_mode: SamplingMode::General,
handle: 100 + packed,
batch_capacity: 1,
draft_tokens: 5,
token_capacity: 6,
packed_token_capacity: packed,
prefill_capacity: 0,
estimated_duration_ns: nanos,
confidence_width: 5,
tuned_bindings: vec![],
input_bindings: vec![],
});
}
s.measured_graph_durations(&graphs).unwrap();
s.enable_speculation(&[1, 6]).unwrap();
s.capabilities.speculative_allocations = [2, 3, 4, 5, 6]
.into_iter()
.map(|packed| (1, 5, 6, packed, crate::execution::PrefixAllocation::Gpu))
.collect();
s
}

fn observe_single_lane_packed_round(
s: &mut RequestScheduler,
handle: RequestHandle,
plan: &ScheduledPlan,
conf_per_step: f32,
realized: &[f32; 5],
) {
let fence = Fence(s.submitted.0 + 1);
s.commit(plan, fence).unwrap();
let mut records = completion_records(s, plan, fence, 0);
let remaining = plan.packed_token_capacity.saturating_sub(plan.lanes.len());
let offered = plan.lanes[0].tokens.saturating_sub(1).min(remaining).min(5);
let vec = records[handle.slot as usize].vector.as_mut().unwrap();
vec.verification_width = plan.token_capacity as i64;
vec.offered_drafts = offered as i64;
let slots = s.limits.request_capacity();
let mut surv = vec![vec![-1.0f32; 15]; slots];
for d in 0..offered {
surv[handle.slot as usize][d] = realized[d];
}
let confidence = vec![vec![conf_per_step; 15]; plan.batch_capacity];
let duration = plan.estimated_duration_ns().max(MS);
s.advance_to(s.now_ns + duration).unwrap();
s.complete_packed_round_with_survival(fence, &records, gpu_time(duration), &confidence, Some(&surv))
.unwrap();
}

#[test]
fn gpu_confidence_multi_step_calibration_escapes_packed_three_trap_and_respects_low_depth_two_survival() {
// (b) High per-step acceptance (realized = [0.95..0.75]) with low per-step confidence (0.55)
// calibrates deep prefix products across all 5 steps and selects Ordinary packed = 6.
let mut s = single_lane_packed_calibration_engine();
let handle = add(&mut s, 1, 64, 1024);
ready(&mut s, &[handle]);
s.refresh.acceptance_credit_ns = 0.0;
s.refresh.credit_ns = 0.0;

let initial = s.plan().unwrap();
assert_eq!(initial.origin, PlanOrigin::Ordinary);
assert_eq!(initial.packed_token_capacity, 3);
let high_realized = [0.95f32, 0.90, 0.85, 0.80, 0.75];
observe_single_lane_packed_round(&mut s, handle, &initial, 0.55, &high_realized);

for _ in 0..48 {
s.refresh.acceptance_credit_ns = refresh::CREDIT_CAP_NS;
let plan = s.plan().unwrap();
observe_single_lane_packed_round(&mut s, handle, &plan, 0.55, &high_realized);
}

assert!(
s.forecasts.request_calibration_factor(handle, 4) > 8.0,
"depth 4 calibration factor must exceed the depth-0 2.0 ceiling: {}",
s.forecasts.request_calibration_factor(handle, 4)
);
let req = s.request(handle).unwrap();
let curve = s.speculative_curve(req, false);
for (depth, &expected) in high_realized.iter().enumerate() {
assert!(
(curve[depth] - f64::from(expected)).abs() < 0.05,
"depth {depth} should calibrate near {expected}, got {:?}",
&curve[..5]
);
}
s.refresh.acceptance_credit_ns = 0.0;
let selected = s.plan().unwrap();
assert_eq!(selected.origin, PlanOrigin::Ordinary);
assert_eq!(selected.packed_token_capacity, 6);

// (c) Low realized survival at depth >= 2 continues to select Ordinary packed = 3.
let mut s_low = single_lane_packed_calibration_engine();
let handle_low = add(&mut s_low, 1, 64, 1024);
ready(&mut s_low, &[handle_low]);
let low_realized = [0.95f32, 0.90, 0.05, 0.0, 0.0];
for _ in 0..24 {
s_low.refresh.acceptance_credit_ns = refresh::CREDIT_CAP_NS;
let plan = s_low.plan().unwrap();
observe_single_lane_packed_round(&mut s_low, handle_low, &plan, 0.55, &low_realized);
}
s_low.refresh.acceptance_credit_ns = 0.0;
let selected_low = s_low.plan().unwrap();
assert_eq!(selected_low.origin, PlanOrigin::Ordinary);
assert_eq!(selected_low.packed_token_capacity, 3);
}
40 changes: 24 additions & 16 deletions helios/src/scheduler/rounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const POPULATION_WEIGHT: f32 = 1.0 / 64.0;
const CALIBRATION_PRIOR_WEIGHT: f64 = 4.0;
const POPULATION_CALIBRATION_MAX_WEIGHT: f64 = 64.0;
const CALIBRATION_FACTOR_MAX: f64 = 2.0;
const CALIBRATION_STEP_FACTOR_MAX: f64 = 1.75;
const CALIBRATION_MAX_DEPTH: usize = 30;
const REQUEST_CALIBRATION_PRIOR_WEIGHT: f64 = 2.0;
const REQUEST_CALIBRATION_MAX_WEIGHT: f64 = 32.0;
const REQUEST_CALIBRATION_HALF_LIFE: f64 = 128.0;
Expand All @@ -19,6 +21,18 @@ const SURPLUS_HISTORY_CAPACITY: usize = 256;
const SURPLUS_MIN_SAMPLES: usize = 4;
const SURPLUS_FACTOR_MAX: f64 = 2.0;

fn calibration_factor_max(depth: usize) -> f64 {
CALIBRATION_FACTOR_MAX * CALIBRATION_STEP_FACTOR_MAX.powi(depth.min(CALIBRATION_MAX_DEPTH) as i32)
}

fn calibration_ratio(realized: f64, predicted: f64, depth: usize) -> f64 {
if predicted > 1e-6 {
(realized / predicted).clamp(0.0, calibration_factor_max(depth))
} else {
1.0
}
}

#[derive(Clone, Copy)]
struct DepthCalibration {
realized: f64,
Expand All @@ -39,12 +53,8 @@ impl DepthCalibration {
self.predicted += (predicted - self.predicted) / self.weight;
}

fn factor(self) -> f64 {
if self.predicted > 1e-6 {
(self.realized / self.predicted).clamp(0.0, CALIBRATION_FACTOR_MAX)
} else {
1.0
}
fn factor(self, depth: usize) -> f64 {
calibration_ratio(self.realized, self.predicted, depth)
}
}

Expand Down Expand Up @@ -75,23 +85,21 @@ impl RequestCalibration {
base * Self::decay(generated, self.observed_at)
}

fn factor(self, global: DepthCalibration) -> f64 {
fn factor(self, depth: usize, global: DepthCalibration) -> f64 {
if self.weight == 0.0 {
global.factor()
} else if self.predicted > 1e-6 {
(self.realized / self.predicted).clamp(0.0, CALIBRATION_FACTOR_MAX)
global.factor(depth)
} else {
1.0
calibration_ratio(self.realized, self.predicted, depth)
}
}

fn observe(&mut self, realized: f64, predicted: f64, generated: u32, global: DepthCalibration) {
fn observe(&mut self, depth: usize, realized: f64, predicted: f64, generated: u32, global: DepthCalibration) {
debug_assert!(realized.is_finite() && (0.0..=1.0).contains(&realized));
debug_assert!(predicted.is_finite() && (0.0..=1.0).contains(&predicted));
let aged = if self.weight == 0.0 {
let anchor = predicted.max(REQUEST_CALIBRATION_ANCHOR_FLOOR);
self.predicted = anchor;
self.realized = (anchor * global.factor()).clamp(0.0, 1.0);
self.realized = (anchor * global.factor(depth)).clamp(0.0, 1.0);
REQUEST_CALIBRATION_PRIOR_WEIGHT * Self::decay(generated, self.observed_at)
} else {
self.weight_at(generated)
Expand Down Expand Up @@ -277,7 +285,7 @@ impl Forecast {

fn calibration_factor(&self, depth: usize, global: &[DepthCalibration]) -> f64 {
let global_entry = global.get(depth).copied().unwrap_or(DepthCalibration::new(1.0));
self.calibration.get(depth).map_or_else(|| global_entry.factor(), |entry| entry.factor(global_entry))
self.calibration.get(depth).map_or_else(|| global_entry.factor(depth), |entry| entry.factor(depth, global_entry))
}

fn calibration_information(&self, depth: usize, generated: u32) -> f64 {
Expand All @@ -287,7 +295,7 @@ impl Forecast {
fn observe_calibration(&mut self, depth: usize, realized: f64, predicted: f64, generated: u32, global: &[DepthCalibration]) {
if let Some(entry) = self.calibration.get_mut(depth) {
let global_entry = global.get(depth).copied().unwrap_or(DepthCalibration::new(1.0));
entry.observe(realized, predicted, generated, global_entry);
entry.observe(depth, realized, predicted, generated, global_entry);
}
}
}
Expand Down Expand Up @@ -327,7 +335,7 @@ impl Forecasts {

#[cfg(test)]
pub(super) fn calibration_factor(&self, depth: usize) -> f64 {
self.calibration.get(depth).map_or(1.0, |entry| entry.factor())
self.calibration.get(depth).map_or(1.0, |entry| entry.factor(depth))
}

#[cfg(test)]
Expand Down
Loading