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

Raw Markdown for agents: https://autobot.eidonix.com/p/1a7ba0f7-5ae3-4e31-b8b5-78e44b04472f.md

#1 [high] PR #658 correctness review — verified findings with citations

Head f4851b3. Files inspected in full: ignition/preparation/core_storage.py, ignition/preparation/capture.py, ignition/components/sampling.py, ignition/preparation/tensor_metadata.py, ignition/preparation/persistent.py, ignition/cli/serve.py (227-232), ignition/preparation/runtime_core.py (343-441), heliopause/src/export/capture.rs (1886-2010, 3412-3440), heliopause/src/export/reachability.rs (1-321), docs/runtime-core-design.md (95-135), docs/windows.md (215-240), docs/preparation-development.md (77-99), tests/preparation/test_low_window.py, test_capture_cublas_workspace_cuda.py, test_capture_stream_isolation.py, test_core_storage.py, test_capture_graph_metadata.py (1-120).

F1 MAJOR — reseal is one graph too late. capture.py:222-227: with communication, session.capture(), torch.cuda.graph(graph, pool=pool, stream=stream): function() then graph = record_export_graph(...) then seal_low_window(). Any external allocation made while the low spans were open (during the just-finished capture) stays low; the seal only prevents the next one. Scoped to the exporting branch only (exporting = retain_graphs and warmup_storage is not None, capture.py:202); the elif retain_graphs: path at 228 never seals. Test test_capture_graph_metadata.py:556-569 enshrines the order (serving_capture, seal_low_window). Fix: call seal_low_window() after the torch.cuda.graph block and before record_export_graph.

F2 MINOR — duplicate seal: capture.py:184 seal_low_window() runs immediately after the probe's with warmup(): exits, and CoreStorage.transient.exit already does torch.cuda.empty_cache(); self.seal_low_window() (core_storage.py:290-296). Cost = a second full 4 GiB→36 GiB host scan.

F3 MINOR/MAJOR cost — seal_low_window scans LOW_SCAN_START(1<<32)→EXTERNAL_FLOOR (core_storage.py:44,173-191). Floor moved 12 GiB→36 GiB (core_storage.py:71), so each pass covers 32 GiB instead of 8 GiB; number of passes = 1 probe + 1 per exported graph (10 on the reported 9-graph export). Unmeasured; test_low_window.py:73 bounds calls only for the 12 GiB class default.

F4 MAJOR claim accuracy — core_storage.py:68-70 and the PR text say the band is "24 GiB [0x9_0000_0000, 0xf_0000_0000) ... below DEVICE_BASE (0x12_0000_0000)". 0xf_0000_0000 == DEVICE_BASE - RESERVATION_BYTES (0x12_0000_0000 - 0x3_0000_0000); the band to DEVICE_BASE is 36 GiB. New test asserts only 0x303000000 < EXTERNAL_FLOOR < DEVICE_BASE (test_core_storage.py:113), which is why the wrong bound passes.

F5 evidence/claim gaps: no in-repo artifact for the cuRAND [0x3_0000_0000, 0x3_0300_0000] claim (only the new comment); live SM87 verification is self-reported on a pre-rebase checkout a747a5a5 while head is f4851b3; the changed path's CUDA tests are gated FLARE_RUN_CUDA_TESTS=1 (test_capture_cublas_workspace_cuda.py:22-23) and were not run.

F6 MINOR test quality: test_core_storage.py:83-113 lets _Cuda() raise (StopAfterWindow) so _reserve_low_window (core_storage.py:139-144, the only real effectiveness check) never runs; test_capture_graph_metadata.py:477-500 runs real torch.mm/addmm on CPU so it proves ordering only; no new test fails on main.

F7 MINOR platform parity: probe capture gated only on warmup_storage.integrated (capture.py:173) while seal_low_window no-ops on Windows (core_storage.py:170); docs still say 256 GiB (docs/runtime-core-design.md:113-116, docs/windows.md:226-227).

F8 MINOR addmm: capture.py:72-73 doesn't pin preferred_blas_library; test_capture_cublas_workspace_cuda.py:39-40 shows which library is warmed is a runtime switch. NOTE: NOT a bug — out is fully written by mm before addmm reads it, so no uninitialized read.

Sound: no Rust/proto/CLI/wire change; device_address_floor semantics preserved (runtime_core.py:428 → export check capture.rs:3417-3425); sampling change is a no-op outside the CoreStorage numeric scope (tensor_metadata.py:112-115); the (8,7) guard and new constants keep DEVICE_BASE+48GiB = 0x1E_0000_0000 = 120 GiB < 128 GiB GA10B window.

#2 [high] Research summary

Reviewed all five changed files in context plus their callers/contracts: ignition/preparation/capture.py (full), core_storage.py (full), components/sampling.py, tensor_metadata.py, persistent.py, runtime_core.py:343-441, the only production caller chain (ignition/cli/serve.py:227-232 → recipe prepare), the export-side contract (heliopause/src/export/capture.rs:3412-3440, reachability.rs), the three design docs, and every directly related test. Findings are in scratchpad entry 1.

Headline finding: the new reseal is one graph too late. In ignition/preparation/capture.py:222-227 the seal runs after the just-finished serving capture has already let external allocations land in the reopened low spans — it only protects the next capture — and it is scoped to the exporting branch only, so the elif retain_graphs: path (:228) never reseals at all. The new test tests/preparation/test_capture_graph_metadata.py:556-569 enshrines exactly that ordering, and no new test fails on main. Secondary: a redundant second full seal_low_window() per export (capture.py:184, since CoreStorage.transient.__exit__ already seals at core_storage.py:290-296), and an unmeasured cost increase because the floor was raised 12→36 GiB while the scan still starts at 4 GiB (core_storage.py:44,173-191), i.e. 32 GiB per pass × (1 probe + 1 per exported graph).

Claim accuracy: the new comment and the PR text state the external band is "24 GiB [0x9_0000_0000, 0xf_0000_0000) … below DEVICE_BASE (0x12_0000_0000)", but 0xf_0000_0000 is DEVICE_BASE - RESERVATION_BYTES; the band is 36 GiB. The added assertion (tests/preparation/test_core_storage.py:113) only checks 0x303000000 < floor < DEVICE_BASE, so the wrong bound is invisible to CI, and the test fakes _Cuda() to raise, bypassing _reserve_low_window entirely. The cuRAND bit-pattern claim has no in-repo evidence, and the live SM87/Orin runs are self-reported on a pre-rebase checkout while the changed capture path's CUDA tests are env-gated and unrun.

The rest is sound: no wire/proto/CLI/Rust change, device_address_floor semantics and load compatibility preserved, the vocab_ids record is a no-op outside the CoreStorage numeric scope, and the addmm addition does not read uninitialised out.

#3 [high] PR #658 review — verbatim quotes, severities, impacts, fixes (for the report)

