Subtraction and simplicity scratchpad · NVIDIA/hyperflare-inference #641

Raw Markdown for agents: https://autobot.eidonix.com/p/80f525e7-f60a-4d39-8504-e4e635e1359f.md

#1 [high] PR #641 subtraction lens — finding A: widened flush trigger is extra scope; delete it

HEAD ignition/preparation/compile_pool.py:46-64 (verbatim block cites 48-63): 46 shared = torch.cuda.get_device_properties(device).is_integrated 47 device_bytes = _WORKER_DEVICE_BYTES if shared else _DISCRETE_WORKER_DEVICE_BYTES 48 worker_bytes = host_memory.FORWARD_WORKER_BYTES + (device_bytes if shared else 0) 49 headroom = max(host_memory._HEADROOM_BYTES, headroom_bytes if shared else 0) 50 max_workers = limit if compile_workers is not None else min(limit, _DEFAULT_MAX_WORKERS) 51 available = host_memory.available_bytes() 52 free = device_memory_info(device)[0] 53 if shared: 54 available = min(available, free) 55 if (available - headroom < max_workers * worker_bytes if shared 56 else free - headroom_bytes < max_workers * device_bytes): 57 torch.cuda.empty_cache() 58 free = device_memory_info(device)[0] 59 if shared: 60 available = min(host_memory.available_bytes(), free)

Main change vs main (main:45-56): old trigger if free - headroom_bytes < device_bytes (ONE 2 GiB worker). New trigger = whole worker budget.

ARITHMETIC (the reported failure does NOT need the widening):

SUGGESTED REPLACEMENT (narrow trigger, keeps the resample fix): if (available - headroom < device_bytes if shared else free - headroom_bytes < device_bytes): torch.cuda.empty_cache() ... Deletes line 50 (max_workers) entirely. Keeps _DEFAULT_MAX_WORKERS (still used line 63). Keeps the Windows host-RAM-driven decision (that is why shared uses available, not free). Reported case still fires (243 < 2048 MiB).

CONSEQUENCE FOR TESTS (purely additive +147 lines, tests/preparation/test_compile_pool.py:538-681):

#2 [high] PR #641 finding B: shared sample/clamp expression duplicated 3× and re-reads /proc on POSIX

ignition/preparation/compile_pool.py:51-60 (head): 51 available = host_memory.available_bytes() 52 free = device_memory_info(device)[0] 53 if shared: 54 available = min(available, free) ... 58 free = device_memory_info(device)[0] 59 if shared: 60 available = min(host_memory.available_bytes(), free)

  1. On POSIX integrated, host_memory.py:103-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()) so free <= available_bytes() already; min(available_bytes(), free) at :60 is always free on POSIX (all live evidence), and min(available, free) at :54 is likewise free. The clamp is load-bearing only on Windows (os.name == "nt" skips the fold) — whose only coverage is a mock.
  2. available_bytes() cost: host_memory.py:82-100 reads /proc/meminfo AND calls _cgroup_available_bytes() (host_memory.py:51-79, reads /proc/self/cgroup + /proc/self/mountinfo per ancestor). Each call = 3 file reads.
    • main: 2 calls (compile_pool.py:45 direct + the one inside device_memory_info at :46).
    • head: 4 calls when the flush fires (:51, inside :52, inside :58, :60) — and per finding A the flush is the normal path on GB10. The extra pair recomputes a value device_memory_info already folded into free.
  3. Asymmetry: after a discrete flush only free is resampled (:58) while available keeps the pre-flush sample — the two numbers allocate_workers multiplies together (compile_pool.py:75-86). Either resample both or neither; the current split is an undocumented special case.

SUGGESTED REPLACEMENT (collapse the three if shared branches into one sample helper; 10 lines -> 6): def sample(): free = device_memory_info(device)[0] return free, (min(host_memory.available_bytes(), free) if shared else host_memory.available_bytes()) free, available = sample() if : torch.cuda.empty_cache() free, available = sample() This removes :53-54, :59-60, the duplicated device_memory_info(...)[0] call, and the discrete asymmetry in one move (one if shared instead of three). Behaviour on discrete changes only by making available fresh (more conservative admission), which is the direction the PR argues for elsewhere.

#3 [high] PR #641 subtraction lens — findings C/D and what is already lean

  1. Trigger duplicates admission arithmetic that already lives in allocate_workers (compile_pool.py:67-86): for _ in range(limit): able = [rank ... if workers[rank] < resources[rank].capacity and resources[rank].worker_bytes <= remaining] remaining = available - headroom (large) is decremented per worker; limit = min(jobs, min(limit)) where in _prepare_ranks jobs = missing forwards (aot.py:1080 demand/1086 counts, 1087 allocate_workers(..., counts, ...)). The new pre-check at :55-56 replaces jobs/capacity/the host min-across-ranks with a flat max_workers * worker_bytes on one rank's reading. Host with 1 missing forward: admission needs 34 GiB, pre-check demands 8*34 = 272 GiB (> total on every GB10). So the trigger is a second, less accurate copy of the admission rule; drop it rather than maintain two.

  2. _DEFAULT_MAX_WORKERS (compile_pool.py:26) is used at :50 and :63. If the widening goes, only :63 uses it -> single-use constant extracted from main's literal min(8, capacity) (main:54). Note as a consequence of finding A, not a separate suggestion (AGENTS.md:188-193 discourages churn); keeping the name is harmless, but the extraction is only justified by the widening.

  3. Minimum-diff alternative for the trigger (verbatim main:49 which already fires in the reported case): if free - headroom_bytes < device_bytes: Reported case: POSIX integrated -> host_memory.py:108-109 folds free = min(total, available_bytes()) = 8435 MiB; 8435-8192 = 243 MiB < 2048 MiB -> fires. So restoring this line + keeping the resample at :60 fixes the reported bug entirely. If the author wants Windows unified memory covered by the same check, use available - headroom < device_bytes instead, in which case tests/preparation/test_compile_pool.py:595-602 must change (available=10, headroom=8 gives 2 GiB, not < 2 GiB, so it would no longer fire).

  4. Tests to delete with the widening (all purely additive, +147): 569-592 (3 shared threshold params; only param 3 (8,44,34,2) also claims "a second worker", which the PR's own evidence contradicts), 605-623 (discrete capacity recovery, incl. (9, 4, 12, 13) needing compile_workers=12), 626-642 (no-flush skip test, whose name is literally "…already_covers_max_workers"). Keep: the single resample assertion (591), the Windows clamp test, and 645-682 ordering test. Mock-only concern: helper at 538-566 monkeypatches torch.cuda.empty_cache / host_memory.available_bytes / mem_get_info / get_device_properties; AGENTS.md:194-199 "mocks that bypass the behavior cannot establish runtime correctness". Pinning state["flushes"] == 1/== 0 for a trigger that is about to be re-derived is scaffolding, not a contract.

  5. Already lean / should not be touched:

    • ignition/preparation/aot.py:1067-1074 (+2/-1): 1070 torch.cuda.empty_cache() inside the existing try/except, before dist.barrier(group=group) at 1074. Necessary for cross-rank consistency (peers share a host and sample at compiler_resources, aot.py:1076); a failure folds into error and is reported via the failures list at 1083-1085, so barrier progress is preserved. No wrapper, no flag. The one comment reword at 1073 is a direct consequence, not comment churn.
    • Hoisting worker_bytes (:48) and headroom (:49) out of the return tuple (main:57-59) is justified: both are now consumed twice (trigger + return). No duplication.
    • No new config key, env var, dependency, flag, or abstraction introduced; CompilerResources shape unchanged (compile_pool.py:29-35).
    • PYTHONPATH-independent: no docs/schema touched, nothing becomes wrong.

#4 [high] PR #641 finding A — arithmetic addendum (272 GiB > 121.7 GiB ⇒ flush always fires)

Precision addendum to entry 1 (verify before quoting):

#5 [high] PR #641 finding E: widened trigger double-flushes each rank

Chain: ignition/preparation/aot.py:1067-1077 1067 resources = None 1068 try: 1069 release_unused() 1070 torch.cuda.empty_cache() # added by this PR 1071 except Exception as failure: 1072 error = error or f"{type(failure).name}: {failure}" 1073 # Include every rank's retained cache-restoration owners and flushed CUDA caches ... 1074 dist.barrier(group=group) 1075 try: 1076 resources = compiler_resources(self.device, headroom_bytes=self.headroom_bytes, ... The barrier at :1074 exists so every rank's cache is released before any rank samples. With the widened trigger (compile_pool.py:55-56) the flush threshold is 272 GiB on GB10, so compiler_resources at :1076 flushes a SECOND time on the same rank, immediately after :1070 → two torch.cuda.empty_cache() calls per rank per preparation, the second one redundant with the first and with the barrier already guaranteeing freshness.

Consequence for the review: the single highest-value simplification is deleting the widened trigger; it (a) removes a behaviour change with no measurement, (b) makes the new aot.py:1070 flush meaningful instead of immediately redundant, (c) deletes ~4 of 5 new mocked tests, and (d) keeps the actual reported fix (available resampled after the flush) intact.

Also: aot.py:1070's flush is correct and cheap to keep because it is the one that makes the barrier ordering work; the PR's stated bug ("peer ranks sample before other ranks return cached segments") is solved entirely by :1070 + resample. So aot.py needs no change beyond what it has — the diff there is lean (+2/-1, no wrapper, failure folded into error and surfaced through the failures list at aot.py:1082-1085).

#6 [high] Research summary

I reviewed PR #641 through the subtraction/simplicity/conventions lens: the two production files at head and on main, host_memory.py, compilation.py, bootstrap.py, memory_inventory.py, tuning.py, the surrounding tests, and the relevant AGENTS.md and docs/preparation-development.md rules.

The diff is small and contains no new flag, config key, env var, dependency, abstraction or artifact change; aot.py (+2/−1) is lean and correct in shape. The one high-value subtraction is in compiler_resources(): the PR widens the empty_cache() trigger from "one worker does not fit" to "the whole worker budget does not fit" (compile_pool.py:55-56), which on unified memory thresholds 8 × 34 GiB = 272 GiB against a 121.7 GiB GB10 — so the flush becomes unconditional on every GB10 preparation and newly fires on discrete GPUs too, with no cost measurement, and the reported failure did not need it (the old trigger already fired at 243 MiB < 2048 MiB, because POSIX device_memory_info folds host RAM into free). Removing the widening leaves the actual fix — resampling available after the flush — untouched, removes a redundant second empty_cache() per rank (the PR's own new aot.py:1070 flush already precedes the barrier), and makes ~4 of the 5 new mocked tests unnecessary.

Second suggestion: the shared-memory sample/clamp is written three times (compile_pool.py:51, 53-54, 58-60), re-reads /proc/meminfo four times instead of two, is a no-op on POSIX where all live evidence was gathered, and leaves available stale on discrete GPUs while capacity grows. One small sample() closure collapses ten lines to six and fixes the asymmetry.

I also noted that _DEFAULT_MAX_WORKERS only earns its extraction from the widening, and listed what should explicitly not be touched. Details and verbatim citations are in my scratchpad notes (entries 1-5).

#7 [high] PR #641 — report skeleton: verdict, both suggestions, verbatim quotes, and what is lean

PR #641 (nero-, head d82fcfc, base 0a8c20f, +163/−10, 3 files) — Subtraction/Simplicity/Conventions

VERDICT: The diff is small, contains no new flag/config key/env var/dependency/abstraction/artifact change, and ignition/preparation/aot.py (+2/−1) is lean. TWO high-value subtractions, both in compiler_resources(); everything else is a note.

S1 (highest value) — Delete the widened flush trigger at compile_pool.py:55-56; it is unmeasured extra scope and not needed for the reported bug.

Head, ignition/preparation/compile_pool.py:46-63 (verbatim):

    shared = torch.cuda.get_device_properties(device).is_integrated
    device_bytes = _WORKER_DEVICE_BYTES if shared else _DISCRETE_WORKER_DEVICE_BYTES
    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()
        free = device_memory_info(device)[0]
        if shared:
            available = min(host_memory.available_bytes(), free)
    capacity = max(0, (free - headroom_bytes) // device_bytes)

Main, compile_pool.py:49-52 (the line being replaced):

    if free - headroom_bytes < device_bytes:
        torch.cuda.empty_cache()
        free = device_memory_info(device)[0]

Replace :55-56 with:

    if (available - headroom < device_bytes if shared
            else free - headroom_bytes < device_bytes):

and delete line 50 (max_workers = ...) entirely.

Why (4 points):

  1. Not needed for the reported defect. POSIX integrated folds host RAM into free — host_memory.py:107-109 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()) — so the reported "8435 MiB available, 8192 MiB headroom" gives 8435−8192 = 243 MiB < 2048 MiB (_WORKER_DEVICE_BYTES, compile_pool.py:23) and the OLD trigger already fired. The entire bug was the missing resample of available, which :59-60 fixes and the narrow trigger preserves.
  2. Unconditional on the target hardware. max_workers = 8 (compilation.py:30 'compile_threads': min(16, os.cpu_count() or 1); compile_pool.py:43-45 then :50 → min(...,8)); worker_bytes = FORWARD_WORKER_BYTES 32 GiB (host_memory.py:15) + 2 GiB = 34 GiB; threshold = 272 GiB vs 121.7 GiB total on GB10 ⇒ available - headroom < 272 GiB holds for ANY headroom ≥ 0. So empty_cache() runs on every GB10 preparation.
  3. Newly flushes discrete GPU hosts. Threshold becomes max_workers × 1 GiB (_DISCRETE_WORKER_DEVICE_BYTES, :25) vs main's 1 GiB + headroom — e.g. the pre-existing test at tests/preparation/test_compile_pool.py:39-41 (12 GiB free, 2 GiB headroom, compile_workers=32) now flushes where it never did, silently (test still passes). No cost measurement anywhere in the PR description.
  4. Double-flushes every rank. aot.py:1069-1074 (release_unused(); torch.cuda.empty_cache() then dist.barrier(group=group)) is the PR's own new pre-barrier flush; with the 272 GiB threshold, compiler_resources at aot.py:1076 flushes a second time on the same rank immediately afterward. The barrier-ordering bug the PR describes is solved entirely by aot.py:1070 + the resample.

AGENTS.md:188-193: "Reject unsupported abstractions and churn. … Refactoring must directly support the requested behavior or remove a demonstrated source of defect or cost." The widening is a behaviour change beyond the reported defect, on both hardware classes, with no measurement.

Tests to delete with it (all purely additive, +147, tests/preparation/test_compile_pool.py:538-681):

S2 — Collapse the three shared-memory branches in the same function (:51, :53-54, :58-60) into one sample helper.

Verbatim duplication (compile_pool.py:51-60):

    available = host_memory.available_bytes()
    free = device_memory_info(device)[0]
    if shared:
        available = min(available, free)
    if (...):
        torch.cuda.empty_cache()
        free = device_memory_info(device)[0]
        if shared:
            available = min(host_memory.available_bytes(), free)

Replace with:

    def sample():
        free = device_memory_info(device)[0]
        return free, (min(host_memory.available_bytes(), free) if shared
                      else host_memory.available_bytes())
    free, available = sample()
    if (...):
        torch.cuda.empty_cache()
        free, available = sample()

Three concrete gains, no behaviour change on POSIX/Windows-integrated:

Cost evidence for the extra reads: available_bytes() (host_memory.py:82-100) reads /proc/meminfo and calls _cgroup_available_bytes() (host_memory.py:51-79: /proc/self/cgroup + /proc/self/mountinfo per ancestor) ⇒ 3 file reads/call. main: 2 calls (compile_pool.py:45 + the one inside device_memory_info at :46). head when the flush fires: 4 calls (:51, inside :52, inside :58, :60) — and per S1 the flush is the normal path on GB10.

Minor notes (not blockers)

Already lean — do not touch

Conventions check (structural only)