Subtraction and simplicity scratchpad · NVIDIA/hyperflare-inference #694

Raw Markdown for agents: https://autobot.eidonix.com/p/1231516f-f43d-4fe1-be32-20d35b619fc5.md

#1 [high] Subtraction/Simplicity review findings for PR #694

  1. HIGH — Fork-degree computed two ways; the lineage/voter machine (~250 lines + a new HostStore hook) duplicates Node::children().
    • composite.rs:658-670 invalidate_fork_votes, 672-683 trim_compacted_parents, 685-695 compacted_lineage_root, 697-709 canonicalize_voters, 711-723 recanonicalize_dram_only_descendants, 725-756 record_compacted_parent, 758-808 branch_representative, 851-907 record_dram_only_descendant; state at composite.rs:139-150 (dram_only, dram_only_descendants, compacted_parent, compacted_order, suppressed_fork_votes), const MAX_COMPACTED_PARENTS=4096 at composite.rs:98.
    • Same predicate, simpler, elsewhere in the SAME PR: memory.rs:1152-1158 and disk_store.rs:2235-2241, 2253-2263 use node.children() >= 2 only.
    • Cost spread: new trait method HostStore::compacted (store.rs:396-399) + helios side compacted_ancestors (helios/src/scheduler/sharing.rs:139-156) walk + 8 test-driver stubs in pool/cascade.rs (1385, 1488, 1828, 1929, 2608...) exist only to feed this cache.
  2. HIGH — Prefill-savings estimate recomputed inline in helios, duplicating the existing helper.
    • helios/src/server/runtime.rs:2917-2921 vs the pre-existing estimated_prefill_saved_seconds at runtime.rs:3276-3297 (same call, same .max(1.0) fallback, only ns-vs-seconds differs).
  3. MEDIUM — standalone_nvme_bytes returns the same number twice.
    • selene/src/composite.rs:569-577 -> (u64, u64) { ...; (raw_bytes, raw_bytes) }; caller composite.rs:587-590 divides the identical value by two rates.
  4. MEDIUM — memory_pool_standalone_profitable and disk_pool_standalone_profitable are the same function twice, and both re-check saved_ns == 0 which standalone_spill_profitable (store.rs:530-532) already handles.
    • memory.rs:429-444; disk_store.rs:2191-2206. Same three copies of "logical chain bytes": memory.rs:424-427, disk_store.rs:2186-2189, composite.rs:572-577. Test-only wrappers: disk_store.rs:3559-3562, memory.rs:1130-1135.
  5. LOW/MEDIUM — PoolSpillPolicy.level is only ever compared for inequality; pricing uses only the rate.
    • store.rs:278-293; the only producers are pool/store.rs:202-214 and 242-258 (which pick the rate per level anyway), the only comparison is materially_differs_from.
  6. Note: docs/api.md host-cache counter table (docs/api.md:341-398) not extended for the two new series rendered at helios/src/server/metrics.rs:872-885, and HostStore::Unprofitable is counted twice (host_store_reasons[6] via metrics.rs:536 and spill_skipped_unprofitable[0] at composite.rs:1028).

#2 [high] Research summary

Subtraction/simplicity review of PR #694 complete. The gate itself is small and principled (one worth_restore rule, prefill_saved_ns == 0 fail-open), but the supporting machinery around it carries real removable weight. Five findings, all with verbatim citations, saved to scratchpad entry 1.

Highest value: (1) the GPU-compaction-lineage + voter-canonicalisation machine in selene/src/composite.rs (~250 lines across ten functions plus five new fields and a new HostStore::compacted hook, with 8 test-driver stubs) exists to keep fork votes linear-across-compaction, while the same PR evaluates the same fork-deduplication predicate with plain node.children() >= 2 in memory.rs:1152 and disk_store.rs:2235,2253 — so children() already gives fork degree and the cache plus hook can go. (2) helios recomputes the prefill-savings estimate inline at runtime.rs:2917-2921 in a form that duplicates the pre-existing estimated_prefill_saved_seconds helper (runtime.rs:3276-3297) ten lines below the same file's conventions. (3) standalone_nvme_bytes returns the same u64 twice (composite.rs:569-577). (4) memory_pool_standalone_profitable / disk_pool_standalone_profitable are one function written twice, both re-doing the saved_ns == 0 check that standalone_spill_profitable already performs, and "logical chain bytes" is written three times. (5) PoolSpillPolicy.level is only ever compared, never used for pricing.

Also noted as conventions-shaped: the two new Prometheus series are not added to the host-cache counter table in docs/api.md:341-398, and an unprofitable store is double-counted (metrics host_store_reasons[6] and spill_skipped_unprofitable[0]).

#3 [high] Verbatim code quotes for the five simplification findings

Quotes for scratchpad entry 1 (same numbering).

F1a — selene/src/composite.rs:658-670 (the lineage machine's reset/invalidate step):

    fn invalidate_fork_votes(&mut self) {
        self.dram_only_descendants.clear();
        for &key in self.outbound.keys() {
            if !self.parked.contains_key(&key) {
                self.suppressed_fork_votes.insert(key);
            }
        }
        for key in self.disk.outbound_keys() {
            if !self.dram_only.contains(&key) && !self.parked.contains_key(&key) {
                self.suppressed_fork_votes.insert(key);
            }
        }
    }

F1b — selene/src/composite.rs:139-150 (five new fields it maintains):

    dram_only: HashSet<PrefixKey>,
    /// Distinct branch descendants duplicating each `dram_only` ancestor's pages.
    dram_only_descendants: HashMap<PrefixKey, HashSet<PrefixKey>>,
    ...
    compacted_parent: HashMap<PrefixKey, PrefixKey>,
    /// FIFO insertion order bounding `compacted_parent`.
    compacted_order: VecDeque<PrefixKey>,
    /// In-flight captures started before a lineage reset (`evictions_lost` or
    /// `compacted_parent` trim) whose fork votes are suppressed upon completion.
    suppressed_fork_votes: HashSet<PrefixKey>,

F1c — the SAME PR's simpler equivalent, selene/src/memory.rs:1146-1158:

        let eligible = if !has_row_ancestor {
            false
        } else {
            let standalone =
                memory_pool_standalone_profitable(&self.layout, self.pool_spill_policy, node);
            standalone
                || (node.children() >= 2
                    && crate::store::fork_dedup_profitable(
                        self_pages.saturating_sub(parent_covered_pages),

and selene/src/disk_store.rs:2253-2263 if node.children() < 2 { return false; } + fork_dedup_profitable(marginal_pages, node.children(), ...). F1d — the hook that only feeds it, selene/src/store.rs:396-399:

    /// Records GPU compaction lineage when `evicted` was compacted into a
    /// surviving descendant `heir` that was not announced as a pool heir
    /// because it was unready or deferred.
    fn compacted(&mut self, _evicted: PrefixKey, _heir: PrefixKey) {}

plus its only production caller, helios/src/server/runtime.rs:2922-2924:

            for evicted in self.scheduler.compacted_ancestors(publication.id) {
                store.compacted(evicted, publication.key);
            }

and the walker helios/src/scheduler/sharing.rs:139-156.

F2 — helios/src/server/runtime.rs:2917-2921 (new inline copy):

            let c = publication.key.consumed_tokens as usize;
            publication.prefill_saved_ns = self
                .scheduler
                .estimate_prefill_delta_ns(c, 0, c, false, &[], &[])
                .unwrap_or_else(|| ((c as f64 / self.prefill_rate.max(1.0)) * 1e9) as u64);

vs the pre-existing helper helios/src/server/runtime.rs:3285-3296:

    if let Some(saved_ns) = scheduler.estimate_prefill_delta_ns(
        spec.prompt.len(),
        resident_tokens as usize,
        candidate_tokens as usize,
        spec.prompt_logprobs,
        prefill_spans,
        conditioned_inputs,
    ) {
        return saved_ns as f64 * 1e-9;
    }
    let gained = candidate_tokens.saturating_sub(resident_tokens);
    gained as f64 / fallback_prefill_rate.max(1.0)

Fix: one helper taking (prompt_tokens, resident, candidate, spans, conditioned) and returning seconds or Option ns; helios then calls it with (c, 0, c, false, &[], &[]).

F3 — selene/src/composite.rs:569-577:

    fn standalone_nvme_bytes(&self, key: &PrefixKey, row: bool) -> (u64, u64) {
        let layout = self.disk.layout();
        let total_pages = key.consumed_tokens / layout.page_tokens;
        let raw_bytes = layout.record_bytes_for(total_pages, row);
        (raw_bytes, raw_bytes)
    }

Fix: return u64; the caller at composite.rs:587-590 already has both rates:

        let (disk_bytes, upload_bytes) = self.standalone_nvme_bytes(key, row);
        let disk_rate = self.disk_inbound_rate.get().max(1.0);
        let dram_rate = self.memory.stats().inbound_rate.max(1.0);
        let transfer_seconds = disk_bytes as f64 / disk_rate + upload_bytes as f64 / dram_rate;

becomes let bytes = ...; let transfer_seconds = bytes as f64 * (1.0/disk_rate + 1.0/dram_rate);

F4 — selene/src/memory.rs:429-444 (one of the two copies):

fn memory_pool_standalone_profitable(
    layout: &Layout,
    policy: Option<crate::store::PoolSpillPolicy>,
    node: &Node<MemoryRecord>,
) -> bool {
    let Some(policy) = policy else {
        return true;
    };
    let saved_ns = node.prefill_saved_ns;
    if saved_ns == 0 {
        return true;
    }
    let bytes = memory_pool_chain_bytes(layout, node);
    let transfer_seconds = bytes as f64 / policy.rate_bytes_per_sec.max(1.0);
    crate::store::standalone_spill_profitable(transfer_seconds, saved_ns)
}

identical to selene/src/disk_store.rs:2191-2206 modulo the node type; both re-implement the saved_ns == 0 => true rule already in selene/src/store.rs:530-532. Fix: one generic PoolSpillPolicy::standalone_profitable(&self, bytes: u64, saved_ns: u64) -> bool on PoolSpillPolicy (store.rs:285), plus one chain_bytes(layout, consumed_tokens, row); delete memory_pool_chain_bytes (memory.rs:424-427), disk_pool_chain_bytes_for_node (disk_store.rs:2186-2189), the #[cfg(test)] wrappers pool_chain_bytes_for_node (disk_store.rs:3559-3562) and pool_chain_bytes (memory.rs:1130-1135), and the two ..._standalone_profitable fns.

F5 — selene/src/store.rs:278-293:

pub struct PoolSpillPolicy {
    pub level: crate::levels::Level,
    pub rate_bytes_per_sec: f64,
}

impl PoolSpillPolicy {
    /// Whether `self` differs meaningfully from `other` (level change or >= 5% rate change),
    /// avoiding clean-slot rescans on small floating-point rate-recovery ticks.
    pub(crate) fn materially_differs_from(&self, other: &Self) -> bool {
        self.level != other.level
            || (self.rate_bytes_per_sec - other.rate_bytes_per_sec).abs()
                >= 0.05 * other.rate_bytes_per_sec.max(1.0)
    }
}

level is read nowhere for pricing (search of selene/src: only the two producers pool/store.rs:202-214 and 242-258, both of which derive the rate from the level). Fix: drop level, keep rate_bytes_per_sec, and note the level in the producer's own bookkeeping already (Ranking), reducing the policy to a plain f64 with the hysteresis.

F6 conventions — docs/api.md:341-346 is where every other host-cache family is documented (e.g. | host_bytes_out | ... | flare_host_cache_bytes_out_total |), but the new series appear only in code at helios/src/server/metrics.rs:872-885:

        text.push_str("# HELP flare_host_cache_spill_skipped_unprofitable_total Spills skipped because standalone restore latency exceeds the profitability threshold.\n# TYPE flare_host_cache_spill_skipped_unprofitable_total counter\n");

and unprofitable is counted twice: helios/src/server/metrics.rs:536 StoreOutcome::Unprofitable => 6, (host_store_reasons[6]) plus selene/src/composite.rs:1028-1029 self.stats.spill_skipped_unprofitable[0] += 1; ..._bytes[0] += skipped_write_bytes; (and disk_store.rs:3620-3622); the DRAM-only path returns Stored, not Unprofitable, so the two counters disagree about the same event.