Head f4851b3, base main. Line-number provenance: capture.py / core_storage.py / sampling.py / tensor_metadata.py / runtime_core.py / docs / heliopause were read with line numbers (exact). tests/preparation/test_core_storage.py new lines confirmed by search_code hits at :83, :108, :109, :113, :116, :135 (diff hunk @@ -78,3 +78,62 @@). tests/preparation/test_capture_graph_metadata.py new block is diff hunk @@ -472,3 +472,97 @@ => lines 475-569; search_code confirmed capture.capture_forwards( at :545 and retain_graphs=True, at :553, so the probe/reseal test occupies ~:536-569 and the mm/addmm test ~:475-519. Cite those as ranges, not invented single lines.

== F1 (Major) Reseal runs one graph too late, and only on the exporting branch == ignition/preparation/capture.py:222-228

                    with communication, session.capture(), torch.cuda.graph(graph, pool=pool, stream=stream):
                        function()
                    if exporting:
                        graph = record_export_graph(graph, warmup_storage, owners=(function,))
                        if getattr(warmup_storage, "integrated", False):
                            warmup_storage.seal_low_window()
                    elif retain_graphs:

Impact: the defect being fixed is "capture reopens low spans, then an external allocation lands there". The seal after capture N only closes spans for capture N+1; it cannot evict an allocation that already landed during capture N. So each capture still runs with the previous capture's reopened low span available to cuBLAS/cuDNN/etc. Scoped to exporting (capture.py:202: exporting = retain_graphs and warmup_storage is not None), so the elif retain_graphs: path never reseals. Test tests/preparation/test_capture_graph_metadata.py:~556-569 locks in the late order. Fix: move the seal inside the same block, immediately after the torch.cuda.graph(...) context (before record_export_graph), and cover the non-exporting retain path if it shares the stream.

== F2 (Minor) Redundant second full low-window scan per export == ignition/preparation/capture.py:177-184

            with warmup():
                probe_buf = torch.empty((1,), dtype=torch.int8, device=device)
                probe_graph = torch.cuda.CUDAGraph()
                with torch.cuda.graph(probe_graph, stream=stream):
                    probe_buf.zero_()
                del probe_graph, probe_buf
            stream.synchronize()
            warmup_storage.seal_low_window()

ignition/preparation/core_storage.py:288-296 (transient exit already seals)

        try:
            with torch.cuda.use_mem_pool(pool, device=self.device):
                yield
        finally:
            torch.cuda.synchronize(self.device)
            if not shared:
                del pool
                torch.cuda.empty_cache()
                # Returned segments may reopen low addresses that later library allocations would take.
                self.seal_low_window()

Impact: warmup = warmup_storage.transient (capture.py:116), so leaving the with warmup(): block already empties the cache and seals. The extra call is a second entire 4 GiB->36 GiB reservation sweep per export. Fix: drop line 184 (transient exit covers it) or document why a second sweep is needed.

== F3 (Minor->Major on cost, unmeasured) Seal cost grew 4x with the floor == ignition/preparation/core_storage.py:42-44

    # Keep library-owned device storage above the ranges occupied by Linux and WDDM contexts.
    EXTERNAL_FLOOR = 0x4000000000
    # Where sealing below the floor starts; a context's own storage lies above it.
    LOW_SCAN_START = 1 << 32

core_storage.py:172-191 walk from LOW_SCAN_START to EXTERNAL_FLOOR, granule by granule for taken spans, with cuMemAddressReserve/cuMemAddressFree per probe (_reserve_at/_release_last, :146-162). Impact: floor 12 GiB -> 36 GiB (core_storage.py:71) makes each sweep cover 32 GiB instead of 8 GiB, and the PR adds 1 probe sweep + 1 sweep per exported graph (10 sweeps on the reported 9-graph export). Each new granule is a CUDA driver call. The existing bound test only covers the 12 GiB class default: tests/preparation/test_low_window.py:73 assert space.calls - calls < 2 * (context[1] // GRANULE) + 50. No measurement in the PR. Fix: hoist the post-capture seal to once after the capture loop (the export scan happens later anyway), or state the measured wall-clock cost of the 9-graph export.

== F4 (Major, claim accuracy) The stated 24 GiB band and its upper bound are wrong == ignition/preparation/core_storage.py:67-73

            # Jetson Orin (GA10B) exposes a 128 GiB CUDA VMM address window (below 0x20_0000_0000).
            # Place EXTERNAL_FLOOR at 0x9_0000_0000 (36 GiB) so external library allocations lie in
            # the 24 GiB [0x9_0000_0000, 0xf_0000_0000) range free of cuRAND precalc_xorwow_offset_matrix
            # false-positive 64-bit pointer patterns (such as 0x3_0000_0000 and 0x3_0300_0000).
            self.EXTERNAL_FLOOR = 0x900000000
            self.DEVICE_BASE = 0x1200000000
            self.RESERVATION_BYTES = 48 << 30

0xf_0000_0000 = DEVICE_BASE - RESERVATION_BYTES (0x1200000000 - 0x300000000); the band from the floor to DEVICE_BASE is 36 GiB (0x90000000..0x120000000), and the allocated window is 48 GiB. The PR description repeats the same two errors ("24 GiB ... below DEVICE_BASE"). Constants themselves are consistent: DEVICE_BASE + 48 GiB = 0x1E_0000_0000 = 120 GiB < the 128 GiB window at :67. Fix: state 36 GiB or replace both bounds with the real ones; add assert owner.DEVICE_BASE - owner.EXTERNAL_FLOOR == 36 << 30 (or the intended value) instead of the loose bound below.

== F5 (Major, claim accuracy / evidence) cuRAND and live-run claims are unbacked == Only in-tree trace of the cuRAND claim is the new comment above plus the new test name test_ga10b_integrated_window_places_external_floor_above_curand_table (tests/preparation/test_core_storage.py:83) and its re-statement at :110-113. The live evidence ("ga10b-tp1-202612010822, 9 graphs", flare run --memory-limit -16m on Orin Nano) is self-reported and the serving check is on a pre-rebase checkout (a747a5a5) while head is f4851b3. The CUDA tests over the changed path are env-gated and unrun: tests/preparation/test_capture_cublas_workspace_cuda.py:22-23

pytestmark = pytest.mark.skipif(os.environ.get("FLARE_RUN_CUDA_TESTS") != "1",
                               reason="requires an explicitly assigned CUDA device")

Fix: attach the export log / allocation dump showing words 0x3_0000_0000 and 0x3_0300_0000, and re-run the export on f4851b3.

== F6 (Minor, tests) The two new GA10B tests cannot fail on main for the right reason == tests/preparation/test_core_storage.py:95-113

    class FakeCuda:
        def __init__(self):
            recorded["called"] = True
            raise StopAfterWindow
...
    owner = CoreStorage.__new__(CoreStorage)
    with pytest.raises(StopAfterWindow):
        CoreStorage.__init__(owner, rank=0, world=1, device=0)
...
    assert 0x303000000 < owner.EXTERNAL_FLOOR < owner.DEVICE_BASE

_Cuda() raising means _reserve_low_window() (core_storage.py:130-144, the only check that allocations actually land above the floor: raise RuntimeError(f"device allocations still start at {above:#x} below {self.EXTERNAL_FLOOR:#x}")) never runs; the test asserts config only. The mm/addmm test runs real torch.mm/addmm on "cpu" and asserts events == ["mm", "addmm", "sync"] (added block tests/preparation/test_capture_graph_metadata.py:475-519), i.e. ordering only. The probe test mocks torch.cuda.graph to nothing and asserts the event list (tests/preparation/test_capture_graph_metadata.py:~536-569). None is a regression test. Fix: extend test_low_window.py with a 36 GiB-floor fixture (AddressSpace over 0x900000000) so the sweep cost/coverage is exercised, and assert the seal happens before the next capture rather than after the last one.

== F7 (Minor) Windows asymmetry, docs not updated == capture.py:173 gate is if getattr(warmup_storage, "integrated", False): while core_storage.py:170-171 is if not self.integrated or os.name == "nt": return, and CoreStorage only reserves on non-Windows (core_storage.py:110-111 if self.integrated and os.name != "nt":). So an integrated WDDM host captures a probe graph and reseals nothing. Docs still say 256 GiB: docs/runtime-core-design.md:113-116 and docs/windows.md:225-227 (the base must lie at or above EXTERNAL_FLOOR (256 GiB)). Fix: note the GA10B exception or scope the docs sentence, and either gate the probe on os.name != "nt" or comment the deliberate no-op.

== F8 (Minor) addmm does not pin the BLAS library == capture.py:68-74 (quoted above). addmm with a 1-D bias may dispatch to cublasLt or legacy cuBLAS depending on torch.backends.cuda.preferred_blas_library(); tests/preparation/test_capture_cublas_workspace_cuda.py:39-40 shows the library is a runtime switch (torch.backends.cuda.preferred_blas_library("cublaslt")). Any test/export run under a different setting warms a different cache. Not a bug: out is fully written by torch.mm before torch.addmm reads it, so no uninitialised read. Fix: run the seed under the same preferred library the capture uses, or say which workspace is targeted.

== Explicitly sound (state this in the report) ==

== Charter / scope notes ==