Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ pub fn plan_serving(
host: &HostBudget,
candidates: &[ModelFootprint],
demand: &ServingDemand,
) -> Option<ServingPlan> {
plan_serving_into(host, candidates, demand, &mut None)
}

/// A plan and the training growth decision the requirements allocator made for it (`None`
/// when no requirements chose it). The decision rides out to the plan's publisher, the one
/// owner of "the plan changed", which reports it once per change: the planner itself runs
/// several times a second (22,184 per-call rows in 100 min on IntelMac 2026-11-02).
#[derive(Debug, Clone)]
pub struct TracedPlan {
pub plan: ServingPlan,
pub training_growth: Option<crate::inference::planning::window_allocator::TrainingGrowth>,
}

/// [`plan_serving`]'s one body; `training_growth` receives the allocator's growth decision.
fn plan_serving_into(
host: &HostBudget,
candidates: &[ModelFootprint],
demand: &ServingDemand,
training_growth: &mut Option<crate::inference::planning::window_allocator::TrainingGrowth>,
) -> Option<ServingPlan> {
// The slot count is sized to every mind this seat SERVES: its residents plus the
// minds leased in from other nodes. Counting only residents is how one seat carried
Expand Down Expand Up @@ -228,16 +248,7 @@ pub fn plan_serving(
} else {
match crate::inference::planning::window_allocator::allocate(&demand.requirements, candidates, host, demand_lanes.max(1)) {
Ok(a) => {
continuum_base::probe!(
class = "serving.training_growth",
model = %a.model_id,
window = a.window as u64,
lanes = a.lanes as u64,
budget_bytes = host.usable_bytes,
device_bytes = a.device_bytes,
decision = ?a.training_growth,
"shared-window growth reports unresolved training demand separately from conversational admission"
);
*training_growth = Some(a.training_growth.clone());
let chosen = candidates
.iter()
.find(|c| c.model_id == a.model_id)
Expand Down Expand Up @@ -428,6 +439,28 @@ pub fn plan_serving_stable(
candidates: &[ModelFootprint],
incumbent: Option<&str>,
demand: &ServingDemand,
) -> Option<ServingPlan> {
plan_serving_stable_into(host, candidates, incumbent, demand, &mut None)
}

/// [`plan_serving_stable`] with the chosen plan's training growth decision.
pub fn plan_serving_stable_traced(
host: &HostBudget,
candidates: &[ModelFootprint],
incumbent: Option<&str>,
demand: &ServingDemand,
) -> Option<TracedPlan> {
let mut training_growth = None;
plan_serving_stable_into(host, candidates, incumbent, demand, &mut training_growth)
.map(|plan| TracedPlan { plan, training_growth })
}

fn plan_serving_stable_into(
host: &HostBudget,
candidates: &[ModelFootprint],
incumbent: Option<&str>,
demand: &ServingDemand,
training_growth: &mut Option<crate::inference::planning::window_allocator::TrainingGrowth>,
) -> Option<ServingPlan> {
// The credit is applied HERE, for a caller holding the LIVE budget (the
// incumbent's weights read as "used"). A caller whose budget is already at rest
Expand All @@ -444,7 +477,7 @@ pub fn plan_serving_stable(
},
None => *host,
};
plan_serving_at_rest(&at_rest, candidates, incumbent, demand)
plan_serving_at_rest_into(&at_rest, candidates, incumbent, demand, training_growth)
}

/// [`plan_serving_stable`] for a budget that is ALREADY at rest — the incumbent's own
Expand All @@ -455,6 +488,29 @@ pub fn plan_serving_at_rest(
candidates: &[ModelFootprint],
incumbent: Option<&str>,
demand: &ServingDemand,
) -> Option<ServingPlan> {
plan_serving_at_rest_into(at_rest, candidates, incumbent, demand, &mut None)
}

/// [`plan_serving_at_rest`] with the chosen plan's training growth decision.
pub fn plan_serving_at_rest_traced(
at_rest: &HostBudget,
candidates: &[ModelFootprint],
incumbent: Option<&str>,
demand: &ServingDemand,
) -> Option<TracedPlan> {
let mut training_growth = None;
plan_serving_at_rest_into(at_rest, candidates, incumbent, demand, &mut training_growth)
.map(|plan| TracedPlan { plan, training_growth })
}

/// The at-rest body. `training_growth` receives the decision of whichever plan it returns.
fn plan_serving_at_rest_into(
at_rest: &HostBudget,
candidates: &[ModelFootprint],
incumbent: Option<&str>,
demand: &ServingDemand,
training_growth: &mut Option<crate::inference::planning::window_allocator::TrainingGrowth>,
) -> Option<ServingPlan> {
let host = at_rest;
// NB: do NOT `?`-bail here. A deep transient dip can leave `plan_serving`
Expand All @@ -463,7 +519,7 @@ pub fn plan_serving_at_rest(
// down to "nothing" is the exact harm we're guarding against, so `fresh` is
// an Option we fall back to only when the incumbent genuinely can't hold.
let Some(inc_id) = incumbent else {
return plan_serving(&host, candidates, &demand);
return plan_serving_into(&host, candidates, &demand, training_growth);
};
// NOTE (2026-08-04): there used to be an early return here — "fresh already chose the
// incumbent → nothing to stabilize" — and it was the lane-flap bug. `fresh` is computed
Expand All @@ -485,7 +541,7 @@ pub fn plan_serving_at_rest(
// Incumbent dropped off disk entirely → honour whatever `fresh` chose
// (possibly None = nothing servable).
let Some(inc) = candidates.iter().find(|m| m.model_id == inc_id) else {
return plan_serving(&host, candidates, &demand);
return plan_serving_into(&host, candidates, &demand, training_growth);
};
// The incumbent is ALREADY resident: its own weights read as "used" in live
// free memory, which is exactly what depresses `usable_bytes` while it loads.
Expand Down Expand Up @@ -523,10 +579,12 @@ pub fn plan_serving_at_rest(
// budget bump cannot flap it. An EXTERNAL squeeze is still not credited back — only the
// incumbent's own committed weights are — so a real over-commit still forces the
// down-switch. [[never-thrash-sticky-hysteresis-on-every-lane]]
let fresh = plan_serving(&at_rest, candidates, &demand);
let mut fresh_growth = None;
let fresh = plan_serving_into(&at_rest, candidates, &demand, &mut fresh_growth);
// Even crediting its own residency, can the incumbent still hold a lane? If
// not, a real squeeze has genuinely evicted it → take `fresh`.
if inc.weights_bytes.saturating_add(inc.kv_at(MIN_SERVE_CTX)) > at_rest.usable_bytes {
*training_growth = fresh_growth;
return fresh;
}
// Switch UP to `fresh` ONLY if it is strictly more capable AND fits the POST-EVICTION
Expand All @@ -550,7 +608,7 @@ pub fn plan_serving_at_rest(
// than the roster needs leaves minds cold that were warm — not an upgrade,
// whatever it scores. (BigMama, card a374c014: this compares FOOTPRINTS, so a
// stale weights_bytes on a long-running core lies to it — fix the input there.)
let inc_lanes = plan_serving(&at_rest, std::slice::from_ref(inc), &demand)
let inc_lanes = plan_serving_into(&at_rest, std::slice::from_ref(inc), &demand, &mut None)
.map(|p| p.lanes as u32)
.unwrap_or(0); // unwrap_or: an incumbent that cannot plan at all has no lanes to protect
let upgrade_worth_it = fresh.as_ref().is_some_and(|fresh_plan| {
Expand All @@ -562,6 +620,7 @@ pub fn plan_serving_at_rest(
&& !starves_the_roster
});
if upgrade_worth_it {
*training_growth = fresh_growth;
return fresh;
}
// Keep the incumbent: re-rank it to the top so `plan_serving` selects it and
Expand All @@ -572,7 +631,7 @@ pub fn plan_serving_at_rest(
if let Some(m) = promoted.iter_mut().find(|m| m.model_id == inc_id) {
m.capability_rank = u8::MAX;
}
plan_serving(&at_rest, &promoted, &demand)
plan_serving_into(&at_rest, &promoted, &demand, training_growth)
}

pub(super) fn bytes_gb(bytes: u64) -> f64 {
Expand Down
2 changes: 1 addition & 1 deletion core/continuum-serving/src/modules/serving_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use crate::capacity::placement::PlacementRequest;
use crate::cognition::model_resolver::types::HwCapabilityTier;
use crate::inference::planning::serving_plan::footprint::{footprint_for, footprint_from_parts};
use crate::inference::planning::serving_plan::{
host_budget_from, plan_serving, plan_serving_at_rest, plan_serving_stable, HostBudget,
host_budget_from, plan_serving, plan_serving_at_rest_traced, plan_serving_stable_traced, HostBudget,
HostBudgetInputs, ModelFootprint, ServingDemand, ServingPlan,
};
use crate::gpu::GpuMemoryManager;
Expand Down
78 changes: 65 additions & 13 deletions core/continuum-serving/src/modules/serving_daemon/planning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,15 @@ impl ServingDaemonModule {
// reading the guards that prevent them (#232's `apply_kv_quantization`, and the
// credit-back-exactly-once ledger above). Nobody could say which arm had run, because
// nothing said so. This says it.
let stable = if ledger_credited {
plan_serving_at_rest(&budget, candidates, incumbent.as_deref(), &demand)
let traced = if ledger_credited {
plan_serving_at_rest_traced(&budget, candidates, incumbent.as_deref(), &demand)
} else {
plan_serving_stable(&budget, candidates, incumbent.as_deref(), &demand)
plan_serving_stable_traced(&budget, candidates, incumbent.as_deref(), &demand)
};
// The chosen plan's training growth decision, reported by the change-gated
// serving.plan probe below instead of by the planner on every call.
let training_growth = traced.as_ref().and_then(|t| t.training_growth.clone());
let stable = traced.map(|t| t.plan);
{
let credited_bytes: u64 = self
.resource_daemon
Expand Down Expand Up @@ -461,16 +465,7 @@ impl ServingDaemonModule {
// a relative deadband (>1/8 from the last-emitted value), the
// same shape as the #415 prefill deadband: a real re-plan
// (16384→26368) clears it, measurement wobble never does.
let decisions = format!(
"{}|{}|{}|{}|{}|{}|{}",
plan.base_model.model_id,
plan.lanes,
plan.resident_models,
plan.fits_on_gpu,
candidates.len(),
spike_of_served.unwrap_or(0),
demand.lanes,
);
let decisions = plan_decisions(&plan, candidates.len(), spike_of_served, demand.lanes, training_growth.as_ref());
let plan_probe_changed = {
let mut last = self
.last_plan_probe
Expand Down Expand Up @@ -537,6 +532,9 @@ impl ServingDaemonModule {
// above says only whether the WINDOW came out below demand, whatever
// budget did it; this names the term that decided `lanes` (card eb8d1839).
lanes_bound = ?plan.lanes_bound,
// The shared-window growth decision for training reservations, once per
// change (it was a per-call probe of its own, ~4/s).
training_growth = ?training_growth,
"serving plan recomputed",
);
// Warm-slot oversubscription for the ADOPTED plan (#266) —
Expand Down Expand Up @@ -640,6 +638,30 @@ fn account_training_leases(demand: &mut ServingDemand, leases: &[crate::resource
}
}


/// The plan's decisions the `serving.plan` probe changes on: what was chosen and what chose it,
/// never a measured byte figure that wobbles per tick. Includes the training growth decision, so
/// the plan publisher reports it once per change instead of the planner on every call. Pure.
fn plan_decisions(
plan: &ServingPlan,
candidates: usize,
spike_of_served: Option<u64>,
demand_lanes: u32,
training_growth: Option<&crate::inference::planning::window_allocator::TrainingGrowth>,
) -> String {
format!(
"{}|{}|{}|{}|{}|{}|{}|{:?}",
plan.base_model.model_id,
plan.lanes,
plan.resident_models,
plan.fits_on_gpu,
candidates,
spike_of_served.unwrap_or(0), // JUSTIFIED unwrap_or: a key component; 0 is the deadband's documented disabled value, the same one the probe reports
demand_lanes,
training_growth,
)
}

#[cfg(test)]
mod training_lease_tests {
use super::*;
Expand Down Expand Up @@ -682,4 +704,34 @@ mod training_lease_tests {
let allocated = allocate(&demand.requirements, &[model], &budget, 1).expect("fixture fits");
assert_eq!(allocated.training_growth, TrainingGrowth::Reserved { bytes: 100, target: 64_256, reached: true });
}

// what this catches: the training growth decision probed on every plan call (IntelMac
// 2026-11-02: 22,184 rows in 100 min, rotating the core log every ~23 min). It rides the
// plan into serving.plan's change key: an unchanged decision keeps the key, so the
// change-gated probe stays silent; a changed one changes it, so the probe says it once.
#[test]
fn a_training_growth_change_changes_the_plan_key_and_an_unchanged_one_does_not() {
use crate::inference::planning::serving_plan::LanesBound;
let model = ModelFootprint {
model_id: "fixture".into(), weights_bytes: 1 << 30, kv_per_token: 33_000,
context_window: 32_768, capability_rank: 1, fixed_per_lane_bytes: 0,
};
let plan = ServingPlan {
base_model: model,
served_context_window: 32_768,
lanes: 2,
grid_overflow_lanes: 0,
resident_models: 1,
fits_on_gpu: true,
rationale: String::new(),
lanes_bound: LanesBound::Demand,
};
let key = |growth: &TrainingGrowth| plan_decisions(&plan, 1, None, 2, Some(growth));
assert_eq!(key(&TrainingGrowth::NoDemand), key(&TrainingGrowth::NoDemand), "unchanged: the probe stays silent");
assert_ne!(
key(&TrainingGrowth::NoDemand),
key(&TrainingGrowth::Reserved { bytes: 100, target: 64_256, reached: false }),
"a changed growth decision changes the key, so the plan probe reports it once"
);
}
}
Loading