Subtraction, Simplicity & Conventions review — PR #645 (Overhaul Helios prefill scheduling, cost modeling, and two-frame pipelining)
Reviewed at head 05cccb6 against main; 20 files, +1417/−127, single commit. Lens: what can be deleted, collapsed or single-sourced. Correctness/contract issues (e.g. the Mixed !prefill guard, the lost checkpoint stride path at checkpoints.rs:225) are left to the other lens except where a buggy workaround should be deleted rather than patched.
Verdict: the diff is dense but mostly load-bearing. The 497-line cost model, the coalescing value function and the pipelining plumbing each carry real behaviour. Most of the growth is unavoidable. But six concrete subtractions are available, two of them pure duplication inside a single new file.
Key findings
ActionCosts::prefill_durationduplicatesprefill_entry's key literal and 29 lines of body — collapses to two lines on the accessor that is already there (cost.rs:529-557vscost.rs:559-583). Highest-value, zero-risk.- New
PrefillBucketre-implements the 9-sample ring + median that already exists in the same file asReplayTimes(cost.rs:126-169vscost.rs:389-413), down to the identical(lo + hi + 1) / 2middle-pair average. ~25 lines and a second median definition. - The widened non-barrier predicate is now copy-pasted 8 times, and one copy already disagrees with the rest —
policy.rs:1335-1338still carries the old two-kind list whilescheduler.rs:1136,policy.rs:1290,policy.rs:1711,transport.rs:968/1021,threaded.rs:876,parallel.rs:263,threaded/protocol.rs:35carry the three-kind list.GraphKindinmanifest.rs:84-101is the existing home for this kind of predicate. resize_prefillhand-rolls the helper this PR itself adds, including a guard that is already inside the callee (policy.rs:1688-1694vs the neweffective_prefill_catalogatpolicy.rs:341-354).prefill_step_requires_feedbackopens with an unreachable guard, because its only call site already tests the kind (scheduler.rs:1109-1112vsscheduler.rs:1137).coalescing_prefill_log_bonusscans the slot table twice with overlapping filters and recomputes the samepost_prefillexpression (policy.rs:475-477andpolicy.rs:605-607), and its newCandidate::feedbackwrites re-derive a predicate the scheduler now computes authoritatively.
1. prefill_duration should be two lines on prefill_entry (duplication, highest value)
helios/src/scheduler/cost.rs:529-557:
pub fn prefill_duration(
&self,
kind: GraphKind,
capacity: usize,
work: usize,
start_position: u64,
) -> Option<u64> {
let key = Key {
drafts: 0,
kind,
batch: 1,
tokens: capacity,
prefill: 0,
packed: 0,
};
let estimate = self.entry(key)?;
if estimate.nanos == 0 {
return Some(0);
}
Some(
estimate
.prefill
.as_ref()
.and_then(|model| model.predictor)
.map_or(estimate.nanos, |predictor| {
predictor.predict(work, start_position)
}),
)
}
helios/src/scheduler/cost.rs:559-583 builds the identical Key { drafts: 0, kind, batch: 1, tokens: capacity, prefill: 0, packed: 0 }, calls the same self.entry(key)?, and applies the same estimate.nanos == 0 short-circuit. The only difference is map_or(estimate.nanos, predict) vs unwrap_or_else(|| PrefillPredictor::constant(capacity, estimate.nanos)).
The asymmetry is harmless: PrefillPredictor::constant(capacity, estimate.nanos) is the same estimate.nanos that prefill_duration falls back to, so the two paths agree by construction. Replace the body (lines 536-556) with:
self.prefill_entry(kind, capacity)
.map(|predictor| predictor.map_or(0, |p| p.predict(work, start_position)))
Semantics are preserved on all three branches: missing entry → None; nanos == 0 → prefill_entry returns Some(None) → 0; predictor present → predict(work, start_position); no predictor → constant → predict returns the raw nanos. Exactly one self.entry() call still happens, so the ENTRY_LOOKUPS budget asserted at cost.rs:673-677 is unaffected. prefill_predictor (cost.rs:585-587) stays as prefill_entry(..).flatten(). Net: −20 lines and one key literal instead of two, in the function that every prefill planning path in this PR now calls.
2. PrefillBucket duplicates ReplayTimes (duplication inside the new file)
helios/src/scheduler/cost.rs:140-169 (new, added by this PR) against helios/src/scheduler/cost.rs:396-412 (existing):
fn observe(&mut self, sample: PrefillSample) {
self.samples[self.next] = sample;
self.next = (self.next + 1) % self.samples.len();
self.len = (self.len + 1).min(self.samples.len());
}
fn median(&self) -> Option<(f64, f64, f64)> {
...
let mid = self.len / 2;
if self.len % 2 == 0 {
let lo = ordered[mid - 1];
let hi = ordered[mid];
let nanos = ((u128::from(lo.nanos) + u128::from(hi.nanos) + 1) / 2) as f64;
Some((nanos, 0.5 * (lo.u + hi.u), 0.5 * (lo.h (not opened) + hi.h (not opened))))
} else {
versus
self.samples[self.next] = sample;
self.next = (self.next + 1) % self.samples.len();
self.len = (self.len + 1).min(self.samples.len());
...
((u128::from(ordered[middle - 1]) + u128::from(ordered[middle]) + 1) / 2) as u64
Same 9-slot ring, same (lo + hi + 1) / 2 rounding, same even/odd branch. Two independent median definitions in one 945-line file is exactly the kind of drift this repo's cost.rs history has already struggled with (#459 introduced the ring budget, #555 the capacity refresh). Collapse to one ring: make ReplayTimes generic over the sample type, or keep ReplayTimes as the only ring and hold u/h in parallel [f64; 9] arrays inside PrefillCostModel. PrefillSample is already Copy + Default (cost.rs:126-131) and its ordering policy is already spelled out at cost.rs:152-157, so a generic Ring<T> with a comparator is a small, mechanical change. Saves ~25 lines and leaves one definition of the 9-sample median.
3. One predicate, eight copies — and the copies already disagree (conventions / drift)
The widened non-barrier kind list appears verbatim at scheduler.rs:1136, policy.rs:1290, policy.rs:1711, transport.rs:968, transport.rs:1021, threaded.rs:876, parallel.rs:263, threaded/protocol.rs:35. One surviving site was not widened, helios/src/scheduler/policy.rs:1335-1338:
let feedback = !self.costs.is_fresh(cost::Key { drafts,
kind, batch, tokens: capacity, prefill: 0, packed,
})
|| !matches!(kind, GraphKind::Decode | GraphKind::Speculative)
That is the pre-PR two-kind list still present in the continuation_candidate/ordinary-candidate path after this PR taught every other layer that Prefill is a non-barrier kind. Whether this is a correctness bug or harmless depends on how Candidate::feedback is consumed (see finding 6), but structurally it is the predictable result of copying a predicate eight times instead of naming it once.
helios/src/scheduler/manifest.rs:84-101 (not opened) is already the home for exactly this class of predicate (is_prefill, samples). Add:
pub fn pipelines(self) -> bool {
matches!(self, Self::Decode | Self::Speculative | Self::Prefill)
}
and use it at all nine sites. Then widening the set again (or narrowing it) is a one-line change instead of an eight-file audit, and the policy.rs:1338 drift becomes impossible. This is the single highest-leverage convention fix in the PR; without it the next pipelining change will miss a site exactly as this one missed policy.rs:1338.
4. resize_prefill re-implements effective_prefill_catalog and re-tests what the callee tests
helios/src/scheduler/policy.rs:1688-1694:
let catalog = if !request.prompt_logprobs
&& self.prefill_requires_terminal_completion(request, cut)
{
&self.terminal_prefill_catalog
} else {
self.prefill_catalog(request.prefill_kind())
};
This is added by the same PR that adds effective_prefill_catalog (policy.rs:338-352), whose body is the same three-way choice including the ScoredPrefill arm, and whose condition is prefill_requires_terminal_completion(request, cut). The !request.prompt_logprobs conjunct is redundant on top of that: scheduler.rs:909-911 already returns false for prompt_logprobs. Replace the six lines with:
let catalog = self.effective_prefill_catalog(request, request.prefill_kind(), cut);
policy.rs:1089 (self.prefill_catalog(request.prefill_kind())) and policy.rs:1424 (for &capacity in self.prefill_catalog(kind)) are the same shape and deserve the same audit — at minimum, one helper with three callers beats three inline catalog choices. Note the callee returns a &[usize], so *catalog.last()? at policy.rs:1695 keeps working unchanged.
5. Unreachable guard in prefill_step_requires_feedback
helios/src/scheduler.rs:1109-1112:
fn prefill_step_requires_feedback(&self, plan: &ScheduledPlan) -> bool {
if plan.kind != GraphKind::Prefill {
return true;
}
The only call site is scheduler.rs:1137, (plan.kind == GraphKind::Prefill && self.prefill_step_requires_feedback(plan)), so the guard can never fire. It is not free: it encodes "this helper is safe to call for any kind", which is false for the caller's contract and invites a future caller to rely on a true return that means something different from the function's documented purpose (the _requires_feedback name reads as a total predicate).
Delete lines 1110-1112 and let the kind dispatch live in one place. Replace scheduler.rs:1135-1137:
plan.requires_feedback
|| !matches!(plan.kind, GraphKind::Decode | GraphKind::Speculative | GraphKind::Prefill)
|| (plan.kind == GraphKind::Prefill && self.prefill_step_requires_feedback(plan))
with a single dispatch:
plan.requires_feedback
|| match plan.kind {
GraphKind::Decode | GraphKind::Speculative => false,
GraphKind::Prefill => self.prefill_step_requires_feedback(plan),
_ => true,
}
Same semantics, one place where the kind set is named (and, with finding 3, it becomes !plan.kind.pipelines()).
6. Candidate::feedback's two new writes duplicate plan_requires_feedback; one scan of the slot table is enough for the coalescing bonus
helios/src/scheduler/policy.rs gains two writes of the non-barrier predicate into Candidate::feedback:
let selected = 1u64 << slot;
// `materialize` computes the authoritative `plan_requires_feedback` after
// checkpoint cuts and any prefill resizing are resolved.
let feedback = !matches!(kind, GraphKind::Decode | GraphKind::Speculative | GraphKind::Prefill);
(policy.rs:1287-1290, feeding policy.rs:1304) and
candidate.duration_ns = duration_ns;
candidate.feedback =
!matches!(kind, GraphKind::Decode | GraphKind::Speculative | GraphKind::Prefill);
(policy.rs:1705-1711).
The comment at 1288-1289 states the problem itself: materialize computes the authoritative value. At policy.rs:1793-1797 the candidate value goes into plan.step.requires_feedback and is then replaced by plan.step.requires_feedback = self.plan_requires_feedback(&plan);, and plan_requires_feedback reads the plan-level plan.requires_feedback (scheduler.rs:1135), not the step field it just wrote. So these two assignments are either (a) discarded — dead code added by this PR — or (b) live only as a conservative forcing term through some step→plan copy, in which case they are a third copy of a predicate the scheduler now owns, and a copy already out of sync at policy.rs:1338. Either way the right subtraction is the same: delete both new assignments and the 1287-1290 comment. If a forcing hint is genuinely wanted, it belongs in Candidate as force_feedback: bool set only where a caller truly needs to force it (policy.rs:1540's feedback: true for Mixed is the one real user), not recomputed from a kind set. Deleting the field entirely also removes policy.rs:92, policy.rs:1540, rounds.rs:714, rounds.rs:876 and one literal in policy_tests.rs:58. This is the one place where a worrying behaviour (the policy.rs:1338 drift) is better fixed by deletion than by patching the outlier.
Second, in the new coalescing_prefill_log_bonus: the function walks all slots to classify prefiller/decoder and computes
let post_prefill = (r.max_new_tokens as usize)
.min(self.limits.max_sequence_tokens.saturating_sub(r.prompt_tokens))
.saturating_sub(1);
(policy.rs:475-477, guarded by !r.prompt_logprobs && r.scheduled_consumed < r.prompt_tokens as u64), then walks all slots again and recomputes the byte-identical expression at policy.rs:605-607 under the same guard (policy.rs:592-601). Capture (slot, post_prefill) in the first pass in a [(usize, f64); REQUEST_LANES] — REQUEST_LANES-bounded, the same pattern decoder_slots already uses at policy.rs:452-468 — and reuse it in the second loop. That removes one full scan of the slot table from a function that runs on every lane_values call, and ~12 lines.
Minor / optional
helios/src/scheduler/policy.rs:26,42:coalescing_horizon_tokens(default64.0, validated atpolicy.rs:225-230) is a new always-on value coefficient with no--scheduler-*flag and no server-config key, while every sibling coefficient inPolicyConfighas one, and it is missing from the defaults table atdocs/scheduling-runtime-design.md:651-661andhelios/README.md:478-488(not opened). Under AGENTS.md's "no configuration switches without a concrete requirement", pick one: wire it throughflare-cli/server/config.rs(not opened) like its siblings, or make it a module const next to the other rationale-documented constants (PREFILL_CTX_SCALE, the 0.75/0.15 narrow-regime thresholds). Leaving it as an undocumented, unreachable knob is the worst of both.scripts/scheduling_sim/workloads.py:52-53addsburst_four_lowandburst_eight_low, which nothing references —tests/test_scheduling_utility.py:339only exercisesburst_eight. Either assert on them or drop them; unused workload definitions in a 54-line file are pure noise.- Docs were updated in three places (
docs/scheduling-runtime-design.md:198-207,403-423,567-577), which is above the repo norm rather than below it. No other AGENTS.md structural violations found. The inverted testprefill_frames_drain_even_when_the_next_request_is_independent→nonterminal_prefill_frames_pipeline_two_deep_and_terminal_chunks_drain(policy_tests.rs:1420-1480) is the correct convention for a deliberate behaviour reversal — rename with the change — not a smell.
Open questions / not verified
- Finding 6's liveness branch: whether
Candidate::feedbackis truly discarded or is copied step→plan somewhere I did not read. If a step→plan copy exists, the field is live but the two new writes are still duplicative and still disagree withpolicy.rs:1338; either way the recommendation (delete the new writes, single-source the predicate viaGraphKind::pipelines()) holds. A one-line grep for.step.requires_feedbackinscheduler.rssettles it. - Whether losing the checkpoint stride fast path (
checkpoints.rs:225conditioned onconstant_ns(), which requirestheta1 == theta2 == 0andu_max >= 0.75,cost.rs:298-302) is intended. Flagged as correctness/performance for the other lens, but it bears on finding 2: if the non-constant regime is the common regime, thenPrefillCostModel's extra machinery buys less than the description implies while costing the O(1) budget #459 introduced. - Merge-order duplication with #644, which rewrites
LaneValue(decode_within,finish) in the sameutility.rs/service.rs/policy.rsregions this PR addsprefill_finishto. Findings 3 and 6 shrink the conflict surface; nothing here blocks it.