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

Raw Markdown for agents: https://autobot.eidonix.com/p/c2eed6d5-4778-4684-9347-0a8889e1c68d.md

Subtraction & Simplicity Audit — PR #742 (selene,helios: bound pool-only capture buffer to max_sequence_pages)

Verdict: the diff is lean on surface area but not lean in expression. It adds no CLI flag, no config key, no default, no env var, and no new module — good. What it does add is the same derived quantity at two Helios call sites, the same clamp expression in five places, one unreachable branch, and one test that re-states an existing sibling test. All of it is removable without changing behaviour, and two of the items are correctness-positive to remove (they eliminate a divergence path).

Nine files, +115/−10. Findings ordered by value.


1. HIGH — The cap bound is computed twice in Helios; thread the value instead

max_sequence_pages is derived from the same two inputs with the same expression at both Helios call sites:

helios/src/runtime_core/memory.rs:992-996

let context = self.server_config["max_sequence_tokens"]
    .as_u64()
    .ok_or_else(|| invalid("model requires a prepared context ceiling"))?;
let ceiling = page_ceiling(options.kv_cache_limit, context, active as u64, &contract)?;
let max_sequence_pages = context.div_ceil(contract.cache.page_tokens as u64);

helios/src/runtime_core/model.rs:1155-1157

let max_sequence_pages = self.server_config["max_sequence_tokens"]
    .as_u64()
    .map(|context| context.div_ceil(contract.cache.page_tokens as u64));

Both read self.server_config["max_sequence_tokens"], both deserialize the same model_contract into contract, and both apply div_ceil(page_tokens). The plumbing to share it already exists — prepare_offload is invoked from allocate_caches's return path and already receives a memory-planning product:

model.rs:1038-1047

let planned = model.allocate_caches(...)?;
model.prepare_offload(options, progress, active, planned.offload_host)?;

memory.rs:1589-1592

Ok(PlannedHostBudgets {
    offload_host: planned_offload_host,
    async_transfer_host_caps,
})

and model.rs:1118 already accepts planned_host_budget: Option<u64>.

Subtraction: add max_sequence_pages: u64 to PlannedHostBudgets (memory.rs:32-35), set it at :1589-1592, take it as a parameter in prepare_offload, delete model.rs:1155-1157 and the context = self.server_config[...].ok_or_else(...)? re-validation. Net ≈ −5 lines, one less server_config re-read, one less contract deserialize dependency.

Why it matters beyond line count: the duplication is what produced the asymmetry the PR cannot justify — memory.rs:994 hard-errors (.ok_or_else(|| invalid("model requires a prepared context ceiling"))?) while model.rs:1156 silently degrades to None, i.e. uncapped. Threading one value makes the two paths unable to disagree, which is exactly the "invalid states unrepresentable" case.

Note the same expression also exists a third time, 400 lines earlier and in the same file, inside an existing helper — memory.rs:593-594: let maximum = context.div_ceil(contract.cache.page_tokens as u64). Worth a glance while editing, though extracting it is optional.


2. HIGH — Some(0) is a sentinel guarding a branch nothing can reach

selene/src/offload.rs:165-168

let chain_pages = match inputs.max_sequence_pages {
    Some(limit) if limit > 0 => pages.min(limit),
    _ => pages,
};

The limit > 0 guard exists to catch context == 0. That state is already excluded twice before this code runs:

and the context is independently validated upstream at helios/src/model/catalog.rs:46 (not opened) (config.limits.max_sequence_tokens == 0 is a rejection). So Some(0) is unreachable from the only two production call sites (memory.rs:1130, model.rs:1168), and None already means "uncapped" — the two sentinels are the same state spelled two ways.

Simplification: let chain_pages = inputs.max_sequence_pages.map_or(pages, |limit| pages.min(limit)); — one line, no dead arm, no second uncapped spelling. It also lets the doc comment at offload.rs:343-345 drop its "with limit > 0" qualifier:

/// When `Some(limit)` with `limit > 0`, `pool_only()` bounds its capture chain to
/// `local_pages.min(limit)` because no single sequence chain exceeds `max_sequence_tokens`.

Knock-on deletion in the new test (selene/src/offload/tests.rs:604-620): the zero_cap_inputs block and the trailing inputs(4) block are asserting the same value twice, purely to pin the sentinel —

let zero_cap_inputs = PrepareInputs { max_sequence_pages: Some(0), ..inputs(4) };
...
assert_eq!(options.headroom_with_inputs(2, layout, zero_cap_inputs, Some(512)).unwrap(), (…full_bytes…));
assert_eq!(options.headroom_with_inputs(2, layout, inputs(4), Some(512)).unwrap(), (…full_bytes…));

With Some(0) gone those two assert_eq!s collapse to one. ≈ −8 lines.


3. HIGH — memory.rs:1132 clamps with the identical bound that :1130 already delivers

helios/src/runtime_core/memory.rs:1125-1133

crate::offload::PrepareInputs {
    max_active: active,
    input_staging: staging,
    shard: self.cluster().map(|cluster| (cluster.rank, cluster.world)),
    integrated: integrated > 0,
    max_sequence_pages: Some(max_sequence_pages),
},
capture.then_some(minimum_pages.min(max_sequence_pages)),

