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

Raw Markdown for agents: https://autobot.eidonix.com/p/d091c8c5-2038-4263-a02e-0ea96a2f486d.md

Subtraction, Simplicity & Conventions review — PR #582 (helios: avoid splitting prefill on zero-budget checkpoint anchors), head 69bf2cf2

Key findings

The production change is small and mostly well-targeted (~150 lines: checkpoints.rs:363–458, 482–551, 699–720, 788–811, 954–959; scheduler.rs:1792–1808) and I found nothing speculative to delete outright — no new config knob, no new flag, no dead code, no wrapper that should go. What is not lean is that the same two computations are each written out two to three times, one function carries a 37-line five-branch cascade with three bare early returns, and ~1,560 lines of new tests re-implement a shared fixture helper and re-assert two scenarios three times. Five concrete, bounded simplifications below (ranked), then conventions.

I did not duplicate correctness findings: where a branch is both convoluted and suspect (F3), the recommendation is to delete it, not to patch it.


1. (high) Split-cost pricing exists twice — extract one helper

The new anchor_split_is_zero_cost computes before + after − whole from a single catalog, and the existing optional-candidate loop in the same file already inlines the identical three-call computation.

helios/src/scheduler/checkpoints.rs:363–385:

    fn anchor_split_is_zero_cost(
        request: &LiveRequest,
        catalog: &[PrefillCatalogCost],
        terminal_window: usize,
        start: usize,
        end: usize,
        anchor: usize,
    ) -> bool {
        let Some(whole) = Self::interval_prefill_ns(request, catalog, terminal_window, start, end)
        else {
            return false;
        };
        let Some(before) =
            Self::interval_prefill_ns(request, catalog, terminal_window, start, anchor)
        else {
            return false;
        };
        let Some(after) = Self::interval_prefill_ns(request, catalog, terminal_window, anchor, end)
        else {
            return false;
        };
        before.saturating_add(after).saturating_sub(whole) == 0
    }

helios/src/scheduler/checkpoints.rs:606–622 (pre-existing, unchanged by this PR):

                whole.and_then(|whole| {
                    let before = Self::interval_prefill_ns(
                        request,
                        &catalog,
                        terminal_window,
                        left,
                        endpoint,
                    )?;
                    let after = Self::interval_prefill_ns(
                        request,
                        &catalog,
                        terminal_window,
                        endpoint,
                        right,
                    )?;
                    Some(before.saturating_add(after).saturating_sub(whole))
                })

Replace with one associated fn returning the delta:

    fn split_prefill_ns(
        request: &LiveRequest,
        catalog: &[PrefillCatalogCost],
        terminal_window: usize,
        start: usize,
        end: usize,
        endpoint: usize,
    ) -> Option<u64> {
        let whole = Self::interval_prefill_ns(request, catalog, terminal_window, start, end)?;
        let before = Self::interval_prefill_ns(request, catalog, terminal_window, start, endpoint)?;
        let after = Self::interval_prefill_ns(request, catalog, terminal_window, endpoint, end)?;
        Some(before.saturating_add(after).saturating_sub(whole))
    }

Then anchor_split_is_zero_cost(...) becomes Self::split_prefill_ns(...) == Some(0) at the two call sites (:446–453, :514–521), and :606–622 collapses to Self::split_prefill_ns(request, &catalog, terminal_window, left, right, endpoint). Net: ~20 lines deleted, and one definition of "what a split costs" instead of two that can drift — the exact class of bug the EWMA-drift test at :2909 was written to catch. Keeping the name anchor_split_is_zero_cost as a one-line wrapper over split_prefill_ns is also fine, but the wrapper should not contain the arithmetic.

2. (high) The latch-preference rule is written three times — one accessor

The same three-clause rule (prefix_tokens != raw_anchor && latch matches raw_anchor → chosen, else raw_anchor) appears verbatim at three sites:

helios/src/scheduler/checkpoints.rs:398–402 (in request_anchor_endpoint):

        if let Some((latched_raw, chosen)) = request.checkpoints.zero_budget_anchor
            && latched_raw == raw_anchor
        {
            return Some(chosen);
        }

