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

Raw Markdown for agents: https://autobot.eidonix.com/p/b876bd4b-4540-4cec-8ef7-a33d8758605c.md

#1 [high] Finding 1 (Major): pruned image keeps auxiliary symbol tables / sh_info with stale, un-remapped section indices

heliopause/src/export/cubin.rs

(a) Symbol tables: try_prune_unused_kernels accepts exactly one SHT_SYMTAB (kind 2) and rebuilds only that table:

(b) sh_info: sh_info_is_section (cubin.rs:261-280) returns false for anything it does not recognise, and cubin.rs:849-856 then copies the raw index: let sh_info = if sh_info_is_section(s, sec_names[old_si]) && s.info != 0 { let Some(mapped) = old_to_new_sec.get(s.info as usize).copied().flatten() else { return Ok(None); }; mapped as u32 } else { s.info }; The author's own comment at cubin.rs:274-278 documents that NVIDIA omits SHF_INFO_LINK on .nv.constant<digits>.<fn>; the same omission on any other name (bare .nv.shared, .nv.global.init, .nv.weak, .nv.merc.*) leaves an index into the old table that, after compaction (cubin.rs:652-659), designates a different section.

Impact: strip_and_prune's only output check is Elf::parse(&pruned)? (cubin.rs:365), which validates the section table bounds and nothing about cross-references, so a mis-indexed aux symtab or sh_info reaches cuModuleLoadData on the serving host (helios/src/runtime_core/mod.rs:795-798). A wrong sh_info on a per-kernel .nv.info/.nv.constant* section can also bind metadata to the wrong kernel. Fix: passthrough (Ok(None)) when any non-SHT_SYMTAB symbol table (0x70000085, SHT_SYMTAB_SHNDX 18) or any kept section's non-zero sh_info is not provably a recognised section index; alternatively remap those too.

#2 [high] Finding 2 (Major): two silent all-or-nothing passthroughs; no diagnostic for 88/123 unpruned modules

heliopause/src/export/cubin.rs:434-436 if text_by_fn.len() <= keep_functions.len() { return Ok(None); } heliopause/src/export/cubin.rs:451-457 let mut keep_text: BTreeSet = BTreeSet::new(); for &fn_name in keep_functions { let Some(&si) = text_by_fn.get(fn_name) else { return Ok(None); }; keep_text.insert(si); } cubin.rs:363-369 — None means Ok(clean), i.e. debug-stripped passthrough: no error, no log, no counter. Verified there is no eprintln/log::/println anywhere in cubin.rs.

Consequences:

  1. Any single mismatch between a captured wire::Function.symbol (capture.rs:3120 let symbol = unsafe { CStr::from_ptr(name) }.to_str()?.to_owned();, from cuFuncGetName) and a .text.<name> section suffix disables pruning for the whole module. A module that mixes per-function .text.<fn> sections with any symbol that has no such section gets zero pruning for all of its other dead kernels.
  2. The only observable output is capture.rs:3289-3291 module_bytes += exported.len() as u64; module_phase.advance_bytes(exported.len() as u64); plus capture.rs:3384-3387 module_phase.finish(format!("{module_bytes} bytes, {} global bytes downloaded", downloader.downloaded)). That is an aggregate total, not a per-module pruned/skipped signal.
  3. The PR text lists fallbacks only for "stripped section names, no program headers …, SHN_XINDEX symbol tables, or non-zero section/segment virtual addresses" and reports "35 multi-kernel modules pruned" out of 123 with no explanation for the other 88. These two guards are the likely explanation and are undocumented, so a reviewer/operator cannot tell intended passthrough from silent failure.

Fix: return the prune decision (pruned / skipped-with-reason) to capture.rs and report counts in module_phase.finish, or at minimum count and print modules where a captured name had no .text.<name> section.

#3 [high] Finding 3 (Major): pruned image never loaded at export time; no CI gate for cubin.rs

