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
24 changes: 13 additions & 11 deletions core/continuum-cognition/src/cognition/working_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,20 +918,22 @@ pub fn requirements_for(
.iter()
.map(|p| {
let demand = working_set.demand_of(*p);
let wish = demand
.as_ref()
.map(requirement_tokens)
.unwrap_or(0); // JUSTIFIED unwrap_or: a mind with no turn yet is Unknown to the allocator (0 = no measurement, never a number)
let need = demand
.as_ref()
.and_then(need_tokens)
.unwrap_or(0); // JUSTIFIED unwrap_or: no held turn measured = no floor beyond the target (0 = no measurement)
use crate::inference::planning::window_allocator::{LaneRequirement, Tokens};
// Measured or not, as a type: no turn yet is unmeasured, never a zero standing in for one.
let wish = demand.as_ref().and_then(|d| Tokens::new(requirement_tokens(d)));
let need = demand.as_ref().and_then(need_tokens).and_then(Tokens::new);
// Her parked training examples' reservation, carried beside her conversational target
// and need (card ea157b1a): the allocator grows toward it only from spare budget.
let training = crate::genome::fine_tuning::training_demand::TrainingDemand::for_persona(*p).reservation();
crate::inference::planning::window_allocator::LaneRequirement::from_demand(*p, wish, crate::inference::planning::serving_plan::SENT_HEADROOM)
.with_need(need)
.with_training(training)
let measured = match wish {
None => LaneRequirement::unmeasured(*p),
Some(wish) => LaneRequirement::wished(*p, wish, crate::inference::planning::serving_plan::SENT_HEADROOM),
};
match need {
Some(need) => measured.held(need),
None => measured,
}
.with_training(training)
})
.collect()
}
Expand Down
50 changes: 40 additions & 10 deletions core/continuum-engine/src/capacity/window_grant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

use std::collections::BTreeMap;

use crate::inference::planning::window_allocator::Tokens;

/// What the serving daemon grants the local minds on its engine's pool, each by her persona id.
/// Published on every tick that moves a grant; a mind's binding follows hers.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
Expand All @@ -26,9 +28,6 @@ pub struct PoolGrants {
/// window), a slot's window on fixed slots. No grant exceeds it.
pub per_sequence_cap: u32,
pub by_mind: BTreeMap<uuid::Uuid, u32>,
/// The minds whose requirement is not their own measurement yet but the population's (the
/// median measured requirement on the seat), marked so no reader takes it for hers.
pub from_population: std::collections::BTreeSet<uuid::Uuid>,
}

