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

Raw Markdown for agents: https://autobot.eidonix.com/p/699a9b5d-a26a-4b95-bfd5-be496590d7f8.md

Subtraction, Simplicity & Conventions review — PR #681 helios/scheduler: use active cohort mean in coalescing horizon

Key findings

The production change is already lean, and I want to say that plainly before the nits. helios/src/scheduler/policy.rs is +16/−18, net −2 lines (pr_files), and the net direction is deletion: peer_budget and its .min() chain are gone (policy.rs:530-531, :548-553, :567), the dead if horizon > 0.0 wrapper is gone (:669-679, only per_token.is_finite() && per_token > 0.0 survives at :676), and the numeric core is a four-line local helper reused three times (mean_capped, policy.rs:599-601; used :630, :639, :671). No flag, config key, wrapper, trait, extension point or fallback branch is introduced anywhere in the diff — worth recording because AGENTS.md:188-193 forbids exactly those: "Do not add frameworks, extension points, speculative compatibility, fallback layers, or configuration switches without a concrete requirement." The PR passes that bar.

What is left is five small asymmetries, ordered by value. Findings 1–2 are in the production/parity path and are the ones I would block a "simplify" round on; findings 3–5 are test and doc shape.

# Where What Effort
1 scripts/scheduling_sim/utility.py:236 Unreachable if horizon > 0: left behind after the same guard was deleted in Rust delete + dedent
2 scripts/scheduling_sim/utility.py:215-219, :235 Cohort mean spelled inline twice; Python never got the mean_capped helper Rust added one helper, two call sites
3 helios/src/scheduler/policy_tests.rs:7023, :6908/:6921-6922/:6950-6951 Unused let _d1 binding; same surplus expression typed five times 2-line edit, one closure
4 policy_tests.rs:6971-7018; tests/test_scheduling_utility.py:529-593 Three ~24-line GraphDescriptor literals where the repo uses a builder closure; four-way duplicated Python run/assert block ~50 → ~25 lines each
5 docs/scheduling-runtime-design.md:224-229 One 9-line sentence, five nested clauses, trailing clause restates the previous one formula block, as the same doc already does at :214-218

1. Dead guard left in the Python twin (highest-value subtraction)