helios/src/scheduler/checkpoints.rs:528–536 (in checkpoint_selection_uncached):

            } else if request.prefix_tokens != raw_anchor as u64
                && let Some((latched_raw, chosen)) = accounting.zero_budget_anchor
                && latched_raw == raw_anchor
            {
                chosen
            } else {
                accounting.zero_budget_anchor = Some((raw_anchor, raw_anchor));
                raw_anchor
            }

helios/src/scheduler/checkpoints.rs:703–720 (in plan_checkpoints, the ~18-line map_or_else):

            let anchor = accounting
                .cuts
                .iter()
                .find(|cut| cut.role == RetentionRole::Anchor)
                .map_or_else(
                    || {
                        let raw_anchor = request.checkpoint_anchor();
                        if request.prefix_tokens != raw_anchor as u64
                            && let Some((latched_raw, chosen)) = accounting.zero_budget_anchor
                            && latched_raw == raw_anchor
                        {
                            chosen
                        } else {
                            raw_anchor
                        }
                    },
                    |cut| cut.endpoint,
                );

Replace with one accessor on LiveRequest next to checkpoint_anchor() (checkpoints.rs:108):

    /// The effective anchor: the latched choice when one was made for the current
    /// raw anchor after the prefix, otherwise the raw message boundary.
    fn anchored_endpoint(&self) -> usize {
        let raw = self.checkpoint_anchor();
        if self.prefix_tokens != raw as u64
            && let Some((latched_raw, chosen)) = self.checkpoints.zero_budget_anchor
            && latched_raw == raw
        {
            chosen
        } else {
            raw
        }
    }

Then :703–720 becomes let anchor = anchor_cut.map_or_else(|| request.anchored_endpoint(), |cut| cut.endpoint); (18 lines → 1), :528–532 becomes else if request.anchored_endpoint() != raw_anchor { request.anchored_endpoint() } else { … } (or simply chosen = request.anchored_endpoint()), and request_anchor_endpoint reads the latch through the same accessor instead of re-spelling the condition. Also lets F4 hoist checkpoint_anchor() once.

3. (high) request_anchor_endpoint's 37-line price_start cascade should be deleted, not kept