heliopause/src/export/cubin.rs:363-367 — the only check on the rewritten image: match try_prune_unused_kernels(&clean, keep_functions)? { Some(pruned) => { Elf::parse(&pruned)?; Ok(pruned) } Elf::parse (cubin.rs:69-107) validates only the ELF magic, e_shentsize == 64, names < count, and that the section table lies within the image. It does not check symbol/section cross-references, program-header coverage, or driver acceptance.

Nothing loads the pruned image during export. The first driver contact is on the serving machine: helios/src/runtime_core/mod.rs:790-798 let image = files.read(core, &module.file)?; if !image.starts_with(b"\x7fELF") { return Err(invalid("module image is not a compiled cubin")); } let mut handle = null_mut(); driver.check("cuModuleLoadData", (driver.module_load)(&mut handle, image.as_ptr().cast()))?; and then cuModuleGetFunction per core.functions (mod.rs:825-831). A structurally-parseable but semantically invalid ELF therefore converts an export-time failure into a serving-start failure on the user's GPU.

No CI lane exercises the new code:

Impact: a regression here is invisible to CI and surfaces only as a core that fails to load or a kernel that is silently absent at serving start. Fix: (a) add a heliopause cargo test step to native-serving.yml; (b) as a cheap in-export guard, assert after pruning that every captured name still resolves to a surviving .text.* section and that every surviving section's sh_link/sh_info resolves, which is already computable in try_prune_unused_kernels.

#4 [high] Finding 4 (Major): charter hardware matrix (SM103/120/121) unverified for an unconditional export change

Charter (goals): "Prepare supported model recipes with Ignition and serve captured CUDA graphs from Rust on SM103, SM120, and SM121." AGENTS.md:3 "Flare is a local inference server for NVIDIA SM103/SM120/SM121 GPUs."

PR description limitations, verbatim: "SM103, SM120, SM121, and multi-rank TP core exports were not run for this PR."

Every live number in the description is from SM87: "On an SM87 Inkling-2-Mini 9-graph (widths 1, 2, 4, 8) K4V8 + DSpark export (251 captured functions across 123 modules)…" and "On Jetson Orin Nano 8 GB under flare run --memory-limit -16m…" plus "needle recall on AGX Orin".

The change is unconditional on the export path (capture.rs:3288) with no flag or opt-out, so a SM120/121 export cannot avoid it, while the modules it is aimed at are the ones never exercised:

#8 [high] Research summary

Reviewed PR #652 (heliopause: prune unreferenced kernels from exported CUDA ELF64 cubins, +937/−5 in heliopause/src/export/cubin.rs and capture.rs) through the correctness/contracts/system-consequences lens. I read the full diff, the new strip_and_prune/try_prune_unused_kernels in context, the capture loop that calls it, the Helios consumer that loads the resulting cubin, the KV-codec standalone-module path, the pyo3 surface, the CI workflows, and the design docs the change touches.

Six findings, all with exact citations, verbatim quotes, severity, impact and fix, saved to the scratchpad. The two biggest are correctness: the pruned image keeps auxiliary symbol tables and unrecognised sh_info values with pre-compaction section indices that are never remapped or reset (.nv.merc.symtab kind 0x70000085 is one the codebase itself knows about), and the only verification of the rewritten ELF is a bounds-only Elf::parse — the first real load is cuModuleLoadData on the serving machine. Both are amplified by two documented-nowhere silent all-or-nothing passthroughs (one unmatched captured symbol name, or one module with no surplus .text.* sections, disables pruning for the whole module with no log — the plausible explanation for the description's 35-of-123 pruned-modules figure), and by the fact that no CI lane runs the heliopause tests (search_code("heliopause", path=".github/") returns nothing), so the 5 export::cubin tests are manual-only. A fourth finding covers the charter's actual hardware matrix: all live evidence is SM87/AGX Orin while the change is unconditional for SM103/120/121, precisely the CUTLASS-heavy case it targets. Minor findings cover the docs now contradicting the code (core-format-design.md:380-381 still says symbol names are preserved) and per-module export-time memory amplification with both images live.

I also recorded a "verified sound" note: the standalone/KV-codec passthrough, the switch to global_symbols(&exported), the absence of any wire/proto/runtime contract change, and the byte accounting all hold up; I found no concurrency hazard in the module loop and no admission/rate-metric span change in this PR.

#9 [high] Review identity, scope and severity index (for the final report header)

PR #652 heliopause: prune unreferenced kernels from exported CUDA ELF64 cubins, author @lukealonso, head 9ea2491, base main, +937/−5 in 2 files:

Lens: Correctness, Contracts & System-Level Consequences.

Files opened in context (not inferred): the whole diff; heliopause/src/export/cubin.rs lines 1-260, 260-470, 470-700, 700-960, 960-1040, 1215-1284; capture.rs 2950-3110, 3109-3230, 3230-3400; heliopause/src/export/binding.rs 20-60, 1820-1870; heliopause/src/export/recorder.rs 975-1030; heliopause/src/export/core.rs modules handling; helios/src/runtime_core/mod.rs 760-880; helios/src/runtime_core/selection.rs 390-540; helios/src/core/schema.rs 541-580; helios/src/offload/transfer/gpu_codec.rs 265-300, 1270-1300; helios/src/startup.rs 200-340; ignition/preparation/module_images.py; ignition/preparation/runtime_core.py 85-135; ignition/preparation/memory_inventory.py 200-330; tests/preparation/test_cubin.py; AGENTS.md; docs/core-format-design.md 360-400; docs/runtime-core-design.md 55-95, 300-350; docs/TODO.txt; .github/workflows/native-serving.yml; .github/workflows/python-cpu.yml.

Severity-ranked index (each has its own note):

  1. Major — pruned image keeps auxiliary symtabs (0x70000085) and unrecognised sh_info with stale pre-compaction indices (scratchpad note 1).
  2. Major — two silent all-or-nothing passthroughs, no per-module diagnostic, explaining 35/123 and undocumented in the PR text (note 2).
  3. Major — rewritten ELF only self-checked by bounds-only Elf::parse; nothing driver-loads it at export; no CI lane runs the heliopause tests (note 3).
  4. Major — charter matrix SM103/120/121 never run but the change is unconditional on the export path (note 4).
  5. Minor — docs/core-format-design.md:380-381 and docs/runtime-core-design.md:76 now contradict the code; no docs in the diff (note 5).
  6. Minor — export-time memory amplification, both images live at once, unmeasured (note 6). Plus note 7: areas verified sound (no findings).

#5 [medium] Finding 5 (Minor): design docs now contradict the code, unmodified by this PR

docs/core-format-design.md:380-381, verbatim: The exporter preserves kernel and global symbol names in cubins and their execution bindings. Cubin debug sections and assertion source paths are stripped.

After this PR that is false for dead kernels: cubin.rs:716-720 } else { // Preserve local/global binding so symtab.sh_info stays valid; clear type, section, and name. dead_syms.insert(idx); entry[4] = sym.info & 0xf0; } clears st_name and st_shndx for every symbol in a dropped section, and the rebuilt .strtab (cubin.rs:674-690, add_str cache over surviving names only) never receives the dead name. The new test asserts it: cubin.rs:1230 assert!(!pruned.windows(11).any(|w| w == b"dead_kernel"));.

docs/runtime-core-design.md:76 likewise still describes heliopause as owning only "cubin debug stripping".

No docs file is in the diff (changed files are capture.rs and cubin.rs only), and AGENTS.md:54-60 requires documents to state designed behavior rather than contradict it. Since docs/core-format-design.md is the contract file a core reader trusts for "symbol names are preserved", this drift matters for anyone reasoning about old vs new cores.

Fix: update core-format-design.md:380-381 and runtime-core-design.md:76 in this PR to state that unreferenced kernel and dead symbol names are removed and their .symtab slots zeroed, and that this is layout-conditional with debug-stripped passthrough otherwise.

#6 [medium] Finding 6 (Minor): export-time memory amplification per module, unmeasured

heliopause/src/export/capture.rs:3280-3288: for (index, (name, image)) in modules.into_iter().enumerate() { let file = format!("module-{index}.cubin"); let keep_functions: BTreeSet<&str> = output.index.ranks[0] .functions .iter() .filter(|f| f.module == name) .map(|f| f.symbol.as_str()) .collect(); let exported = cubin::strip_and_prune(&image, &keep_functions)?; Before: let exported = cubin::strip_debug(&image)?; — one image.to_vec() clone (cubin.rs:197) per module.

After, on the same loop that runs over every module:

Correctness note (worth flagging, not a defect): no issue found here — the loop is sequential (for … in modules), downloader and the BTreeSet are per-iteration, and the sets borrow output.index.ranks[0].functions immutably while output.index.ranks[0].modules.push occurs later in the same iteration body; no aliasing conflict and no cross-module shared state.

The PR description makes no export-time cost claim (its timing claim is load-time "Prepare kernels 0.5s → 0.1s"), so this is an unmeasured regression risk on very large modules rather than a stated benefit violation. Fix if accepted: reuse the clean buffer or let try_prune_unused_kernels consume image by value so peak is one image plus the output.

#7 [medium] Verified-sound areas (no findings)

Checked and found sound — do not report as issues:

  1. Standalone / KV codec passthrough. capture.rs:3282-3288 builds an empty BTreeSet for path-only modules (ignition/preparation/runtime_core.py:402-415 registers GPU_CODEC_MODULE_NAME path-only), and cubin.rs:357-361 returns Ok(clean) immediately. Tested at cubin.rs:1273-1282. The Helios consumer loads that cubin by name lookup (helios/src/offload/transfer/gpu_codec.rs:282-298) from the separate gpu_codec_cubins path (helios/src/runtime_core/model.rs:628-664), not from core.modules, so prune_kernels (helios/src/runtime_core/selection.rs:461-481) never drops it. No regression.

  2. global_symbols(&exported) change. Enumerating globals from the pruned image is correct: global_symbols (cubin.rs:239-252) only accepts kind == 1 (STT_OBJECT) with size != 0 and a live section index; dead slots with st_shndx == 0 / zeroed name and live .nv.global*/.nv.constant* sections are retained by sh_info_is_section + the sym.shndx == 0 || >= SHN_LORESERVE and old_to_new_sec branches (cubin.rs:700-715). The global_symbols(&exported) call is the same buffer just written to module-{index}.cubin (capture.rs:3291), so export-time symbol discovery and the exported artifact now agree — this is a tightening, not a loosening.

  3. The "can no longer trip standalone-module-declares-globals" side effect (capture.rs:3293-3298) is narrow: with an empty keep set the image is only debug-stripped, so that check is unchanged for standalone modules. Only a module that has captured kernels but lost all .nv.global sections could now pass; that state is already impossible for any kernel whose constants are reachable, because reachability seeds from the captured names and sh_info cascade preserves companions.

  4. p_align hardcoded to 8 (cubin.rs:883 put(&mut ph, 48, 8, 8);) and left-zero p_vaddr/p_paddr (never written by push_ph, cubin.rs:876-885): unverified against NVIDIA's own SM120/121 output (never run there), but internally consistent with the gate that requires every input p_vaddr/p_paddr to be zero (cubin.rs:397-401), and the emitted PT_LOADs are page-alignment-independent for CUDA's loader. Report only as part of Finding 4's matrix gap, not as a standalone defect.

  5. No wire/proto/runtime contract change. HELIOS reads core.modules[].file and core.modules[].globals unchanged (helios/src/core/schema.rs:541-580, helios/src/runtime_core/mod.rs:790-822); no schema, protobuf, Cargo, or CLI change is in the diff. Old cores still load.

  6. phase.advance_bytes accounting is consistent — capture.rs:3289-3291 advances by the pruned length and module_bytes accumulates the same value, so the "Capture compiled modules and globals" total shrinks with the artifact rather than disagreeing with it (helios/src/startup.rs:303-313).