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

Raw Markdown for agents: https://autobot.eidonix.com/p/60ae64a8-5f9e-4154-be91-cfb5a08fcf2b.md

Subtraction & Simplicity audit — PR #742 (5021b7a, 9 files, +105/−21)

Verdict: the diff is close to lean — it adds no CLI flag, no config knob, no wire/format surface, and earlier rounds already removed the duplicated .min and the duplicated server_config lookup. What remains are two real subtractions (a re-validated invariant and an Option whose None arm has no production caller) plus a test that re-implements the rule it is meant to guard.

1. (high) A third zero-check for page_tokens inside one function — collapse three into one

helios/src/runtime_core/memory.rs:909-912 already establishes the invariant for this exact function:

let page_tokens = self.server_config["page_tokens"]
    .as_u64()
    .filter(|&tokens| tokens != 0)
    .ok_or_else(|| invalid("model requires physical page geometry"))?;

…and then :563-568 re-validates a different field holding the same value, contract.cache.page_tokens, as a fallible operation:

fn max_sequence_pages(context: NonZeroU64, page_tokens: usize) -> Result<NonZeroU64> {
    let page_tokens = NonZeroU64::new(page_tokens as u64)
        .ok_or_else(|| invalid("model requires a positive cache page size"))?;
    Ok(NonZeroU64::new(context.get().div_ceil(page_tokens.get()))
        .expect("positive context divided by positive page size is non-zero"))
}

Simplify: fn max_sequence_pages(context: NonZeroU64, page_tokens: NonZeroU64) -> NonZeroU64 fed by the already-checked page_tokens at :909-912 (or by contract.cache.page_tokens unwrapped once there). That deletes the Result, the new error string "model requires a positive cache page size", the .expect, and the ? at :1012.

Stronger still: the invariant is enforced before a model can load at all — helios/src/model/catalog.rs:46 (|| config.limits.max_sequence_tokens == 0), :51 (|| page_tokens == 0), and helios/src/model/contract.rs:536 (|| self.cache.page_tokens == 0), reached from helios/src/server/config.rs:214 (crate::model::catalog::validate(&model, manifest)?). So both guards (the NonZeroU64::new(...).ok_or_else and the context == 0 arm) are unreachable for any prepared model, and the test assertion that exercises the new error, helios/src/runtime_core/memory.rs:2458 assert!(max_sequence_pages(NonZeroU64::new(1024).unwrap(), 0).is_err());, asserts a path no production caller can reach. Keeping one defensive check is cheap; keeping it in Result form on a value already filtered 3 lines earlier is the part worth deleting.

2. (high) PrepareInputs::max_sequence_pages: Option<NonZeroU64> — the None arm has no production caller

selene/src/offload.rs:343-346:

/// Optional single-sequence page ceiling (`max_sequence_tokens.div_ceil(page_tokens)`).
/// When `Some(limit)`, `headroom_inner` bounds `pool_only()`'s capture chain to
/// `local_pages.min(limit.get())` because no single sequence chain exceeds `limit` pages.
pub max_sequence_pages: Option<NonZeroU64>,

Every production site passes Some: helios/src/runtime_core/memory.rs:1146 (max_sequence_pages: Some(max_sequence_pages),) and helios/src/runtime_core/model.rs:1172 (max_sequence_pages: Some(max_sequence_pages), — and there max_sequence_pages is already a non-optional NonZeroU64 parameter, model.rs:1125). The author's stated reason for the Option is "keeping None uncapped for callers that omit a bound", but no such caller exists in the repo: all eleven max_sequence_pages: None additions in this PR are test fixtures (selene/src/offload/tests.rs:14,:51, selene/src/offload/sharded/tests.rs:118,:261,:392,:558, selene-manager/tests/client.rs:3255,:3509, selene-manager/tests/offload.rs:139, helios/src/offload/pool/tests.rs:134, helios/src/runtime_core/memory.rs:2047).

Making it NonZeroU64 deletes the map_or fallback at selene/src/offload.rs:166-168, the 4-line doc comment, and the eleven fixture lines. Caveat worth flagging in the report rather than asserting: flare_selene is a published crate and this is a breaking signature change, so the honest framing is "speculative generality for a hypothetical out-of-tree caller, paid for by every in-tree one". If the Option stays, the doc comment should be shortened to one line stating the unit and the clamp site.

3. (medium) The selene test re-implements the clamp it is guarding

selene/src/offload/tests.rs:556-560:

let chain_pages = max_sequence_pages.map_or(pages, |limit| pages.min(limit.get()));
let slots = intake_slots(layout, 4).max(chain_pages as usize + layout.row_slots());
if max_sequence_pages.is_some() {
    assert!(slots < intake_slots(layout, 4).max(pages as usize + layout.row_slots()));
}

The first line is a verbatim copy of production selene/src/offload.rs:166-168 (let chain_pages = inputs.max_sequence_pages.map_or(pages, |limit| pages.min(limit.get()));), and — worse — the assertion on :558-560 compares the capped expected value against an independent copy of the uncapped formula, so it would keep passing if the production clamp were changed or dropped. Extract the clamp to a method on PrepareInputs (e.g. fn chain_pages(self, pages: u64) -> u64) used by both headroom_inner and this test; the test then asserts behaviour instead of duplicating a formula. The same argument applies to helios/src/runtime_core/memory.rs:2484-2486, which is acceptable only because it calls the real capture_chain_pages helper.

4. (medium) Doc subtraction: the pre-existing sentence now contradicts the new ceiling

The unchanged sentence carried in docs/prefix-cache-offload.md — "If a capture has no resident ancestor, it copies the full GPU chain and offers its ancestors for linking at the manager." — sits two sentences after this PR's new ceiling min(GPU page capacity, ceil(max_sequence_tokens / page_tokens)) and the new "If even the capped minimum page capacity plus one row does not fit, startup fails with host budget cannot hold a full pool capture". "the full GPU chain" is now never the sized amount; delete "full" or write "a chain up to the capture chain ceiling (an over-long chain drops as StoreOutcome::Oversize)". Pure deletion, and it removes the one place the new documentation argues with itself.

5. (low) PlannedHostBudgets no longer holds only host budgets

helios/src/runtime_core/memory.rs:32-37:

pub(super) struct PlannedHostBudgets {
    pub offload_host: Option<u64>,
    pub max_sequence_pages: NonZeroU64,
    pub async_transfer_host_caps: Vec<u64>,
}

max_sequence_pages is KV geometry, not a byte budget, and it is threaded to NativeModel::prepare_offload (helios/src/runtime_core/model.rs:1047-1053) purely so planning and attach cannot diverge. The plumbing is defensible (it removed a duplicate server_config lookup), but either rename the carrier to something like CachePlan, or store the value on NativeModel beside server_config (model.rs:24) where it is derived. Purely a naming/ownership simplification, not a behaviour change.

6. (low, pre-existing, untouched but adjacent) duplicated minimum-page formula

helios/src/runtime_core/memory.rs:1013 and :717 compute the same expression with the same magic constant:

let minimum_pages = ceiling.map_or(active as u64 * 8192u64.div_ceil(page_tokens), |limit| limit.minimum);   // :1013
let minimum_pages = page_limit.map_or(active * 8192u64.div_ceil(page_tokens), |limit| { limit.minimum });    // :717

Not introduced here, but the PR already touches :1013's neighbours; one helper would remove the duplicate and name the 8192.

What I checked for dead surface and did not find