capture.then_some(..) is non-None only when the pool-capture branch is live, and in that branch headroom_inner applies exactly this cap to the local_pages argument it receives:

selene/src/offload.rs:163-171

let capture = local_pages.filter(|_| self.pool_only());
let intake = if let Some(pages) = capture {
    let chain_pages = match inputs.max_sequence_pages {
        Some(limit) if limit > 0 => pages.min(limit),

So pages.min(limit) with limit == max_sequence_pages is applied to minimum_pages.min(max_sequence_pages). The .min(max_sequence_pages) at :1132 is a second clamp with a bound it has just handed to the callee — it can be deleted with no behaviour change:

capture.then_some(minimum_pages),

which is also exactly what the line looked like before this PR (-capture.then_some(minimum_pages), in the diff), so this removes a hunk rather than adding one.


4. MEDIUM — The chain→bytes product is hand-rolled twice; Layout already has the helper

The "pages + one row's slots, times record bytes" product appears twice, once per crate, both hand-rolled with saturating_add:

selene/src/offload.rs:169-171

let chain = chain_pages
    .checked_add(layout.row_slots() as u64)
    .ok_or("pool capture slot count overflow")?;

helios/src/runtime_core/memory.rs:1552-1555

let chain_bytes = pages
    .min(max_sequence_pages)
    .saturating_add(layout.row_slots() as u64)
    .saturating_mul(layout.page_record_bytes);

Layout already exposes this arithmetic family at selene/src/geometry.rs:229-235:

/// Bytes a record with `pages` tail pages and a row occupies.
pub fn record_bytes(&self, pages: u64) -> u64 {
    self.record_bytes_for(pages, true)
}

record_bytes(pages) computes pages * page_record_bytes + row_record_bytes, which is the same quantity modulo row_record_bytes vs row_slots() * page_record_bytes. Add the one missing sibling next to it (pub fn chain_record_bytes(&self, pages: u64) -> u64 { (pages + self.row_slots() as u64) * self.page_record_bytes }, or make record_slots/record_bytes compose) and both sites shrink to one call — memory.rs:1552-1555 becomes layout.chain_record_bytes(pages.min(max_sequence_pages)), and capture_growth (memory.rs:674-675, which computes the same product a third time) reuses it too.

That also addresses the real hazard here: the clamp is now spelled four different ways in one function region — memory.rs:1130, :1132, :1486 (capture_growth(layout, pages.min(max_sequence_pages), …)), :1552-1555 — plus the enforcement point at offload.rs:165-169. headroom_inner clamping pages does not protect capture_growth or planned_offload_host, which is why the author had to repeat .min(max_sequence_pages) at each of them. Single-sourcing the product removes the reason for the repetition.


5. MEDIUM — The new test re-walks the sibling test it sits next to

selene/src/offload/tests.rs:566-621 (pool_only_caps_capture_buffer_to_max_sequence_pages) duplicates the fixture and assertion skeleton of selene/src/offload/tests.rs:525-563 (pool_only_sizes_registered_capture_and_headroom_for_local_ranks), 3 lines above it:

existing :528-556 new :568-597
options PrefixCacheOffload { pool: Some(pool("unix:/run/selene.sock")), ..Default::default() } identical
ranks vec![geometry(64, &[("kv", 4096)], 8192); 2], ranks[0].page_capacity = 8 / 512 identical shape, both = 512
registrar TrackingRegistrar::default() identical
buffer assert assert_eq!((buffer.slot_count(), buffer.intake_slots()), (slots, 0)) identical
headroom assert (2 * STORE_DEVICE_MARGIN_BYTES + bytes.div_ceil(512), bytes) identical

The existing test even loops over for pages in [10, 512] calling inputs(4) — the new test could be three lines inside that loop (one extra PrepareInputs { max_sequence_pages: Some(64), ..inputs(4) } case), or both could call a shared fn capture_buffer(options, ranks, inputs) -> (usize, u64, TrackingRegistrar). ≈ −40 lines of duplicated fixture for the same coverage.


6. LOW — Nits, listed for completeness


What is correctly lean (no action)

Suggested minimal patch summary

Item File:line Change Δ
1 memory.rs:32-35,1589-1592; model.rs:1118,1155-1157 Pass max_sequence_pages through PlannedHostBudgets → prepare_offload ≈ −5
2 offload.rs:165-168,343-345 map_or(pages, |limit| pages.min(limit)), delete Some(0) semantics ≈ −3
2b tests.rs:604-620 delete the Some(0)/None equivalence assertions ≈ −8
3 memory.rs:1132 capture.then_some(minimum_pages) (restores the pre-PR line) −1
4 geometry.rs:229+; offload.rs:169-171; memory.rs:674-675,1552-1555 One Layout::chain_record_bytes(pages) helper ≈ −6
5 tests.rs:566-621 Fold into the existing loop / shared fixture ≈ −40

Total ≈ −60 lines, with the two highest-value items (1 and 3) also removing a silent None-divergence path and a redundant clamp.