impl PoolGrants {
Expand All @@ -52,12 +51,17 @@ pub fn current_grant(persona: uuid::Uuid) -> Option<u32> {
GRANTS.get()?.borrow().as_ref()?.grant_for(persona)
}

/// One mind's current demand on the pool: what her next turn requires, and whether a turn of hers
/// is in flight now.
/// One measured mind's current demand on the pool: her floor and target, and whether a turn of hers
/// is in flight now. A mind not measured yet has no `Demand`: there is no number to grant her
/// (`Measure::Unmeasured`), and her first view measures her.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Demand<K> {
pub mind: K,
pub required_tokens: u32,
/// The least her turn holds: below it she cannot think (the median of her held turns, else her
/// wish). Never trimmed here; a need above what one sequence holds waits.
pub need: Tokens,
/// What she would read whole (her wish with headroom), granted when the pool holds every target.
pub target: Tokens,
pub in_flight: bool,
/// The most she may be granted: what one sequence of the engine can hold.
pub ceiling: u32,
Expand Down Expand Up @@ -98,9 +102,9 @@ impl<K: Ord + Copy> WindowPolicy<K> for DemandFollowing {
// waits). A holder whose need outgrew it keeps what she holds as her need: her binding stays
// counted, so it can never be bound beside the pool uncounted (Cormac on #5745: A holding 40k,
// need 70k, dropped while B was granted 60k bound 100k on a 65,536 pool).
let need = |d: &Demand<K>| match d.required_tokens > d.ceiling {
let need = |d: &Demand<K>| match d.need.get() > d.ceiling {
true => holds(d),
false => d.required_tokens,
false => d.need.get(),
};
let demands: Vec<&Demand<K>> = demands.iter().filter(|d| need(d) > 0).collect();
let demands = demands.as_slice();
Expand All @@ -109,6 +113,12 @@ impl<K: Ord + Copy> WindowPolicy<K> for DemandFollowing {
let want = |d: &Demand<K>| need(d).max(holds(d));
let total = |f: &dyn Fn(&Demand<K>) -> u32| demands.iter().map(|d| u64::from(f(d))).sum::<u64>();
let pool = u64::from(pool_tokens);
// Room for every target: each mind reads whole, never past one sequence, never below what she
// holds. Otherwise needs first, and what is left stays in the pool.
let aim = |d: &Demand<K>| d.target.get().min(d.ceiling).max(want(d));
if total(&aim) <= pool {
return demands.iter().map(|d| (d.mind, aim(d))).collect();
}
if total(&want) <= pool {
return demands.iter().map(|d| (d.mind, want(d))).collect();
}
Expand Down Expand Up @@ -162,8 +172,11 @@ impl<K: Ord + Copy> WindowPolicy<K> for DemandFollowing {
mod tests {
use super::*;

fn demand(mind: u32, required_tokens: u32, in_flight: bool, ceiling: u32) -> Demand<u32> {
Demand { mind, required_tokens, in_flight, ceiling }
use crate::inference::planning::window_allocator::test_tokens as t;

/// A mind whose need is her target (a wish with no held turn yet).
fn demand(mind: u32, need: u32, in_flight: bool, ceiling: u32) -> Demand<u32> {
Demand { mind, need: t(need), target: t(need), in_flight, ceiling }
}

// what this catches: a window divided by a count (Joel, 2026-11-03: "What a mind needs not what
Expand Down Expand Up @@ -262,4 +275,21 @@ mod tests {
assert!(bound <= u64::from(pool), "bound {bound} on a {pool} pool: {grants:?}");
}


// what this catches (the M5, 2026-11-03, b7c815484): grants sized from her wish instead of her
// need. Benchy's wish took the model's whole 262,144 of a 524,288 pool, Aiko's 104,015 and
// Aris's 88,380, and Atlas got nothing. Needs are granted first; targets only when every target
// fits.
#[test]
fn needs_are_granted_before_any_target_and_targets_only_when_all_fit() {
let pool = 524_288;
let mind = |m, need, target| Demand { mind: m, need: t(need), target: t(target), in_flight: false, ceiling: 262_144 };
let wishes = [mind(1, 70_000, 262_144), mind(2, 60_000, 104_015), mind(3, 50_000, 88_380), mind(4, 40_000, 90_000)];
let grants = DemandFollowing.grant(&wishes, &BTreeMap::new(), pool);
assert_eq!(grants, BTreeMap::from([(1, 70_000), (2, 60_000), (3, 50_000), (4, 40_000)]), "every need, no target: the targets sum past the pool");
let modest = [mind(1, 70_000, 100_000), mind(2, 60_000, 104_015), mind(3, 50_000, 88_380), mind(4, 40_000, 90_000)];
let grants = DemandFollowing.grant(&modest, &BTreeMap::new(), pool);
assert_eq!(grants, BTreeMap::from([(1, 100_000), (2, 104_015), (3, 88_380), (4, 90_000)]), "every target fits: each reads whole");
}

}
63 changes: 40 additions & 23 deletions core/continuum-engine/src/capacity/window_sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@

use std::collections::BTreeMap;

use crate::inference::planning::window_allocator::Tokens;

/// A scenario's literal token count, checked nonzero when the scenario compiles: a zero literal is a
/// compile error, never a runtime fallback.
macro_rules! tokens {
($n:expr) => {
const {
match Tokens::new($n) {
Some(tokens) => tokens,
None => panic!("a scenario's token literal is never zero"),
}
}
};
}

/// A mind on the pool (a resident, or the scratch traffic beside them).
pub type MindId = u32;

Expand All @@ -26,7 +41,8 @@ pub type MindId = u32;
pub struct DemandEvent {
pub at_ms: u64,
pub mind: MindId,
pub required_tokens: u32,
/// Her need, which is her target in the sim (a wish with no held turn).
pub need: Tokens,
pub in_flight: bool,
}

Expand Down Expand Up @@ -68,7 +84,8 @@ pub fn run(scenario: &WindowScenario, policy: &mut dyn WindowPolicy<MindId>) ->
event.mind,
Demand {
mind: event.mind,
required_tokens: event.required_tokens,
need: event.need,
target: event.need,
in_flight: event.in_flight,
// the shared pool: one sequence may hold all of it
ceiling: scenario.pool_tokens,
Expand All @@ -89,7 +106,7 @@ pub fn run(scenario: &WindowScenario, policy: &mut dyn WindowPolicy<MindId>) ->
true => held.get(&d.mind).copied().unwrap_or(0), // unwrap_or: a turn that holds nothing yet is owed only her demand
false => 0,
};
u64::from(d.required_tokens.max(floor))
u64::from(d.need.get().max(floor))
};
let demanded: u64 = current.iter().map(owed).sum();
// Granted past what is owed: room an idle mind holds that a reading mind could have used.
Expand All @@ -109,7 +126,7 @@ pub fn run(scenario: &WindowScenario, policy: &mut dyn WindowPolicy<MindId>) ->
{
out.in_flight_shrinks += 1;
}
if granted < demand.required_tokens {
if granted < demand.need.get() {
if demanded <= u64::from(scenario.pool_tokens) {
out.unmet_when_fits += 1;
}
Expand Down Expand Up @@ -150,24 +167,24 @@ pub fn m5_uneven_reading() -> WindowScenario {
const ATLAS: MindId = 3;
const BENCHY: MindId = 4;
const SCRATCH: MindId = 5;
let e = |at_ms, mind, required_tokens, in_flight| DemandEvent {
let e = |at_ms, mind, need, in_flight| DemandEvent {
at_ms,
mind,
required_tokens,
need,
in_flight,
};
WindowScenario {
name: "m5-uneven-reading",
pool_tokens: 290_560,
events: vec![
e(0, ATLAS, 12_000, false),
e(1, ARIS, 40_000, true),
e(2, BENCHY, 52_000, true),
e(3, SCRATCH, 1_000, true),
e(4, AIKO, 70_000, true),
e(5, AIKO, 76_000, true),
e(6, ATLAS, 9_000, false),
e(7, BENCHY, 58_000, true),
e(0, ATLAS, tokens!(12_000), false),
e(1, ARIS, tokens!(40_000), true),
e(2, BENCHY, tokens!(52_000), true),
e(3, SCRATCH, tokens!(1_000), true),
e(4, AIKO, tokens!(70_000), true),
e(5, AIKO, tokens!(76_000), true),
e(6, ATLAS, tokens!(9_000), false),
e(7, BENCHY, tokens!(58_000), true),
],
}
}
Expand Down Expand Up @@ -201,11 +218,11 @@ mod tests {
// because her demand fell mid-turn; the grant she is using holds until the turn ends.
#[test]
fn a_turn_in_flight_keeps_its_grant_when_her_demand_falls_and_everything_fits() {
let e = |at_ms, mind, required_tokens, in_flight| DemandEvent { at_ms, mind, required_tokens, in_flight };
let e = |at_ms, mind, need, in_flight| DemandEvent { at_ms, mind, need, in_flight };
let scenario = WindowScenario {
name: "falling-demand-in-flight",
pool_tokens: 100_000,
events: vec![e(0, 1, 60_000, true), e(1, 1, 30_000, true), e(2, 2, 20_000, false)],
events: vec![e(0, 1, tokens!(60_000), true), e(1, 1, tokens!(30_000), true), e(2, 2, tokens!(20_000), false)],
};
let run = run(&scenario, &mut DemandFollowing);
assert!(run.kept(), "{run:?}");
Expand All @@ -216,11 +233,11 @@ mod tests {
// fits 100k, the floors do not, so the neighbour's 40k is the pool's limit, not a breach.
#[test]
fn fits_and_slack_count_a_turn_in_flight_at_what_it_holds() {
let e = |at_ms, mind, required_tokens, in_flight| DemandEvent { at_ms, mind, required_tokens, in_flight };
let e = |at_ms, mind, need, in_flight| DemandEvent { at_ms, mind, need, in_flight };
let scenario = WindowScenario {
name: "held-floor-beside-a-big-neighbour",
pool_tokens: 100_000,
events: vec![e(0, 1, 60_000, true), e(1, 1, 30_000, true), e(2, 2, 50_000, false)],
events: vec![e(0, 1, tokens!(60_000), true), e(1, 1, tokens!(30_000), true), e(2, 2, tokens!(50_000), false)],
};
let run = run(&scenario, &mut DemandFollowing);
assert!(run.kept(), "{run:?}");
Expand All @@ -229,19 +246,19 @@ mod tests {
// what this catches: an overloaded pool shrinking a turn in flight to seat another.
#[test]
fn an_overloaded_pool_never_shrinks_a_turn_in_flight() {
let e = |at_ms, mind, required_tokens, in_flight| DemandEvent {
let e = |at_ms, mind, need, in_flight| DemandEvent {
at_ms,
mind,
required_tokens,
need,
in_flight,
};
let scenario = WindowScenario {
name: "overloaded",
pool_tokens: 100_000,
events: vec![
e(0, 1, 60_000, true),
e(1, 2, 60_000, false),
e(2, 3, 30_000, false),
e(0, 1, tokens!(60_000), true),
e(1, 2, tokens!(60_000), false),
e(2, 3, tokens!(30_000), false),
],
};
let run = run(&scenario, &mut DemandFollowing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// the plan says so with fits_on_gpu=false instead of planning a 2k lane.
#[test]
pub(super) fn with_requirements_the_plan_is_the_allocators_never_a_starved_lane() {
use crate::inference::planning::window_allocator::LaneRequirement;
use crate::inference::planning::window_allocator::{test_tokens, LaneRequirement};
const GB: u64 = 1 << 30;
let qwen = ModelFootprint {
model_id: "qwen-27b".into(),
Expand All @@ -23,8 +23,8 @@
};
let host = HostBudget { usable_bytes: 30 * GB, perf_cores: 8 };
let two = vec![
LaneRequirement::declared(uuid::Uuid::new_v4(), 70_000),
LaneRequirement::declared(uuid::Uuid::new_v4(), 70_000),
LaneRequirement::declared(uuid::Uuid::new_v4(), test_tokens(70_000)),
LaneRequirement::declared(uuid::Uuid::new_v4(), test_tokens(70_000)),
];
// The poisoned inputs the 5090 had: a sent median of 828 and a sticky 2048.
let demand = ServingDemand::new(2, Some(56_057))
Expand All @@ -35,7 +35,7 @@
assert_eq!((plan.lanes, plan.served_context_window), (2, 70_000), "{}", plan.rationale);
assert!(plan.fits_on_gpu);

let five: Vec<_> = (0..5).map(|_| LaneRequirement::declared(uuid::Uuid::new_v4(), 70_000)).collect();
let five: Vec<_> = (0..5).map(|_| LaneRequirement::declared(uuid::Uuid::new_v4(), test_tokens(70_000))).collect();
let squeezed = plan_serving(&host, std::slice::from_ref(&qwen), &ServingDemand::new(5, Some(56_057)).with_requirements(five)).expect("plan");
assert_eq!((squeezed.lanes, squeezed.served_context_window), (2, 70_000), "minds shed, window kept");
assert_eq!(squeezed.grid_overflow_lanes, 3);
Expand Down
Loading
Loading