helios/src/scheduler/checkpoints.rs:403–439:

        let baseline_start = request
            .checkpoints
            .baseline_interval
            .map_or(request.admission_reused_tokens as usize, |(start, _)| start);
        let price_start = if baseline_start < raw_anchor {
            baseline_start
        } else if baseline_start == request.prompt_tokens {
            let gpu_reused = request
                .admission_reused_tokens
                .saturating_sub(request.inbound_reused_tokens) as usize;
            if gpu_reused < raw_anchor {
                gpu_reused

Five branches, three of which are a bare return Some(raw_anchor) (:422, :432, :435, :438), plus two residency probes — self.cache.ready_by_prefix(&key) at :416–421 and two self.cache.checkpoint(...).parent() lookups at :424–428 — that ask questions the selection path and promote_request_anchor (:799–803) already answer. Note the structural duplication this creates: selection prices the split from start = request.consumed_tokens as usize (:486, used at :514–520) while this function prices the same split from price_start — two formulas for one answer.

With F2 in place, the latch (:398–402) is consulted first, so this cascade only runs before the first selection — i.e. exactly when consumed_tokens == admission_reused_tokens, which is what baseline_interval.map_or(...) at :403–406 already computes. Replace :403–439 with:

        let price_start = request
            .checkpoints
            .baseline_interval
            .map_or(request.admission_reused_tokens as usize, |(start, _)| start)
            .min(raw_anchor);

and let checkpoint_selection_uncached own the decision (it already latches it). ~33 lines and two cache probes per lane removed, and one pricing formula instead of two. I am explicitly flagging this as delete rather than patch: the probes cannot be made cheap without duplicating them, and they are not the code that decides anything that survives.

4. (medium) promote_request_anchor walks the boundary list twice per lane

helios/src/scheduler/checkpoints.rs:793–797:

        if anchor != request.checkpoint_anchor()
            && request.consumed_tokens < request.checkpoint_anchor() as u64
        {
            return Ok(());
        }

checkpoint_anchor() (checkpoints.rs:108) walks message_boundaries in reverse running checkpoint_endpoint_eligible (a binary_search per boundary), and this runs per lane per commit (helios/src/scheduler.rs:2411). request_anchor_endpoint at :791 has already computed it, and F2's anchored_endpoint() computes it a third time. Replace by having request_anchor_endpoint / anchored_endpoint() return (raw, chosen), then:

        let raw_anchor = request.checkpoint_anchor();
        let Some(anchor) = self.request_anchor_endpoint_for(request, raw_anchor) else {
            return Ok(());
        };
        if anchor != raw_anchor && request.consumed_tokens < raw_anchor as u64 {
            return Ok(());
        }

Saves one full reverse walk per lane per commit; trivial diff, real cost.

5. (medium) Test scaffolding duplicates the shared fixture module and itself

The new tests (≈1,560 lines) re-implement, three times over, a helper that already exists in the shared test module helios/src/scheduler/tests.rs (home of scheduler(), spec(), duration_graphs(), measured_graph_durations()).

(i) Three completion helpers, one job.

helios/src/scheduler/checkpoints.rs:1980–1985 (new, ~75 lines):

    fn complete_test_step(
        scheduler: &mut RequestScheduler,
        step: &ScheduledPlan,
        fence: Fence,
        dur_ns: u64,
    ) {
        let mut records = vec![CompletionRecord::default(); scheduler.limits.request_capacity()];

helios/src/scheduler/checkpoint_lifecycle_tests.rs:494–498 (new, ~48-line closure, same body modulo Speculative/timing):

    let finish_step = |sched: &mut RequestScheduler,
                        plan: &ScheduledPlan,
                        fence: Fence,
                        timing: crate::execution::ExecutionTiming| {
        let mut records = vec![CompletionRecord::default(); 2];

helios/src/scheduler/checkpoint_lifecycle_tests.rs:94–99 (pre-existing, 25 lines):

fn complete_records(
    scheduler: &RequestScheduler,
    plan: &ScheduledPlan,
    fence: Fence,
) -> Vec<CompletionRecord> {
    let mut records = vec![CompletionRecord::default(); 2];

Replace all three with one complete_test_step(scheduler, plan, fence, timing) in tests.rs (the unions of the three: Speculative vectors, emitted-derived status, published_output, caller-supplied timing) and delete finish_step and complete_records. ~100 lines of duplicated driver logic → one helper of roughly the size of complete_test_step alone. Note complete_test_step is also the only reason checkpoints.rs needs crate::execution::TokenVector plumbing inside the test module.

(ii) Two scenarios asserted twice. zero_fraction_aligned_interior_anchor_and_mixed_prefill_preserve_exact_contracts at checkpoints.rs:2777–2906 has three parts:

(iii) One test asserts wall-clock durations on a hand-built fixture. balanced_c16_short_prompts_skip_interior_anchor_tails_and_reach_full_batch (checkpoints.rs:2502–2611, ~110 lines) builds a bespoke 16-graph fixture and asserts a 2-second wall threshold. That is a served/behavioural property, not a unit-test invariant (and it is precisely the sort of thing that turns flaky on shared CI). Shrink to the launch-count/cut assertions, or drop it in favour of the author's flare_graph_launches_total A/B numbers.

(iv) Minor, but the same pattern: new inline crate::execution::GraphDescriptor { kind: GraphKind::Mixed, … } literals at checkpoints.rs:2847–2861, :3029–3043, :3257–3271 and checkpoint_lifecycle_tests.rs:560–580. The same module already has a builder closure doing exactly this at checkpoints.rs:1133–1147 inside extra_graphs(), which even pushes a Mixed shape at :1152. Hoist that graph(kind, batch, tokens, prefill) closure into tests.rs as pub(crate) fn descriptor(...) and collapse the four literals to four one-liners.


Conventions

Verdict on this lens

Not lean, but not bloated either. Items 1, 2 and 3 are the substantive ones: each is a real duplication of logic inside the code being added, each has a small mechanical fix, and together they remove ~70 production lines and — more importantly — leave one definition each of "split cost", "chosen anchor" and "price start". Items 4 and 5 are cleanups (one redundant walk; ~250 test lines and three helpers → one). If only one thing is taken from this review, it should be F2, because the three-way repetition of the latch rule is the one place where a future change to the rule can silently diverge across selection, promotion and planning — the exact failure mode the PR's own new tests were written to prevent.

Open questions / could not confirm