The author deleted an unreachable horizon > 0.0 guard in Rust — visible in the diff at policy.rs:667-679 — but the structurally identical guard in the simulator survived. scripts/scheduling_sim/utility.py:232-244:

                if best_surplus_per_horizon > 0:
                    for view in prefillers:
                        ph = min(self.config.coalescing_horizon_tokens, float(view.output_budget - 1))
                        horizon = sum(min(float(v.output_budget), ph) for v in decoders) / k
                        if horizon > 0:
                            initial_prefill = max(

if horizon > 0: at utility.py:236 cannot be false:

Do: delete the if horizon > 0: line and dedent the initial_prefill = max(...) / raw_coalesce[view.name] = (...) block four columns, matching the Rust shape at policy.rs:669-679. Keeping the two implementations structurally identical is the point of the simulator-parity rule (docs/design-review.md:195-206 (not opened)).

2. Python lacks the helper Rust has, and computes the same mean twice

Rust collapsed the cohort mean into one helper, helios/src/scheduler/policy.rs:599-601:

        fn mean_capped(rems: &[usize], cap: f64) -> f64 {
            rems.iter().map(|&rem| (rem as f64).min(cap)).sum::<f64>() / rems.len() as f64
        }

The Python twin still spells the same mean out twice, with different argument shapes. scripts/scheduling_sim/utility.py:211-219:

                    def best_decode_rate(n_streams: int) -> float:
                        best = 0.0
                        for graph in decode_graphs:
                            served = min(n_streams, graph.decode_capacity)
                            mean_out = sum(
                                min(v.output_budget, expected_output(v.acceptance, graph.width))
                                for v in decoders
                            ) / k
                            best = max(best, served * mean_out / graph.duration_ms)

and again at utility.py:235:

                        horizon = sum(min(float(v.output_budget), ph) for v in decoders) / k

This is precisely the duplication the review's Subtract/Simplify item 1 removed on the Rust side. Do: add one module-level helper next to COALESCING_HORIZON_TOKENS (utility.py:12), keep the / k — which is the same thing, and is cheaper and clearer than rebuilding the list:

def mean_capped(total: float, k: int, cap: float) -> float:
    return total / k

is too thin to be a helper; instead add:

def mean_capped(budgets, cap: float) -> float:
    budgets = list(budgets)
    return sum(min(float(b), cap) for b in budgets) / len(budgets)

and call it at both sites: mean_out = mean_capped((v.output_budget for v in decoders), expected_output(v.acceptance, graph.width)) at :215-219, horizon = mean_capped((v.output_budget for v in decoders), ph) at :235. That makes a future divergence between the Rust and Python formula much harder to write, which is the maintainability argument, not a line-count argument (:215-219 and :235 collapse by one line each).

3. Unused binding and five copies of one expression in the new tests

helios/src/scheduler/policy_tests.rs:7022-7024:

        let d0 = add_active_decoder(&mut s, 1, 64, 1);
        let _d1 = add_active_decoder(&mut s, 2, 64, 100);
        let p0 = add(&mut s, 3, 512, 128);

_d1 is never read; the call exists only for cohort membership. Do: drop the binding — add_active_decoder(&mut s, 2, 64, 100);.

Same file, test coalescing_horizon_averages_active_decoder_budgets_across_cohort (:6887-6963) writes the same surplus expression five times: :6908, :6921-6922, :6950-6951 (and the speculative test repeats it at :7043-7047, :7071-7075):

    let surplus = PolicyConfig::default().output_token_value * 3.0 * (1.0 - base_rate / next_rate);
    let expected_mixed = (surplus * 32.5) / 512.0;

Do: one local closure near the setup closure at :6889, e.g. let bonus = |k: f64, base: f64, next: f64, horizon: f64| PolicyConfig::default().output_token_value * k * (1.0 - base / next) * horizon / 512.0;, then let expected_mixed = bonus(3.0, base_rate, next_rate, 32.5);. Not a correctness issue — the five copies are independently readable — but it is the difference between editing one constant and five when output_token_value or the 512.0 prompt-token baseline changes.

Note :6933-6937 re-reads slots[0] after construction to apply the KV clamp; that is acceptable and I would not change it (the setup closure returns after the prefill is added, so post-hoc mutation is the cheaper route).

4. Two test bodies that ignore builders the repo already uses

Rust. policy_tests.rs:6971-6998 contains three near-identical ~24-line crate::execution::GraphDescriptor literals that differ only in name, handle, draft_tokens, token_capacity, packed_token_capacity and estimated_duration_ns:

        graphs.push(crate::execution::GraphDescriptor {
            tuned_bindings: vec![],
            name: "speculative-b2-d7-w8-p16".to_string(),
            kind: GraphKind::Speculative,
            sampling_mode: SamplingMode::General,
            confidence_width: 0,
            draft_tokens: 7,
            handle: 1,
            batch_capacity: 2,
            token_capacity: 8,
            packed_token_capacity: 16,
            prefill_capacity: 0,
            estimated_duration_ns: 20 * MS,

(then :6986-7000 and :7003-7017 repeat it). The module's own convention is a builder closure, helios/src/scheduler/cost.rs:907-921:

        let make = |name: &str, drafts, width, packed| GraphDescriptor {
            name: name.into(),
            kind: GraphKind::Speculative,
            sampling_mode: SamplingMode::General,
            confidence_width: drafts,
            draft_tokens: drafts,
            handle: 1,
            batch_capacity: 16,
            token_capacity: width,
            packed_token_capacity: packed,
            prefill_capacity: 0,
            estimated_duration_ns: 0,
            input_bindings: vec![],
            tuned_bindings: vec![],
        };

(a second instance at cost.rs:992, a third at :1097). Do: inside make_spec_scheduler (policy_tests.rs:6967), define let spec = |handle, name: String, drafts, batch, toks, packed, ms| GraphDescriptor { .. } with the invariant fields hard-coded, and reduce the three pushes (~50 lines) to ~25. This is a convention the repo already established for exactly this case, so it is not "unsolicited refactoring" under AGENTS.md:190-193 — it is matching the module.

Python. tests/test_scheduling_utility.py:529-593 is the same four-way duplication: two request lists and two run(...) pairs plus eight metric assertions that differ only in acceptance, max_new_tokens, prefill count and three ceilings. tests/test_scheduling_utility.py:557-562:

    assert dec_coal.summary["elapsed_ms"] < dec_off.summary["elapsed_ms"]
    assert dec_coal.summary["elapsed_ms"] <= 7740.0
    assert sum(dec_coal_ttfts) / len(dec_coal_ttfts) < sum(dec_off_ttfts) / len(dec_off_ttfts)
    assert max(dec_coal_ttfts) < max(dec_off_ttfts) and max(dec_coal_ttfts) <= 330.0
    assert dec_coal.summary["max_output_gap_ms"] < dec_off.summary["max_output_gap_ms"]
    assert dec_coal.summary["max_output_gap_ms"] <= 190.0

against the speculative twin at :588-593 with ceilings 1885 / 440 / 220. Do: one @pytest.mark.parametrize("acceptance,max_new,prefills,max_elapsed,max_ttft,max_gap", [...]) over those four values, body shared — ~45 lines to ~25. The file already uses parametrize heavily (:96-357), so this is the house pattern. Lower priority than the Rust side: the explicit duplication is somewhat more readable for a reviewer checking the ceilings, and only worth doing if the author touches the test again.

5. The spec sentence should be the formula the same document already uses

docs/scheduling-runtime-design.md:224-229 is a single sentence with five nested clauses whose last clause restates the previous one:

decode-batch throughput surplus over a bounded cohort-mean decode horizon
(`coalescing_horizon_tokens`, default `64.0`, averaged across active decoders
after clamping each decoder's remaining output budget to the prefiller's
post-prefill output budget and the configured horizon, with speculative
per-round expected output similarly clamped per decoder and averaged across the
cohort),

"averaged across active decoders after clamping … per decoder" and "similarly clamped per decoder and averaged across the cohort" say the same thing twice. The document already formats the adjacent utility gradient as a text block at :214-218 (prefill token: 1 + f/x, etc.). Do: keep the prose lead-in to "…prices the multi-step K -> K + m decode-batch throughput surplus over a bounded cohort-mean decode horizon:", then

horizon  = mean_i min(rem_i, min(coalescing_horizon_tokens, post_prefill))
mean_out = mean_i min(expected_at(width), rem_i)

and resume at "amortized uniformly across the prefiller's uncached prompt tokens". This is also the cheapest place to declare the Rust-only KV clamp in rem_i — see finding 6, which the correctness lens owns but whose remedy from this lens is deletion, not annotation.

6. Adjacent: where the diff adds documentation instead of removing a divergence

Not a subtraction finding on the diff's own lines, but it belongs in a subtraction report because the PR's response to a divergence is a comment rather than a deletion. policy_tests.rs:6929-6932 now says:

    // KV-headroom-clamped decoder (max_sequence_tokens - scheduled_consumed = 2 < max_new - generated = 100)
    // alongside two long decoders (rem = 100) gives cohort-mean horizon (2 + 64 + 64) / 3.
    // Note: KV-headroom clamping is Rust-specific (`scripts/scheduling_sim/simulator.py` tracks
    // `max_new_tokens - generated` only).

Rust computes rem = min(max_new_tokens - generated, max_sequence_tokens - scheduled_consumed) (policy.rs:548-549); the simulator's output_budget carries no such clamp (scripts/scheduling_sim/simulator.py:26). docs/design-review.md:195-206 (not opened) says not to diverge Helios from the simulator's qualified policy without validating the rule across the catalog. The correctness lens owns whether the clamp is right; from =the subtraction lens the fix is to delete one of the two notions, not to add a third paragraph of prose (finding 5) and a fourth comment explaining them. Flagging so the author does not "resolve" the reviewer's parity concern by documentation alone.


Verified non-findings (checked so a reviewer does not have to)

Open questions

  1. Does the author intend the Python if horizon > 0: to stay? If it is deliberate defensive coding despite being unreachable, that is a legitimate (if inconsistent) choice given the identical guard was just removed in Rust — but the inconsistency itself is the finding.
  2. Is the mean-vs-min discriminator going to move to the Python side? Finding 2 makes the formula single-sourced; it does not make the new end-to-end test (tests/test_scheduling_utility.py:529-593) able to fail if the formula reverts to min_i, which is the correctness lens's evidence finding. The two fixes are independent and I do not claim finding 2 closes that gap.
  3. Would parametrizing the Python e2e test hurt readability of the hard-coded ceilings? I lean yes-it-slightly-hurts, which is why finding 4's Python half is ranked below its Rust half; the author's call.
  4. Whether the policy_tests.rs GraphDescriptor builder refactor is in scope: it touches lines this PR adds (:6971-7018) and matches cost.rs:907-921, so I read it as in-scope convention-matching rather than AGENTS.md:190-193 churn — but a reviewer who disagrees is not wrong.