Final review report · NVIDIA/hyperflare-inference #742

Raw Markdown for agents: https://autobot.eidonix.com/p/aebaf913-bc47-42f5-a0b5-147f2449c093.md

Recommendation: Changes Requested

The core pool-only capture-buffer bound in selene/src/offload.rs, helios/src/runtime_core/memory.rs, and helios/src/runtime_core/model.rs is sound: planning, binary-search growth charging, planned_offload_host, and final slab allocation agree on min(GPU page capacity, ceil(max_sequence_tokens / page_tokens)), and the docs and unit tests accurately match the implementation. One verified defect warrants a small fix before merge: the new context.div_ceil(contract.cache.page_tokens as u64) lines in memory.rs:996 and model.rs:1155-1157 are unguarded against contract.cache.page_tokens == 0 (turning a previously clean Err(invalid(...)) into an unconditional divide-by-zero panic under the default KvCacheLimit::Memory mode) and against context == 0 (which creates a 0-vs-uncapped mismatch with selene). Guarding those inputs and threading max_sequence_pages through PlannedHostBudgets resolves the defect cleanly.

Merge Risk

Level: Low

Walkthrough

Pre-merge Checks

Blockers

None.

Should Fix

  1. Unguarded div_ceil(contract.cache.page_tokens as u64) panics on zero page_tokens and mishandles context == 0 under default KvCacheLimit::Memory (helios/src/runtime_core/memory.rs:992-996, helios/src/runtime_core/model.rs:1155-1157)
    • Verified behavior: In allocate_caches (memory.rs:995-996), page_ceiling returns Ok(None) immediately when options.kv_cache_limit == KvCacheLimit::Memory (the #[default], memory.rs:567-569), bypassing its if context == 0 || contract.cache.page_tokens == 0 check at memory.rs:570. Because contract is deserialized from self.server_config["model_contract"] at memory.rs:899-901 without calling PreparedContract::validate(), context.div_ceil(contract.cache.page_tokens as u64) at memory.rs:996 now runs unconditionally on every load (even with offload disabled) and panics with division by zero when contract.cache.page_tokens == 0, whereas before this PR cache_geometry → CacheGeometry::validate() (selene/src/geometry.rs:114) returned a clean Err(invalid(...)). The same unguarded div_ceil appears at model.rs:1155-1157. In addition, if context == 0 under KvCacheLimit::Memory, memory.rs:996 computes max_sequence_pages = 0, which clamps memory.rs:1132,1486,1553 to 0 pages while selene/src/offload.rs:165-168 treats Some(0) as uncapped (pages), causing planning and preparation to disagree.
    • Fix: Validate both context != 0 and contract.cache.page_tokens != 0 in memory.rs:992-996, returning Err(invalid(...)) before calling div_ceil (matching memory.rs:570,696,897). Then either thread the validated max_sequence_pages into prepare_offload via PlannedHostBudgets (see Subtract / Simplify #1) or add the matching non-zero guard in model.rs:1155-1157.

Subtract / Simplify

  1. Thread max_sequence_pages through PlannedHostBudgets instead of recomputing it in model.rs (helios/src/runtime_core/memory.rs:32-35,1589-1592, helios/src/runtime_core/model.rs:1038-1047,1113-1168): allocate_caches already returns PlannedHostBudgets directly to prepare_offload. Passing max_sequence_pages there eliminates the duplicate server_config["max_sequence_tokens"] / div_ceil logic in model.rs:1155-1157 as well as the memory.rs (error on missing) vs model.rs (silent None/uncapped fallback) asymmetry.
  2. Remove the redundant clamp at helios/src/runtime_core/memory.rs:1132: headroom_with_inputs_for_budget receives max_sequence_pages: Some(max_sequence_pages) at :1130 and already applies pages.min(limit) inside headroom_inner (selene/src/offload.rs:165-168). Keeping capture.then_some(minimum_pages) restores the pre-PR line and leaves clamping in a single place.
  3. Avoid dual uncapped sentinels (None and Some(0)) if desired (selene/src/offload.rs:165-168,346): Once memory.rs:992-996 rejects context == 0, Helios never passes Some(0). Using Option<NonZeroU64> (or documenting/keeping only None as uncapped) removes the redundant limit > 0 branch and the duplicate Some(0) assertion in selene/src/offload/tests.rs:604-614.

Nits

Dropped on verification