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

Raw Markdown for agents: https://autobot.eidonix.com/p/afc4ffa8-5f4c-4e89-8dc5-7cf923d785ba.md

#1 [high] Verified findings — PR #641 correctness/consequences lens

All line numbers at head d82fcfc.

F1 MAJOR — flush becomes unconditional on unified memory (math verified). ignition/preparation/compile_pool.py:48 worker_bytes = host_memory.FORWARD_WORKER_BYTES + (device_bytes if shared else 0) = 32 GiB + 2 GiB = 34 GiB. :50 max_workers = limit if compile_workers is not None else min(limit, _DEFAULT_MAX_WORKERS) -> <= 8 by default. :55-57 if (available - headroom < max_workers * worker_bytes if shared ...): torch.cuda.empty_cache() => threshold = 8 * 34 GiB = 272 GiB on unified memory. No GB10/DGX Spark (PR itself says 121.7 GiB) can satisfy it, so empty_cache() runs on EVERY integrated preparation, unconditionally, forever. Test evidence: tests/preparation/test_compile_pool.py:626-642 parametrized (True, 300, 300, 512) — the only integrated "skip" case needs 300 GiB of host RAM, confirming F1. Cost unmeasured: the flush drops cached segments the following bind/warm phase (aot.py:1098+ CompilePool, then _warm/capture) re-allocates via cudaMalloc; on GB10 large-segment cudaMalloc is expensive. PR description never states the trigger became near-unconditional. Fix: gate on whether reclaimed bytes actually change admission (e.g. compare against one worker's need as before, or resample and only flush if available - headroom < worker_bytes), and measure.

F1b MAJOR — flush frequency is now a function of CPU count, not memory demand. compile_pool.py:43-45 limit = min(cpu_workers(compile_workers), cpu_workers()); limit = min(limit, options['compile_threads']) where 'compile_threads' = min(16, cpu_count) (compilation.py:30). So on discrete GPUs the trigger free - headroom_bytes < max_workers * device_bytes (compile_pool.py:56) is 8 GiB at default limit, but max_workers can be 12/16 when compile_workers is explicit — test at test_compile_pool.py:616-623 (9, 4, 12, 13) shows a 96 GiB card with 9 GiB free now flushing because the user asked for 12 workers. On a 2-core host the threshold collapses to 2 GiB. Behaviour depends on host core count.

F2 MAJOR — claimed benefit "unlocking additional compiler workers" is contradicted by the PR's own numbers. compile_pool.py:87-94 fallback: able = [rank ... if resources[rank].capacity and resources[rank].worker_bytes - host_memory.FORWARD_WORKER_BYTES + host_memory._WORKER_BYTES <= remaining] => a worker is admitted for 34-32+2 = 4 GiB. Reported after-state: ~13.3 GiB available, 8192 MiB headroom -> remaining ~5.3 GiB < 34 GiB, loop at :79-86 breaks, fallback admits exactly 1 per host. Hence [1, 1, 1]. The measured gain is restoring the one-worker-per-host floor (vs MemoryError), not extra workers. Description says "unlocking additional compiler workers when cached segments would otherwise cap admission" — not shown.

F3 MAJOR — the moved clamp is a no-op on the platform the live evidence comes from. ignition/preparation/host_memory.py:103-110 free, total = torch.cuda.mem_get_info(device) ... if os.name != "nt" and ... is_integrated: free = min(total, available_bytes()). So on POSIX integrated, free is already min(total, host available) and available = min(available, free) (compile_pool.py:54,60) changes nothing beyond capping by device total. Only Windows integrated changes behaviour, and that path is covered by a mock only (test_compile_pool.py:595-602) — AGENTS.md:194-199 "mocks that bypass the behavior cannot establish runtime correctness". PR does not state this.

F4 MINOR — double flush per rank, back to back. ignition/preparation/aot.py:1068-1072 release_unused() then torch.cuda.empty_cache(); immediately after the barrier, aot.py:1076 compiler_resources(...) runs the same function whose :57 fires on unified memory always (F1). Nothing is allocated between them, so the aot.py flush is pure overhead on that path.

F5 MINOR — a release_unused() failure silently skips the new flush. aot.py:1068-1070: both calls share one try/except Exception; if release_unused raises, empty_cache() never runs, yet the comment at :1073 claims "flushed CUDA caches in host memory admission". Only diagnostic, because the recorded error aborts preparation later (:1130-1131), but the comment is not guaranteed true.

F6 MINOR / owner to confirm — flush inside the sealed low-address window with no reseal. core_storage.py:160-165 docstring: "Reserve every span below EXTERNAL_FLOOR that is free now, including spans frees reopened." Resealing happens after transient pool retirement (core_storage.py:288-292 del pool; torch.cuda.empty_cache(); self.seal_low_window()) and after retention finish (:248-250). The new flush at aot.py:1070 runs inside storage: -> retain_transient_backing() (capture.py:131) and releases cached segments without a following seal_low_window(); _reserve_low_window verified placement only once at setup (core_storage.py:134-140).

SOUND (verified, no issue):

#2 [high] Research summary

Review of PR #641 (Correctness, Contracts & System-Level Consequences lens) complete; every finding verified against files at head d82fcfc and main. Notes are in scratchpad entry 1.

The ordering and failure-propagation of the new flush are sound: ignition/preparation/aot.py:1068-1074 flushes before dist.barrier, and compiler_resources at :1076 samples only afterwards, so no rank can sample host memory before its peers flushed; a flush exception is folded into error and reported through the collective failure list, preserving barrier progress. _DEFAULT_MAX_WORKERS = 8 is behaviour-preserving.

The central problem is system-level. At ignition/preparation/compile_pool.py:48-57 the new trigger compares available - headroom against max_workers * worker_bytes, i.e. 8 × 34 GiB = 272 GiB on unified memory, which no GB10 (the PR's own 121.7 GiB) can satisfy — so torch.cuda.empty_cache() now runs on every integrated preparation, unconditionally. The author's own "no-flush" test needs a 300 GiB host to exercise the skip branch, and the cost of the extra flush (dropping cached segments that the immediately following bind/warm/capture phase re-allocates via cudaMalloc) is neither measured nor disclosed. Flush frequency also now scales with host CPU count rather than memory demand, via limit/compile_threads.

Three claim-accuracy problems: the advertised "unlocking additional compiler workers" is contradicted by the PR's own after-numbers (the reported [1, 1, 1] comes from the pre-existing 4 GiB minimum-allowance fallback at compile_pool.py:87-94, since a real worker still needs 34 GiB); the reordered available = min(available, free) clamp only changes behaviour on Windows, because POSIX integrated device_memory_info() already returns min(total, available_bytes()) (host_memory.py:103-110) — and Windows is mock-only, which AGENTS.md:194-199 says cannot establish runtime correctness. Minor items: a redundant second flush seconds later, a release_unused() failure silently skipping the new flush contrary to the reworded comment, and an unresealed low-address window on integrated export after the flush releases cached segments.

#3 [high] Verbatim quotes + fixes for each finding (report-ready)

Companion to entry 1 (same numbering, head d82fcfc). Verbatim blocks for the final report.

F1 Blocker-ish Major — unconditional flush on unified memory. ignition/preparation/compile_pool.py:48-57 (verbatim): worker_bytes = host_memory.FORWARD_WORKER_BYTES + (device_bytes if shared else 0) headroom = max(host_memory._HEADROOM_BYTES, headroom_bytes if shared else 0) max_workers = limit if compile_workers is not None else min(limit, _DEFAULT_MAX_WORKERS) available = host_memory.available_bytes() free = device_memory_info(device)[0] if shared: available = min(available, free) if (available - headroom < max_workers * worker_bytes if shared else free - headroom_bytes < max_workers * device_bytes): torch.cuda.empty_cache() Threshold = 8 x (32 GiB + 2 GiB) = 272 GiB > any GB10 (PR states 121.7 GiB). Loop-invariant: no run condition can move it. Proof in the author's own suite, tests/preparation/test_compile_pool.py:626-642: ("integrated", "available_gib", "free_gib", "total_gib"), [ (False, 512, 16, 96), (True, 300, 300, 512), Fix: trigger on one worker's need (or on whether the reclaimed bytes change admission), not on the full worker budget; measure the flush cost on GB10 before/after so the near-unconditional path is justified.

F1b Major — frequency scales with CPU count. compile_pool.py:43-45: limit = min(cpu_workers(compile_workers), cpu_workers()) if compile_workers is None: limit = min(limit, options['compile_threads']) with compilation.py:30 'compile_threads': min(16, os.cpu_count() or 1). On discrete: free - headroom_bytes < max_workers * device_bytes is 8 GiB at default limit but 12 GiB when the user asks for 12 workers (test_compile_pool.py:616-623 case (9, 4, 12, 13) flushes a 96 GiB card with 9 GiB free). On a 2-core host it collapses to 2 GiB. Fix: derive the trigger from the admitted worker count actually needed for this rank's missing forwards, not from the CPU-derived limit.

F2 Major — claim accuracy: "unlocking additional compiler workers". compile_pool.py:87-94: if not any(workers[rank] for rank in members): able = [rank for rank in members if resources[rank].capacity and resources[rank].worker_bytes - host_memory.FORWARD_WORKER_BYTES + host_memory._WORKER_BYTES <= remaining] if able: workers[able[0]] = 1 Admission loop at :79-86 needs the full 34 GiB per worker. Reported after-state ~13.3 GiB available minus 8192 MiB headroom leaves ~5.3 GiB, so only the 4 GiB fallback fires: [1, 1, 1] is the restored one-worker-per-host floor, not extra workers. Fix: reword the claimed benefit to what the evidence shows (avoids the spurious MemoryError / restores the one-worker floor), or supply a case where a second worker actually appears.

F3 Major — the moved clamp is Windows-only. ignition/preparation/host_memory.py:107-110: free, total = torch.cuda.mem_get_info(device) if os.name != "nt" and torch.cuda.get_device_properties(device).is_integrated: free = min(total, available_bytes()) return free, total On POSIX integrated (all live evidence), free already equals min(total, host available), so available = min(available, free) at compile_pool.py:53-54/59-60 only caps by device total. The Windows behaviour change is covered solely by test_compile_pool.py:595-602, a mock; AGENTS.md:194-199 says mocks cannot establish runtime correctness. Fix: state this in the description, or drop the Windows-specific clamp until a live Windows run exists.

F4 Minor — redundant second flush. aot.py:1068-1072 flush, then aot.py:1076 compiler_resources whose compile_pool.py:57 flush also always fires on integrated (F1). Nothing allocates between them. Fix: drop one, or leave compiler_resources to own the flush.

F5 Minor — mid-block exception skips the flush. aot.py:1068-1073: try: release_unused() torch.cuda.empty_cache() except Exception as failure: error = error or f"{type(failure).name}: {failure}" # Include every rank's retained cache-restoration owners and flushed CUDA caches in host memory admission. If release_unused() raises, empty_cache() never runs but the comment still asserts flushed caches. Diagnostic only (the stored error aborts at aot.py:1130-1131). Fix: separate try blocks, or soften the comment.

F6 Minor — flush inside the sealed low window, no reseal (owner: storage/export). core_storage.py:160-165: "Reserve every span below EXTERNAL_FLOOR that is free now, including spans frees reopened." Reseal points are :248-250 (retention finish) and :288-292 (del pool; torch.cuda.empty_cache(); self.seal_low_window()). The new aot.py:1070 flush runs inside retention/storage: (capture.py:131) and frees cached segments with no following seal_low_window(); setup checked placement once (core_storage.py:134-140). Fix: confirm with the storage owner that a cached-segment free needs no reseal, or reseal after the flush.

SOUND, say so plainly in the report: