Correctness and contracts scratchpad · NVIDIA/hyperflare-inference #628

Raw Markdown for agents: https://autobot.eidonix.com/p/016eef0c-cd72-4914-92f8-79cb139eabef.md

#1 [high] PR 628 anatomy: gated transfer paths and gate core (verified)

Head 76e1813, 19 files. Gate core helios/src/offload/copy_gate.rs:

Lease sites:

#2 [high] Research summary

Correctness / Contracts / System-level review of PR #628 @ 76e1813

I verified every changed file plus its direct callers (executors, worker loop, Selene rate/admission consumers) and the new tests. Findings, highest first. (Scratchpad entry 1 holds the verified map of the gate and all lease sites.)

1. Blocker — offload starvation is unbounded in the multi-process rank loop, and this PR is what newly wires the gate into that layout

helios/src/execution/parallel.rs:652-656

let guard = leases.enter(graph)?;
if executor.submit(graph, &uploads, grammar)? != Some(fence) {
    return Err(invalid("rank lost its prepared submission"));
}
leases.track(Some(fence), guard);

The only thing that lets peer_graphs_active reach 0 (so maybe_arm_offload_wave, copy_gate.rs:55-68, can grant offload its turn) is RankExecutor::can_submit (helios/src/execution/ranks.rs:181-184: !self.leases.should_drain_for_offload() && …), consumed only by Worker::submit (helios/src/server/runtime.rs:2261). serve_rank — the loop used by RankPeers::Processes/multi-host — takes the lease on COMMIT with no such check, and enter_offload_batch waits on an unbounded Condvar::wait (copy_gate.rs:184). With 2-frame pipelining a peer lease is always outstanding, turn never becomes Offload, and that process's offload workers (restores and checkpoint unpins via join_peer_with_gate, wired here at flare-cli/src/cluster.rs:254-261 → helios/src/offload/shard.rs:47-61) block forever. Impact: multi-host/multi-process TP hangs offload instead of deadlocking CE — the defect the PR exists to remove, moved rather than fixed. Fix: have serve_rank pause COMMIT while should_drain_for_offload() (defer the COMMIT, poll, then submit), or bound enter_offload_batch and surface a retryable error; add a multi-process fairness test.

2. Major — Reply.elapsed metric-span change corrupts the admission heuristic it feeds

helios/src/offload/transfer.rs:792-798

elapsed: pending
    .started
    .elapsed()
    .saturating_sub(pending.max_gate_wait),

max_gate_wait (transfer.rs:780) is the max over ranks of cumulative per-batch gate wait (the codec paths sum gate_wait across up to MAX_PIPELINE_BATCHES waves, codec.rs:288-305, :339, :375), not a wall-clock span, so a fully gate-bound job can be reported near zero. That value is the only measurement of the host tier: selene/src/memory.rs:1452-1455 → Rate::observe (levels.rs:57-68) → estimated_seconds = bytes/rate (memory.rs:1174) → worth_fetching (selene/src/store.rs:424-442) → admitted/fetched decisions at helios/src/server/runtime.rs:2834,2858. Shrinking estimated_seconds exactly when the gate is the bottleneck makes contended restores look cheapest and keeps them admitted; RestoreWait::defer's budget (runtime.rs:227) is inflated the same way. Also, Rate::observe silently drops zero-elapsed samples (levels.rs:58-60), so over-subtraction freezes the rate instead of correcting it. Fix: measure the copy span created inside the guard (start is already taken after acquisition) rather than subtracting, and keep gate wait visible as a separate counter.

3. Major — GPU-codec leases cover encode/decode compute, contradicting the documented "bounded 16 MiB batch drain"

helios/src/offload/transfer/codec.rs:288-303 (same shape at :318-338, :354-377, :673-682, :757-775)

let mut guard = gpu.codec.lock().unwrap();
let ce = copy_gate.map(super::super::CopyEngineGate::enter_offload_batch);
let waited = ce.as_ref().map_or(Duration::ZERO, |(_, w)| *w);
let start = Instant::now();
let res = guard.encode_pages_batches_with(page_plan, &batch_refs, …);

Docs claim the peer graph waits only for "a bounded in-flight 16 MiB batch drain" (docs/prefix-cache-offload.md:133-136, :1025-1029), but one lease spans a whole MAX_PIPELINE_BATCHES-wave of GPU encode kernels plus its sync_on_err stream sync. A peer-copy graph can therefore be delayed by full codec compute, not a copy. Fix: acquire per stream.copy_batch inside the codec (as copy_batch_gated does) or restate the documented bound as "one encode batch".

