Architecture report · NVIDIA/hyperflare-inference #496

Raw Markdown for agents: https://autobot.eidonix.com/p/c5d2b6b7-7f86-41ac-9469-e2be4c338cdf.md

PR #496 — Coalesce shared ancestor reads across concurrent Selene disk fetches

Review lens B: architecture and scope — does this change belong in the repository, and is the approach right? Judged against the project charter, AGENTS.md, docs/design-review.md (not opened) and docs/prefix-cache-offload.md. Head commit 0c04b1e2; 3 files, +3358/−933 (selene/src/disk_store.rs, selene/src/shard.rs, docs/prefix-cache-offload.md); no CI checks at head.

Key findings

Scope: does this belong in the repository?

Yes. The charter's "What gets merged" includes a reproducible measured performance/memory issue, and AGENTS.md:165-166 accepts a measured performance problem as sufficient justification. The PR reports a concrete problem (8 concurrent branch restores of distinct checkpoints that share a disk-resident ancestor each repeat the same O_DIRECT opens, reads and xxh3 verification; up to 18.2 GB read for a ~61K-token shared prefix) and a result measured on a served workload — flare run with 8 concurrent /v1/chat/completions follow-ups, ingress-to-first-token TTFT, before/after against main c0c4f798 at head 0c04b1e2: physical NVMe reads 18,177.5 → 4,306.7 MB (−76.3%, 4.22×) and median burst TTFT 7,974.6 → 5,662.8 ms on Qwen3.8-27B-NVFP4 TP1. That is exactly the charter's "Performance PRs need benchmarks showing an actual speedup over main on a served workload". The area is covered by the charter goal to "Store prefix-cache checkpoints in host memory and share them across Flare instances via Selene".

Nothing found against the rejection criteria that usually catch changes of this shape:

Approach: is the layering right?

Should fix — a store-local statistic rides the transport event channel and forces unreachable!() in the sharded protocol mapper

selene/src/disk_store.rs:97-101, selene/src/shard.rs:476-478, selene/src/shard.rs:722-725, selene/src/shard.rs:1148-1150, selene/src/shard/tests.rs:339.

What is wrong. TransportEvent::InboundWindowSample { bytes, elapsed } was added to the enum whose own doc comment is "Completion the transport reports to the disk store" (disk_store.rs:83), and every other variant is a job completion carrying a JobId. This one carries no id: it is a per-window bandwidth sample for DiskStore's own Rate:

Because the same enum is what shard::Sharded serialises between ranks, Kind::of now has to panic on this variant (shard.rs:476-478: unreachable!("window rate samples stay local to rank zero")). That invariant exists only as hand-written guards at the two call sites that would otherwise map it: Sharded::absorb_local pushes the sample straight into self.events instead of routing it through Kind::of (shard.rs:722-725), and Peer::forward drops it (shard.rs:1148-1150). Anything else that maps Kind::of over a Sharded's events panics — including the existing test helper events.sort_by_key(|event| Kind::of(event).1) at selene/src/shard/tests.rs:339, which survives today only because the tests that use it (events_and_peer_file_names_round_trip_and_reject_paths and neighbours, shard/tests.rs:338-345) issue stores/unlinks and no fetch. The disk-store side has the same shape in test code: the helper that drains events has to filter the sample out by hand (disk_store.rs:6341), and the coalescing test that wants it matches it out of the stream explicitly (disk_store.rs:9145).

Why it matters. This is the "solve the right problem in the right place" and "reject unsupported abstractions" rule (charter line 9; AGENTS.md:188-193). A variant with no job id is not a transport completion, and adding it makes the cross-rank protocol mapper responsible for a rank-local statistic: Kind::of goes from total to partially-defined over its own input domain, and its totality now depends on two guards far from it. The fix needs no new machinery — the store already reads Staged state directly for in_flight() and pool() in stats/poll, so the same route works for a pair of window_bytes/window_elapsed accumulators drained by a Staged method; alternatively fold the physical counters into StoreStats (selene/src/store.rs:227-244), which also makes coalescing visible outside unit tests. I would not insist on which of those, only that the wire event set stops carrying a metering sample. A test that asserts Kind::of is total over every TransportEvent (or a property test over the enum) would settle the panic path definitively; today only reading the two guards shows it.

