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

Raw Markdown for agents: https://autobot.eidonix.com/p/2dd0cd92-f26c-4346-9bf9-1ae5f2aacea1.md

#1 [high] BLOCKER: narrowing precedes lazy flare-kv-codec pool creation → pool halved and pinned

The deferral comment claims background threads keep the full mask, but one pool is built lazily after narrowing, on the narrowed thread.

Chain (all verified at head ba2796c0):

Impact on GB10: pool sized 10→5 threads, all with Cpus_allowed_list=5-9,15-19 (efficiency cores excluded), i.e. host KV compression loses half its parallelism and loses the cores the comment promises it keeps. The PR's own /proc evidence lists only flare-model, flare-roce, serve_rank — the codec pool was never inspected.

Fix: build/normalize the pool before narrowing (call flare_selene::shared::codec::cpu_pool() during start, before :724), or have cpu_pool() explicitly restore the process-wide allowed mask for its workers, or pass an explicit thread count; and cover flare-kv-codec-* in the /proc evidence.

#2 [high] MAJOR: pinned count can exceed 2-CPU floor; hot-spinning flare-roce shares the narrowed set

eos/src/unix.rs:211-225 — the fallback only guarantees two CPUs:

    if selected.len() < 2 {
        // Avoid collapsing multiple busy threads onto a single prime core on
        // three-tier topologies: fall back to all cores above the capacity midpoint.
        let sum = u64::from(min_cap) + u64::from(max_cap);
        selected = allowed_with_capacity.iter()
            .filter(|&&(_, cap)| u64::from(cap) * 2 > sum) ...
    }
    if selected.len() >= 2 && selected.len() < allowed_with_capacity.len() { Some(selected) } else { None }

C is identical (helios/native/roce_proxy.c:1555-1568 if (selected >= 2 && selected < count)).

Every narrowing caller puts a latency-critical thread onto that same set:

On GB10 the high set is 10 CPUs (fine). On an operator cpuset such as taskset -c 0-3 on a 1-prime + 3-efficiency asymmetric part, the fallback can select 2 CPUs for flare-model + up to 16 rank threads + a 100%-duty spinning proxy — strictly worse than the scheduler spreading them, and it can re-create the very host wake-gap latency the PR measures, this time from CPU oversubscription rather than core tier. Nothing checks selected.len() against the number of threads that will be pinned, and nothing logs.

Fix: require the selected set to be at least the number of threads that will be pinned (or a named minimum such as 4), no-op otherwise, and consider excluding one CPU for the proxy or not pinning the poller at all.

#3 [high] MAJOR: vendored roce_proxy.c edited in-place, contradicting 4 in-tree "identical" witnesses

helios/native/roce_proxy.c:6-8 states: // Vendored from FlashInfer's pinned preparation dependency // (flashinfer/experimental/b12x/comm/roce/_roce_proxy.c, proxy ABI 14). // Keep the source below identical so preparation and serving share the transport ABI. helios/native/roce_proxy.c:1572-1577 now adds inside that same "identical" region:

static void* proxy_main(void* arg) {
  roce_ctx_t* c = (roce_ctx_t*)arg;
#ifdef __linux__
  (void)pthread_setname_np(pthread_self(), "flare-roce");
  pin_high_capacity_cpus();
#endif

Witnesses left unamended (no doc file in the 8-file diff):

#4 [high] MAJOR: non-GB10 platforms are not no-ops; 90% threshold has no relative-spread guard

PR body Limitations say "On x86_64 Linux, /sys/devices/system/cpu/cpu/cpu_capacity is absent so the helper is a no-op." That is not reliable: the file exists whenever CONFIG_ACPI_CPPC_CPUFREQ/arch_topology provides it, including x86 hybrid parts (Alder Lake/Raptor Lake/Meteor Lake), and most server kernels. Two concrete consequences the PR does not address:

  1. On an x86 hybrid laptop/workstation, cpu_capacity is present with asymmetric values, so this PR would silently pin flare-model and the RoCE proxy on the first launch after merge with no benchmark, no evidence, and no flag — contrary to docs/prefix-cache-offload.md:927-928 "Adding CPU affinity requires a serving-latency measurement that shows a benefit." and the charter's opt-in/evidence rule (#210).
  2. Even where capacities are symmetric, the min_cap == max_cap early return (eos/src/unix.rs:201-203) only covers exact equality; GB10's own data (997..1024 within the performance tier) shows a 2.7% spread, so any x86 or ARM part whose whole allowed set has a small non-zero spread — e.g. all cores at 1023/1024 from CPPC rounding — will produce a "high" subset that is 1-2 CPUs out of 16+, and the code will then pin 6-8 hot threads there (selected.len() >= 2 && selected.len() < allowed_with_capacity.len(), eos/src/unix.rs:221-225). The 90% threshold (eos/src/unix.rs:208, u64::from(cap) * 10 >= u64::from(max_cap) * 9) filters only absolute spread, never relative — a 2% spread across a homogeneous part silently halves or worse the usable CPU set. Impact: a global behavioral default change on hardware nobody measured, and a filter that cannot distinguish "tiered" from "noisy homogeneous". Fix: gate on a minimum absolute spread (e.g. max_cap >= min_cap * 12 / 10, the ~40% gap real big.LITTLE parts show) and/or gate the whole feature behind an explicit env/flag as the docs require.

#12 [high] Research summary

I completed the correctness/contracts/system-level lens over all 8 changed files plus their callers, and recorded 11 scratchpad notes (9 findings, 1 verified-sound record, 1 ordering note).

Headline: one Blocker (the deferral at helios/src/server/runtime.rs:724 is incomplete — selene::shared::codec::cpu_pool() is a OnceLock built lazily on first use, and its first use is store.poll → drain_compressed → pump_spawns → host_compress_pool inside worker.run, i.e. on the already-narrowed flare-model thread; on GB10 the flare-kv-codec-* pool is built with 5 instead of 10 threads and every worker inherits 5-9,15-19, directly contradicting the comment at runtime.rs:721-723). Three Majors: the vendored roce_proxy.c was edited inside the region four in-tree witnesses call byte-identical (roce_proxy.c:8, NOTICE:16-19, roce_proxy_dlopen.c:7-9, docs/roce-ring.md:361-364, docs/TODO.txt:80-86) with no doc file in the diff; the "x86_64 is a no-op" limitation is not reliable (x86 hybrid parts do expose cpu_capacity, and the 90% threshold has no relative-spread guard — only exact equality is rejected at unix.rs:201-203, so a ~1% spread yields a 1-2 CPU "high" set that 6-8 hot threads are then pinned to); and the selector only guarantees ≥2 CPUs while flare-model, up to 16 flare-rank-N, and the 20M-spin flare-roce poller all share that same set. Minors cover observability (all six sites let _ = the syscall, no log/flag/metric, silent no-op on any unreadable cpu_capacity), the undocumented flare-roce rename plus the stale rule at docs/prefix-cache-offload.md:927-928, missing syscall-path/non-inheritance tests (only 3 pure-selector tests, C mirror untested, CI has no aarch64 execution), and the replay.rs:174 narrowing running before Stream::open in the opposite order to the documented design.

Areas I checked and found genuinely sound: no hot-path locks/allocation cost, Windows stub parity is total, no error-propagation change, no rate/latency/admission metric span shift, no ABI or core-format change (ABI 14 and existing cores unaffected), and offload thread priority (SCHED_BATCH/nice) untouched. I found no instruction-like or injected content.

#13 [high] Verified diff metadata + claim-vs-diff accuracy deltas (5 items, with line cites)

Verified against the diff at head ba2796c0 (pr_diff): exactly 8 files, +230 / −1, matching the dossier.

Claim-accuracy deltas I verified myself (PR text vs diff):

  1. Body: "no-op when cpu_capacity is absent, homogeneous, or already single-tier" + Limitations "On x86_64 Linux ... cpu_capacity is absent so the helper is a no-op" → not reliable; see note on the threshold (cpu_capacity is present on x86 hybrid parts, and only exact equality is treated as homogeneous, eos/src/unix.rs:201-203).
  2. Body: "Defer ... until after child-thread spawning so background threads retain the unnarrowed process affinity mask" → true for eager children (runtime.rs:585/684-707/719 all precede :724) but false for the lazily built flare-kv-codec-* pool (note 1).
  3. Body: evidence "inspecting /proc//task/*/status ... confirmed flare-model, flare-roce, and peer serve_rank threads had Cpus_allowed_list=5-9,15-19 while the ImagePreparation child thread ..., flare-http, tokio-rt-worker, and CUDA driver helper threads retained 0-19" → the list contains no flare-kv-codec-* entry, i.e. the one pool that is created after narrowing was not inspected; and it was measured on binary 4c82ab5b, not this head.
  4. Body: "no-op ... or already single-tier" / "falling back above midpoint to never collapse below 2 cores" → the fallback is accurate, but "never collapse below 2" is the only floor: selected.len() >= 2 (eos/src/unix.rs:221, roce_proxy.c:1566) with no relationship to how many threads get pinned.
  5. Body claims RoCE proxy rename as instrument; the rename is a user-visible comm change and no doc was updated (see the undocumented-changes note).

#5 [medium] MINOR/observability: silent unpinned outcome, no log, no flag, no metric

Six changed call sites narrow with let _ = sched_setaffinity(...) (eos/src/unix.rs:260) / (void)sched_setaffinity (helios/native/roce_proxy.c:1567) and no log, metric, or flag:

#6 [medium] TESTS: no test for the syscall path or for the non-inheritance property the design depends on

Only pure-function tests are added (eos/src/unix.rs:270-330: three select_high_capacity_cpus cases). Nothing tests the syscall path — prefer_high_capacity_cpus (eos/src/unix.rs:232-261) has no test, no injection point for a fake sysfs root (the path is hard-coded format!("/sys/devices/system/cpu/cpu{cpu}/cpu_capacity"), :243), and no test asserts that a narrowed thread spawns children with a wider mask. The C mirror pin_high_capacity_cpus (helios/native/roce_proxy.c:1509-1569) has no test at all and cannot be reached by the existing execution::roce::tests (those "exercise the ABI, preflight rejection, and GPU control-error interpretation without opening an RDMA device", docs/roce-ring.md:378-380 — roce_start is the only caller of proxy_main, helios/src/execution/roce.rs:300). CI cannot run either implementation meaningfully: .github/workflows/native-serving.yml:16-22 is ubuntu-24.04 x86_64 + windows-2022 only, and the aarch64 leg (release.yml) only runs --version/--help (per dossier A6). So the two implementations of the same selector are verified by 3 Rust unit tests and 0 C tests, and the property the PR actually relies on (non-inheritance by children) is asserted only by an author-supplied /proc observation on binary 4c82ab5b, not by the PR head. Fix: factor the path root into a parameter (env or cfg(test) override) and add a test over a temp sysfs tree, plus a test that a child thread spawned after narrowing sees a wider mask than its parent.

#7 [medium] MINOR/observability: silent outcome, no log, no flag, no metric

Six changed call sites narrow with let _ = sched_setaffinity(...) (eos/src/unix.rs:260) / (void)sched_setaffinity (helios/native/roce_proxy.c:1567) and no log, metric, or flag:

#8 [medium] MINOR: undocumented operator-visible changes (thread rename, affinity on proxy) + stale doc rule

Developer/operator-visible behavior change with no docs anywhere in the 8-file diff (pr_files: only eos/src/*.rs, helios/native/roce_proxy.c, helios/src/**.rs).

  1. pthread_setname_np(pthread_self(), "flare-roce") (helios/native/roce_proxy.c:1575) renames the proxy thread from the process/executable name to flare-roce. Anyone (or anything, e.g. the flare agent / Orbit monitor, top/htop, a pgrep flare wrapper, systemd accounting) that identifies proxy threads by comm, or that samples per-thread CPU by comm, now sees a different name. This is also the instrument the PR's placement evidence relies on, so the rename is load-bearing and should be documented (docs/roce-ring.md, docs/prefix-cache-offload.md:919-928 CPU bullet) rather than only appearing in the diff.
  2. The CPU bullet at docs/prefix-cache-offload.md:919-928 is now stale in a second way: it enumerates exactly how offload threads get priority (SCHED_BATCH + positive nice) and ends with "Adding CPU affinity requires a serving-latency measurement that shows a benefit." After this PR, one offload-adjacent thread (the RoCE proxy) does carry CPU affinity and no measurement for that path is supplied. Either supply the measurement the doc demands or amend the doc to scope the rule to the offload transfer/disk threads.
  3. What is not changed, verified: SCHED_BATCH/nice behaviour (helios/src/offload/transfer.rs:281 fn lower_priority, called at :531) and selene's disk-thread policy (selene/src/disk.rs:631) are untouched, so the "offload threads lower priority" contract still holds for the threads that keep the full mask.

#9 [medium] MINOR/observability: silent outcome, no log, no flag, no metric

Six changed call sites narrow with let _ = sched_setaffinity(...) (eos/src/unix.rs:260) / (void)sched_setaffinity (helios/native/roce_proxy.c:1567) and no log, metric, or flag:

#10 [medium] Areas verified SOUND (no finding): no hot-path cost, Windows stub parity, no metric-span shift, no ABI change

Verified NOT a problem, so no finding (state plainly in the report):

#11 [medium] MINOR: flare-cuda narrows before Stream::open, unlike the documented after-children ordering

helios/src/replay.rs:171-176:

        let thread = thread::Builder::new()
            .name("flare-cuda".into())
            .spawn(move || {
                flare_eos::prefer_high_capacity_cpus();
                run(receiver, completion_sender, ready_sender)
            })

and run's closure body begins helios/src/replay.rs:113-123 with let stream = match Stream::open(bundle.context, bundle.device) { ... }.

So unlike the flare-model path — where the comment at helios/src/server/runtime.rs:721-723 explicitly promises narrowing happens only after child threads are spawned — the flare-cuda thread narrows before it calls Stream::open, which is the point at which any CUDA-runtime-internal threads for that stream/context would be created. The PR body's non-inheritance evidence ("CUDA driver helper threads retained 0-19", measured on the updated binary) cannot cover this case, because in the flare-model path RankExecutor::new (runtime.rs:585) runs before the narrowing at :724 — a different ordering. Impact is bounded: Stream::open takes a pre-existing bundle.context (ReplayWorker::new / GraphBundle), so it is not creating the context; but if it does spawn per-stream driver threads, those now inherit the narrowed mask, and no evidence in the PR shows either way. Severity Minor — it is an evidence/ordering asymmetry, not a proven defect. Fix: move prefer_high_capacity_cpus() into run after Stream::open succeeds (i.e. after :123, before the command loop at :125), matching the runtime.rs ordering and the stated design rule.

#14 [medium] Sound: no UB/overflow/out-of-bounds in the new Rust and C selectors (cites checked)

Checked the unsafe / boundary surface of the new code; no UB, no overflow, no out-of-bounds — record so it is not re-derived.

  1. Bit-set bounds. Rust: let max_cpus = std::mem::size_of::<libc::cpu_set_t>() * 8; (eos/src/unix.rs:239) then libc::CPU_SET(cpu, &mut next) for cpu values drawn only from that same 0..max_cpus range that were tested with CPU_ISSET (:242). cpu_set_t is exactly that many bits, so set/test indices cannot exceed the array. C: int cpus[CPU_SETSIZE]; uint32_t caps[CPU_SETSIZE]; and for (int cpu = 0; cpu < CPU_SETSIZE; cpu++) (roce_proxy.c:1516-1521) — count can never exceed CPU_SETSIZE because it only increments for CPUs inside the same loop, so cpus[count]/caps[count] are in range. CPU_ZERO is called on both allowed and next before use (:1511, :1543).
  2. Arithmetic. Rust widens before multiplying: u64::from(cap) * 10 >= u64::from(max_cap) * 9 (eos/src/unix.rs:208) and u64::from(min_cap) + u64::from(max_cap) (:214); C casts the same way, (uint64_t)caps[i] * 10 >= (uint64_t)max_cap * 9 (roce_proxy.c:1550) and uint64_t sum = (uint64_t)min_cap + (uint64_t)max_cap (:1557), plus a val > UINT32_MAX guard before narrowing the fscanf unsigned long (:1539). No u32 overflow in either language.
  3. unsafe blocks. All are plain libc FFI with zeroed cpu_set_t (eos/src/unix.rs:233, 256) — layout-correct and fully initialised by zeroed(); the SAFETY contract of sched_getaffinity/sched_setaffinity (pointer + size of cpu_set_t) is satisfied. No raw pointer arithmetic in Rust.
  4. Behaviour on a mask larger than the kernel's nr_cpu_ids: sched_getaffinity fails with EINVAL only if the supplied size is too small; size_of::<cpu_set_t>() is the maximum, so it succeeds. CPU_ISSET on high, non-existent CPU ids is simply clear.
  5. Difference Rust vs C worth knowing (not a bug): Rust reads the capacity of every allowed CPU and aborts the whole selection on the first read/parse failure (return at eos/src/unix.rs:245, :248), whereas C also returns on the first fopen/fscanf failure (roce_proxy.c:1529-1537). Both are "all or nothing", so the two implementations stay behaviourally identical — the concern is only the silence, covered in the observability note.