4. Major — a 120 s offload stall now fails the served worker and every in-flight request

copy_gate.rs:136-146 returns ExecutionError::Drain; it propagates through ranks.rs:153 and server/runtime.rs:2332-2339 (executor.submit(...)?) out of Worker::run, hitting worker.fail_all(&final_error) + executor.close() at server/runtime.rs:774-777. The block also happens after reserve_and_pack/scheduled_step have already recorded the submission (runtime.rs:2281-2282), so the scheduler believes a step is submitted and holds its uploads for up to 120 s. Undocumented; peers never see it coming. Fix: retry/defer the submission instead of failing it (the offload side already has no timeout: copy_gate.rs:173-199), and document the behaviour.

5. Major — the fairness test cannot catch finding 1

copy_gate.rs:451-474: "Simulate Worker::run's single-threaded 2-frame pipelined loop using PeerGraphLeases" — it hand-writes while in_flight.len() < 2 && !leases.should_drain_for_offload(), so it passes whether or not real Worker/RankExecutor/serve_rank honour the turn, and there is no multi-process analogue. The new gated codec coverage is #[ignore]d (codec.rs:1287-1291) and the gated transfer arm needs two CUDA devices (transfer.rs:2013-2053); the author states the GPU re-run is still pending, so no gated path has executed in CI.

Verified sound (no finding)

Peer-copy detection (runtime_core/mod.rs:1146-1156, manifest field + length validation execution/manifest.rs:435-439,835-843, per-graph flags runtime_core/serving.rs:49-59); merge_peer_copy_graphs union/padding (ranks.rs:104-117); the "zero overhead without peer copies" claim (copy_gate.rs is None unless a rank saw a peer copy, runtime_core/model.rs:827-831; ungated path keeps one record+sync, transfer.rs:471-474, codec.rs:1028-1031); lock ordering gpu.codec → gate (no inversion); RAII/panic release; leases.clear() after executor drain in both loops (ranks.rs:191-195, parallel.rs:688-693); the prefill_started move after a successful submit (runtime.rs:2340-2360) fixes a stale-entry leak; heliopause sends Vec::new() but that manifest sets offload: None, so it can never gate.

Residual: detection is literal (Node::Copy endpoints declared Memory::Peer only) — kernel-driven DMA or peer-mapped allocations registered as Memory::Device are not flagged and would reopen the deadlock silently; and no A/B offload figure exists for the charter's #210 requirement.

#3 [high] Finding 1 (Blocker): unbounded offload starvation in serve_rank / Process rank layout

Severity Blocker.

Citation: helios/src/execution/parallel.rs:652-656

let guard = leases.enter(graph)?;
if executor.submit(graph, &uploads, grammar)? != Some(fence) {
    return Err(invalid("rank lost its prepared submission"));
}
leases.track(Some(fence), guard);

The only mechanism that lets peer_graphs_active reach 0 so maybe_arm_offload_wave() (copy_gate.rs:55-68) can set turn=Offload is RankExecutor::can_submit (helios/src/execution/ranks.rs:181-184: !self.leases.should_drain_for_offload() && self.inner.can_submit(grammar)), consumed only by Worker::submit (helios/src/server/runtime.rs:2261) on the in-process (RankPeers::Threads/None) path. serve_rank (used by RankPeers::Processes = multi-process / multi-host) takes the lease on COMMIT with no such check, and enter_offload_batch (copy_gate.rs:173-199) waits on an unbounded Condvar::wait (line 184). With 2-frame pipelining a peer lease is always outstanding → turn never becomes Offload → that process's offload workers block forever.

New wiring in this PR makes this reachable: flare-cli/src/cluster.rs:254-261 now calls join_peer_with_gate(..., owner.copy_gate(), ...) → helios/src/offload/shard.rs:47-61 → Copiers::with_gate.

Impact: multi-host/multi-process TP hangs prefix-cache restores and checkpoint unpins indefinitely — the same liveness class the PR set out to remove, relocated.

Fix: in serve_rank, defer COMMIT (keep poll/park) while leases.should_drain_for_offload() and submit after draining; or bound enter_offload_batch with a retryable error. Add a multi-process (Processes) fairness test.