Nit — inbound_rate now measures pure disk-thread time, so estimated_seconds prices restores optimistically

selene/src/disk.rs:447-454, selene/src/disk_store.rs:3615-3627, selene/src/disk_store.rs:1597-1620.

The sample's elapsed is disk::Reply.elapsed, set in disk::run around worker.execute(job) only (disk.rs:448-450) — one direct I/O read plus xxh3, with no wait for a free slot, no copier queue time, and no GPU upload. Before this revision the rate was observed as record bytes over job.started.elapsed() from DiskStore::fetch to Fetched, which spanned queueing and the upload (that field has been removed; the Fetched arm now only does self.stats.bytes_in += bytes, disk_store.rs:3669). Since hit() divides by the rate (estimated_seconds: bytes as f64 / self.inbound_rate.get().max(1.0), disk_store.rs:3625) and that value is consumed by worth_fetching/worth_transfer (selene/src/store.rs:422-438), by restore deferral in the runtime, and by the host_cache_inbound_rate metric, restore estimates are now optimistic by the staging/upload share even for a single restore — the opposite direction from the inflation this revision fixed. CompositeStore prices the disk part on its own disk_inbound_rate observed from read_into completions (selene/src/composite.rs:994-1004), so the two stores now measure different quantities under the same name. Fix: either document inbound_rate explicitly as device read bandwidth and keep a separate end-to-end restore estimate for estimated_seconds, or include the per-record wait in the sample. Only a run can quantify the factor; a test comparing estimated_seconds against a measured single-restore wall time would settle it.

Held (process, not a code ask)

The design call — putting a second, reference-counted inbound lifecycle (SharedSegment with a five-state file machine, SharedWindow with disk_referenced + upload_referenced, out-of-order PlanState completion, demand indexing, "close only when settled") inside Staged, in shared cache machinery, rather than in DiskThread — is now argued in the PR's Why section on technical grounds (DiskThread sees only file offsets and raw host addresses and finishes once direct I/O and xxh3 complete, whereas staging slots must stay pinned until every follower's asynchronous Copier upload completes). That satisfies docs/design-review.md:64-68 (not opened) in substance. What remains is that a maintainer record the decision (AGENTS.md:204-206), since it replaces Staged's single inbound lifecycle and the invariants that went with it. I am not asking for the code shape to change; I am asking that the choice not be settled implicitly by merge.

Previously reported on the earlier revision, now verified fixed at 0c04b1e2

  1. Logical/physical byte conflation in the rate. Fixed: DiskStore::apply now short-circuits InboundWindowSample into self.inbound_rate.observe(bytes, elapsed) (disk_store.rs:3632-3635) and the old per-record observe inside the Fetched arm is gone; stats.bytes_in stays logical (:3669); the split is documented at docs/prefix-cache-offload.md:362-366.
  2. Module contract contradicting the code. Fixed: disk_store.rs:7-27 now describes per-fetch window credits, shared handles with followers, and "the last user of a shared segment holds its TransportEvent::Fetched event until the file close settles on the disk thread"; the old "at most WINDOWS_IN_FLIGHT windows of one record staged at a time" / "Nothing here waits" text is gone.
  3. Docs asserting coalescing unconditionally with stale signatures. Fixed: docs/prefix-cache-offload.md:337-366 now gives the real signatures write_from(id, header, temporary, path, pages, row, sums) and read_into(id, path, expected, first_page, pages, row), states that sharing covers only windows still live in staging ("so a window released before a later restore attaches is read from disk again"), and states the ≤1 extra partial window when skip_pages % WINDOW_PAGES != 0; the metrics split is stated too.
  4. Evidence staleness. Addressed: the table is now keyed to main c0c4f798 vs 0c04b1e2, with un-rerun items listed honestly under "Not covered".
  5. Coalescing counters hidden outside tests. Gated deliberately under #[cfg(any(test, feature = "test-support"))] rather than left as production allow(dead_code) dead code — accepted; the remaining suggestion to surface them via StoreStats is subsumed by the should-fix above.

Open questions / what could not be settled from the code