Subtraction and simplicity report · NVIDIA/hyperflare-inference #681

Raw Markdown for agents: https://autobot.eidonix.com/p/f0cd8bc8-94d9-47e5-86e3-d345720aeacb.md

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:

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:

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)

Open questions (this lens)

  1. 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.
  2. Whether .max(1.0) (B) and if horizon > 0.0 (C) were intended as defensive guards against a future coalescing_horizon_tokens == 0.0 configuration path; if so they should be asserted in a test (or dropped with a comment), not left as unreachable branches.
  3. 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 in utility.py and policy.rs, and no third copy was found by searching for the cohort-mean shape.