Subtraction, Simplicity & Conventions review — PR #681 helios/scheduler: use active cohort mean in coalescing horizon
Key findings
Verdict: the diff is already lean; it removes more structure than it adds. The production change (helios/src/scheduler/policy.rs, +11/−10, net +1 LOC) deletes the let mut peer_budget accumulator, deletes the unreachable peer_budget == usize::MAX arm of the early-return guard, and adds no flag, config key, wrapper, fallback layer or abstraction. The simulator change (scripts/scheduling_sim/utility.py) is a net −4 lines and also deletes the peer_budget reduction. Nothing in AGENTS.md:188-193 ("Do not add frameworks, extension points, speculative compatibility, fallback layers, or configuration switches without a concrete requirement") is violated — a rare thing to be able to say about a scheduling PR in this series.
The four worthwhile simplifications below are all on lines this PR itself writes, and all fall under the AGENTS.md:192-193 allowance that "Refactoring must directly support the requested behavior". None is a blocker; A is the one I would actually ask for, because it also fixes the file's only two >150-column lines.
| # | Where | What | Weight |
|---|---|---|---|
| A | policy.rs:627, :636, :668-672 |
Same cohort-mean expression written three times; collapse to one helper | medium-high |
| B | policy.rs:627, :636 |
.max(1.0) is provably non-binding — delete rather than preserve in the helper |
medium |
| C | policy.rs:673 |
if horizon > 0.0 is unreachable behind existing guards — delete and unindent |
medium |
| D | policy_tests.rs:7002-7011 |
New test hand-copies production interpolation instead of calling rounds::forecast_tokens |
medium |
| E | policy.rs:530-533 |
Parallel arrays where the adjacent cohort uses one tuple array (optional, only with A) | low |
A. MEDIUM-HIGH — one cohort mean written three times
Three copies of Σ f(rem_i) / n sit within 50 lines of each other, in the function this PR edits.
helios/src/scheduler/policy.rs:627 (verbatim):
let mean_out = decoder_rems[..decoder_count].iter().map(|&rem| expected_at(eff_width).min(rem as f64).max(1.0)).sum::<f64>() / decoder_count as f64;
helios/src/scheduler/policy.rs:636 (verbatim):
let mean_out = decoder_rems[..decoder_count].iter().map(|&rem| expected_at(width as f64).min(rem as f64).max(1.0)).sum::<f64>() / decoder_count as f64;
helios/src/scheduler/policy.rs:668-672 (verbatim):
let horizon = decoder_rems[..decoder_count]
.iter()
.map(|&rem| (rem as f64).min(ph))
.sum::<f64>()
/ decoder_count as f64;
Replace all three with one free function. policy.rs already has module-level free helpers alongside this impl (and the sibling module exposes rounds.rs:991-993 forecast_tokens in the same style), so there is no new abstraction being invented — it is the existing local idiom:
fn cohort_mean(rems: &[usize], f: impl Fn(f64) -> f64) -> f64 {
rems.iter().map(|&rem| f(rem as f64)).sum::<f64>() / rems.len() as f64
}
with call sites let mean_out = cohort_mean(&decoder_rems[..decoder_count], |rem| expected_at(eff_width).min(rem)); and let horizon = cohort_mean(&decoder_rems[..decoder_count], |rem| rem.min(ph));.
Two supporting facts:
- Style/convention evidence:
policy.rs:627and:636are the only two lines in the 1,969-line file longer than 150 columns that this PR introduces (the file otherwise wraps multi-line, as:668-672two lines below does correctly, and as:1093-1097does elsewhere). Extracting the helper removes both violations for free. - The simulator already factors this, which is what the design spec asks Helios to converge on:
scripts/scheduling_sim/utility.py:215-219computes a singlemean_outsum reused by every graph shape, and this PR's own Python change reuses the same shape atutility.py:234-235. Rust should mirror that structure rather than inline it twice.
B. MEDIUM — the .max(1.0) clamp is dead; delete it, don't carry it into the helper
Both PR-added lines carry .max(1.0):
let mean_out = decoder_rems[..decoder_count].iter().map(|&rem| expected_at(eff_width).min(rem as f64).max(1.0)).sum::<f64>() / decoder_count as f64;
It can never bind, by two independent guarantees in the same function:
expected_at(policy.rs:586-598) returns exactly1.0on theeff_width <= 1.0 || mean_curve.is_empty()branch, otherwise1.0 + sum, so it is>= 1.0unconditionally; andmean_curveentries come frompowered_curve(rounds.rs:625-627), which raises probabilities in[0,1]to a non-negative exponent, so the added terms are>= 0.- every recorded decoder has
rem >= 1, becausepolicy.rs:550isif rem > 0 { ... }.
Therefore min(expected_at(..), rem) >= 1.0 always. The clamp is evaluated 2 * decoder_count times per planning call for nothing. Delete .max(1.0) at both sites and do not put it in cohort_mean. (The old expected_at(..).min(peer_budget as f64).max(1.0) was equally vacuous, so this is dead code the PR relocated rather than introduced — which is exactly the kind of thing a subtraction pass should not let through again.)
C. MEDIUM — the if horizon > 0.0 guard at policy.rs:673 is unreachable
helios/src/scheduler/policy.rs:667-682 (verbatim):
let ph = self.policy.coalescing_horizon_tokens.min(post_prefill as f64);
let horizon = decoder_rems[..decoder_count]
.iter()
.map(|&rem| (rem as f64).min(ph))
.sum::<f64>()
/ decoder_count as f64;
if horizon > 0.0 {
// Amortize the unlocked coalescing surplus uniformly across the prefiller's
// uncached prompt tokens so total credit sums to 1x regardless of chunking
// and chunk sizing stays smooth under active decode waiting age.
let per_token = (best_surplus_per_horizon * horizon) / initial_prefill as f64;
if per_token.is_finite() && per_token > 0.0 {
bonus_log[slot] = per_token.ln();
}
}
horizon > 0.0 is guaranteed by four upstream guards in the same function: coalescing_horizon_tokens > 0.0 (:527 early return), rem > 0 for every recorded decoder (:550), post_prefill > 0 so post_prefill as f64 >= 1.0 (:561, hence ph > 0), and decoder_count >= 1 (:567 early return). So every term of the sum is strictly positive.
Meanwhile the degenerate-value defence the outer guard might have been standing in for is already the inner ones at :678. Delete if horizon > 0.0 { and its closing brace, unindent the body one level, which also lets the three-line comment sit at natural indent. This shortens the amortization block by two lines and one nesting level while keeping identical behaviour.
D. MEDIUM — the new test re-implements production interpolation; use the existing helper
helios/src/scheduler/policy_tests.rs:7002-7011 (verbatim):
let expected_at = |eff_width: f64| -> f64 {
let drafts = eff_width - 1.0;
let full = (drafts.floor() as usize).min(curve.len());
let frac = (drafts - full as f64).clamp(0.0, 1.0);
let mut sum = 1.0 + curve[..full].iter().sum::<f64>();
if full < curve.len() {
sum += frac * curve[full];
}
sum
};
This is a hand transcription of production expected_at (policy.rs:586-598). It is unnecessary here: every width the test actually queries is integral — 8.0 (:7012), 6.0 and 4.0 (:7044-7045), which are the packed_per_lane values 16/2, 12/2, 12/3 — and at integer width the interpolation collapses to 1.0 + Σ curve[..w-1], which is exactly the existing helper rounds::forecast_tokens:
helios/src/scheduler/rounds.rs:991-993:
pub(super) fn forecast_tokens(powered: &[f64], width: usize) -> f64 {
f64::from(width != 0) + powered[..width.saturating_sub(1)].iter().sum::<f64>()
}
Replace the closure with let expected_at = |w: usize| super::rounds::forecast_tokens(&curve, w); and change the three call sites to integer literals (8, 6, 4). Ten lines deleted, and the test then cannot silently drift from production arithmetic — which matters, since this test's entire purpose is to pin the production formula against a hand-derived 32.5/cohort-mean expectation. The precedent is already in this file: policy_tests.rs:7152 uses super::rounds::forecast_tokens for exactly this purpose.
E. LOW / OPTIONAL — parallel arrays where the neighbouring cohort uses a tuple array
helios/src/scheduler/policy.rs:530-533 (verbatim):
let mut decoder_slots = [0usize; REQUEST_LANES];
let mut decoder_rems = [0usize; REQUEST_LANES];
let mut decoder_count = 0usize;
let mut prefillers = [(0usize, 0usize, 0usize); REQUEST_LANES];
decoder_slots and decoder_rems are two parallel [usize; REQUEST_LANES] arrays (2 × 4 KiB on a 512-lane build) that are only ever written together (:551-553) and read together (:574, :627, :636, :668), while the prefill cohort three lines below uses a single tuple array. Collapse to let mut decoders = [(0usize, 0usize); REQUEST_LANES]; of (slot, rem), matching the local convention and removing one buffer and one indexed store per decoder. Only worth doing together with A (the helper should then take &decoders[..decoder_count] and map over tuples).
Checked and deliberately not flagged (so this report does not pad)
- No new knob/wrapper/fallback to subtract.
coalescing_horizon_tokensis pre-existing (#645), not added here; #681 adds no new config, no flag, no branch layer. Whether it should have an off-switch is a reach/charter argument for other lenses, not a subtraction finding. scripts/scheduling_sim/utility.py:232-236is a net deletion (peer_budgetgone), introduces no duplication, and itsphlocal faithfully mirrors the Rust structure. Nothing to simplify.- The three inline
GraphDescriptorliterals in the new test (policy_tests.rs:6941,:6956,:6973, 14 fields each) are verbose, but that is the established style in this file (:4139,:4280,:4500) and no builder exists to reuse — not a convention violation. coalescing_bonus(policy_tests.rs:6866-6875) forcingcoalescing_horizon_tokens = 0.0to isolate the term follows the neighbouring burst test's technique (run_burst(0.0, ..)at:6794-6845). Fine.add_active_decoder(policy_tests.rs:6877-6885) near-duplicates the inlineadd_decoderclosure at:6698-6713, but that one carries test-specific acceptance seeding; extracting a shared helper would be unrelated churn.docs/scheduling-runtime-design.md:222-235is a one-clause rewrite, not expansion (+5/−2). No comment bloat added.- The new Python test (
tests/test_scheduling_utility.py:506-526) is compact, asserts expected values rather than re-deriving them through a copy of production code, and adds the[1, 100]heterogeneous pair the Rust side also covers. Good, not to be touched.
Open questions (this lens)
- Whether the author would accept the three-site helper in A; it is the only item where I would push, and it also resolves the two over-long lines.
- Whether
.max(1.0)(B) andif horizon > 0.0(C) were intended as defensive guards against a futurecoalescing_horizon_tokens == 0.0configuration path; if so they should be asserted in a test (or dropped with a comment), not left as unreachable branches. - I did not audit
scripts/scheduling_sim/learning.py(not opened) or the formal models for duplication with this change — the changed formulas appear only inutility.pyandpolicy.rs, and no third copy was found by searching for the cohort-mean shape.