Subtraction and simplicity scratchpad · NVIDIA/hyperflare-inference #661

Raw Markdown for agents: https://autobot.eidonix.com/p/26766720-9d2e-44d6-87ff-a21b3ab8c4a9.md

#1 [high] Research summary

Subtraction, Simplicity & Conventions review — PR #661 @ 3d868f84

The diff is leaner than the average change of this size: it removes the 60-line duplicated prefill branch from add_candidate into a dedicated add_prefill_candidate, caches the previously recomputed terminal_prefill_window (scheduler.rs:847-866), adds no CLI flag, no PolicyConfig field, no unsafe, and keeps the add_candidate / add_prefill_candidate split conventional. What remains is 4–5 real simplifications, all with a single cause: the age-price branch was added alongside the existing terminal-completion logic instead of through it.

1. Delete the last_interp memo and the 9-argument function it forces. policy.rs:517-543 plus policy.rs:1092, policy.rs:1429:

    #[allow(clippy::too_many_arguments)]
    fn prefill_candidate_ns(
        &self, plan: &mut PrefillSlotPlan, request: &LiveRequest, kind: GraphKind, capacity: usize,
        work: usize, age_price: f64, predictor: Option<cost::PrefillPredictor>,
        last_interp: &mut Option<(usize, u64, Option<u64>)>,
    ) -> Option<u64> {
        ...
        if age_price > 0.0 && plan.nat_fit.is_some_and(|nat| capacity <= nat) {
            let interp = match *last_interp {
                Some((w, pos, res)) if w == work && pos == request.scheduled_consumed => res,
                _ => {
                    let res = self.costs.interpolated_prefill_duration(kind, capacity, work, request.scheduled_consumed);
                    *last_interp = Some((work, request.scheduled_consumed, res));
                    res
                }
            };
            if let Some(ns) = interp { return Some(ns); }
        }

The memo is keyed on (work, scheduled_consumed) and is declared per capacity (it hits only when two slots in the same mask happen to have identical work), so it saves almost nothing while costing an opaque tuple type spelled out twice, an #[allow(clippy::too_many_arguments)] that exists only for the memo parameter, and a match nested in a closure-in-match. Replace the whole block with if let Some(ns) = self.costs.interpolated_prefill_duration(kind, capacity, work, request.scheduled_consumed) { return Some(ns); } and drop last_interp from prefill_candidate_ns, add_prefill_candidate (:1429) and continuation_candidate (:1092). If the cost genuinely matters, the memo belongs in ActionCosts where the O(entries) scan lives (cost.rs:716), not at the call site.

2. Three near-identical catalog scans. scheduler.rs:973-993 (prefill_must_complete_terminally_from), policy.rs:417-433 (natural_fit_prefill_capacity_for_terminal) and policy.rs:451-459 (fits_single_prefill_from) all iterate a prefill catalog, compute prefill_work_*, call resolve_prefill_kind*, skip GraphKind::Prefill when terminal_only, and then apply one differing acceptance test (!= Prefill / duration(...).is_some() / prefill_entry(...).is_some()):

        for &capacity in catalog {
            let work = self.prefill_work_from(request, start, capacity, cut);
            if work == 0 {
                continue;
            }
            if self.resolve_prefill_kind_from(request, start, capacity, work, cut)
                == GraphKind::Prefill
            {
                return false;
            }
            if capacity >= limit {
                return true;
            }
        }

Collapse to one helper taking the catalog, terminal_only, and a impl Fn(usize, usize, GraphKind) -> bool acceptance predicate; the three callers become 3–4 lines each.

3. The age-price terminal dispatch is spelled twice, with different predicates. scheduler.rs:1013-1021 dispatches on age_price > 0.0; service.rs:297-301 does the same 5 lines by hand on has_active_age_priced_stream() and with start = consumed_tokens rather than prefill_cursor:

            let terminal_only = if self.has_active_age_priced_stream() {
                self.prefill_must_complete_terminally_from(request, start, cut)
            } else {
                self.prefill_requires_terminal_completion_from(request, start, cut)
            };

service.rs should call a single prefill_requires_terminal_completion_for_age(request, start, cut, ...) (widen the existing helper to take start and the stream predicate) so one expression decides this everywhere. Relatedly, has_active_age_priced_stream() (scheduler.rs:996-1005) scans all slots and is called once per request from service_rates inside the rates fold at policy.rs:750-757 — O(slots²) per plan. The PR just learned this lesson for terminal_prefill_window; do the same here: compute it once per lane_values pass and pass the bool into service_rates, or store it as derived state like terminal_prefill_window.

4. Small wrappers worth deleting. policy.rs:371-374:

    fn prefill_catalog_for_terminal(&self, kind: GraphKind, terminal_only: bool) -> &[usize] {
        self.prefill_catalog(if kind == GraphKind::ScoredPrefill { kind }
            else if terminal_only { GraphKind::TerminalPrefill } else { GraphKind::Prefill })
    }

is a re-dispatch over prefill_catalog (:363-369) that uses GraphKind as a boolean; a single match kind { ScoredPrefill => kind, _ if terminal_only => GraphKind::TerminalPrefill, _ => GraphKind::Prefill } at the two call sites (policy.rs:527, :1840) removes the extra indirection level. Also policy.rs:545-548:

    fn is_unmeasured_candidate(&self, candidate: Candidate) -> bool {
        candidate.rate.duration_ns == 0
            || self.costs.observation(candidate.cost_key()).and_then(|(_, observed, _)| observed).is_none()
    }

The observation tuple destructuring is roundabout for "no measured nanos". Add ActionCosts::is_measured(Key) -> bool next to is_fresh (cost.rs:867-869) and say it once.

5. Documentation duplication (AGENTS.md / repo-prose). docs/scheduling-theory.md:562-573 restates, in 13 lines, the interpolated-discovery rule that the same PR added to docs/scheduling-runtime-design.md:492-511, and the paragraph itself already cross-links there ("match the Helios interpolated prefill-discovery rule in [the scheduling runtime design]"). Replace it with two sentences ("initial_discovery = "interpolated" preserves native discovery when age_price == 0 or no decoding stream is resident; otherwise unmeasured pure-prefill capacities at or below natural fit are priced by sibling or peer interpolation, as the scheduling runtime design specifies") and keep the algorithm in one place. The doc comment on preserves_first_output_wait (policy.rs:462-478) has also grown to 25 lines and carries a justification parenthetical that reads as change history ("we intentionally do not require prefill < step.token_capacity because…") which the skill asks to be stated as an invariant, not as a rationale for a past choice.

One structural note that supports (1): PrefillSlotPlan (policy.rs:89-96) carries two independent memo-ready flags and an Option<usize> nat_fit that is computed from the first kind seen for that slot but reused across Prefill/TerminalPrefill/ScoredPrefill because candidates() shares one prefill_plans array across kinds (policy.rs:1537, :1569-1574). Deleting the nat_fit memo (or keying it on kind) removes both the extra fields and the key hazard; it is not worth a hand-rolled per-slot memo for a value two iter().find() passes can produce.

Verdict: no reject-level convention violation; suggest items 1–3 as the substantive cuts and 4–5 as cleanup.

#2 [high] S&S review #661 — overall verdict and framing

PR #661 @ 3d868f84 (NVIDIA/hyperflare-inference), lens: Subtraction, Simplicity & Conventions.

Verdict: no reject-level convention violation; diff is leaner than the average change of this size. Credit where due: the change deletes the 60-line duplicated prefill branch out of add_candidate into a dedicated add_prefill_candidate (policy.rs diff, add_candidate -> add_prefill_candidate at policy.rs:1422-1458), caches the previously recomputed terminal_prefill_window (scheduler.rs:847-866 + field :662, init :757, callers :808/:843), adds no CLI flag, no PolicyConfig field, no unsafe, no ABI change.

Substantive cuts: (1) delete last_interp memo + the 9-arg fn it forces; (2) collapse three near-identical catalog scans; (3) dedupe the age-price terminal dispatch between scheduler.rs:1013-1021 and service.rs:297-301, and stop calling has_active_age_priced_stream() per request. Cleanup: (4) prefill_catalog_for_terminal wrapper + is_unmeasured_candidate tuple destructuring; (5) docs duplication theory vs runtime-design, and the 25-line doc comment on preserves_first_output_wait.

Supporting structural note for (1): PrefillSlotPlan (policy.rs:89-96) carries two memo-ready flags and a nat_fit memo computed from the first kind seen for a slot (policy.rs:526-530) but reused across Prefill/TerminalPrefill/ScoredPrefill because candidates() shares one prefill_plans array across kinds (policy.rs:1537, loop :1538-1548, use :1569-1574).

#3 [high] S&S #661 item 1 — last_interp memo is dead weight

helios/src/scheduler/policy.rs:517-543 (verbatim):

#[allow(clippy::too_many_arguments)]
fn prefill_candidate_ns(
    &self, plan: &mut PrefillSlotPlan, request: &LiveRequest, kind: GraphKind, capacity: usize,
    work: usize, age_price: f64, predictor: Option<cost::PrefillPredictor>,
    last_interp: &mut Option<(usize, u64, Option<u64>)>,
) -> Option<u64> {
    ...
    if age_price > 0.0 && plan.nat_fit.is_some_and(|nat| capacity <= nat) {
        let interp = match *last_interp {
            Some((w, pos, res)) if w == work && pos == request.scheduled_consumed => res,
            _ => {
                let res = self.costs.interpolated_prefill_duration(kind, capacity, work, request.scheduled_consumed);
                *last_interp = Some((work, request.scheduled_consumed, res));
                res
            }
        };
        if let Some(ns) = interp { return Some(ns); }
    }

Memo declared per capacity: policy.rs:1429 (let mut last_interp: Option<(usize, u64, Option<u64>)> = None;) inside add_prefill_candidate, and policy.rs:1092 in continuation_candidate. So it hits only when two slots in the same mask have identical (work, scheduled_consumed).

Fix: replace the block with if let Some(ns) = self.costs.interpolated_prefill_duration(kind, capacity, work, request.scheduled_consumed) { return Some(ns); }; drop last_interp from prefill_candidate_ns, add_prefill_candidate (:1429/:1437) and continuation_candidate (:1092/:1117). If cost matters, memoize inside ActionCosts where the O(entries) scan lives (cost.rs:716 + loop to :763).